tradefloor 0.11.0

A reproducible evaluation environment for financial AI agents.
Documentation
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
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
//! The engine — WP5. Where the port becomes something you can run.
//!
//! # What this owns, and what it deliberately does not
//!
//! It owns the seeded generator, the per-company price state, the economy and
//! the central bank. That is all.
//!
//! Events, AI decisions, whale trades, corporate actions and earnings stay in
//! the embedder and cross this boundary as **data** — news impulses and
//! impact-queue entries, not behaviour. That split is not a simplification;
//! it is what makes the boundary tractable. Whale events, for instance, reach
//! the price only through the generic news channels, so there is nothing to
//! port for them.
//!
//! # The streams are split, and that is the 2026-08 era boundary
//!
//! The reference ran every consumer — market, economy, microstructure,
//! embedder — off ONE PCG32 stream, so changing what any consumer drew
//! shifted every draw every other consumer saw afterwards. This engine
//! instead derives three independent substreams from the root seed
//! ([`crate::rng::stream`] documents the derivation contract):
//!
//! - **market** — everything inside `simulate_market_tick`, settlement
//!   included. With [`SettleDrawPolicy::FourAlways`] its schedule is a pure
//!   function of (status, active set, sector count): no price, macro value
//!   or order flow can move its position.
//! - **economy** — the daily macro chain, whose draw count genuinely
//!   depends on macro state. Its branches stay its own problem now.
//! - **external** — [`Engine::draw_uniform`] / [`Engine::draw_normal`]:
//!   seed-derived, reproducible, and incapable of perturbing the market.
//!
//! One seed still fully determines the whole simulation. What the split
//! buys is the counterfactual: vary the order flow (TCA), the macro path
//! (pinned-versus-baseline), or the embedder's own consumption (cutover),
//! and every other domain's sequence is bit-identical.
//!
//! Each stream keeps its own Box-Muller spare, inside its own `GameRng` —
//! the parity of normal draws is per-stream state and never crosses
//! between domains.
//!
//! Replaying a PRE-SPLIT recorded stream is still possible:
//! [`Engine::tick_with`] and [`Engine::advance_day_with`] take an external
//! draw source and consume it in the reference's shared-stream order,
//! settlement's four-or-zero included.
//!
//! # Columnar access
//!
//! State is held internally as `Vec<TickCompany>` — array-of-structs — because
//! that is what WP4's gated tick operates on, and re-shaping it would
//! invalidate 18,720 verified values for a layout preference.
//!
//! The FFI surface is columnar instead ([`Engine::prices`],
//! [`Engine::write_prices`]): one contiguous `f64` slice per field, which
//! crosses a WASM boundary as a single view rather than 108 marshalled
//! objects. Conversion happens when the embedder reads, not per tick, so the
//! cost is paid once per boundary crossing rather than 390 times a day.

use crate::economy::{
    Decision,
    check_cycle_transition_for, update_central_bank_with, update_economy_daily, CentralBankState,
    DailyInputs, EconomicShock, EconomyState,
};
use crate::market::{
    close_day_with, get_market_status, intraday_fraction, reset_daily_prices,
    simulate_market_tick, AvgVolumePolicy, CloseInputs, GameTime, MarketStatus,
    MarketVarianceState, NewsEvent, NewsImpactEntry, OrderVolume, SettleDrawPolicy, TickCompany,
    TickInputs,
};
pub use crate::opening_cache::OpeningCacheInfo;
use crate::params::ModelParams;
use crate::rng::{stream, DrawKind, DrawOverlay, DrawRecord, GameRng, Rng, RngState, Site};

/// The index, the live VIX and the forecast: what the derivatives read
/// (`index_level_listed`, `vix_intraday_live`, `forecast_horizon_sessions`).
mod foundations;

/// The index futures and their night session (`futures_index_listed`,
/// `night_session_steps`).
mod futures;
mod vix_futures;
mod rate_futures;
mod oil_futures;
mod margin;
/// The index options, their surface and their dealer (`options_index_listed`,
/// `surface_ssvi`, `option_dealer_spread`).
mod options;
/// The single-name options, their surfaces, exercise and delivery
/// (`options_names_listed`, `surface_name_premium`, `surface_name_skew`).
mod name_options;

/// The reference MAIN stream's sequence. Not 0 and not 1 —
/// both are different streams from the same seed, and picking the wrong one
/// produces a plausible market that matches nothing.
///
/// Since the 2026-08 stream split the ENGINE no longer seeds itself here —
/// it derives per-domain substreams instead (`crate::rng::stream`). The
/// constant remains because it is what a pre-split recording was produced
/// with: a replay harness reconstructing the reference's generator needs
/// `GameRng::new(seed, MAIN_STREAM)`, exactly as before.
pub const MAIN_STREAM: u32 = 99;

/// A fading VIX target premium under this many points is cleared to 0.0
/// ([`Engine::set_vix_target_premium`]), so a faded event stops being state
/// a snapshot carries.
pub const VIX_TARGET_PREMIUM_CLEARED_UNDER: f64 = 1e-6;

/// `Engine::macro_pins_today`: the VIX was pinned today.
pub const PIN_VIX: u16 = 1;
/// `Engine::macro_pins_today`: the corporate yield was pinned today.
pub const PIN_CORPORATE: u16 = 2;
/// `Engine::macro_pins_today`: the cycle phase was pinned today. Kept only
/// under `macro_pins_hold`, where the close holds the phase, or
/// `cycle_nowcast_accuracy`, where the market's belief was put on the pinned
/// phase (a pin is public news) and tonight's close takes no report, so the
/// belief holds on the pin through the close.
pub const PIN_CYCLE: u16 = 4;
/// The 10-year was pinned today (`macro_pins_hold`).
pub const PIN_T10: u16 = 8;
/// The 2-year was pinned today (`macro_pins_hold`).
pub const PIN_T2: u16 = 16;
/// The policy rate was pinned today (`macro_pins_hold`).
pub const PIN_POLICY: u16 = 32;
/// Any of the other fields `pin_macro` writes was pinned today
/// (`macro_pins_hold`); which ones is read off the economy's own copy of
/// them at the start of the close, since nothing between a pin and the
/// close writes any of them.
pub const PIN_INFLATION: u16 = 64;
pub const PIN_GROWTH: u16 = 128;
pub const PIN_UNEMPLOYMENT: u16 = 256;
pub const PIN_FEAR_GREED: u16 = 512;
pub const PIN_OIL: u16 = 1024;
pub const PIN_QE_PE: u16 = 2048;
pub const PIN_QE_ASSETS: u16 = 4096;
pub const PIN_TARIFF: u16 = 8192;
/// Every mark `macro_pins_hold` keeps.
pub const PIN_ALL: u16 = 0x3fff;
/// The corporate SPREAD over the 10-year was pinned today
/// (`pin_macro(corporate_spread=...)`): kept whatever the dials, since only
/// that pin sets it.
pub const PIN_SPREAD: u16 = 0x4000;

/// The fewest steps, in minutes of the engine's clock (a step is a day of
/// 24 * 60), between the last central-bank meeting and an intermeeting one
/// (`fed_put_emergency_vix`): 21, a month of sessions on the session
/// calendar pt-v20 runs, so one stressed month calls at most one.
pub const FED_PUT_EMERGENCY_GAP_MINUTES: i64 = 21 * 24 * 60;

/// The sessions over which `fed_drawdown_hold` reads the index's highest
/// close: a year.
pub const DRAWDOWN_WINDOW: usize = 252;

/// The stress hold's clock before any stressed close (`fed_stress_hold`):
/// longer than any hold the dial allows, and the ceiling the clock ages to.
pub const STRESS_HOLD_NEVER: f64 = 1.0e9;

/// The exact position of the engine's ten random streams, the checkpoint
/// half that cannot be reconstructed from the columns.
///
/// One [`RngState`] per stream, because each stream has its own LCG
/// position AND its own Box-Muller spare. A checkpoint that carried only
/// some of them would restore a market whose untouched domains replay
/// correctly and whose missing ones silently start a different sequence.
///
/// # Word order is not field order
///
/// [`EngineRngState::to_words`] and [`EngineRngState::from_words`] lay the
/// streams out by stream id ([`crate::rng::stream`]), which is the order of
/// [`EngineRngState::STREAM_NAMES`]:
///
/// | words | stream | id |
/// |---|---|---|
/// | 0..5 | `market` | 0 |
/// | 5..10 | `economy` | 1 |
/// | 10..15 | `external` | 2 |
/// | 15..20 | `jumps` | 3 |
/// | 20..25 | `volume` | 4 |
/// | 25..30 | `news` | 5 |
/// | 30..35 | `volume_idio` | 6 |
/// | 35..40 | `overnight` | 7 |
/// | 40..45 | `market_vol_level` | 8 |
/// | 45..50 | `crisis_epicentre` | 9 |
///
/// The fields below are declared with `volume_idio` before `news`, so a
/// host that packs the fields in declaration order puts those two streams
/// in each other's slots, and `from_words` restores them swapped without
/// complaint: both are valid streams. Pack with `to_words`, or by name with
/// [`EngineRngState::to_named_words`] and
/// [`EngineRngState::from_named_words`], which match streams by name and
/// cannot swap them.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct EngineRngState {
    /// Stream 0, words 0..5 of [`EngineRngState::to_words`].
    pub market: RngState,
    /// Stream 1, words 5..10.
    pub economy: RngState,
    /// Stream 2, words 10..15.
    pub external: RngState,
    /// The jump stream, stream 3, words 15..20. Carried here for the reason
    /// this type's own documentation gives: a stream left out of a
    /// checkpoint restores to a DIFFERENT sequence while looking correct.
    /// That is harmless while jumps are inert and silently wrong the day
    /// they are not.
    pub jumps: RngState,
    /// The persistent-volume stream, stream 4, words 20..25, carried for
    /// the same reason.
    pub volume: RngState,
    /// The per-name volume stream, carried for the same reason. Stream 6,
    /// words 30..35: AFTER `news` in [`EngineRngState::to_words`], though
    /// declared before it here.
    pub volume_idio: RngState,
    /// The endogenous-news stream, carried for the same reason. Left out,
    /// a restored engine would draw a different news sequence from the one
    /// it was checkpointed on, which is invisible while news is inert and
    /// silently wrong the day a preset switches it on. Stream 5, words
    /// 25..30: BEFORE `volume_idio` in [`EngineRngState::to_words`], though
    /// declared after it here.
    pub news: RngState,
    /// The overnight stream, stream 7, words 35..40, carried for the same
    /// reason.
    pub overnight: RngState,
    /// The market factor's slow-level stream, stream 8, words 40..45,
    /// carried for the same reason.
    pub market_vol_level: RngState,
    /// The crisis epicentre's stream, stream 9, words 45..50, carried for
    /// the same reason. It draws only on the session an unpinned episode
    /// starts, so a restore that dropped it would give the NEXT crisis a
    /// different epicentre from the one the parent would have drawn --
    /// invisible while `crisis_epicentre_extra` is 0.0 and silently wrong
    /// the day it is not.
    pub crisis_epicentre: RngState,
}

impl EngineRngState {
    /// Every stream in one flat array of
    /// [`ENGINE_RNG_STATE_WIDTH`](crate::ENGINE_RNG_STATE_WIDTH) numbers:
    /// stream `k` of [`crate::rng::stream`] (market 0, economy 1, external
    /// 2, jumps 3, volume 4, news 5, volume_idio 6, overnight 7,
    /// market_vol_level 8, crisis_epicentre 9) at words `k * W .. (k + 1) *
    /// W`, each as [`RngState::to_words`] writes it, where `W` is
    /// [`RNG_STREAM_WIDTH`](crate::RNG_STREAM_WIDTH). The order is the stream
    /// ids', [`EngineRngState::STREAM_NAMES`], not the fields': `news` (5)
    /// comes before `volume_idio` (6), which is declared first. A stream
    /// added later goes on the end.
    pub fn to_words(&self) -> [f64; crate::widths::ENGINE_RNG_STATE_WIDTH] {
        const W: usize = crate::widths::RNG_STREAM_WIDTH;
        let mut out = [0.0; crate::widths::ENGINE_RNG_STATE_WIDTH];
        for (k, s) in self.streams().iter().enumerate() {
            out[k * W..(k + 1) * W].copy_from_slice(&s.to_words());
        }
        out
    }

    /// Read back what [`EngineRngState::to_words`] wrote. Refuses a slice
    /// that is not exactly `ENGINE_RNG_STATE_WIDTH` long, and any stream
    /// [`RngState::from_words`] refuses, naming the stream.
    pub fn from_words(words: &[f64]) -> Result<Self, String> {
        const W: usize = crate::widths::RNG_STREAM_WIDTH;
        const N: usize = crate::widths::ENGINE_RNG_STATE_WIDTH;
        if words.len() != N {
            return Err(format!(
                "the engine's RNG state is {N} numbers ({} streams of {W}), got {}",
                N / W,
                words.len()
            ));
        }
        let read = |id: u32| {
            let k = id as usize;
            RngState::from_words(&words[k * W..(k + 1) * W])
                .map_err(|e| format!("stream {k}: {e}"))
        };
        Ok(EngineRngState {
            market: read(stream::MARKET)?,
            economy: read(stream::ECONOMY)?,
            external: read(stream::EXTERNAL)?,
            jumps: read(stream::JUMPS)?,
            volume: read(stream::VOLUME)?,
            news: read(stream::NEWS)?,
            volume_idio: read(stream::VOLUME_IDIO)?,
            overnight: read(stream::OVERNIGHT)?,
            market_vol_level: read(stream::MARKET_VOL_LEVEL)?,
            crisis_epicentre: read(stream::CRISIS_EPICENTRE)?,
        })
    }

    /// The streams' names in word order: entry `k` is the field whose
    /// stream [`EngineRngState::to_words`] writes at words
    /// `k * RNG_STREAM_WIDTH ..`. `news` comes before `volume_idio` here,
    /// the reverse of the fields' declaration order.
    pub const STREAM_NAMES: [&'static str; crate::widths::ENGINE_RNG_STREAMS] = [
        "market",
        "economy",
        "external",
        "jumps",
        "volume",
        "news",
        "volume_idio",
        "overnight",
        "market_vol_level",
        "crisis_epicentre",
    ];

    /// Every stream with its field's name, in word order: the same words as
    /// [`EngineRngState::to_words`], one block a stream. A host that saves
    /// the blocks under their names, in a map or a keyed record, and reads
    /// them back with [`EngineRngState::from_named_words`] never depends on
    /// the order.
    pub fn to_named_words(
        &self,
    ) -> [(&'static str, [f64; crate::widths::RNG_STREAM_WIDTH]); crate::widths::ENGINE_RNG_STREAMS]
    {
        let streams = self.streams();
        std::array::from_fn(|k| (Self::STREAM_NAMES[k], streams[k].to_words()))
    }

    /// Read streams back by name, in any order: each entry is a field's name
    /// from [`EngineRngState::STREAM_NAMES`] and the stream's words as
    /// [`RngState::to_words`] writes them. Refuses an unknown name, a name
    /// given twice, a stream left out, and any words
    /// [`RngState::from_words`] refuses, naming the stream.
    pub fn from_named_words(streams: &[(&str, &[f64])]) -> Result<Self, String> {
        const N: usize = crate::widths::ENGINE_RNG_STREAMS;
        let mut found: [Option<RngState>; N] = [None; N];
        for (name, words) in streams {
            let Some(k) = Self::STREAM_NAMES.iter().position(|n| n == name) else {
                return Err(format!(
                    "unknown RNG stream {name:?}; the streams are {:?}",
                    Self::STREAM_NAMES
                ));
            };
            if found[k].is_some() {
                return Err(format!("RNG stream {name:?} is given twice"));
            }
            found[k] = Some(
                RngState::from_words(words).map_err(|e| format!("stream {name}: {e}"))?,
            );
        }
        let missing: Vec<&str> = (0..N)
            .filter(|&k| found[k].is_none())
            .map(|k| Self::STREAM_NAMES[k])
            .collect();
        if !missing.is_empty() {
            return Err(format!("the RNG state is missing stream(s) {missing:?}"));
        }
        let s = |k: usize| found[k].expect("checked above");
        Ok(EngineRngState {
            market: s(stream::MARKET as usize),
            economy: s(stream::ECONOMY as usize),
            external: s(stream::EXTERNAL as usize),
            jumps: s(stream::JUMPS as usize),
            volume: s(stream::VOLUME as usize),
            news: s(stream::NEWS as usize),
            volume_idio: s(stream::VOLUME_IDIO as usize),
            overnight: s(stream::OVERNIGHT as usize),
            market_vol_level: s(stream::MARKET_VOL_LEVEL as usize),
            crisis_epicentre: s(stream::CRISIS_EPICENTRE as usize),
        })
    }

    /// The streams in stream-id order. An array literal, so a stream added
    /// to `stream::COUNT` without a place here fails to compile.
    fn streams(&self) -> [RngState; stream::COUNT] {
        const _: () = assert!(
            stream::MARKET == 0
                && stream::ECONOMY == 1
                && stream::EXTERNAL == 2
                && stream::JUMPS == 3
                && stream::VOLUME == 4
                && stream::NEWS == 5
                && stream::VOLUME_IDIO == 6
                && stream::OVERNIGHT == 7
                && stream::MARKET_VOL_LEVEL == 8
                && stream::CRISIS_EPICENTRE == 9
        );
        [
            self.market,
            self.economy,
            self.external,
            self.jumps,
            self.volume,
            self.news,
            self.volume_idio,
            self.overnight,
            self.market_vol_level,
            self.crisis_epicentre,
        ]
    }
}

/// Cumulative draws per stream. Diagnostic, per D-R1: the single most
/// useful numbers for locating a divergence, and deliberately not part of
/// any behavioural contract.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct StreamDraws {
    pub market: usize,
    pub economy: usize,
    pub external: usize,
}

impl StreamDraws {
    pub fn total(&self) -> usize {
        self.market + self.economy + self.external
    }
}

/// The draw positions of every stream at one day's open, with what the
/// market stream's per-tick schedule needs to address a company's normals.
///
/// Within one open-market tick the market stream takes, in this order: one
/// normal for the market factor, one normal per sector, then for each
/// active company one normal (the idiosyncratic factor) and one uniform
/// (stashed for phase 3), then four uniforms per active company at
/// settlement. So the normals per tick are `1 + sectors + active.len()`,
/// and active company `k` (its position in `active`) draws its normal at
/// `normals_at_open + tick * per_tick + 1 + sectors + k`. That arithmetic
/// is `Engine::market_day_layout`; the draw log is the check on it.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct DayMark {
    pub day: i64,
    /// `(uniforms, normals)` per stream, indexed by `crate::rng::stream`.
    pub positions: [(u64, u64); stream::COUNT],
    pub active: Vec<u32>,
    pub sectors: u32,
    pub ticks: u32,
}

/// A company's market-stream normals on one day, as a strided set:
/// `first + t * stride` for `t` in `0..ticks`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct MarketDayLayout {
    pub company: u32,
    pub first: u64,
    pub stride: u64,
    pub ticks: u32,
}

/// What the embedder supplies for one tick.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TickRequest<'a> {
    pub time: GameTime,
    /// Difficulty-driven noise multiplier.
    pub volatility_multiplier: f64,
    /// News reduced to the four fields the factor model reads.
    pub news: &'a [NewsEvent],
    pub news_impact_queue: &'a [NewsImpactEntry],
    /// Aggregated pending order volume, keyed by ticker.
    pub order_volumes: &'a [(String, OrderVolume)],
}

impl<'a> TickRequest<'a> {
    /// One tick at `time` with a volatility multiplier of 1.0, no news and
    /// no orders. Set `volatility_multiplier`, `news`, `news_impact_queue`
    /// or `order_volumes` on the value to add them.
    pub fn new(time: GameTime) -> Self {
        TickRequest {
            time,
            volatility_multiplier: 1.0,
            news: &[],
            news_impact_queue: &[],
            order_volumes: &[],
        }
    }
}

/// What one tick produced.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct TickOutcome {
    pub market_status: MarketStatus,
    /// Indices of the companies that were active, in processing order.
    pub active_indices: Vec<usize>,
    /// Fair value per active company — the book's anchor, NOT a price.
    pub fair_values: Vec<f64>,
    pub volumes: Vec<f64>,
    /// Draws consumed by this tick. Zero when the market was closed.
    pub draws_consumed: usize,
}

/// What an agent's order did when it met the book.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OrderReport {
    pub order_id: String,
    pub agent: String,
    pub ticker: String,
    pub side: crate::order_book::Side,
    pub requested: f64,
    /// Shares taken from the book on arrival.
    pub filled: f64,
    /// Volume-weighted across the taker fills, or `None` if none.
    pub average_price: Option<f64>,
    /// The last level the order reached, or `None` if it took nothing.
    pub worst_price: Option<f64>,
    /// The mid of the book the order met.
    pub reference: f64,
    /// Shares left waiting, in the queue or for the traded range.
    pub resting: f64,
    /// Shares of a market order the book could not fill. Zero for a limit
    /// order, whose remainder waits.
    pub unfilled: f64,
    /// How the remainder waits, when it does.
    pub mode: Option<crate::agent_book::RestMode>,
    /// The taker fills, one per level, in the order the order took them.
    pub fills: Vec<crate::agent_book::AgentFill>,
}

/// What the embedder supplies at the close of a simulated day.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DayCloseRequest<'a> {
    /// Per company, the day's accumulated `randomNoise` from factor
    /// attribution. `None` falls back to the day's total return.
    pub daily_innovations: &'a [Option<f64>],
    /// Per company, `sectorBaseDailyVariance(sector)`.
    pub sector_base_variances: &'a [f64],
    /// How the close treats `avg_volume`. `AvgVolumePolicy::Hold` -- the
    /// shipped default -- unless you are replaying a reference tape; the
    /// argument for the divergence lives on the policy itself.
    pub avg_volume: AvgVolumePolicy,
}

/// The live mark's refresh grid, in session minutes (`rate_intraday_live`):
/// the projection of tonight's curve is recomputed at the open, on the first
/// tick and on every tick whose minute of the session is a multiple of this.
/// Five lines up with `tf.evaluate`'s 65-minute steps.
pub const RATE_LIVE_REFRESH_MINUTES: i64 = 5;

/// The share of a session's market variance still to come after `elapsed`
/// of its 390 minutes, on the intraday profile the tick draws with.
fn session_variance_remaining(elapsed: i64) -> f64 {
    static PROFILE: std::sync::OnceLock<Vec<f64>> = std::sync::OnceLock::new();
    let tail = PROFILE.get_or_init(|| {
        // tail[k] = sum over minutes k..390 of the profile's variance.
        let n = 390usize;
        let mut tail = vec![0.0; n + 1];
        for i in (0..n).rev() {
            let v = crate::market::hours::intraday_vol(i as f64 / n as f64);
            tail[i] = tail[i + 1] + v * v;
        }
        tail
    });
    let k = elapsed.clamp(0, 390) as usize;
    if tail[0] > 0.0 { tail[k] / tail[0] } else { 0.0 }
}

/// The rest of a session's market move, integrated by eight equally likely
/// nodes: the means of a standard normal's eight equal-probability bins
/// (+-0.158, +-0.4913, +-0.8954, +-1.6468), rescaled so the rule's variance
/// is one. Three-point Gauss-Hermite with the factor's sd left the open's
/// expectation of the corporate yield 1.2 to 2.0 bp a day low on pt-v20, a
/// standing long-into-the-close edge; this rule on the index's sd leaves
/// -0.31 bp (r13 prototype).
fn live_mark_nodes() -> &'static [f64; 8] {
    static NODES: std::sync::OnceLock<[f64; 8]> = std::sync::OnceLock::new();
    NODES.get_or_init(|| {
        let bins = [-1.6468, -0.8954, -0.4913, -0.158, 0.158, 0.4913, 0.8954, 1.6468];
        let var: f64 = bins.iter().map(|x| x * x).sum::<f64>() / 8.0;
        let sd = crate::mathx::sqrt(var);
        let mut out = [0.0; 8];
        for (o, b) in out.iter_mut().zip(bins.iter()) {
            *o = b / sd;
        }
        out
    })
}

impl<'a> DayCloseRequest<'a> {
    /// A close under `AvgVolumePolicy::Hold`, the shipped default. Both
    /// slices take one entry per company, in roster order.
    pub fn new(daily_innovations: &'a [Option<f64>], sector_base_variances: &'a [f64]) -> Self {
        DayCloseRequest {
            daily_innovations,
            sector_base_variances,
            avg_volume: AvgVolumePolicy::Hold,
        }
    }
}

/// What the embedder supplies for the daily macro step.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DayAdvanceRequest<'a> {
    pub volatility: f64,
    pub active_shocks: &'a [EconomicShock],
    pub market_return_pct: f64,
    pub game_day: i64,
    /// Game timestamp in minutes, for the central bank's meeting calendar.
    pub timestamp: i64,
}

impl<'a> DayAdvanceRequest<'a> {
    /// The macro step as `Engine::advance_macro_day` would take it without
    /// a market: volatility 1.0, no active shocks and a market return of
    /// zero. Set `volatility`, `active_shocks` or `market_return_pct` on
    /// the value to change them.
    pub fn new(game_day: i64, timestamp: i64) -> Self {
        DayAdvanceRequest {
            volatility: 1.0,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day,
            timestamp,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct DayAdvanceOutcome {
    pub phase_changed: bool,
    pub meeting_held: bool,
    /// The action taken, when a meeting was held.
    ///
    /// `None` and `!meeting_held` are the same fact from two directions;
    /// `meeting_held` is kept because removing it would break callers for no
    /// gain. Before 0.4.2 this was computed, reduced to the boolean and
    /// discarded, so an embedder saw the rate move with nothing able to say
    /// why. Reconstructing it from the rate delta is guesswork:
    /// `StagflationHike` and `LaborEmergencyCut` are separated by the
    /// economic context that selected them, not by the size of the move.
    pub decision: Option<Decision>,
    /// Index into the six announcement variants, as `MeetingOutcome` reports
    /// it. Carried instead of the string, for the same reason as there: the
    /// draw is contractual and the prose is not.
    pub announcement_variant: Option<usize>,
    pub draws_consumed: usize,
}

/// One declared cash dividend: the name's roster slot and ticker, the
/// session it was declared on, the session it goes ex, and the amount per
/// share. See [`crate::market::dividends`].
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct Distribution {
    pub company: usize,
    pub ticker: String,
    pub declared_day: i64,
    pub ex_day: i64,
    pub amount: f64,
}

/// What of a re-mark's move is growth (`remark_growth_open_share`): none of
/// it, all of it (the aggregate level's close step), or each name's own part.
enum RemarkGrowth {
    None,
    All,
    Part(Vec<f64>),
}

/// Cloneable, and that is a correctness property rather than a convenience.
///
/// An in-memory fork is `self.clone()`. The alternative -- rebuilding a fresh
/// engine from a hand-written list of fields to copy -- was the mechanism, and
/// the list was incomplete five separate times: the per-day attribution
/// accumulators, the market-open flag, the market factor's variance, the
/// common log-volume state and the day counter were each added after a fork
/// diverged from the parent it was supposed to be a copy of. A derived clone
/// cannot omit a field, because a field added to this struct is copied
/// without anyone remembering to.
#[derive(Clone)]
pub struct Engine {
    /// True only while the economy runs alone before day zero (the
    /// stationary opening's draw and the macro burn-in), when the cycle
    /// reads `cycle_equity_hazard_opening` in place of an index it does not
    /// have. False in every session; not state, so nothing snapshots,
    /// restores or hashes it.
    economy_alone: bool,
    /// The root seed the streams were derived from. Kept only to key
    /// [`Engine::arrival_order`], which is a function of it and holds no
    /// state, so nothing snapshots, restores or hashes it: an engine is
    /// always built from its seed.
    root_seed: u64,
    market_rng: GameRng,
    economy_rng: GameRng,
    external_rng: GameRng,
    jump_rng: GameRng,
    volume_rng: GameRng,
    volume_idio_rng: GameRng,
    news_rng: GameRng,
    overnight_rng: GameRng,
    market_vol_level_rng: GameRng,
    crisis_epicentre_rng: GameRng,
    /// `stream::CYCLE_NOWCAST`: drawn only while `cycle_nowcast_accuracy` is set.
    cycle_nowcast_rng: GameRng,
    /// The opening mispricing draws, one standard normal per name of the
    /// roster the engine was built with and one more for the market's
    /// common level, from the one-shot [`stream::OPENING`]. Empty unless
    /// `opening_mispricing_sigma` or `opening_market_sigma` is non-zero,
    /// and emptied once the opening has been applied.
    opening_z: Vec<f64>,
    /// The mispricing each name opens at under the market's prehistory's
    /// valuation carry (`market_prehistory_valuation`), in roster order,
    /// NaN for a name the copy left without one: the copy's `s` at the end
    /// of its prehistory, which the opening takes in place of its draw.
    /// Empty unless the dial is on, and emptied with `opening_z` once the
    /// opening has been applied.
    opening_carry: Vec<f64>,
    /// The move each name's `s` took at the last open under the overnight
    /// process, in roster order, 0.0 where nothing moved. Per-day state
    /// like the attribution: the tape books it onto the day's first row.
    overnight_moves: Vec<f64>,
    /// What each name's `s` gave up to its fair value at the last open under
    /// a night split with a permanent share (`overnight_market_share` or
    /// `overnight_idio_share` beside `fair_value_news_share`), in roster
    /// order, 0.0 where nothing moved. Per-day output like `overnight_moves`
    /// and for the same reader: the tape books it onto the day's first row
    /// as `fair_value_shift`, so the columns sum to the change in `s` there.
    /// Not state, not hashed, not in the snapshot.
    overnight_fair_value_moves: Vec<f64>,
    /// What each name's `s` gave up to its fair value at the last close's
    /// jump, in roster order (`fair_value_shift`), 0.0 where nothing moved.
    /// Per-day state like `overnight_moves`: the tape books it onto the row
    /// where the jump is observed, beside the jump.
    jump_fair_value_moves: Vec<f64>,
    /// The move each name's `s` took at the last open's ex-date drop
    /// (`dividend_payout_share`), in roster order, 0.0 where nothing went
    /// ex. Per-day state like `overnight_moves`, and for the same reader:
    /// the tape books it onto the day's first row.
    dividend_moves: Vec<f64>,
    /// Every dividend declared so far, in declaration order. A record for
    /// the reader, like the tape: not state, not hashed, not in the
    /// snapshot. Empty on every preset.
    distribution_log: Vec<Distribution>,
    /// The earnings calendar's key (`rng::GameRng::earnings_key` of the
    /// seed the engine was built with): every report's date and draws are
    /// functions of it, the company's id and the quarter (`crate::earnings`).
    /// Derived at construction on every engine and read only with
    /// `earnings_surprise_sigma` set, which is also when the snapshot and
    /// the state hash carry it.
    earnings_key: u64,
    /// The key of the skew premium's noise draws (`surface_skew_noise_sd`),
    /// derived from the root seed and carried by a snapshot that holds it.
    skew_noise_key: u64,
    /// The skew premium's noise, skewness units (`surface_skew_noise_sd`):
    /// 0.0 with the dial off, which it never leaves.
    skew_noise: f64,
    /// The earnings surprise each name's opening print realised at the last
    /// open, in roster order, 0.0 where none did. Per-day output like
    /// `overnight_moves`, recomputed at every open and read by nothing.
    earnings_moves: Vec<f64>,
    /// The part of the aggregate earnings cycle's move each name's fair
    /// value is holding back until its next report
    /// (`earnings_cycle_report_share`), in roster order, as a log level.
    /// Zeros, and neither snapshotted nor hashed, unless that share, the
    /// calendar and the cycle are all on.
    earnings_withheld: Vec<f64>,
    /// Tonight's market-factor draw under a night split
    /// (`overnight_market_share`), per unit of beta, before the tilt: the
    /// part of `market_vol`'s day factor the night put there. The session's
    /// live lagged-wire condition (`market_beta_down_asym_lag_live`) reads
    /// the day factor LESS this, so the wire keys on the session's own draws
    /// and the night's gap does not set the session's drift. Zero at every
    /// close; snapshotted and hashed only while that wire reads it
    /// ([`Engine::carries_night_market_factor`]).
    night_market_factor: f64,
    /// Each name's last print at the most recent `close_market`, in roster
    /// order; the construction price before the first close. Read by
    /// nothing that prices, so it is outside the state hash and the draw
    /// schedule. See [`Engine::last_closes`].
    last_closes: Vec<f64>,
    /// `last_closes` as it stood at the most recent `open_market`: the
    /// close the current day's change is measured from. See
    /// [`Engine::prior_closes`].
    prior_closes: Vec<f64>,
    /// Whether construction ran the model's own opening (the macro burn-in
    /// or the stationary cycle draw) over the economy it was given. See
    /// [`Engine::opening_settled`].
    opening_settled: bool,
    /// The day's endogenous news, generated once in `open_market` (§117).
    /// A field rather than a local because a tick loop and a single
    /// `run_session` are two spellings of the same day and both must read
    /// the same events; when this was a local in `run_session` they did not.
    session_news: Vec<crate::market::NewsEvent>,
    /// The session minute each of `session_news` is released at
    /// (`news_session_release_share`), in the same order; empty while the
    /// dial is 0.0, and then nothing reads it. Per-day state like the
    /// events: snapshotted and hashed exactly while it is carried.
    session_news_release: Vec<f64>,
    /// The day's endogenous news released before the open
    /// (`news_preopen_share`): drawn with the day's news in `open_market`
    /// and priced by the night that follows it there, which empties it. So
    /// it is empty between calls and is neither snapshotted nor hashed.
    preopen_news: Vec<crate::market::NewsEvent>,
    companies: Vec<TickCompany>,
    economy: EconomyState,
    central_bank: CentralBankState,
    sector_keys: Vec<String>,
    /// The per-sector variance state as a RATIO to the VIX-coupled target
    /// (`tick::sector_sigma_at` squared): the sector's daily variance is
    /// `target * s`, `s` a GARCH(1,1) with unconditional mean 1.0 on the
    /// standardised daily factor. Live when `sector_vol_alpha` or `_beta`
    /// is set; 0.0 means "not yet seeded" and reads as 1.0.
    sector_variance: Vec<f64>,
    /// The day's accumulated sector factor per sector key, the shock the
    /// state updates on at the close; reset at each close.
    sector_day_factor: Vec<f64>,
    /// The target variance (`sector_sigma_at` squared) the day's sector
    /// draws were scaled by, recorded in the tick loop so the close
    /// standardises the day's factor by the scale it was drawn at, not by
    /// the target after the close moved the VIX. 0.0 until the first tick.
    sector_target_day: f64,
    /// The per-name jump excitation `h_i`, live when `jump_idio_excitation`
    /// is set: the idiosyncratic arrival rate is `lambda (1 + h_i)`.
    jump_excitation: Vec<f64>,
    /// The per-name idiosyncratic variance RATIO state
    /// (`ModelParams::idio_vol_alpha`): mean one, multiplying the variance
    /// of the name's own draw. 0.0 is unseeded and reads 1.0. Beside it, the
    /// name's own jump the last close applied (the next session realises it,
    /// so it enters that session's shock) with the jump variance expected at
    /// the rate it was drawn, and the same pair for tonight's close, which
    /// is empty between sessions. Never read or written while all three dials
    /// are 0.0, which every preset carries.
    idio_variance: Vec<f64>,
    idio_jump_pending: Vec<f64>,
    idio_jump_var_pending: Vec<f64>,
    idio_jump_today: Vec<f64>,
    idio_jump_var_today: Vec<f64>,
    /// Per-company attribution, accumulated across the current day.
    ///
    /// Four entries per company -- company_news, order_flow_impact,
    /// short_squeeze_effect, random_noise -- summed tick by tick and reset at
    /// `open_market`.
    /// Ten slots: the tick's eight `S_COMPONENT_KEYS`, the daily jump,
    /// which `apply_jumps` writes to `s` outside the tick loop (it was
    /// missing until 2026-08-26, so on any preset carrying jumps the truth
    /// table's components did not reconstruct the move, §74), and the
    /// overnight move, which `apply_overnight` writes to `s` at the open.
    attribution: Vec<[f64; crate::market::factors::COMPONENT_COUNT]>,
    /// The `random_noise` column split into market, sector and
    /// idiosyncratic, accumulated across the day beside `attribution` and
    /// reset with it, plus the day's summed square of the scale the
    /// idiosyncratic part was drawn at with the name's own sigma divided
    /// out (`kappa^2`).
    ///
    /// `random_noise` is what the close feeds the per-name GJR as the day's
    /// innovation, and the column alone cannot say how much of that
    /// innovation is the name's own variance rather than the factor's and
    /// the sector's. These say it. Reported through `noise_part_column`,
    /// and read on the price path only while
    /// `ModelParams::garch_innovation_commensurate` is non-zero -- at 0.0
    /// nothing reads them and no trajectory owes them anything. They are
    /// per-DAY accumulators and `state_snapshot` carries them, exactly as
    /// it carries the attribution they sit beside and for the same reason:
    /// a fork taken mid-day whose close read them at zero would feed the
    /// per-name GJR a different innovation and price differently from the
    /// parent it is meant to be a copy of.
    noise_parts: Vec<[f64; 3]>,
    noise_own_scale2: Vec<f64>,
    /// The day's innovation for each name's GJR under a night split
    /// (`overnight_market_share` or `overnight_idio_share` set): the night's
    /// own noise, then every tick's `random_noise`, summed in that order.
    ///
    /// The attribution counts the night once, in its `overnight` slot, so
    /// its twelve slots sum to the day's change in `s` and agree with the
    /// tape. The GJR steps on the whole day's noise, the night's included,
    /// and this keeps that sum in the order the `random_noise` slot summed
    /// it when it also carried the night, so the close feeds the GJR the
    /// same bits. Empty while the split is off, where the close reads the
    /// `random_noise` slot as before. Per-day state, sized from construction
    /// and snapshotted and hashed exactly while the split is on.
    innovation_day: Vec<f64>,
    /// This tick's ground truth, per company slot.
    ///
    /// `attribution` above sums across the day, which is what a scorer wants
    /// and is useless as a dataset: it can say order flow moved a price today
    /// but not WHEN, and a label that cannot be aligned to a bar is not a
    /// label. The per-tick figures were computed and thrown away; these keep
    /// them.
    ///
    /// Three different quantities that are easy to confuse, so they are named
    /// apart: `fundamental` is the valuation, `anchor` is
    /// `fundamental * exp(s)` -- the price the model wanted before the book
    /// touched it -- and the printed price is what the book actually settled.
    tick_components: Vec<[f64; crate::market::factors::TICK_COMPONENT_COUNT]>,
    tick_fundamental: Vec<f64>,
    tick_anchor: Vec<f64>,
    /// This tick's print decomposition, per company slot: the shock that
    /// arrived and the depth that absorbed it, in log units.
    ///
    /// `anchor` above is the price the model wanted and the print is what the
    /// book settled, so the gap between them was already computable. These
    /// two put the LAST print on the same footing, which is what a consumer
    /// reading a finished table cannot recover: the first tick of a day has
    /// no previous row to difference against.
    tick_shock: Vec<f64>,
    tick_absorbed: Vec<f64>,
    /// How far the print breaker moved this tick's print, per slot. The
    /// book's own share is `tick_absorbed - tick_clamp`.
    tick_clamp: Vec<f64>,
    /// How far each price was moved between its last print and this tick,
    /// per slot, in log units: `log(the price the tick started from / the
    /// last print)`. Zero on every tick nothing wrote the price between
    /// prints. `repriced + shock + absorbed` is the print's log move from
    /// the previous print, where `shock + absorbed` alone is its move from
    /// the price the tick started from.
    tick_repriced: Vec<f64>,
    /// The log move written to each name's price since its last print and
    /// not yet booked onto a tape row, per slot: the close's re-mark and a
    /// `pin_macro`'s (`macro_publication_repricing`) and the opening print
    /// (`overnight_variance_ratio`). The next tick moves it into
    /// `tick_repriced` and zeroes it. NaN where it is not known, which is
    /// after a `restore_state` on a model that can write a price between
    /// prints: the snapshot does not carry it.
    ///
    /// RECORDING state, like the `tick_*` columns: it moves no price and no
    /// draw, it is outside the snapshot and the state hash, and a fork
    /// (`Clone`) carries it.
    repriced_pending: Vec<f64>,
    /// The depth counterfactual, per company slot. Empty unless
    /// [`Engine::set_settle_depth_counterfactual`] turned it on, which is
    /// how the reporting surface knows whether it has an arm to report.
    tick_unbounded_print: Vec<f64>,
    tick_liquidity_share: Vec<f64>,
    /// Whether every open tick settles a second time against unbounded
    /// depth. Off by default, and inert to the market either way: see
    /// [`crate::market::TickInputs::settle_depth_counterfactual`].
    settle_depth_counterfactual: bool,
    /// The market factor's conditional-variance state
    /// (`market::factor_vol`). Advanced at every close from the day's
    /// accumulated factor — zero draws — and read by every generated tick
    /// as the factor's sigma. Recorded-stream replay (`tick_with`)
    /// bypasses it: that era's factor was constant-sigma.
    market_vol: MarketVarianceState,
    /// The universe's remembered stress, in VIX points above the crisis
    /// threshold. Ratchets up with a shock and decays geometrically, so an
    /// event's effect on the cross-section OUTLIVES the day it happened.
    /// Zero, and inert, under every preset before pt-v4.
    universe_stress: f64,
    /// Forced-flow budget spent, in VIX-point-days. 0.0 while the
    /// reservoir dial ships 0.0; see `ModelParams::forced_flow_reservoir`.
    forced_flow_spent: f64,
    /// The market factor's slow variance level, in logs. 0.0 means a
    /// multiplier of exactly 1.0, which is every preset through pt-v18,
    /// where `market_vol_level_sigma` is 0.0, and every shipped preset
    /// since the 2026-09-20 recomposition. A preset with a positive sigma
    /// moves this field every session; pt-v19 did from 2026-09-14 until
    /// that recomposition, when the level went back to 0.0.
    /// This line said "every preset through pt-v19" until 2026-09-18, and
    /// stopped being true when pt-v19 took the dial off zero. See
    /// `ModelParams::market_vol_level_sigma`.
    market_vol_log_level: f64,
    /// The VIX's own slow log-level, in logs; 0.0 means a multiplier of
    /// exactly 1.0 on what the VIX prices, which is every preset. Driven by
    /// the same normal `market_vol_log_level` reads, so it adds no draw.
    vix_log_level: f64,
    /// The anchor's slow memory of the read-back's log deviation. Moves only
    /// with `vix_anchor_memory` nonzero. See `ModelParams::vix_anchor_memory`.
    vix_anchor_slow: f64,
    /// The business cycle's multiplier on the market factor's volatility,
    /// in logs (`l` in `ModelParams::market_vol_cycle_ratio`). `None` until
    /// the first close with the dial set, which puts it at its phase's
    /// value; never touched with the dial at 0.0, where the snapshot and the
    /// state hash do not carry it.
    market_vol_cycle_log: Option<f64>,
    /// The time-held fade's clock (`market_vol_cycle_hold_onset`): the phase
    /// the last close traded in and the consecutive closes it has held,
    /// that close included. `None` before the first close with the dial set,
    /// and always with it at 0.0, where the snapshot and the state hash do
    /// not carry it.
    cycle_hold: Option<(crate::economy::CyclePhase, i64)>,
    /// The held VIX's slow log average (`market_vol_vix_hold_half_life`),
    /// kept from a contraction's or trough's first close past its onset;
    /// `None` otherwise, and always with the dial at 0.0.
    vix_hold_log: Option<f64>,
    /// The published VIX's stress-premium memory: the read-back's log
    /// deviation from the anchor's centre, kept at `vix_anchor_memory`'s
    /// rate and set to 0.0 by a pinned VIX. Moves only with
    /// `vix_stress_premium` non-zero, which no preset sets, and is carried
    /// by the snapshot and the state hash only then. See
    /// `ModelParams::vix_stress_premium` and [`Engine::published_vix`].
    vix_stress_memory: f64,
    /// The VIX's fear memory, in log units: the share of the VIX's
    /// excursion over its target that the target carries. Moves only with
    /// `vix_fear_uptake` non-zero, which no preset sets, and is carried by
    /// the snapshot and the state hash only then. See
    /// `ModelParams::vix_fear_uptake`.
    vix_fear: f64,
    /// The published VIX's log average the forecast's nowcast of the VIX's
    /// slow level reads (`forecast_vix_level_nowcast`). Kept only with that
    /// dial set, which no preset sets, and carried by the snapshot and the
    /// state hash only then; 0.0 before the first close.
    vix_log_average: f64,
    /// The host's premium on the VIX target, in VIX points, and the
    /// half-life in sessions it fades at (0.0 holds it). 0.0 with nothing
    /// written, where the target reads no term. See
    /// [`Engine::set_vix_target_premium`].
    vix_target_premium: f64,
    vix_target_premium_half_life: f64,
    /// The host's floor under the VIX target, held until cleared. See
    /// [`Engine::set_vix_target_floor`].
    vix_target_floor: Option<f64>,
    /// Whether a crisis EPISODE is running. The episode starts at the open
    /// of the first session whose VIX is above `crisis_vix_threshold` with
    /// no episode running, and ends after `crisis_epicentre_end_sessions`
    /// consecutive sessions back under it.
    ///
    /// False on every session of pt-v1 through pt-v18: the whole block is
    /// gated on `crisis_epicentre_extra`, which those presets set to 0.0.
    /// pt-v19 ships 1.93, so on the default this is true through every
    /// episode. See `ModelParams::crisis_epicentre_extra`.
    crisis_in_episode: bool,
    /// Consecutive sessions the running episode has spent under the
    /// threshold. Reset to zero by any session back above it, which is what
    /// makes one crisis one episode rather than a run of scattered days.
    crisis_sessions_under: i64,
    /// The sector index in `crate::sectors::SECTORS` this episode's
    /// epicentre was drawn at, or `-1` for `none` -- a crisis with no
    /// epicentre, which is two of the tape's five episodes and is a DRAW
    /// rather than the absence of one. Meaningless while
    /// `crisis_in_episode` is false, and written to `-1` when an episode
    /// ends so a snapshot carries no stale sector.
    crisis_epicentre: i32,
    /// The epicentre a scenario has PINNED, if any: `-1` for `none`, else a
    /// sector index. `Some` overrides the draw for every episode that
    /// starts while it is set, and a pinned episode takes NO draw, so a
    /// pinned run consumes nothing on `stream::CRISIS_EPICENTRE`.
    ///
    /// Not part of the episode state: it is an input to the run, written by
    /// `pin_macro` and carried by the snapshot for the same reason the
    /// pinned macro fields are -- a fork that dropped it would resume
    /// drawing its own epicentre part-way through a pinned experiment.
    crisis_epicentre_pin: Option<i32>,
    /// Tonight's close sets the market factor's variance from the VIX
    /// instead of stepping toward it. Written by `pin_macro` when a scenario
    /// forces the VIX with its `vix_sets_variance` switch on, and consumed
    /// by the next `close_market`, which clears it: one forced session, one
    /// set. A session nobody forced closes as a free one.
    ///
    /// False on every session of every run that does not ask for it, so no
    /// preset and no existing scenario ever reads it true. Carried by the
    /// snapshot and the state hash only while true, for the reason the
    /// epicentre pin is: a fork taken between the pin and the close that
    /// dropped it would close a forced session as a free one.
    vix_sets_variance_pending: bool,
    /// What a caller pinned today, for tonight's corporate yield
    /// (`economy::daily`, the corporate yield between meetings). Bit
    /// [`PIN_VIX`]: the close charges the corporate yield no VIX term, since
    /// the close's VIX move is the law's reversion from the written level,
    /// which the next pin discards. Bit [`PIN_CORPORATE`]: the pinned
    /// corporate yield holds through the close. Set only while
    /// `corporate_yield_daily` is on, cleared by the close, and carried by
    /// the snapshot and the state hash only while non-zero, as
    /// `vix_sets_variance_pending` is, so no engine that never pins, and no
    /// preset through pt-v19, hashes or snapshots differently.
    macro_pins_today: u16,
    /// The corporate spread over the 10-year a caller pinned today, in
    /// percent; read only while `PIN_SPREAD` is marked.
    pinned_corporate_spread: f64,
    /// The volatility-feedback discount today's VIX pins moved the index
    /// by, in log points at a beta of one (`pinned_vix_variance_share`):
    /// `fair_value_vix_discount` times the change the pins made to the
    /// smoothed exposure. Summed over the session's pins and cleared by the
    /// close; 0.0, never written, with the dial off. Carried by the snapshot
    /// and the state hash only while non-zero, as `macro_pins_today` is.
    pinned_vix_jump: f64,
    /// The tape's conditional mean on today's VIX pins (`pinned_vix_return`):
    /// `-b` times the VIX's change at each pin, in log points at a beta of
    /// one, summed over the session's pins and cleared by the close. The
    /// variance trim reads it in place of `pinned_vix_jump`. 0.0, never
    /// written, with the dial off; carried by the snapshot and the state hash
    /// only while non-zero.
    pinned_vix_move: f64,
    /// What the session's market draws carry of `pinned_vix_move`: the move
    /// less the discount the same pins priced (`pinned_vix_feedback`), so the
    /// pin's same-day total is the conditional mean once. Spread over the
    /// open session's ticks (`market::tick`). 0.0, never written, with the
    /// dial off.
    pinned_vix_mean: f64,
    /// The day's market t scale (`market_day_tail_df`): the multiplier on
    /// the market factor's variance for this session, drawn at the open
    /// and cleared to 1.0 by the close. Exactly 1.0, never written, with
    /// the dial off. Carried by the snapshot and the state hash only while
    /// it is not 1.0.
    market_day_scale: f64,
    /// Shared log-scale volume multiplier state. 0.0 means a multiplier of
    /// exactly 1.0, which is every preset before pt-v4.
    volume_state: f64,
    /// Per-NAME volume state, one per company (§107).
    volume_idio: Vec<f64>,
    /// The log jump each name's `s` took at the LAST close, one per
    /// company, carried across the open for the volume scale to subtract.
    ///
    /// Read only while `volume_move_jump_share` is off 1.0, and written
    /// only then, so every shipped preset leaves it all zeros and pays
    /// nothing for it. It is per-DAY state that outlives the open, which
    /// the attribution accumulator it is copied from does not: `apply_jumps`
    /// books the jump at the close of the day BEFORE the session that
    /// trades the gap in, and `open_market` clears the accumulator in
    /// between. Carried by `state_snapshot`, so a restored engine's first
    /// session reads the gap the copy's does; without it a restore lost one
    /// session of the mechanism.
    jump_move: Vec<f64>,
    /// Cumulative draws per stream, including any the embedder took through
    /// [`Engine::draw_uniform`]. The single most useful numbers for
    /// diagnosing a divergence: if these differ between two runs, nothing
    /// downstream is worth comparing.
    draws: StreamDraws,
    /// The day the engine is on, for the draw log and the day marks. Set by
    /// the caller that knows it (`set_current_day`), at each open, so the
    /// draws a close takes carry the day they close; zero on a fresh engine.
    /// A LABEL: the fills the book stamps and the draw log carry it, and
    /// nothing that prices reads it. The valuation reads `elapsed_days`.
    current_day: i64,
    /// Trading days elapsed when the current day opened: the clock the
    /// valuation reads, which is the buyback factor's elapsed time
    /// (`buyback_payout_share`). `set_current_day` sets it with the label,
    /// so a caller of the core sees one number as before; the Python
    /// binding sets the two apart, the label from `first_day` or `set_day`
    /// and this from its own day counter, so a label cannot reprice the
    /// market. They are equal on every run that numbers its days from the
    /// counter, which is every run that passes neither.
    elapsed_days: i64,
    /// The ticks run on the current day, carried across a restore. A
    /// restore drops the day marks, which is where the count normally
    /// lives (`ticks_today`), so a fill after it was stamped tick 0.
    /// `None` until a restore sets it, and ignored once a day mark exists.
    carried_ticks: Option<u32>,
    /// Each company's fair-value inputs as it was built or listed: earnings,
    /// book value and revenue growth per share. What `set_fundamentals`
    /// changes is measured against this, so a snapshot carries the three
    /// only once they have moved and every engine that was never told
    /// anything snapshots and hashes as it did before they were carried.
    base_fundamentals: Vec<[Option<f64>; 3]>,
    /// Each company's share count as it was built or listed. What
    /// `set_shares_outstanding` changes is measured against this, on the
    /// same rule as `base_fundamentals`: a snapshot carries the counts, and
    /// the state hash covers them, only once a host has moved one.
    base_shares: Vec<f64>,
    /// One mark per opened day: the day, the seven streams' draw positions
    /// at the open, the active company indices and the sector count, and
    /// the ticks the day ran. Together they map a `(day, company)` pair to
    /// its market-stream normal indices (`Engine::market_day_layout`).
    day_marks: Vec<DayMark>,
    /// Nominal output when this engine was built: `gdp * cpi` from the
    /// economy it was constructed with.
    ///
    /// The base of the growth term's ratio, so it is a constant of the run
    /// rather than state that advances, and it is carried in the snapshot
    /// for the reason every per-engine field is: an engine restored
    /// without it would rebase its valuation on the day it was restored
    /// and grow from there, which reads as a plausible market and is not
    /// the one the snapshot describes. Inert under every preset before
    /// pt-v18, where `earnings_nominal_growth` is 0.0 and nothing reads
    /// it.
    nominal_output_base: f64,
    /// The model coefficients this engine runs (the runtime seam).
    /// [`crate::params::PT_V1`] unless the engine was
    /// built with [`Engine::with_params`]; immutable for the engine's life,
    /// which is what lets its fingerprint be quoted for the whole run.
    params: ModelParams,
    /// `params.fingerprint()`, taken the first time anything asks and kept.
    ///
    /// The fingerprint hashes the whole preset surface and then every
    /// shipped preset to find a match, about a millisecond, and
    /// [`Engine::state_hash`] folds it in. The sandbox's tamper guard hashes
    /// the state twice around every call into agent code, so re-deriving it
    /// each time was about 90 per cent of the guard's cost and a tenth of an
    /// `evaluate`. `params` is fixed for the engine's life, so the first
    /// answer is the only answer; a clone carries it with the params it
    /// describes.
    model_fingerprint: std::sync::OnceLock<String>,
    /// The VIX at which every variance coupling reads ONE — the factor's
    /// forward map, the sector draw's sigma, the per-name GARCH clamp
    /// reference and the jump arrival rate.
    ///
    /// `params.market_vol_vix_anchor` under every preset before pt-v19, so
    /// every one of those four sites reads exactly the value it read
    /// before. Under [`ModelParams::vix_level_identity`] it is DERIVED at
    /// construction from the index's own unconditional variance
    /// ([`Engine::derive_vix_anchor`]) and the dial is not read at all.
    ///
    /// A field rather than a call, because it is a constant of the run: it
    /// depends on the roster and the parameters, and both are fixed when
    /// the engine is built.
    vix_anchor: f64,
    /// The index variance the LAST VIX update read, term by term.
    ///
    /// `None` until an `advance_day_with` has run under
    /// [`ModelParams::vix_level_identity`], and `None` for ever under
    /// every preset that leaves the identity off — because there the
    /// read-back is not computed at all and a zero here would be a
    /// variance of nothing rather than an absence.
    ///
    /// A REMEMBERED VALUE AND NOT A GETTER, and the difference is a day.
    /// `advance_day_with` computes this from post-close states at
    /// `VIX_{t-1}` — the sector sigma and the jump rate are read at the
    /// VIX the close saw — and then updates the VIX. Anything that
    /// recomputed the identity afterwards would read the instantaneous
    /// terms at `VIX_t` and hand back a variance no VIX update ever saw.
    ///
    /// Pure diagnostic: nothing in the engine reads it, and it is
    /// deliberately NOT in `state_snapshot`, so it moves no state hash and
    /// no digest. A FORK carries it, because a fork is a `Clone` and its
    /// last VIX update really was the parent's. `restore_state` does not,
    /// because the snapshot does not hold it: a restored engine keeps
    /// whatever reading it had of its own — `None` if it had advanced no
    /// day — until its own next day advances. That is the price of
    /// staying out of the hash, and it is stated rather than hidden.
    last_index_variance: Option<crate::market::index_var::IndexVarianceTerms>,
    /// The variance targets the last close reverted toward, `(fast, slow)`.
    /// Diagnostic only, like `last_index_variance`, and kept OUT of
    /// `state_snapshot` and the state hash for the same reason.
    last_market_targets: Option<(f64, Option<f64>)>,
    /// `rate_intraday_live`: the two projections of tonight's curve the rate
    /// indices' live mark differences, as (2-year, 10-year, corporate) in per
    /// cent: `[0..3]` on an empty session (the open's expectation), `[3..6]`
    /// on the session so far at the last refresh. Computed at the open and
    /// refreshed on the grid (`RATE_LIVE_REFRESH_MINUTES`); `None` outside a
    /// session, after a pin until the next tick, and always with the dial
    /// off. Carried in the snapshot and the state hash while the dial is set
    /// and this is `Some`, since a refresh reads the state at its own minute.
    rate_live: Option<[f64; 6]>,
    /// `index_level_listed`: the index's divisor, 0.0 until the first session
    /// opens, and its level on the last close's prints, 0.0 before the first
    /// close. Never touched with the switch off; carried in the snapshot and
    /// the state hash only while it is set. See `engine::derivatives`.
    index_divisor: f64,
    index_close: f64,
    /// `vix_intraday_live`: the projection of tonight's published VIX on the
    /// session so far, at the last refresh. `None` outside a session, after a
    /// pin until the next tick, and always with the switch off. Carried in the
    /// snapshot and the state hash while the switch is set and this is `Some`,
    /// since a refresh reads the state at its own minute.
    vix_live: Option<f64>,
    /// `forecast_horizon_sessions`: the forecast the last close computed.
    /// `None` before the first close and always with the dial off. Carried in
    /// the snapshot and the state hash while it is `Some`.
    forecast: Option<crate::derivatives::Forecast>,
    /// `futures_index_listed`: the index futures, their book, their
    /// settlements and their generator (`stream::DERIVATIVES`); the night's
    /// path under `night_session_steps`. Empty and never drawn from with the
    /// switch off; carried in the snapshot and the state hash only while it
    /// is set. See `engine::futures`.
    futures: futures::FuturesState,
    /// The VIX futures (`futures_vix_listed`): empty and never touched with
    /// the switch off; carried in the snapshot and the state hash only while
    /// it is set. See `engine::vix_futures`.
    vix_futures: vix_futures::VixFuturesState,
    /// The policy-rate and term-rate futures (`futures_rates_listed`): empty
    /// and never touched with the switch off; carried in the snapshot and the
    /// state hash only while it is set. See `engine::rate_futures`.
    rate_futures: rate_futures::RateFuturesState,
    /// The oil futures (`futures_oil_listed`): empty and never touched with
    /// the switch off; carried in the snapshot and the state hash only while
    /// it is set. See `engine::oil_futures`.
    oil_futures: oil_futures::OilFuturesState,
    /// The listed contracts' margin (`margin_scan_coverage`): empty and never
    /// touched with the dial off; carried in the snapshot and the state hash
    /// only while it is set. See `engine::margin`.
    margin: margin::MarginState,
    /// The index options and the surface's clock (`surface_ssvi`,
    /// `options_index_listed`): empty and never touched with both off;
    /// carried in the snapshot and the state hash only while `surface_ssvi`
    /// is set. See `engine::options`.
    options: options::OptionsState,
    /// The single-name options (`options_names_listed`): empty and never
    /// touched with the switch off; carried in the snapshot and the state
    /// hash only while it is set. See `engine::name_options`.
    name_options: name_options::NameOptionsState,
    /// `fed_stress_cut`: the highest published VIX since the last meeting.
    /// 0.0 and never touched with the cut off; carried in the snapshot and
    /// the state hash only while it is set.
    stress_vix_max: f64,
    /// `fed_stress_hold`: sessions since the last close whose published VIX
    /// was at or over `fed_stress_vix`, [`STRESS_HOLD_NEVER`] before the
    /// first. Never touched with the dial off; carried in the snapshot and
    /// the state hash only while it is set.
    stress_hold_age: f64,
    /// `fed_drawdown_hold`: the close-to-close log return of total public
    /// market cap over the last [`DRAWDOWN_WINDOW`] sessions, oldest first,
    /// from which the meeting reads the index's fall from its highest close
    /// in the window. Empty and never touched with the dial off; carried in
    /// the snapshot and the state hash only while it is set.
    drawdown_returns: std::collections::VecDeque<f64>,
    /// `fed_drawdown_hold`: total public market cap at the last close, the
    /// base of the next return. 0.0 before the first close.
    drawdown_mcap_prev: f64,
    /// `treasury_path_pricing`: the market's forecast of the policy rate's
    /// further change, `M`, percentage points: each change decayed at
    /// `treasury_path_half_life` sessions. 0.0 and never touched with the
    /// dial off; carried in the snapshot and the state hash only while it is
    /// set.
    policy_path: f64,
    /// `policy_anticipation`: the share of the next meeting's expected change
    /// the curve prices tonight, `P`, percentage points, signed. 0.0 and
    /// never touched with the dial off; carried in the snapshot and the
    /// state hash only while it is set.
    policy_anticipation_priced: f64,
    /// The rate instruments, if the embedder listed any
    /// ([`Engine::set_rate_instruments`]). Empty on every engine built
    /// without them, and an empty book is never touched: no hook runs, no
    /// column or buffer grows and nothing extra is hashed, so such an engine
    /// is the engine it was before rate instruments existed. They take no
    /// draws and write nothing back to the economy, so adding them leaves
    /// every equity price bit-identical. See `crate::rates`.
    rates: crate::rates::RateBook,

    /// The agent-facing book's state: consumed depth, agents' waiting
    /// orders, their taker flow not yet applied, and fills and impact rows
    /// not yet collected. See `crate::agent_book`.
    ///
    /// Pristine on every engine no agent has sent an order through, and a
    /// pristine state takes no part in a tick, the state hash or the
    /// snapshot, so every such run is the one it was before this existed.
    book: crate::agent_book::BookState,

    /// The phases the economy held at the last `cycle_publication_lag + 1`
    /// closes, oldest first; the front is the phase published
    /// ([`Engine::published_cycle_phase`]). Seeded at construction with the
    /// opening phase, so until the lag's worth of sessions has closed the
    /// opening phase is what an observer reads. Empty, never touched,
    /// unsnapshotted and unhashed while the dial is 0.0, which every preset
    /// through pt-v19 carries, so such an engine is the engine it was
    /// before this existed.
    cycle_history: std::collections::VecDeque<crate::economy::CyclePhase>,

    /// The market's belief over the five phases in `phase_cycle` order
    /// (`cycle_nowcast_accuracy`). Zero, never touched, unsnapshotted and
    /// unhashed while the dial is 0.0.
    cycle_nowcast: [f64; 5],

    /// The nowcast's and the spread blend's constants, fixed for the
    /// engine's life because `params` is: each phase's exit rate `lambda_j =
    /// min(1, 1 / mean_sojourn_j)` and the multiplier's occupancy mean
    /// `m_bar`. Derived once at construction while `cycle_nowcast_accuracy`
    /// or `corporate_spread_cycle` is set (zeros otherwise), since the
    /// sojourn walk is too dear to run at every close.
    cycle_nowcast_terms: ([f64; 5], f64),

    /// The GDP growth figure as published under `gdp_publication_lag`, and
    /// what it is computed from: the quarter being averaged and the
    /// quarters averaged and awaiting release. Seeded at construction with
    /// the opening growth. Default, never touched, unsnapshotted and
    /// unhashed while the dial is 0.0, which every preset through pt-v19
    /// carries.
    gdp_publication: GdpPublication,

    /// `D`, the accumulated expected drift of the anticipated earnings level
    /// the valuation leaves out (`earnings_anticipation_drift_share`), and
    /// `A - e` as the last refresh left it, which the next close adds to
    /// `D`. Zero, never touched, unsnapshotted and unhashed while the share
    /// is 0.0, which every preset carries.
    anticipation_drift: f64,
    anticipation_raw: f64,

    /// The business cycle's publication schedule under
    /// `cycle_publication_lag_draw`. Default, never touched, unsnapshotted
    /// and unhashed while the switch is 0.0, which every preset carries;
    /// only its key is set at construction.
    cycle_publication: CyclePublication,
    /// The key of the oil long factor's draws (`oil_target_drift_sd`),
    /// derived from the root seed and carried by a snapshot that holds the
    /// factor, so a restore onto an engine built from another seed draws
    /// the run's own.
    oil_drift_key: u64,
    /// The log of the oil price's long-term level (`oil_target_drift_sd`):
    /// 0.0 with the dial off, which it never leaves.
    oil_target_drift: f64,
    /// The key of core inflation's trend draws (`core_trend_sd`), derived
    /// from the root seed and carried by a snapshot that holds the process.
    core_trend_key: u64,
    /// Core inflation's slow trend, points over the 2.0 target headline
    /// reverts to (`core_trend_sd`): 0.0 with the dial off, which it never
    /// leaves.
    core_trend: f64,
    /// The key of credit's own state's draws (`corporate_spread_noise_sd`),
    /// derived from the root seed and carried by a snapshot that holds it.
    spread_noise_key: u64,
    /// Credit's own state, points of the spread formula's base
    /// (`corporate_spread_noise_sd`): 0.0 with the dial off, which it never
    /// leaves.
    spread_noise: f64,
    /// The key of the 10-year's own term premium's draws
    /// (`treasury_term_premium_sd`), derived from the root seed and carried
    /// by a snapshot that holds it.
    term_premium_key: u64,
    /// The 10-year's own term premium, percentage points added to its anchor
    /// (`treasury_term_premium_sd`): 0.0 with the dial off, which it never
    /// leaves.
    term_premium: f64,
    /// The aggregate earnings level after the last close's step
    /// (`fair_value_aggregate_jensen`, `fair_value_aggregate_half_life`):
    /// `None` before the first such close, and always with both off.
    aggregate_level: Option<f64>,
    /// The session's conditional variance of the aggregate earnings level's
    /// move (`fair_value_aggregate_expected`), computed at the open and read
    /// by the close's step: `None` before the first such open, and always
    /// with the switch off.
    aggregate_variance: Option<f64>,
    /// The earnings look-through's state (`earnings_lookthrough_half_life`):
    /// `m`, the log of the common multiplier `market.earnings` has written
    /// (`Engine::note_market_earnings`), and `a`, its anchor. Both 0.0 with
    /// the dial off, which they never leave, and in a run that never writes.
    lookthrough_written: f64,
    lookthrough_anchor: f64,
    /// The decision the Fed's own reading implied at the last close
    /// (`fed_eve_decision`), which the next scheduled meeting delivers;
    /// `None` before the first close with the switch on, and always with it
    /// off.
    eve_decision: Option<f64>,

    /// The background traders sharing the agent-facing book, when the
    /// engine was built with them (`crate::population`). `None` on every
    /// engine built without one, which then never calls into that module,
    /// so its market is the market it was before populations existed.
    population: Option<Box<crate::population::PopulationRun>>,
}

/// The state behind the published cycle phase under
/// `cycle_publication_lag_draw`: each true turn is published at its own
/// close, `pi_k = max(pi_(k-1), tau_k + L_k)`, `L_k` a stateless draw on the
/// turn's index (`rng::publication_uniform`).
///
/// Closes are counted from the opening (the construction, or a seeding),
/// and a turn is a close whose true phase differs from the last close's.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CyclePublication {
    /// The key of the draws, from the root seed (`rng::publication_key`).
    pub key: u64,
    /// The phase an observer reads: the latest turn published, or the
    /// opening phase before the first.
    pub published: crate::economy::CyclePhase,
    /// The true phase at the last close (or the opening), against which the
    /// next close's phase is a turn.
    pub last_true: crate::economy::CyclePhase,
    /// Closes since the opening.
    pub closes: i64,
    /// True turns since the opening: the next turn's draw index.
    pub turns: u64,
    /// Turns not yet published, oldest first: the close each is published
    /// on (non-decreasing), and the phase it turned into.
    pub pending: std::collections::VecDeque<(i64, crate::economy::CyclePhase)>,
}

impl Default for CyclePublication {
    fn default() -> Self {
        CyclePublication {
            key: 0,
            published: crate::economy::CyclePhase::Expansion,
            last_true: crate::economy::CyclePhase::Expansion,
            closes: 0,
            turns: 0,
            pending: std::collections::VecDeque::new(),
        }
    }
}

/// The publication lag of a turn into `phase` under
/// `cycle_publication_lag_draw`, from its uniform `u` in [0, 1): whole
/// sessions uniform in [84, 252] into a peak or a contraction (the NBER's
/// peak announcements, 4 to 12 months) and [168, 441] into a trough, a
/// recovery or an expansion (its trough announcements, 8 to 21 months).
pub fn cycle_publication_lag_of(phase: crate::economy::CyclePhase, u: f64) -> i64 {
    use crate::economy::CyclePhase;
    let (lo, hi) = match phase {
        CyclePhase::Peak | CyclePhase::Contraction => (84i64, 252i64),
        CyclePhase::Trough | CyclePhase::Recovery | CyclePhase::Expansion => (168i64, 441i64),
    };
    let span = (hi - lo + 1) as f64;
    let step = (u * span).floor() as i64;
    lo + if step > hi - lo { hi - lo } else if step < 0 { 0 } else { step }
}

/// The state behind the published GDP growth figure
/// (`gdp_publication_lag`), in the economy's percent.
///
/// Quarters are the macro calendar's own ([`crate::economy::MacroCalendar::days_per_quarter`]):
/// day `d` belongs to quarter `d.div_euclid(q)`, the quarter whose first
/// close takes the quarterly GDP step in `economy::daily`, and day 0, the
/// opening, belongs to quarter 0. A quarter's figure is the mean of the true
/// growth after each of its closes (the opening's value on day 0), and it is
/// released on the close `lag` sessions after the quarter's last day.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct GdpPublication {
    /// The figure an observer reads: the last quarter released, or the
    /// opening growth before the first release.
    pub published: f64,
    /// The quarter being averaged.
    pub quarter: i64,
    /// Closes averaged so far in that quarter.
    pub count: u32,
    /// Their growth, summed.
    pub sum: f64,
    /// Quarters averaged and not yet released, oldest first: the close on
    /// which each is released, and its figure.
    pub pending: std::collections::VecDeque<(i64, f64)>,
}

/// The draw source a shadow meeting reads: every uniform 0.5 and every
/// normal 0, so no stream is touched.
struct SilentDraws;

impl Rng for SilentDraws {
    fn next_f64(&mut self) -> f64 {
        0.5
    }
    fn next_normal(&mut self) -> f64 {
        0.0
    }
}

impl Engine {
    /// Derive the three engine streams from the root seed and take
    /// ownership of the state.
    ///
    /// # Both orderings are CONTRACTUAL
    ///
    /// `companies` is an ordered SEQUENCE, never a set or a mapping. The tick
    /// walks it in index order and draws per company as it goes, so reordering
    /// the roster produces a different market from the same seed. A
    /// well-meaning `sort_by(|a, b| a.ticker.cmp(&b.ticker))` anywhere upstream
    /// is a silent, total divergence — nothing will fail, the numbers will just
    /// be different ones.
    ///
    /// `sector_keys` must likewise be in `Object.keys(SECTOR_CONFIGS)` order:
    /// one normal is drawn per key, per tick, in that order.
    ///
    /// Roster SIZE is contractual too, and for the same reason. Adding a
    /// company does not append a name to an otherwise-unchanged market — draws
    /// scale with `n`, so every subsequent draw shifts and the whole market
    /// changes. A 30-name universe and a 100-name universe from one seed have
    /// nothing to do with each other.
    ///
    /// # The economy you pass is a starting point, not the opening
    ///
    /// This runs the default preset's opening over `economy` before day 1,
    /// as [`Engine::with_params`] does: on `pt-v20` a 755-day macro burn-in
    /// (`macro_burn_in_days`) and a draw of the cycle's phase and age
    /// (`cycle_stationary_opening`). A VIX, a policy rate or a phase you
    /// set is relaxed away. To keep the economy you pass, build with
    /// [`Engine::with_params_keeping_opening`].
    pub fn new(
        seed: u64,
        companies: Vec<TickCompany>,
        economy: EconomyState,
        central_bank: CentralBankState,
        sector_keys: Vec<String>,
    ) -> Self {
        Self::with_params(
            seed,
            companies,
            economy,
            central_bank,
            sector_keys,
            Self::default_model(),
        )
    }

    /// Advance the universe's remembered stress by one day.
    ///
    /// A ratchet with decay: today's stress enters immediately and in full,
    /// and what is already remembered decays geometrically. Asymmetric on
    /// purpose, because correlation is — it spikes with the shock and
    /// unwinds over weeks, rather than lagging the shock on the way in.
    ///
    /// `universe_stress_decay` is a per-day survival fraction, so 0.97 is a
    /// 23-day half-life and 0.0 means nothing survives the night, which is
    /// the behaviour of every preset before pt-v4.
    fn update_universe_stress(&mut self) {
        let threshold = self.params.crisis_vix_threshold;
        let from_vix = if self.economy.vix > threshold {
            self.economy.vix - threshold
        } else {
            0.0
        };
        // THE CYCLE, WHICH THE MARKET HAS NEVER READ. The engine runs a
        // five-phase business cycle and `cycle_phase` appears nowhere in
        // src/market/: the central bank changes its entire policy by
        // phase while the price process behaves as though the economy
        // were always expanding. A contraction now reaches the market the
        // same way a VIX spike does, and is remembered the same way.
        let from_regime = self.params.regime_stress_points
            * self.economy.cycle_phase.stress_intensity();
        let instant = crate::mathx::max(from_vix, from_regime);
        let remembered = self.params.universe_stress_decay * self.universe_stress;
        self.universe_stress = crate::mathx::max(instant, remembered);
    }

    /// The universe's remembered stress, for checkpoints and for anyone
    /// asking what the market is still carrying.
    pub fn universe_stress(&self) -> f64 {
        self.universe_stress
    }

    /// The common log-volume state, for checkpoints and forks.
    ///
    /// An AR(1) the whole roster multiplies its volume by. It was missing
    /// from the snapshot until 2026-08-26, which was invisible while the
    /// mechanism shipped switched off and became a divergence the day pt-v10
    /// turned it on: a restored engine traded different volume, which walks
    /// the book differently and prints different prices (§74).
    pub fn volume_state(&self) -> f64 {
        self.volume_state
    }

    /// Put the volume state back. See [`Engine::volume_state`].
    pub fn set_volume_state(&mut self, state: f64) {
        self.volume_state = state;
    }

    /// Put the remembered stress back. See [`Engine::universe_stress`].
    pub fn set_universe_stress(&mut self, stress: f64) {
        self.universe_stress = stress;
    }

    /// The per-name volume states, for checkpoints and forks.
    ///
    /// One AR(1) per company, walked at every tick. Inert under every preset
    /// through pt-v15 (`volume_idio_sigma` is 0.0), which is exactly the
    /// position [`Engine::volume_state`] was in before pt-v10 turned it on
    /// and a restored engine started trading different volume. Carried now,
    /// while carrying it is free.
    pub fn volume_idio(&self) -> &[f64] {
        &self.volume_idio
    }

    /// Put the per-name volume states back. See [`Engine::volume_idio`].
    ///
    /// # A width mismatch is refused rather than resized
    ///
    /// A snapshot written before issue #148 was fixed on 2026-09-02 holds
    /// this array at the width the engine was CONSTRUCTED with, because
    /// `add_company` and `remove_company` left it alone. Any such snapshot
    /// from a run that listed or delisted an instrument therefore carries a
    /// width its own roster disagrees with, and it fails here.
    ///
    /// Resizing it silently would be worse than the failure. The array is
    /// positional against the roster, so a pad or a truncation attaches each
    /// state to whichever company now sits at that index, and the restored
    /// market continues plausibly under states belonging to other names.
    /// The caller is told which two numbers disagree and where the mismatch
    /// came from.
    ///
    /// # Nothing stands when this refuses
    ///
    /// `PyEngine::restore_state` writes into a copy of the engine and keeps
    /// it only once every field has been read, so an engine that has caught
    /// this error holds the state it held before the call.
    /// `tests/test_volume_idio_roster.py` asserts that.
    pub fn set_volume_idio(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.volume_idio.len() {
            return Err(format!(
                "this snapshot carries {} per-name volume states and the \
                 roster holds {} companies. A snapshot written before issue \
                 #148 was fixed records the array at the width the engine \
                 was constructed with, so a run that listed or delisted an \
                 instrument saved a width its own roster disagrees with. \
                 The states are positional against the roster, so this \
                 restore is refused rather than padded or truncated. \
                 Reproduce the run from its seed and its order log to get a \
                 snapshot at the roster's width.",
                values.len(),
                self.volume_idio.len(),
            ));
        }
        self.volume_idio.copy_from_slice(values);
        Ok(())
    }

    /// The per-SECTOR variance state, for checkpoints and forks.
    ///
    /// A GARCH(1,1) on each sector factor, standardised by its own target, so
    /// its unconditional mean is exactly 1.0 and it multiplies the sector's
    /// variance. Zero-length before the sector table existed, and exactly
    /// 0.0 on every preset through pt-v18, where `sector_vol_alpha` and
    /// `sector_vol_beta` are both 0.0. pt-v19 turns it on, and until this
    /// was carried a restored engine continued the run with every sector
    /// back at its target while the state hash said the two markets were
    /// the same one.
    pub fn sector_variance(&self) -> &[f64] {
        &self.sector_variance
    }

    /// Put the per-sector variance states back. See
    /// [`Engine::sector_variance`].
    ///
    /// A width mismatch is refused rather than resized, for the reason
    /// [`Engine::set_volume_idio`] gives at length: the array is positional
    /// against the sector table, so a pad or a truncation attaches each
    /// state to whichever sector now sits at that index.
    pub fn set_sector_variance(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.sector_variance.len() {
            return Err(format!(
                "this snapshot carries {} per-sector variance states and                  this engine's sector table holds {}. The states are                  positional against that table, so the restore is refused                  rather than padded or truncated.",
                values.len(),
                self.sector_variance.len(),
            ));
        }
        self.sector_variance.copy_from_slice(values);
        Ok(())
    }

    /// The day's accumulated per-sector factor, for checkpoints and forks.
    ///
    /// Zero at a day boundary and non-zero between the open and the close,
    /// so it matters exactly where `attribution` and `tick_components`
    /// matter: a fork taken mid-day. See [`Engine::sector_target_day`] for
    /// its partner.
    pub fn sector_day_factor(&self) -> &[f64] {
        &self.sector_day_factor
    }

    /// The target variance the day's sector draws were scaled by.
    ///
    /// Recorded in the tick loop so the close standardises the day's factor
    /// by the scale it was DRAWN at rather than by the target after the
    /// close moved the VIX, which makes it per-day state and not a
    /// derivable constant. 0.0 until the first tick of a day.
    pub fn sector_target_day(&self) -> f64 {
        self.sector_target_day
    }

    /// Put the sector day accumulators back. Width refused as elsewhere.
    pub fn set_sector_day(&mut self, factor: &[f64], target: f64) -> Result<(), String> {
        if factor.len() != self.sector_day_factor.len() {
            return Err(format!(
                "this snapshot carries {} per-sector day factors and this                  engine's sector table holds {}. The accumulators are                  positional against that table, so the restore is refused                  rather than padded or truncated.",
                factor.len(),
                self.sector_day_factor.len(),
            ));
        }
        self.sector_day_factor.copy_from_slice(factor);
        self.sector_target_day = target;
        Ok(())
    }

    /// The per-NAME jump excitation state, for checkpoints and forks.
    ///
    /// A Hawkes-style self-excitation: a name that jumped carries a raised
    /// intensity that decays over the following sessions. 0.0 on every
    /// preset through pt-v18, where `jump_idio_excitation` is 0.0, and live
    /// on pt-v19. Carried for the reason every state beside it is: a
    /// checkpoint that dropped it resumed a market where nothing had ever
    /// jumped.
    pub fn jump_excitation(&self) -> &[f64] {
        &self.jump_excitation
    }

    /// Each name's log fair-value level `v`, in roster order, 0.0 where
    /// nothing has moved it. See `ModelParams::fair_value_news_share`. For
    /// checkpoints and forks; every preset through pt-v19 holds zeros.
    pub fn fair_value_offsets(&self) -> Vec<f64> {
        self.companies.iter().map(|c| c.stock.fair_value_offset.unwrap_or(0.0)).collect()
    }

    /// Put the fair-value levels back. A width mismatch is refused, as the
    /// jump excitation's is: the levels are positional against the roster.
    pub fn set_fair_value_offsets(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.companies.len() {
            return Err(format!(
                "this snapshot carries {} fair-value levels and the roster holds {} \
                 companies. The levels are positional against the roster, so this \
                 restore is refused rather than padded or truncated.",
                values.len(),
                self.companies.len()
            ));
        }
        for (c, &v) in self.companies.iter_mut().zip(values) {
            c.stock.fair_value_offset = if v == 0.0 { None } else { Some(v) };
        }
        Ok(())
    }

    /// The opening draws not yet applied: one standard normal per name of
    /// the roster the engine was built with and one for the market's common
    /// level, until the first open that is not closed takes them, and empty
    /// after it. For checkpoints and forks. `state_hash` has covered them
    /// since pt-v20 composed, and a snapshot that dropped them rebuilt a
    /// pre-open engine that opened at other draws and hashed apart.
    pub fn opening_z(&self) -> &[f64] {
        &self.opening_z
    }

    /// Put the unapplied opening draws back. Empty is a legal state (the
    /// opening has happened), and any other length must be the roster's
    /// plus one, the length the engine drew.
    pub fn set_opening_z(&mut self, values: &[f64]) -> Result<(), String> {
        if !values.is_empty() && values.len() != self.companies.len() + 1 {
            return Err(format!(
                "this snapshot carries {} opening draws and the roster holds {} \
                 companies, which draws {}. The draws are positional against the \
                 roster, so this restore is refused rather than padded or truncated.",
                values.len(),
                self.companies.len(),
                self.companies.len() + 1
            ));
        }
        self.opening_z = values.to_vec();
        Ok(())
    }

    /// The prehistory's carried opening mispricing not yet applied
    /// (`market_prehistory_valuation`): one per name of the roster, NaN for
    /// a name that takes its draw, until the first open that is not closed
    /// takes them, and empty after it or with the dial off. For checkpoints
    /// and forks, beside [`Self::opening_z`].
    pub fn opening_carry(&self) -> &[f64] {
        &self.opening_carry
    }

    /// Put the unapplied carried opening back. Empty is a legal state; any
    /// other length must be the roster's.
    pub fn set_opening_carry(&mut self, values: &[f64]) -> Result<(), String> {
        if !values.is_empty() && values.len() != self.companies.len() {
            return Err(format!(
                "this snapshot carries {} opening mispricings and the roster holds {} \
                 companies. They are positional against the roster, so this restore \
                 is refused rather than padded or truncated.",
                values.len(),
                self.companies.len()
            ));
        }
        self.opening_carry = values.to_vec();
        Ok(())
    }

    /// Whether this engine runs the per-name idiosyncratic variance state
    /// (`ModelParams::idio_vol_alpha`), which is when the snapshot and the
    /// state hash carry it. Off on every preset, whose two dials are 0.0.
    pub fn carries_idio_vol_state(&self) -> bool {
        self.idio_state_on()
    }

    /// The per-name idiosyncratic variance state, for checkpoints and forks:
    /// the ratio (0.0 unseeded), the own jump the last close applied and
    /// the jump variance expected at the rate it was drawn, each in roster
    /// order. Between sessions this is the whole state: tonight's pair is
    /// moved into the pending one at the close and is empty until the next.
    pub fn idio_vol_state(&self) -> (&[f64], &[f64], &[f64]) {
        (&self.idio_variance, &self.idio_jump_pending, &self.idio_jump_var_pending)
    }

    /// Put the per-name idiosyncratic variance state back. See
    /// [`Engine::idio_vol_state`]. Each vector must be the roster's width:
    /// the states are positional, so a mismatch is refused rather than
    /// padded or truncated.
    pub fn set_idio_vol_state(
        &mut self,
        variance: &[f64],
        jump: &[f64],
        jump_var: &[f64],
    ) -> Result<(), String> {
        let n = self.companies.len();
        for (name, v) in [("ratios", variance), ("pending own jumps", jump),
                          ("pending jump variances", jump_var)] {
            if v.len() != n {
                return Err(format!(
                    "this snapshot carries {} idiosyncratic variance {} and the roster \
                     holds {} companies. The states are positional against the roster, \
                     so this restore is refused rather than padded or truncated.",
                    v.len(), name, n));
            }
        }
        self.idio_variance = variance.to_vec();
        self.idio_jump_pending = jump.to_vec();
        self.idio_jump_var_pending = jump_var.to_vec();
        self.idio_jump_today = vec![0.0; n];
        self.idio_jump_var_today = vec![0.0; n];
        Ok(())
    }

    /// Whether this engine's model carries the volatility feedback's
    /// smoothed exposure, which is when the snapshot and the state hash
    /// carry it. Off on every preset through pt-v19; on for pt-v20.
    pub fn carries_vix_feedback(&self) -> bool {
        self.params.fair_value_vix_discount != 0.0 && self.params.fair_value_vix_half_life != 0.0
    }

    /// Whether this engine's model carries the market factor's return
    /// memory, which is when the snapshot and the state hash carry it: only
    /// with `market_vol_leverage` set. Off on every shipped preset.
    pub fn carries_market_vol_leverage(&self) -> bool {
        self.params.market_vol_leverage != 0.0
    }

    /// The market factor's return memory. See
    /// [`crate::params::ModelParams::market_vol_leverage`].
    pub fn market_vol_leverage_memory(&self) -> f64 {
        self.market_vol.leverage_memory()
    }

    /// Put the market factor's return memory back. Call after
    /// [`Engine::set_market_variance_state_with_components`], which resets
    /// it to 0.0.
    pub fn set_market_vol_leverage_memory(&mut self, value: f64) {
        self.market_vol.set_leverage_memory(value);
    }

    /// Whether this engine's model carries the market factor's fast return
    /// memory, which is when the snapshot and the state hash carry it: only
    /// with `market_vol_leverage_fast` set. Off on every shipped preset.
    pub fn carries_market_vol_leverage_fast(&self) -> bool {
        self.params.market_vol_leverage_fast != 0.0
    }

    /// The market factor's fast return memory. See
    /// [`crate::params::ModelParams::market_vol_leverage_fast`].
    pub fn market_vol_leverage_fast_memory(&self) -> f64 {
        self.market_vol.leverage_fast_memory()
    }

    /// Put the market factor's fast return memory back. Call after
    /// [`Engine::set_market_variance_state_with_components`], which resets
    /// it to 0.0.
    pub fn set_market_vol_leverage_fast_memory(&mut self, value: f64) {
        self.market_vol.set_leverage_fast_memory(value);
    }

    /// The index's conditional variance terms as the VIX's read-back takes
    /// them: [`Self::index_conditional_variance_terms_now`] exactly with
    /// `market_vol_leverage_fast` at 0.0 (the same f64 is passed), and off it
    /// at the factor variance with the fast return memory read over the
    /// VIX's window (`MarketVarianceState::vix_window_variance`).
    fn index_conditional_variance_terms_vix(
        &self,
    ) -> crate::market::index_var::IndexVarianceTerms {
        self.index_conditional_variance_terms_at(self.market_vol.vix_window_variance(&self.params))
    }

    /// Whether this engine's model carries the Fed put's state (the
    /// intermeeting return, the put's stock, what it owes and the previous
    /// close's market cap), which is when the snapshot and the state hash
    /// carry it: with `fed_put_gain` non-zero. Off on every preset.
    pub fn carries_fed_put(&self) -> bool {
        self.params.fed_put_gain != 0.0
    }

    /// Whether a meeting tonight holds any rise: within `fed_stress_hold`
    /// sessions of a stressed close, or, under `fed_drawdown_hold`, with
    /// credit's leverage gap (the index's log fall below its slow average)
    /// at or above that dial. False with both off.
    fn stress_hold_now(&self) -> bool {
        (self.params.fed_stress_hold != 0.0 && self.stress_hold_age < self.params.fed_stress_hold)
            || (self.params.fed_drawdown_hold != 0.0
                && self.index_drawdown() >= self.params.fed_drawdown_hold)
    }

    /// The index's log fall from its highest close of the last
    /// [`DRAWDOWN_WINDOW`] sessions (`fed_drawdown_hold`), read on total
    /// public market cap: 0.0 at a new high, and 0.0 with the dial off.
    pub fn index_drawdown(&self) -> f64 {
        let (mut level, mut high) = (0.0, 0.0);
        for r in self.drawdown_returns.iter() {
            level += r;
            high = crate::mathx::max(high, level);
        }
        high - level
    }

    /// Whether this engine keeps the drawdown's window of returns (and so
    /// the snapshot and both state hashes carry it): with
    /// `fed_drawdown_hold` or `market_vol_cycle_recovery_release` set. Off
    /// on every preset.
    pub fn keeps_drawdown_window(&self) -> bool {
        self.params.fed_drawdown_hold != 0.0 || self.params.market_vol_cycle_recovery_release != 0.0
    }

    /// Book tonight's close into the drawdown's window
    /// (`fed_drawdown_hold`): the log change of total public market cap
    /// since the last close, the oldest session dropped once the window is
    /// full. The first close only records its base. Nothing with the dial
    /// off.
    fn book_drawdown_close(&mut self) {
        if !self.keeps_drawdown_window() {
            return;
        }
        let mut mcap = 0.0;
        for c in self.companies.iter() {
            if c.is_public && !c.is_bankrupt {
                mcap += c.stock.market_cap;
            }
        }
        if self.drawdown_mcap_prev > 0.0 && mcap > 0.0 {
            self.drawdown_returns.push_back(crate::mathx::log(mcap / self.drawdown_mcap_prev));
            while self.drawdown_returns.len() > DRAWDOWN_WINDOW {
                self.drawdown_returns.pop_front();
            }
        }
        self.drawdown_mcap_prev = mcap;
    }

    /// Whether this engine's model carries credit's leverage gap
    /// (`EconomyState::spread_equity_gap`), which is when the snapshot and
    /// the state hash carry it: `corporate_spread_equity_gain` or
    /// `cycle_equity_hazard` set. Off on every preset.
    pub fn carries_spread_equity_gap(&self) -> bool {
        self.params.corporate_spread_equity_gain != 0.0 || self.params.cycle_equity_hazard != 0.0
    }

    /// Whether this engine's model carries the gap the 10-year's anchor
    /// prices (`EconomyState::treasury_equity_priced_gap`), which is when the
    /// snapshot and the state hash carry it: `treasury_equity_lag` and its
    /// gain set, with credit's leverage gap carried. Off on every preset.
    pub fn carries_treasury_equity_lag(&self) -> bool {
        self.params.treasury_equity_lag != 0.0
            && self.params.treasury_equity_lag_gain != 0.0
            && self.carries_spread_equity_gap()
    }

    /// Whether this engine's model carries the gap credit has priced
    /// (`EconomyState::spread_priced_gap`), which is when the snapshot and
    /// the state hash carry it: `corporate_spread_equity_lag` and
    /// `corporate_spread_equity_gain` both set. Off on every preset.
    pub fn carries_spread_priced_gap(&self) -> bool {
        self.params.corporate_spread_equity_lag != 0.0 && self.params.corporate_spread_equity_gain != 0.0
    }

    /// The Fed put's cut the curve prices tonight (`treasury_put_pricing`),
    /// percentage points: the share of the cut the put would ask for at a
    /// meeting now, no more than the policy rate, and 0.0 with inflation at
    /// or above the put's ceiling. 0.0 unless both dials are set.
    fn priced_fed_put(&self) -> f64 {
        let p = &self.params;
        if p.fed_put_gain == 0.0
            || p.treasury_put_pricing == 0.0
            || self.policy_inflation() >= crate::economy::central_bank::FED_PUT_INFLATION_CEILING
        {
            return 0.0;
        }
        let asked = crate::economy::central_bank::fed_put_ask(
            p.fed_put_gain, p.fed_put_threshold, self.economy.intermeeting_return);
        p.treasury_put_pricing
            * crate::mathx::min(asked, crate::mathx::max(0.0, self.economy.federal_funds_rate))
    }

    /// Whether this engine's model can move a fair-value level, which is
    /// when the snapshot and the state hash carry them. Off on every preset
    /// through pt-v19, so their snapshots and hashes are the ones they were.
    pub fn carries_fair_value_offsets(&self) -> bool {
        self.params.fair_value_news_share != 0.0
            || self.params.fair_value_market_share != 0.0
            || self.params.opening_mispricing_sigma != 0.0
            || self.params.opening_market_sigma != 0.0
            || self.params.earnings_surprise_sigma != 0.0
    }

    /// Whether this engine's model runs the variance cascade
    /// (`garch_cascade_components` at 1 or more), which is when the snapshot
    /// and the state hash carry its components. Off on every shipped
    /// preset, so their snapshots and hashes are the ones they were.
    pub fn carries_garch_cascade(&self) -> bool {
        self.params.garch_cascade_components >= 1.0
    }

    /// Every company's cascade components, `CASCADE_MAX` per name in roster
    /// order. For checkpoints and forks: the close updates them from the
    /// day's return, so a restore that dropped them ran every name's
    /// variance off another level (1.4 per cent in log price after twenty
    /// days at three components).
    pub fn garch_cascade(&self) -> Vec<f64> {
        self.companies
            .iter()
            .flat_map(|c| c.stock.garch_cascade.iter().copied())
            .collect()
    }

    /// Put the cascade components back, `CASCADE_MAX` per name. A width
    /// mismatch is refused, as the fair-value levels' is: the components
    /// are positional against the roster.
    pub fn set_garch_cascade(&mut self, values: &[f64]) -> Result<(), String> {
        let width = crate::market::garch::CASCADE_MAX;
        if values.len() != self.companies.len() * width {
            return Err(format!(
                "this snapshot carries {} variance cascade components and the \
                 roster holds {} companies of {width} each. The components are \
                 positional against the roster, so this restore is refused \
                 rather than padded or truncated.",
                values.len(),
                self.companies.len()
            ));
        }
        if let Some(v) = values.iter().find(|v| !v.is_finite()) {
            return Err(format!(
                "this snapshot's variance cascade holds {v}. A component is a \
                 variance, and a non-finite one would carry into every price \
                 its name prints."
            ));
        }
        for (c, chunk) in self.companies.iter_mut().zip(values.chunks_exact(width)) {
            c.stock.garch_cascade.copy_from_slice(chunk);
        }
        Ok(())
    }

    /// Put the per-name jump excitation back. See
    /// [`Engine::jump_excitation`]. A width mismatch is refused, on the
    /// reasoning in [`Engine::set_volume_idio`], and this write sits AFTER
    /// that one in `restore_state`, so the boundary that docstring
    /// describes is unchanged.
    pub fn set_jump_excitation(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.jump_excitation.len() {
            return Err(format!(
                "this snapshot carries {} per-name jump excitation states                  and the roster holds {} companies. The states are                  positional against the roster, so this restore is refused                  rather than padded or truncated.",
                values.len(),
                self.jump_excitation.len(),
            ));
        }
        self.jump_excitation.copy_from_slice(values);
        Ok(())
    }

    /// The day's endogenous news, for checkpoints and forks.
    ///
    /// Generated once in `open_market` and read by every tick of that day, so
    /// it is per-DAY state and not a per-tick input. A fork taken mid-day
    /// without it runs the rest of the day with the news missing and prices
    /// differently from the engine it forked from -- which is what happened,
    /// on the shipped default preset, until this was carried.
    pub fn session_news(&self) -> &[NewsEvent] {
        &self.session_news
    }

    /// Put the day's endogenous news back. See [`Engine::session_news`].
    pub fn set_session_news(&mut self, news: Vec<NewsEvent>) {
        self.session_news = news;
    }

    /// The session minute each of [`Engine::session_news`] is released at
    /// (`news_session_release_share`), in the same order; empty while the
    /// dial is 0.0.
    pub fn session_news_release(&self) -> &[f64] {
        &self.session_news_release
    }

    /// Put the day's release minutes back. See
    /// [`Engine::session_news_release`].
    pub fn set_session_news_release(&mut self, release: Vec<f64>) {
        self.session_news_release = release;
    }

    /// The preset an engine gets when the caller names none.
    ///
    /// One definition, because the alternative is what this replaced: the
    /// default written as a bare `PT_V1` at two call sites, where moving an
    /// era means finding both.
    ///
    /// Since 0.11.0 this is [`PT_V22`]: the preset that lists the contracts
    /// (index, rate and oil futures, and options on every name), graded as
    /// its record says (`tf.preset_record("pt-v22")`, `validation/pt-v22/`).
    ///
    /// From 0.10.0 until 0.11.0 it was [`PT_V21`]: pt-v20 with 104 dials moved, each
    /// inert on every earlier preset (see [`crate::params::ModelParams::pt_v21`]).
    /// It holds all fifteen rows of the fixed-roster panel at 252 days,
    /// fifteen of fifteen at 504, fifteen on both held-out axes, and all
    /// 40 long-run criteria, read on 270 histories
    /// (`validation/pt-v21/programme/ptv21-registration-18.md`). Volatility
    /// clustering at lag 1 reads 0.095 against a real median of 0.103, and
    /// the index falls 3 per cent or more on 0.98 per cent of days over 360
    /// seeds, inside the ruled band of 0.64 to 2.34. Its gaps: the crisis
    /// lever is 4.98x against a real 6.16x, and the VIX's persistence reads
    /// 0.956 against the tape's 0.930, which the structural sign test
    /// refuses.
    ///
    /// From 0.8.5 to 0.9.1 it was [`PT_V20`]: pt-v19 with a tape that follows the
    /// model price, a closing cross, the stock- and sector-specific part of
    /// every shock in fair value, the agent-facing book on, the curve dials
    /// and the aggregate earnings cycle. It holds all fifteen rows of the
    /// fixed-roster panel at 252 days, fifteen of fifteen at 504, fifteen
    /// on both held-out axes, and all 28 long-run criteria registered for
    /// it (`validation/pt-v20/programme/ptv20-registration.md`). It reads further
    /// from real on two rows: the crisis lever is 3.60x against a real
    /// 6.16x (pt-v19 5.22x), and on the level protocol the index returns
    /// +1.14 per cent a year, inside the ruled band of 1.1 to 10.3 at its
    /// floor (pt-v19 +7.65).
    ///
    /// In 0.8.0 and 0.8.1 it was [`PT_V19`]: pt-v18 with four dials moved and
    /// nothing else. It holds all fourteen shape rows at 252 and 504 days
    /// and on both held-out axes, as pt-v18 did, and every one of the
    /// fourteen at the real centre where pt-v18 held twelve. On the level
    /// protocol the index level returns +6.52 per cent a year against a
    /// band of 2.90 to 11.90 (pt-v18: +5.80), and the -3 per cent fear row
    /// reads 5.82 against a tape centre of 5.73, where pt-v18 read 3.25
    /// and sat a tenth of the way into its band. Nine of the ten graded
    /// mechanisms are shown, as on pt-v18.
    ///
    /// It reads FURTHER from real on one row: the crisis lever is 5.28x
    /// against real markets' 6.16x, where pt-v18 read 6.53x -- 14 per cent
    /// under real where pt-v18 was 6 per cent over. The VIX level identity
    /// reads the market's variance target against a derived anchor rather
    /// than the dial's, so a held VIX 65 is a smaller multiple of it.
    ///
    /// This constant and [`crate::params::DEFAULT_PRESET_NAME`] are the two
    /// things that decide the default, and a test at the bottom of
    /// `params.rs` asserts they agree. Moving one alone changes what the
    /// library REPORTS while every engine keeps running the other, which is
    /// the substitution that shipped once already: `model_preset()` answered
    /// "pt-v1" for runs executing pt-v3.
    ///
    /// Every earlier preset stays selectable and bit-reproducing, so
    /// anything recorded under one replays exactly by naming it.
    ///
    /// [`PT_V19`]: crate::params::PT_V19
    /// [`PT_V20`]: crate::params::PT_V20
    /// [`PT_V21`]: crate::params::PT_V21
    /// [`PT_V22`]: crate::params::PT_V22
    pub const fn default_model() -> crate::params::ModelParams {
        crate::params::PT_V22
    }

    /// [`Engine::new`] under an explicit model preset (the runtime seam).
    /// With [`crate::params::PT_V1`] this IS `new`: the
    /// preset-constructed engine reproduces the const build's trajectories
    /// bit for bit, draw for draw — the phase-1 acceptance gate.
    ///
    /// # It settles the opening, overwriting the economy you pass
    ///
    /// Before day 1 this runs the preset's own opening over `economy`:
    /// `macro_burn_in_days` days of the macro step (755 on `pt-v18` onward)
    /// and, where `cycle_stationary_opening` is on, a draw of the cycle's
    /// phase and age. Every macro field you set comes out as the model's
    /// dynamics left it. Measured on `pt-v20` (the test
    /// `with_params_settles_a_host_economy_and_keeping_opening_keeps_it`),
    /// an economy passed in at a VIX of 45 in a contraction opens at a VIX
    /// of 22.28 in an expansion. Presets before `pt-v18` have no burn-in, so
    /// this arrived in 0.7.0 under callers who had not changed their code.
    ///
    /// That is what you want when `economy` is
    /// [`crate::economy::create_initial_economy_state`]'s default, which
    /// would otherwise open every run in expansion at phase age zero. A
    /// host that owns the macro state, or restores one it saved, wants
    /// [`Engine::with_params_keeping_opening`] instead.
    /// [`Engine::opening_settled`] says afterwards which of the two an
    /// engine got.
    pub fn with_params(
        seed: u64,
        companies: Vec<TickCompany>,
        economy: EconomyState,
        central_bank: CentralBankState,
        sector_keys: Vec<String>,
        params: ModelParams,
    ) -> Self {
        Self::with_params_from_opening(seed, companies, economy, central_bank,
                                       sector_keys, params, true)
    }

    /// [`Engine::with_params`] when the economy you pass is the opening: the
    /// constructor for a host that owns the macro state.
    ///
    /// No burn-in runs and no cycle phase is drawn, so the engine opens on
    /// exactly the `economy` and `central_bank` given, and
    /// [`Engine::opening_settled`] reads `false`. The Python package does
    /// the same whenever a caller passes `macro_state`. It is
    /// [`Engine::with_params_from_opening`] with `settle_opening` false,
    /// under a name that says what it keeps.
    ///
    /// Use it when a host supplies its own starting economy, and when it
    /// rebuilds an engine from saved state. Use [`Engine::with_params`]
    /// when the economy is the library's default and the model should
    /// settle it.
    pub fn with_params_keeping_opening(
        seed: u64,
        companies: Vec<TickCompany>,
        economy: EconomyState,
        central_bank: CentralBankState,
        sector_keys: Vec<String>,
        params: ModelParams,
    ) -> Self {
        Self::with_params_from_opening(seed, companies, economy, central_bank,
                                       sector_keys, params, false)
    }

    /// Whether construction ran the model's own opening over the economy it
    /// was given.
    ///
    /// `true` when the engine was built with [`Engine::new`] or
    /// [`Engine::with_params`] (or `settle_opening` true) under a preset
    /// whose `macro_burn_in_days` or `cycle_stationary_opening` is non-zero,
    /// which is every preset from `pt-v18` on. Then the economy the engine
    /// opened on is not the one passed in. `false` when the opening was
    /// kept, or when the preset has nothing to settle.
    ///
    /// A host that supplies its own economy can assert on this once after
    /// construction, so a constructor swapped in a refactor fails loudly
    /// rather than quietly replacing the host's opening.
    pub fn opening_settled(&self) -> bool {
        self.opening_settled
    }

    /// [`Engine::with_params`], saying whether the opening is the model's to
    /// settle or the caller's to keep.
    /// [`Engine::with_params_keeping_opening`] is the `false` case by name.
    ///
    /// `settle_opening` is true for `with_params` and every path that takes
    /// the DEFAULT macro state, which is what `macro_burn_in_days` exists to
    /// fix: every run otherwise opens in expansion at phase age zero with the
    /// constructor's own field values, and the burn-in relaxes those into
    /// something a run can start from.
    ///
    /// It is FALSE when the caller supplied a macro state, because then the
    /// opening is a statement rather than an artefact. Measured at 0.7.0,
    /// when the default gained the dial: an engine asked for a VIX of 45.0
    /// and a policy rate of 5 per cent opened at 21.55 and 0.00 -- the
    /// burn-in had relaxed the request away over 755 days, and
    /// `Macro`'s own round-trip contract, that a value read back can be
    /// written straight in, was silently false.
    ///
    /// Settling and then restoring the named fields was considered and
    /// refused: the variance state the burn-in leaves behind tracks the VIX
    /// path it actually ran, so writing a crisis VIX back on top of it
    /// produces an engine whose volatility state and VIX disagree. Skipping
    /// is what a caller naming an opening asked for, and it is what every
    /// preset before pt-v18 did.
    ///
    /// # A prehistory is played once a process
    ///
    /// A build that plays a market prehistory (`market_prehistory_sessions`
    /// above zero with the opening settled, which is every `pt-v21` build
    /// that takes the default economy) is kept in a small process-wide
    /// cache, and a later build from the same arguments, to the bit, is a
    /// clone of it rather than a second prehistory. It cannot move a result:
    /// see [`Engine::opening_cache_info`] and
    /// [`Engine::set_opening_cache_capacity`].
    pub fn with_params_from_opening(
        seed: u64,
        companies: Vec<TickCompany>,
        economy: EconomyState,
        central_bank: CentralBankState,
        sector_keys: Vec<String>,
        params: ModelParams,
        settle_opening: bool,
    ) -> Self {
        if !(settle_opening && params.market_prehistory_sessions > 0.0) {
            return Self::build_from_opening(seed, companies, economy, central_bank,
                                            sector_keys, params, settle_opening);
        }
        let key = if crate::opening_cache::on() {
            crate::opening_cache::key(seed, &companies, &economy, &central_bank,
                                      &sector_keys, &params, settle_opening)
        } else {
            None
        };
        if let Some(engine) = key.as_ref().and_then(crate::opening_cache::get) {
            return engine;
        }
        let engine = Self::build_from_opening(seed, companies, economy, central_bank,
                                              sector_keys, params, settle_opening);
        if let Some(key) = key {
            crate::opening_cache::put(key, &engine);
        }
        engine
    }

    /// Hold at most `entries` engines in the opening cache, dropping the
    /// least recently used beyond that; 0 turns it off and empties it. It
    /// holds 16 until this is called.
    ///
    /// The cache keeps engines whose construction played a market
    /// prehistory, which on `pt-v21` is about 1.45 s of the build over 20
    /// names, so a second build from the same seed, roster, economy and
    /// model is a clone of the first. A clone is the same engine to the bit,
    /// so the setting moves no result, only the time a repeated build takes
    /// and the memory the kept engines hold: about 160 KB for a 20-name
    /// engine and 1.9 MB for a 500-name one. The kept engines hold 2,000
    /// names between them at most, whatever the count, and a build whose
    /// arguments hold a NaN is never kept.
    ///
    /// The cache is one per process and shared by every thread.
    pub fn set_opening_cache_capacity(entries: usize) {
        crate::opening_cache::set_capacity(entries);
    }

    /// What the opening cache holds and how many builds it has served
    /// (`hits`) or had to play (`misses`) since the process started. See
    /// [`Engine::set_opening_cache_capacity`].
    pub fn opening_cache_info() -> OpeningCacheInfo {
        crate::opening_cache::info()
    }

    /// [`Engine::with_params_from_opening`] without the opening cache: the
    /// build itself.
    pub(crate) fn build_from_opening(
        seed: u64,
        companies: Vec<TickCompany>,
        economy: EconomyState,
        central_bank: CentralBankState,
        sector_keys: Vec<String>,
        params: ModelParams,
        settle_opening: bool,
    ) -> Self {
        // THE ROSTER'S BETA, normalised before anything reads it
        // (`market_beta_normalise`). A branch, so at 0.0 no sum is taken and
        // every beta is the instrument's bit for bit.
        let companies = if params.market_beta_normalise == 0.0 {
            companies
        } else {
            normalise_roster_beta(companies, params.market_beta_normalise)
        };
        let companies_len = companies.len();
        // Read before the companies move into the struct, and never
        // rewritten: what `set_fundamentals` is measured against.
        let base_fundamentals = companies
            .iter()
            .map(|c| [c.eps, c.book_value_per_share, c.revenue_growth])
            .collect();
        let base_shares = companies.iter().map(|c| c.stock.shares_outstanding).collect();
        // Read before the economy moves into the struct, and never
        // recomputed: this is where the run's nominal output starts.
        let nominal_output_base = economy.gdp * economy.cpi;
        let mut engine = Self {
            economy_alone: false,
            nominal_output_base,
            root_seed: seed,
            market_rng: GameRng::substream(seed, stream::MARKET),
            economy_rng: GameRng::substream(seed, stream::ECONOMY),
            external_rng: GameRng::substream(seed, stream::EXTERNAL),
            jump_rng: GameRng::substream(seed, stream::JUMPS),
            volume_rng: GameRng::substream(seed, stream::VOLUME),
            volume_idio_rng: GameRng::substream(seed, stream::VOLUME_IDIO),
            news_rng: GameRng::substream(seed, stream::NEWS),
            overnight_rng: GameRng::substream(seed, stream::OVERNIGHT),
            market_vol_level_rng: GameRng::substream(seed, stream::MARKET_VOL_LEVEL),
            crisis_epicentre_rng: GameRng::substream(seed, stream::CRISIS_EPICENTRE),
            cycle_nowcast_rng: GameRng::substream(seed, stream::CYCLE_NOWCAST),
            // One normal per name, then one for the market's common level,
            // whenever either opening dial is on.
            opening_z: if params.opening_mispricing_sigma != 0.0
                || params.opening_market_sigma != 0.0
            {
                let mut g = GameRng::substream(seed, stream::OPENING);
                (0..companies_len + 1).map(|_| g.next_normal()).collect()
            } else {
                Vec::new()
            },
            opening_carry: Vec::new(),
            overnight_moves: vec![0.0; companies_len],
            overnight_fair_value_moves: vec![0.0; companies_len],
            jump_fair_value_moves: vec![0.0; companies_len],
            dividend_moves: vec![0.0; companies_len],
            distribution_log: Vec::new(),
            earnings_key: GameRng::earnings_key(seed),
            skew_noise_key: crate::rng::skew_noise_key(seed),
            skew_noise: 0.0,
            earnings_moves: vec![0.0; companies_len],
            earnings_withheld: vec![0.0; companies_len],
            night_market_factor: 0.0,
            last_closes: companies.iter().map(|c| c.stock.price).collect(),
            prior_closes: companies.iter().map(|c| c.stock.price).collect(),
            opening_settled: false,
            companies,
            economy,
            central_bank,
            attribution: vec![[0.0; crate::market::factors::COMPONENT_COUNT]; companies_len],
            noise_parts: vec![[0.0; 3]; companies_len],
            noise_own_scale2: vec![0.0; companies_len],
            innovation_day: if params.overnight_market_share != 0.0
                || params.overnight_idio_share != 0.0
            {
                vec![0.0; companies_len]
            } else {
                Vec::new()
            },
            tick_components: vec![[0.0; crate::market::factors::TICK_COMPONENT_COUNT]; companies_len],
            // NaN, not zero: a company that has never ticked has no valuation,
            // and zero is a real one that would silently read as "worthless"
            // rather than as "not yet computed".
            tick_fundamental: vec![f64::NAN; companies_len],
            tick_anchor: vec![f64::NAN; companies_len],
            // Zero, not NaN: a company that has not ticked has not moved, and
            // a move of zero is the true reading rather than a missing one.
            tick_shock: vec![0.0; companies_len],
            tick_absorbed: vec![0.0; companies_len],
            tick_clamp: vec![0.0; companies_len],
            tick_repriced: vec![0.0; companies_len],
            // Nothing has been written to a price before the first print.
            repriced_pending: vec![0.0; companies_len],
            // Empty until the counterfactual is switched on, so emptiness is
            // the one signal that says whether an arm ran.
            tick_unbounded_print: Vec::new(),
            tick_liquidity_share: Vec::new(),
            settle_depth_counterfactual: false,
            market_vol: MarketVarianceState::new_with(&params),
            universe_stress: 0.0,
            volume_state: 0.0,
            forced_flow_spent: 0.0,
            market_vol_log_level: 0.0,
            vix_log_level: 0.0,
            vix_anchor_slow: 0.0,
            market_vol_cycle_log: None,
            cycle_hold: None,
            vix_hold_log: None,
            vix_stress_memory: 0.0,
            vix_fear: 0.0,
            vix_log_average: 0.0,
            vix_target_premium: 0.0,
            vix_target_premium_half_life: 0.0,
            vix_target_floor: None,
            crisis_in_episode: false,
            crisis_sessions_under: 0,
            crisis_epicentre: -1,
            crisis_epicentre_pin: None,
            vix_sets_variance_pending: false,
            macro_pins_today: 0,
            pinned_corporate_spread: 0.0,
            pinned_vix_jump: 0.0,
            pinned_vix_move: 0.0,
            pinned_vix_mean: 0.0,
            market_day_scale: 1.0,
            session_news: Vec::new(),
            session_news_release: Vec::new(),
            preopen_news: Vec::new(),
            volume_idio: vec![0.0; companies_len],
            jump_move: vec![0.0; companies_len],
            sector_variance: vec![0.0; sector_keys.len()],
            sector_day_factor: vec![0.0; sector_keys.len()],
            sector_target_day: 0.0,
            jump_excitation: vec![0.0; companies_len],
            idio_variance: vec![0.0; companies_len],
            idio_jump_pending: vec![0.0; companies_len],
            idio_jump_var_pending: vec![0.0; companies_len],
            idio_jump_today: vec![0.0; companies_len],
            idio_jump_var_today: vec![0.0; companies_len],
            sector_keys,
            draws: StreamDraws::default(),
            current_day: 0,
            elapsed_days: 0,
            carried_ticks: None,
            base_fundamentals,
            base_shares,
            day_marks: Vec::new(),
            params,
            model_fingerprint: std::sync::OnceLock::new(),
            // Replaced immediately below. Zero rather than the dial's own
            // value so that a path which somehow skipped the derivation
            // would divide by zero rather than run on a plausible number.
            vix_anchor: 0.0,
            // No day has closed, so no VIX update has read a variance.
            last_index_variance: None,
            last_market_targets: None,
            rate_live: None,
            index_divisor: 0.0,
            index_close: 0.0,
            vix_live: None,
            forecast: None,
            futures: futures::FuturesState::new(seed),
            vix_futures: vix_futures::VixFuturesState::default(),
            rate_futures: rate_futures::RateFuturesState::default(),
            oil_futures: oil_futures::OilFuturesState::default(),
            margin: margin::MarginState::default(),
            options: options::OptionsState::default(),
            name_options: name_options::NameOptionsState::default(),
            stress_vix_max: 0.0,
            stress_hold_age: STRESS_HOLD_NEVER,
            drawdown_returns: std::collections::VecDeque::new(),
            drawdown_mcap_prev: 0.0,
            policy_path: 0.0,
            policy_anticipation_priced: 0.0,
            rates: crate::rates::RateBook::default(),
            book: crate::agent_book::BookState::default(),
            cycle_history: std::collections::VecDeque::new(),
            cycle_nowcast: [0.0; 5],
            cycle_nowcast_terms: ([0.0; 5], 0.0),
            gdp_publication: GdpPublication::default(),
            anticipation_drift: 0.0,
            anticipation_raw: 0.0,
            cycle_publication: CyclePublication {
                key: crate::rng::publication_key(seed),
                ..CyclePublication::default()
            },
            oil_drift_key: crate::rng::oil_drift_key(seed),
            oil_target_drift: 0.0,
            core_trend_key: crate::rng::core_trend_key(seed),
            core_trend: 0.0,
            spread_noise_key: crate::rng::spread_noise_key(seed),
            spread_noise: 0.0,
            term_premium_key: crate::rng::term_premium_key(seed),
            term_premium: 0.0,
            aggregate_level: None,
            aggregate_variance: None,
            lookthrough_written: 0.0,
            lookthrough_anchor: 0.0,
            eve_decision: None,
            population: None,
        };
        // CORE INFLATION'S TREND OPENING (`core_trend_sd`): a draw from the
        // process's stationary distribution on its own key. Nothing at 0.0.
        engine.core_trend = engine.core_trend_opening();
        // CREDIT'S OWN STATE OPENING (`corporate_spread_noise_sd`): a draw
        // from its stationary distribution on its own key. Nothing at 0.0.
        engine.spread_noise = engine.spread_noise_opening();
        // THE 10-YEAR'S OWN TERM PREMIUM OPENING (`treasury_term_premium_sd`):
        // a draw from its stationary distribution on its own key. Nothing at
        // 0.0.
        engine.term_premium = engine.term_premium_opening();
        engine.vix_anchor = engine.derive_vix_anchor();
        engine.cycle_nowcast_terms = engine.derive_cycle_nowcast_terms();
        // The front two index futures (`futures_index_listed`); nothing with
        // the switch off.
        engine.futures_list_initial();
        // The index options' first expiries (`options_index_listed`);
        // nothing with the switch off.
        engine.options_list_initial();
        // The opening meeting interval, 45 calendar days, onto the macro
        // calendar's steps. Only a fresh schedule is moved, and never on
        // the shipped calendar, where `scale_days` is the literal anyway.
        {
            let cal = engine.macro_calendar();
            let cb = &mut engine.central_bank;
            if !cal.is_shipped() && cb.next_meeting_date - cb.last_meeting_date == 45 * 24 * 60 {
                cb.next_meeting_date = cb.last_meeting_date + cal.scale_days(45) * 24 * 60;
            }
        }
        // Unemployment's impulse opens at the drive of the economy it opens
        // in, so the adjustment starts stationary; the burn-in then runs it.
        engine.seed_unemployment_impulse();
        // The burn-in's phase path, kept only for the market's prehistory
        // (`market_prehistory_sessions`), and empty with that dial at 0.0.
        let burn_in_path = if settle_opening {
            engine.burn_in_economy()
        } else {
            Vec::new()
        };
        // The same two dials `burn_in_economy` reads: either one rewrites
        // the economy the caller passed in.
        engine.opening_settled = settle_opening
            && (engine.params.macro_burn_in_days > 0.0
                || engine.params.cycle_stationary_opening != 0.0);
        // The earnings cycle opens at the level of the phase the economy
        // opens in, so a market that opens in a contraction does not spend
        // its first months drifting toward it: a stationary opening, as the
        // mispricing's is. The opening's premium split books the difference
        // into the names' fair-value levels, so no opening price moves.
        if engine.params.earnings_cycle_depth != 0.0 {
            engine.economy.earnings_cycle = engine.earnings_cycle_target();
        }
        // The market opens knowing the phase it opens in.
        engine.seed_cycle_nowcast();
        // THE SKEW NOISE OPENING (`surface_skew_noise_sd`): a draw from its
        // stationary distribution on its own key. Nothing at 0.0.
        engine.skew_noise = engine.skew_noise_opening();
        // The burn-in's accumulated drift is not the run's: `D` opens at
        // zero (`earnings_anticipation_drift_share`).
        engine.anticipation_drift = 0.0;
        engine.refresh_earnings_anticipation();
        // After the burn-in and the stationary opening, so the phase the
        // run opens in is the one published until the lag has elapsed.
        engine.seed_cycle_history();
        // Likewise the growth the run opens at, as day 0 of quarter 0.
        engine.seed_gdp_publication(0);
        // THE MARKET'S PREHISTORY, last, on the engine as it opens: a copy
        // lives the burn-in's last sessions and hands back its volatility
        // state. A branch at 0.0, every preset: nothing is copied or run, and
        // the market opens at the constructor's baseline as it always did.
        // Not when the caller named the opening, for the burn-in's reason.
        if settle_opening && engine.params.market_prehistory_sessions > 0.0 {
            engine.live_market_prehistory(&burn_in_path);
        }
        engine
    }

    /// Play the market's last `market_prehistory_sessions` sessions before
    /// day zero on a copy of this engine, and open with the copy's
    /// volatility state.
    ///
    /// # Why
    ///
    /// The burn-in (`macro_burn_in_days`) runs the economy without a market,
    /// so every volatility state the constructor seeds opens at its
    /// phase-free baseline: the factor variance at `market_factor_sigma`
    /// squared, the VIX wherever the index's baseline variance puts it, the
    /// anchor's memory at zero and the cycle's multiplier unset. A run that
    /// opens in an expansion then spends two quarters falling to the level
    /// its expansions hold, and one that opens in a contraction rises
    /// through the whole of its own. Both are travel the run's later years
    /// never show.
    ///
    /// # What the copy lives through
    ///
    /// The economy's own recorded phases, one a session, ending on the
    /// phase and age the run opens in: `path` is the burn-in's phase after
    /// each of its days. A prehistory longer than the burn-in holds its
    /// first phase for the difference (and the opening's phase throughout
    /// when there is no burn-in). The copy's cycle may turn between
    /// sessions; the recorded phase is set again before the next, so the
    /// copy's volatility follows the path the run's economy actually took.
    ///
    /// # What it draws
    ///
    /// Every stream of the copy is a surgery generator of the run's root
    /// seed under [`crate::rng::PREHISTORY_TAG`], and its earnings and
    /// publication keys are mixed with the same tag, so no session of the
    /// prehistory shares a draw with any session of the run. The run's own
    /// generators are not touched: its draw schedule is the one it had.
    ///
    /// # What comes back
    ///
    /// The volatility state and nothing else: the factor variance (its two
    /// components, the mixture, the smoothed VIX and the return memory),
    /// the VIX, the VIX's and the factor's slow levels, the anchor's and
    /// the stress premium's memories, the cycle's volatility multiplier,
    /// and each sector's variance and each name's GARCH and idiosyncratic
    /// variance and jump excitation. Prices, fair values, the economy's
    /// other fields, the central bank and the books are the run's own. The
    /// central bank's stress level (`fed_stress_cut`), the highest VIX
    /// since the last meeting, is restarted at the VIX the run opens on.
    fn live_market_prehistory(&mut self, path: &[(crate::economy::CyclePhase, f64)]) {
        let n = self.params.market_prehistory_sessions as usize;
        let tag = crate::rng::PREHISTORY_TAG;
        let root = self.root_seed;
        let mut pre = self.clone();
        pre.market_rng = GameRng::surgery(root, stream::MARKET, tag);
        pre.economy_rng = GameRng::surgery(root, stream::ECONOMY, tag);
        pre.external_rng = GameRng::surgery(root, stream::EXTERNAL, tag);
        pre.jump_rng = GameRng::surgery(root, stream::JUMPS, tag);
        pre.volume_rng = GameRng::surgery(root, stream::VOLUME, tag);
        pre.volume_idio_rng = GameRng::surgery(root, stream::VOLUME_IDIO, tag);
        pre.news_rng = GameRng::surgery(root, stream::NEWS, tag);
        pre.overnight_rng = GameRng::surgery(root, stream::OVERNIGHT, tag);
        pre.market_vol_level_rng = GameRng::surgery(root, stream::MARKET_VOL_LEVEL, tag);
        pre.crisis_epicentre_rng = GameRng::surgery(root, stream::CRISIS_EPICENTRE, tag);
        pre.cycle_nowcast_rng = GameRng::surgery(root, stream::CYCLE_NOWCAST, tag);
        // The futures write nothing a price reads, and the prehistory hands
        // back prices' state alone, so its copy lists none.
        pre.params.futures_index_listed = 0.0;
        pre.params.night_session_steps = 0.0;
        pre.params.futures_impact_memory = 0.0;
        pre.params.futures_impact_memory_half_life = 0.0;
        pre.params.futures_vix_listed = 0.0;
        pre.params.futures_vix_live_fast_share = 0.0;
        pre.params.futures_vix_live_fast_half_life = 0.0;
        pre.params.futures_vix_live_slow_half_life = 0.0;
        pre.params.forecast_vix_level_nowcast = 0.0;
        pre.params.forecast_vix_level_nowcast_half_life = 0.0;
        pre.params.forecast_vix_level_nowcast_centre = 0.0;
        pre.params.futures_vix_premium_response = 0.0;
        pre.params.futures_rates_listed = 0.0;
        pre.params.futures_oil_listed = 0.0;
        pre.params.margin_scan_coverage = 0.0;
        pre.params.margin_scan_tail = 0.0;
        pre.params.margin_scan_option_tail = 0.0;
        pre.params.options_index_listed = 0.0;
        pre.params.option_dealer_spread = 0.0;
        pre.params.options_index_forward_from_future = 0.0;
        pre.params.surface_ssvi = 0.0;
        pre.params.surface_skew_physical = 0.0;
        pre.params.surface_skew_physical_slope = 0.0;
        pre.params.surface_skew_premium = 0.0;
        pre.params.surface_skew_premium_slope = 0.0;
        pre.params.surface_skew_noise_sd = 0.0;
        pre.params.surface_skew_noise_half_life = 0.0;
        pre.params.surface_curvature = 0.0;
        pre.params.surface_index_curvature = 0.0;
        pre.params.surface_index_vertex = 0.0;
        pre.params.surface_index_tenor_exponent = 0.0;
        pre.params.surface_index_tenor_strip = 0.0;
        pre.params.surface_curvature_exponent = 0.0;
        pre.params.surface_term_premium_short = 0.0;
        pre.params.surface_term_premium_long = 0.0;
        pre.params.surface_earnings_weight = 0.0;
        pre.params.options_names_listed = 0.0;
        pre.params.surface_name_premium = 0.0;
        pre.params.surface_name_skew = 0.0;
        pre.params.option_name_inventory_slope = 0.0;
        pre.params.option_name_inventory_half_life = 0.0;
        pre.params.option_name_spread_otm = 0.0;
        pre.params.option_name_spread_itm = 0.0;
        pre.params.option_name_spread_tenor_exponent = 0.0;
        pre.params.surface_name_earnings_premium = 0.0;
        pre.params.surface_name_term_slope = 0.0;
        pre.options = options::OptionsState::default();
        pre.name_options = name_options::NameOptionsState::default();
        pre.earnings_key = crate::rng::prehistory_key(self.earnings_key);
        pre.cycle_publication.key = crate::rng::prehistory_key(self.cycle_publication.key);
        let opening = (self.economy.cycle_phase, self.economy.months_in_current_phase);
        let mut buffer = SessionBuffer::new();
        let names = pre.companies.len();
        for k in 0..n {
            // Session k of n ends the prehistory at k = n - 1 on the path's
            // last day, which is the phase and age the run opens in.
            let back = n - k;
            let (phase, months) = if path.is_empty() {
                opening
            } else if back <= path.len() {
                path[path.len() - back]
            } else {
                path[0]
            };
            pre.economy.cycle_phase = phase;
            pre.economy.months_in_current_phase = months;
            pre.set_current_day(k as i64);
            pre.open_market();
            let innovations: Vec<Option<f64>> = vec![None; names];
            let variances = pre.sector_base_variances();
            pre.run_session(
                &SessionRequest {
                    start: GameTime { hour: 9, minute: 30, day_of_week: 3 },
                    ticks: 390,
                    volatility_multiplier: 1.0,
                    news: &[],
                    news_impact_queue: &[],
                    order_volumes: &[],
                    fills: &[],
                    close_at_end: false,
                    reopen: false,
                    daily_innovations: &innovations,
                    sector_base_variances: &variances,
                    stop: None,
                },
                &mut buffer,
            );
            pre.close_day(k as i64 + 1);
        }
        self.market_vol = pre.market_vol;
        self.economy.vix = pre.economy.vix;
        self.vix_log_level = pre.vix_log_level;
        self.market_vol_log_level = pre.market_vol_log_level;
        self.vix_anchor_slow = pre.vix_anchor_slow;
        self.vix_stress_memory = pre.vix_stress_memory;
        self.vix_fear = pre.vix_fear;
        self.market_vol_cycle_log = pre.market_vol_cycle_log;
        self.sector_variance = pre.sector_variance.clone();
        self.idio_variance = pre.idio_variance.clone();
        self.jump_excitation = pre.jump_excitation.clone();
        for (c, p) in self.companies.iter_mut().zip(pre.companies.iter()) {
            c.stock.garch_variance = p.stock.garch_variance;
        }
        if self.params.fed_stress_cut != 0.0 {
            self.stress_vix_max = self.economy.vix;
        }
        // The drawdown hold's window: the copy's returns, so the run's first
        // year reads a full window. Its base is the run's own cap, which the
        // first close records.
        if self.keeps_drawdown_window() {
            self.drawdown_returns = pre.drawdown_returns.clone();
        }
        // THE VALUATION STATE, under its own switch: a branch at 0.0, every
        // preset, where the market opens at the valuation it always did.
        if self.params.market_prehistory_valuation != 0.0 {
            self.carry_prehistory_valuation(&pre);
        }
    }

    /// Open with the valuation state the market's prehistory left on its
    /// copy (`market_prehistory_valuation`), booked so no opening price
    /// moves.
    ///
    /// # Why
    ///
    /// The burn-in has no market, so every valuation state the market feeds
    /// opens where a market that never traded leaves it, and drifts to its
    /// level over the first year: the names' mispricing (whose settled mean
    /// is below zero, from the non-zero-mean market terms left in `s`), the
    /// VIX feedback's exposure (built by spikes and given back at
    /// `fair_value_vix_release_half_life`), the anticipation's drift, the
    /// earnings cycle (the burn-in's level, replaced by the phase's target),
    /// credit's leverage gap, the Fed put's owed cut and the policy path's
    /// forecast. On R17Bd with a
    /// 252-session prehistory year 0 returned about two points less than
    /// the years after it, all in its first two quarters.
    ///
    /// # What is carried
    ///
    /// From the copy, at the end of its prehistory:
    ///
    /// - each name's mispricing `s`, which the opening's split takes in
    ///   place of its draw (`opening_carry`), so the name's fair-value level
    ///   absorbs the difference at the first open;
    /// - the VIX feedback's exposure, the anticipation's drift and the
    ///   earnings cycle, which move fair value only, so the same split books
    ///   them into the fair-value levels;
    /// - credit's leverage gap, with the corporate yield moved by the change
    ///   it makes to the spread formula at tonight's VIX and multiplier;
    /// - the Fed put's owed cut and its stock, with the policy rate lowered
    ///   by the owed cut (not below zero) and the prime rate, both Treasury
    ///   yields, the corporate yield and the mortgage rate moved by the same
    ///   amount, which is where the rule, the curve's anchor (whose ladder
    ///   adds the owed cut back) and the spreads put them.
    /// - the market's forecast of the policy path, with the curve moved by
    ///   the share of its change each yield prices (the burn-in leaves the
    ///   forecast where the economy's own early cuts put it).
    ///
    /// Every price the run opens at, its draws and every other state are its
    /// own. The rate instruments are marked after construction, at the curve
    /// the run opens on.
    fn carry_prehistory_valuation(&mut self, pre: &Engine) {
        if !self.opening_z.is_empty() {
            self.opening_carry = self
                .companies
                .iter()
                .zip(pre.companies.iter())
                .map(|(c, p)| match p.stock.mispricing_s {
                    Some(v) if !c.is_bankrupt && c.is_public && !p.is_bankrupt && p.is_public => v,
                    _ => f64::NAN,
                })
                .collect();
        }
        if self.carries_vix_feedback() {
            self.economy.vix_feedback = pre.economy.vix_feedback;
        }
        if self.params.earnings_cycle_depth != 0.0 {
            self.economy.earnings_cycle = pre.economy.earnings_cycle;
        }
        if self.carries_anticipation_drift() {
            self.anticipation_drift = pre.anticipation_drift;
        }
        self.refresh_earnings_anticipation();
        if self.carries_spread_equity_gap() || self.carries_spread_noise() {
            let gap = pre.economy.spread_equity_gap;
            let m = if self.params.cycle_nowcast_accuracy != 0.0
                || self.params.corporate_spread_cycle != 0.0
            {
                self.priced_spread_multiplier(self.economy.cycle_phase)
            } else {
                crate::economy::central_bank::spread_multiplier_of(self.economy.cycle_phase)
            };
            let cut = self.params.corporate_spread_vix_cut;
            let gain = self.params.corporate_spread_equity_gain;
            let vix = self.economy.vix;
            // Under `corporate_spread_equity_lag` the spread's term is the
            // priced gap's, carried with it.
            let lagged = self.params.corporate_spread_equity_lag_gain;
            let (then, now) = if self.carries_spread_priced_gap() && lagged != 0.0 {
                // Credit's lagged leverage term (`corporate_spread_equity_lag_gain`):
                // the gap itself beside the priced gap, as one gap.
                let k = lagged / gain;
                (gap + k * pre.economy.spread_priced_gap,
                 self.economy.spread_equity_gap + k * self.economy.spread_priced_gap)
            } else if self.carries_spread_priced_gap() {
                (pre.economy.spread_priced_gap, self.economy.spread_priced_gap)
            } else if self.carries_spread_equity_gap() {
                (gap, self.economy.spread_equity_gap)
            } else {
                (0.0, 0.0)
            };
            // Credit's own state (`corporate_spread_noise_sd`) is carried
            // the same way, beside the leverage term.
            let noise = self.carries_spread_noise();
            let base = |g: f64, x: f64| if noise { gain * g + x } else { gain * g };
            let moved = crate::economy::central_bank::spread_formula_with(vix, m, cut, base(then, pre.spread_noise))
                - crate::economy::central_bank::spread_formula_with(vix, m, cut, base(now, self.spread_noise));
            if self.carries_spread_equity_gap() {
                self.economy.spread_equity_gap = gap;
            }
            if self.carries_spread_priced_gap() {
                self.economy.spread_priced_gap = pre.economy.spread_priced_gap;
            }
            // The gap the 10-year has priced is carried with the gap.
            if self.carries_treasury_equity_lag() {
                self.economy.treasury_equity_priced_gap = pre.economy.treasury_equity_priced_gap;
            }
            if noise {
                self.spread_noise = pre.spread_noise;
            }
            self.economy.corporate_bond_yield += moved;
        }
        if self.params.fed_put_gain != 0.0 {
            let owed = pre.economy.fed_put_owed;
            let ffr = self.economy.federal_funds_rate;
            let cut = ffr - crate::mathx::max(0.0, ffr - owed);
            self.economy.fed_put = if owed > 0.0 { pre.economy.fed_put * cut / owed } else { 0.0 };
            self.economy.fed_put_owed = cut;
            self.economy.federal_funds_rate -= cut;
            self.economy.prime_rate -= cut;
            self.economy.treasury_yield_10y -= cut;
            self.economy.treasury_yield_2y -= cut;
            self.economy.corporate_bond_yield -= cut;
            self.economy.mortgage_rate_30y -= cut;
        }
        if self.params.treasury_path_pricing != 0.0 {
            // The priced path's change moves the 10-year's anchor by `1 - d`
            // of itself and the 2-year's formula by `0.85 + 0.15 (1 - d)`,
            // `d` the damping, as `reprice_anticipated_meeting` books a
            // priced change; the corporate yield and the mortgage rate move
            // with the 10-year.
            let moved = self.params.treasury_path_pricing * (pre.policy_path - self.policy_path);
            let f10 = 1.0 - self.params.treasury_policy_damping;
            let f2 = 0.85 + 0.15 * f10;
            self.policy_path = pre.policy_path;
            self.economy.treasury_yield_10y += f10 * moved;
            self.economy.treasury_yield_2y += f2 * moved;
            self.economy.corporate_bond_yield += f10 * moved;
            self.economy.mortgage_rate_30y += f10 * moved;
        }
    }

    /// The share of the gap to its drive unemployment's impulse closes at a
    /// monthly release, from `unemployment_adjustment_half_life`; 0.0 off.
    fn unemployment_adjustment(&self) -> f64 {
        let h = self.params.unemployment_adjustment_half_life;
        if h == 0.0 {
            return 0.0;
        }
        1.0 - crate::mathx::pow(0.5, self.macro_calendar().month_f64() / h)
    }

    /// Set unemployment's impulse to what its cyclical drivers ask for in
    /// the economy as it stands: at construction, or on a restore from a
    /// snapshot that carried none. Nothing with the dial at 0.0.
    pub fn seed_unemployment_impulse(&mut self) {
        if self.params.unemployment_adjustment_half_life == 0.0 {
            return;
        }
        let e = &self.economy;
        let phase = crate::economy::phase_characteristics_for(
            e.cycle_phase, self.params.cycle_us_calibration != 0.0);
        self.economy.unemployment_impulse = crate::economy::daily::unemployment_drive_with(
            phase.unemployment_trend, e.cycle_phase, e.gdp_growth,
            self.params.unemployment_okun_coefficient);
    }

    /// `gdp_publication_lag` in sessions; 0 is off.
    pub fn gdp_publication_lag(&self) -> i64 {
        self.params.gdp_publication_lag as i64
    }

    /// GDP growth as published, in percent: the mean of the true daily
    /// growth over the last macro-calendar quarter released, released
    /// `gdp_publication_lag` sessions after that quarter's last close, and
    /// the opening growth until the first release. With the dial at 0.0 the
    /// growth the economy runs at, `economy().gdp_growth`.
    ///
    /// Every route that REPORTS growth reads this; everything that MOVES on
    /// it (output, earnings, unemployment, the cycle's hazards, the central
    /// bank) reads the true figure.
    pub fn published_gdp_growth(&self) -> f64 {
        if self.params.gdp_publication_lag == 0.0 {
            return self.economy.gdp_growth;
        }
        self.gdp_publication.published
    }

    /// The published figure and the quarters behind it. Default with the
    /// dial at 0.0.
    pub fn gdp_publication(&self) -> &GdpPublication {
        &self.gdp_publication
    }

    /// Put the published figure's state back (a restore). Refuses any
    /// state while the dial is 0.0, a quarter with no close averaged,
    /// non-finite figures, and releases out of order.
    pub fn set_gdp_publication(&mut self, state: GdpPublication) -> Result<(), String> {
        if self.params.gdp_publication_lag == 0.0 {
            return Err(
                "this snapshot carries a published GDP growth state, and this \
                 engine's gdp_publication_lag is 0, so it keeps none".to_string());
        }
        if state.count == 0 {
            return Err(
                "this snapshot's published GDP growth state averages no close \
                 in its quarter; gdp_publication_lag's state always holds one".to_string());
        }
        let finite = state.published.is_finite() && state.sum.is_finite()
            && state.pending.iter().all(|&(_, v)| v.is_finite());
        if !finite {
            return Err(
                "this snapshot's published GDP growth state (gdp_publication_lag) \
                 carries a non-finite figure".to_string());
        }
        if state.pending.iter().zip(state.pending.iter().skip(1)).any(|(a, b)| a.0 >= b.0) {
            return Err(
                "this snapshot's pending GDP releases (gdp_publication_lag) are \
                 not in release order".to_string());
        }
        self.gdp_publication = state;
        Ok(())
    }

    /// Start the published figure at the growth the economy holds now,
    /// taken as the value of `day`, the last day closed: the opening (day
    /// 0), or a restore from a snapshot that carried no state. The quarter
    /// `day` falls in is averaged from this value on; nothing is pending.
    /// Nothing with the dial at 0.0.
    pub fn seed_gdp_publication(&mut self, day: i64) {
        if self.params.gdp_publication_lag == 0.0 {
            self.gdp_publication = GdpPublication::default();
            return;
        }
        let q = self.macro_calendar().days_per_quarter();
        let g = self.economy.gdp_growth;
        self.gdp_publication = GdpPublication {
            published: g,
            quarter: day.div_euclid(q),
            count: 1,
            sum: g,
            pending: std::collections::VecDeque::new(),
        };
    }

    /// The close of `day` into the published figure: a new quarter closes
    /// the one before (its mean queued for release `lag` sessions after its
    /// last day), the day's growth joins its quarter, and every release due
    /// by this close is published, the latest last. Nothing with the dial
    /// at 0.0.
    fn record_gdp_growth(&mut self, day: i64) {
        let lag = self.gdp_publication_lag();
        if lag == 0 {
            return;
        }
        let q = self.macro_calendar().days_per_quarter();
        let quarter = day.div_euclid(q);
        let growth = self.economy.gdp_growth;
        let p = &mut self.gdp_publication;
        if quarter != p.quarter {
            if p.count > 0 {
                let last_day = (p.quarter + 1) * q - 1;
                p.pending.push_back((last_day + lag, p.sum / f64::from(p.count)));
            }
            p.quarter = quarter;
            p.count = 0;
            p.sum = 0.0;
        }
        p.sum += growth;
        p.count += 1;
        while let Some(&(release, value)) = p.pending.front() {
            if release > day {
                break;
            }
            p.published = value;
            p.pending.pop_front();
        }
    }

    /// `cycle_publication_lag` in sessions; 0 is off, and so is a lag under
    /// `cycle_publication_lag_draw`, which keeps no history.
    pub fn cycle_publication_lag(&self) -> usize {
        if self.params.cycle_publication_lag_draw != 0.0 {
            return 0;
        }
        self.params.cycle_publication_lag as usize
    }

    /// Whether the engine keeps the fixed lag's phase history: the snapshot
    /// and the state hash carry it exactly then.
    pub fn carries_cycle_history(&self) -> bool {
        self.cycle_publication_lag() != 0
    }

    /// The publication schedule under `cycle_publication_lag_draw`.
    pub fn cycle_publication(&self) -> &CyclePublication {
        &self.cycle_publication
    }

    /// Put a publication schedule back (a restore). Refused with the switch
    /// at 0.0, which keeps none, and for pending turns out of order.
    pub fn set_cycle_publication(&mut self, state: CyclePublication) -> Result<(), String> {
        if self.params.cycle_publication_lag_draw == 0.0 {
            return Err("this snapshot carries a cycle publication schedule, and this \
                        engine's cycle_publication_lag_draw is 0, so it keeps none"
                .to_string());
        }
        let mut last = i64::MIN;
        for &(close, _) in &state.pending {
            if close < last {
                return Err(format!(
                    "a cycle publication schedule's pending closes are in order, got {:?}",
                    state.pending));
            }
            last = close;
        }
        if state.closes < 0 {
            return Err(format!("a cycle publication schedule's closes are {}", state.closes));
        }
        self.cycle_publication = state;
        Ok(())
    }

    /// Open the publication schedule on the phase the economy is in: the
    /// opening, or a restore from a snapshot that carried none. The key is
    /// kept. Nothing with `cycle_publication_lag_draw` at 0.0.
    pub fn seed_cycle_publication(&mut self) {
        if self.params.cycle_publication_lag_draw == 0.0 {
            return;
        }
        let phase = self.economy.cycle_phase;
        self.cycle_publication = CyclePublication {
            key: self.cycle_publication.key,
            published: phase,
            last_true: phase,
            closes: 0,
            turns: 0,
            pending: std::collections::VecDeque::new(),
        };
    }

    /// One close of the schedule: a turn draws its lag and joins the queue,
    /// and every turn whose close has come is published, the latest last.
    fn record_cycle_publication(&mut self) {
        let now = self.economy.cycle_phase;
        let p = &mut self.cycle_publication;
        p.closes += 1;
        if now != p.last_true {
            let u = crate::rng::publication_uniform(p.key, p.turns);
            let lag = cycle_publication_lag_of(now, u);
            p.turns += 1;
            let earliest = p.pending.back().map(|&(c, _)| c).unwrap_or(i64::MIN);
            let at = p.closes + lag;
            p.pending.push_back((if at > earliest { at } else { earliest }, now));
            p.last_true = now;
        }
        while let Some(&(close, phase)) = p.pending.front() {
            if close > p.closes {
                break;
            }
            p.published = phase;
            p.pending.pop_front();
        }
    }

    /// The business-cycle phase as published: the phase the economy held at
    /// the close `cycle_publication_lag` sessions ago, or the opening phase
    /// while fewer than that many sessions have closed. With the dial at 0.0
    /// the phase the economy is in, `economy().cycle_phase`.
    ///
    /// Every route that REPORTS the phase reads this; everything that PRICES
    /// or MOVES on it (the earnings cycle and its anticipation, the cycle's
    /// hazards, the stress intensity, the central bank) reads the true phase.
    pub fn published_cycle_phase(&self) -> crate::economy::CyclePhase {
        // Under `cycle_publication_lag_draw` the phase of the latest turn
        // published, and the fixed lag is not read.
        if self.params.cycle_publication_lag_draw != 0.0 {
            return self.cycle_publication.published;
        }
        if self.params.cycle_publication_lag == 0.0 {
            return self.economy.cycle_phase;
        }
        self.cycle_history.front().copied().unwrap_or(self.economy.cycle_phase)
    }

    /// The phase history the published phase is read from, oldest first:
    /// `cycle_publication_lag + 1` phases, the last the current one as of
    /// the last close. Empty with the dial at 0.0.
    pub fn cycle_history(&self) -> &std::collections::VecDeque<crate::economy::CyclePhase> {
        &self.cycle_history
    }

    /// Put a phase history back (a restore). Refuses one whose length is not
    /// `cycle_publication_lag + 1`, or any history while the dial is 0.0.
    pub fn set_cycle_history(
        &mut self,
        history: Vec<crate::economy::CyclePhase>,
    ) -> Result<(), String> {
        let lag = self.cycle_publication_lag();
        if lag == 0 {
            if history.is_empty() {
                return Ok(());
            }
            return Err(format!(
                "this snapshot carries a published-phase history of {} phases, \
                 and this engine's cycle_publication_lag is 0, so it keeps none",
                history.len()));
        }
        if history.len() != lag + 1 {
            return Err(format!(
                "this snapshot carries a published-phase history of {} phases, \
                 and cycle_publication_lag {} keeps {}",
                history.len(), lag, lag + 1));
        }
        self.cycle_history = history.into();
        Ok(())
    }

    /// Fill the history with the current phase: the opening, or a restore
    /// from a snapshot that carried none. Nothing with the dial at 0.0.
    pub fn seed_cycle_history(&mut self) {
        // The drawn schedule opens where the fixed history does.
        self.seed_cycle_publication();
        let lag = self.cycle_publication_lag();
        self.cycle_history.clear();
        if lag > 0 {
            self.cycle_history.extend(std::iter::repeat_n(self.economy.cycle_phase, lag + 1));
        }
    }

    /// Record the phase the close has left the economy in, and drop the
    /// oldest. Nothing with the dial at 0.0.
    fn record_cycle_phase(&mut self) {
        if self.params.cycle_publication_lag_draw != 0.0 {
            self.record_cycle_publication();
            return;
        }
        let lag = self.cycle_publication_lag();
        if lag == 0 {
            return;
        }
        self.cycle_history.push_back(self.economy.cycle_phase);
        while self.cycle_history.len() > lag + 1 {
            self.cycle_history.pop_front();
        }
    }

    /// The anticipated earnings level's phase terms, `g_p` in
    /// [`crate::economy::cycle::phase_cycle`] order, and its weight `c` on
    /// the current level. `None` with the anticipation or the cycle off.
    ///
    /// # The derivation
    ///
    /// The valuation reads `A = E[ integral rho e^(-rho s) e(t+s) ds ]`, the
    /// earnings cycle's level averaged over the future with a discount of
    /// `rho = ln 2 / earnings_anticipation_half_life` a session. The level
    /// follows the engine's own law, `de = kappa (T_p - e)` with `kappa =
    /// ln 2 / earnings_cycle_half_life` toward the phase's target `T_p`
    /// (`-depth` in a contraction or a trough, `+depth * upside` otherwise),
    /// and the phase leaves at the rate `lambda_p = 1 / E[T_p]`, its mean
    /// sojourn on the engine's own hazard table (`mean_sojourn_days_for`),
    /// to the next phase of the cycle. Trying `A = c e + g_p` in the
    /// generator equation `rho A = rho e + kappa (T_p - e) dA/de +
    /// lambda_p (A_next - A)` gives `c = rho / (rho + kappa)` and
    /// `(rho + lambda_p) g_p = kappa c T_p + lambda_p g_next`, five linear
    /// equations around the cycle, solved here in closed form.
    ///
    /// So a turn of phase moves `A` at once, by `g_next - g_p`, and a
    /// trough, from which a recovery is nearer than from a contraction,
    /// already reads above a contraction while the level is still falling:
    /// the price's trough leads the earnings'. The sojourns are treated as
    /// exponential, where the table's are Weibull; that is the one
    /// approximation.
    pub fn earnings_anticipation_terms(&self) -> Option<([f64; 5], f64)> {
        let p = &self.params;
        if p.earnings_anticipation_half_life <= 0.0 || p.earnings_cycle_depth == 0.0 {
            return None;
        }
        let rho = std::f64::consts::LN_2 / p.earnings_anticipation_half_life;
        let kappa = std::f64::consts::LN_2 / p.earnings_cycle_half_life;
        let c = rho / (rho + kappa);
        let (mean, _) = crate::economy::cycle::stationary_phase_shares_for(&self.cycle_spec());
        let phases = crate::economy::cycle::phase_cycle();
        let target = |ph: crate::economy::CyclePhase| match ph {
            crate::economy::CyclePhase::Contraction | crate::economy::CyclePhase::Trough => {
                -p.earnings_cycle_depth
            }
            _ => p.earnings_cycle_depth * p.earnings_cycle_upside,
        };
        let mut a = [0.0; 5];
        let mut b = [0.0; 5];
        for k in 0..5 {
            let lambda = if mean[k] > 0.0 { 1.0 / mean[k] } else { 0.0 };
            a[k] = kappa * c * target(phases[k]) / (rho + lambda);
            b[k] = lambda / (rho + lambda);
        }
        // g_k = a_k + b_k g_(k+1): unroll once around the cycle for g_0,
        // then walk backwards.
        let mut acc_a = 0.0;
        let mut acc_b = 1.0;
        for k in 0..5 {
            acc_a += acc_b * a[k];
            acc_b *= b[k];
        }
        let mut g = [0.0; 5];
        g[0] = acc_a / (1.0 - acc_b);
        for k in (1..5).rev() {
            let next = if k == 4 { g[0] } else { g[k + 1] };
            g[k] = a[k] + b[k] * next;
        }
        Some((g, c))
    }

    /// Keep `economy.earnings_anticipation` current: `A - e`, or 0.0 with
    /// the anticipation off. Called wherever the phase or the level moves.
    /// Under `cycle_nowcast_accuracy` the phase term is the belief's
    /// `pi . g`, not the true phase's `g`.
    pub fn refresh_earnings_anticipation(&mut self) {
        let raw = match self.earnings_anticipation_terms() {
            None => 0.0,
            Some((g, c)) => {
                let gk = if self.params.cycle_nowcast_accuracy != 0.0 {
                    let mut acc = 0.0;
                    for (p, gj) in self.cycle_nowcast.iter().zip(g.iter()) {
                        acc += p * gj;
                    }
                    acc
                } else {
                    let k = crate::economy::cycle::phase_cycle()
                        .iter()
                        .position(|ph| *ph == self.economy.cycle_phase)
                        .unwrap_or(0);
                    g[k]
                };
                gk + c * self.economy.earnings_cycle - self.economy.earnings_cycle
            }
        };
        // `earnings_anticipation_drift_share`: the valuation reads `A - e`
        // less the expected drift accumulated in `D`. A branch, so with the
        // share at 0.0 this is the assignment that stood.
        if self.params.earnings_anticipation_drift_share == 0.0 {
            self.economy.earnings_anticipation = raw;
        } else {
            self.anticipation_raw = raw;
            self.economy.earnings_anticipation = raw - self.anticipation_drift;
        }
        // The look-through rides beside it, so whatever refreshes the
        // anticipation after the economy is rebuilt refreshes it too.
        self.refresh_earnings_lookthrough();
    }

    /// Whether the engine keeps `D` (`earnings_anticipation_drift_share`):
    /// the snapshot and the state hash carry it, and the last `A - e`,
    /// exactly then.
    pub fn carries_anticipation_drift(&self) -> bool {
        self.params.earnings_anticipation_drift_share != 0.0
    }

    /// `(D, A - e at the last refresh)`; zeros with the share at 0.0.
    pub fn anticipation_drift(&self) -> (f64, f64) {
        (self.anticipation_drift, self.anticipation_raw)
    }

    /// Put `D` and the last `A - e` back (a restore), and the anticipation
    /// the valuation reads with them. Refused with the share at 0.0, which
    /// keeps neither, and for a value that is not finite.
    pub fn set_anticipation_drift(&mut self, drift: f64, raw: f64) -> Result<(), String> {
        if !self.carries_anticipation_drift() {
            return Err("this snapshot carries an anticipation drift, and this engine's \
                        earnings_anticipation_drift_share is 0, so it keeps none"
                .to_string());
        }
        if !drift.is_finite() || !raw.is_finite() {
            return Err(format!(
                "an anticipation drift and its last A - e are finite, got {drift} and {raw}"));
        }
        self.anticipation_drift = drift;
        self.anticipation_raw = raw;
        self.economy.earnings_anticipation = raw - drift;
        Ok(())
    }

    /// Zero `D`: the opening, or a restore from a snapshot that carried
    /// none. The anticipation is refreshed on it. Nothing with the share at
    /// 0.0.
    pub fn seed_anticipation_drift(&mut self) {
        if !self.carries_anticipation_drift() {
            return;
        }
        self.anticipation_drift = 0.0;
        self.refresh_earnings_anticipation();
    }

    /// `earnings_anticipation_drift_share`: at a close, decay `D` with its
    /// half-life and add the share of the session's expected drift of `A`,
    /// `rho (A - e)` as the last refresh left it. Nothing with the share at
    /// 0.0 or the anticipation off.
    fn advance_anticipation_drift(&mut self) {
        let m = self.params.earnings_anticipation_drift_share;
        let h = self.params.earnings_anticipation_half_life;
        if m == 0.0 || h <= 0.0 || self.params.earnings_cycle_depth == 0.0 {
            return;
        }
        let rho = std::f64::consts::LN_2 / h;
        let hd = if self.params.earnings_anticipation_drift_half_life == 0.0 {
            1260.0
        } else {
            self.params.earnings_anticipation_drift_half_life
        };
        self.anticipation_drift =
            self.anticipation_drift * crate::mathx::exp(-std::f64::consts::LN_2 / hd)
                + m * rho * self.anticipation_raw;
    }

    /// The market's belief over the phases, `phase_cycle` order; zeros with
    /// `cycle_nowcast_accuracy` off.
    pub fn cycle_nowcast(&self) -> [f64; 5] {
        self.cycle_nowcast
    }

    /// Put the whole belief on the phase the economy is in: the opening, or
    /// a restore from a snapshot that carried none. Nothing with the dial off.
    pub fn seed_cycle_nowcast(&mut self) {
        if self.params.cycle_nowcast_accuracy == 0.0 {
            return;
        }
        let k = crate::economy::cycle::phase_cycle()
            .iter()
            .position(|ph| *ph == self.economy.cycle_phase)
            .unwrap_or(0);
        self.cycle_nowcast = [0.0; 5];
        self.cycle_nowcast[k] = 1.0;
    }

    /// The nowcast generator's position, for the snapshot.
    pub fn cycle_nowcast_rng_state(&self) -> crate::rng::RngState {
        self.cycle_nowcast_rng.snapshot()
    }

    /// Put the belief and its generator back from a snapshot. Refused with
    /// `cycle_nowcast_accuracy` at 0.0, which keeps neither, and for a
    /// belief that is not five finite non-negative weights summing to one.
    pub fn set_cycle_nowcast(
        &mut self,
        belief: [f64; 5],
        rng: Option<crate::rng::RngState>,
    ) -> Result<(), String> {
        if self.params.cycle_nowcast_accuracy == 0.0 {
            return Err("this snapshot carries a cycle nowcast, and this engine's \
                        cycle_nowcast_accuracy is 0, so it keeps none"
                .to_string());
        }
        let total: f64 = belief.iter().sum();
        if belief.iter().any(|v| !v.is_finite() || *v < 0.0) || (total - 1.0).abs() > 1e-9 {
            return Err(format!(
                "a cycle nowcast is five finite non-negative weights summing to one, got {belief:?}"));
        }
        self.cycle_nowcast = belief;
        if let Some(state) = rng {
            self.cycle_nowcast_rng = GameRng::restore(state);
        }
        Ok(())
    }

    /// One session of the nowcast: the report, drawn from the phase the
    /// session ran in, and the forward filter's predict and update.
    fn update_cycle_nowcast(&mut self) {
        // A pinned phase is public: the belief was put on it at the pin and
        // holds there through the close, with no report drawn.
        if self.macro_pins_today & PIN_CYCLE != 0 {
            return;
        }
        let q = self.params.cycle_nowcast_accuracy;
        let phases = crate::economy::cycle::phase_cycle();
        let k = phases.iter().position(|ph| *ph == self.economy.cycle_phase).unwrap_or(0);
        self.cycle_nowcast_rng.site(Site::CycleNowcastU, 0);
        let u = self.cycle_nowcast_rng.next_f64();
        let r = if u < q {
            k
        } else {
            // One of the other four, uniformly; a uniform that rounds to
            // the top edge takes the last.
            let j = ((u - q) / (1.0 - q) * 4.0) as usize;
            (k + 1 + if j > 3 { 3 } else { j }) % 5
        };
        let lambda = self.cycle_nowcast_terms.0;
        let mut pred = [0.0; 5];
        for (j, (&lam, &p)) in lambda.iter().zip(self.cycle_nowcast.iter()).enumerate() {
            pred[j] += p * (1.0 - lam);
            pred[(j + 1) % 5] += p * lam;
        }
        let other = (1.0 - q) / 4.0;
        let mut total = 0.0;
        for (j, pj) in pred.iter_mut().enumerate() {
            *pj *= if j == r { q } else { other };
            total += *pj;
        }
        if total > 0.0 && total.is_finite() {
            for pj in pred.iter_mut() {
                *pj /= total;
            }
            self.cycle_nowcast = pred;
        } else {
            self.seed_cycle_nowcast();
        }
    }

    /// The spread's cycle multiplier the market prices: the belief's `pi . m`
    /// under the nowcast, the true phase's `m` otherwise, blended toward the
    /// stationary mean by `corporate_spread_cycle`. `phase` is the true phase
    /// to read with the nowcast off.
    fn priced_spread_multiplier(&self, phase: crate::economy::CyclePhase) -> f64 {
        let phases = crate::economy::cycle::phase_cycle();
        let m = if self.params.cycle_nowcast_accuracy != 0.0 {
            let mut acc = 0.0;
            for (p, &ph) in self.cycle_nowcast.iter().zip(phases.iter()) {
                acc += p * crate::economy::central_bank::spread_multiplier_of(ph);
            }
            acc
        } else {
            crate::economy::central_bank::spread_multiplier_of(phase)
        };
        let s = self.params.corporate_spread_cycle;
        if s == 0.0 {
            return m;
        }
        (1.0 - s) * m + s * self.cycle_nowcast_terms.1
    }

    /// `cycle_nowcast_terms`: each phase's exit rate for the nowcast's
    /// predict step and the spread multiplier's occupancy mean for the
    /// blend, from the cycle's own mean sojourns (the ones
    /// `earnings_anticipation_terms` reads). Zeros with both dials off, so
    /// nothing is walked.
    fn derive_cycle_nowcast_terms(&self) -> ([f64; 5], f64) {
        if self.params.cycle_nowcast_accuracy == 0.0 && self.params.corporate_spread_cycle == 0.0 {
            return ([0.0; 5], 0.0);
        }
        let phases = crate::economy::cycle::phase_cycle();
        let (mean, cycle) = crate::economy::cycle::stationary_phase_shares_for(&self.cycle_spec());
        let mut lambda = [0.0; 5];
        let mut mbar = 0.0;
        for j in 0..5 {
            lambda[j] = if mean[j] > 0.0 { crate::mathx::min(1.0, 1.0 / mean[j]) } else { 0.0 };
            if cycle > 0.0 {
                mbar += mean[j] / cycle * crate::economy::central_bank::spread_multiplier_of(phases[j]);
            }
        }
        (lambda, mbar)
    }

    /// The level the earnings cycle is pulled toward in the current phase:
    /// `-depth` in a contraction or a trough, `+depth * upside` otherwise.
    fn earnings_cycle_target(&self) -> f64 {
        let p = &self.params;
        match self.economy.cycle_phase {
            crate::economy::CyclePhase::Contraction | crate::economy::CyclePhase::Trough => {
                -p.earnings_cycle_depth
            }
            _ => p.earnings_cycle_depth * p.earnings_cycle_upside,
        }
    }

    /// The VIX at which every variance coupling reads one.
    ///
    /// # At `vix_level_identity` 0.0 — the dial's value, unchanged
    ///
    /// A branch, not arithmetic. Every preset before pt-v19 reads exactly
    /// `params.market_vol_vix_anchor` at all four coupling sites, which is
    /// the expression each of them carried, so the whole change is
    /// bit-inert there.
    ///
    /// # At 1.0 — derived, and the dial is not read
    ///
    /// `market_vol_vix_anchor`'s own definition is "the VIX level at which
    /// a coupled target equals the baseline variance"
    /// (`market::factor_vol`). Under the identity a VIX IS a variance —
    /// `VIX = (1 + pi) * 100 * sqrt(252 * V)` — so that definition has an
    /// answer rather than a value:
    ///
    /// ```text
    /// anchor = (1 + pi) * 100 * sqrt(252 * V_uncond(roster, params))
    /// ```
    ///
    /// and the forward map `base * (1 - c + c (VIX / anchor)^2)` and the
    /// read-back then agree at the unconditional point BY CONSTRUCTION.
    /// That is the property `vix_implied_from_market` has always claimed in
    /// its comment ("so the loop is consistent") and the two constants
    /// never delivered: at the shipped pair the factor at its baseline is
    /// 12.05 per cent annualised against an anchor of 15.98.
    ///
    /// # Why it is roster-dependent, and why that is right
    ///
    /// A VIX prices its OWN index. A forty-name index with 5.3 effective
    /// names carries a larger idiosyncratic block than a five-hundred-name
    /// one, and the level a real VIX would take on it is genuinely
    /// different. So the anchor stops being a coefficient of the model and
    /// becomes a function of the model AND the universe — which means a
    /// preset record that quotes it must say which roster it was derived
    /// on.
    ///
    /// # The evaluation point needs no anchor, which is what makes it
    /// non-circular
    ///
    /// At `vix == anchor` every coupling's ratio is exactly 1.0 and every
    /// coupled quantity collapses to its own baseline: the factor's target
    /// is `base` (`1 - c + c * 1`), `sector_sigma_for` returns
    /// `sector_factor_sigma`, the per-name clamp reference is the sector's
    /// base variance and `apply_jumps`' rate scale is one. So
    /// `index_unconditional_variance` evaluates the identity at a point
    /// defined without reference to the number being derived.
    fn derive_vix_anchor(&self) -> f64 {
        if self.params.vix_level_identity == 0.0 {
            return self.params.market_vol_vix_anchor;
        }
        let names = self.index_variance_names();
        let bases = self.sector_base_variances_for(&names);
        let v = crate::market::index_var::index_unconditional_variance(
            &self.params, &names, self.sector_keys.len(), &bases);
        let derived =
            crate::market::index_var::vix_from_variance(self.params.vix_variance_premium, v);
        // A ROSTER WITH NO INDEX HAS NO VIX. An engine built with no public
        // names, or one whose whole roster is bankrupt, leaves an
        // unconditional variance of nothing but the market jump, and the
        // anchor it derives is a denominator four coupling sites divide by.
        // The dial is the honest answer there rather than a number the
        // roster cannot support -- and it is a FALLBACK with a condition,
        // not a clamp: on any roster that has an index at all this branch is
        // not taken.
        if derived.is_finite() && derived > 0.0 {
            derived
        } else {
            self.params.market_vol_vix_anchor
        }
    }

    /// The roster as the variance identity reads it: previous-close cap
    /// weights, betas, sector slots, GARCH states and caps.
    ///
    /// The weights are built from `market_cap`, which is the same quantity
    /// `advance_day_with` builds the day's index return from, so the
    /// variance and the return it is the variance OF are weighted the same
    /// way. Bankrupt and unlisted names are skipped by both.
    /// Whether the per-sector variance state is running: either of its
    /// two dials set. A branch, so every preset at (0.0, 0.0) never reads
    /// or writes the state.
    fn sector_state_on(&self) -> bool {
        self.params.sector_vol_alpha != 0.0 || self.params.sector_vol_beta != 0.0
    }

    /// Whether the per-name idiosyncratic variance state runs: either of
    /// `idio_vol_alpha`, `idio_vol_beta` and `idio_vol_jump_bump` off zero.
    /// Off on every preset.
    fn idio_state_on(&self) -> bool {
        self.params.idio_vol_alpha != 0.0
            || self.params.idio_vol_beta != 0.0
            || self.params.idio_vol_jump_bump != 0.0
    }

    /// The per-name idiosyncratic variance ratio of each name
    /// `index_variance_names` lists, in that order, or EMPTY when the state
    /// is off: the VIX identity then prices the idiosyncratic term exactly
    /// as before. An unseeded name reads 1.0.
    fn index_variance_idio_ratios(&self) -> Vec<f64> {
        if !self.idio_state_on() {
            return Vec::new();
        }
        let mut total = 0.0;
        for c in self.companies.iter() {
            if c.is_public && !c.is_bankrupt && c.stock.market_cap > 0.0 {
                total += c.stock.market_cap;
            }
        }
        if !(total > 0.0) {
            return Vec::new();
        }
        let mut out = Vec::with_capacity(self.companies.len());
        for (i, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.market_cap <= 0.0 {
                continue;
            }
            out.push(self.idio_ratio_at(i));
        }
        out
    }

    /// One name's idiosyncratic variance ratio as the draws read it: the
    /// state, or 1.0 where it is unseeded.
    fn idio_ratio_at(&self, index: usize) -> f64 {
        match self.idio_variance.get(index) {
            Some(v) if *v > 0.0 => *v,
            _ => 1.0,
        }
    }

    /// The close of the per-name idiosyncratic variance state
    /// (`ModelParams::idio_vol_alpha`). `u^2` is the session's own noise
    /// (the own-noise part the ticks drew, already at the state's scale)
    /// over its expected variance, `kappa^2 max(h, idio_sigma_floor)`
    /// (`kappa^2` is `noise_own_scale2`, which carries the state). The jump
    /// channel adds `idio_vol_jump_bump * (I - lambda)`, `I` whether the own
    /// jump the previous close applied was non-zero and `lambda` the rate it
    /// was drawn at (its expected variance over `jump_sigma_idio^2`). `h_day` is each
    /// name's GJR variance before tonight's `close_day`, the one the day's
    /// draws were scaled by. Then tonight's own jump becomes tomorrow's.
    /// Runs after `apply_jumps`, and not at all while the state is off.
    fn close_idio_state(&mut self, h_day: &[f64]) {
        if !self.idio_state_on() {
            return;
        }
        let p = &self.params;
        let (a, b) = (p.idio_vol_alpha, p.idio_vol_beta);
        let n = self.companies.len();
        for v in [
            &mut self.idio_variance,
            &mut self.idio_jump_pending,
            &mut self.idio_jump_var_pending,
            &mut self.idio_jump_today,
            &mut self.idio_jump_var_today,
        ] {
            if v.len() < n {
                v.resize(n, 0.0);
            }
        }
        let bump = p.idio_vol_jump_bump;
        let var1 = p.jump_sigma_idio * p.jump_sigma_idio;
        for i in 0..n {
            let prev = if self.idio_variance[i] > 0.0 { self.idio_variance[i] } else { 1.0 };
            let own = self.noise_parts.get(i).map(|x| x[2]).unwrap_or(0.0);
            let k2 = self.noise_own_scale2.get(i).copied().unwrap_or(0.0);
            let h = h_day.get(i).copied().unwrap_or(0.0);
            let expected = k2 * crate::mathx::max(h, p.idio_sigma_floor);
            // A name that did not trade today (bankrupt, private, or not yet
            // ticked) has no expected variance and keeps its state.
            if expected > 0.0 && own.is_finite() {
                let u2 = own * own / expected;
                // The jump channel, re-centred on the rate the jump realised
                // today was drawn at, so it adds nothing to the mean.
                let jump_term = if bump != 0.0 && var1 > 0.0 {
                    let rate = self.idio_jump_var_pending[i] / var1;
                    let hit = if self.idio_jump_pending[i] != 0.0 { 1.0 } else { 0.0 };
                    bump * (hit - rate)
                } else {
                    0.0
                };
                let raw = (1.0 - a - b) + a * u2 + b * prev + jump_term;
                self.idio_variance[i] = crate::mathx::max(
                    crate::mathx::min(raw, p.garch_ceiling_multiple),
                    p.garch_floor_multiple,
                );
            }
            self.idio_jump_pending[i] = self.idio_jump_today[i];
            self.idio_jump_var_pending[i] = self.idio_jump_var_today[i];
            self.idio_jump_today[i] = 0.0;
            self.idio_jump_var_today[i] = 0.0;
        }
    }

    /// One daily sigma per sector key from the state, or EMPTY when the
    /// state is off (the tick and the read-back then use the shared
    /// VIX-coupled sigma exactly as before). An unseeded sector reads the
    /// target it would be seeded at.
    fn sector_sigmas_now(&self) -> Vec<f64> {
        if !self.sector_state_on() {
            return Vec::new();
        }
        let target = crate::market::tick::sector_sigma_at(
            &self.params, &self.economy, self.vix_anchor);
        self.sector_variance
            .iter()
            .map(|s| if *s > 0.0 { target * crate::mathx::sqrt(*s) } else { target })
            .collect()
    }

    /// The jump excitation of each name `index_variance_names` lists, in
    /// that order, or EMPTY when the excitation dial is 0.0.
    fn index_variance_excitations(&self) -> Vec<f64> {
        if self.params.jump_idio_excitation == 0.0 {
            return Vec::new();
        }
        let mut total = 0.0;
        for c in self.companies.iter() {
            if c.is_public && !c.is_bankrupt && c.stock.market_cap > 0.0 {
                total += c.stock.market_cap;
            }
        }
        if !(total > 0.0) {
            return Vec::new();
        }
        let mut out = Vec::with_capacity(self.companies.len());
        for (i, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.market_cap <= 0.0 {
                continue;
            }
            out.push(self.jump_excitation.get(i).copied().unwrap_or(0.0));
        }
        out
    }

    /// The close of the per-sector variance state: a symmetric GARCH(1,1)
    /// per sector on the day's accumulated sector factor, reverting to the
    /// VIX-coupled sigma the stateless draw uses as its long-run level,
    /// clamped to the per-name multiples. On the tape the sector residual
    /// has persistence 0.971 +/-
    /// 0.018 with a shock share of 0.063 +/- 0.018.
    fn close_sector_state(&mut self) {
        if !self.sector_state_on() {
            return;
        }
        let p = &self.params;
        // The scale the day's draws were made at. The ratio form: the
        // GARCH(1,1) runs on the factor standardised by the VIX-coupled
        // target, so the coupling's own state is kept whole and the
        // sector's memory net of it is what the dials carry (19.7).
        let target = if self.sector_target_day > 0.0 {
            self.sector_target_day
        } else {
            let sigma = crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
            sigma * sigma
        };
        let a = p.sector_vol_alpha;
        let b = p.sector_vol_beta;
        for k in 0..self.sector_keys.len() {
            let prev = if self.sector_variance[k] > 0.0 { self.sector_variance[k] } else { 1.0 };
            let d = self.sector_day_factor[k];
            let z2 = if target > 0.0 { d * d / target } else { 0.0 };
            let raw = (1.0 - a - b) + a * z2 + b * prev;
            self.sector_variance[k] = crate::mathx::max(
                crate::mathx::min(raw, p.garch_ceiling_multiple),
                p.garch_floor_multiple,
            );
            self.sector_day_factor[k] = 0.0;
        }
    }

    fn index_variance_names(&self) -> Vec<crate::market::index_var::NameVariance> {
        self.index_variance_names_with(None)
    }

    /// [`Self::index_variance_names`] with each slot's GARCH variance taken
    /// from `garch` when given (a projection of the close,
    /// `rate_intraday_live`).
    fn index_variance_names_with(
        &self,
        garch: Option<&[f64]>,
    ) -> Vec<crate::market::index_var::NameVariance> {
        let mut total = 0.0;
        for c in self.companies.iter() {
            if c.is_public && !c.is_bankrupt && c.stock.market_cap > 0.0 {
                total += c.stock.market_cap;
            }
        }
        if !(total > 0.0) {
            return Vec::new();
        }
        let mut out = Vec::with_capacity(self.companies.len());
        for (slot, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.market_cap <= 0.0 {
                continue;
            }
            out.push(crate::market::index_var::NameVariance {
                weight: c.stock.market_cap / total,
                beta: c.stock.beta.unwrap_or(1.0),
                sector: self
                    .sector_keys
                    .iter()
                    .position(|k| *k == c.sector)
                    .unwrap_or(usize::MAX),
                garch_variance: match garch {
                    Some(g) => g.get(slot).copied().unwrap_or(c.stock.garch_variance),
                    None => c.stock.garch_variance,
                },
                market_cap: c.stock.market_cap,
            });
        }
        out
    }

    /// The sector base variances of the names the identity kept, in the
    /// same order — the clamp reference each of their GARCH processes
    /// reverts between.
    ///
    /// `sector_base_variances()` is per COMPANY SLOT and includes the
    /// bankrupt and unlisted names `index_variance_names` drops, so the two
    /// lists are not the same length and pairing them by index would
    /// silently mis-assign a floor to a name. Built by walking the roster
    /// under the same filter instead.
    fn sector_base_variances_for(
        &self,
        names: &[crate::market::index_var::NameVariance],
    ) -> Vec<f64> {
        let all = self.sector_base_variances();
        let mut out = Vec::with_capacity(names.len());
        for (i, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.market_cap <= 0.0 {
                continue;
            }
            out.push(all.get(i).copied().unwrap_or(c.stock.garch_variance));
        }
        out
    }

    /// The index's one-day-ahead conditional variance, from the states the
    /// engine holds at the close.
    ///
    /// Called after `close_market` has advanced the factor's variance, the
    /// per-name GARCH states and the jumps, so every state it reads is
    /// TOMORROW's — which is what a one-day-ahead variance means and what
    /// an implied volatility prices. The VIX-driven quantities (the sector
    /// draw's sigma, the jump arrival rate, **and the crisis blend's
    /// spike**) are read at the close's VIX, which is the information the
    /// close has.
    ///
    /// # The regime, and why it is passed rather than assumed
    ///
    /// The variance this returns is now REGIME-AWARE, which is charter bar
    /// B4. Two mechanisms the closed form used to be blind to reach it:
    ///
    /// - the **crash amplifier**, through the conditional factor variance
    ///   already passed as `factor_variance` — `index_var` forms the regime
    ///   ratio `sqrt(v_f) / market_factor_sigma` from it, which is the
    ///   quantity `factors.rs` builds `shock_magnitude` from, so there is
    ///   no new state and nothing here to keep in step;
    /// - the **crisis blend**, through `crisis_spike_for` on the close's VIX
    ///   and the remembered universe stress. That is the SAME function
    ///   `compute_tick` calls, not a copy of it, so the read-back and the
    ///   tick cannot disagree about when a crisis is on;
    /// - the **downside transmission tilt and its lagged wire**, through
    ///   `market_vol.prev_day_down()` — the same accessor `simulate_tick`
    ///   passes into `TickInputs`, read here AFTER `close_day_at` has rolled
    ///   the day's factor into `prev_day_factor`, so it is the bit
    ///   tomorrow's ticks will run under and not today's.
    ///
    /// `crisis_blend_variance_damp` is the one piece of the blend this does
    /// NOT carry: it scales the injection by a clamped fractional power of
    /// the draw itself, which is not a polynomial in `z` and has no moment
    /// in `phi` and `Phi`. It is 0.0 in every shipped preset and inert there
    /// through `factors.rs`'s own branch. A preset that set it would have
    /// this read-back price the UNDAMPED blend and therefore overstate the
    /// crisis, and that is declared here and on
    /// [`IndexVarianceTerms::crisis_raw`] rather than caught at runtime —
    /// the residual is the residual, and a panic on a legal dial
    /// combination would be a worse answer than a stated one.
    ///
    /// [`IndexVarianceTerms::crisis_raw`]:
    ///     crate::market::index_var::IndexVarianceTerms::crisis_raw
    fn index_conditional_variance_terms_now(
        &self,
    ) -> crate::market::index_var::IndexVarianceTerms {
        self.index_conditional_variance_terms_at(self.market_vol.variance())
    }

    /// [`Self::index_conditional_variance_terms_now`] with the market
    /// factor's variance supplied rather than read from the state, so a
    /// forced close can ask what the identity would read at a variance the
    /// state does not hold yet. `_now` passes `market_vol.variance()`, the
    /// same f64 it passed before this existed.
    fn index_conditional_variance_terms_at(
        &self,
        factor_variance: f64,
    ) -> crate::market::index_var::IndexVarianceTerms {
        self.index_conditional_variance_terms_with(
            factor_variance, self.market_vol.prev_day_down(), None)
    }

    /// [`Self::index_conditional_variance_terms_at`] with the lagged
    /// transmission wire's down bit and the names' GARCH variances supplied,
    /// for a projection of the close (`rate_intraday_live`).
    fn index_conditional_variance_terms_with(
        &self,
        factor_variance: f64,
        prev_day_down: bool,
        garch: Option<&[f64]>,
    ) -> crate::market::index_var::IndexVarianceTerms {
        let names = self.index_variance_names_with(garch);
        let sector_sigma = crate::market::tick::sector_sigma_at(
            &self.params, &self.economy, self.vix_anchor);
        let ratio = self.economy.vix / self.vix_anchor;
        let rate_scale = if self.params.jump_vix_coupling == 0.0 {
            1.0
        } else {
            (1.0 - self.params.jump_vix_coupling)
                + ((self.params.jump_vix_coupling * ratio) * ratio)
        };
        let crisis_spike = crate::market::tick::crisis_spike_for(
            &self.params, self.economy.vix, self.universe_stress);
        // The two per-component states, empty when their dials are 0.0, in
        // which case the read-back prices the shared sigma and the
        // independent arrivals exactly as before.
        let sigmas = if self.sector_state_on() {
            self.sector_sigmas_now()
        } else {
            vec![sector_sigma; self.sector_keys.len()]
        };
        let excitations = self.index_variance_excitations();
        let idio_ratios = self.index_variance_idio_ratios();
        crate::market::index_var::index_conditional_variance_terms_with_states(
            &self.params,
            &names,
            self.sector_keys.len(),
            factor_variance,
            &sigmas,
            &excitations,
            &idio_ratios,
            rate_scale,
            crisis_spike,
            // THE LAGGED TRANSMISSION WIRE, and the timing is the point.
            // `close_day_at` has already rolled `day_factor` into
            // `prev_day_factor`, so this bit is the one TOMORROW's ticks
            // will read — which is what a one-day-ahead variance needs. The
            // same accessor the tick calls, not a copy of the comparison.
            prev_day_down,
            crate::market::index_var::intraday_variance_factor(),
        )
    }

    /// The terms of the index variance the last VIX update read, or `None`
    /// if no day has advanced under the identity. See
    /// [`Engine::last_index_variance`] for why it is remembered rather
    /// than recomputed.
    pub fn last_index_variance(
        &self,
    ) -> Option<crate::market::index_var::IndexVarianceTerms> {
        self.last_index_variance
    }

    /// The variance targets the last close reverted toward: `(fast, slow)`.
    ///
    /// `None` before any close. The SLOW element is `None` whenever the
    /// preset has no slow component (`market_vol_slow_weight == 0.0`,
    /// which pt-v1 through pt-v3 and `PT_V1` all ship) — on that branch
    /// `factor_vol.rs` returns before a slow target is ever computed, so
    /// there is none to report. On such a preset the FIRST element is THE
    /// target, not a "fast" one.
    ///
    /// So a `None` in the outer option and a `None` in the inner one mean
    /// different things: no close yet, versus no slow component. A fork
    /// carries the reading (`Engine` derives `Clone`); a restore does not,
    /// because the snapshot has no such key — which is what keeps this out
    /// of the state hash — so a restored engine keeps its own last reading
    /// until its next day.
    pub fn market_variance_target(&self) -> Option<(f64, Option<f64>)> {
        self.last_market_targets
    }

    /// Draw the day-zero cycle phase and its age from the cycle's own
    /// stationary law, and say whether it drew.
    ///
    /// A BRANCH at 0.0, which is every preset before pt-v19: it returns
    /// before touching the economy or the generator, so construction is
    /// what it always was and no arithmetic changes. That is what makes
    /// bit-identity a property of the control flow rather than of
    /// floating-point luck -- and it is asserted anyway, over 9,000 daily
    /// returns on thirty seeds, against a digest taken from a build of the
    /// commit this branch was cut from, by
    /// `tests/test_stationary_opening.py`.
    ///
    /// The uniforms come from the ECONOMY substream, the stream
    /// [`Engine::burn_in_economy`] already consumes, so the market's
    /// day-zero draws sit exactly where they sat and only this domain's
    /// sequence moves. Two draws, counted through the same `Counting`
    /// wrapper the daily step uses, so `draws_consumed` records them.
    ///
    /// They are recorded at [`Site::EconomyCycle`], which is the site they
    /// belong to: what is being drawn is the cycle's own transition law,
    /// read as a distribution instead of rolled forward one day at a time.
    /// No new site is declared, so the draw-schedule registry in
    /// `python/tradefloor/noise.py` is unchanged.
    ///
    /// See [`ModelParams::cycle_stationary_opening`] for the identity, why
    /// the AGE is the part that cannot be skipped, and why the field
    /// relaxation is `macro_burn_in_days`' half of the same opening.
    fn draw_stationary_opening(&mut self) -> bool {
        if self.params.cycle_stationary_opening == 0.0 {
            return false;
        }
        let mut rng = std::mem::replace(&mut self.economy_rng, GameRng::new(0, MAIN_STREAM));
        let mut counting = Counting {
            inner: &mut rng,
            count: 0,
        };
        counting.site(Site::EconomyCycle, 0);
        let u_phase = counting.next_f64();
        let u_age = counting.next_f64();
        let consumed = counting.count;
        self.economy_rng = rng;
        self.draws.economy += consumed;

        let (phase, months) = crate::economy::stationary_opening_for(
            &self.cycle_spec(),
            u_phase,
            u_age,
        );
        self.economy.cycle_phase = phase;
        self.economy.months_in_current_phase = months;
        true
    }

    /// Advance the economy alone to the state its own dynamics reach,
    /// before day zero.
    ///
    /// A BRANCH at 0.0 days, which is every preset before pt-v18: nothing
    /// runs, nothing is drawn, and construction is what it always was.
    ///
    /// # What it fixes
    ///
    /// The economy opens at unemployment 4.00, inflation 2.00 and a
    /// corporate yield of 4.56, and settles at 2.50, 2.74 and 4.82. So a
    /// certified year is spent travelling rather than at rest, and the
    /// travel is a one-way cost to the multiple. The length is measured
    /// rather than round: the last field to enter one stationary standard
    /// deviation of its mean is the corporate yield, on day 755.
    ///
    /// # Three things it does deliberately
    ///
    /// The draws come from the economy's own substream, so the market's
    /// draws for day 0 onward sit exactly where they did. A burn-in
    /// consumes economy draws by running the economy, which is the
    /// mechanism rather than a side effect of it.
    ///
    /// The phase is held for the burn-in's length, because under the
    /// hazard as written 755 days would leave an expansion. It is held by
    /// restoring BOTH the phase and its age whenever a transition fires,
    /// rather than the phase alone. Restoring the phase alone leaves
    /// `months_in_current_phase` at zero, which fires the phase-change
    /// shock on the next day and lifts growth half a point above its
    /// target for the rest of the burn-in. The transition roll still
    /// happens and still consumes its uniform either way.
    ///
    /// The clock is reset to zero at the end, so the year opens at the
    /// start of a phase as every certified year has. Keeping the burn-in's
    /// age would open a random distance into an expansion, which is a
    /// different certified year and a choice rather than a correction.
    ///
    /// # Both of those stop, and only when the opening is DRAWN
    ///
    /// Holding the phase and resetting the clock restore the same point
    /// this burn-in started from, which is the defect
    /// [`ModelParams::cycle_stationary_opening`] exists for: the fields
    /// settle and the cohort survives. When that dial has drawn the phase
    /// and its age, the burn-in runs FREE -- the phase is not held and the
    /// clock is not reset -- because the chain is already in its
    /// stationary law, which a free run preserves by definition, and what
    /// is left to do is relax the fields under the phase path the run has
    /// just lived through. Holding a drawn contraction for 755 days
    /// instead would drive unemployment and the policy rate far past any
    /// state a contraction reaches, and the fields would open inconsistent
    /// with the mix.
    ///
    /// At the shipped default the dial draws nothing and every line here
    /// is the line that stood before it.
    ///
    /// # The growth term's base
    ///
    /// `gdp` and `cpi` compound on every one of these days, so the base of
    /// `earnings_nominal_growth` is re-read at the end. Left at the
    /// pre-burn-in value it would open every valuation about nine per cent
    /// above its earnings, which is a level jump nobody asked for and
    /// which the ratio was never meant to carry.
    fn burn_in_economy(&mut self) -> Vec<(crate::economy::CyclePhase, f64)> {
        // The day-zero phase and its age first, so what follows relaxes
        // the fields under the phase the run will OPEN in. Inert at the
        // default, where it returns without drawing and every line below
        // is the line that stood here.
        //
        // The economy runs alone from here to the end of the burn-in, so the
        // cycle reads `cycle_equity_hazard_opening` for the index it does
        // not have (0.0 on every preset, where nothing changes).
        self.economy_alone = true;
        let drawn = self.draw_stationary_opening();
        // The market's belief on the phase the burn-in opens in; the
        // construction puts it back on the opening phase afterwards. Nothing
        // with `cycle_nowcast_accuracy` at 0.0.
        self.seed_cycle_nowcast();
        if self.params.macro_burn_in_days <= 0.0 {
            self.economy_alone = false;
            return Vec::new();
        }
        let days = self.params.macro_burn_in_days as i64;
        let phase = self.economy.cycle_phase;
        // The phase after each day, for the market's prehistory
        // (`market_prehistory_sessions`); nothing is kept with it at 0.0.
        let keep = self.params.market_prehistory_sessions > 0.0;
        let mut path = Vec::with_capacity(if keep { days as usize } else { 0 });
        for day in 1..=days {
            let months_before = self.economy.months_in_current_phase;
            self.advance_day(&DayAdvanceRequest {
                volatility: 1.0,
                active_shocks: &[],
                market_return_pct: 0.0,
                game_day: day,
                timestamp: day * 24 * 60,
            });
            if !drawn && self.economy.cycle_phase != phase {
                self.economy.cycle_phase = phase;
                self.economy.months_in_current_phase =
                    months_before + 1.0 / self.macro_calendar().month_f64();
            }
            if keep {
                path.push((self.economy.cycle_phase, self.economy.months_in_current_phase));
            }
        }
        if !drawn {
            self.economy.months_in_current_phase = 0.0;
            // The clock the run opens on, which the held burn-in resets.
            if let Some(last) = path.last_mut() {
                last.1 = 0.0;
            }
        }
        // PUT THE CALENDARS BACK ON THE CALLER'S AXIS.
        //
        // The loop above ran on the caller's own day numbering, 1..=days
        // with `timestamp: day * 24 * 60`, so every clock the macro chain
        // keeps as an ABSOLUTE time came out of it `days` in the caller's
        // FUTURE. The run then starts at day 1 (`day_count` is 0 at
        // construction and the first close makes it 1), and each of those
        // clocks is read as a difference against the current day:
        // `update_central_bank` refuses a meeting while
        // `current_timestamp < next_meeting_date`, and the OPEC arm fires on
        // `day - oil_last_opec_day >= OIL_OPEC_INTERVAL`.
        //
        // Measured on `pt-v18` before this, against `pt-v16`: the first
        // central-bank meeting moved from day 45 to day 781 and the first
        // OPEC decision from day 90 to day 810, so 13 meetings landed in
        // 1,400 days where pt-v16 held 29 -- and the certified 252-day
        // horizon contained no monetary policy and no OPEC decision at all.
        // Two macro subsystems, inert for the whole window every published
        // figure is measured on.
        //
        // This is the same restoration the phase lines above perform and it
        // was missing: this function's contract, in `macro_burn_in_days`'
        // own docstring, is that it "HOLDS the phase and RESETS its clock,
        // so it restores the same point after settling the fields". The
        // cycle's two fields were restored and these three were not.
        //
        // SHIFTED, not reset. The burn-in's whole purpose is that the fields
        // arrive settled, and the meeting cadence is one of them: an opening
        // that settles into an inflation crisis carries the 21-30 day
        // cadence `update_central_bank` sets there, where resetting to the
        // construction value would hand day 1 a calm calendar under crisis
        // fields. Every consumer reads a DIFFERENCE, so translating the
        // origin preserves each interval exactly. It is what running the
        // burn-in on days `-days..=0` would have produced, without moving
        // the burn-in's own trajectory to get there.
        let elapsed_minutes = days * 24 * 60;
        self.central_bank.next_meeting_date -= elapsed_minutes;
        self.central_bank.last_meeting_date -= elapsed_minutes;
        self.economy.oil_last_opec_day -= days;
        self.nominal_output_base = self.economy.gdp * self.economy.cpi;
        self.economy_alone = false;
        path
    }

    /// The model coefficients this engine runs. Read-only: an engine's
    /// model is fixed at construction, so its fingerprint describes the
    /// whole run rather than the moment someone asked.
    pub fn params(&self) -> &ModelParams {
        &self.params
    }

    /// `self.params().fingerprint()`, worked out once per engine. A shipped
    /// preset's name, or `custom-XXXXXXXX`. See the field for why it is
    /// kept.
    pub fn model_fingerprint(&self) -> &str {
        self.model_fingerprint.get_or_init(|| self.params.fingerprint())
    }

    /// The macro calendar `macro_calendar_days_per_year` selects.
    pub fn macro_calendar(&self) -> crate::economy::MacroCalendar {
        crate::economy::MacroCalendar::from_days_per_year(self.params.macro_calendar_days_per_year)
    }

    /// The clock and phase table the business cycle reads.
    pub fn cycle_spec(&self) -> crate::economy::CycleSpec {
        crate::economy::CycleSpec {
            per_month: self.params.cycle_hazard_per_month,
            month_days: self.macro_calendar().month_f64(),
            us: self.params.cycle_us_calibration != 0.0,
            equity_hazard: self.params.cycle_equity_hazard,
            equity_knee: self.params.cycle_equity_hazard_knee,
            equity_opening: if self.economy_alone {
                self.params.cycle_equity_hazard_opening
            } else {
                0.0
            },
            inflation_hazard: self.params.cycle_inflation_hazard,
            rate_hazard: self.params.cycle_rate_hazard,
        }
    }

    /// The VIX at which every variance coupling reads one. See the field.
    ///
    /// Read-only and fixed for the engine's life, like `params`: it is
    /// derived once from the roster and the coefficients, so quoting it
    /// describes the whole run.
    pub fn vix_anchor(&self) -> f64 {
        self.vix_anchor
    }

    // ── Draw delegation ───────────────────────────────────────────────────

    /// Take one uniform from the EXTERNAL stream, on the embedder's behalf.
    ///
    /// The embedder's own subsystems — events, corporate actions, earnings —
    /// drew from the engine's one shared stream in the reference, which
    /// meant an extra event roll on the embedder's side moved every price.
    /// Since the stream split these draws come from a substream of the same
    /// root seed: still fully seed-determined and reproducible, but taking
    /// one — or a thousand — leaves the market's own sequence untouched.
    /// That isolation is what lets an embedder change what IT rolls without
    /// invalidating every seeded market trajectory it embeds.
    pub fn draw_uniform(&mut self) -> f64 {
        self.draws.external += 1;
        self.external_rng.site(Site::External, 0);
        self.external_rng.next_f64()
    }

    /// Take one normal from the EXTERNAL stream. See
    /// [`Engine::draw_uniform`]; the Box-Muller spare involved is the
    /// external stream's own and is never visible to the market.
    pub fn draw_normal(&mut self) -> f64 {
        self.draws.external += 1;
        self.external_rng.site(Site::External, 0);
        self.external_rng.next_normal()
    }

    /// The exact position of all three streams.
    ///
    /// The half of a checkpoint that cannot be reconstructed from the roster.
    /// Every other piece of engine state -- prices, GARCH variance, maker
    /// inventory, the mispricing carry -- is observable through `column()`, so
    /// an embedder that persists its own instruments already has it. The
    /// stream positions are not observable that way, and without them a
    /// restored market continues from a different sequence while looking
    /// correct.
    pub fn rng_state(&self) -> EngineRngState {
        EngineRngState {
            market: self.market_rng.snapshot(),
            economy: self.economy_rng.snapshot(),
            external: self.external_rng.snapshot(),
            jumps: self.jump_rng.snapshot(),
            volume: self.volume_rng.snapshot(),
            volume_idio: self.volume_idio_rng.snapshot(),
            news: self.news_rng.snapshot(),
            overnight: self.overnight_rng.snapshot(),
            market_vol_level: self.market_vol_level_rng.snapshot(),
            crisis_epicentre: self.crisis_epicentre_rng.snapshot(),
        }
    }

    /// Put all three generators back to a captured position.
    ///
    /// Deliberately narrow: it restores the STREAMS and nothing else. Company
    /// state is the caller's to restore, because the caller is the one that
    /// persisted it. A method that pretended to restore everything would have
    /// to be kept in step with every field ever added to a company, and would
    /// fail silently the first time it was not.
    pub fn set_rng_state(&mut self, state: EngineRngState) {
        self.market_rng = GameRng::restore(state.market);
        self.economy_rng = GameRng::restore(state.economy);
        self.external_rng = GameRng::restore(state.external);
        self.jump_rng = GameRng::restore(state.jumps);
        self.volume_rng = GameRng::restore(state.volume);
        self.volume_idio_rng = GameRng::restore(state.volume_idio);
        self.news_rng = GameRng::restore(state.news);
        self.overnight_rng = GameRng::restore(state.overnight);
        self.market_vol_level_rng = GameRng::restore(state.market_vol_level);
        self.crisis_epicentre_rng = GameRng::restore(state.crisis_epicentre);
    }

    /// Cumulative draws across all three streams. The per-stream split is
    /// [`Engine::draws_by_stream`].
    pub fn draws_consumed(&self) -> usize {
        self.draws.total()
    }

    /// Cumulative draws, per stream. Diagnostic (D-R1): equality of the
    /// market counts between two runs is what "the two markets saw the same
    /// noise" means operationally.
    pub fn draws_by_stream(&self) -> StreamDraws {
        self.draws
    }

    // ── Draw addressing ───────────────────────────────────────────────────

    fn stream_rng(&self, id: u32) -> &GameRng {
        match id {
            stream::MARKET => &self.market_rng,
            stream::ECONOMY => &self.economy_rng,
            stream::EXTERNAL => &self.external_rng,
            stream::JUMPS => &self.jump_rng,
            stream::VOLUME => &self.volume_rng,
            stream::NEWS => &self.news_rng,
            stream::VOLUME_IDIO => &self.volume_idio_rng,
            stream::OVERNIGHT => &self.overnight_rng,
            stream::MARKET_VOL_LEVEL => &self.market_vol_level_rng,
            stream::CRISIS_EPICENTRE => &self.crisis_epicentre_rng,
            _ => panic!("unknown stream {id}"),
        }
    }

    fn stream_rng_mut(&mut self, id: u32) -> &mut GameRng {
        match id {
            stream::MARKET => &mut self.market_rng,
            stream::ECONOMY => &mut self.economy_rng,
            stream::EXTERNAL => &mut self.external_rng,
            stream::JUMPS => &mut self.jump_rng,
            stream::VOLUME => &mut self.volume_rng,
            stream::NEWS => &mut self.news_rng,
            stream::VOLUME_IDIO => &mut self.volume_idio_rng,
            stream::OVERNIGHT => &mut self.overnight_rng,
            stream::MARKET_VOL_LEVEL => &mut self.market_vol_level_rng,
            stream::CRISIS_EPICENTRE => &mut self.crisis_epicentre_rng,
            _ => panic!("unknown stream {id}"),
        }
    }

    /// The market jump's effective daily intensity, here and now.
    ///
    /// The threshold `apply_jumps` compares its market uniform against,
    /// at this engine's dials and its current VIX. A caller that wants to
    /// know whether a jump can fire asks for this rather than restating
    /// the scaling: a Python copy of it had already diverged on a zero
    /// anchor, where the copy returned the base intensity and this
    /// divides by zero, and CONTRIBUTING keeps a modelling decision in
    /// one place for exactly that reason.
    ///
    /// Takes no draw and changes nothing.
    ///
    /// It has to agree with `apply_jumps`, and
    /// `test_the_intensity_is_the_threshold_the_jump_fires_on` holds the
    /// two together by behaviour: a market uniform patched just under it
    /// fires and one just over it does not.
    #[rustfmt::skip]
    #[allow(clippy::let_and_return, reason = "the body is generated by tools/mechanism/emit.py, which binds every mechanism output by name")]
    pub fn market_jump_intensity(&self) -> f64 {
        // mechanism:jumps.intensity_market begin -- generated by tools/mechanism/emit.py from
        // tools/mechanism/mechanisms/jumps.py; do not edit by hand.
        let p = &self.params;
        let ratio = self.economy.vix / self.vix_anchor;
        let rate_scale = if p.jump_vix_coupling == 0.0 { 1.0 } else { (1.0 - p.jump_vix_coupling) + ((p.jump_vix_coupling * ratio) * ratio) };
        let intensity_market = if p.jump_vix_coupling == 0.0 { p.jump_intensity_market } else { p.jump_intensity_market * rate_scale };
        intensity_market
        // mechanism:jumps.intensity_market end
    }

    /// The day the next `open_market` opens. The engine cannot know it on
    /// its own: `run_session` and `tick` take no day, and only the caller
    /// that numbers its days does.
    ///
    /// Sets both of the engine's day numbers: the label the draw log, the
    /// day marks and the book's fill stamps carry, and the elapsed trading
    /// days the valuation reads (`elapsed_days`). A caller of the core that
    /// counts its days from zero, which is what this has always asked of
    /// one, sees one number and the behaviour it always had.
    ///
    /// It was a label until pt-v18. `buyback_payout_share` made the number
    /// the valuation's clock as well: the buyback factor is an earnings
    /// yield over elapsed time. With one field serving both, a label moved
    /// the market. `run_days(first_day=1000)` on pt-v20 priced every name as
    /// though a thousand days had passed, 0.17 in log price within thirty
    /// days, and the log recorded no input that said so. So the two are two
    /// fields now, and [`Engine::set_day_label`] moves the label alone.
    ///
    /// `open_market` is the only caller that must run before a draw is
    /// taken, because the day mark and the day's news draws are taken
    /// there, and `PyEngine::restore_state` sets both numbers for the
    /// mid-day case that no open follows.
    ///
    /// # A host numbers its days
    ///
    /// Nothing else advances the clock: not `open_market`, not a session
    /// and not `close_day`. A host that runs its own day loop calls this
    /// with the trading day, counted from zero, before each `open_market`,
    /// as the Python package's `run_days` does. Without it the valuation's
    /// clock stays at day zero for the whole run. On a preset with an
    /// earnings calendar, dividends or buybacks (pt-v21) the calendars stop
    /// there: a name whose report or ex-dividend date falls on day zero
    /// reports, or goes ex, at every session, every other name never does,
    /// and the buyback yield never accrues. Measured on pt-v21, 108 names,
    /// 20 seeds, 504 sessions: the median name's volatility reads 19.7 per
    /// cent a year with the clock left at zero and 20.8 with the days
    /// numbered.
    pub fn set_current_day(&mut self, day: i64) {
        self.set_day_label(day);
        self.elapsed_days = day;
    }

    /// Move the label alone: the day the draw log, the day marks and the
    /// book's fill stamps carry. Nothing that prices reads it.
    pub fn set_day_label(&mut self, day: i64) {
        self.current_day = day;
        for id in 0..stream::COUNT as u32 {
            self.stream_rng_mut(id).set_day(day);
        }
    }

    /// Set the valuation's clock alone: trading days elapsed at the open of
    /// the current day. See `elapsed_days` on the struct.
    pub fn set_elapsed_days(&mut self, days: i64) {
        self.elapsed_days = days;
    }

    pub fn current_day(&self) -> i64 {
        self.current_day
    }

    /// Trading days elapsed at the open of the current day, the number the
    /// valuation reads.
    pub fn elapsed_days(&self) -> i64 {
        self.elapsed_days
    }

    /// The ticks run on the current day: the tick a fill is stamped with.
    pub fn session_ticks(&self) -> u32 {
        self.ticks_today()
    }

    /// Put back the ticks a restored day had run, for the fills stamped
    /// before the next open. See `carried_ticks` on the struct.
    pub fn set_session_ticks(&mut self, ticks: u32) {
        self.carried_ticks = Some(ticks);
    }

    /// `(uniforms, normals)` taken so far on each stream, by stream id.
    pub fn stream_positions(&self) -> [(u64, u64); stream::COUNT] {
        let mut out = [(0u64, 0u64); stream::COUNT];
        for (id, slot) in out.iter_mut().enumerate() {
            *slot = self.stream_rng(id as u32).positions();
        }
        out
    }

    /// Install one substitution at `(stream, kind, index)`.
    pub fn patch_draw(&mut self, stream_id: u32, kind: DrawKind, index: u64, value: f64) {
        self.stream_rng_mut(stream_id).patch(kind, index, value);
    }

    pub fn draw_overlay(&self, stream_id: u32) -> Option<&DrawOverlay> {
        self.stream_rng(stream_id).overlay()
    }

    pub fn set_draw_overlay(&mut self, stream_id: u32, overlay: Option<DrawOverlay>) {
        self.stream_rng_mut(stream_id).set_overlay(overlay);
    }

    /// Record every draw one stream takes on days in `[from_day, to_day]`.
    pub fn enable_draw_log(&mut self, stream_id: u32, from_day: i64, to_day: i64) {
        self.stream_rng_mut(stream_id).enable_log(from_day, to_day);
    }

    /// Drop what every stream's log holds, keeping each range.
    ///
    /// Recording only: the counters, the ranges and every generator's
    /// position are untouched, so a run continues identically. It is for
    /// a copy that will never be asked what it recorded, which is what
    /// the explanation store keeps.
    pub fn clear_draw_log_records(&mut self) {
        for id in 0..stream::COUNT as u32 {
            self.stream_rng_mut(id).clear_log_records();
        }
    }

    pub fn draw_log(&self, stream_id: u32) -> &[DrawRecord] {
        match self.stream_rng(stream_id).log() {
            Some(log) => &log.records,
            None => &[],
        }
    }

    pub fn day_marks(&self) -> &[DayMark] {
        &self.day_marks
    }

    /// Drop every day mark.
    ///
    /// The marks describe the days THIS engine opened, so a restore has to
    /// drop them: restoring a snapshot replaces the run, and marks kept
    /// across it named days the restored engine never ran. An engine that
    /// ran two days, restored a three-day snapshot and ran two more reported
    /// marks for days 0, 1, 3 and 4. `market_day_layout` reads the newest
    /// match and so resolved correctly throughout, which is why this was
    /// invisible. The marks are per-run diagnostic state and a snapshot does
    /// not carry them, so a restored engine starts with none and gains one
    /// per day it opens itself.
    pub fn clear_day_marks(&mut self) {
        self.day_marks.clear();
    }

    /// Where each active company's market-stream normals sit on `day`,
    /// from that day's mark and the per-tick schedule in [`DayMark`].
    pub fn market_day_layout(&self, day: i64) -> Option<Vec<MarketDayLayout>> {
        let mark = self.day_marks.iter().rev().find(|m| m.day == day)?;
        let per_tick = 1 + mark.sectors as u64 + mark.active.len() as u64;
        let base = mark.positions[stream::MARKET as usize].1;
        Some(
            mark.active
                .iter()
                .enumerate()
                .map(|(k, &company)| MarketDayLayout {
                    company,
                    first: base + 1 + mark.sectors as u64 + k as u64,
                    stride: per_tick,
                    ticks: mark.ticks,
                })
                .collect(),
        )
    }

    // ── The tick ──────────────────────────────────────────────────────────

    /// Run one simulated minute.
    ///
    /// A closed market costs zero draws and changes nothing — the guard is
    /// inside [`simulate_market_tick`] and precedes every draw site, which
    /// matters because most of the clock is closed.
    pub fn tick(&mut self, request: &TickRequest) -> TickOutcome {
        // The population acts first, on an open tick, and only on an engine
        // that holds one (`crate::population`).
        let population_open = self.population.is_some()
            && get_market_status(request.time) == MarketStatus::Open;
        if population_open {
            self.population_before_tick();
        }
        // The market generator is moved out, used, and moved back.
        // `tick_inner` takes `&mut self`, so it cannot also borrow
        // `self.market_rng` — and swapping is clearer than duplicating the
        // tick body for the two cases. The placeholder is never drawn from:
        // the real generator is restored before this method returns.
        let mut rng = std::mem::replace(&mut self.market_rng, GameRng::new(0, MAIN_STREAM));
        let mut counting = Counting {
            inner: &mut rng,
            count: 0,
        };
        let mut outcome = self.tick_inner(request, &mut counting, SettleDrawPolicy::FourAlways);
        let consumed = counting.count;
        self.market_rng = rng;
        self.draws.market += consumed;
        outcome.draws_consumed = consumed;
        if let Some(mark) = self.day_marks.last_mut() {
            mark.ticks += 1;
        } else if let Some(ticks) = self.carried_ticks.as_mut() {
            // A restored day with no mark of its own: counted here, so the
            // fills of its later ticks carry the tick the original's did.
            *ticks += 1;
        }
        // The rate indices' minute, after the equities' and reading nothing
        // they wrote: the economy does not move inside a tick, and the flow
        // an instrument reads is keyed by its own ticker, which no equity
        // carries. No draw.
        //
        // Under `rate_intraday_live` the minute's print is around the live
        // mark, the published curve plus what the session so far adds to
        // tonight's expected curve, refreshed on the grid.
        //
        // Under `vix_intraday_live` the live VIX refreshes on the same grid,
        // from the same projection of the close when both are due. It reads
        // the state and writes only its own mark.
        let open = crate::market::get_market_status(request.time) == crate::market::MarketStatus::Open;
        let rate_live_on = self.params.rate_intraday_live != 0.0 && !self.rates.is_empty();
        let vix_live_on = self.params.vix_intraday_live != 0.0;
        if open && (rate_live_on || vix_live_on) {
            let elapsed = (request.time.hour - 9) * 60 + request.time.minute - 30 + 1;
            let on_grid = elapsed <= 1 || elapsed % RATE_LIVE_REFRESH_MINUTES == 0;
            let rates_due = rate_live_on && (on_grid || self.rate_live.is_none());
            let vix_due = vix_live_on && (on_grid || self.vix_live.is_none());
            self.refresh_live_marks(session_variance_remaining(elapsed), rates_due, vix_due);
        }
        if !self.rates.is_empty() {
            let live = if rate_live_on && open { self.rate_live_curve() } else { None };
            self.rates.tick_live(request.time, &self.economy, live, request.order_volumes);
        }
        // The index futures' minute (`futures_index_listed`), after
        // everything a price reads: the basis steps on its own stream and
        // resting orders the moved book crosses fill. Nothing with it off.
        if open {
            self.futures_session_step();
            // The VIX futures' minute (`futures_vix_listed`): their flow's
            // mark steps and resting orders the moved books cross fill.
            self.vix_futures_session_step();
            // The rate futures' minute (`futures_rates_listed`).
            self.rate_futures_session_step();
            // The oil futures' minute (`futures_oil_listed`).
            self.oil_futures_session_step();
            // The options dealer's minute (`options_index_listed`): its
            // pressure decays and waiting orders its quote crosses fill.
            self.options_session_step();
            // The same for the single-name options (`options_names_listed`).
            self.name_options_session_step();
        }
        if population_open {
            if let Some(pop) = self.population.as_mut() {
                pop.after_tick(&self.companies);
            }
        }
        outcome
    }

    // ── The population (`crate::population`) ──────────────────────────────

    /// Attach a population, built for this engine's roster. Called once,
    /// right after construction.
    pub fn set_population(
        &mut self,
        fingerprint: String,
        participants: Vec<crate::population::Participant>,
    ) -> Result<(), String> {
        let run = crate::population::PopulationRun::new(fingerprint, participants, &self.companies)?;
        self.population = Some(Box::new(run));
        Ok(())
    }

    /// The population this engine holds, if any.
    pub fn population(&self) -> Option<&crate::population::PopulationRun> {
        self.population.as_deref()
    }

    /// Install a population state from a snapshot; the engine must hold the
    /// population it was taken from.
    pub fn set_population_state(&mut self, tickers: Vec<String>, flat: &[f64]) -> Result<(), String> {
        match self.population.as_mut() {
            Some(pop) => pop.set_flat(tickers, flat),
            None => Err("this engine holds no population".to_string()),
        }
    }

    /// The population's turn at the start of an open tick: it rolls the
    /// day, books the agents' flow that reaches this tick, and sends its
    /// orders through the agent-facing book.
    fn population_before_tick(&mut self) {
        let Some(mut pop) = self.population.take() else {
            return;
        };
        pop.align(&self.companies);
        if pop.day != Some(self.current_day) {
            pop.roll_day(self.current_day);
        }
        let tick = self.ticks_today();
        for (agent, ticker, bought, sold) in &self.book.flow {
            if crate::population::is_population_label(agent) {
                continue;
            }
            if let Some(i) = self.companies.iter().position(|c| &c.ticker == ticker) {
                let volume = crate::agent_book::daily_volume(&self.companies[i]);
                pop.observe_flow(i, tick, bought - sold, volume);
            }
        }
        pop.prepare(tick, &self.companies);
        let orders = pop.decide(tick, &self.companies, self.market_vol.sigma_daily(), self.economy.vix);
        for (k, i, signed) in orders {
            let label = pop.participants[k].label();
            if let Some(limit) = pop.max_spread(k) {
                if pop.adds(k, i, signed) {
                    // The quoted spread in daily sigmas. With several
                    // detectors, one reading per name per five-tick window
                    // (`PopulationRun::gates`) serves every detector deciding
                    // in it; infinite when the book is not two-sided.
                    let window = (self.current_day as f64) * (crate::population::SESSION_TICKS / crate::population::GATE_WINDOW) as f64
                        + (tick / crate::population::GATE_WINDOW) as f64;
                    let cached = match pop.gates.get(2 * i..2 * i + 2) {
                        Some(&[x, w]) if w == window => Some(x),
                        _ => None,
                    };
                    let spread = match cached {
                        Some(x) => x,
                        None => {
                            let sigma = crate::agent_book::daily_sigma(&self.companies[i], self.market_vol.sigma_daily());
                            let x = match self.agent_book_at(i, Some(&label)) {
                                Some(book) => match (book.best_bid(), book.best_ask()) {
                                    (Some(b), Some(a)) if a > b && sigma > 0.0 => (a - b) / (0.5 * (a + b)) / sigma,
                                    _ => f64::INFINITY,
                                },
                                None => f64::INFINITY,
                            };
                            if let Some(slot) = pop.gates.get_mut(2 * i..2 * i + 2) {
                                slot[0] = x;
                                slot[1] = window;
                            }
                            x
                        }
                    };
                    if !(spread <= limit) {
                        continue;
                    }
                }
            }
            let side = if signed > 0.0 {
                crate::order_book::Side::Buy
            } else {
                crate::order_book::Side::Sell
            };
            let (fills, _) = self.meet_book_as(i, &label, &label, side, signed.abs(), None, true, false, false, true);
            pop.book_order(k);
            for f in &fills {
                pop.book_fill(k, i, side, f.quantity, f.price);
            }
        }
        // Its fills live on its own ledger, not in what `take_fills` returns
        // (`meet_book` does not record them).
        self.population = Some(pop);
    }

    /// Run one simulated minute against an EXTERNAL draw source.
    ///
    /// This is what a replay harness needs: the engine's own generator cannot
    /// reproduce a recorded the reference implementation stream, because `next_normal` routes
    /// through `cos` and diverges on 1.545% of draws. Feeding recorded draws
    /// separates the arithmetic under test from the generator that is known to
    /// differ.
    ///
    /// Because the source is a RECORDING of the pre-split shared stream,
    /// this path keeps the pre-split schedule: settlement draws four or
    /// zero, exactly as the guards decided when the tape was cut
    /// ([`SettleDrawPolicy::FourOrZero`]). The engine's own generated
    /// schedule ([`Engine::tick`]) draws settlement's four unconditionally.
    ///
    /// `draws_consumed` in the returned outcome is 0 here — the caller owns
    /// the source and already knows what it handed over.
    pub fn tick_with(&mut self, request: &TickRequest, rng: &mut impl Rng) -> TickOutcome {
        self.tick_inner(request, rng, SettleDrawPolicy::FourOrZero)
    }

    fn tick_inner(
        &mut self,
        request: &TickRequest,
        rng: &mut impl Rng,
        settle_draws: SettleDrawPolicy,
    ) -> TickOutcome {
        // The day's endogenous news, chained onto whatever the caller
        // supplied. Here rather than in `run_session` because this is the
        // one function every path reaches: `run_session`'s loop, the public
        // `tick`, `tick_with`, and `EngineBatch::tick`, which builds its own
        // `TickRequest` with `news: &[]` (§117).
        //
        // Taken out of `self` and put back, the same move `tick` makes with
        // the market generator and for the same reason: `tick_inner` holds
        // `&mut self`, so it cannot also borrow a field. At zero intensity
        // the vector is empty and the caller's slice is used untouched, so
        // every preset before pt-v11 allocates nothing and is bit-identical.
        let day_news = std::mem::take(&mut self.session_news);
        let chained: Vec<NewsEvent> = if day_news.is_empty() {
            Vec::new()
        } else {
            // The caller's events first, then ours: the news arm is ORDERED
            // and exclusive, so order decides which branch an event takes.
            let mut v = Vec::with_capacity(request.news.len() + day_news.len());
            v.extend_from_slice(request.news);
            // WHEN the day's move lands. At `news_absorption_half_life` 0.0,
            // every preset through pt-v18, each tick carries the event whole and the tick
            // divides it by 390, so the move lands in a straight line over
            // the session: this branch is the code that always stood. Off
            // zero, each event is carried at this minute's share of the
            // absorption profile, `390 * (A(m + 1) - A(m))`, and the same
            // division prices `A(m + 1) - A(m)` of it. The weight is the
            // same for every event, so the sign, the peer transfer and the
            // day's total are unchanged. Caller-supplied news is not
            // touched: it has no release time the profile could start from.
            //
            // Released during the session (`news_session_release_share`):
            // each event carries its own release minute, and the profile
            // starts there. Empty while the dial is 0.0.
            if !self.session_news_release.is_empty() {
                let minutes = (request.time.hour - 9) * 60 + (request.time.minute - 30);
                v.extend(day_news.iter().zip(self.session_news_release.iter()).map(|(e, &r)| {
                    let w = crate::market::factors::news_release_weight(&self.params, minutes, r);
                    NewsEvent {
                        company_id: e.company_id.clone(),
                        sector: e.sector.clone(),
                        price_impact: e.price_impact.map(|x| x * w),
                    }
                }));
            } else if self.params.news_absorption_half_life == 0.0 {
                v.extend(day_news.iter().cloned());
            } else {
                let minutes = (request.time.hour - 9) * 60 + (request.time.minute - 30);
                let w = crate::market::factors::news_absorption_weight(&self.params, minutes);
                v.extend(day_news.iter().map(|e| NewsEvent {
                    company_id: e.company_id.clone(),
                    sector: e.sector.clone(),
                    price_impact: e.price_impact.map(|x| x * w),
                }));
            }
            v
        };
        let request = &TickRequest {
            news: if chained.is_empty() { request.news } else { &chained },
            news_impact_queue: request.news_impact_queue,
            order_volumes: request.order_volumes,
            time: request.time,
            volatility_multiplier: request.volatility_multiplier,
        };
        let outcome = self.tick_body(request, rng, settle_draws);
        self.session_news = day_news;
        outcome
    }

    fn tick_body(
        &mut self,
        request: &TickRequest,
        rng: &mut impl Rng,
        settle_draws: SettleDrawPolicy,
    ) -> TickOutcome {
        let status = get_market_status(request.time);

        // The factor's sigma follows the draw policy because the two mark
        // the same era boundary: the generated schedule belongs to the era
        // whose market factor carries conditional volatility, while
        // `FourOrZero` replays a RECORDED reference stream, and that era
        // drew the factor at constant sigma. Replaying a tape through
        // today's variance state would price recorded draws under dynamics
        // the recording never had.
        let market_sigma_daily = match settle_draws {
            SettleDrawPolicy::FourAlways => self.market_sigma_today(),
            SettleDrawPolicy::FourOrZero => crate::market::tick::MARKET_FACTOR_SIGMA,
        };

        // The per-sector sigmas from the state, or empty (the stateless draw).
        let sector_sigmas = self.sector_sigmas_now();
        // `'static`, so it does not borrow `self` while the tick takes it
        // mutably. `None` on every preset before pt-v19 and outside an
        // episode on pt-v19.
        let epicentre = self.crisis_epicentre_key();
        if !sector_sigmas.is_empty() {
            let t = crate::market::tick::sector_sigma_at(&self.params, &self.economy, self.vix_anchor);
            self.sector_target_day = t * t;
        }
        if !self.opening_z.is_empty() && status != crate::market::MarketStatus::Closed {
            self.apply_opening();
        }
        // The agent-facing book's part of the tick, before the market moves:
        // the maker re-quotes on the inventory agents left it and the
        // consumed depth refills; agents' taker flow reaches the market,
        // once, on the first tick that is not closed; resting orders join
        // the settlement book. None of it runs on an engine no agent has
        // used, which is what keeps every such tick the tick it was.
        let book_on = !self.book.is_pristine();
        let open_now = status == MarketStatus::Open;
        if book_on && open_now {
            self.requote_book();
        }
        let mut merged_flow: Option<Vec<(String, OrderVolume)>> = None;
        let mut fill_impact: Vec<f64> = Vec::new();
        let mut applied: Vec<(String, String, f64, f64)> = Vec::new();
        if book_on && status != MarketStatus::Closed && !self.book.flow.is_empty() {
            applied = std::mem::take(&mut self.book.flow);
            if self.params.fill_impact_coefficient != 0.0 {
                fill_impact = vec![0.0; self.companies.len()];
                for (_, t, b, s) in &applied {
                    if let Some(i) = self.companies.iter().position(|c| &c.ticker == t) {
                        fill_impact[i] += self.fill_impact_of(i, b - s);
                    }
                }
            } else {
                let mut agg: Vec<(String, OrderVolume)> = Vec::new();
                for (_, t, b, s) in &applied {
                    match agg.iter_mut().find(|(x, _)| x == t) {
                        Some((_, v)) => {
                            v.buy += *b;
                            v.sell += *s;
                        }
                        None => agg.push((t.clone(), OrderVolume { buy: *b, sell: *s })),
                    }
                }
                merged_flow = Some(merge_order_volumes(request.order_volumes, &agg));
            }
        }
        // The metaorder memory: decay, feed, and book its displacement into
        // the order-flow slot beside the linear law's. Only with the dial
        // on and the book used, so every other tick is the tick it was.
        let mut memory_plan: Vec<(usize, [f64; crate::agent_book::MEMORY_WIDTH], f64)> = Vec::new();
        // What this tick books into each name's `s` for the memory; read
        // back below if the breaker cut the tick.
        let mut memory_delta: Vec<f64> = Vec::new();
        if book_on && status != MarketStatus::Closed && self.params.impact_memory_coefficient > 0.0 {
            memory_delta = vec![0.0; self.companies.len()];
        }
        if book_on && status != MarketStatus::Closed && self.params.impact_memory_coefficient > 0.0 {
            memory_plan = self.plan_memory(open_now);
            if !memory_plan.is_empty() && fill_impact.is_empty() {
                fill_impact = vec![0.0; self.companies.len()];
            }
            let phi = self.params.s_phi_tick;
            for (i, row, _) in &memory_plan {
                let booked = self.book.memory[*i][crate::agent_book::MEMORY_BOOKED];
                let target = row[crate::agent_book::MEMORY_BOOKED];
                // `s` keeps `phi` of what was booked on an open tick, so
                // the booking tops it back up to the target.
                let delta = if open_now {
                    (target - booked) + (1.0 - phi) * booked
                } else {
                    target - booked
                };
                memory_delta[*i] = delta;
                if delta != 0.0 {
                    fill_impact[*i] += delta;
                }
            }
        }
        let resting: Vec<Vec<crate::microstructure::RestingOrder>> = if book_on
            && open_now
            && self.book.orders.iter().any(|o| o.mode == crate::agent_book::RestMode::Queue)
        {
            let mut per: Vec<Vec<crate::microstructure::RestingOrder>> =
                vec![Vec::new(); self.companies.len()];
            for o in &self.book.orders {
                if o.mode != crate::agent_book::RestMode::Queue {
                    continue;
                }
                if let Some(i) = self.companies.iter().position(|c| c.ticker == o.ticker) {
                    per[i].push(crate::microstructure::RestingOrder {
                        id: o.id.clone(),
                        side: o.side,
                        price: o.limit,
                        quantity: o.remaining,
                        owner_id: o.agent.clone(),
                    });
                }
            }
            per
        } else {
            Vec::new()
        };
        let prints_before: Vec<f64> = if book_on {
            self.companies.iter().map(|c| c.stock.price).collect()
        } else {
            Vec::new()
        };
        let order_volumes: &[(String, OrderVolume)] = match &merged_flow {
            Some(flow) => flow.as_slice(),
            None => request.order_volumes,
        };
        // The reaction session's discovery and the follow-through, this
        // minute's step into fair value, before the tick prices it. Off the
        // calendar nothing runs.
        if open_now && self.carries_earnings() {
            self.walk_earnings_sessions(request.time);
        }
        // Recomputed from the calendar every tick rather than kept, so a
        // fork taken mid-session needs nothing beyond the key. Empty off the
        // calendar.
        let earnings_volume = self.earnings_volume_column();

        // The per-name idiosyncratic variance ratios, one per roster slot,
        // or EMPTY while the state is off, in which case the tick multiplies
        // nothing.
        let idio_vol_ratios: Vec<f64> = if self.idio_state_on() {
            (0..self.companies.len()).map(|i| self.idio_ratio_at(i)).collect()
        } else {
            Vec::new()
        };
        // Read before the tick borrows the companies.
        let market_permanent_ceiling_scale = self.market_vol_cycle_cap_scale();
        let outcome = simulate_market_tick(
            &mut self.companies,
            &TickInputs {
                economy: &self.economy,
                prev_day_down: self.market_vol.prev_day_down(),
                // Read only by `market_beta_down_asym_lag_live`; at its 0.0
                // the tick never looks at either and the tape is unchanged.
                // `day_factor()` is today's accumulator BEFORE the
                // `accumulate` below, which is the point.
                prev_day_factor: self.market_vol.prev_day_factor(),
                // Less tonight's draw under a night split with the live wire
                // on (`night_market_factor`), so the condition keys on the
                // session's own draws; a branch, so at 0.0 this is the read
                // that stood.
                day_factor: if self.night_market_factor == 0.0 {
                    self.market_vol.day_factor()
                } else {
                    self.market_vol.day_factor() - self.night_market_factor
                },
                forced_flow_eff: if self.params.forced_flow_reservoir > 0.0 {
                    crate::mathx::max(
                        0.0,
                        1.0 - self.forced_flow_spent / self.params.forced_flow_reservoir,
                    )
                } else {
                    1.0
                },
                universe_stress: self.universe_stress,
                volume_state: self.volume_state,
                volume_idio: &self.volume_idio,
                jump_move: &self.jump_move,
                earnings_volume: &earnings_volume,
                market_status: status,
                intraday_t: intraday_fraction(request.time),
                volatility_multiplier: request.volatility_multiplier,
                news: request.news,
                news_impact_queue: request.news_impact_queue,
                order_volumes,
                sector_keys: &self.sector_keys,
                sector_sigmas: &sector_sigmas,
                idio_vol_ratios: &idio_vol_ratios,
                market_sigma_daily,
                market_permanent_ceiling_scale,
                // The session's conditional mean from today's VIX pins
                // (`pinned_vix_return`); 0.0 with the dial off.
                pinned_mean: self.pinned_vix_mean,
                vix_anchor: self.vix_anchor,
                settle_draws,
                settle_depth_counterfactual: self.settle_depth_counterfactual,
                resting_orders: &resting,
                fill_impact: &fill_impact,
                nominal_output_base: self.nominal_output_base,
                // Resolved at this session's `open_market` and fixed for
                // the day; `None` on every preset before pt-v19.
                crisis_epicentre: epicentre,
                elapsed_days: self.elapsed_days,
                params: &self.params,
            },
            rng,
        );

        // Accumulate the day's factor innovation for the close's variance
        // update. A closed tick contributes exactly zero (the factor is
        // not drawn), and the replay path accumulates harmlessly into
        // state it never reads.
        // Under a priced VIX move (`pinned_vix_variance_share`) the state
        // reads the draw at the variance it would have had: the priced move
        // stands in for the part of the draw it took.
        self.market_vol.accumulate(self.factor_for_state(outcome.shared_factors.market_factor));
        if self.sector_state_on() {
            for (k, (_, f)) in outcome.shared_factors.sector_factors.iter().enumerate() {
                if let Some(acc) = self.sector_day_factor.get_mut(k) {
                    *acc += *f;
                }
            }
        }

        // Accumulate the APPLIED contributions, which is the same quantity the
        // `truth` table reports per tick -- so the day total of a column here
        // equals that column summed over the day, and the two surfaces cannot
        // disagree about what drove a price.
        //
        // It used to accumulate the RAW factors, and that was wrong in a way
        // that mattered. The three drift factors are divided by 390 before
        // they reach `s` and the noise term is multiplied by the intraday
        // volatility curve, so raw sums overstate news, flow and squeeze by
        // ~390x relative to noise. Anything ranking factors by magnitude --
        // "was this agent right for the right reasons" -- therefore answered
        // `company_news` on days that were overwhelmingly noise. Measured on
        // one session: raw called it news at 6.0e0 against noise at 6.0e-2;
        // applied calls it noise at 7.8e-1 against news at 1.5e-2.
        // Zeroed rather than left stale. A company that did not tick did not
        // move, so every component contributed exactly zero to its `s` -- which
        // is true, and keeps the columns summing to a Δs of zero. Carrying the
        // previous tick's values forward would invent activity.
        for slot in self.tick_components.iter_mut() {
            *slot = [0.0; crate::market::factors::TICK_COMPONENT_COUNT];
        }
        // The print decomposition is zeroed on the same argument and for the
        // same reason: a company that did not tick moved by nothing, so its
        // shock and the depth that absorbed it are both zero. Carrying the
        // previous tick's pair forward would report the same move twice.
        for slot in self.tick_shock.iter_mut() {
            *slot = 0.0;
        }
        for slot in self.tick_absorbed.iter_mut() {
            *slot = 0.0;
        }
        for slot in self.tick_clamp.iter_mut() {
            *slot = 0.0;
        }
        // Every slot's row prints, active or not, so every slot books what
        // was written to its price since the last print, and the pending
        // value is spent. A name that did not tick prints the price it
        // carries, so its row's move is exactly this.
        self.tick_repriced.clear();
        self.tick_repriced.resize(self.companies.len(), 0.0);
        for (slot, pending) in self.repriced_pending.iter_mut().enumerate() {
            if let Some(v) = self.tick_repriced.get_mut(slot) {
                *v = *pending;
            }
            *pending = 0.0;
        }
        // The counterfactual columns are prices, so their zero is the price
        // the company is carrying: unbounded depth does not move a name that
        // did not settle. Written before the copy below so an inactive slot
        // is right, and an active one is overwritten.
        for (slot, value) in self.tick_unbounded_print.iter_mut().enumerate() {
            *value = self
                .companies
                .get(slot)
                .map(|c| c.stock.price)
                .unwrap_or(f64::NAN);
        }
        for slot in self.tick_liquidity_share.iter_mut() {
            *slot = 0.0;
        }
        for (n, slot) in outcome.active_indices.iter().enumerate() {
            if let (Some(acc), Some(computed)) = (
                self.attribution.get_mut(*slot),
                outcome.s_components.get(n),
            ) {
                for (k, value) in computed.iter().enumerate() {
                    acc[crate::market::factors::attribution_slot_for_tick(k)] += value;
                }
            }
            // The GJR's own sum under a night split, the noise slot's
            // increment added in the same order the slot adds it.
            if let (Some(acc), Some(computed)) = (
                self.innovation_day.get_mut(*slot),
                outcome.s_components.get(n),
            ) {
                *acc += computed[random_noise_index()];
            }
            if let (Some(row), Some(computed)) = (
                self.tick_components.get_mut(*slot),
                outcome.s_components.get(n),
            ) {
                *row = *computed;
            }
            // Beside the attribution and on the same guard: a slot the tick
            // did not fill is left alone rather than zeroed.
            if let (Some(acc), Some(computed)) = (
                self.noise_parts.get_mut(*slot),
                outcome.noise_parts.get(n),
            ) {
                for (k, value) in computed.iter().enumerate() {
                    acc[k] += value;
                }
            }
            if let (Some(acc), Some(computed)) = (
                self.noise_own_scale2.get_mut(*slot),
                outcome.noise_own_scale2.get(n),
            ) {
                *acc += *computed;
            }
            // Levels, unlike contributions, PERSIST between ticks: a company
            // that did not trade still has the valuation and anchor it last
            // had, and blanking them would make the columns unusable for
            // exactly the join they exist for.
            if let Some(v) = outcome.fundamental_values.get(n) {
                if let Some(slot_v) = self.tick_fundamental.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.fair_values.get(n) {
                if let Some(slot_v) = self.tick_anchor.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.shock.get(n) {
                if let Some(slot_v) = self.tick_shock.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.absorbed.get(n) {
                if let Some(slot_v) = self.tick_absorbed.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.clamp.get(n) {
                if let Some(slot_v) = self.tick_clamp.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.unbounded_print.get(n) {
                if let Some(slot_v) = self.tick_unbounded_print.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
            if let Some(v) = outcome.liquidity_share.get(n) {
                if let Some(slot_v) = self.tick_liquidity_share.get_mut(*slot) {
                    *slot_v = *v;
                }
            }
        }

        // The memory's new state. A name the tick did not settle carries
        // none of the booking into `s`, so its booked part stays where it
        // was and the next tick it settles books the difference.
        let mut memory_transient: Vec<f64> = Vec::new();
        if !memory_plan.is_empty() {
            memory_transient = vec![0.0; self.companies.len()];
            for (i, row, transient) in &memory_plan {
                let settled = outcome.active_indices.iter().position(|x| x == i);
                let slot = &mut self.book.memory[*i];
                slot[crate::agent_book::MEMORY_FAST] = row[crate::agent_book::MEMORY_FAST];
                slot[crate::agent_book::MEMORY_SLOW] = row[crate::agent_book::MEMORY_SLOW];
                // The flow it paid for is in the memory now; a resting
                // order's taker fill below adds the next tick's.
                slot[crate::agent_book::MEMORY_PAID] = 0.0;
                slot[crate::agent_book::MEMORY_FLOW] = 0.0;
                if let Some(n) = settled {
                    // What landed in `s`: the booking less whatever part of
                    // it the model-price breaker took back this tick (its
                    // slot, 7, is the cut). Booking the target regardless
                    // would record a displacement `s` never received, and
                    // unbooking it as the memory decays would push `s` the
                    // other way by the amount cut.
                    let target = row[crate::agent_book::MEMORY_BOOKED];
                    let delta = memory_delta.get(*i).copied().unwrap_or(0.0);
                    let cut = outcome.s_components.get(n).map(|c| c[7]).unwrap_or(0.0);
                    slot[crate::agent_book::MEMORY_BOOKED] = if delta > 0.0 && cut < 0.0 {
                        target - delta + crate::mathx::max(0.0, delta + cut)
                    } else if delta < 0.0 && cut > 0.0 {
                        target - delta + crate::mathx::min(0.0, delta + cut)
                    } else {
                        target
                    };
                    memory_transient[*i] = *transient;
                }
            }
        }

        if book_on {
            self.settle_book(&outcome, &applied, &prints_before, open_now, &memory_transient);
        }

        TickOutcome {
            market_status: status,
            active_indices: outcome.active_indices,
            fair_values: outcome.fair_values,
            volumes: outcome.volumes,
            draws_consumed: 0,
        }
    }

    /// The maker re-quotes: the inventory agents left it lands, its ladder
    /// is whole again, and consumed latent depth refills by one tick.
    fn requote_book(&mut self) {
        use crate::agent_book::*;
        let refill = refill_factor(&self.params);
        for (i, row) in self.book.taken.iter_mut().enumerate() {
            if row[TAKEN_MAKER_INVENTORY] != 0.0 {
                if let Some(c) = self.companies.get_mut(i) {
                    c.stock.maker_inventory =
                        Some(c.stock.maker_inventory.unwrap_or(0.0) + row[TAKEN_MAKER_INVENTORY]);
                }
                row[TAKEN_MAKER_INVENTORY] = 0.0;
            }
            row[TAKEN_MAKER_BID] = 0.0;
            row[TAKEN_MAKER_ASK] = 0.0;
            for k in [TAKEN_DEPTH_BID, TAKEN_DEPTH_ASK] {
                // Below a millionth of a share it is gone, rather than a
                // geometric tail that never reaches zero.
                row[k] = if row[k] * refill < 1e-6 { 0.0 } else { row[k] * refill };
            }
        }
    }

    /// The metaorder memory's step for this tick, per name it touches:
    /// `(slot, [fast, slow, displacement, 0, 0], transient)`. On an open
    /// tick both memories decay first; then the name's net flow against the
    /// house since the last step ([`crate::agent_book::MEMORY_FLOW`]), over
    /// its daily volume, is added to both; the displacement is `D` of the
    /// combined memory; and `transient` is what that flow alone moved it,
    /// `D(after the flow) - D(decayed)`. A name whose memory is empty is
    /// left out.
    ///
    /// Only flow the house took the other side of feeds the memory: a fill
    /// between two agents moves no liquidity the book has to refill, and
    /// the pair's cash nets to zero, so counting the taker's side would let
    /// two agents walk the tape for nothing (a wash: one rests an ask, the
    /// other lifts it).
    #[allow(clippy::type_complexity)]
    fn plan_memory(&mut self, open_now: bool) -> Vec<(usize, [f64; crate::agent_book::MEMORY_WIDTH], f64)> {
        use crate::agent_book::*;
        let n = self.companies.len();
        if self.book.memory.iter().all(|r| r.iter().all(|x| *x == 0.0)) {
            return Vec::new();
        }
        if self.book.memory.len() < n {
            self.book.memory.resize(n, [0.0; MEMORY_WIDTH]);
        }
        let (delta, _) = depth_shape(&self.params);
        let (fast, slow) = if open_now {
            (memory_decay(self.params.impact_memory_half_life),
             memory_decay(self.params.impact_memory_slow_half_life))
        } else {
            (1.0, 1.0)
        };
        let snap = |x: f64| if x.abs() < 1e-12 { 0.0 } else { x };
        let market_sigma = self.market_vol.sigma_daily();
        let mut plan = Vec::new();
        for i in 0..n {
            let row = self.book.memory[i];
            if row.iter().all(|x| *x == 0.0) {
                continue;
            }
            let v = daily_volume(&self.companies[i]);
            let shares = row[MEMORY_FLOW];
            let net = if v > 0.0 { shares / v } else { 0.0 };
            let sigma = daily_sigma(&self.companies[i], market_sigma);
            let mut next = row;
            next[MEMORY_FAST] = snap(row[MEMORY_FAST] * fast);
            // The slow memory runs only with its half-life set; without it
            // it is carried at zero.
            next[MEMORY_SLOW] = if self.params.impact_memory_slow_half_life > 0.0 {
                snap(row[MEMORY_SLOW] * slow)
            } else {
                0.0
            };
            let at = memory_position(&self.params, &next);
            let decayed = memory_displacement(&self.params, at, sigma);
            next[MEMORY_PAID] = 0.0;
            next[MEMORY_FLOW] = 0.0;
            if net != 0.0 {
                // The flow moves the displacement at most (1 + delta) times
                // what it paid per share, so selling back along the
                // memory's path never recovers more than the flow cost.
                let mut add = net;
                let cap = (1.0 + delta) * row[MEMORY_PAID] / shares.abs();
                let uncapped = memory_displacement(&self.params, at + add, sigma);
                if (uncapped - decayed).abs() > cap {
                    let target = decayed + if net > 0.0 { cap } else { -cap };
                    add = memory_inverse(&self.params, target, sigma) - at;
                }
                next[MEMORY_FAST] += add;
                if self.params.impact_memory_slow_half_life > 0.0 {
                    next[MEMORY_SLOW] += add;
                }
            }
            let d = memory_displacement(&self.params, memory_position(&self.params, &next), sigma);
            next[MEMORY_BOOKED] = d;
            plan.push((i, next, d - decayed));
        }
        plan
    }

    /// `impact_memory_refill`: a resting order filled on the side against
    /// the memory's lean (by the market's flow, or crossed during the
    /// session) is new depth where the lean consumed it, so its `quantity`
    /// shares come off the memory, uncapped and never past zero. On the
    /// side the memory leans it changes nothing. Returns the shares the
    /// memory absorbed. The next tick books the new displacement into `s`
    /// as it books any change of the memory.
    fn refill_memory(&mut self, index: usize, side: crate::order_book::Side, quantity: f64) -> f64 {
        use crate::agent_book::*;
        use crate::order_book::Side;
        let Some(row) = self.book.memory.get(index).copied() else {
            return 0.0;
        };
        let m = memory_position(&self.params, &row);
        let against = match side {
            Side::Sell => m > 0.0,
            Side::Buy => m < 0.0,
        };
        let v = daily_volume(&self.companies[index]);
        if !against || !(v > 0.0) || !(quantity > 0.0) {
            return 0.0;
        }
        let r = crate::mathx::min(quantity / v, m.abs());
        let dr = if m > 0.0 { -r } else { r };
        let slot = &mut self.book.memory[index];
        slot[MEMORY_FAST] += dr;
        if self.params.impact_memory_slow_half_life > 0.0 {
            slot[MEMORY_SLOW] += dr;
        }
        for k in [MEMORY_FAST, MEMORY_SLOW] {
            if slot[k].abs() < 1e-12 {
                slot[k] = 0.0;
            }
        }
        crate::mathx::min(quantity, r * v)
    }

    /// The linear law's change to one name's `s` for a net signed size:
    /// `gamma * sigma * net / V`.
    fn fill_impact_of(&self, index: usize, net: f64) -> f64 {
        let c = &self.companies[index];
        let sigma = crate::agent_book::daily_sigma(c, self.market_vol.sigma_daily());
        self.params.fill_impact_coefficient * sigma * net / crate::agent_book::daily_volume(c)
    }

    /// After the market moved: attribute the flow that was applied, record
    /// the fills of resting orders in the settlement, and fill any waiting
    /// limit the tick's print reached.
    fn settle_book(
        &mut self,
        outcome: &crate::market::TickOutcome,
        applied: &[(String, String, f64, f64)],
        prints_before: &[f64],
        open_now: bool,
        memory_transient: &[f64],
    ) {
        use crate::agent_book::*;
        let (day, tick) = (self.current_day, self.ticks_today());
        let memory_on = self.params.impact_memory_coefficient > 0.0;

        // Permanent impact, per agent and name.
        let linear = self.params.fill_impact_coefficient != 0.0;
        for (agent, t, b, s) in applied {
            if crate::population::is_population_label(agent) {
                continue;
            }
            let Some(i) = self.companies.iter().position(|c| &c.ticker == t) else {
                continue;
            };
            let Some(n) = outcome.active_indices.iter().position(|&x| x == i) else {
                continue;
            };
            let permanent = if linear {
                self.fill_impact_of(i, b - s)
            } else {
                let mut net = 0.0;
                for (_, t2, b2, s2) in applied {
                    if t2 == t {
                        net += b2 - s2;
                    }
                }
                if net == 0.0 {
                    0.0
                } else {
                    outcome.s_components[n][4] * (b - s) / net
                }
            };
            // The memory's part of the tick, shared by signed net shares
            // among the agents whose flow reached this name on it.
            let transient = if memory_on {
                let total = memory_transient.get(i).copied().unwrap_or(0.0);
                let mut net = 0.0;
                for (_, t2, b2, s2) in applied {
                    if t2 == t {
                        net += b2 - s2;
                    }
                }
                Some(if net == 0.0 || total == 0.0 { 0.0 } else { total * (b - s) / net })
            } else {
                None
            };
            self.book.impacts.push(AgentImpact {
                agent: agent.clone(),
                ticker: t.clone(),
                bought: *b,
                sold: *s,
                permanent,
                transient,
                day,
                tick,
            });
        }

        // Resting orders the settlement filled. A resting order the maker's
        // re-quote left crossed took liquidity, so its share of the fills
        // is taker flow like any other agent's, and reaches the market on
        // the next tick. Left out, a standing bid at the ask would take the
        // maker's fresh size every tick and never pay the impact of it.
        let at_limit = self.params.book_cross_at_limit != 0.0;
        let refill = memory_on && self.params.impact_memory_refill != 0.0;
        for (i, f) in &outcome.agent_fills {
            let ticker = self.companies[*i].ticker.clone();
            // `book_cross_at_limit`: a resting order the maker's re-quote
            // left crossed trades at its own limit, and the improvement is
            // the arriving re-quote's. Between two agents the book already
            // fills at the earlier order's price.
            let price = if at_limit && f.taker && is_house(&f.counterparty) {
                self.book.orders.iter().find(|o| o.id == f.order_id).map(|o| o.limit).unwrap_or(f.price)
            } else {
                f.price
            };
            if f.taker {
                // With the metaorder memory on, only what the house took the
                // other side of is flow to the market: a resting order can
                // also cross another agent's (see `meet_book`).
                if !memory_on {
                    self.book.add_flow(&f.agent, &ticker, f.side, f.quantity);
                } else if is_house(&f.counterparty) {
                    self.book.add_flow(&f.agent, &ticker, f.side, f.quantity);
                    // `impact_memory_refill`: a crossed resting order
                    // against the memory's lean refills it first, as a
                    // resting order the flow fills does; only what it
                    // takes beyond zero is flow against the house.
                    let absorbed = if refill { self.refill_memory(*i, f.side, f.quantity) } else { 0.0 };
                    let n = self.companies.len();
                    let reference = prints_before.get(*i).copied().unwrap_or(f64::NAN);
                    self.book.add_house_flow(n, *i, f.side, f.quantity - absorbed, price, reference);
                }
            } else if refill && f.counterparty == FLOW_OWNER {
                self.refill_memory(*i, f.side, f.quantity);
            }
            self.reduce_order(&f.order_id, f.quantity);
            self.push_fill(AgentFill {
                agent: f.agent.clone(),
                order_id: f.order_id.clone(),
                ticker,
                side: f.side,
                quantity: f.quantity,
                price,
                liquidity: if f.taker { Liquidity::Taker } else { Liquidity::Maker },
                counterparty: f.counterparty.clone(),
                reference: prints_before.get(*i).copied().unwrap_or(f64::NAN),
                day,
                tick,
                sequence: 0,
            });
        }

        // Limits waiting for the traded range.
        if open_now {
            let waiting: Vec<AgentOrder> = self
                .book
                .orders
                .iter()
                .filter(|o| o.mode == RestMode::Range)
                .cloned()
                .collect();
            for o in waiting {
                let Some(i) = self.companies.iter().position(|c| c.ticker == o.ticker) else {
                    continue;
                };
                if !outcome.active_indices.contains(&i) {
                    continue;
                }
                let print = self.companies[i].stock.price;
                if !range_reached(o.side, o.limit, print) {
                    continue;
                }
                if refill {
                    self.refill_memory(i, o.side, o.remaining);
                }
                self.reduce_order(&o.id, o.remaining);
                self.push_fill(AgentFill {
                    agent: o.agent.clone(),
                    order_id: o.id.clone(),
                    ticker: o.ticker.clone(),
                    side: o.side,
                    quantity: o.remaining,
                    price: o.limit,
                    liquidity: Liquidity::Range,
                    counterparty: RANGE_OWNER.to_string(),
                    reference: prints_before.get(i).copied().unwrap_or(f64::NAN),
                    day,
                    tick,
                    sequence: 0,
                });
            }
        }
    }

    // ── Agents' orders: the agent-facing book ──────────────────────────────
    //
    // `crate::agent_book` carries the model. What is here is the engine's
    // half: an order meeting the book, what it leaves behind, and what the
    // tick does with it.

    /// The agent-facing book's state, for a snapshot.
    pub fn book_state(&self) -> &crate::agent_book::BookState {
        &self.book
    }

    /// Install a book state, for a restore. `taken` must follow the roster
    /// or be empty.
    pub fn set_book_state(&mut self, state: crate::agent_book::BookState) -> Result<(), String> {
        if !state.taken.is_empty() && state.taken.len() != self.companies.len() {
            return Err(format!(
                "the book's consumed-depth table has {} rows and the roster {} names",
                state.taken.len(),
                self.companies.len()
            ));
        }
        if !state.memory.is_empty() && state.memory.len() != self.companies.len() {
            return Err(format!(
                "the book's metaorder memory has {} rows and the roster {} names",
                state.memory.len(),
                self.companies.len()
            ));
        }
        self.book = state;
        Ok(())
    }

    /// Whether an agent's order must execute in the engine rather than be
    /// priced off a snapshot of the book: when agents consume the book they
    /// share, or when their limit orders rest in it.
    pub fn book_live(&self) -> bool {
        self.params.book_shared != 0.0 || self.params.book_resting != 0.0
    }

    /// The order in which a cohort's orders reach the book on one step:
    /// `labels` sorted, which is the order at `book_arrival_shuffle` 0.0,
    /// or with the switch on sorted by [`crate::rng::arrival_priority`] of
    /// this engine's seed, `day` and `step_of_day`, ties (a 2^-64 event)
    /// broken by the label. Reads no stream and moves nothing.
    pub fn arrival_order(&self, day: u64, step_of_day: u64, labels: &[String]) -> Vec<String> {
        let mut out: Vec<String> = labels.to_vec();
        if self.params.book_arrival_shuffle == 0.0 {
            out.sort();
            return out;
        }
        let seed = self.root_seed;
        let mut keyed: Vec<(u64, String)> = out
            .drain(..)
            .map(|l| (crate::rng::arrival_priority(seed, day, step_of_day, &l), l))
            .collect();
        keyed.sort();
        keyed.into_iter().map(|(_, l)| l).collect()
    }

    /// Whether the book a caller reads is the agent-facing one rather than
    /// the maker's ladder alone.
    fn agent_view(&self) -> bool {
        self.book_live() || self.params.book_depth_coefficient != 0.0 || !self.book.is_pristine()
    }

    /// The ticks already run on the current day: the day mark's count, or
    /// the count a restore carried when no day has opened since it.
    fn ticks_today(&self) -> u32 {
        match self.day_marks.last() {
            Some(m) => m.ticks,
            None => self.carried_ticks.unwrap_or(0),
        }
    }

    /// The agent-facing book for one name, leaving out one agent's orders.
    fn agent_book_at(&self, index: usize, exclude: Option<&str>) -> Option<crate::order_book::OrderBook> {
        let company = self.companies.get(index)?;
        let taken = self
            .book
            .taken
            .get(index)
            .copied()
            .unwrap_or([0.0; crate::agent_book::TAKEN_WIDTH]);
        let memory = self
            .book
            .memory
            .get(index)
            .copied()
            .unwrap_or([0.0; crate::agent_book::MEMORY_WIDTH]);
        Some(crate::agent_book::agent_book(&crate::agent_book::AgentBookInputs {
            company,
            vix: self.economy.vix,
            params: &self.params,
            market_sigma_daily: self.market_vol.sigma_daily(),
            taken,
            memory,
            orders: &self.book.orders,
            exclude_agent: exclude,
        }))
    }

    fn push_fill(&mut self, mut fill: crate::agent_book::AgentFill) -> crate::agent_book::AgentFill {
        fill.sequence = self.book.fill_sequence;
        self.book.fill_sequence += 1;
        self.book.fills.push(fill.clone());
        fill
    }

    /// Reduce a waiting order by a fill, removing it once it is done.
    fn reduce_order(&mut self, order_id: &str, quantity: f64) {
        if let Some(o) = self.book.orders.iter_mut().find(|o| o.id == order_id) {
            o.remaining -= quantity;
        }
        self.book.orders.retain(|o| o.remaining > 1e-9);
    }

    /// An order meets the book an agent trades against, for one name.
    ///
    /// Everything it takes is recorded: the consumed depth and the maker's
    /// inventory when the book is shared, the other agents' resting orders
    /// it hit, a taker fill per level and, when `count_flow`, the taker flow
    /// the next tick applies. Returns the taker fills and the mid the order
    /// met.
    #[allow(clippy::too_many_arguments)]
    fn meet_book(
        &mut self,
        index: usize,
        agent: &str,
        order_id: &str,
        side: crate::order_book::Side,
        quantity: f64,
        limit: Option<f64>,
        count_flow: bool,
        at_limit: bool,
        crossing: bool,
    ) -> (Vec<crate::agent_book::AgentFill>, f64) {
        self.meet_book_as(index, agent, order_id, side, quantity, limit, count_flow, at_limit, crossing, false)
    }

    /// [`Engine::meet_book`], with `quiet` for a population participant's
    /// order: its own fills are returned but not recorded.
    #[allow(clippy::too_many_arguments)]
    fn meet_book_as(
        &mut self,
        index: usize,
        agent: &str,
        order_id: &str,
        side: crate::order_book::Side,
        quantity: f64,
        limit: Option<f64>,
        count_flow: bool,
        at_limit: bool,
        crossing: bool,
        quiet: bool,
    ) -> (Vec<crate::agent_book::AgentFill>, f64) {
        use crate::agent_book::{self as ab, AgentFill, Liquidity};
        use crate::order_book::{Side, SubmitOptions};
        let Some(mut book) = self.agent_book_at(index, Some(agent)) else {
            return (Vec::new(), f64::NAN);
        };
        let ticker = self.companies[index].ticker.clone();
        let reference = book.mid_price().unwrap_or(self.companies[index].stock.price);
        let r = book.submit(
            side,
            quantity,
            agent,
            SubmitOptions { limit_price: limit, post_remainder: false, order_id: None, skip_own: true, house_ids: false },
        );
        let shared = self.params.book_shared != 0.0;
        if shared {
            self.book.fit(self.companies.len());
        }
        let (day, tick) = (self.current_day, self.ticks_today());
        let mut taker = Vec::with_capacity(r.fills.len());
        for f in &r.fills {
            let owner: &str = &f.maker_id;
            // `book_cross_at_limit`: a resting order crossed during the
            // session takes the house's side at its own limit.
            let price = match limit {
                Some(p) if at_limit && ab::is_house(owner) => p,
                _ => f.price,
            };
            if shared {
                if let Some(slot) = ab::taken_slot(side, owner) {
                    self.book.taken[index][slot] += f.quantity;
                }
                if owner == crate::market_maker::MARKET_MAKER_ID {
                    // The maker is opposite the taker; it quotes on this
                    // from its next re-quote.
                    self.book.taken[index][ab::TAKEN_MAKER_INVENTORY] += match side {
                        Side::Buy => -f.quantity,
                        Side::Sell => f.quantity,
                    };
                }
            }
            if !ab::is_house(owner) {
                self.reduce_order(&f.maker_order_id, f.quantity);
                self.push_fill(AgentFill {
                    agent: owner.to_string(),
                    order_id: f.maker_order_id.clone(),
                    ticker: ticker.clone(),
                    side: match side { Side::Buy => Side::Sell, Side::Sell => Side::Buy },
                    quantity: f.quantity,
                    price,
                    liquidity: Liquidity::Maker,
                    counterparty: agent.to_string(),
                    reference,
                    day,
                    tick,
                    sequence: 0,
                });
            }
            // A population participant's own fills go on its ledger, not in
            // the fills `take_fills` returns: it keeps the sequence number
            // the fill would have taken and only the price and size, so the
            // agents' fills are numbered as they were.
            let fill = if quiet {
                let sequence = self.book.fill_sequence;
                self.book.fill_sequence += 1;
                AgentFill {
                    agent: String::new(),
                    order_id: String::new(),
                    ticker: String::new(),
                    side,
                    quantity: f.quantity,
                    price,
                    liquidity: Liquidity::Taker,
                    counterparty: String::new(),
                    reference,
                    day,
                    tick,
                    sequence,
                }
            } else {
                self.push_fill(AgentFill {
                    agent: agent.to_string(),
                    order_id: order_id.to_string(),
                    ticker: ticker.clone(),
                    side,
                    quantity: f.quantity,
                    price,
                    liquidity: Liquidity::Taker,
                    counterparty: owner.to_string(),
                    reference,
                    day,
                    tick,
                    sequence: 0,
                })
            };
            if count_flow {
                // With the metaorder memory on, a fill against another
                // agent's resting order is not flow to the market: it took
                // no liquidity from the house (the maker, the latent depth),
                // the resting order had just added what it took, and the
                // pair's cash nets to zero. Counted, one agent resting an
                // ask and another lifting it would walk the tape, through
                // the memory and the linear law alike, at no cost (a wash).
                // Off, every share taken is flow, as it always was.
                if self.params.impact_memory_coefficient == 0.0 {
                    self.book.add_flow(agent, &ticker, side, f.quantity);
                } else if ab::is_house(owner) {
                    self.book.add_flow(agent, &ticker, side, f.quantity);
                    // `impact_memory_refill`: a resting order crossed during
                    // the session refills the memory first (see
                    // `settle_book`).
                    let absorbed = if crossing && self.params.impact_memory_refill != 0.0 {
                        self.refill_memory(index, side, f.quantity)
                    } else {
                        0.0
                    };
                    let n = self.companies.len();
                    self.book.add_house_flow(n, index, side, f.quantity - absorbed, price, reference);
                }
            }
            taker.push(fill);
        }
        (taker, reference)
    }

    /// Match every resting order on one name that the book now crosses,
    /// against the book at the book's prices, in arrival order.
    ///
    /// A resting order is never posted crossed, and a tick's settlement
    /// matches any its re-quote crosses. What is left is the night: the
    /// open moves every price without a tick, and an order the gap went
    /// through would otherwise sit crossed for the first agent to pick off
    /// at a price the market left behind. Matched here it fills at the
    /// opening ladder's prices, as an opening auction would fill it, and
    /// what it takes is taker flow like any other.
    fn cross_resting(&mut self, index: usize, at_open: bool) {
        use crate::agent_book::RestMode;
        use crate::order_book::Side;
        let Some(ticker) = self.companies.get(index).map(|c| c.ticker.clone()) else {
            return;
        };
        let ids: Vec<String> = self
            .book
            .orders
            .iter()
            .filter(|o| o.mode == RestMode::Queue && o.ticker == ticker)
            .map(|o| o.id.clone())
            .collect();
        for id in ids {
            let Some(o) = self.book.orders.iter().find(|o| o.id == id).cloned() else {
                continue;
            };
            let Some(book) = self.agent_book_at(index, Some(&o.agent)) else {
                continue;
            };
            let crossed = match o.side {
                Side::Buy => book.best_ask().is_some_and(|a| o.limit >= a),
                Side::Sell => book.best_bid().is_some_and(|b| o.limit <= b),
            };
            if !crossed {
                continue;
            }
            // At the open the gap's orders fill at the opening ladder's
            // prices, as an auction fills them; during the session, under
            // `book_cross_at_limit`, at their own limit.
            let at_limit = !at_open && self.params.book_cross_at_limit != 0.0;
            let (fills, _) =
                self.meet_book(index, &o.agent, &o.id, o.side, o.remaining, Some(o.limit), true, at_limit, !at_open);
            let filled: f64 = fills.iter().map(|f| f.quantity).sum();
            self.reduce_order(&o.id, filled);
        }
    }

    /// The book at the open: the maker's pending inventory lands, the
    /// consumed depth is whole again, and crossed resting orders match.
    fn open_book(&mut self) {
        use crate::agent_book::{RestMode, TAKEN_MAKER_INVENTORY};
        for (i, row) in self.book.taken.iter_mut().enumerate() {
            if row[TAKEN_MAKER_INVENTORY] != 0.0 {
                if let Some(c) = self.companies.get_mut(i) {
                    c.stock.maker_inventory =
                        Some(c.stock.maker_inventory.unwrap_or(0.0) + row[TAKEN_MAKER_INVENTORY]);
                }
            }
            *row = [0.0; crate::agent_book::TAKEN_WIDTH];
        }
        let names: Vec<usize> = (0..self.companies.len())
            .filter(|&i| {
                let t = &self.companies[i].ticker;
                self.book.orders.iter().any(|o| o.mode == RestMode::Queue && &o.ticker == t)
            })
            .collect();
        for i in names {
            self.cross_resting(i, true);
        }
    }

    /// Send one agent's order to the book.
    ///
    /// A market order (`limit` None) takes what the book holds up to
    /// `quantity` and the rest is unfilled. A limit order takes what the
    /// book holds at its limit or better, and its remainder waits: resting
    /// in the queue with `book_resting` on, outside the book for the traded
    /// range with it off. Every share taken is taker flow, applied to the
    /// market once, on the next open tick; with `impact_memory_coefficient`
    /// set, every share taken from the house (a fill against another
    /// agent's resting order is not, see `meet_book`).
    ///
    /// Orders are processed in the order they arrive. A caller submitting
    /// for several agents in one step decides that order, and
    /// `PyEngine::submit_many` documents the one it uses.
    #[allow(clippy::too_many_arguments)]
    pub fn submit_order(
        &mut self,
        agent: &str,
        ticker: &str,
        side: crate::order_book::Side,
        quantity: f64,
        limit: Option<f64>,
        order_id: Option<String>,
    ) -> Result<OrderReport, String> {
        use crate::agent_book::{self as ab, AgentOrder, RestMode};
        if agent.is_empty() || ab::is_house(agent) {
            return Err(format!(
                "{agent:?} cannot place orders: agent labels are non-empty and \
                 not one of the book's own owners (mm, depth, flow, range)"
            ));
        }
        if self.population.is_some() && crate::population::is_population_label(agent) {
            return Err(format!(
                "{agent:?} cannot place orders: labels starting {:?} belong to \
                 this engine's population",
                crate::population::LABEL_PREFIX
            ));
        }
        if !(quantity > 0.0) || !quantity.is_finite() {
            return Err(format!("quantity must be finite and greater than zero, got {quantity}"));
        }
        if let Some(p) = limit {
            if !(p > 0.0) || !p.is_finite() {
                return Err(format!("limit_price must be finite and greater than zero, got {p}"));
            }
        }
        // A listed contract trades in its own book (`futures_index_listed`,
        // `futures_vix_listed`), and an option with the dealer
        // (`options_index_listed`, `options_names_listed`).
        if (self.futures_on()
            || self.vix_futures_on()
            || self.rate_futures_on()
            || self.oil_futures_on()
            || self.options_on()
            || self.names_on())
            && !self.companies.iter().any(|c| c.ticker == ticker)
        {
            if let Ok(symbol) = crate::derivatives::ContractSymbol::parse(ticker) {
                if let crate::derivatives::SymbolKind::Option { .. } = symbol.kind {
                    if self.is_listed_name_option(ticker) {
                        return self.submit_name_option_order(agent, ticker, side, quantity, limit, order_id);
                    }
                    return self.submit_option_order(agent, ticker, side, quantity, limit, order_id);
                }
                if self.vix_futures_on() && symbol.root == crate::derivatives::ContractKind::VixFuture.root() {
                    return self.submit_vix_future_order(agent, ticker, side, quantity, limit, order_id);
                }
                if self.rate_futures_on() && self.rate_futures_slot(ticker).is_some() {
                    return self.submit_rate_future_order(agent, ticker, side, quantity, limit, order_id);
                }
                if self.oil_futures_on() && symbol.root == crate::derivatives::ContractKind::OilFuture.root() {
                    return self.submit_oil_future_order(agent, ticker, side, quantity, limit, order_id);
                }
                return self.submit_contract_order(agent, ticker, side, quantity, limit, order_id);
            }
        }
        // The rate indices quote their own ladder (`crate::rates`) and take
        // their flow from `run_session`'s `fills`; the agent-facing book
        // holds equities only.
        if self.rates.instruments.iter().any(|i| i.spec.ticker == ticker) {
            return Err(format!(
                "{ticker} is a simulated rate index, and the agent-facing book holds \
                 equities only: price it with Portfolio.execute, which reads its own \
                 book, and pass its flow in run_session's fills"
            ));
        }
        let index = self
            .companies
            .iter()
            .position(|c| c.ticker == ticker)
            .ok_or_else(|| format!("no instrument with ticker {ticker:?}"))?;
        let company = &self.companies[index];
        if company.is_bankrupt || !company.is_public {
            return Err(format!("{ticker} does not trade: it is bankrupt or no longer public"));
        }
        if self.night_walked() {
            return Err(format!(
                "{ticker} does not trade at night: stocks trade from the next open \
                 (open_market()); the night session is for index futures"
            ));
        }
        let id = match order_id {
            Some(id) => {
                if id.is_empty() {
                    return Err("order_id cannot be empty".to_string());
                }
                id
            }
            None => format!("{agent}-{}", self.book.sequence),
        };
        if self.contract_order_id_taken(&id) {
            return Err(format!("order id {id:?} is already waiting in the book"));
        }
        let sequence = self.book.sequence;
        self.book.sequence += 1;

        if self.book.orders.iter().any(|o| o.mode == RestMode::Queue && o.ticker == ticker) {
            self.cross_resting(index, false);
        }
        let (fills, reference) = self.meet_book(index, agent, &id, side, quantity, limit, true, false, false);
        let mut filled = 0.0;
        let mut notional = 0.0;
        let mut worst: Option<f64> = None;
        for f in &fills {
            filled += f.quantity;
            notional += f.price * f.quantity;
            worst = Some(f.price);
        }
        let remainder = quantity - filled;
        let (resting, mode) = match limit {
            Some(p) if remainder > 1e-9 => {
                let mode = if self.params.book_resting != 0.0 { RestMode::Queue } else { RestMode::Range };
                self.book.orders.push(AgentOrder {
                    id: id.clone(),
                    agent: agent.to_string(),
                    ticker: ticker.to_string(),
                    side,
                    limit: p,
                    quantity,
                    remaining: remainder,
                    sequence,
                    mode,
                });
                (remainder, Some(mode))
            }
            _ => (0.0, None),
        };
        Ok(OrderReport {
            order_id: id,
            agent: agent.to_string(),
            ticker: ticker.to_string(),
            side,
            requested: quantity,
            filled,
            average_price: if filled > 0.0 { Some(notional / filled) } else { None },
            worst_price: worst,
            reference,
            resting,
            unfilled: if mode.is_some() { 0.0 } else { remainder },
            mode,
            fills,
        })
    }

    /// Cancel a waiting order, on a name or on a listed contract. `agent`,
    /// when given, must own it. Returns whether an order was removed.
    pub fn cancel_order(&mut self, order_id: &str, agent: Option<&str>) -> bool {
        let before = self.book.orders.len();
        self.book
            .orders
            .retain(|o| !(o.id == order_id && agent.is_none_or(|a| a == o.agent)));
        if self.book.orders.len() != before {
            return true;
        }
        self.cancel_contract_order(order_id, agent)
            || self.cancel_vix_future_order(order_id, agent)
            || self.cancel_rate_future_order(order_id, agent)
            || self.cancel_oil_future_order(order_id, agent)
            || self.cancel_option_order(order_id, agent)
            || self.cancel_name_option_order(order_id, agent)
    }

    /// Waiting orders, in arrival order, for one agent or all: the names'
    /// first, then the index futures' (`futures_index_listed`), then the VIX
    /// futures' (`futures_vix_listed`).
    pub fn open_orders(&self, agent: Option<&str>) -> Vec<crate::agent_book::AgentOrder> {
        self.book
            .orders
            .iter()
            .chain(self.futures.book.orders.iter())
            .chain(self.vix_futures_orders().iter())
            .chain(self.rate_futures_orders().iter())
            .chain(self.oil_futures_orders().iter())
            .chain(self.option_orders().iter())
            .chain(self.name_option_orders().iter())
            .filter(|o| agent.is_none_or(|a| a == o.agent))
            .cloned()
            .collect()
    }

    /// Collect fills, for one agent or all, in the order they happened: the
    /// names' first, then the index futures' (`futures_index_listed`), then
    /// the VIX futures' (`futures_vix_listed`); each family's `sequence`
    /// counts its own fills alone.
    pub fn take_fills(&mut self, agent: Option<&str>) -> Vec<crate::agent_book::AgentFill> {
        let (mut taken, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut self.book.fills)
            .into_iter()
            .partition(|f| agent.is_none_or(|a| a == f.agent));
        self.book.fills = kept;
        if !self.futures.book.fills.is_empty() {
            let (contracts, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut self.futures.book.fills)
                .into_iter()
                .partition(|f| agent.is_none_or(|a| a == f.agent));
            self.futures.book.fills = kept;
            taken.extend(contracts);
        }
        // The VIX futures' fills (`futures_vix_listed`), on their own counter.
        taken.extend(self.take_vix_futures_fills(agent));
        // The rate futures' fills (`futures_rates_listed`), on their own counter.
        taken.extend(self.take_rate_futures_fills(agent));
        // The oil futures' fills (`futures_oil_listed`), on their own counter.
        taken.extend(self.take_oil_futures_fills(agent));
        // The options' fills (`options_index_listed`), on their own counter.
        taken.extend(self.take_option_fills(agent));
        // The single-name options' (`options_names_listed`), on theirs.
        taken.extend(self.take_name_option_fills(agent));
        taken
    }

    /// Collect permanent-impact rows, for one agent or all.
    pub fn take_impacts(&mut self, agent: Option<&str>) -> Vec<crate::agent_book::AgentImpact> {
        let (taken, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut self.book.impacts)
            .into_iter()
            .partition(|f| agent.is_none_or(|a| a == f.agent));
        self.book.impacts = kept;
        taken
    }

    /// Queue fills that arrived from outside the engine (`run_session`'s
    /// `fills`) as anonymous taker flow, so the linear law prices them too.
    /// Only under `fill_impact_coefficient`; the imbalance law merges them
    /// into the first tick's flow as it always has.
    pub fn queue_external_fills(&mut self, fills: &[(String, OrderVolume)]) {
        for (ticker, v) in fills {
            if v.buy > 0.0 {
                self.book.add_flow("", ticker, crate::order_book::Side::Buy, v.buy);
            }
            if v.sell > 0.0 {
                self.book.add_flow("", ticker, crate::order_book::Side::Sell, v.sell);
            }
        }
    }

    // ── Day boundaries ────────────────────────────────────────────────────

    /// Market-open reset. Zero draws.
    ///
    /// This is what anchors the circuit-breaker band to today's open, which is
    /// what makes it a SESSION band and leaves the overnight gap outside it by
    /// design (D6).
    /// Attribution for the current day, four values per company:
    /// `[company_news, order_flow_impact, short_squeeze_effect, random_noise]`.
    ///
    /// # Four, not the reference's six
    ///
    /// The reference declares six attribution keys, but three of them --
    /// `earningsRevision`, `multipleChange` and `sentiment` -- belong to
    /// factors the live flags discard, and its `shortSqueezeEffect` is folded
    /// into `orderFlowImpact` for display rather than reported separately.
    ///
    /// Reporting six columns here would mean shipping three columns of
    /// structural zeros, which is the "knobs wired to nothing" documentation
    /// lie this port has already had to correct once. So the four live
    /// components are reported, and the squeeze is kept separate because it is
    /// genuinely a distinct mechanism.
    pub fn attribution(&self) -> &[[f64; crate::market::factors::COMPONENT_COUNT]] {
        &self.attribution
    }

    /// This tick's `s` decomposition per company slot, in
    /// [`crate::market::factors::S_COMPONENT_KEYS`] order. Zero for a company
    /// that did not tick.
    pub fn tick_components(&self) -> &[[f64; crate::market::factors::TICK_COMPONENT_COUNT]] {
        &self.tick_components
    }

    /// The valuation each company was last measured at. NaN before its first
    /// tick.
    pub fn tick_fundamental(&self) -> &[f64] {
        &self.tick_fundamental
    }

    /// The book anchor each company was last given: `fundamental * exp(s)`.
    pub fn tick_anchor(&self) -> &[f64] {
        &self.tick_anchor
    }

    /// This tick's shock per company slot: `log(anchor / the last print)`.
    ///
    /// Zero for a company that did not tick.
    pub fn tick_shock(&self) -> &[f64] {
        &self.tick_shock
    }

    /// This tick's absorption per company slot: `log(the print / anchor)`.
    ///
    /// Measured against the printed tape, so a halted name books the
    /// breaker's clamp here. Zero for a company that did not tick.
    pub fn tick_absorbed(&self) -> &[f64] {
        &self.tick_absorbed
    }

    /// How far the print breaker moved each company's print this tick. Zero
    /// where it did not fire, and `tick_absorbed - tick_clamp` is the book's
    /// own share.
    pub fn tick_clamp(&self) -> &[f64] {
        &self.tick_clamp
    }

    /// This tick's repricing per company slot: `log(the price the tick
    /// started from / the last print)`, what the close's re-mark, a pin's
    /// re-mark or the opening print wrote to the price between the two.
    /// Zero where nothing did; NaN where it is not known (after a restore;
    /// see `repriced_pending`).
    pub fn tick_repriced(&self) -> &[f64] {
        &self.tick_repriced
    }

    /// Forget what was written to each price since its last print, as not
    /// known: NaN on a model that can write a price between prints
    /// (`macro_publication_repricing` or `overnight_variance_ratio` set),
    /// zero on one that cannot. For a restore, whose snapshot does not
    /// carry it, so the engine restored into does not keep its own.
    pub fn forget_repriced(&mut self) {
        let unknown = self.params.macro_publication_repricing != 0.0
            || self.params.overnight_variance_ratio != 0.0;
        let value = if unknown { f64::NAN } else { 0.0 };
        self.repriced_pending.clear();
        self.repriced_pending.resize(self.companies.len(), value);
        // The rate indices likewise: only the close's re-mark writes their
        // price between prints.
        let rate_value = if self.params.rate_close_remark != 0.0 { f64::NAN } else { 0.0 };
        self.rates.forget_repriced(rate_value);
    }

    /// What each company would have printed this tick against unbounded
    /// depth. Empty while the counterfactual is off.
    pub fn tick_unbounded_print(&self) -> &[f64] {
        &self.tick_unbounded_print
    }

    /// Liquidity's share of each company's move this tick. Empty while the
    /// counterfactual is off.
    pub fn tick_liquidity_share(&self) -> &[f64] {
        &self.tick_liquidity_share
    }

    /// Settle every open tick a second time against unbounded depth.
    ///
    /// Off by default. Switching it on adds a second settlement per active
    /// company per open tick, served the same four uniforms, on its own book,
    /// with its result reaching no company field. The market is the same
    /// market either way and the digest is the same digest; what changes is
    /// that [`Engine::tick_unbounded_print`] and
    /// [`Engine::tick_liquidity_share`] have something in them.
    ///
    /// It adds a second settlement per active company per open tick, and
    /// that settlement quotes every level rather than the two ordinary flow
    /// reaches. A run takes three to four times as long with it on, best of
    /// seven over 24 names and three days of 390 ticks at seed 42 on pt-v16.
    /// The ratio and not just the times move with load on a shared machine,
    /// because the arm-off run is the smaller number, so it is given as a
    /// range rather than to two decimals. It stays off until a caller asks
    /// for it.
    pub fn set_settle_depth_counterfactual(&mut self, on: bool) {
        self.settle_depth_counterfactual = on;
        let n = self.companies.len();
        self.tick_unbounded_print.clear();
        self.tick_liquidity_share.clear();
        if on {
            self.tick_unbounded_print.resize(n, f64::NAN);
            self.tick_liquidity_share.resize(n, 0.0);
        }
    }

    /// Whether the depth counterfactual is running.
    pub fn settle_depth_counterfactual(&self) -> bool {
        self.settle_depth_counterfactual
    }

    /// Restore the per-day accumulators that a column snapshot does not hold.
    ///
    /// The columns are per-COMPANY state -- price, variance, inventory, the
    /// mispricing carry. These four are per-DAY, live beside them, and were
    /// missing from the snapshot, which made a mid-day fork diverge:
    /// `attribution` is fed to GARCH as the day's innovation at the close, so
    /// a fork that lost it closed on a different variance and priced
    /// differently from the parent it was supposed to be identical to.
    ///
    /// Lengths are checked rather than truncated. A short slice would restore
    /// a market correct for its first companies and stale for the rest.
    pub fn restore_day_state(
        &mut self,
        attribution: &[f64],
        components: &[f64],
        fundamental: &[f64],
        anchor: &[f64],
    ) -> Result<(), String> {
        let n = self.companies.len();
        // Nine-wide attribution rows predate the overnight slot, ten-wide
        // ones the fair-value shift and eleven-wide ones the dividend; each
        // restores with the slots it lacks at zero, which is what such a day
        // held. Likewise eight-wide tick rows.
        let width = crate::market::factors::COMPONENT_COUNT;
        let attribution: Vec<f64> = match (n, attribution.len()) {
            (n, len) if n > 0
                && (len == n * (width - 1) || len == n * (width - 2) || len == n * (width - 3)) => {
                let old = len / n;
                attribution
                    .chunks_exact(old)
                    .flat_map(|c| c.iter().copied().chain(std::iter::repeat_n(0.0, width - old)))
                    .collect()
            }
            _ => attribution.to_vec(),
        };
        let tick_width = crate::market::factors::TICK_COMPONENT_COUNT;
        let components: Vec<f64> = if n > 0 && components.len() == n * (tick_width - 1) {
            components
                .chunks_exact(tick_width - 1)
                .flat_map(|c| c.iter().copied().chain(std::iter::once(0.0)))
                .collect()
        } else {
            components.to_vec()
        };
        for (name, len, want) in [
            ("attribution", attribution.len(), n * crate::market::factors::COMPONENT_COUNT),
            ("tick_components", components.len(), n * tick_width),
            ("tick_fundamental", fundamental.len(), n),
            ("tick_anchor", anchor.len(), n),
        ] {
            if len != want {
                return Err(format!("{name} has {len} values, expected {want}"));
            }
        }
        self.attribution = attribution
            .chunks_exact(crate::market::factors::COMPONENT_COUNT)
            .map(|c| {
                let mut row = [0.0; crate::market::factors::COMPONENT_COUNT];
                row.copy_from_slice(c);
                row
            })
            .collect();
        self.tick_components = components
            .chunks_exact(tick_width)
            .map(|c| {
                let mut row = [0.0; crate::market::factors::TICK_COMPONENT_COUNT];
                row.copy_from_slice(c);
                row
            })
            .collect();
        self.tick_fundamental = fundamental.to_vec();
        self.tick_anchor = anchor.to_vec();
        Ok(())
    }

    /// The day's noise each name's GJR steps on at tonight's close, before
    /// `garch_innovation_commensurate` rescales it: the `random_noise`
    /// attribution column, and under a night split the night's noise ahead
    /// of it (`innovation_day`), which the attribution books in `overnight`.
    pub fn day_noise_column(&self) -> Vec<f64> {
        if self.innovation_day.is_empty() {
            return self.attribution_column(random_noise_index());
        }
        (0..self.companies.len())
            .map(|i| self.innovation_day.get(i).copied().unwrap_or(0.0))
            .collect()
    }

    /// The day's GJR sum under a night split, per company slot; empty while
    /// the split is off. See [`Engine::day_noise_column`].
    pub fn innovation_day(&self) -> &[f64] {
        &self.innovation_day
    }

    /// Put the day's GJR sum back (see [`Engine::innovation_day`]). Empty
    /// clears it, which is the state of a model without a night split.
    pub fn restore_innovation_day(&mut self, values: &[f64]) -> Result<(), String> {
        if !values.is_empty() && values.len() != self.companies.len() {
            return Err(format!(
                "innovation_day has {} values, expected {}",
                values.len(),
                self.companies.len()
            ));
        }
        self.innovation_day = values.to_vec();
        Ok(())
    }

    /// The day's `random_noise` split, per company slot: market, sector,
    /// idiosyncratic. See [`Engine::noise_part_column`].
    pub fn noise_parts(&self) -> &[[f64; 3]] {
        &self.noise_parts
    }

    /// The day's summed square of the scale each name's idiosyncratic part
    /// was drawn at. See [`Engine::noise_parts`].
    pub fn noise_own_scale2(&self) -> &[f64] {
        &self.noise_own_scale2
    }

    /// Put the day's noise split back, flattened three values per company
    /// slot for the parts and one per slot for the scale.
    ///
    /// Restored for the reason [`Engine::restore_day_state`] exists: above
    /// zero on `ModelParams::garch_innovation_commensurate` the close builds
    /// the per-name GJR innovation out of these, so a mid-day fork that lost
    /// them closes on a different innovation.
    pub fn restore_noise_split(&mut self, parts: &[f64], scale2: &[f64]) -> Result<(), String> {
        let n = self.companies.len();
        if parts.len() != n * 3 {
            return Err(format!(
                "noise_parts has {} values, expected {}",
                parts.len(),
                n * 3
            ));
        }
        if scale2.len() != n {
            return Err(format!(
                "noise_own_scale2 has {} values, expected {}",
                scale2.len(),
                n
            ));
        }
        self.noise_parts = parts.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect();
        self.noise_own_scale2 = scale2.to_vec();
        Ok(())
    }

    /// The jump each name booked at the last close, kept across the open for
    /// the session that trades the gap in. Written and read only off the
    /// shipped `ModelParams::volume_move_jump_share` of 1.0.
    pub fn jump_move(&self) -> &[f64] {
        &self.jump_move
    }

    /// Put that jump back, one value per company slot. See
    /// [`Engine::jump_move`].
    pub fn set_jump_move(&mut self, moves: &[f64]) -> Result<(), String> {
        let n = self.companies.len();
        if moves.len() != n {
            return Err(format!(
                "jump_move has {} values, expected {}",
                moves.len(),
                n
            ));
        }
        self.jump_move = moves.to_vec();
        Ok(())
    }

    /// The market factor's variance state, for checkpoints:
    /// `(variance, day_factor)`.
    ///
    /// Engine-level state with no column to live in: a fork that did not
    /// carry it would re-open at the BASELINE factor sigma mid-regime and
    /// close its first day on a truncated innovation — pricing differently
    /// from the parent it forked from, the exact failure class
    /// [`Engine::restore_day_state`] exists for.
    pub fn market_variance_state(&self) -> (f64, f64, f64, f64, f64, f64) {
        self.market_vol.snapshot()
    }

    /// Put the market factor's variance state back. See
    /// [`Engine::market_variance_state`].
    pub fn set_market_variance_state(&mut self, variance: f64, day_factor: f64) {
        self.market_vol = MarketVarianceState::restore(variance, day_factor);
    }

    /// Put the market factor's variance state back including the slow
    /// component. See [`Engine::market_variance_state`].
    pub fn set_market_variance_state_with_components(
        &mut self,
        variance: f64,
        day_factor: f64,
        fast_variance: f64,
        slow_variance: f64,
        prev_day_factor: f64,
        smoothed_vix: f64,
    ) {
        self.market_vol = MarketVarianceState::restore_with_components(
            variance, day_factor, fast_variance, slow_variance,
            prev_day_factor, smoothed_vix);
    }

    /// One attribution column across all companies, by index 0..7.
    pub fn attribution_column(&self, factor: usize) -> Vec<f64> {
        self.attribution
            .iter()
            .map(|a| a.get(factor).copied().unwrap_or(f64::NAN))
            .collect()
    }

    /// One part of the day's `random_noise` across all companies, by index
    /// 0 (market), 1 (sector) or 2 (idiosyncratic). Reporting only; the
    /// close reads the parts through `daily_innovation_column`.
    pub fn noise_part_column(&self, part: usize) -> Vec<f64> {
        self.noise_parts
            .iter()
            .map(|a| a.get(part).copied().unwrap_or(f64::NAN))
            .collect()
    }

    /// The day's GARCH innovation per company, as `close_market` feeds it.
    ///
    /// At `garch_innovation_commensurate` 0.0 this is the `random_noise`
    /// attribution column and nothing else, returned by branch, so every
    /// preset before the dial is bit-identical.
    ///
    /// Above zero it is the name's OWN noise divided by the `kappa` the
    /// tick drew it with, which is what puts the innovation back in the
    /// units the GJR coefficients were fitted in. See
    /// [`crate::params::ModelParams::garch_innovation_commensurate`] for
    /// the defect, the arithmetic and the measurements.
    ///
    /// A name whose ticks never ran has no scale to divide by, so it keeps
    /// the whole column: that is the pre-dial value, and a zero divisor is
    /// not a smaller innovation, it is no statement at all.
    fn daily_innovation_column(&self) -> Vec<f64> {
        let whole = self.day_noise_column();
        let share = self.params.garch_innovation_commensurate;
        if share == 0.0 {
            return whole;
        }
        whole
            .iter()
            .enumerate()
            .map(|(i, &raw)| {
                let own = self.noise_parts.get(i).map(|a| a[2]).unwrap_or(f64::NAN);
                let kappa2 = self.noise_own_scale2.get(i).copied().unwrap_or(0.0);
                if !(kappa2 > 0.0) || !own.is_finite() {
                    return raw;
                }
                let commensurate = own / crate::mathx::sqrt(kappa2);
                if share == 1.0 {
                    commensurate
                } else {
                    (1.0 - share) * raw + share * commensurate
                }
            })
            .collect()
    }

    /// Step the crisis episode and, on the session one starts, draw its
    /// epicentre.
    ///
    /// Called once per session from `open_market`, which is the one point
    /// every spelling of a day passes through exactly once, and BEFORE the
    /// session's ticks, so the epicentre is fixed for the whole day.
    ///
    /// # Draw discipline
    ///
    /// A BRANCH at `crisis_epicentre_extra` 0.0 -- every preset before pt-v19's
    /// fourth composition of 2026-09-22:
    /// nothing runs, no state moves and no draw is taken, here or anywhere.
    /// When the dial is live the one uniform is taken on
    /// `stream::CRISIS_EPICENTRE` and nowhere else, so `MARKET`, `ECONOMY`,
    /// `EXTERNAL` and the six mechanism streams keep the positions they
    /// would have had and every recorded trajectory replays exactly. That
    /// the draw is CONDITIONAL -- once per episode, and not at all under a
    /// pin -- is allowed here for the reason the stream's own docs give:
    /// the only schedule it can move is its own, and nothing else reads it.
    fn update_crisis_episode(&mut self) {
        if self.params.crisis_epicentre_extra == 0.0 {
            return;
        }
        let above = self.economy.vix > self.params.crisis_vix_threshold;
        if !self.crisis_in_episode {
            // "Crossing from below" needs no previous VIX: an episode
            // cannot be running, so the last session either was under the
            // threshold or was before the run began.
            if above {
                self.crisis_in_episode = true;
                self.crisis_sessions_under = 0;
                self.crisis_epicentre = match self.crisis_epicentre_pin {
                    // PINNED: no draw. The stream's position is where the
                    // last episode left it, so a pinned run and an unpinned
                    // one are not the same random world on this stream --
                    // which is correct, since the pin replaces the draw
                    // rather than overriding its result.
                    Some(pin) => pin,
                    None => {
                        self.crisis_epicentre_rng
                            .site(Site::CrisisEpicentreU, 0);
                        let u = self.crisis_epicentre_rng.next_f64();
                        match crate::sectors::draw_crisis_epicentre(u) {
                            Some(i) => i as i32,
                            None => -1,
                        }
                    }
                };
            }
        } else if above {
            self.crisis_sessions_under = 0;
        } else {
            self.crisis_sessions_under += 1;
            // `>=`, so a value at or below zero ends the episode on the
            // first session back under the threshold rather than never.
            if (self.crisis_sessions_under as f64) >= self.params.crisis_epicentre_end_sessions {
                self.crisis_in_episode = false;
                self.crisis_sessions_under = 0;
                self.crisis_epicentre = -1;
            }
        }
    }

    /// The sector key the running episode's epicentre was drawn at, or
    /// `None` -- no episode, the dial off, the episode drew `none`, or the
    /// sector it drew has no name in THIS roster.
    ///
    /// That last one is the tick's question rather than the episode's, which
    /// is why it is answered here and not in `crisis_episode`: the draw
    /// happened and the state records it either way. But the multiples the
    /// tick applies move the roster's crisis variance from one sector to the
    /// others, and with nobody in the epicentre there is nothing to move it
    /// TO -- every name would be scaled down and the roster would simply go
    /// quiet in a crisis, which is the one thing this mechanism promises not
    /// to do. An epicentre nobody is in is not an epicentre.
    ///
    /// `'static`, because it is a key from the sector table rather than a
    /// borrow of this engine, which is what lets the tick's inputs carry it
    /// while the rest of the engine is borrowed mutably.
    pub fn crisis_epicentre_key(&self) -> Option<&'static str> {
        if !self.crisis_in_episode || self.crisis_epicentre < 0 {
            return None;
        }
        let key = crate::sectors::SECTORS
            .get(self.crisis_epicentre as usize)
            .map(|s| s.key)?;
        if self.companies.iter().any(|c| c.sector == key) {
            Some(key)
        } else {
            None
        }
    }

    /// The episode state: whether one is running, how many consecutive
    /// sessions it has spent under the threshold, and its epicentre as the
    /// sector key or the string `"none"`.
    ///
    /// The epicentre reads `None` only when no episode is running, so a
    /// caller can tell "no crisis" from "a crisis with no epicentre", which
    /// the tick deliberately cannot.
    pub fn crisis_episode(&self) -> (bool, i64, Option<&'static str>) {
        let who = if !self.crisis_in_episode {
            None
        } else if self.crisis_epicentre < 0 {
            Some("none")
        } else {
            crate::sectors::SECTORS
                .get(self.crisis_epicentre as usize)
                .map(|s| s.key)
        };
        (self.crisis_in_episode, self.crisis_sessions_under, who)
    }

    /// The VIX level above which this engine's crisis behaviour runs:
    /// [`ModelParams::crisis_vix_threshold`], 30.88325108 on `pt-v13` onward
    /// and 25.5 before. A host with crisis gates of its own should read
    /// this rather than copy the number, which differs between presets.
    ///
    /// It is a coefficient like any other: read it here or as
    /// `engine.params().crisis_vix_threshold`, and set it on the
    /// `ModelParams` before construction with
    /// `ModelParams::with_override("crisis_vix_threshold", x)`. The dollar's
    /// safe-haven bid has its own threshold,
    /// [`ModelParams::usd_crisis_vix_threshold`] (25.5 on every preset),
    /// which does not follow this one.
    pub fn crisis_vix_threshold(&self) -> f64 {
        self.params.crisis_vix_threshold
    }

    /// Whether the VIX is above [`Engine::crisis_vix_threshold`] now: the
    /// test the crisis gates themselves apply (the universe's stress
    /// memory, the tick's crisis spike, the economy's crisis premium and the
    /// start of a crisis episode). A strict `>`, as theirs is.
    ///
    /// This is the instantaneous gate. A crisis EPISODE
    /// ([`Engine::crisis_episode`]) starts on the same test but ends only
    /// after `crisis_epicentre_end_sessions` sessions back under the
    /// threshold, so the two can disagree for up to that many sessions.
    pub fn vix_above_crisis_threshold(&self) -> bool {
        self.economy.vix > self.params.crisis_vix_threshold
    }

    /// The raw episode state, for the snapshot.
    pub fn crisis_episode_raw(&self) -> (bool, i64, i32, Option<i32>) {
        (
            self.crisis_in_episode,
            self.crisis_sessions_under,
            self.crisis_epicentre,
            self.crisis_epicentre_pin,
        )
    }

    /// Restore the episode state, for a snapshot.
    pub fn set_crisis_episode_raw(
        &mut self,
        in_episode: bool,
        sessions_under: i64,
        epicentre: i32,
        pin: Option<i32>,
    ) {
        self.crisis_in_episode = in_episode;
        self.crisis_sessions_under = sessions_under;
        self.crisis_epicentre = epicentre;
        self.crisis_epicentre_pin = pin;
    }

    /// Pin the epicentre, or clear the pin.
    ///
    /// `Some(-1)` pins `none`, a crisis with no epicentre; `None` clears the
    /// pin and hands the next episode back to the draw.
    pub fn set_crisis_epicentre_pin(&mut self, pin: Option<i32>) {
        self.crisis_epicentre_pin = pin;
    }

    /// Whether tonight's close will SET the market factor's variance from
    /// the VIX rather than step toward it. See `vix_sets_variance_pending`.
    pub fn vix_sets_variance_pending(&self) -> bool {
        self.vix_sets_variance_pending
    }

    /// Mark tonight's close as a forced one, or unmark it. The close
    /// consumes the mark. See [`Self::close_market`] and
    /// `MarketVarianceState::close_day_forced`.
    pub fn set_vix_sets_variance_pending(&mut self, on: bool) {
        self.vix_sets_variance_pending = on;
    }

    /// Today's pins that tonight's corporate yield reads, as bits
    /// ([`PIN_VIX`], [`PIN_CORPORATE`]). See `macro_pins_today`.
    pub fn macro_pins_today(&self) -> u16 {
        self.macro_pins_today
    }

    /// Add today's pins (an OR; the close clears them). A spread pin is
    /// always kept; the VIX and corporate bits while `corporate_yield_daily`
    /// is on, the VIX bit also under `pinned_vix_feedback`; every bit under
    /// `macro_pins_hold`; the cycle bit also while `cycle_nowcast_accuracy` is
    /// set; the VIX bit also while `vix_stress_premium` is set. Each is kept
    /// only while something reads it.
    pub fn mark_macro_pins_today(&mut self, bits: u16) {
        let mut keep = PIN_SPREAD;
        if self.params.corporate_yield_daily != 0.0 {
            keep |= PIN_VIX | PIN_CORPORATE;
        }
        if self.params.pinned_vix_feedback != 0.0 && self.carries_vix_feedback() {
            keep |= PIN_VIX;
        }
        if self.params.macro_pins_hold != 0.0 {
            keep |= PIN_ALL;
        }
        if self.params.cycle_nowcast_accuracy != 0.0 {
            keep |= PIN_CYCLE;
        }
        // The published VIX's premium is another reader of a VIX pin:
        // tonight's close keeps its memory at 0.0 (`Engine::published_vix`,
        // `vix_stress_premium`).
        if self.params.vix_stress_premium != 0.0 {
            keep |= PIN_VIX;
        }
        // And the cycle's volatility multiplier, which a pinned VIX or a
        // pinned phase withholds (`market_vol_cycle_pin_neutral`,
        // `market_vol_cycle_pin_phase`). The phase bit's other readers act
        // only under their own dials (`macro_pins_hold`,
        // `cycle_nowcast_accuracy`).
        if self.params.market_vol_cycle_ratio != 0.0 {
            if self.params.market_vol_cycle_pin_neutral != 0.0 {
                keep |= PIN_VIX;
            }
            if self.params.market_vol_cycle_pin_phase != 0.0 {
                keep |= PIN_CYCLE;
            }
        }
        self.macro_pins_today |= bits & keep;
    }

    /// A PINNED VIX IS PRICED WHEN IT IS PUBLISHED (`pinned_vix_feedback`):
    /// the volatility feedback's smoothed exposure moves the dial's share of
    /// the way to the VIX's own excess over the knee as the pin writes it
    /// (all of it at 1.0), before the pin's re-mark, and the close holds it
    /// there (`advance_day_with`). Under `pinned_vix_variance_share` the
    /// discount's change is recorded for the session's market draws
    /// (`market_sigma_today`). Nothing with the dial off, or without the
    /// gain and the half-life.
    ///
    /// Returns whether the credit leg moved the corporate yield, so the
    /// caller re-marks the corporate index.
    pub fn price_pinned_vix(&mut self, vix_before: f64) -> bool {
        if self.params.pinned_vix_feedback == 0.0 || !self.carries_vix_feedback() {
            return false;
        }
        let exposure_before = self.economy.vix_feedback;
        // The knee's excess, or the calm line's above it (`pinned_vix_calm_knee`).
        let excess = crate::market::tick::pinned_vix_excess(&self.params, self.economy.vix);
        // The whole gap at 1.0, bit for bit; a share of it below.
        let w = self.params.pinned_vix_feedback;
        let stepped = if w == 1.0 {
            excess
        } else {
            exposure_before + w * (excess - exposure_before)
        };
        // A HELD PIN PRICES ONCE (`pinned_vix_priced_cap`): the step never
        // lifts the exposure past the share of the target priced on the day
        // it lands, or past the exposure already standing, so a VIX held at
        // one level closes no more of the gap on the sessions after; a step
        // down is left as it is. A branch, so 0.0 is the step as it stood.
        self.economy.vix_feedback = if self.params.pinned_vix_priced_cap != 0.0 {
            crate::mathx::min(stepped, crate::mathx::max(exposure_before, w * excess))
        } else {
            stepped
        };
        // THE PRICED MOVE IS PART OF THE DAY'S VARIANCE
        // (`pinned_vix_variance_share`): the discount's change is recorded
        // for the session's market factor, which draws only what the
        // variance leaves after it.
        if self.params.pinned_vix_variance_share != 0.0 {
            self.pinned_vix_jump += self.params.fair_value_vix_discount
                * (self.economy.vix_feedback - exposure_before);
        }
        // THE CREDIT LEG: the chain's own VIX slope (the meeting formula's
        // 2 bp a point times the phase's multiplier) on the pin's change, at
        // the pin, where the close would have charged it had the VIX moved
        // there. With a VIX pin the close charges the credit spread nothing
        // (`YieldDials::vix_pinned`), so a pinned rise never reached the
        // corporate yield while the fall after the release did. Only under
        // `macro_pins_hold`, which holds the VIX through the close, so the
        // next morning's pin starts from the pinned level and nothing
        // ratchets; only with `corporate_yield_daily`, whose daily term this
        // is; and never over a level or a spread a caller pinned today.
        if self.params.macro_pins_hold == 0.0
            || self.params.corporate_yield_daily == 0.0
            || self.macro_pins_today & (PIN_CORPORATE | PIN_SPREAD) != 0
        {
            return false;
        }
        // The multiplier the market prices (`cycle_nowcast_accuracy`,
        // `corporate_spread_cycle`), as the close's daily term reads it;
        // the true phase's with both at 0.0.
        let m = if self.params.cycle_nowcast_accuracy != 0.0 || self.params.corporate_spread_cycle != 0.0 {
            self.priced_spread_multiplier(self.economy.cycle_phase)
        } else {
            crate::economy::central_bank::cycle_spread_multiplier(self.economy.cycle_phase)
        };
        // The slope less `corporate_spread_vix_cut`, as the close charges
        // it; a branch, so at 0.0 the slope is the literal that stood.
        let slope = if self.params.corporate_spread_vix_cut != 0.0 {
            0.02 * (1.0 - self.params.corporate_spread_vix_cut)
        } else {
            0.02
        };
        let e = &mut self.economy;
        e.corporate_bond_yield = crate::mathx::max(
            e.corporate_bond_yield + slope * m * (e.vix - vix_before),
            e.treasury_yield_10y + crate::economy::central_bank::CORPORATE_SPREAD_FLOOR,
        );
        true
    }

    /// THE PINNED SESSION'S CONDITIONAL MEAN (`pinned_vix_return`): the pin's
    /// change times `-b` is the move the tape's index makes on such a change
    /// the same day. It is recorded for the variance trim
    /// (`pinned_vix_move`), and, less the discount this pin priced, for the
    /// session's market draws (`pinned_vix_mean`). `feedback_before` is the
    /// smoothed exposure before `price_pinned_vix` ran for this pin. Nothing
    /// with the dial off.
    pub fn price_pinned_vix_return(&mut self, vix_before: f64, feedback_before: f64) {
        let b = self.params.pinned_vix_return;
        if b == 0.0 {
            return;
        }
        let moved = -b * (self.economy.vix - vix_before);
        // The discount's own price move for this pin, `-gain * change`, at a
        // beta of one; nothing when `price_pinned_vix` priced nothing.
        let priced = if self.params.pinned_vix_feedback != 0.0 && self.carries_vix_feedback() {
            -self.params.fair_value_vix_discount * (self.economy.vix_feedback - feedback_before)
        } else {
            0.0
        };
        self.pinned_vix_move += moved;
        self.pinned_vix_mean += moved - priced;
    }

    /// The session's market draws' mean for the day (`pinned_vix_return`),
    /// in log points at a beta of one; 0.0 off a pinned session.
    pub fn pinned_vix_mean(&self) -> f64 {
        self.pinned_vix_mean
    }

    /// Today's conditional move (`pinned_vix_return`), for the snapshot.
    pub fn pinned_vix_move(&self) -> f64 {
        self.pinned_vix_move
    }

    /// Put today's conditional move and the draws' mean back, for checkpoints.
    pub fn set_pinned_vix_return_state(&mut self, moved: f64, mean: f64) {
        self.pinned_vix_move = moved;
        self.pinned_vix_mean = mean;
    }

    /// The priced move the variance trim reads: the conditional move under
    /// `pinned_vix_return`, else the discount's.
    fn pinned_vix_trim_move(&self) -> f64 {
        if self.params.pinned_vix_return != 0.0 {
            self.pinned_vix_move
        } else {
            self.pinned_vix_jump
        }
    }

    /// The market factor's sigma for today's draws: the state's, or, when
    /// today's VIX pins moved the volatility-feedback discount
    /// (`pinned_vix_variance_share`), the state's times
    /// `sqrt(max(1 - J^2 / v, 1 - share))` for the priced move `J` and the
    /// state's daily variance `v`. The priced move is the part of the day's
    /// variance the VIX accounts for; the draw carries the rest, and at
    /// least `1 - share` of it. The state's sigma, bit for bit, with no such
    /// move.
    pub fn market_sigma_today(&self) -> f64 {
        let sigma = self.market_sigma_before_day_scale();
        // The day's t scale (`market_day_tail_df`): exactly 1.0 with the
        // dial off, so the product is skipped and the sigma is the bits it was.
        if self.market_day_scale == 1.0 {
            sigma
        } else {
            sigma * crate::mathx::sqrt(self.market_day_scale)
        }
    }

    /// The session's market sigma before the day's t scale: the state's
    /// sigma, less a priced VIX move's share (`pinned_vix_variance_share`).
    fn market_sigma_before_day_scale(&self) -> f64 {
        let sigma = self.market_vol.sigma_daily();
        let j = self.pinned_vix_trim_move();
        if j == 0.0 || self.params.pinned_vix_variance_share == 0.0 {
            return sigma;
        }
        let v = self.market_vol.variance();
        if !(v > 0.0) {
            return sigma;
        }
        let keep = crate::mathx::max(1.0 - j * j / v, 1.0 - self.params.pinned_vix_variance_share);
        sigma * crate::mathx::sqrt(keep)
    }

    /// The day's t scale on the market factor's variance
    /// (`market_day_tail_df`), 1.0 with the dial off or before the open
    /// drew one.
    pub fn market_day_scale(&self) -> f64 {
        self.market_day_scale
    }

    /// Put the day's t scale back, for checkpoints.
    pub fn set_market_day_scale(&mut self, value: f64) {
        self.market_day_scale = value;
    }

    /// Draw the day's t scale (`market_day_tail_df`) on the overnight
    /// stream: `m = (nu - 2) / X`, `X = 2 G`, `G` a gamma of shape `nu / 2`
    /// by Marsaglia and Tsang, so `E[m] = 1` and the day's market factor
    /// is a unit-variance Student t times the state's sigma. The day's
    /// variance `m v` is held under the state's ceiling and never below `v`
    /// by that cap: `m <= max(1, C b / v)`, `C` the ceiling multiple and `b`
    /// the baseline variance.
    fn draw_market_day_scale(&mut self) {
        let nu = self.params.market_day_tail_df;
        if nu == 0.0 {
            return;
        }
        let rng = &mut self.overnight_rng;
        rng.site(Site::MarketDayTailChi2, 0);
        let shape = 0.5 * nu;
        let d = shape - 1.0 / 3.0;
        let c = 1.0 / crate::mathx::sqrt(9.0 * d);
        let mut gamma = shape;
        // Accepted within a few tries at every shape the dial allows
        // (above 0.97 per try at shape 1.5); the bound is a guard, and the
        // mean is kept if it binds.
        for _ in 0..64 {
            let x = rng.next_normal();
            let t = 1.0 + c * x;
            if t <= 0.0 {
                continue;
            }
            let v = t * t * t;
            let u = rng.next_f64();
            if u > 0.0 && crate::mathx::log(u) < 0.5 * x * x + d - d * v + d * crate::mathx::log(v) {
                gamma = d * v;
                break;
            }
        }
        let chi2 = 2.0 * gamma;
        let mut m = if chi2 > 0.0 { (nu - 2.0) / chi2 } else { 1.0 };
        let v = self.market_vol.variance();
        let b = self.params.market_factor_sigma * self.params.market_factor_sigma;
        if v > 0.0 && b > 0.0 {
            let cap = crate::mathx::max(1.0, self.params.market_vol_ceiling_multiple * b / v);
            m = crate::mathx::min(m, cap);
        }
        self.market_day_scale = m;
    }

    /// A market-factor draw as the variance state reads it: the draw
    /// itself, or, on a session whose draws a priced VIX move scaled
    /// (`market_sigma_today`), the draw at the state's full sigma, so the
    /// state evolves as it would have without the scale. The draw itself,
    /// bit for bit, with no such move.
    fn factor_for_state(&self, f: f64) -> f64 {
        // The day's t scale, as much of it as the state reads
        // (`market_day_tail_state_share`): the draw times
        // `m^(-(1 - share) / 2)`. The draw itself with the dial off.
        let f = if self.market_day_scale == 1.0 {
            f
        } else {
            let share = self.params.market_day_tail_state_share;
            if share == 1.0 {
                f
            } else {
                f * crate::mathx::pow(self.market_day_scale, -0.5 * (1.0 - share))
            }
        };
        if self.pinned_vix_trim_move() == 0.0 || self.params.pinned_vix_variance_share == 0.0 {
            return f;
        }
        let full = self.market_vol.sigma_daily();
        let today = self.market_sigma_before_day_scale();
        if today > 0.0 && today != full {
            f * (full / today)
        } else {
            f
        }
    }

    /// Today's priced VIX move (`pinned_vix_variance_share`), for the
    /// snapshot; 0.0 off a pinned session.
    pub fn pinned_vix_jump(&self) -> f64 {
        self.pinned_vix_jump
    }

    /// Put today's priced VIX move back, for checkpoints.
    pub fn set_pinned_vix_jump(&mut self, value: f64) {
        self.pinned_vix_jump = value;
    }

    /// PIN THE CORPORATE SPREAD over the 10-year, in percent: the level is
    /// the 10-year plus it now, and tonight's close holds the spread (the
    /// meeting's re-anchoring included) while the 10-year moves the level.
    /// Held within the meeting formula's own range, 0.8 to 6 per cent. The
    /// later of a level pin and a spread pin on one session is the one the
    /// close holds.
    pub fn pin_corporate_spread(&mut self, spread: f64) {
        let s = crate::mathx::clamp(
            spread,
            crate::economy::central_bank::CORPORATE_SPREAD_FLOOR,
            6.0,
        );
        self.pinned_corporate_spread = s;
        self.economy.corporate_bond_yield = self.economy.treasury_yield_10y + s;
        self.macro_pins_today &= !PIN_CORPORATE;
        self.mark_macro_pins_today(PIN_SPREAD);
    }

    /// A LEVEL PIN on the corporate yield: marked for tonight's close under
    /// the dials that read the mark, and it replaces a spread pinned earlier
    /// today.
    pub fn pin_corporate_level(&mut self) {
        self.macro_pins_today &= !PIN_SPREAD;
        self.mark_macro_pins_today(PIN_CORPORATE);
    }

    /// The spread a caller pinned today, in percent, while `PIN_SPREAD` is
    /// marked; `None` otherwise. The snapshot carries it on that rule.
    pub fn pinned_corporate_spread(&self) -> Option<f64> {
        if self.macro_pins_today & PIN_SPREAD != 0 {
            Some(self.pinned_corporate_spread)
        } else {
            None
        }
    }

    /// A caller pinned the cycle phase: the pin is public news, so the
    /// market's belief goes onto the phase now standing and tonight's close
    /// takes no report (`PIN_CYCLE`), and the anticipation is refreshed on
    /// it. With `cycle_nowcast_accuracy` at 0.0 this is only the refresh the
    /// pin path always made.
    pub fn cycle_phase_pinned(&mut self) {
        if self.params.cycle_nowcast_accuracy != 0.0 {
            self.seed_cycle_nowcast();
            self.mark_macro_pins_today(PIN_CYCLE);
        }
        self.refresh_earnings_anticipation();
    }

    /// A pin that CHANGES the cycle phase starts the new phase's clock, as
    /// the model's own transition does, under `macro_pins_hold`; without
    /// it the new phase inherits the old one's age and the hazard reads
    /// it. Nothing with the dial off or when the phase is unchanged.
    pub fn pin_cycle_phase(&mut self, phase: crate::economy::CyclePhase) {
        if self.params.macro_pins_hold != 0.0 && self.economy.cycle_phase != phase {
            self.economy.months_in_current_phase = 0.0;
        }
        self.economy.cycle_phase = phase;
    }

    /// Restore the marks from a snapshot, as they were, with the spread a
    /// `PIN_SPREAD` mark holds (`pinned_corporate_spread`; 0.0 without it).
    pub fn set_macro_pins_today(&mut self, bits: u16, spread: f64) {
        self.macro_pins_today = 0;
        self.pinned_corporate_spread = if bits & PIN_SPREAD != 0 { spread } else { 0.0 };
        self.mark_macro_pins_today(bits);
    }

    /// The VIX-ratio denominator a FORCED close uses.
    ///
    /// Off `market_vol_vix_excursion` the denominator is the anchor, a
    /// constant of the session, and the free close's own denominator is
    /// returned unchanged: the level the law implies is then an explicit
    /// function of the VIX.
    ///
    /// Under the excursion the denominator is the identity's read-back of
    /// the index variance, which the factor variance being set is part of,
    /// so "the level the law implies at this VIX" is a fixed point: the
    /// denominator `d` at which the variance a forced close sets reads back
    /// as `d`. Setting the target at today's read-back instead would move
    /// tomorrow's read-back by the step just taken, and with the exponent
    /// at 4.9 that feedback overshoots and oscillates. The fixed point is
    /// the level a free run would settle at under the same VIX held for
    /// ever, every other term of the index variance as it stands tonight.
    ///
    /// `h(d) = read_back(forced_level(d)) - d` is strictly decreasing
    /// (the level falls as `d` rises, and the read-back rises with the
    /// level), so the root is unique and is bracketed by the read-backs of
    /// the floor and the ceiling. Found by bisection on those bounds. If the
    /// bracket does not hold -- no shipped preset reaches that -- the free
    /// close's denominator is returned, which is the free law's reading.
    fn forced_vix_denominator(&self, free_denominator: f64, level: f64) -> f64 {
        if self.params.market_vol_vix_excursion == 0.0 {
            return free_denominator;
        }
        let vix = self.economy.vix;
        let premium = self.params.vix_variance_premium;
        let read_back = |v: f64| {
            crate::market::index_var::vix_from_variance(
                premium, self.index_conditional_variance_terms_at(v).total())
        };
        let base = self.params.market_factor_sigma * self.params.market_factor_sigma;
        let mut lo = read_back(base * self.params.market_vol_floor_multiple);
        let mut hi = read_back(base * self.params.market_vol_ceiling_multiple);
        let h = |d: f64| {
            read_back(MarketVarianceState::forced_level(&self.params, d, vix, level)) - d
        };
        if !(lo > 0.0 && hi > lo && h(lo) >= 0.0 && h(hi) <= 0.0) {
            return free_denominator;
        }
        for _ in 0..80 {
            let mid = 0.5 * (lo + hi);
            if h(mid) >= 0.0 {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        0.5 * (lo + hi)
    }

    /// Open a trading day. Call once per day, before its first session.
    ///
    /// Steps the overnight processes (the crisis episode, the overnight gap
    /// in each name's `s`, the day's endogenous news), resets the day's
    /// attribution and anchors the daily open. [`Engine::run_session`] with
    /// `reopen: true` calls this itself.
    ///
    /// It does not advance the day: a host calls
    /// [`Engine::set_current_day`] first, with the trading day counted from
    /// zero.
    pub fn open_market(&mut self) {
        // The live marks belong to a session; this one's are computed below.
        self.rate_live = None;
        self.vix_live = None;
        // THE CRISIS EPISODE, stepped before anything else the session does.
        // At `crisis_epicentre_extra` 0.0 -- every preset before pt-v19's fourth
        // composition of 2026-09-22 -- this
        // returns without touching state or taking a draw.
        self.update_crisis_episode();
        // Attribution is per DAY. Resetting here rather than at close means a
        // caller can still read yesterday's decomposition after the close has
        // run, which is when they would actually want it.
        self.attribution.clear();
        self.attribution.resize(self.companies.len(), [0.0; crate::market::factors::COMPONENT_COUNT]);
        self.noise_parts.clear();
        self.noise_parts.resize(self.companies.len(), [0.0; 3]);
        self.noise_own_scale2.clear();
        self.noise_own_scale2.resize(self.companies.len(), 0.0);
        self.innovation_day.clear();
        if self.night_split_on() {
            self.innovation_day.resize(self.companies.len(), 0.0);
        }
        self.tick_components.clear();
        self.tick_components.resize(self.companies.len(), [0.0; crate::market::factors::TICK_COMPONENT_COUNT]);
        self.tick_fundamental.clear();
        self.tick_fundamental.resize(self.companies.len(), f64::NAN);
        self.tick_anchor.clear();
        self.tick_anchor.resize(self.companies.len(), f64::NAN);
        self.tick_shock.clear();
        self.tick_shock.resize(self.companies.len(), 0.0);
        self.tick_absorbed.clear();
        self.tick_absorbed.resize(self.companies.len(), 0.0);
        self.tick_clamp.clear();
        self.tick_clamp.resize(self.companies.len(), 0.0);
        self.tick_repriced.clear();
        self.tick_repriced.resize(self.companies.len(), 0.0);
        // Not the pending repricing: it carries the close's re-mark across
        // the open to the first print. Only its width follows the roster.
        self.repriced_pending.resize(self.companies.len(), 0.0);
        // Resized only while the arm is on, so a roster that changed between
        // days does not leave a short column behind -- and emptiness keeps
        // meaning "no arm ran" rather than "no companies".
        self.tick_unbounded_print.clear();
        self.tick_liquidity_share.clear();
        if self.settle_depth_counterfactual {
            self.tick_unbounded_print
                .resize(self.companies.len(), f64::NAN);
            self.tick_liquidity_share.resize(self.companies.len(), 0.0);
        }
        // The factor-variance day accumulator is per-day state like the
        // attribution above: an abandoned day must not leak its partial
        // innovation into the next close's update.
        self.market_vol.open_day();

        // The day mark: every stream's position at the open, the active
        // roster and the sector count, so a (day, company) pair addresses
        // its market normals (`market_day_layout`). Ticks are counted as
        // they run.
        let active: Vec<u32> = self
            .companies
            .iter()
            .enumerate()
            .filter(|(_, c)| !c.is_bankrupt && c.is_public)
            .map(|(i, _)| i as u32)
            .collect();
        // The day's own mark counts its ticks from here, so a count a
        // restore carried for the day before is spent.
        self.carried_ticks = None;
        self.day_marks.push(DayMark {
            day: self.current_day,
            positions: self.stream_positions(),
            active,
            sectors: self.sector_keys.len() as u32,
            ticks: 0,
        });

        // Endogenous news for the day (§101, moved here by §117). Two draws
        // per company, ALWAYS, on the NEWS stream: the uniform decides
        // occurrence and the normal decides impact, and at zero intensity
        // `u < 0.0` is false for every u in [0, 1). Same draw discipline as
        // `apply_jumps`, for the same reason. Skipped entirely at zero
        // intensity so every preset before pt-v11 is bit-identical.
        //
        // `open_market` is the one point every spelling of a day passes
        // through exactly once, which is what makes a tick loop and a
        // session agree. It takes no other draws, so moving the generation
        // here left the NEWS stream's own order untouched.
        self.session_news.clear();
        self.session_news_release.clear();
        self.preopen_news.clear();
        if self.params.endogenous_news_intensity != 0.0 {
            let intensity = self.params.endogenous_news_intensity;
            let sigma = self.params.endogenous_news_sigma;
            // Released before the open (`news_preopen_share`): the occurrence
            // uniform below `share * intensity`. Given an event, `u /
            // intensity` is uniform and independent of `z`, so no draw is
            // added. At 0.0 the branch is never taken.
            let preopen = self.params.news_preopen_share;
            for i in 0..self.companies.len() {
                self.news_rng.site(Site::NewsU, i as u32);
                let u = self.news_rng.next_f64();
                self.news_rng.site(Site::NewsZ, i as u32);
                let z = self.news_rng.next_normal();
                if u < intensity {
                    let event = crate::market::NewsEvent {
                        company_id: Some(self.companies[i].id.clone()),
                        sector: Some(self.companies[i].sector.clone()),
                        price_impact: Some(sigma * z),
                    };
                    if preopen != 0.0 && u < preopen * intensity {
                        self.preopen_news.push(event);
                    } else {
                        self.session_news.push(event);
                        // Its release minute (`news_session_release_share`):
                        // what is left of the occurrence uniform past the
                        // pre-open part, over the session's part, is uniform
                        // on [0, 1) and independent of `z`, so no draw is
                        // added. Below the share it is released on the
                        // measured profile; above it, at the first tick.
                        let release = self.params.news_session_release_share;
                        if release != 0.0 {
                            let v = (u - preopen * intensity) / ((1.0 - preopen) * intensity);
                            let minute = if v < release {
                                crate::market::factors::news_release_minute(v / release)
                            } else {
                                0.0
                            };
                            self.session_news_release.push(minute);
                        }
                    }
                }
            }
        }

        // The last close, kept only under a night split: the day's return
        // is then read from it, not from the open the night moved.
        let last_closes: Vec<f64> = if self.night_split_on() {
            self.companies.iter().map(|c| c.stock.price).collect()
        } else {
            Vec::new()
        };

        // The close today's change is measured from, before the night or
        // the open's reset can move a price. Read by nothing that prices.
        self.prior_closes.clone_from(&self.last_closes);

        // The session's expected variance of the aggregate earnings level's
        // move (`fair_value_aggregate_expected`), after the morning's pins and
        // the crisis episode's step and before the night draws: the state the
        // session's draws are made at. Nothing runs with the switch off.
        if self.carries_aggregate_variance() && self.carries_fair_value_offsets() {
            self.aggregate_variance = self.aggregate_expected_variance_now();
        }

        // The night, before the day's marks are set, so the session band
        // anchors on the post-gap open and the gap sits outside it.
        self.apply_overnight();

        // The dividends: declarations, the ex-date drops and the accrual
        // fair value reads today. Nothing, and no state, with
        // `dividend_payout_share` at 0.0.
        self.apply_dividends();

        reset_daily_prices(&mut self.companies);
        // UNDER A NIGHT SPLIT THE DAY RUNS FROM THE LAST CLOSE. The session
        // carries only part of the day's variance, so a day read from the
        // open would hand the VIX (`vix_return_source`), the forced flow
        // (`last_daily_return`), the per-name close and the volume scale a
        // fraction of the move the day made. `previous_close` is what each
        // of them reads, so it keeps the last close; the open, high and low
        // are the session's. No preset splits the day, and without a split
        // this writes nothing.
        for (c, last) in self.companies.iter_mut().zip(last_closes.iter()) {
            c.stock.previous_close = *last;
        }

        // The rate indices' open: a day's carry if a close came before, then
        // the curve as it now stands, after the close's macro step and any
        // pin written since. No draw. Skipped on an engine without them.
        //
        // Under `rate_close_remark` the close already priced its curve, so
        // the open's move is the night's carry (and any pin since), which is
        // booked for the first tape row's `repriced` as the equities' are.
        if !self.rates.is_empty() {
            let before: Vec<f64> = if self.params.rate_close_remark != 0.0 {
                self.rates.instruments.iter().map(|i| i.price).collect()
            } else {
                Vec::new()
            };
            self.rates.open(&self.economy);
            if self.params.rate_close_remark != 0.0 {
                self.rates.book_open_repricing(&before);
            }
        }

        // The index's base, once, on the prices the first session opens at
        // (`index_level_listed`). Nothing with the switch off.
        self.index_open();

        // The book opens whole: the maker quotes afresh on its inventory,
        // the night has refilled any consumed depth, and a resting order
        // the night moved the price through is matched against the opening
        // ladder at the ladder's prices, as an opening auction would fill
        // it, rather than left crossed for the first agent to pick off.
        if !self.book.is_pristine() {
            self.open_book();
        }

        // The live mark's open: both projections on the session as it opens,
        // so a fill before the first tick already reads the night's gap. The
        // live VIX opens the same way (`vix_intraday_live`).
        self.refresh_live_marks(
            1.0,
            self.params.rate_intraday_live != 0.0 && !self.rates.is_empty(),
            self.params.vix_intraday_live != 0.0,
        );

        // The index futures on the opening prints (`futures_index_listed`):
        // an expiring contract settles on them and the next is listed.
        // Nothing with the switch off.
        self.futures_open();
        // The VIX futures at the open (`futures_vix_listed`): an expiring
        // contract settles on the published VIX and the next is listed.
        self.vix_futures_open();
        // The rate futures' session begins (`futures_rates_listed`).
        self.rate_futures_open();
        // The oil futures at the open (`futures_oil_listed`): an expiring
        // contract settles on the oil price and the next is listed.
        self.oil_futures_open();
        // The index options on the opening prints (`options_index_listed`):
        // an expiry due settles on them and the next is listed; the
        // surface's clock starts the session (`surface_ssvi`).
        self.options_open();
        // The single-name options' strikes widen around the opening prints
        // (`options_names_listed`).
        self.name_options_open();
    }

    /// The overnight move, applied once per name at the open, before the
    /// day's marks are set.
    ///
    /// Nothing moved a price between sessions before this: the price after
    /// `open_market` was the price after the previous `close_market` on
    /// every name-night, the jump the close wrote to `s` reached the price
    /// through the next session's ticks, and the day bar's open read the
    /// first of them (issue #179). Real large caps carry a fifth to two
    /// fifths of their daily variance overnight: the forty-name reference
    /// panel reads a median share of 0.33 across nine non-crisis windows,
    /// band 0.23 to 0.43, by `tools/calibration/overnight_band.py`.
    ///
    /// # What the night carries
    ///
    /// Two things, and only the second is a draw. The state that changed
    /// between the close and the open is realised in the OPENING PRINT
    /// rather than dribbled through the first ticks: the price the session
    /// opens at is fair value as of the open, with the macro step the close
    /// advanced inside it, times `exp(s)`, with the close's jump inside
    /// that. That is what puts the jump's discontinuity where a real
    /// earnings gap sits, at the open, and it is what gives the night its
    /// shape: the reference panel's nights put 0.50 to 0.68 of their
    /// variance in the largest five percent, against 0.36 to 0.49 for the
    /// sessions and 0.28 for a Gaussian.
    ///
    /// And the night's own diffusion: one normal for the market, one per
    /// sector and one per name on the [`stream::OVERNIGHT`] stream,
    /// composed exactly as a session's factor structure is, beta on the
    /// market factor at its conditional daily sigma, the sector loading on
    /// the sector factor, the name's own GARCH sigma at its idiosyncratic
    /// scale and size multiplier, and the whole scaled by the square root
    /// of `overnight_variance_ratio`. The composition is the session's
    /// because the reference panel's nights and sessions read the same
    /// cross-sectional correlation, 0.77 to 1.47 times each other across
    /// windows with no direction; what differs is the size, which the dial
    /// carries, and the shape, which the jump landing here supplies. The
    /// session's crash amplifier, downside tilt, forced flow and book are
    /// session mechanisms and do not run overnight.
    ///
    /// The move lands on `s`, the channel news and the jump use, so it
    /// reverts on the mispricing's own half-life, about one percent of the
    /// night over the following session, against the panel's pooled
    /// reversal slope of -0.02 with a window range of -0.10 to -0.01. It
    /// is kept out of the close's momentum roll, as the jump's carried
    /// share is, because a gap is not herding.
    ///
    /// # Draw discipline
    ///
    /// The draws are taken unconditionally on their own stream, one normal
    /// for the market, one per sector, one per company in roster order, so
    /// the schedule cannot depend on the dial and no other stream moves. At
    /// a ratio of exactly 0.0 nothing after the draws runs: no state is
    /// touched, and every preset before this dial is bit-identical, the
    /// known-answer digests included, since this stream is not among the
    /// three `draws_consumed` counts. A name that has not ticked yet has
    /// no `s` and is left alone, so the first session opens at the
    /// universe's own prices as it always did.
    fn apply_overnight(&mut self) {
        let sectors = self.sector_keys.len();
        self.overnight_rng.site(Site::OvernightMarketZ, 0);
        let z_market = self.overnight_rng.next_normal();
        let mut z_sector = Vec::with_capacity(sectors);
        for k in 0..sectors {
            self.overnight_rng.site(Site::OvernightSectorZ, k as u32);
            z_sector.push(self.overnight_rng.next_normal());
        }
        let mut z_idio = Vec::with_capacity(self.companies.len());
        for i in 0..self.companies.len() {
            self.overnight_rng.site(Site::OvernightIdioZ, i as u32);
            z_idio.push(self.overnight_rng.next_normal());
        }
        // The day's market t scale (`market_day_tail_df`), after the three
        // sites above and before the night reads the market sigma. Nothing
        // is drawn with the dial off.
        self.draw_market_day_scale();
        self.overnight_moves.clear();
        self.overnight_moves.resize(self.companies.len(), 0.0);
        self.overnight_fair_value_moves.clear();
        self.overnight_fair_value_moves.resize(self.companies.len(), 0.0);
        self.earnings_moves.clear();
        self.earnings_moves.resize(self.companies.len(), 0.0);
        if self.night_split_on() {
            // The night's student-t scales, on a site of their own after the
            // three above, and only while the dial is set: every other
            // model's overnight schedule is the one it was.
            let df = self.params.overnight_idio_df;
            let t_scales: Vec<f64> = if df == 0.0 {
                Vec::new()
            } else {
                let k = df as usize;
                (0..self.companies.len())
                    .map(|i| {
                        self.overnight_rng.site(Site::OvernightIdioChi2, i as u32);
                        let mut chi2 = 0.0;
                        for _ in 0..k {
                            let n = self.overnight_rng.next_normal();
                            chi2 += n * n;
                        }
                        if chi2 > 0.0 { crate::mathx::sqrt((df - 2.0) / chi2) } else { 1.0 }
                    })
                    .collect()
            };
            self.apply_night_split(z_market, &z_sector, &z_idio, &t_scales);
            return;
        }
        let ratio = self.params.overnight_variance_ratio;
        if ratio == 0.0 {
            return;
        }
        let p = &self.params;
        let scale = crate::mathx::sqrt(ratio);
        let market_sigma_daily = self.market_sigma_today();
        let sector_sigma =
            crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
        let idio_ratios: Vec<f64> = if self.idio_state_on() {
            (0..self.companies.len()).map(|i| self.idio_ratio_at(i)).collect()
        } else {
            Vec::new()
        };
        let night_vix = crate::market::tick::vix_feedback_exposure(&self.params, &self.economy);
        let econ_view = crate::fair_value::EconomyValuationInputs {
            corporate_bond_yield: Some(self.economy.corporate_bond_yield),
            federal_funds_rate: self.economy.federal_funds_rate,
            qe_pe_boost: Some(self.economy.qe_pe_boost),
            qe_assets_ratio: Some(self.economy.qe_assets_ratio),
        };
        let nominal =
            crate::market::tick::nominal_scale(p, &self.economy, self.nominal_output_base);
        for (index, company) in self.companies.iter_mut().enumerate() {
            if company.is_bankrupt || !company.is_public {
                continue;
            }
            let Some(s) = company.stock.mispricing_s else {
                continue;
            };
            let beta = company.stock.beta.unwrap_or(1.0);
            let market = beta * market_sigma_daily * z_market;
            let sector = match self.sector_keys.iter().position(|k| *k == company.sector) {
                Some(k) => {
                    crate::market::factors::sector_loading_for(p, beta) * sector_sigma * z_sector[k]
                }
                None => 0.0,
            };
            // The overnight path's copy of the tick's absolute floor; see
            // `market::factors`, where the same constant is read from the
            // same field. Two spellings of one floor must move together.
            let daily_sigma = crate::mathx::sqrt(
                crate::mathx::max(company.stock.garch_variance, p.idio_sigma_floor));
            let idio = daily_sigma
                * crate::market::factors::idio_scale_for(p, beta)
                * crate::market::factors::cap_size_multiplier_with(p, company.stock.market_cap)
                * z_idio[index];
            // The idiosyncratic variance state reaches the night's own draw
            // as it reaches the session's. A branch, so nothing is
            // multiplied while it is off.
            let idio = if idio_ratios.is_empty() {
                idio
            } else {
                idio * crate::mathx::sqrt(idio_ratios[index])
            };
            let night = scale * (market + sector + idio);
            let after = crate::market::tick::clamp_s(p, s + night);
            let moved = after - s;
            company.stock.mispricing_s = Some(after);
            if let Some(prev) = company.stock.mispricing_s_prev_close {
                company.stock.mispricing_s_prev_close = Some(prev + moved);
            }
            if let Some(acc) = self.attribution.get_mut(index) {
                acc[crate::market::factors::OVERNIGHT_SLOT] += moved;
            }
            self.overnight_moves[index] = moved;
            // The opening print: fair value as of the open times exp(s),
            // the same valuation the first tick would otherwise have
            // settled the print toward over the session.
            //
            // This reads fair value without the buyback term, so under
            // `buyback_payout_share` the open sits below the tick's model
            // price by the compounded buyback yield and the first tick
            // undoes it. The night split (`overnight_market_share`,
            // `overnight_idio_share`) prints through the tick's own fair
            // value and the buyback term's fixed point
            // (`market::tick::opening_print`) instead. This path keeps the
            // arithmetic it had, because a model that sets
            // `overnight_variance_ratio` alone is a model that existed before
            // the split and must price as it did.
            let valuation = if nominal == 1.0 {
                company.valuation()
            } else {
                crate::market::tick::scale_valuation(company.valuation(), nominal)
            };
            let valuation = match company.stock.fair_value_offset {
                Some(v) if v != 0.0 => {
                    crate::market::tick::scale_valuation(valuation, crate::mathx::exp(v))
                }
                _ => valuation,
            };
            let fv = crate::fair_value::compute_fair_value_at(
                &valuation, &econ_view, p.fair_value_book_floor,
                p.qe_pe_gain, p.qe_pe_stock_gain, p.neutral_discount_rate,
                p.rate_pe_sensitivity,
            )
            .fair_value;
            let fv = crate::market::tick::with_vix_discount(p, fv, night_vix, company.stock.beta);
            let price = crate::mathx::min(
                crate::mathx::max(fv * crate::mathx::exp(after), 0.01),
                p.price_hard_cap,
            );
            // For the tape, as at the re-mark: the first print's `repriced`
            // books the opening print's move from the last close.
            if let Some(v) = self.repriced_pending.get_mut(index) {
                *v += crate::mathx::log(price / company.stock.price);
            }
            company.stock.price = price;
            company.stock.market_cap = price * company.stock.shares_outstanding;
        }
    }

    /// Whether the day is split between the night and the session
    /// (`overnight_market_share` or `overnight_idio_share` set). Off on
    /// every preset.
    pub fn night_split_on(&self) -> bool {
        self.params.overnight_market_share != 0.0 || self.params.overnight_idio_share != 0.0
    }

    /// Whether the earnings calendar runs (`earnings_surprise_sigma` set),
    /// which is when the snapshot and the state hash carry its key. Off on
    /// every preset.
    pub fn carries_earnings(&self) -> bool {
        self.params.earnings_surprise_sigma != 0.0
    }

    /// Whether names hold back part of the earnings cycle for their reports
    /// (`earnings_cycle_report_share`, with the calendar and the cycle on),
    /// which is when the snapshot and the state hash carry the amounts.
    pub fn carries_earnings_withheld(&self) -> bool {
        self.carries_earnings()
            && self.params.earnings_cycle_report_share != 0.0
            && self.params.earnings_cycle_depth != 0.0
    }

    /// What each name holds back of the earnings cycle for its next report.
    pub fn earnings_withheld(&self) -> &[f64] {
        &self.earnings_withheld
    }

    /// Put the held-back amounts back, for a restore. A width mismatch is
    /// refused: the amounts are positional against the roster.
    pub fn set_earnings_withheld(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.companies.len() {
            return Err(format!(
                "this snapshot carries {} held-back earnings-cycle amounts and the roster \
                 holds {} companies. They are positional against the roster, so this \
                 restore is refused rather than padded or truncated.",
                values.len(), self.companies.len()));
        }
        self.earnings_withheld = values.to_vec();
        Ok(())
    }

    /// The earnings calendar's key. See [`Engine::carries_earnings`].
    pub fn earnings_key(&self) -> u64 {
        self.earnings_key
    }

    /// Put the earnings calendar's key back, for a restore.
    pub fn set_earnings_key(&mut self, key: u64) {
        self.earnings_key = key;
    }

    /// The earnings surprise each name's opening print realised at the last
    /// open, in roster order; 0.0 where none did.
    pub fn earnings_moves(&self) -> &[f64] {
        &self.earnings_moves
    }

    /// The reports ahead: `(roster index, session)` for every public,
    /// solvent name's reaction sessions in `[from_day, from_day + horizon)`,
    /// ordered by session and then roster. Dates only: the surprise is a
    /// draw nobody reads before the opening print realises it. Empty with
    /// the calendar off.
    pub fn earnings_calendar(&self, from_day: i64, horizon: i64) -> Vec<(usize, i64)> {
        let mut out = Vec::new();
        if !self.carries_earnings() || horizon <= 0 {
            return out;
        }
        let q = crate::earnings::QUARTER_SESSIONS;
        let first_q = std::cmp::max(from_day, 0).div_euclid(q);
        let last_q = std::cmp::max(from_day + horizon - 1, 0).div_euclid(q);
        for (i, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt {
                continue;
            }
            let h = crate::earnings::id_hash(&c.id);
            for quarter in first_q..=last_q {
                let d = crate::earnings::reaction_session(self.earnings_key, h, quarter);
                if d >= from_day && d < from_day + horizon {
                    out.push((i, d));
                }
            }
        }
        out.sort_by_key(|&(i, d)| (d, i));
        out
    }

    /// Whether roster name `index` reacts to a report on `day`, and the
    /// multiple its volume trades at if so (`earnings_volume_multiple`).
    /// EMPTY with the calendar off or no multiple set, which the tick reads
    /// as no multiple.
    fn earnings_volume_column(&self) -> Vec<f64> {
        let m = self.params.earnings_volume_multiple;
        if !self.carries_earnings() || m == 0.0 || m == 1.0 {
            return Vec::new();
        }
        self.companies
            .iter()
            .map(|c| {
                let h = crate::earnings::id_hash(&c.id);
                match crate::earnings::reacts_on(self.earnings_key, h, self.elapsed_days) {
                    Some(_) => m,
                    None => 1.0,
                }
            })
            .collect()
    }

    /// Whether the night's market draw is kept for the session's live
    /// lagged-wire condition: a night split on the market
    /// (`overnight_market_share`) with the lagged wire read live
    /// (`market_beta_down_asym_lag` and `market_beta_down_asym_lag_live`
    /// set). Also when the snapshot and the state hash carry it. Off on
    /// every preset.
    pub fn carries_night_market_factor(&self) -> bool {
        self.params.overnight_market_share != 0.0
            && self.params.market_beta_down_asym_lag != 0.0
            && self.params.market_beta_down_asym_lag_live != 0.0
    }

    /// Tonight's market-factor draw, per unit of beta. See the field.
    pub fn night_market_factor(&self) -> f64 {
        self.night_market_factor
    }

    /// Put tonight's market-factor draw back, for a restore.
    pub fn set_night_market_factor(&mut self, value: f64) {
        self.night_market_factor = value;
    }

    /// Each name's earnings unit: its NON-MARKET daily sigma, the own GJR
    /// sigma at its idiosyncratic scale and size multiplier and its sector
    /// loading on the sector's sigma (the sector state's own where it runs),
    /// in quadrature. What a residual on the market reads as the name's
    /// idiosyncratic sd, the unit the real reaction days are measured in.
    /// The same arithmetic `apply_night_split` prices the surprise in, read
    /// on the state now standing; it takes no draw.
    fn earnings_unit_sigma(&self, company: &TickCompany) -> f64 {
        let p = &self.params;
        let beta = company.stock.beta.unwrap_or(1.0);
        let shared_sigma =
            crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
        let sector_on = p.sector_vol_alpha != 0.0 || p.sector_vol_beta != 0.0;
        let sector_sigma = match self.sector_keys.iter().position(|k| *k == company.sector) {
            Some(k) => {
                let sigma = if sector_on {
                    match self.sector_variance.get(k) {
                        Some(v) if *v > 0.0 => shared_sigma * crate::mathx::sqrt(*v),
                        _ => shared_sigma,
                    }
                } else {
                    shared_sigma
                };
                crate::market::factors::sector_loading_for(p, beta) * sigma
            }
            None => 0.0,
        };
        let daily_sigma = crate::mathx::sqrt(
            crate::mathx::max(company.stock.garch_variance, p.idio_sigma_floor));
        let own = daily_sigma
            * crate::market::factors::idio_scale_for(p, beta)
            * crate::market::factors::cap_size_multiplier_with(p, company.stock.market_cap);
        crate::mathx::sqrt(own * own + sector_sigma * sector_sigma)
    }

    /// THE REACTION SESSION'S OWN DISCOVERY AND THE FOLLOW-THROUGH, walked
    /// into fair value one open minute at a time (`earnings_session_sigma`
    /// on a name's reaction session, `earnings_followthrough_sigma` on the
    /// session after). Each minute `m` moves the name's fair-value level by
    /// `dv = sigma u tick_scale intraday_vol(m / 390) / sqrt(K)` with the
    /// Ito term (`dv - dv^2 / 2`), `u` a standard normal KEYED on (the
    /// calendar's key, the name, the quarter, the part, the minute), `sigma`
    /// the part's size times the name's earnings unit and `K` the session's
    /// intraday variance factor, so a full session carries `sigma^2` and
    /// `exp(v)` is a martingale. The price follows through `fv exp(s)`: the
    /// session trades the information in as it arrives, with no drift and
    /// no first-tick jump.
    ///
    /// Until the review of 50dfeed each part joined fair value in one piece
    /// straight after the opening print, so it printed on the session's
    /// first tick: that tick carried 1.15 times the reaction session's
    /// idiosyncratic variance, and the night's share of the reaction day
    /// (G3) passed only because a 9:30:01 jump counted as session.
    ///
    /// Keyed, so it takes no draw from any stream, a fork taken mid-session
    /// walks the same minutes, and nothing beyond the key and the
    /// fair-value levels (both already carried with the calendar on) is
    /// state. Called only on an open minute with the calendar on and a part
    /// set.
    fn walk_earnings_sessions(&mut self, time: GameTime) {
        let p = &self.params;
        let (ss, sf) = (p.earnings_session_sigma, p.earnings_followthrough_sigma);
        if ss == 0.0 && sf == 0.0 {
            return;
        }
        let minute = (time.hour - 9) * 60 + (time.minute - 30);
        // The session's 390 open minutes, as the tick draws them.
        const MINUTES: i64 = 390;
        if !(0..MINUTES).contains(&minute) {
            return;
        }
        let per_minute = crate::market::hours::intraday_vol(minute as f64 / MINUTES as f64)
            / crate::mathx::sqrt(
                MINUTES as f64 * crate::market::index_var::intraday_variance_factor());
        let key = self.earnings_key;
        let day = self.elapsed_days;
        let mut moves: Vec<(usize, f64)> = Vec::new();
        for (i, c) in self.companies.iter().enumerate() {
            if c.is_bankrupt || !c.is_public || c.stock.mispricing_s.is_none() {
                continue;
            }
            let name = crate::earnings::id_hash(&c.id);
            let mut sd2 = 0.0;
            let mut u = 0.0;
            if ss != 0.0 {
                if let Some(q) = crate::earnings::reacts_on(key, name, day) {
                    u += ss * crate::earnings::session_unit(key, name, q, minute);
                    sd2 += ss * ss;
                }
            }
            if sf != 0.0 {
                if let Some(q) = crate::earnings::reacts_on(key, name, day - 1) {
                    u += sf * crate::earnings::followthrough_unit(key, name, q, minute);
                    sd2 += sf * sf;
                }
            }
            if sd2 == 0.0 {
                continue;
            }
            let dv = u * self.earnings_unit_sigma(c) * per_minute;
            moves.push((i, dv));
        }
        for (i, dv) in moves {
            let c = &mut self.companies[i];
            let v = c.stock.fair_value_offset.unwrap_or(0.0);
            c.stock.fair_value_offset = Some(v + (dv - 0.5 * dv * dv));
        }
    }

    /// The night as a share of the day (`overnight_market_share`,
    /// `overnight_idio_share`), and the earnings reports it realises.
    ///
    /// The market factor's night is `sqrt(wm)` of its conditional daily
    /// sigma and joins the day's factor innovation, so the market GJR steps
    /// on the whole day; each sector's is `sqrt(wi)` of the sector's sigma
    /// (the sector state's own where it runs, and then it joins the
    /// sector's day factor too); each name's own is `sqrt(wi)` of its GARCH
    /// sigma at its idiosyncratic scale and size multiplier, a student t
    /// under `overnight_idio_df`. The session draws the rest
    /// (`market::tick`), so the night and the session sum to the day.
    ///
    /// The market draw takes the session's down tilt and lagged wire, with
    /// their mean given back as the tick gives it, but not the crash
    /// amplifier or the crisis injection. The whole night joins the name's
    /// `random_noise` slot (its GJR's innovation) and the noise split; the
    /// own part moves the fair-value level at `fair_value_news_share` and
    /// the market part at the market's permanent share, only its plain
    /// loading under `fair_value_market_linear`, as the tick does. The move
    /// lands on `s` and is kept out of the momentum roll.
    ///
    /// Then the earnings report, on a name's reaction session: the surprise
    /// joins the fair-value level before the print, so the open prints it.
    /// The reaction session's own part and the session after's are walked
    /// in through those sessions by [`Engine::walk_earnings_sessions`]. The
    /// opening print
    /// is priced through the tick's fair value with the buyback fixed point
    /// (`market::tick::opening_print`).
    fn apply_night_split(
        &mut self,
        z_market: f64,
        z_sector: &[f64],
        z_idio: &[f64],
        t_scales: &[f64],
    ) {
        let p = &self.params;
        let sm = crate::mathx::sqrt(p.overnight_market_share);
        let si = crate::mathx::sqrt(p.overnight_idio_share);
        let market_sigma_daily = self.market_sigma_today();
        let prev_down = self.market_vol.prev_day_down();
        // The night's market factor, per unit of beta, before the tilt.
        let f = sm * market_sigma_daily * z_market;
        // What the variance state reads: `f` itself unless a priced VIX
        // move scaled the draw (`factor_for_state`).
        let f_state = self.factor_for_state(f);
        self.market_vol.accumulate(f_state);
        // Kept for the session's live lagged-wire condition, which reads the
        // day factor less it (see the field). Written only where that wire
        // reads it, so the state carried is the state read.
        if self.carries_night_market_factor() {
            self.night_market_factor = f_state;
        }
        // The sector draws, at the sector state's sigma where it runs.
        let shared_sigma =
            crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
        let sector_on = p.sector_vol_alpha != 0.0 || p.sector_vol_beta != 0.0;
        let sector_draws: Vec<(f64, f64)> = z_sector
            .iter()
            .enumerate()
            .map(|(k, z)| {
                let sigma = if sector_on {
                    match self.sector_variance.get(k) {
                        Some(v) if *v > 0.0 => shared_sigma * crate::mathx::sqrt(*v),
                        _ => shared_sigma,
                    }
                } else {
                    shared_sigma
                };
                (sigma, si * sigma * z)
            })
            .collect();
        let (sector_sigmas, sector_draws): (Vec<f64>, Vec<f64>) = sector_draws.into_iter().unzip();
        if sector_on {
            for (k, g) in sector_draws.iter().enumerate() {
                if let Some(acc) = self.sector_day_factor.get_mut(k) {
                    *acc += *g;
                }
            }
        }
        let tilt = if p.market_beta_down_asym != 0.0 && f < 0.0 {
            1.0 + p.market_beta_down_asym
        } else {
            1.0
        };
        let lag = if p.market_beta_down_asym_lag != 0.0 && prev_down {
            1.0 + p.market_beta_down_asym_lag
        } else {
            1.0
        };
        // The tilt's mean given back, as `market::factors` gives it back at
        // the draw's own sigma, and the WHOLE of the mean the lagged wire's
        // multiple adds to it, whatever `market_beta_down_asym_lag_recentre`
        // says. The session may carry an un-recentred wire (pt-v20 does:
        // lag 0.46, lag recentre 0.0), a drift of about -10 bp a day keyed
        // on yesterday's sign; applied to the night on the same condition,
        // the night and the session drifted together and the equal-weight
        // session return followed the market's gap (slope +0.099 against a
        // real +0.022, the forty 2015-2025). The night keeps the wire's
        // variance, which is what carries the lagged correlation asymmetry
        // into the day, and injects no mean from it.
        let recentre_unit = if p.market_beta_down_asym == 0.0 {
            0.0
        } else {
            let unit = p.market_beta_down_asym * (sm * market_sigma_daily)
                / crate::mathx::sqrt(2.0 * std::f64::consts::PI);
            p.market_beta_down_asym_recentre * unit + (lag - 1.0) * unit
        };
        let permanent = p.fair_value_news_share != 0.0 || p.fair_value_market_share != 0.0;
        let psi = p.fair_value_news_share;
        // The market's permanent share at the ceiling the cycle scales
        // (`market_vol_cycle_cap_relative`), as the session's ticks read it;
        // exactly 1.0 with the cycle off.
        let psim = crate::market::tick::market_permanent_share(
            p, market_sigma_daily, self.market_vol_cycle_cap_scale());
        let earnings = p.earnings_surprise_sigma != 0.0;
        let withhold = earnings
            && p.earnings_cycle_report_share != 0.0
            && p.earnings_cycle_depth != 0.0;
        if withhold && self.earnings_withheld.len() != self.companies.len() {
            self.earnings_withheld.resize(self.companies.len(), 0.0);
        }
        let key = self.earnings_key;
        let day = self.elapsed_days;
        // The idiosyncratic variance state (`idio_vol_alpha`) reaches the
        // night's own draw as it reaches the session's: through the own unit,
        // so `noise_own_scale2` carries it and the close's `u^2` reads the
        // whole day's own noise against the whole day's expected variance.
        // EMPTY while the state is off, a branch.
        let idio_ratios: Vec<f64> = if self.idio_state_on() {
            (0..self.companies.len()).map(|i| self.idio_ratio_at(i)).collect()
        } else {
            Vec::new()
        };
        // The news released before the open (`news_preopen_share`), composed
        // for each name as the session composes news, at the open's crisis
        // spike. EMPTY with the dial at 0.0, and then nothing below reads it.
        let preopen_news = std::mem::take(&mut self.preopen_news);
        let preopen_spike = if preopen_news.is_empty() {
            0.0
        } else {
            crate::market::tick::crisis_spike_for(p, self.economy.vix, self.universe_stress)
        };
        for (index, company) in self.companies.iter_mut().enumerate() {
            if company.is_bankrupt || !company.is_public {
                continue;
            }
            let Some(s) = company.stock.mispricing_s else {
                continue;
            };
            let beta = company.stock.beta.unwrap_or(1.0);
            let market = beta * f * tilt * lag + beta * recentre_unit;
            let (sector, sector_sigma) =
                match self.sector_keys.iter().position(|k| *k == company.sector) {
                    Some(k) => {
                        let loading = crate::market::factors::sector_loading_for(p, beta);
                        (loading * sector_draws[k], loading * sector_sigmas[k])
                    }
                    None => (0.0, 0.0),
                };
            let daily_sigma = crate::mathx::sqrt(
                crate::mathx::max(company.stock.garch_variance, p.idio_sigma_floor));
            let own_unit = crate::market::factors::idio_scale_for(p, beta)
                * crate::market::factors::cap_size_multiplier_with(p, company.stock.market_cap);
            let own_unit = match idio_ratios.get(index) {
                Some(&r) => own_unit * crate::mathx::sqrt(r),
                None => own_unit,
            };
            let t = t_scales.get(index).copied().unwrap_or(1.0);
            let idio = si * daily_sigma * own_unit * z_idio[index] * t;
            let own = sector + idio;
            let night = market + own;
            // The pre-open news: an endogenous event names a company, so it
            // carries no market-wide part, and all of it is the name's own.
            let news = if preopen_news.is_empty() {
                0.0
            } else {
                crate::market::factors::news_terms(
                    &company.id, &company.sector, &preopen_news, preopen_spike, p).0
            };
            let after = if news == 0.0 {
                crate::market::tick::clamp_s(p, s + night)
            } else {
                crate::market::tick::clamp_s(p, s + night + news)
            };
            let moved = after - s;
            if let Some(prev) = company.stock.mispricing_s_prev_close {
                company.stock.mispricing_s_prev_close = Some(prev + moved);
            }
            // The night counts once in the attribution, in `overnight`, so
            // the day's slots sum to its change in `s` and match the tape.
            // The GJR still steps on it: it starts the day's innovation.
            if let Some(acc) = self.attribution.get_mut(index) {
                acc[crate::market::factors::OVERNIGHT_SLOT] += moved;
            }
            if let Some(acc) = self.innovation_day.get_mut(index) {
                *acc += night;
            }
            if let Some(parts) = self.noise_parts.get_mut(index) {
                parts[0] += market;
                parts[1] += sector;
                parts[2] += idio;
            }
            if let Some(k2) = self.noise_own_scale2.get_mut(index) {
                *k2 += (si * own_unit) * (si * own_unit);
            }
            self.overnight_moves[index] = moved;
            let mut s_now = after;
            // THE PERMANENT SHARE, as the tick's: the own part at
            // `fair_value_news_share`, the market's plain loading (or the
            // whole market part off `fair_value_market_linear`) at the
            // market's permanent share.
            if permanent {
                let market_perm = if p.fair_value_market_linear == 0.0 { market } else { beta * f };
                let dv = if p.fair_value_market_share == 0.0 {
                    psi * own
                } else {
                    psi * own + psim * market_perm
                };
                // The pre-open news at the session news's own share.
                let dv = if news == 0.0 { dv } else { dv + psi * news };
                if dv != 0.0 {
                    let s_new = crate::market::tick::clamp_s(p, after - dv);
                    if let Some(prev) = company.stock.mispricing_s_prev_close {
                        company.stock.mispricing_s_prev_close = Some(prev - dv);
                    }
                    if let Some(acc) = self.attribution.get_mut(index) {
                        acc[crate::market::factors::FAIR_VALUE_SLOT] += s_new - after;
                    }
                    // For the tape, beside `overnight_moves`: the first
                    // row's `fair_value_shift` carries it, or the columns
                    // miss that row's change in `s` by the night's
                    // permanent part.
                    if let Some(slot) = self.overnight_fair_value_moves.get_mut(index) {
                        *slot = s_new - after;
                    }
                    let v = company.stock.fair_value_offset.unwrap_or(0.0);
                    company.stock.fair_value_offset = Some(v + (dv - 0.5 * dv * dv));
                    s_now = s_new;
                }
            }
            company.stock.mispricing_s = Some(s_now);
            // THE REPORT. The surprise joins the fair-value level before
            // the print, so the open prints it. Its unit is the name's
            // NON-MARKET daily sigma, its own and its sector's draws
            // together: what a residual on the market reads as the name's
            // idiosyncratic sd, the unit the real reaction days are
            // measured in (3.47 sd, the forty names 2015-2025).
            let own_sigma = crate::mathx::sqrt(
                (daily_sigma * own_unit) * (daily_sigma * own_unit) + sector_sigma * sector_sigma);
            let name = if earnings { crate::earnings::id_hash(&company.id) } else { 0 };
            let quarter = if earnings { crate::earnings::reacts_on(key, name, day) } else { None };
            if quarter.is_some() && withhold {
                // The cycle this name has held back since its last report.
                let w = std::mem::take(&mut self.earnings_withheld[index]);
                if w != 0.0 {
                    let v = company.stock.fair_value_offset.unwrap_or(0.0);
                    company.stock.fair_value_offset = Some(v + w);
                }
            }
            if let Some(q) = quarter {
                let x = crate::earnings::mean_one(
                    p.earnings_surprise_sigma * own_sigma,
                    crate::earnings::surprise_unit(key, name, q, p.earnings_surprise_df));
                let v = company.stock.fair_value_offset.unwrap_or(0.0);
                company.stock.fair_value_offset = Some(v + x);
                self.earnings_moves[index] = x;
            }
            let last = company.stock.price;
            let price = crate::market::tick::opening_print(
                p, &self.economy, self.nominal_output_base, day, company, s_now);
            if let Some(v) = self.repriced_pending.get_mut(index) {
                *v += crate::mathx::log(price / last);
            }
            company.stock.price = price;
            company.stock.market_cap = price * company.stock.shares_outstanding;
            // The reaction session's own discovery and the follow-through on
            // the session after are NOT set here: they are walked into fair
            // value tick by tick through those sessions
            // (`Engine::walk_earnings_sessions`), so the session trades them
            // in rather than printing them on its first tick.
        }
    }

    /// The move each name's `s` took at the last open under the overnight
    /// process, in roster order; 0.0 where nothing moved.
    pub fn overnight_moves(&self) -> &[f64] {
        &self.overnight_moves
    }

    /// What each name's `s` gave up to its fair value at the last open
    /// under a night split with a permanent share, in roster order; 0.0
    /// where nothing moved. The tape's `fair_value_shift` on the day's first
    /// row.
    pub fn overnight_fair_value_moves(&self) -> &[f64] {
        &self.overnight_fair_value_moves
    }

    /// The move each name's `s` took at the last open's ex-date drop, in
    /// roster order; 0.0 where nothing went ex.
    pub fn dividend_moves(&self) -> &[f64] {
        &self.dividend_moves
    }

    /// True when the model pays dividends (`dividend_payout_share` set):
    /// the snapshot and the state hash carry the per-name states only then.
    pub fn carries_dividends(&self) -> bool {
        self.params.dividend_payout_share != 0.0
    }

    /// Each name's dividend state as `STATE_WIDTH` f64s, roster order, NaN
    /// for a name without one. For the snapshot.
    pub fn dividend_states(&self) -> Vec<f64> {
        let w = crate::market::dividends::STATE_WIDTH;
        let mut out = Vec::with_capacity(self.companies.len() * w);
        for c in &self.companies {
            match c.stock.dividend {
                Some(d) => out.extend_from_slice(&d.to_array()),
                None => out.extend(std::iter::repeat_n(f64::NAN, w)),
            }
        }
        out
    }

    /// Write the dividend states back from [`Engine::dividend_states`].
    pub fn set_dividend_states(&mut self, values: &[f64]) -> Result<(), String> {
        let w = crate::market::dividends::STATE_WIDTH;
        if values.len() != self.companies.len() * w {
            return Err(format!(
                "dividend has {} values, expected {} ({} per company)",
                values.len(), self.companies.len() * w, w));
        }
        for (c, chunk) in self.companies.iter_mut().zip(values.chunks_exact(w)) {
            c.stock.dividend = crate::market::dividends::DividendState::from_slice(chunk);
        }
        Ok(())
    }

    /// The amount per share each name paid at this session's open, roster
    /// order; 0.0 where nothing went ex, and on every preset.
    pub fn dividends_today(&self) -> Vec<f64> {
        self.companies
            .iter()
            .map(|c| c.stock.dividend.map(|d| d.paid_today).unwrap_or(0.0))
            .collect()
    }

    /// Every dividend declared so far, in declaration order.
    pub fn distributions(&self) -> &[Distribution] {
        &self.distribution_log
    }

    /// THE DIVIDENDS (`dividend_payout_share`), once per name at the open,
    /// after the overnight move and before the day's marks are set.
    ///
    /// Per name: the EMA of closes takes the last close; on the name's
    /// declaration session (21 before its ex-date) the next amount is
    /// declared; on its ex-date the price drops by the amount exactly, `s`
    /// is re-read against the fair value without the accrual (the small
    /// change is the `dividend` slot of the attribution, so the slots still
    /// sum to the change in `s`), a resting agent buy limit on the name is
    /// lowered by the amount (FINRA Rule 5330), and the amount is recorded
    /// as paid today; then the accrual fair value reads today is set. See
    /// [`crate::market::dividends`].
    ///
    /// No draw. With the dial at 0.0 it returns before touching anything,
    /// so every preset is bit-identical.
    fn apply_dividends(&mut self) {
        use crate::market::dividends as dv;
        if self.params.dividend_payout_share == 0.0 {
            return;
        }
        let n = self.companies.len();
        self.dividend_moves.clear();
        self.dividend_moves.resize(n, 0.0);
        for c in self.companies.iter_mut() {
            if let Some(d) = c.stock.dividend.as_mut() {
                d.paid_today = 0.0;
            }
        }
        let day = self.elapsed_days;
        let alpha = dv::ema_weight();
        for i in 0..n {
            let (ticker, price, fresh) = {
                let c = &self.companies[i];
                if c.is_bankrupt || !c.is_public {
                    continue;
                }
                (c.ticker.clone(), c.stock.price, c.stock.dividend.is_none())
            };
            let k = dv::sessions_since_ex(&ticker, day);
            if fresh {
                // A new state takes today's price as its reference and pays
                // nothing today; the accrual starts where the quarter is.
                let mut st = dv::new_state(&self.params, &self.companies[i], price);
                if k >= dv::DIVIDEND_PERIOD - dv::DECLARATION_LEAD {
                    st.declared = true;
                }
                if st.declared && st.target_yield > 0.0 {
                    self.distribution_log.push(Distribution {
                        company: i,
                        ticker: ticker.clone(),
                        declared_day: day,
                        ex_day: day + dv::DIVIDEND_PERIOD - k,
                        amount: st.amount,
                    });
                }
                st.accrual = dv::accrual_for(&self.params, &st, price, k);
                self.companies[i].stock.dividend = Some(st);
                // A name that has traded keeps its price: `s` is re-read
                // against the fair value with the accrual in it.
                if self.companies[i].stock.mispricing_s.is_some() && st.accrual != 0.0 {
                    let fv = crate::market::tick::tick_fair_value(
                        &self.params, &self.economy, self.nominal_output_base, day,
                        &self.companies[i], price);
                    if fv > 0.0 && price > 0.0 {
                        let s_new = crate::market::tick::clamp_s(
                            &self.params, crate::mathx::log(price / fv));
                        let stock = &mut self.companies[i].stock;
                        let before = stock.mispricing_s.unwrap_or(s_new);
                        stock.mispricing_s = Some(s_new);
                        if let Some(prev) = stock.mispricing_s_prev_close {
                            stock.mispricing_s_prev_close = Some(prev + (s_new - before));
                        }
                    }
                }
                continue;
            }
            let mut st = self.companies[i].stock.dividend.unwrap();
            st.price_ema += alpha * (price - st.price_ema);
            let mut paid = 0.0;
            if k == 0 {
                if !st.declared {
                    st.amount = dv::declare(&self.params, &st, price);
                    if st.target_yield > 0.0 {
                        self.distribution_log.push(Distribution {
                            company: i,
                            ticker: ticker.clone(),
                            declared_day: day,
                            ex_day: day,
                            amount: st.amount,
                        });
                    }
                }
                paid = st.amount;
                st.declared = false;
            } else if k == dv::DIVIDEND_PERIOD - dv::DECLARATION_LEAD {
                st.amount = dv::declare(&self.params, &st, price);
                st.declared = true;
                if st.target_yield > 0.0 {
                    self.distribution_log.push(Distribution {
                        company: i,
                        ticker: ticker.clone(),
                        declared_day: day,
                        ex_day: day + dv::DECLARATION_LEAD,
                        amount: st.amount,
                    });
                }
            }
            st.accrual = dv::accrual_for(&self.params, &st, price, k);
            st.paid_today = paid;
            self.companies[i].stock.dividend = Some(st);
            if paid > 0.0 {
                self.go_ex(i, paid);
            }
        }
    }

    /// The ex-date open for one name: the price drops by `amount`, `s` is
    /// re-read against today's fair value (whose accrual has just returned
    /// to zero), and resting agent buy limits drop by the amount.
    fn go_ex(&mut self, i: usize, amount: f64) {
        let day = self.elapsed_days;
        let old = self.companies[i].stock.price;
        let new = crate::mathx::max(0.01, old - amount);
        {
            let stock = &mut self.companies[i].stock;
            stock.price = new;
            stock.market_cap = new * stock.shares_outstanding;
        }
        if let Some(v) = self.repriced_pending.get_mut(i) {
            *v += crate::mathx::log(new / old);
        }
        if self.companies[i].stock.mispricing_s.is_some() {
            let fv = crate::market::tick::tick_fair_value(
                &self.params, &self.economy, self.nominal_output_base, day,
                &self.companies[i], new);
            if fv > 0.0 {
                let s_new = crate::market::tick::clamp_s(&self.params, crate::mathx::log(new / fv));
                let stock = &mut self.companies[i].stock;
                let before = stock.mispricing_s.unwrap_or(s_new);
                let moved = s_new - before;
                stock.mispricing_s = Some(s_new);
                if let Some(prev) = stock.mispricing_s_prev_close {
                    stock.mispricing_s_prev_close = Some(prev + moved);
                }
                if let Some(acc) = self.attribution.get_mut(i) {
                    acc[crate::market::factors::DIVIDEND_SLOT] += moved;
                }
                self.dividend_moves[i] = moved;
            }
        }
        let ticker = self.companies[i].ticker.clone();
        for o in self.book.orders.iter_mut() {
            if o.ticker == ticker && o.side == crate::order_book::Side::Buy {
                o.limit = crate::mathx::max(0.01, o.limit - amount);
            }
        }
    }

    /// What each name's `s` gave up to its fair value at the last close's
    /// jump, in roster order; 0.0 where nothing moved.
    pub fn jump_fair_value_moves(&self) -> &[f64] {
        &self.jump_fair_value_moves
    }

    /// Each name's last print at the most recent close, in roster order:
    /// the price as [`Engine::close_market`] (or [`Engine::close_day`])
    /// found it, before the close re-marks anything. Before the first
    /// close, the price the name was built or listed at.
    ///
    /// # Why this is not `previous_close`
    ///
    /// The `previous_close` field on each stock is reset at
    /// [`Engine::open_market`] to the day's OPENING price, after the
    /// overnight gap and after the close's re-mark to newly published macro
    /// data (`macro_publication_repricing`, on from `pt-v20`). It anchors
    /// the session's circuit-breaker band and the daily return GARCH reads,
    /// so it measures open to now, and the move between the last print and
    /// the open sits outside it by design. For a close-to-close day change,
    /// divide by [`Engine::prior_closes`] instead.
    ///
    /// Not part of the trajectory: nothing that prices reads it, it takes
    /// no draw, and [`Engine::state_hash`] leaves it out. A host that saves
    /// and restores an engine between days carries it with
    /// [`Engine::restore_closes`] if it wants the next day's change to be
    /// close-to-close.
    pub fn last_closes(&self) -> &[f64] {
        &self.last_closes
    }

    /// The close each name's current day is measured from, in roster
    /// order: [`Engine::last_closes`] as it stood at the most recent
    /// [`Engine::open_market`]. So `price / prior_close - 1` is the
    /// close-to-close change of the day in progress, and it stays that
    /// day's change after the close until the next open.
    ///
    /// On `pt-v20`, over 119 days of a 20-name market, the open differed
    /// from the previous day's last print on every name and day, by a
    /// median of 0.12 per cent and at most 0.55, so a day change taken
    /// against `previous_close` is off by that much.
    pub fn prior_closes(&self) -> &[f64] {
        &self.prior_closes
    }

    /// Restore [`Engine::last_closes`] and [`Engine::prior_closes`], one
    /// value per name each, as a host's own save carried them. Lengths are
    /// checked rather than truncated. Changes no price and no draw.
    pub fn restore_closes(&mut self, last: &[f64], prior: &[f64]) -> Result<(), String> {
        let n = self.companies.len();
        for (name, len) in [("last", last.len()), ("prior", prior.len())] {
            if len != n {
                return Err(format!("{name} closes have {len} values, expected {n}"));
            }
        }
        self.last_closes = last.to_vec();
        self.prior_closes = prior.to_vec();
        Ok(())
    }

    /// Close-of-day bookkeeping. Zero draws.
    ///
    /// Must run BEFORE any earnings shock the embedder applies that evening:
    /// the momentum roll reads `s` as it stands at the close, and an earnings
    /// gap applied first would be counted again as next-day herding. The
    /// the reference implementation's earnings path patches `sPrevClose` by the shock for the
    /// same reason.
    pub fn close_market(&mut self, request: &DayCloseRequest) {
        // The session is over: the rate indices mark to the committed level
        // until the close's re-mark and the next open, and the live VIX is
        // the published one.
        self.rate_live = None;
        self.vix_live = None;
        assert_eq!(
            request.daily_innovations.len(),
            self.companies.len(),
            "one innovation per company"
        );
        assert_eq!(
            request.sector_base_variances.len(),
            self.companies.len(),
            "one sector base variance per company"
        );
        // The session's last print, before anything below or the macro step
        // after it can re-mark a price. Read by nothing that prices.
        self.last_closes.clear();
        self.last_closes.extend(self.companies.iter().map(|c| c.stock.price));
        // The index on the same prints (`index_level_listed`). Nothing with
        // the switch off.
        self.index_mark_close();
        // The index futures' settlement marks on that index
        // (`futures_index_listed`). Nothing with the switch off.
        self.futures_close_marks();
        // The options' session ends (`surface_ssvi`).
        self.options_close();
        // WHAT THE FACTOR'S VARIANCE TARGET MEASURES THE VIX AGAINST, and
        // it has to be read HERE, before the per-name GARCH loop below
        // moves a single name's variance.
        //
        // At `market_vol_vix_excursion` 0.0 this is `self.vix_anchor`, the
        // expression that stood at the call site, so the argument is the
        // same f64 and every preset through pt-v18 is bit-identical with
        // no arithmetic executed on this branch at all.
        //
        // At nonzero it is the level the identity says today's VIX ought to
        // be: the read-back of the index's conditional variance AS THE
        // SESSION TRADED IT. Every input is engine state that
        // `state_snapshot` carries — the names' GARCH variances before the
        // loop, `market_vol.variance()` before its own close, and
        // `economy.vix` before the macro chain advances — so a restored
        // engine recomputes the same number rather than inheriting one.
        // That is why it is recomputed and not taken from
        // `last_index_variance`, which is deliberately out of the hash.
        //
        // `prev_day_down()` here is the bit TODAY's ticks read, because
        // `close_day_at` has not rolled `day_factor` yet. The economy
        // update's own call reads it after that roll and gets tomorrow's,
        // which is what a one-day-ahead variance needs and this is not:
        // this prices the session that just happened.
        //
        // See `ModelParams::market_vol_vix_excursion` for why the ratio's
        // denominator is the whole mechanism.
        let vix_ratio_denominator = if self.params.market_vol_vix_excursion == 0.0 {
            self.vix_anchor
        } else {
            let implied = crate::market::index_var::vix_from_variance(
                self.params.vix_variance_premium,
                self.index_conditional_variance_terms_now().total(),
            );
            // A read-back of zero or worse is not reachable — the factor
            // variance is floored at `market_vol_floor_multiple` times base
            // and the sum is of non-negative terms — but a denominator is
            // the one place where "not reachable" is worth a line rather
            // than a panic in somebody's overnight run. Falling back to the
            // anchor makes such a day read as the old form did.
            if implied > 0.0 { implied } else { self.vix_anchor }
        };
        // The GJR variance each name's draws were scaled by today, before
        // `close_day` steps it: the idiosyncratic state's expected
        // variance reads it after the jumps. Empty while the state is off.
        let idio_h_day: Vec<f64> = if self.idio_state_on() {
            self.companies.iter().map(|c| c.stock.garch_variance).collect()
        } else {
            Vec::new()
        };
        for (i, company) in self.companies.iter_mut().enumerate() {
            close_day_with(
                &self.params,
                company,
                &CloseInputs {
                    daily_innovation: request.daily_innovations[i],
                    sector_base_daily_variance: request.sector_base_variances[i],
                    vix: self.economy.vix,
                    // The PER-NAME channel is `garch_vix_coupling`, one of
                    // the instantaneous couplings, and it keeps the anchor.
                    // §3.4's option C names the variance arm of the loop and
                    // only the variance arm.
                    vix_anchor: self.vix_anchor,
                    avg_volume: request.avg_volume,
                },
            );
        }
        // The jumps, HERE and not after the sector close, where they stood
        // until `jump_market_variance_share` was wired. They have to run
        // after the per-name closes, because the momentum roll in those
        // closes sets the reference that `jump_momentum_share` then moves;
        // and they have to
        // run before the factor's close below, because that close consumes
        // `day_factor` and zeros it, so a jump added afterwards would reach
        // no shock at all -- `open_market` clears the accumulator again
        // before the next session's ticks.
        //
        // The move costs no preset a bit. Nothing between here and where
        // the call stood reads or writes `mispricing_s`, the attribution
        // accumulator or the jump excitation, and `apply_jumps` reads only
        // the params, the VIX and the anchor, none of which the factor,
        // sector, forced-flow or stress updates touch. The known-answer
        // digest is the proof rather than this paragraph.
        self.apply_jumps();
        self.close_idio_state(&idio_h_day);
        // The market factor's own close: its variance updates from the
        // day's accumulated factor, beside the per-name GARCH updates
        // above and with the same zero-draw discipline. The VIX read here
        // is the day's TRADING value — the macro chain has not advanced
        // yet, exactly as the per-name updates see the day they closed.
        // THE SLOW STOCHASTIC VARIANCE LEVEL, one normal per session.
        //
        // Drawn UNCONDITIONALLY, whatever the dials read, and on a stream of
        // its own: the schedule cannot depend on a settable, and the zero
        // arm of this mechanism is therefore the SAME RANDOM WORLD as the
        // live arm rather than a reshuffled one. See
        // `rng::stream::MARKET_VOL_LEVEL` for why that distinction is what
        // makes the comparison a measurement.
        //
        // At `market_vol_level_sigma` 0.0 -- every preset through pt-v18,
        // where the dial is zero -- the draw is taken,
        // `market_vol_log_level` stays exactly 0.0, the multiplier is
        // exactly 1.0 and `close_day_scaled` calls the very function the
        // close called before this existed. A preset with a positive sigma
        // takes the other branch, the level live and the multiplier not
        // 1.0; pt-v19 did from 2026-09-14 until the 2026-09-20 recomposition
        // returned the level to 0.0, and no shipped preset does now. This line said
        // "every preset through pt-v19" until 2026-09-18, and stopped
        // being true when pt-v19 took the dial off zero.
        self.market_vol_level_rng.site(Site::MarketVolLevelZ, 0);
        let level_z = self.market_vol_level_rng.next_normal();
        // THE VIX'S OWN SLOW LEVEL, on the same draw. A branch at sigma 0.0,
        // which is every shipped preset: no state moves and the multiplier
        // the close hands the VIX is exactly 1.0. Started from its stationary
        // distribution for the factor level's reason, with the same 0.0
        // sentinel; normalised to a mean of one on the LEVEL, since it
        // multiplies the VIX itself and not a variance whose root is read.
        if self.params.vix_level_sigma != 0.0 {
            let phi = self.params.vix_level_persistence;
            // The dial IS the switch above, and what the recursion drives
            // is the dispersion after the loop's own transmission has been
            // divided out. At `vix_level_loop_gain` 0.0 that is the dial's
            // own f64 and this line reads as it did.
            let sigma = self.vix_level_sigma_applied();
            let one_minus = 1.0 - phi * phi;
            let stationary_var = if one_minus > 0.0 { sigma * sigma / one_minus } else { 0.0 };
            self.vix_log_level = if self.vix_log_level == 0.0 {
                crate::mathx::sqrt(stationary_var) * level_z
            } else {
                phi * self.vix_log_level + sigma * level_z
            };
        }
        let market_vol_level = if self.params.market_vol_level_sigma == 0.0 {
            1.0
        } else {
            let phi = self.params.market_vol_level_persistence;
            let sigma = self.params.market_vol_level_sigma;
            // A persistence at or past one has no stationary dispersion, so
            // there is nothing to normalise against and nothing to start
            // from: such a level is a random walk and its own
            // non-stationarity is the thing to notice, not a NaN three
            // thousand sessions later.
            let one_minus = 1.0 - phi * phi;
            let stationary_var = if one_minus > 0.0 { sigma * sigma / one_minus } else { 0.0 };
            // STARTED FROM THE STATIONARY DISTRIBUTION, not from its centre.
            //
            // At the derived half-life of 295 sessions an AR(1) started at
            // zero has covered 42 per cent of its variance by day 252 and
            // the whole of the normalisation has been applied from day one,
            // so every recording would sit about five per cent low in
            // volatility and the box would measure a transient rather than
            // the process. Measured before this line existed: the 120-day
            // sample standard deviation read 0.9605 against a control's
            // 1.0072, which is `exp(-sd^2/4 / 2)` to three figures.
            //
            // The tape's windows are draws from a market that has been
            // running forever. This one has to be too, and one draw at the
            // stationary scale is the whole of the burn-in.
            //
            // The sentinel for "not yet started" is a log level of EXACTLY
            // 0.0, which is also a fresh engine's value and a pre-level
            // snapshot's. It needs no flag beside it and cannot go wrong if
            // it is ever hit by a genuine draw: re-starting a stationary
            // AR(1) from its own stationary distribution leaves it
            // stationary, so the sentinel firing spuriously costs one
            // session's autocorrelation and nothing else.
            self.market_vol_log_level = if self.market_vol_log_level == 0.0 {
                let opening = crate::mathx::sqrt(stationary_var) * level_z;
                // THE MARKET-SIDE WARM-UP, and this is the only place it
                // can go: the level it warms the variance to is drawn on
                // the line above.
                //
                // `macro_burn_in_days` settles the ECONOMY by running
                // `advance_day`, and `close_market` -- this function -- is
                // never called during it, so the two variance components
                // `close_day_scaled` is about to step have never run.
                // They open at the unscaled baseline while the level has
                // already opened at a stationary draw, and they spend the
                // first hundred sessions travelling to it. That travel is
                // what a measurement of the level sigma by horizon found to
                // be the whole of
                // the 252/504 calibration gap.
                //
                // A BRANCH at 0.0 sessions, which is every preset through
                // pt-v19: nothing runs, no state moves and no draw is
                // taken -- here or anywhere, at any setting. See
                // `ModelParams::market_burn_in_sessions` for why a
                // conditional MEAN of the close's own recursion is enough
                // and what it leaves behind, and `warm_to_level` for the
                // recursion.
                //
                // `vix_ratio_denominator` above was computed from the COLD
                // variance, because it is read before the per-name loop and
                // the level is not drawn until here. One session's
                // denominator, on the one session whose ticks already
                // traded at the cold sigma; the VIX's own three-session
                // relaxation closes it long before anything is recorded.
                //
                // `stationary_var > 0.0` is not belt and braces. At a
                // persistence at or past one there is no stationary
                // dispersion, the opening is exactly 0.0, the sentinel
                // above never clears and the level restarts every session
                // -- so a warm-up gated on the sentinel alone would run
                // 504 iterations a day forever on a model that is a random
                // walk and says so. There is also nothing to warm to.
                if self.params.market_burn_in_sessions > 0.0 && stationary_var > 0.0 {
                    self.market_vol.warm_to_level(
                        &self.params,
                        vix_ratio_denominator,
                        self.economy.vix,
                        opening,
                        stationary_var,
                        self.params.market_burn_in_sessions as i64,
                    );
                }
                opening
            } else {
                phi * self.market_vol_log_level + sigma * level_z
            };
            // NORMALISED ON THE SQUARE ROOT, not on the level. With
            // `E[L] = 1` the median annualised volatility falls by
            // `1 - exp(-sd^2/8)`, six per cent at the derived dispersion,
            // and `annualised_vol_pct` is a graded row -- the mechanism
            // would pay for its tail with a level nobody asked it to move.
            // `E[sqrt(L)] = 1` instead, which subtracts `sd^2/4` from the
            // log. A constant of the construction and not a free dial; see
            // `ModelParams::market_vol_level_sigma`.
            //
            crate::mathx::exp(self.market_vol_log_level - 0.25 * stationary_var)
        };
        // A FORCED close sets the variance to the level the law implies at
        // the VIX a scenario forced, instead of stepping toward it; see
        // `MarketVarianceState::close_day_forced`. The mark is consumed
        // here, so the next session closes free unless it is forced again.
        // False on every session nothing forced, which takes the branch that
        // stood here, unchanged.
        //
        // THE CYCLE'S VOLATILITY MULTIPLIER (`market_vol_cycle_ratio`): a
        // branch at 0.0, every preset through pt-v20, that reads and moves
        // nothing (pt-v21 sets it).
        // Off it, the multiplier steps toward the true phase's value every
        // close and scales the level the variance's baseline is taken at
        // and the VIX coupling's denominator -- except on a FORCED close,
        // which moves it but does not apply it: the VIX a scenario pinned
        // already carries the phase, and the forced level is the one that
        // VIX implies.
        let (market_vol_level, vix_ratio_denominator) = if self.params.market_vol_cycle_ratio == 0.0 {
            (market_vol_level, vix_ratio_denominator)
        } else {
            let l = self.step_market_vol_cycle();
            if self.vix_sets_variance_pending {
                (market_vol_level, vix_ratio_denominator)
            } else {
                // As the session applied it: one on a session a caller's
                // pins cover (`market_vol_cycle_pin_neutral`,
                // `market_vol_cycle_pin_phase`), `l` itself otherwise.
                let m = crate::mathx::exp(self.market_vol_cycle_applied_log(l));
                (market_vol_level * m * m, vix_ratio_denominator * self.market_vol_cycle_vix_scale())
            }
        };
        // THE TIME-HELD FADE'S CLOCK AND THE HELD VIX
        // (`market_vol_cycle_hold_onset`, `market_vol_vix_hold_half_life`):
        // a branch at 0.0, every shipped preset, that reads and moves
        // nothing. Off it, the phase's age is counted here, after the
        // multiplier's step has read tonight's age, and past a contraction's
        // or a trough's onset the coupling's denominator follows the held
        // VIX.
        let vix_ratio_denominator = if self.params.market_vol_cycle_hold_onset == 0.0 {
            vix_ratio_denominator
        } else {
            self.step_cycle_hold(vix_ratio_denominator)
        };
        self.last_market_targets = Some(if self.vix_sets_variance_pending {
            self.vix_sets_variance_pending = false;
            let denominator =
                self.forced_vix_denominator(vix_ratio_denominator, market_vol_level);
            self.market_vol.close_day_forced(
                &self.params,
                denominator,
                self.economy.vix,
                market_vol_level,
            )
        } else {
            self.market_vol.close_day_scaled(
                &self.params,
                vix_ratio_denominator,
                self.economy.vix,
                market_vol_level,
            )
        });
        self.close_sector_state();
        // Tonight's market draw was part of the day now closed.
        self.night_market_factor = 0.0;
        // The forced-flow reservoir drains on stress days and rebuilds in
        // calm. Updated only while the mechanism is live: at gain 0 or
        // reservoir 0 the state stays exactly 0.0 and nothing changes.
        if self.params.forced_flow_gain != 0.0 && self.params.forced_flow_reservoir > 0.0 {
            let excess = crate::mathx::max(
                0.0,
                self.economy.vix - self.params.forced_flow_threshold,
            );
            if excess > 0.0 {
                self.forced_flow_spent += excess;
            } else if self.params.forced_flow_replenish != 0.0 {
                self.forced_flow_spent *= 1.0 - self.params.forced_flow_replenish;
            }
        }
        self.update_universe_stress();
        // The jumps ran above, before the factor's close.
        self.carry_jump_moves();
        self.update_volume_state();
        self.update_volume_idio();
        // The rate indices only note that a night has begun, so the next
        // open accrues its carry. They reprice to the economy's new yields at
        // that open, not here: the close's macro step runs after this, and
        // under `rate_close_remark` re-marks them when it ends
        // (`advance_macro_day`).
        if !self.rates.is_empty() {
            self.rates.close();
        }
        self.accrue_buybacks();
        self.pull_relative_levels();
        self.step_aggregate_level();
        self.step_earnings_lookthrough();
        // The day's market t scale is the session's: the factor's close
        // above read the day it scaled, and the next open draws its own.
        self.market_day_scale = 1.0;
    }

    /// `fair_value_relative_knee`'s close step, after the buybacks. The
    /// reference is the equal-weighted mean fair-value level of the public,
    /// solvent names with a mispricing state; each such name whose level
    /// sits more than the knee below it is pulled toward the knee at
    /// `fair_value_relative_half_life`. A branch at 0.0, and nothing runs
    /// unless the model carries the levels (so the snapshot and the state
    /// hash see every change). No draws.
    fn pull_relative_levels(&mut self) {
        let knee = self.params.fair_value_relative_knee;
        if knee == 0.0 || !self.carries_fair_value_offsets() {
            return;
        }
        let pull = 1.0 - crate::mathx::pow(0.5, 1.0 / self.params.fair_value_relative_half_life);
        let live = |c: &TickCompany| {
            c.is_public && !c.is_bankrupt && c.stock.mispricing_s.is_some()
        };
        let (mut sum, mut n) = (0.0, 0usize);
        for c in self.companies.iter().filter(|c| live(c)) {
            sum += c.stock.fair_value_offset.unwrap_or(0.0);
            n += 1;
        }
        if n < 2 {
            return;
        }
        let floor = sum / n as f64 - knee;
        for c in self.companies.iter_mut().filter(|c| live(c)) {
            let v = c.stock.fair_value_offset.unwrap_or(0.0);
            if v < floor {
                c.stock.fair_value_offset = Some(v + pull * (floor - v));
            }
        }
    }

    /// Whether the model steps the aggregate earnings level
    /// (`fair_value_aggregate_jensen` or `fair_value_aggregate_half_life`
    /// set), which is when the snapshot and the state hash carry it.
    pub fn carries_aggregate_level(&self) -> bool {
        self.params.fair_value_aggregate_jensen != 0.0 || self.params.fair_value_aggregate_half_life != 0.0
    }

    /// The aggregate earnings level `A = ln(sum shares * eps * exp(v) / sum
    /// shares * eps)` over the public, solvent names with a mispricing state
    /// and a positive `eps`; `None` when no name qualifies.
    fn aggregate_level_now(&self) -> Option<f64> {
        let (mut num, mut den) = (0.0, 0.0);
        for c in self.companies.iter() {
            if !c.is_public || c.is_bankrupt || c.stock.mispricing_s.is_none() {
                continue;
            }
            let Some(eps) = c.eps else { continue };
            if !(eps > 0.0 && eps.is_finite()) {
                continue;
            }
            let w = c.stock.shares_outstanding * eps;
            den += w;
            num += w * crate::mathx::exp(c.stock.fair_value_offset.unwrap_or(0.0));
        }
        if den > 0.0 && num > 0.0 { Some(crate::mathx::log(num / den)) } else { None }
    }

    /// The aggregate earnings level's close step, after the relative knee
    /// (`fair_value_aggregate_jensen`, `fair_value_aggregate_half_life`):
    /// one shift `c` to every public, solvent name's level with a mispricing
    /// state, `c = (dA)^2 / 2` under the switch (dA is `A` now less `A` after
    /// the last step; the first close records `A` only) plus `-(1 - 0.5^(1/h))
    /// A` under the half-life. `A` moves by `c` exactly, and that is the level
    /// recorded. A branch at 0.0 on both, and nothing runs unless the model
    /// carries the levels. No draws.
    fn step_aggregate_level(&mut self) {
        if !self.carries_aggregate_level() || !self.carries_fair_value_offsets() {
            return;
        }
        let Some(a) = self.aggregate_level_now() else { return };
        let mut c = 0.0;
        if self.params.fair_value_aggregate_jensen != 0.0 {
            if let Some(prev) = self.aggregate_level {
                // Half the session's expected variance under
                // `fair_value_aggregate_expected` (a branch), else half the
                // realised squared change.
                if self.params.fair_value_aggregate_expected != 0.0 {
                    if let Some(v) = self.aggregate_variance {
                        c += 0.5 * v;
                    }
                } else {
                    let d = a - prev;
                    c += 0.5 * d * d;
                }
            }
        }
        let h = self.params.fair_value_aggregate_half_life;
        if h != 0.0 {
            c -= (1.0 - crate::mathx::pow(0.5, 1.0 / h)) * a;
        }
        if c != 0.0 {
            // THE STEP PRICED AT THE CLOSE (`fair_value_aggregate_remark`):
            // the fair values before the shift, so each name is re-marked
            // to the price its premium over fair value implies after it, as
            // the close's macro re-mark does. A branch: off, the shift
            // reaches the price at the next opening print.
            let marks = if self.params.fair_value_aggregate_remark != 0.0 {
                Some(self.fair_value_marks_now())
            } else {
                None
            };
            for comp in self.companies.iter_mut() {
                if !comp.is_public || comp.is_bankrupt || comp.stock.mispricing_s.is_none() {
                    continue;
                }
                let v = comp.stock.fair_value_offset.unwrap_or(0.0);
                comp.stock.fair_value_offset = Some(v + c);
            }
            if marks.is_some() {
                // The shift is growth throughout (`remark_growth_open_share`).
                self.reprice_marks(marks, RemarkGrowth::All);
            }
        }
        self.aggregate_level = Some(a + c);
    }

    /// Whether the model holds the session's expected variance of the
    /// aggregate earnings level (`fair_value_aggregate_expected` with
    /// `fair_value_aggregate_jensen`), which is when the snapshot and the
    /// state hash carry it.
    pub fn carries_aggregate_variance(&self) -> bool {
        self.params.fair_value_aggregate_expected != 0.0 && self.params.fair_value_aggregate_jensen != 0.0
    }

    /// The session's conditional variance of the aggregate earnings level's
    /// move, `V`, read at the open from the coefficients the session's draws
    /// are made at (`fair_value_aggregate_expected`). `omega_i` is name `i`'s
    /// weight in `A` (`shares * eps * exp(v)` over the names
    /// [`Engine::aggregate_level_now`] reads), `psi` the own share
    /// (`fair_value_news_share`), `psim` the market's permanent share at the
    /// session's sigma, `s_m` and `s_i` the night's shares, `K` the intraday
    /// curve's second moment and `g_i` the crisis epicentre's gain on the
    /// session's sector and own draws:
    ///
    /// ```text
    /// V = sigma_m^2 [ s_m (psim b)^2 + (1 - s_m) K (psim b + psi x c)^2 ]   b = sum omega_i beta_i,
    ///   + psi^2 sum_s sigma_s^2 [ s_i l_s^2 + (1 - s_i) K h^2 lg_s^2 ]       c = sum omega_i L_i g_i
    ///   + psi^2 sum_i omega_i^2 idio_i^2 r_i (s_i + (1 - s_i) K g_i^2)
    ///   + psim^2 (lambda_m (mu^2 + sigma_m,J^2) - (lambda_m mu)^2)
    ///   + psi^2 sum_i omega_i^2 lambda_i (1 + h_i) sigma_J^2
    ///   + psi^2 nu sigma_news^2 sum_i omega_i^2
    ///   + sum_i omega_i^2 (sigma_E^2 u_i'^2 1[report] + (sigma_S^2 1[reacts] + sigma_F^2 1[reacted]) u_i^2)
    /// ```
    ///
    /// with `l_s` and `lg_s` the sector's loaded weight without and with the
    /// gains, `x` the crisis blend's leak `spike (1 - source)` and `h = 1 - x`
    /// (`market::tick`'s `kept`), and `u_i'` and `u_i` the report's and the
    /// walks' units (`apply_night_split`, `earnings_unit_sigma`). The session
    /// block is `market::index_var`'s identity at these weights and shares.
    /// Left out, as there: the extended-hours ticks and caller-injected
    /// market news; and the crash amplifier, tilt and injection, which reach
    /// the level only off `fair_value_market_linear` (the plain loading is
    /// priced). `None` when no name qualifies. No draws.
    fn aggregate_expected_variance_now(&self) -> Option<f64> {
        let p = &self.params;
        let mut ws: Vec<(usize, f64)> = Vec::new();
        let mut den = 0.0;
        for (i, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.mispricing_s.is_none() {
                continue;
            }
            let Some(eps) = c.eps else { continue };
            if !(eps > 0.0 && eps.is_finite()) {
                continue;
            }
            let w = c.stock.shares_outstanding * eps * crate::mathx::exp(c.stock.fair_value_offset.unwrap_or(0.0));
            if !(w > 0.0 && w.is_finite()) {
                continue;
            }
            den += w;
            ws.push((i, w));
        }
        if !(den > 0.0) {
            return None;
        }
        let psi = p.fair_value_news_share;
        let sigma_m = self.market_sigma_before_day_scale();
        let psim = if p.fair_value_market_share == 0.0 {
            0.0
        } else {
            crate::market::tick::market_permanent_share(p, sigma_m, self.market_vol_cycle_cap_scale())
        };
        let sm = p.overnight_market_share;
        let si = p.overnight_idio_share;
        let night = self.night_split_on();
        let (sm, si) = if night { (sm, si) } else { (0.0, 0.0) };
        let k = crate::market::index_var::intraday_variance_factor();
        let shared_sigma = crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
        let sector_sigmas = self.sector_sigmas_now();
        let n_sectors = self.sector_keys.len();
        let spike = crate::market::tick::crisis_spike_for(p, self.economy.vix, self.universe_stress);
        let leak = spike * (1.0 - p.crisis_blend_source);
        let keep = 1.0 - leak;
        let epicentre = self.crisis_epicentre_key();
        let (g_up, g_down) = crate::market::factors::crisis_epicentre_gains(p.crisis_epicentre_extra);
        let idio_on = self.idio_state_on();
        let ratio = self.economy.vix / self.vix_anchor;
        let rate_scale = if p.jump_vix_coupling == 0.0 { 1.0 } else { (1.0 - p.jump_vix_coupling) + ((p.jump_vix_coupling * ratio) * ratio) };
        let intensity_market = if p.jump_vix_coupling == 0.0 { p.jump_intensity_market } else { p.jump_intensity_market * rate_scale };
        let intensity_idio = if p.jump_vix_coupling == 0.0 || p.jump_idio_vix_decoupled != 0.0 { p.jump_intensity_idio } else { p.jump_intensity_idio * rate_scale };
        let earnings = self.carries_earnings();
        let key = self.earnings_key;
        let day = self.elapsed_days;
        let (se, ss, sf) = (p.earnings_surprise_sigma, p.earnings_session_sigma, p.earnings_followthrough_sigma);

        let mut b = 0.0; // sum omega beta
        let mut lc = 0.0; // sum omega L g, the blend's leak loading
        let mut l_night = vec![0.0; n_sectors];
        let mut l_day = vec![0.0; n_sectors];
        let mut own = 0.0; // psi^2 sum omega^2 idio^2 r (s_i + (1 - s_i) K g^2)
        let mut wsq = 0.0;
        let mut jumps_own = 0.0;
        let mut earn = 0.0;
        for (i, w) in ws {
            let c = &self.companies[i];
            let omega = w / den;
            let beta = c.stock.beta.unwrap_or(1.0);
            let sector = self.sector_keys.iter().position(|key| *key == c.sector);
            let g = match (epicentre, sector) {
                (Some(e), Some(_)) => if c.sector.as_str() == e { g_up } else { g_down },
                _ => 1.0,
            };
            b += omega * beta;
            let loading = crate::market::factors::sector_loading_for(p, beta);
            if let Some(s) = sector {
                l_night[s] += omega * loading;
                l_day[s] += omega * loading * g;
                lc += omega * loading * g;
            }
            let daily_sigma = crate::mathx::sqrt(crate::mathx::max(c.stock.garch_variance, p.idio_sigma_floor));
            let unit = crate::market::factors::idio_scale_for(p, beta)
                * crate::market::factors::cap_size_multiplier_with(p, c.stock.market_cap);
            let r = if idio_on { self.idio_ratio_at(i) } else { 1.0 };
            let idio2 = daily_sigma * unit * daily_sigma * unit * r;
            own += omega * omega * idio2 * (si + (1.0 - si) * k * g * g);
            wsq += omega * omega;
            let rate = if p.jump_idio_excitation != 0.0 {
                crate::mathx::min(intensity_idio * (1.0 + self.jump_excitation.get(i).copied().unwrap_or(0.0)), 1.0)
            } else {
                crate::mathx::min(intensity_idio, 1.0)
            };
            jumps_own += omega * omega * rate * p.jump_sigma_idio * p.jump_sigma_idio;
            if earnings {
                let name = crate::earnings::id_hash(&c.id);
                let today = crate::earnings::reacts_on(key, name, day).is_some();
                let yesterday = crate::earnings::reacts_on(key, name, day - 1).is_some();
                if today || yesterday {
                    let sector_sigma = match sector {
                        Some(s) => loading * sector_sigmas.get(s).copied().unwrap_or(shared_sigma),
                        None => 0.0,
                    };
                    let own_unit = daily_sigma * unit;
                    let mut e2 = 0.0;
                    if today && se != 0.0 && night {
                        // The report's surprise, at the night's unit (its own
                        // draw at the idiosyncratic ratio).
                        let u2 = own_unit * own_unit * r + sector_sigma * sector_sigma;
                        e2 += se * se * u2;
                    }
                    let u2 = own_unit * own_unit + sector_sigma * sector_sigma;
                    if today && ss != 0.0 {
                        e2 += ss * ss * u2;
                    }
                    if yesterday && sf != 0.0 {
                        e2 += sf * sf * u2;
                    }
                    earn += omega * omega * e2;
                }
            }
        }
        let m_night = psim * b;
        let m_day = psim * b + psi * leak * lc;
        let mut v = sigma_m * sigma_m * (sm * m_night * m_night + (1.0 - sm) * k * m_day * m_day);
        for s in 0..n_sectors {
            let sigma = sector_sigmas.get(s).copied().unwrap_or(shared_sigma);
            v += psi * psi * sigma * sigma
                * (si * l_night[s] * l_night[s] + (1.0 - si) * k * keep * keep * l_day[s] * l_day[s]);
        }
        v += psi * psi * own;
        let mu = p.jump_mean_market;
        let sj = p.jump_sigma_market;
        let lm = crate::mathx::min(intensity_market, 1.0);
        v += psim * psim * (lm * (mu * mu + sj * sj) - (lm * mu) * (lm * mu));
        v += psi * psi * jumps_own;
        v += psi * psi * p.endogenous_news_intensity * p.endogenous_news_sigma * p.endogenous_news_sigma * wsq;
        v += earn;
        if v.is_finite() { Some(v) } else { None }
    }

    /// The session's expected variance for the snapshot and the state hash:
    /// a presence flag and the variance, while the model carries it.
    pub fn aggregate_variance_words(&self) -> Option<[f64; 2]> {
        if !self.carries_aggregate_variance() {
            return None;
        }
        Some(match self.aggregate_variance {
            Some(v) => [1.0, v],
            None => [0.0, 0.0],
        })
    }

    /// Put the session's expected variance back from
    /// [`Engine::aggregate_variance_words`].
    pub fn set_aggregate_variance_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.carries_aggregate_variance()) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                if w.len() != 2 || !(w[0] == 0.0 || w[0] == 1.0) || !w[1].is_finite() {
                    return Err(format!(
                        "this snapshot's aggregate_variance is {w:?}: it is a presence flag and the session's \
                         expected variance of the aggregate earnings level"));
                }
                self.aggregate_variance = if w[0] == 1.0 { Some(w[1]) } else { None };
                Ok(())
            }
            (None, true) => Err("this model sets fair_value_aggregate_expected and fair_value_aggregate_jensen \
                                 and the snapshot carries no aggregate_variance".to_string()),
            (Some(_), false) => Err("this snapshot carries aggregate_variance and this model does not set both \
                                     fair_value_aggregate_expected and fair_value_aggregate_jensen".to_string()),
        }
    }

    /// The aggregate level's state for the snapshot and the state hash: a
    /// presence flag and the level, while the model carries it.
    pub fn aggregate_level_words(&self) -> Option<[f64; 2]> {
        if !self.carries_aggregate_level() {
            return None;
        }
        Some(match self.aggregate_level {
            Some(a) => [1.0, a],
            None => [0.0, 0.0],
        })
    }

    /// Whether the model looks through the written earnings
    /// (`earnings_lookthrough_half_life` set), which is when the snapshot and
    /// the state hash carry its state.
    pub fn carries_earnings_lookthrough(&self) -> bool {
        self.params.earnings_lookthrough_half_life != 0.0
    }

    /// The look-through's weight on the written deviation, `c = rho / (rho +
    /// kappa)` with `rho = ln 2 / earnings_lookthrough_discount_half_life` and
    /// `kappa = ln 2 / earnings_lookthrough_half_life`; `None` with the dial
    /// off.
    ///
    /// # The derivation
    ///
    /// The written log level is `m = tau + x`, a permanent part and a
    /// deviation that reverts at `kappa`. The valuation reads the expected
    /// path discounted at `rho`, `E[ integral rho e^(-rho s) m(t+s) ds ] = a +
    /// c (m - a)`, with `a` the filtered permanent part: the generator
    /// equation `earnings_anticipation_terms` solves, with no phases.
    pub fn earnings_lookthrough_share(&self) -> Option<f64> {
        if !self.carries_earnings_lookthrough() {
            return None;
        }
        let h_kappa = self.params.earnings_lookthrough_half_life;
        let h_rho = self.params.earnings_lookthrough_discount_half_life;
        // rho / (rho + kappa) with both as ln 2 over their half-lives; a
        // discount half-life of 0 is an immediate discount, c = 1.
        Some(h_kappa / (h_kappa + h_rho))
    }

    /// Record a `market.earnings` write: `log_factor` is the log of the common
    /// factor it scaled every name's earnings by. The written level moves by
    /// it and the anchor by `earnings_lookthrough_gain` of it, and the
    /// valuation's look-through is refreshed. A branch with the dial off: the
    /// call changes nothing. Refused for a factor that is not finite.
    pub fn note_market_earnings(&mut self, log_factor: f64) -> Result<(), String> {
        if !log_factor.is_finite() {
            return Err(format!(
                "note_market_earnings takes the log of a positive, finite factor; this one is {log_factor}"));
        }
        if !self.carries_earnings_lookthrough() {
            return Ok(());
        }
        self.lookthrough_written += log_factor;
        self.lookthrough_anchor += self.params.earnings_lookthrough_gain * log_factor;
        self.refresh_earnings_lookthrough();
        Ok(())
    }

    /// The look-through's close step: the anchor takes
    /// `earnings_lookthrough_gain` of the reversion the valuation expected
    /// over the session and did not see, `(1 - 0.5^(1/h)) (m - a)`. A branch
    /// with the dial off, and nothing moves while `m == a`. No draws.
    fn step_earnings_lookthrough(&mut self) {
        if !self.carries_earnings_lookthrough() {
            return;
        }
        let gap = self.lookthrough_written - self.lookthrough_anchor;
        if gap != 0.0 {
            let h = self.params.earnings_lookthrough_half_life;
            let expected = (1.0 - crate::mathx::pow(0.5, 1.0 / h)) * gap;
            self.lookthrough_anchor += self.params.earnings_lookthrough_gain * expected;
        }
        self.refresh_earnings_lookthrough();
    }

    /// Keep `economy.earnings_lookthrough` current: `-(1 - c) (m - a)`, and
    /// exactly 0.0 with the dial off or while `m == a`.
    pub fn refresh_earnings_lookthrough(&mut self) {
        let look = match self.earnings_lookthrough_share() {
            None => 0.0,
            Some(c) => {
                let gap = self.lookthrough_written - self.lookthrough_anchor;
                if gap == 0.0 { 0.0 } else { -(1.0 - c) * gap }
            }
        };
        self.economy.earnings_lookthrough = look;
    }

    /// The look-through's state for the snapshot and the state hash, `[m,
    /// a]`, while the dial is set.
    pub fn earnings_lookthrough_words(&self) -> Option<[f64; 2]> {
        if !self.carries_earnings_lookthrough() {
            return None;
        }
        Some([self.lookthrough_written, self.lookthrough_anchor])
    }

    /// Put the look-through's state back from
    /// [`Engine::earnings_lookthrough_words`], and the valuation's reading of
    /// it with it.
    pub fn set_earnings_lookthrough_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.carries_earnings_lookthrough()) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                if w.len() != 2 || !w[0].is_finite() || !w[1].is_finite() {
                    return Err(format!(
                        "this snapshot's earnings_lookthrough is {w:?}: it is the written log level and its \
                         anchor"));
                }
                self.lookthrough_written = w[0];
                self.lookthrough_anchor = w[1];
                self.refresh_earnings_lookthrough();
                Ok(())
            }
            (None, true) => Err("this model sets earnings_lookthrough_half_life and the snapshot carries no \
                                 earnings_lookthrough".to_string()),
            (Some(_), false) => Err("this snapshot carries earnings_lookthrough and this model does not set \
                                     earnings_lookthrough_half_life".to_string()),
        }
    }

    /// Put the aggregate level back from [`Engine::aggregate_level_words`].
    pub fn set_aggregate_level_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.carries_aggregate_level()) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                if w.len() != 2 || !(w[0] == 0.0 || w[0] == 1.0) || !w[1].is_finite() {
                    return Err(format!(
                        "this snapshot's aggregate_level is {w:?}: it is a presence flag and the aggregate \
                         earnings level"));
                }
                self.aggregate_level = if w[0] == 1.0 { Some(w[1]) } else { None };
                Ok(())
            }
            (None, true) => Err("this model sets fair_value_aggregate_jensen or fair_value_aggregate_half_life \
                                 and the snapshot carries no aggregate_level".to_string()),
            (Some(_), false) => Err("this snapshot carries aggregate_level and this model sets neither \
                                     fair_value_aggregate_jensen nor fair_value_aggregate_half_life".to_string()),
        }
    }

    /// `buyback_accrual`'s close step, after the rates close. Each public,
    /// solvent name with a mispricing state adds one session's buyback
    /// yield, read at the close price on the earnings the valuation holds
    /// (nominal scale and fair-value level included), to its log share-count
    /// reduction. See `market::tick::buyback_accrual_step`.
    fn accrue_buybacks(&mut self) {
        if !self.carries_buyback_log_shares() {
            return;
        }
        let p = &self.params;
        let nominal = crate::market::tick::nominal_scale(p, &self.economy, self.nominal_output_base);
        for c in self.companies.iter_mut() {
            if !c.is_public || c.is_bankrupt || c.stock.mispricing_s.is_none() {
                continue;
            }
            let Some(eps) = c.eps else { continue };
            let earnings = if nominal == 1.0 { eps } else { eps * nominal };
            let earnings = match c.stock.fair_value_offset {
                Some(v) if v != 0.0 => earnings * crate::mathx::exp(v),
                _ => earnings,
            };
            let l = c.stock.buyback_log_shares.unwrap_or(0.0);
            // The name's own share under `dividend_buyback_substitution`,
            // `buyback_payout_share` otherwise: the one function the
            // valuation's buyback term reads too.
            let share = crate::market::dividends::buyback_share(p, c);
            let dl = crate::market::tick::buyback_accrual_step(p, share, Some(earnings), c.stock.price, l);
            if dl != 0.0 {
                c.stock.buyback_log_shares = Some(l + dl);
            }
        }
    }

    /// Each name's accrued log share-count reduction under
    /// `buyback_accrual`, in roster order, 0.0 where nothing has accrued.
    /// For checkpoints and forks.
    pub fn buyback_log_shares(&self) -> Vec<f64> {
        self.companies.iter().map(|c| c.stock.buyback_log_shares.unwrap_or(0.0)).collect()
    }

    /// Put the accrued log share-count reductions back. A width mismatch is
    /// refused, as the fair-value levels' is: they are positional against
    /// the roster.
    pub fn set_buyback_log_shares(&mut self, values: &[f64]) -> Result<(), String> {
        if values.len() != self.companies.len() {
            return Err(format!(
                "this snapshot carries {} buyback share-count reductions and the \
                 roster holds {} companies. They are positional against the roster, \
                 so this restore is refused rather than padded or truncated.",
                values.len(),
                self.companies.len()
            ));
        }
        for (c, &v) in self.companies.iter_mut().zip(values) {
            c.stock.buyback_log_shares = if v == 0.0 { None } else { Some(v) };
        }
        Ok(())
    }

    /// Whether this engine's model accrues the buyback share count, which is
    /// when the snapshot and the state hash carry it: `buyback_accrual` and
    /// `buyback_payout_share` both set. Off on every preset as shipped.
    pub fn carries_buyback_log_shares(&self) -> bool {
        self.params.buyback_accrual != 0.0 && self.params.buyback_payout_share != 0.0
    }

    /// The stationary opening (`opening_mispricing_sigma`), applied once,
    /// before the first tick that prices anything.
    ///
    /// Each name's day-zero premium of price over its published fair value
    /// is split in two. The mispricing `s` is a common level plus this
    /// name's draw at `opening_mispricing_sigma`, the draws re-centred to
    /// cap-weighted zero. The common level is a draw at
    /// `opening_market_sigma` when that is non-zero, the market opening at
    /// a point of its own stationary mispricing; at 0.0 it is the roster's
    /// cap-weighted premium, so the index opens with the mispricing the
    /// generated roster gives it (and reverts from it over the first months,
    /// which is a start-up drift of up to 30 per cent on a 20-name roster). The rest of the premium is the name's opening
    /// fair-value level `v`. The price does not move: `P = FV exp(v) exp(s)`
    /// holds with the same `P`. What changes is how much of the premium the
    /// model later pulls back: only the stationary part.
    ///
    /// A name listed after the engine was built has no draw and takes the
    /// lazy opening in `market::tick`, which adopts its whole premium as `s`.
    fn apply_opening(&mut self) {
        let z = std::mem::take(&mut self.opening_z);
        let sigma = self.params.opening_mispricing_sigma;
        // The per-name draws are the first `n`, the market's the last.
        let n_draws = z.len().saturating_sub(1);
        let mut gap = vec![f64::NAN; self.companies.len()];
        let (mut wsum, mut wgap, mut wz) = (0.0, 0.0, 0.0);
        // The prehistory's carried mispricing (`market_prehistory_valuation`),
        // empty unless that dial is on: a carried name opens at the copy's
        // `s` and stays out of the draws' centring.
        let carry = std::mem::take(&mut self.opening_carry);
        let mut carried = vec![false; self.companies.len()];
        for (i, c) in self.companies.iter().enumerate() {
            if i >= n_draws || c.stock.mispricing_s.is_some() || c.is_bankrupt || !c.is_public {
                continue;
            }
            let is_carried = carry.get(i).is_some_and(|v| v.is_finite());
            let fv = crate::market::tick::published_fair_value(
                &self.params, &self.economy, self.nominal_output_base, self.elapsed_days, c);
            let g = crate::mathx::log(crate::mathx::max(0.01, c.stock.price) / fv);
            gap[i] = g;
            if is_carried {
                carried[i] = true;
                continue;
            }
            let w = c.stock.price * c.stock.shares_outstanding;
            wsum += w;
            wgap += w * g;
            wz += w * z[i];
        }
        if !(wsum > 0.0) && !carried.iter().any(|&b| b) {
            return;
        }
        let centre = if self.params.opening_market_sigma != 0.0 {
            self.params.opening_market_sigma * z[n_draws]
        } else {
            wgap / wsum
        };
        let zbar = wz / wsum;
        for (i, c) in self.companies.iter_mut().enumerate() {
            if gap[i].is_nan() {
                continue;
            }
            let s0 = if carried[i] {
                crate::market::tick::clamp_s(&self.params, carry[i])
            } else {
                crate::market::tick::clamp_s(&self.params, centre + sigma * (z[i] - zbar))
            };
            c.stock.mispricing_s = Some(s0);
            c.stock.mispricing_s_prev_close = Some(s0);
            c.stock.mispricing_momentum = Some(0.0);
            // With a dividend accrued, fair value is `base * exp(v) + A`
            // rather than `(base + A) * exp(v)`, so the level that opens the
            // name at its own price solves `(base * exp(v) + A) * exp(s0) =
            // P`. A branch: without an accrual, the line that stood.
            let a = crate::market::dividends::accrual(c);
            let v = if a == 0.0 {
                gap[i] - s0
            } else {
                let price = crate::mathx::max(0.01, c.stock.price);
                let base = price / crate::mathx::exp(gap[i]) - a;
                let rest = price / crate::mathx::exp(s0) - a;
                if base > 0.0 && rest > 0.0 {
                    crate::mathx::log(rest / base)
                } else {
                    gap[i] - s0
                }
            };
            c.stock.fair_value_offset = Some(v);
        }
    }

    /// Endogenous jumps, applied once per name at the day close.
    ///
    /// The model has no discontinuities without this. Prices diffuse; real
    /// markets gap, and nothing here ever surprised the market unless a
    /// caller injected news by hand. That is why excess kurtosis reads 5.2
    /// over 504-day windows against real markets' 7.1 to 22 -- fat tails at
    /// that scale are not reachable from a diffusion plus GARCH at any
    /// coefficients, so this is a mechanism gap and not a calibration one.
    ///
    /// # Where the jump lands, and why not on the price
    ///
    /// A jump moves `mispricing_s`, the same channel news already uses,
    /// rather than the price directly. That makes it a gap AWAY from fair
    /// value which then mean-reverts on the existing process -- which is
    /// what a news or panic jump does -- and it reuses a tested path
    /// instead of opening a second way for something to move a price.
    /// The existing cap applies, so a jump cannot dislocate a name further
    /// than the model's own guard allows.
    ///
    /// # Draw discipline
    ///
    /// Two draws for the market, then two per company, ALWAYS -- never
    /// conditionally. A schedule that depended on whether a jump fired
    /// would make the stream position a function of the parameters, and
    /// every preset would stop being comparable under common random
    /// numbers. The uniform decides occurrence, the normal decides size,
    /// and at zero intensity `u < 0.0` is false for every `u` in [0, 1),
    /// so nothing fires.
    ///
    /// These draws come from [`stream::JUMPS`], which no earlier preset
    /// touched. That is what lets a draw-CONSUMING mechanism ship inert:
    /// the market, economy and external streams are untouched, so every
    /// shipped preset reproduces bit for bit.
    ///
    /// `rustfmt::skip` holds the generated body exactly as the emitter
    /// wrote it. A format run would re-wrap the one-line if-else
    /// expressions inside the markers, and the generated body is compared
    /// to the committed text byte for byte, so a routine format run would
    /// leave that comparison failing until somebody regenerated the body.
    #[rustfmt::skip]
    #[allow(clippy::if_same_then_else, reason = "the generated idiosyncratic rate spells out each branch of the spec, two of which give the same value")]
    fn apply_jumps(&mut self) {
        self.jump_fair_value_moves.clear();
        self.jump_fair_value_moves.resize(self.companies.len(), 0.0);
        // The permanent share's reading of the jump (`fair_value_news_share`):
        // each name's `s` before the generated body, so the company's own
        // jump can be told from the market's after it. Empty, and nothing
        // below runs, at 0.0.
        //
        // The idiosyncratic variance state reads the same split for its own
        // shock, so it takes `s_before` too, and the excitation each name's
        // rate is drawn at (the generated body below steps it forward).
        let idio_on = self.idio_state_on();
        let excitation_drawn: Vec<f64> = if idio_on && self.params.jump_idio_excitation != 0.0 {
            self.jump_excitation.clone()
        } else {
            Vec::new()
        };
        let s_before: Vec<f64> = if self.params.fair_value_news_share != 0.0
            || self.params.fair_value_market_share != 0.0
            || idio_on
        {
            self.companies.iter().map(|c| c.stock.mispricing_s.unwrap_or(f64::NAN)).collect()
        } else {
            Vec::new()
        };
        // mechanism:jumps begin -- generated by tools/mechanism/emit.py from
        // tools/mechanism/mechanisms/jumps.py (spec 0ff870280c51); do not edit by hand.
        // Draws: 1 uniform, 1 normal, 1 uniform per company, 1 normal per company on the jumps stream, unconditionally.
        let p = &self.params;
        let ratio = self.economy.vix / self.vix_anchor;
        let rate_scale = if p.jump_vix_coupling == 0.0 { 1.0 } else { (1.0 - p.jump_vix_coupling) + ((p.jump_vix_coupling * ratio) * ratio) };
        let intensity_market = if p.jump_vix_coupling == 0.0 { p.jump_intensity_market } else { p.jump_intensity_market * rate_scale };
        // The idiosyncratic rate: scaled by the VIX like the market's unless
        // `jump_idio_vix_decoupled` says the tape does not support that
        // (vix-dynamics.md 19.1), and excited by the name's own recent jumps
        // when `jump_idio_excitation` is set. Both are branches at 0.0.
        let intensity_idio = if p.jump_vix_coupling == 0.0 { p.jump_intensity_idio } else if p.jump_idio_vix_decoupled != 0.0 { p.jump_intensity_idio } else { p.jump_intensity_idio * rate_scale };
        let excite = p.jump_idio_excitation;
        let excite_decay = p.jump_idio_excitation_decay;
        let mut excitation = if excite != 0.0 {
            let mut e = std::mem::take(&mut self.jump_excitation);
            if e.len() < self.companies.len() { e.resize(self.companies.len(), 0.0); }
            e
        } else {
            Vec::new()
        };
        self.jump_rng.site(Site::JumpMarketU, 0);
        let u_market = self.jump_rng.next_f64();
        self.jump_rng.site(Site::JumpMarketZ, 0);
        let z_market = self.jump_rng.next_normal();
        let market = if u_market < intensity_market { p.jump_mean_market + (p.jump_sigma_market * z_market) } else { 0.0 };
        let compensator = if p.jump_mean_compensated == 0.0 { 0.0 } else { p.jump_mean_compensated * (intensity_market * p.jump_mean_market) };
        for (index, company) in self.companies.iter_mut().enumerate() {
            self.jump_rng.site(Site::JumpCompanyU, index as u32);
            let u = self.jump_rng.next_f64();
            self.jump_rng.site(Site::JumpCompanyZ, index as u32);
            let z = self.jump_rng.next_normal();
            let rate = if excite != 0.0 { intensity_idio * (1.0 + excitation[index]) } else { intensity_idio };
            let jumped = u < rate;
            let idio = if jumped { p.jump_sigma_idio * z } else { 0.0 };
            if excite != 0.0 {
                excitation[index] = (excite_decay * excitation[index]) + (if jumped { excite } else { 0.0 });
            }
            let total = (market + idio) - compensator;
            if total != 0.0 {
                if let Some(s) = company.stock.mispricing_s {
                    let after = crate::market::tick::clamp_s(&self.params, s + total);
                    if let Some(acc) = self.attribution.get_mut(index) {
                        acc[8] += after - s;
                    }
                    company.stock.mispricing_s = Some(after);
                    let carried = (1.0 - p.jump_momentum_share) * (after - s);
                    if carried != 0.0 {
                        if let Some(prev) = company.stock.mispricing_s_prev_close {
                            company.stock.mispricing_s_prev_close = Some(prev + carried);
                        }
                    }
                }
            }
        }
        if excite != 0.0 {
            self.jump_excitation = excitation;
        }
        // mechanism:jumps end
        //
        // THE JUMP JOINS THE DAY'S FACTOR INNOVATION. `market` is the log
        // return the jump put into every name's `s`, and `day_factor` is
        // the sum of the day's per-tick market factors that the factor's
        // GJR steps on. The tape's index fit was on TOTAL returns, so the
        // shock the coefficients were fitted to saw crash days; this is
        // where the model's shock sees one. See
        // `ModelParams::jump_market_variance_share` for the derivation and
        // for why this call sits before the factor's own close.
        //
        // Read outside the generated region and not inside it, because the
        // body between the markers is compared to what the emitter writes
        // and every shipped preset records its digest. `market` is a
        // top-level binding of that body, so it is still in scope here.
        //
        // Guarded on the share AND on the jump, so a day that does not jump
        // adds nothing at all rather than adding a zero to an accumulator
        // that may be holding a negative one.
        if self.params.jump_market_variance_share != 0.0 && market != 0.0 {
            self.market_vol
                .accumulate(self.params.jump_market_variance_share * market);
        }
        // THE COMPANY'S OWN JUMP, SHARED WITH FAIR VALUE. The body above put
        // `market + idio - compensator` into `s`; what is left after taking
        // the common part out is the company's own jump (exactly zero on a
        // name that did not jump, up to the rounding of `(s + x) - s`, which
        // the 1e-9 floor absorbs: a company jump is `jump_sigma_idio` times a
        // normal). `psi` of it moves to the fair-value level, as the tick
        // does with the name's own noise and news. The attribution slot
        // keeps the whole jump: it reports what moved the price.
        // THE OWN JUMP'S EXPECTED VARIANCE, for the idiosyncratic variance
        // state: the rate each name was drawn at tonight (the VIX scale and
        // the excitation before the body stepped it) times
        // `jump_sigma_idio^2`, in the generated body's own spelling.
        if idio_on {
            let p = &self.params;
            let ratio = self.economy.vix / self.vix_anchor;
            let rate_scale = if p.jump_vix_coupling == 0.0 { 1.0 } else { (1.0 - p.jump_vix_coupling) + ((p.jump_vix_coupling * ratio) * ratio) };
            let intensity_idio = if p.jump_vix_coupling == 0.0 || p.jump_idio_vix_decoupled != 0.0 { p.jump_intensity_idio } else { p.jump_intensity_idio * rate_scale };
            let var1 = p.jump_sigma_idio * p.jump_sigma_idio;
            for i in 0..self.companies.len() {
                let ex = excitation_drawn.get(i).copied().unwrap_or(0.0);
                let rate = crate::mathx::min(intensity_idio * (1.0 + ex), 1.0);
                if let Some(slot) = self.idio_jump_var_today.get_mut(i) {
                    *slot = rate * var1;
                }
            }
        }
        if !s_before.is_empty() {
            let psi = self.params.fair_value_news_share;
            let psim = crate::market::tick::market_permanent_share(
                &self.params, self.market_vol.sigma_daily(),
                self.market_vol_cycle_cap_scale());
            let common = market - compensator;
            for (index, company) in self.companies.iter_mut().enumerate() {
                let (Some(after), Some(&before)) = (company.stock.mispricing_s, s_before.get(index)) else {
                    continue;
                };
                if before.is_nan() {
                    continue;
                }
                let own = (after - before) - common;
                // The company's own jump on the stock-level share, the
                // market's on the market-wide share (a name the clamp held
                // back from the market jump keeps the share of what it took).
                let own = if own.abs() <= 1e-9 { 0.0 } else { own };
                // The own jump, which tomorrow's session realises and
                // tomorrow's close reads into the idiosyncratic state.
                if idio_on {
                    if let Some(slot) = self.idio_jump_today.get_mut(index) {
                        *slot = own;
                    }
                }
                let taken_common = (after - before) - own;
                let dv = psi * own + psim * taken_common;
                if dv == 0.0 {
                    continue;
                }
                let s_new = after - dv;
                company.stock.mispricing_s = Some(s_new);
                // What left `s` for `v`, booked where the tape can find it:
                // the attribution's `fair_value_shift` and, for the tape's
                // row where the jump is observed, `jump_fair_value_moves`.
                if let Some(acc) = self.attribution.get_mut(index) {
                    acc[crate::market::factors::FAIR_VALUE_SLOT] += s_new - after;
                }
                if let Some(slot) = self.jump_fair_value_moves.get_mut(index) {
                    *slot = s_new - after;
                }
                if let Some(prev) = company.stock.mispricing_s_prev_close {
                    let carried = (1.0 - self.params.jump_momentum_share) * dv;
                    company.stock.mispricing_s_prev_close = Some(prev - carried);
                }
                let v = company.stock.fair_value_offset.unwrap_or(0.0);
                company.stock.fair_value_offset = Some(v + dv - 0.5 * dv * dv);
            }
        }
    }

    /// Carry the day's jumps over the open, for the volume scale.
    ///
    /// `apply_jumps` books each name's clamped jump into the jump slot of
    /// the attribution accumulator and is the only writer of that slot, so
    /// after it has run the slot IS the day's jump for that name -- the
    /// clamped one, which is the move the price will actually take.
    /// `open_market` clears the accumulator, and the gap does not trade in
    /// until the session after the close that booked it, so the number has
    /// to be copied somewhere that survives the open.
    ///
    /// Gated on the dial, so at the shipped 1.0 the vector is never
    /// written, stays all zeros, and the close does no work for a
    /// mechanism nothing reads.
    fn carry_jump_moves(&mut self) {
        if self.params.volume_move_jump_share == 1.0 {
            return;
        }
        if self.jump_move.len() < self.companies.len() {
            self.jump_move.resize(self.companies.len(), 0.0);
        }
        for (index, slot) in self.jump_move.iter_mut().enumerate() {
            *slot = match self.attribution.get(index) {
                Some(acc) => acc[crate::market::factors::JUMP_SLOT],
                None => 0.0,
            };
        }
    }

    /// The shared persistent volume component, stepped once per day.
    ///
    /// Volume is otherwise a LEVEL with independent per-tick noise, so
    /// consecutive volumes are near-independent draws around a fixed mean
    /// and differencing them gives a change autocorrelation near -0.5 at any
    /// coefficients. That is the whole reason `volume_change_acf1` sits 13.7
    /// seed-sd outside its band and is excluded from the objective as
    /// structurally unreachable: the defect is an absent process, and no
    /// parameter reaches it.
    ///
    /// A log-scale AR(1), so a busy day is followed by a busy day. The
    /// draw is unconditional and lives on [`stream::VOLUME`], so the
    /// schedule never depends on the parameters and no earlier preset's
    /// sequence moves.
    ///
    /// COMMON component only: every name shares this multiplier. Real volume
    /// persistence is partly idiosyncratic, and that half is not modelled.
    fn update_volume_state(&mut self) {
        self.volume_rng.site(Site::VolumeZ, 0);
        let z = self.volume_rng.next_normal();
        let p = &self.params;
        // Guarded rather than computed through: at zero persistence and zero
        // innovation the state must stay exactly 0.0, so the multiplier stays
        // exactly 1.0 and the tick's volume arithmetic is untouched.
        if p.volume_persistence == 0.0 && p.volume_innovation_sigma == 0.0 {
            return;
        }
        self.volume_state =
            p.volume_persistence * self.volume_state + p.volume_innovation_sigma * z;
    }

    /// The per-NAME volume state, one draw per company per day.
    ///
    /// On its own stream and drawn UNCONDITIONALLY, the same discipline
    /// `apply_jumps` follows: the count must not depend on a parameter or
    /// the schedule stops being comparable across presets. At zero sigma
    /// every state stays exactly 0.0, so the multiplier stays exactly 1.0
    /// and the tick's volume arithmetic is untouched.
    ///
    /// # The count is the roster's width
    ///
    /// The loop walks `volume_idio`, which [`Engine::add_company`] and
    /// [`Engine::remove_company`] hold at the roster's width. So a run that
    /// lists or delists draws from this stream at the NEW width from that
    /// mutation onward, and its position on any later day differs from the
    /// position the same run reached before issue #148 was fixed on
    /// 2026-09-02, when the two mutations left the array at its
    /// construction width. Every other stream is untouched by the
    /// difference, and every shipped preset holds both coefficients at 0.0,
    /// so no shipped price path moves.
    fn update_volume_idio(&mut self) {
        let p = &self.params;
        let (rho, sigma) = (p.volume_idio_persistence, p.volume_idio_sigma);
        for i in 0..self.volume_idio.len() {
            self.volume_idio_rng.site(Site::VolumeIdioZ, i as u32);
            let z = self.volume_idio_rng.next_normal();
            if rho == 0.0 && sigma == 0.0 {
                continue;
            }
            self.volume_idio[i] = rho * self.volume_idio[i] + sigma * z;
        }
    }

    /// The daily macro step: economy, cycle roll, then the central bank.
    ///
    /// The order is the reference implementation's and is load-bearing — the rates and VIX
    /// the factor model reads on the first tick of a new day are already the
    /// day's NEW values, not yesterday's.
    pub fn advance_day(&mut self, request: &DayAdvanceRequest) -> DayAdvanceOutcome {
        // The ECONOMY stream, not the market's. The macro chain's draw count
        // genuinely depends on macro state — a cycle entering contraction
        // draws a shock the expansion never rolls — and before the split
        // that variability shifted every market draw after it. Now its
        // branches move only its own stream.
        let mut rng = std::mem::replace(&mut self.economy_rng, GameRng::new(0, MAIN_STREAM));
        let mut counting = Counting {
            inner: &mut rng,
            count: 0,
        };
        let mut outcome = self.advance_day_with(request, &mut counting);
        let consumed = counting.count;
        self.economy_rng = rng;
        self.draws.economy += consumed;
        outcome.draws_consumed = consumed;
        outcome
    }

    /// The daily macro step against an EXTERNAL draw source. See
    /// [`Engine::tick_with`].
    pub fn advance_day_with(
        &mut self,
        request: &DayAdvanceRequest,
        rng: &mut impl Rng,
    ) -> DayAdvanceOutcome {
        let phase_before = self.economy.cycle_phase;
        // THE MARKET'S NOWCAST, first: the report on the phase this session
        // ran in, so a turn at tonight's close is reported from tomorrow's.
        let nowcast_on = self.params.cycle_nowcast_accuracy != 0.0;
        let spread_blend = nowcast_on || self.params.corporate_spread_cycle != 0.0;
        let spread_before = if spread_blend { self.priced_spread_multiplier(phase_before) } else { 0.0 };
        if nowcast_on {
            self.update_cycle_nowcast();
        }
        let spread_after_report = if spread_blend { self.priced_spread_multiplier(phase_before) } else { 0.0 };
        // Today's pins and the corporate yield a pin wrote, for the end of
        // the step: a pinned corporate yield holds through tonight's close,
        // a meeting's re-anchoring included (`macro_pins_today`).
        let pins_today = self.macro_pins_today;
        let corporate_pinned_at = self.economy.corporate_bond_yield;
        // EVERY PINNED FIELD HOLDS THROUGH THE CLOSE (`macro_pins_hold`):
        // the fields as the pins left them, which nothing between a pin and
        // the close writes. `None`, copying nothing, with the dial off or no
        // pin today.
        let held = if self.params.macro_pins_hold != 0.0 && pins_today != 0 {
            Some(self.economy.clone())
        } else {
            None
        };
        let holds = |bit: u16| held.is_some() && pins_today & bit != 0;

        // THE FED PUT'S CLOCK (`fed_put_gain`): the close's log change of
        // total public market cap into the intermeeting return, which the
        // next meeting reads and restarts, and one session's decay of the
        // put's stock. Nothing runs with the dial at 0.0, and no draw at any
        // setting. The first close only records its base.
        if self.params.fed_put_gain != 0.0 {
            let mut mcap = 0.0;
            for c in self.companies.iter() {
                if c.is_public && !c.is_bankrupt {
                    mcap += c.stock.market_cap;
                }
            }
            if self.economy.fed_put_mcap_prev > 0.0 && mcap > 0.0 {
                self.economy.intermeeting_return +=
                    crate::mathx::log(mcap / self.economy.fed_put_mcap_prev);
            }
            self.economy.fed_put_mcap_prev = mcap;
            self.economy.fed_put *= crate::mathx::pow(0.5, 1.0 / self.params.fed_put_half_life);
        }

        // THE DRAWDOWN'S WINDOW (`fed_drawdown_hold`), before tonight's
        // meeting reads it. Nothing with the dial off, and no draw.
        self.book_drawdown_close();

        // THE PRICED PATH'S CLOCK (`treasury_path_pricing`): one session's
        // decay of the market's forecast, before tonight's curve reads it.
        // Nothing runs with the dial at 0.0, and no draw at any setting.
        if self.params.treasury_path_pricing != 0.0 {
            self.policy_path *= crate::mathx::pow(0.5, 1.0 / self.params.treasury_path_half_life);
        }
        // THE UNMET PUT'S CLOCK (`treasury_zlb_path`): one session's decay at
        // the priced path's half-life. Nothing runs with the dial at 0.0.
        if self.params.treasury_zlb_path != 0.0 {
            self.economy.zlb_unmet *= crate::mathx::pow(0.5, 1.0 / self.params.treasury_path_half_life);
        }

        // The DAY's cap-weighted return, in the same percent units as
        // `market_return_pct`. Read only when `vix_return_source` is
        // non-zero, so the shipped path is untouched. `previous_close` is
        // set at the OPEN (market/daily.rs), so this is open to close, and
        // `apply_jumps` has already run, so the jumps are in `price`.
        let market_day_return_pct = if self.params.vix_return_source == 0.0
            && self.params.vix_level_identity == 0.0
            && self.params.flight_to_quality_day == 0.0
            && self.params.corporate_spread_equity_gain == 0.0
            && self.params.cycle_equity_hazard == 0.0
        {
            0.0
        } else {
            let (mut acc, mut mcap) = (0.0, 0.0);
            for c in self.companies.iter() {
                if !c.is_public || c.is_bankrupt || c.stock.previous_close <= 0.0 {
                    continue;
                }
                let d = (c.stock.price - c.stock.previous_close) / c.stock.previous_close * 100.0;
                acc += d * c.stock.market_cap;
                mcap += c.stock.market_cap;
            }
            if mcap > 0.0 { acc / mcap } else { 0.0 }
        };

        // ONCE, and only under the identity: the read-back and the fear
        // excursion's zero-mean correction are two readings of the same
        // variance, and computing it twice would be one loop over the
        // roster too many and one more place for the two to disagree.
        //
        // The terms are KEPT as well as summed. `total()` is the same
        // operations in the same order as the expression that stood here,
        // so the read-back is the number it always was; what is new is
        // that a measurement can afterwards ask which term of `V_t` the
        // update read, at the VIX it read them at. Off the identity
        // neither the sum nor the terms are computed and the field stays
        // at the `None` it was built with -- `params` is fixed for the
        // engine's life, so that branch has nothing to clear -- and
        // nothing here changes WHEN anything is computed.
        let index_variance = if self.params.vix_level_identity == 0.0 {
            0.0
        } else {
            // The fast return memory at its window's mean
            // (`market_vol_leverage_fast`); `_now` itself with that dial off.
            let terms = self.index_conditional_variance_terms_vix();
            self.last_index_variance = Some(terms);
            terms.total()
        };

        // The anchor's slow memory, advanced to today BEFORE the step reads
        // it. Arithmetic on the day's own state, no draw, and not run at all
        // with the dial at 0.0.
        if self.params.vix_anchor_memory != 0.0 && self.params.vix_level_identity != 0.0 {
            let mult = self.vix_level_multiplier();
            let implied = crate::market::index_var::vix_from_variance(
                self.params.vix_variance_premium, index_variance) * mult;
            // Times the cycle's VIX scale, exactly 1.0 with
            // `market_vol_cycle_ratio` at 0.0 or the side's power at 0.0,
            // so the memory reads fear against the phase's normal level.
            let anchor = self.vix_anchor * mult * self.market_vol_cycle_vix_scale();
            // The memory is kept against the CENTRE the weight pulls to;
            // guarded, so at 0.0 it is the anchor exactly.
            let anchor = if self.params.vix_anchor_centre != 0.0 {
                anchor * crate::mathx::exp(-self.params.vix_anchor_centre)
            } else {
                anchor
            };
            if implied > 0.0 && anchor > 0.0 {
                let h = self.params.vix_anchor_memory;
                self.vix_anchor_slow = (1.0 - h) * self.vix_anchor_slow
                    + h * crate::mathx::log(implied / anchor);
            }
            // THE PUBLISHED VIX'S STRESS MEMORY, the same deviation at the
            // same rate, so in a free run it is the anchor's memory to the
            // last bit; a session whose VIX a caller pinned holds it at 0.0,
            // so the quote publishes the pin. Arithmetic only, no draw, and
            // not run at all with `vix_stress_premium` at 0.0.
            if self.params.vix_stress_premium != 0.0 {
                if pins_today & PIN_VIX != 0 {
                    self.vix_stress_memory = 0.0;
                } else if implied > 0.0 && anchor > 0.0 {
                    let h = self.params.vix_stress_memory_rate();
                    self.vix_stress_memory = (1.0 - h) * self.vix_stress_memory
                        + h * crate::mathx::log(implied / anchor);
                }
            }
        }
        rng.site(Site::EconomyDaily, 0);
        let mut inputs =
            self.daily_inputs(request, market_day_return_pct, index_variance, self.vix_anchor_slow,
                              self.market_vol_cycle_vix_scale());
        // THE FEAR MEMORY, advanced to today on the close's own inputs before
        // the step reads it. Arithmetic on the day's state, no draw, and not
        // run at all with `vix_fear_uptake` at 0.0.
        if self.params.vix_fear_uptake != 0.0 {
            self.vix_fear = crate::economy::daily::advance_vix_fear(
                self.vix_fear, &inputs, self.economy.vix, self.params.vix_fear_half_life);
            inputs.vix_fear = self.vix_fear;
        }
        // The priced spread multiplier at the session's start and after
        // tonight's report (`cycle_nowcast_accuracy`,
        // `corporate_spread_cycle`), which `daily_inputs` cannot know: it
        // projects the close before the report.
        if spread_blend {
            inputs.yields.spread_multiplier = Some((spread_before, spread_after_report));
        }
        // The oil price's long factor (`oil_target_drift_sd`): tonight's log
        // change on its own key, so no stream moves; nothing at 0.0.
        let oil_drift_next = self.oil_drift_step(request.game_day);
        if let Some(next) = oil_drift_next {
            inputs.oil_target_drift = Some((self.oil_target_drift, next));
        }
        // CORE INFLATION'S TREND (`core_trend_sd`): one month of its process
        // on its own key at the month's first step, before tonight's
        // inflation step reverts to it; nothing at 0.0.
        if let Some(next) = self.core_trend_step(request.game_day) {
            self.core_trend = next;
        }
        inputs.core_trend = self.core_trend;
        // CREDIT'S OWN STATE (`corporate_spread_noise_sd`): one session of
        // its process on its own key, the pair handed to the close's
        // corporate move; nothing at 0.0.
        if let Some(next) = self.spread_noise_step(request.game_day) {
            inputs.yields.spread_noise = Some((self.spread_noise, next));
            self.spread_noise = next;
        }
        // THE 10-YEAR'S OWN TERM PREMIUM (`treasury_term_premium_sd`):
        // `daily_inputs` handed the step tonight's state, which the session
        // keeps; nothing at 0.0.
        if let Some(next) = inputs.yields.term_premium {
            self.term_premium = next;
        }
        self.economy = update_economy_daily(&self.economy, &inputs, rng);
        // The host's premium on the target (`set_vix_target_premium`), read
        // by the step above, faded for the next close.
        self.fade_vix_target_premium();
        if let Some(next) = oil_drift_next {
            // A bound that truncated tonight's price moved the long-run level
            // with it (`EconomyState::oil_bound_log_ratio`), so the factor
            // does not walk past the bounds while the price is pinned.
            self.oil_target_drift = next + self.economy.oil_bound_log_ratio;
        }
        if let Some(h) = &held {
            let e = &mut self.economy;
            if pins_today & PIN_VIX != 0 { e.vix = h.vix; }
            if pins_today & PIN_POLICY != 0 { e.federal_funds_rate = h.federal_funds_rate; }
            if pins_today & PIN_INFLATION != 0 { e.inflation_rate = h.inflation_rate; }
            if pins_today & PIN_GROWTH != 0 { e.gdp_growth = h.gdp_growth; }
            if pins_today & PIN_UNEMPLOYMENT != 0 { e.unemployment_rate = h.unemployment_rate; }
            if pins_today & PIN_FEAR_GREED != 0 { e.fear_greed_index = h.fear_greed_index; }
            if pins_today & PIN_OIL != 0 { e.oil_price = h.oil_price; }
            if pins_today & PIN_QE_PE != 0 { e.qe_pe_boost = h.qe_pe_boost; }
            if pins_today & PIN_QE_ASSETS != 0 { e.qe_assets_ratio = h.qe_assets_ratio; }
            if pins_today & PIN_TARIFF != 0 { e.tariff_rate = h.tariff_rate; }
        }
        rng.site(Site::EconomyCycle, 0);
        let spec = self.cycle_spec();
        let aged = self.economy.months_in_current_phase;
        self.economy = check_cycle_transition_for(&self.economy, rng, &spec);
        // A pinned phase holds: the roll is taken and its outcome dropped,
        // and the phase keeps ageing.
        if holds(PIN_CYCLE) {
            if let Some(h) = &held {
                self.economy.cycle_phase = h.cycle_phase;
                self.economy.months_in_current_phase = aged;
            }
        }

        // THE AGGREGATE EARNINGS CYCLE, one step a session after the phase
        // has moved: every company's earnings, beyond what nominal output
        // gives them, pulled toward a level set by the cycle phase at a
        // half-life of `earnings_cycle_half_life` sessions. Contraction and
        // trough pull toward `-depth`, every other phase toward
        // `+depth * earnings_cycle_upside`, the share that centres the
        // level over a cycle. `earnings_cycle_sigma` adds a normal on the
        // economy stream when it is non-zero, and only then. Nothing runs at
        // zero depth, so every preset through pt-v19 takes no draw here and
        // leaves the level at 0.0. See `ModelParams::earnings_cycle_depth`.
        if self.params.earnings_cycle_depth != 0.0 {
            let target = self.earnings_cycle_target();
            let p = &self.params;
            let pull = 1.0 - crate::mathx::pow(0.5, 1.0 / p.earnings_cycle_half_life);
            let mut level = self.economy.earnings_cycle
                + pull * (target - self.economy.earnings_cycle);
            if p.earnings_cycle_sigma != 0.0 {
                rng.site(Site::EconomyCycle, 1);
                level += p.earnings_cycle_sigma * rng.next_normal();
            }
            self.economy.earnings_cycle = level;
        }

        // THE VOLATILITY FEEDBACK'S SMOOTHED EXPOSURE, one step a session
        // after the VIX has moved: the log excess of the VIX over the knee,
        // pulled at a half-life of `fair_value_vix_half_life` sessions.
        // Nothing runs unless both the gain and the half-life are set, so
        // every preset through pt-v19 leaves the field at 0.0. It takes no
        // draw at any setting.
        // A pinned VIX's exposure was set by its pin and holds through the
        // close (`pinned_vix_feedback`).
        let exposure_pinned = self.params.pinned_vix_feedback != 0.0 && pins_today & PIN_VIX != 0;
        if self.params.fair_value_vix_discount != 0.0 && self.params.fair_value_vix_half_life != 0.0
            && !exposure_pinned
        {
            let target = crate::market::tick::vix_excess(&self.params, self.economy.vix);
            // A fall back toward a lower target is pulled at
            // `fair_value_vix_release_half_life` when that is set; a branch,
            // so 0.0 is the one half-life both ways, as it stood.
            let half_life = if self.params.fair_value_vix_release_half_life != 0.0
                && target < self.economy.vix_feedback
            {
                self.params.fair_value_vix_release_half_life
            } else {
                self.params.fair_value_vix_half_life
            };
            let pull = 1.0 - crate::mathx::pow(0.5, 1.0 / half_life);
            self.economy.vix_feedback += pull * (target - self.economy.vix_feedback);
        }

        // THE STRESS THE MEETING READS (`fed_stress_cut`): the highest VIX
        // published since the last meeting, this close's included. Nothing
        // runs with the cut off.
        if self.params.fed_stress_cut != 0.0 {
            self.stress_vix_max = crate::mathx::max(self.stress_vix_max, self.economy.vix);
        }
        // THE STRESS HOLD'S CLOCK (`fed_stress_hold`): this close's published
        // VIX restarts it at 0 when it is at or over `fed_stress_vix`, and
        // any other close ages it a session. Nothing runs with the dial off.
        if self.params.fed_stress_hold != 0.0 {
            self.stress_hold_age = if self.published_vix() >= self.params.fed_stress_vix {
                0.0
            } else {
                crate::mathx::min(self.stress_hold_age + 1.0, STRESS_HOLD_NEVER)
            };
        }
        let policy = self.policy_options(holds(PIN_POLICY));
        // THE EVE'S DECISION (`fed_eve_decision`): only a meeting the calendar
        // called delivers it, so this is decided before the put's emergency
        // below can bring the next meeting forward to tonight.
        let scheduled_tonight = request.timestamp >= self.central_bank.next_meeting_date;
        // THE INTERMEETING MEETING (`fed_put_emergency_vix`): a VIX close at
        // or above the dial, with inflation under the put's ceiling and room
        // to cut, brings the next meeting forward to tonight, at least 21
        // sessions after the last and only while the next is not yet due.
        // Real ones: 2001-01-03, 2001-09-17, 2008-01-22, 2008-10-08 and
        // March 2020. The calendar the meeting then sets is the ordinary one.
        // The meeting is an ordinary one: it takes a meeting's economy draws
        // on a session the calendar would not, and re-anchors the corporate
        // yield at tonight's VIX (see `ModelParams::fed_put_emergency_vix`).
        if self.params.fed_put_gain != 0.0
            && self.params.fed_put_emergency_vix != 0.0
            && self.economy.vix >= self.params.fed_put_emergency_vix
            && self.policy_inflation() < crate::economy::central_bank::FED_PUT_INFLATION_CEILING
            && self.economy.federal_funds_rate > 0.0
            && request.timestamp - self.central_bank.last_meeting_date >= FED_PUT_EMERGENCY_GAP_MINUTES
            && request.timestamp < self.central_bank.next_meeting_date
        {
            self.central_bank.next_meeting_date = request.timestamp;
        }
        // The put's unmet ask as the meeting reads it, before it restarts
        // the intermeeting return (`treasury_zlb_path`); 0.0 with it off.
        let unmet_at_meeting = self.zlb_unmet_ask();
        let meeting =
            {
                rng.site(Site::CentralBank, 0);
                let meeting_policy = if self.params.fed_eve_decision != 0.0 && scheduled_tonight {
                    crate::economy::PolicyOptions { eve_change: self.eve_decision, ..policy }
                } else {
                    policy
                };
                update_central_bank_with(
                    &self.central_bank, &self.economy, request.timestamp, rng, &meeting_policy)
            };
        let meeting_held = meeting.decision.is_some();
        if meeting_held && self.params.fed_stress_cut != 0.0 {
            self.stress_vix_max = 0.0;
        }
        // The rate change the market has just seen moves its forecast by its
        // own size (`treasury_path_pricing`).
        if meeting_held && self.params.treasury_path_pricing != 0.0 {
            let owed = if self.params.fed_put_gain != 0.0 {
                meeting.economy.fed_put_owed - self.economy.fed_put_owed
            } else {
                0.0
            };
            // THE OWED STEP LEFT OUT (`treasury_path_owed_excluded`), as the
            // meeting's `path_after` leaves it out.
            self.policy_path += match self.anticipated_path_carry() {
                Some(carry) if self.params.treasury_path_owed_excluded != 0.0 => carry,
                Some(carry) => carry + owed,
                None => meeting.economy.federal_funds_rate - self.economy.federal_funds_rate + owed,
            };
        }
        let decision = meeting.decision;
        let announcement_variant = meeting.announcement_variant;
        self.central_bank = meeting.central_bank;
        self.economy = meeting.economy;
        // A held meeting's unmet ask joins the market's memory of it.
        if meeting_held && self.params.treasury_zlb_path != 0.0 {
            self.economy.zlb_unmet += unmet_at_meeting;
        }
        // The meeting's writes to a pinned field are undone. A held 10-year
        // takes the corporate yield (and the 2-year's formula share) with
        // it, since the meeting set both off the 10-year it re-anchored.
        if let Some(h) = &held {
            let e = &mut self.economy;
            if pins_today & PIN_T10 != 0 && meeting_held {
                let d = h.treasury_yield_10y - e.treasury_yield_10y;
                e.treasury_yield_10y = h.treasury_yield_10y;
                e.corporate_bond_yield += d;
                e.mortgage_rate_30y += d;
                if pins_today & PIN_T2 == 0 {
                    e.treasury_yield_2y += 0.15 * d;
                }
            }
            if pins_today & PIN_T2 != 0 { e.treasury_yield_2y = h.treasury_yield_2y; }
            if pins_today & PIN_POLICY != 0 { e.federal_funds_rate = h.federal_funds_rate; }
            if pins_today & PIN_QE_PE != 0 { e.qe_pe_boost = h.qe_pe_boost; }
            if pins_today & PIN_QE_ASSETS != 0 { e.qe_assets_ratio = h.qe_assets_ratio; }
        }
        // A PINNED CORPORATE YIELD HOLDS THROUGH THE CLOSE, the meeting's
        // re-anchoring to the formula included, on a preset that moves it
        // daily (the mark is kept only there). The close has read today's
        // pins; tomorrow's are their own.
        if pins_today & PIN_CORPORATE != 0 {
            self.economy.corporate_bond_yield = corporate_pinned_at;
        }
        // A PINNED SPREAD HOLDS THROUGH THE CLOSE, the meeting included; the
        // 10-year, as the close left it, moves the level.
        if pins_today & PIN_SPREAD != 0 {
            self.economy.corporate_bond_yield =
                self.economy.treasury_yield_10y + self.pinned_corporate_spread;
        }
        // THE ANTICIPATED MEETING (`policy_anticipation`): what the curve
        // prices of the next decision, re-read on tonight's economy, and the
        // 10-year, the 2-year and the corporate yield moved by the change in
        // what they price. A branch: off, nothing runs, and no draw at any
        // setting.
        if self.params.policy_anticipation != 0.0 {
            self.reprice_anticipated_meeting(request, &policy, meeting_held, pins_today);
        }
        // THE EVE'S DECISION (`fed_eve_decision`): what the Fed's own reading
        // implies tonight, for the next scheduled meeting to deliver. A silent
        // shadow: no draw. Nothing runs with the switch off.
        if self.params.fed_eve_decision != 0.0 {
            self.eve_decision = Some(self.shadow_own_change(request, &policy));
        }
        self.macro_pins_today = 0;
        self.pinned_vix_jump = 0.0;
        // Guarded rather than written, so the dial off writes nothing.
        if self.params.pinned_vix_return != 0.0 {
            self.pinned_vix_move = 0.0;
            self.pinned_vix_mean = 0.0;
        }
        self.advance_anticipation_drift();
        self.refresh_earnings_anticipation();
        // The close's phase into the published history. The burn-in runs
        // this too, and the construction's seeding overwrites what it left.
        self.record_cycle_phase();
        // The close's growth into the published figure's quarter, and any
        // release now due. The burn-in runs this too, and the
        // construction's seeding overwrites what it left.
        self.record_gdp_growth(request.game_day);

        DayAdvanceOutcome {
            phase_changed: self.economy.cycle_phase != phase_before,
            meeting_held,
            decision,
            announcement_variant,
            draws_consumed: 0,
        }
    }

    /// The meeting's options on the state standing: what tonight's meeting
    /// reads in `advance_day_with`, and what the forecast's shadow of the
    /// next meeting reads (`forecast_horizon_sessions`). `hold_rate` is a
    /// pinned policy rate holding through the close.
    fn policy_options(&self, hold_rate: bool) -> crate::economy::PolicyOptions {
        let spread_blend =
            self.params.cycle_nowcast_accuracy != 0.0 || self.params.corporate_spread_cycle != 0.0;
        crate::economy::PolicyOptions {
            calendar: self.macro_calendar(),
            liftoff: self.params.fed_liftoff_rule,
            // The meeting reads the phase after tonight's transition, as it
            // did; under the nowcast that is the belief, which the transition
            // does not move.
            spread_multiplier: if spread_blend {
                Some(self.priced_spread_multiplier(self.economy.cycle_phase))
            } else {
                None
            },
            growth_cut: self.params.fed_growth_cut,
            stress_cut: self.params.fed_stress_cut,
            stress_vix: self.params.fed_stress_vix,
            stress_inflation_gap: self.params.fed_stress_inflation_gap,
            stress_level: self.stress_vix_max,
            hold_rate,
            put_gain: self.params.fed_put_gain,
            put_threshold: self.params.fed_put_threshold,
            put_pricing: self.params.treasury_put_pricing,
            haven_gain: self.params.treasury_haven_gain,
            put_carry: self.params.fed_put_carry,
            stress_hold: self.stress_hold_now(),
            path_gain: self.params.treasury_path_pricing,
            path_before: self.priced_policy_path(),
            rate_damping: self.params.treasury_policy_damping,
            spread_vix_cut: self.params.corporate_spread_vix_cut,
            spread_equity_gain: self.params.corporate_spread_equity_gain,
            spread_priced_gap: self.carries_spread_priced_gap(),
            // The 10-year's lagged equity term (`treasury_equity_lag_gain`).
            treasury_equity_gain: if self.carries_treasury_equity_lag() {
                self.params.treasury_equity_lag_gain
            } else {
                0.0
            },
            spread_priced_gain: if self.carries_spread_priced_gap() {
                self.params.corporate_spread_equity_lag_gain
            } else {
                0.0
            },
            // Credit's own state (`corporate_spread_noise_sd`); `None` off.
            spread_noise: if self.carries_spread_noise() { Some(self.spread_noise) } else { None },
            // The 10-year's own term premium (`treasury_term_premium_sd`);
            // `None` off.
            term_premium: if self.carries_term_premium() { Some(self.term_premium) } else { None },
            // Core in headline's place for the bank and the curve
            // (`fed_core_inflation`, `treasury_core_inflation`); `None` with
            // the switches off.
            policy_inflation: if self.params.fed_core_inflation != 0.0 {
                Some(self.core_inflation())
            } else {
                None
            },
            curve_inflation: if self.params.treasury_core_inflation != 0.0 {
                Some(self.core_inflation())
            } else {
                None
            },
            zlb_path: self.priced_zlb_path(),
            zlb_haven_gain: self.params.treasury_zlb_haven_gain,
            neutral_real_rate: self.params.fed_neutral_real_rate,
            two_year_carry: self.params.treasury_2y_meeting_carry,
            path_carry: self.anticipated_path_carry(),
            eve_change: None,
            path_owed_excluded: self.params.treasury_path_owed_excluded != 0.0,
        }
    }

    /// The part of the Fed put's ask the policy rate cannot deliver tonight,
    /// percentage points (`treasury_zlb_path`): the ask less what the rate
    /// leaves to cut, under the put's inflation gate. 0.0 with the dial off.
    fn zlb_unmet_ask(&self) -> f64 {
        let p = &self.params;
        if p.treasury_zlb_path == 0.0
            || p.fed_put_gain == 0.0
            || self.policy_inflation() >= crate::economy::central_bank::FED_PUT_INFLATION_CEILING
        {
            return 0.0;
        }
        let asked = crate::economy::central_bank::fed_put_ask(
            p.fed_put_gain, p.fed_put_threshold, self.economy.intermeeting_return);
        crate::mathx::max(0.0, asked - crate::mathx::max(0.0, self.economy.federal_funds_rate))
    }

    /// The unmet put the curve prices tonight (`treasury_zlb_path`),
    /// percentage points: `k * treasury_put_pricing * (x + U)`. 0.0 with the
    /// dial off.
    fn priced_zlb_path(&self) -> f64 {
        if self.params.treasury_zlb_path == 0.0 {
            return 0.0;
        }
        self.params.treasury_zlb_path
            * self.params.treasury_put_pricing
            * (self.zlb_unmet_ask() + self.economy.zlb_unmet)
    }

    /// Core inflation: headline less the oil pass-through's accumulated part
    /// (`EconomyState::oil_inflation_level`, 0.0 unless
    /// `fed_core_inflation` or `treasury_core_inflation` is set).
    fn core_inflation(&self) -> f64 {
        self.economy.inflation_rate - self.economy.oil_inflation_level
    }

    /// The inflation the central bank reads: core under
    /// `fed_core_inflation`, headline otherwise.
    fn policy_inflation(&self) -> f64 {
        if self.params.fed_core_inflation != 0.0 {
            self.core_inflation()
        } else {
            self.economy.inflation_rate
        }
    }

    /// Whether this engine's model keeps the oil pass-through's part of
    /// headline (`EconomyState::oil_inflation_level`), which is when the
    /// snapshot and the state hash carry it: `fed_core_inflation` or
    /// `treasury_core_inflation` set. Off on every preset.
    pub fn carries_oil_inflation_level(&self) -> bool {
        self.params.fed_core_inflation != 0.0 || self.params.treasury_core_inflation != 0.0
    }

    /// The daily step's inputs, as the close builds them, with the session's
    /// return, the index variance the identity reads and the anchor's slow
    /// memory supplied: the close passes its own, and the rate indices' live
    /// mark (`rate_intraday_live`) a projection's. The same expressions in
    /// the same order, so the close's inputs are the f64s they were.
    fn daily_inputs<'a>(
        &self,
        request: &DayAdvanceRequest<'a>,
        market_day_return_pct: f64,
        index_variance: f64,
        vix_anchor_slow: f64,
        cycle_vix_scale: f64,
    ) -> DailyInputs<'a> {
        DailyInputs {
            vix_mean_reversion: self.params.vix_mean_reversion,
            vix_decay_ratio: self.params.vix_decay_ratio,
            // The VIX's own slow reversion toward the identity's anchor,
            // and the anchor it reverts to: the derived anchor times the
            // slow regime level's multiplier, which is exactly 1.0 with
            // `vix_level_sigma` at 0.0. The rate ships 0.0 on every
            // preset, where `daily.rs` does not add the term at all, and
            // `ModelParams::invariants` refuses a rate with the identity
            // off, where the anchor is the dial rather than a derived
            // level. See `ModelParams::vix_anchor_reversion`.
            vix_anchor_reversion: self.params.vix_anchor_reversion,
            // Times the cycle's VIX scale (`market_vol_cycle_ratio`), which
            // is exactly 1.0 with the dial at 0.0: the level the VIX reverts
            // to is the phase's normal level. Passed in, because the close
            // reads it after tonight's step and the live mark's projection
            // reads the step it projects.
            vix_anchor_level: self.vix_anchor * self.vix_level_multiplier() * cycle_vix_scale,
            vix_anchor_weight: self.params.vix_anchor_weight,
            vix_anchor_memory: self.params.vix_anchor_memory,
            macro_compound_days_per_year: self.params.macro_compound_days_per_year,
            macro_calendar: self.macro_calendar(),
            cycle_us_calibration: self.params.cycle_us_calibration,
            vix_anchor_centre: self.params.vix_anchor_centre,
            vix_anchor_weight_level: self.params.vix_anchor_weight_level,
            vix_anchor_weight_level_cap: self.params.vix_anchor_weight_level_cap,
            vix_anchor_weight_level_knee: self.params.vix_anchor_weight_level_knee,
            vix_anchor_weight_level_below: self.params.vix_anchor_weight_level_below,
            vix_anchor_weight_level_knee_fixed: self.params.vix_anchor_weight_level_knee_fixed,
            vix_anchor_level_fixed: self.vix_anchor,
            vix_anchor_slow,
            vix_fear_uptake: self.params.vix_fear_uptake,
            vix_fear: self.vix_fear,
            vix_target_premium: self.vix_target_premium,
            vix_target_floor: self.vix_target_floor,
            vix_jump_intensity: self.params.vix_jump_intensity,
            vix_jump_scale: self.params.vix_jump_scale,
            vix_return_level_exponent: self.params.vix_return_level_exponent,
            vix_return_exponent_up: self.params.vix_return_exponent_up,
            vix_return_level_exponent_up: self.params.vix_return_level_exponent_up,
            vix_innovation_sigma: self.params.vix_innovation_sigma,
            vix_innovation_return_sigma: self.params.vix_innovation_return_sigma,
            vix_jump_level_scale: self.params.vix_jump_level_scale,
            vix_jump_return_intensity: self.params.vix_jump_return_intensity,
            vix_return_gain: self.params.vix_return_gain,
            vix_realised_vol_weight: self.params.vix_realised_vol_weight,
            vix_return_source: self.params.vix_return_source,
            vix_cycle_amplitude: self.params.vix_cycle_amplitude,
            market_day_return_pct,
            // WHAT THE MARKET'S OWN VOLATILITY IS WORTH IN VIX POINTS,
            // and it was wrong twice.
            //
            // The expression below the branch is the shipped one: the
            // market FACTOR's conditional sigma, scaled so a factor at
            // its base sigma reads back as the anchor. That is
            // `anchor / market_factor_sigma` = 2105.1 points per unit
            // of daily sigma where the identity -- one point is one per
            // cent annualised -- is `100 * sqrt(252)` = 1587.5; and its
            // referent is the factor, where the quantity a VIX prices
            // is the INDEX's. On the certified roster the index carries
            // 2.05x the factor's variance and none of the remainder
            // ever reached the VIX.
            //
            // At `vix_level_identity` the read-back is the identity
            // itself on the index's own conditional variance. See
            // `ModelParams::vix_level_identity` and
            // `market::index_var`.
            vix_implied_from_market: if self.params.vix_level_identity != 0.0 {
                // The slow VIX level multiplies the identity's target here
                // and nowhere else: a branch at a multiplier of exactly
                // 1.0, so every preset with `vix_level_sigma` 0.0 wires
                // the very value it wired before the level existed.
                let implied = crate::market::index_var::vix_from_variance(
                    self.params.vix_variance_premium,
                    index_variance,
                );
                let mult = self.vix_level_multiplier();
                if mult == 1.0 { implied } else { implied * mult }
            } else if self.params.vix_realised_vol_weight == 0.0 {
                0.0
            } else {
                self.params.market_vol_vix_anchor * self.market_vol.sigma_daily()
                    / self.params.market_factor_sigma
            },
            // The index's conditional daily sigma in PER CENT, which is
            // the units `market_day_return_pct` is in and therefore the
            // units the zero-mean fear correction needs. Zero, and
            // unread, unless the identity is on.
            vix_index_sigma_pct: if self.params.vix_level_identity == 0.0 {
                0.0
            } else {
                100.0 * crate::mathx::sqrt(index_variance)
            },
            vix_level_identity: self.params.vix_level_identity,
            vix_return_gain_up: self.params.vix_return_gain_up,
            vix_return_exponent: self.params.vix_return_exponent,
            vix_return_clamp: self.params.vix_return_clamp,
            vix_target_shock_cap: self.params.vix_target_shock_cap,
            vix_ceiling: self.params.vix_ceiling,
            vix_target_offset: self.params.vix_target_offset,
            inflation_reversion: self.params.inflation_reversion,
            inflation_ceiling: self.params.inflation_ceiling,
            inflation_floor: self.params.inflation_floor,
            crisis_vix_threshold: self.params.crisis_vix_threshold,
            usd_crisis_vix_threshold: self.params.usd_crisis_vix_threshold,
            daily_credit_floor_gain: self.params.daily_credit_floor_gain,
            oil_supply_response: self.params.oil_supply_response,
            oil_opec_symmetry: self.params.oil_opec_symmetry,
            oil_seasonality_target: self.params.oil_seasonality_target,
            trough_growth_floor: self.params.trough_growth_floor,
            phase_target_range_draw: self.params.phase_target_range_draw,
            unemployment_adjustment: self.unemployment_adjustment(),
            unemployment_natural_pull: self.params.unemployment_natural_pull,
            unemployment_okun_coefficient: self.params.unemployment_okun_coefficient,
            unemployment_natural_rate: self.params.unemployment_natural_rate,
            oil_inventory_reversion: self.params.oil_inventory_reversion,
            oil_mean_reversion: self.params.oil_mean_reversion,
            oil_noise_sd: self.params.oil_noise_sd,
            oil_inventory_level_gain: self.params.oil_inventory_level_gain,
            oil_convenience_yield: self.params.oil_convenience_yield,
            oil_convenience_inventory_var: 0.0,
            oil_convenience_inventory_var_before: 0.0,
            oil_target_drift: None,
            oil_growth: None,
            oil_pushes_in_target: self.params.oil_pushes_in_target != 0.0,
            oil_noise_log_sd: self.params.oil_noise_log_sd,
            oil_dollar_elasticity: self.params.oil_dollar_elasticity,
            oil_inventory_noise_sd: self.params.oil_inventory_noise_sd,
            usd_mean_reversion: self.params.usd_mean_reversion,
            usd_noise_sd: self.params.usd_noise_sd,
            usd_safe_haven_gain: self.params.usd_safe_haven_gain,
            oil_inflation_tracked: self.carries_oil_inflation_level(),
            core_trend: self.core_trend,
            oil_price_floor: self.params.oil_price_floor,
            oil_price_ceiling: self.params.oil_price_ceiling,
            oil_inflation_passthrough: self.params.oil_inflation_passthrough,
            // The phase and growth as an observer reads them tonight,
            // before the step: the same moment the economy's own are
            // read at with the switch off.
            fear_greed_published: if self.params.fear_greed_published_inputs != 0.0 {
                Some((self.published_cycle_phase(), self.published_gdp_growth()))
            } else {
                None
            },
            yields: crate::economy::daily::YieldDials {
                treasury_10y_noise: self.params.treasury_10y_noise,
                treasury_2y_noise: self.params.treasury_2y_noise,
                flight_to_quality_gain: self.params.flight_to_quality_gain,
                flight_to_quality_reversal_cut: self.params.flight_to_quality_reversal_cut,
                flight_to_quality_day: self.params.flight_to_quality_day,
                corporate_yield_daily: self.params.corporate_yield_daily,
                vix_pinned: self.macro_pins_today & PIN_VIX != 0,
                corporate_pinned: self.macro_pins_today & PIN_CORPORATE != 0,
                // A pinned 10-year or 2-year holds through the close
                // (`macro_pins_hold`), so the close's step and its projection
                // read the pinned level.
                treasury_10y_pinned: self.params.macro_pins_hold != 0.0
                    && self.macro_pins_today & PIN_T10 != 0,
                treasury_2y_pinned: self.params.macro_pins_hold != 0.0
                    && self.macro_pins_today & PIN_T2 != 0,
                // The multiplier the market prices now, held through the
                // session: a projection of the close runs before tonight's
                // report moves it. `advance_day_with` replaces it with the
                // start and post-report pair. `None` with both dials off.
                spread_multiplier: if self.params.cycle_nowcast_accuracy != 0.0
                    || self.params.corporate_spread_cycle != 0.0
                {
                    let m = self.priced_spread_multiplier(self.economy.cycle_phase);
                    Some((m, m))
                } else {
                    None
                },
                // The Fed put the curve prices tonight and the Treasury
                // haven (`treasury_put_pricing`, `treasury_haven_gain`), both
                // 0.0 unless their dials are set; the live mark's projection
                // reads them as the close does.
                priced_put: self.priced_fed_put(),
                priced_path: self.priced_policy_path(),
                priced_anticipation: self.priced_anticipation(),
                rate_damping: self.params.treasury_policy_damping,
                haven_gain: self.params.treasury_haven_gain,
                core_inflation: self.params.treasury_core_inflation,
                zlb_path: self.priced_zlb_path(),
                zlb_haven_gain: self.params.treasury_zlb_haven_gain,
                // Credit's VIX slope and leverage term
                // (`corporate_spread_vix_cut`, `corporate_spread_equity_gain`),
                // 0.0 unless set; the projection reads them as the close does.
                spread_vix_cut: self.params.corporate_spread_vix_cut,
                spread_equity_gain: self.params.corporate_spread_equity_gain,
                // The gap also runs for the cycle's hazard (`cycle_equity_hazard`).
                spread_equity_decay: if self.carries_spread_equity_gap() {
                    crate::mathx::pow(0.5, 1.0 / self.params.corporate_spread_equity_half_life)
                } else {
                    0.0
                },
                // Credit's repricing lag (`corporate_spread_equity_lag`).
                spread_priced_keep: if self.carries_spread_priced_gap() {
                    crate::mathx::pow(0.5, 1.0 / self.params.corporate_spread_equity_lag)
                } else {
                    0.0
                },
                // Credit's lagged leverage term (`corporate_spread_equity_lag_gain`).
                spread_priced_gain: if self.carries_spread_priced_gap() {
                    self.params.corporate_spread_equity_lag_gain
                } else {
                    0.0
                },
                // The 10-year's repricing lag and its term (`treasury_equity_lag`).
                treasury_equity_keep: if self.carries_treasury_equity_lag() {
                    crate::mathx::pow(0.5, 1.0 / self.params.treasury_equity_lag)
                } else {
                    0.0
                },
                treasury_equity_gain: if self.carries_treasury_equity_lag() {
                    self.params.treasury_equity_lag_gain
                } else {
                    0.0
                },
                // Credit's own state's step (`corporate_spread_noise_sd`) is
                // set by the session's own step, which draws it; a projection
                // of the close leaves it out.
                spread_noise: None,
                // The 10-year's own term premium tonight
                // (`treasury_term_premium_sd`): a function of the state, the
                // key and the day, so the live mark's projection and the
                // close read the same; `None` at 0.0.
                term_premium: self.term_premium_step(request.game_day),
                // The 2-year's expected path (`treasury_2y_expected_path`),
                // on the standing forecast, as the strip reads it; `None`
                // at 0.0.
                two_year_path: self.two_year_expected_path(),
            },
            volatility: request.volatility,
            active_shocks: request.active_shocks,
            market_return_pct: request.market_return_pct,
            game_day: request.game_day,
        }
    }

    // ── Day-chunked stepping ──────────────────────────────────────────────

    /// Run a whole session in one call, writing per-tick output into a
    /// caller-owned buffer.
    ///
    /// # Why this is core rather than a wrapper convenience
    ///
    /// A caller looping over [`Engine::tick`] from Python crosses the FFI
    /// boundary roughly 98,000 times per simulated year — and worse in
    /// practice, because every attribute read on a returned object is another
    /// crossing. No binding layer can fix that from the outside: if the only
    /// advancement primitive is one tick, the loop is in the host language and
    /// the crossings are unavoidable. So day-chunking lives here.
    ///
    /// # Nothing accumulates beyond one day
    ///
    /// Output goes into [`SessionBuffer`], which the caller sizes once and
    /// drains between days. At tick grain a year of 100 names is ~9.8M rows
    /// per column, so accumulating Rust-side and returning at the end does not
    /// scale — one day is ~312 KB and is reused.
    ///
    /// Buffers are `f64` throughout with no `f32` option, deliberately: the
    /// known-answer files and the cross-platform release gate hash these
    /// buffers, so a half-precision path would be a silent parity break
    /// dressed as a performance switch.
    pub fn run_session(
        &mut self,
        request: &SessionRequest,
        buffer: &mut SessionBuffer,
    ) -> SessionOutcome {
        // The row width is every instrument, rate indices after equities, so
        // the tape carries them in the same rows as the names they trade
        // beside. Without rate instruments this is `companies.len()`.
        buffer.resize(request.ticks, self.instrument_count());
        buffer.resize_counterfactual(self.settle_depth_counterfactual);

        if request.reopen {
            self.open_market();
        }

        // Endogenous news is generated at the DAY boundary (`open_market`)
        // and chained onto the caller's events inside `tick_inner`, which is
        // the ONE point every spelling of a minute passes through. It used to
        // be generated and chained here, and that was wrong twice over
        // (§117): a session is a CALL, so `EngineBatch::tick` re-rolled the
        // day's news on every minute, and `Engine::tick` bypasses this
        // function altogether with `news: &[]`, so a tick-driven caller saw
        // no endogenous news at all. pt-v11 was the first preset to turn the
        // mechanism on, so both were invisible until it did.
        let mut draws = 0usize;
        let (mut hour, mut minute) = (request.start.hour, request.start.minute);
        let mut halted_at = None;

        // The first tick's flow: the standing rate plus the fills, once.
        // Built only when there are fills, so a session without them reads
        // `order_volumes` on every tick exactly as it always did.
        //
        // Under `fill_impact_coefficient` the fills are agent fills priced by
        // the linear law instead, so they join the book's pending flow and
        // the first open tick applies them with every other agent's.
        //
        // The rate indices are not in the agent-facing book and the linear
        // law does not price them, so their fills stay on the first tick's
        // flow, where their own books read them, under either law.
        let first_tick_flow = if request.fills.is_empty() {
            None
        } else if self.params.fill_impact_coefficient != 0.0 {
            let (equity, rates): (Vec<_>, Vec<_>) = request
                .fills
                .iter()
                .cloned()
                .partition(|(t, _)| !self.rates.instruments.iter().any(|i| i.spec.ticker == t.as_str()));
            self.queue_external_fills(&equity);
            if rates.is_empty() {
                None
            } else {
                Some(merge_order_volumes(request.order_volumes, &rates))
            }
        } else {
            Some(merge_order_volumes(request.order_volumes, request.fills))
        };

        for t in 0..request.ticks {
            let order_volumes = match (&first_tick_flow, t) {
                (Some(flow), 0) => flow.as_slice(),
                _ => request.order_volumes,
            };
            let outcome = self.tick(&TickRequest {
                time: GameTime {
                    hour,
                    minute,
                    day_of_week: request.start.day_of_week,
                },
                volatility_multiplier: request.volatility_multiplier,
                news: request.news,
                news_impact_queue: request.news_impact_queue,
                order_volumes,
            });
            draws += outcome.draws_consumed;
            buffer.write_tick(
                t,
                &self.companies,
                &TickTruth {
                    components: &self.tick_components,
                    fundamental: &self.tick_fundamental,
                    anchor: &self.tick_anchor,
                    shock: &self.tick_shock,
                    absorbed: &self.tick_absorbed,
                    clamp: &self.tick_clamp,
                    repriced: &self.tick_repriced,
                    unbounded_print: &self.tick_unbounded_print,
                    liquidity_share: &self.tick_liquidity_share,
                },
            );
            if !self.rates.is_empty() {
                buffer.write_rates(t, self.companies.len(), &self.rates);
            }

            if let Some(stop) = &request.stop {
                if stop.triggered(t, &self.companies) {
                    halted_at = Some(t);
                    buffer.ticks_written = t + 1;
                    break;
                }
            }

            minute += 1;
            if minute >= 60 {
                minute = 0;
                hour += 1;
            }
        }

        if request.close_at_end && halted_at.is_none() {
            // The engine's OWN accumulated noise, where the caller left the
            // slot empty. It cannot be the caller's job to fill it here: the
            // request is built BEFORE the session runs, and the value being
            // asked for is the noise this session is about to accumulate.
            //
            // So on this path an absent innovation is not a choice, it is the
            // only thing an embedder can supply -- and `None` falls through to
            // the day's total return, which is a different quantity (measured
            // elsewhere: median factor 0.82, tenth percentile 0.22, ninetieth
            // 3.20). A parameter that cannot be supplied correctly is a trap,
            // not a feature.
            let own = self.daily_innovation_column();
            let innovations: Vec<Option<f64>> = request
                .daily_innovations
                .iter()
                .enumerate()
                .map(|(i, supplied)| supplied.or_else(|| own.get(i).copied()))
                .collect();
            self.close_market(&DayCloseRequest {
                daily_innovations: &innovations,
                sector_base_variances: request.sector_base_variances,
                avg_volume: AvgVolumePolicy::default(),
            });
        }

        SessionOutcome {
            draws_consumed: draws,
            halted_at,
        }
    }

    // ── The day loop ──────────────────────────────────────────────────────
    //
    // This lived in `python_engine.rs` until the WebAssembly binding needed
    // it too. That is the whole argument for moving it: the crate's own
    // header says the price model is "compiled once and consumed twice",
    // and a day loop implemented separately in each binding is a fork of
    // the model wearing the costume of glue code. The divergence would be
    // invisible until a whole simulated market had drifted apart -- which
    // is exactly the failure this crate was written to end.
    //
    // A binding still owns its own day COUNTER, because counting is not a
    // modelling decision. Everything below is.

    /// Each company's sector base daily variance, in roster order.
    ///
    /// The fallback matches the reference implementation's default for a
    /// sector it does not recognise; a roster is validated on construction,
    /// so it is unreachable in practice and present so this cannot panic on
    /// a surface that has no way to report the error.
    pub fn sector_base_variances(&self) -> Vec<f64> {
        self.companies
            .iter()
            .map(|c| {
                crate::sectors::by_key(&c.sector)
                    .map(|s| s.base_daily_variance())
                    .unwrap_or(0.000225)
            })
            .collect()
    }

    /// Close the trading day: settle the day, then step the macro chain.
    ///
    /// `game_day` is the day being closed, one-based. The caller keeps the
    /// count; the arithmetic on it is here so both bindings agree about
    /// what a day means.
    ///
    /// Daily innovations come from the engine's OWN accumulated noise
    /// rather than from the caller. A second copy of engine state on the
    /// embedder's side is a divergence waiting for the first day the two
    /// disagree, and the caller cannot supply it correctly in any case --
    /// the value being asked for is the noise the session just accumulated.
    /// Read/write the forced-flow segment's spent budget, for checkpoints.
    pub fn market_vol_log_level(&self) -> f64 {
        self.market_vol_log_level
    }

    pub fn vix_log_level(&self) -> f64 {
        self.vix_log_level
    }

    pub fn set_vix_log_level(&mut self, level: f64) {
        self.vix_log_level = level;
    }

    pub fn vix_anchor_slow(&self) -> f64 {
        self.vix_anchor_slow
    }

    pub fn set_vix_anchor_slow(&mut self, value: f64) {
        self.vix_anchor_slow = value;
    }

    /// Sets the anchor a snapshot carries (`vix_level_identity`), in place
    /// of the one this engine derived from the roster it was built on.
    pub fn set_vix_anchor(&mut self, value: f64) {
        self.vix_anchor = value;
    }

    /// The oil long factor's next log level, or `None` with
    /// `oil_target_drift_sd` at 0.0: `(1 - r) D - sigma^2 / 2 + sigma z`,
    /// `z` the first normal of `GameRng::keyed(key, OIL_DRIFT_TAG, day)`.
    fn oil_drift_step(&self, game_day: i64) -> Option<f64> {
        let sigma = self.params.oil_target_drift_sd;
        if sigma == 0.0 {
            return None;
        }
        let z = crate::rng::GameRng::keyed(
            self.oil_drift_key, crate::rng::OIL_DRIFT_TAG as u64, game_day as u64).next_normal();
        let pull = 1.0 - self.params.oil_target_drift_reversion;
        Some(pull * self.oil_target_drift - 0.5 * sigma * sigma + sigma * z)
    }

    /// The oil long factor's state for a snapshot and the state hash,
    /// `[log level, key high word, key low word]` (each word exact in an
    /// f64), or `None` with `oil_target_drift_sd` at 0.0.
    pub fn oil_drift_words(&self) -> Option<[f64; 3]> {
        if self.params.oil_target_drift_sd == 0.0 {
            return None;
        }
        Some([self.oil_target_drift, (self.oil_drift_key >> 32) as f64,
              (self.oil_drift_key & 0xFFFF_FFFF) as f64])
    }

    /// Core inflation's trend opening: the stationary sd `sigma / sqrt(1 -
    /// phi^2)` times the first normal of `GameRng::keyed(key,
    /// CORE_TREND_TAG, u64::MAX)`; 0.0 with `core_trend_sd` at 0.0 or `phi`
    /// at 1.
    fn core_trend_opening(&self) -> f64 {
        let sigma = self.params.core_trend_sd;
        let phi = self.params.core_trend_persistence;
        if sigma == 0.0 || phi >= 1.0 {
            return 0.0;
        }
        let z = crate::rng::GameRng::keyed(
            self.core_trend_key, crate::rng::CORE_TREND_TAG as u64, u64::MAX).next_normal();
        sigma / (1.0 - phi * phi).sqrt() * z
    }

    /// Core inflation's trend next month, or `None` with `core_trend_sd` at
    /// 0.0 or off a month's first step (`game_day % days_per_month != 0`, the
    /// economy's own month start): `phi x + sigma z`, `z` the first normal of
    /// `GameRng::keyed(key, CORE_TREND_TAG, game_day / days_per_month)`.
    fn core_trend_step(&self, game_day: i64) -> Option<f64> {
        let sigma = self.params.core_trend_sd;
        if sigma == 0.0 {
            return None;
        }
        let month = self.macro_calendar().days_per_month;
        if game_day % month != 0 {
            return None;
        }
        let z = crate::rng::GameRng::keyed(
            self.core_trend_key, crate::rng::CORE_TREND_TAG as u64, (game_day / month) as u64).next_normal();
        Some(self.params.core_trend_persistence * self.core_trend + sigma * z)
    }

    /// Core inflation's trend for a snapshot and the state hash, `[trend,
    /// key high word, key low word]`, or `None` with `core_trend_sd` at 0.0.
    pub fn core_trend_words(&self) -> Option<[f64; 3]> {
        if self.params.core_trend_sd == 0.0 {
            return None;
        }
        Some([self.core_trend, (self.core_trend_key >> 32) as f64, (self.core_trend_key & 0xFFFF_FFFF) as f64])
    }

    /// Core inflation's current trend over the target (`core_trend_sd`);
    /// 0.0 off.
    pub fn core_trend_level(&self) -> f64 {
        self.core_trend
    }

    /// Whether this engine's model carries credit's own state
    /// (`corporate_spread_noise_sd` set), which is when the snapshot and the
    /// state hash carry it. Off on every preset.
    pub fn carries_spread_noise(&self) -> bool {
        self.params.corporate_spread_noise_sd != 0.0
    }

    /// Credit's own state's one-session keep, `0.5^(1/H)`, H
    /// `corporate_spread_noise_half_life`; 0.0 (no persistence) at H 0.0.
    fn spread_noise_keep(&self) -> f64 {
        let h = self.params.corporate_spread_noise_half_life;
        if h == 0.0 { 0.0 } else { crate::mathx::pow(0.5, 1.0 / h) }
    }

    /// Credit's own state's opening: the stationary sd `sigma / sqrt(1 -
    /// phi^2)` times the first normal of `GameRng::keyed(key,
    /// SPREAD_NOISE_TAG << 32, 0)`, a word no session's draw uses; 0.0 with
    /// `corporate_spread_noise_sd` at 0.0.
    fn spread_noise_opening(&self) -> f64 {
        if !self.carries_spread_noise() {
            return 0.0;
        }
        let sigma = self.params.corporate_spread_noise_sd;
        let phi = self.spread_noise_keep();
        let z = crate::rng::GameRng::keyed(
            self.spread_noise_key, (crate::rng::SPREAD_NOISE_TAG as u64) << 32, 0).next_normal();
        sigma / (1.0 - phi * phi).sqrt() * z
    }

    /// Credit's own state after tonight's session, or `None` with
    /// `corporate_spread_noise_sd` at 0.0: `phi x + sigma z`, `z` the first
    /// normal of `GameRng::keyed(key, SPREAD_NOISE_TAG, game_day)`.
    fn spread_noise_step(&self, game_day: i64) -> Option<f64> {
        if !self.carries_spread_noise() {
            return None;
        }
        let z = crate::rng::GameRng::keyed(
            self.spread_noise_key, crate::rng::SPREAD_NOISE_TAG as u64, game_day as u64).next_normal();
        Some(self.spread_noise_keep() * self.spread_noise + self.params.corporate_spread_noise_sd * z)
    }

    /// Credit's own state for a snapshot and the state hash, `[x, key high
    /// word, key low word]`, or `None` with `corporate_spread_noise_sd` at 0.0.
    pub fn spread_noise_words(&self) -> Option<[f64; 3]> {
        if !self.carries_spread_noise() {
            return None;
        }
        Some([self.spread_noise, (self.spread_noise_key >> 32) as f64, (self.spread_noise_key & 0xFFFF_FFFF) as f64])
    }

    /// Credit's own state now, points of the spread formula's base
    /// (`corporate_spread_noise_sd`); 0.0 off.
    pub fn spread_noise_level(&self) -> f64 {
        self.spread_noise
    }

    /// Put credit's own state back from [`Engine::spread_noise_words`].
    pub fn set_spread_noise_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.carries_spread_noise()) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                let whole = |v: f64| v.is_finite() && v >= 0.0 && v <= u32::MAX as f64 && v.fract() == 0.0;
                if w.len() != 3 || !w[0].is_finite() || !whole(w[1]) || !whole(w[2]) {
                    return Err(format!(
                        "this snapshot's spread_noise is {w:?}: it is credit's own state and its \
                         key's two 32-bit words"));
                }
                self.spread_noise = w[0];
                self.spread_noise_key = ((w[1] as u64) << 32) | (w[2] as u64);
                Ok(())
            }
            (None, true) => Err(
                "this model sets corporate_spread_noise_sd and the snapshot carries no spread_noise".to_string()),
            (Some(_), false) => Err(
                "this snapshot carries spread_noise and this model's corporate_spread_noise_sd is 0".to_string()),
        }
    }

    /// Whether this engine's model carries the 10-year's own term premium
    /// (`treasury_term_premium_sd` set), which is when the snapshot and the
    /// state hash carry it. Off on every preset.
    pub fn carries_term_premium(&self) -> bool {
        self.params.treasury_term_premium_sd != 0.0
    }

    /// The term premium's one-session keep, `0.5^(1/H)`, H
    /// `treasury_term_premium_half_life`; 0.0 (no persistence) at H 0.0.
    fn term_premium_keep(&self) -> f64 {
        let h = self.params.treasury_term_premium_half_life;
        if h == 0.0 { 0.0 } else { crate::mathx::pow(0.5, 1.0 / h) }
    }

    /// The term premium's opening: the stationary sd `sigma / sqrt(1 -
    /// phi^2)` times the first normal of `GameRng::keyed(key,
    /// TERM_PREMIUM_TAG << 32, 0)`, a word no session's draw uses; 0.0 with
    /// `treasury_term_premium_sd` at 0.0.
    fn term_premium_opening(&self) -> f64 {
        if !self.carries_term_premium() {
            return 0.0;
        }
        let sigma = self.params.treasury_term_premium_sd;
        let phi = self.term_premium_keep();
        let z = crate::rng::GameRng::keyed(
            self.term_premium_key, (crate::rng::TERM_PREMIUM_TAG as u64) << 32, 0).next_normal();
        sigma / (1.0 - phi * phi).sqrt() * z
    }

    /// The term premium after the session `game_day`, or `None` with
    /// `treasury_term_premium_sd` at 0.0: `phi x + sigma z`, `z` the first
    /// normal of `GameRng::keyed(key, TERM_PREMIUM_TAG, day)`. The economy's
    /// burn-in numbers its days from 1 as the run does, so its days take the
    /// index `day | 1 << 40`, which no session reaches, and the run does not
    /// replay the burn-in's draws.
    /// The 2-year's expected path (`treasury_2y_expected_path`): the dial
    /// and the mean, over the 504 closes from the standing forecast's next,
    /// of the policy rate it expects each to set, past its horizon its last
    /// rate and without a forecast the policy rate now, as the rate futures
    /// price their closes to come. `None` with the dial at 0.0. No draw.
    fn two_year_expected_path(&self) -> Option<(f64, f64)> {
        let d = self.params.treasury_2y_expected_path;
        if d == 0.0 {
            return None;
        }
        const CLOSES: usize = 504;
        let r0 = self.economy.federal_funds_rate;
        let path = match self.forecast() {
            Some(f) if !f.policy_rate.is_empty() => {
                let n = if f.policy_rate.len() < CLOSES { f.policy_rate.len() } else { CLOSES };
                let last = f.policy_rate[f.policy_rate.len() - 1];
                let mut sum = 0.0;
                for r in &f.policy_rate[..n] {
                    sum += *r;
                }
                (sum + last * (CLOSES - n) as f64) / CLOSES as f64
            }
            _ => r0,
        };
        Some((d, path))
    }

    fn term_premium_step(&self, game_day: i64) -> Option<f64> {
        if !self.carries_term_premium() {
            return None;
        }
        let index = if self.economy_alone { game_day as u64 | 1 << 40 } else { game_day as u64 };
        let z = crate::rng::GameRng::keyed(
            self.term_premium_key, crate::rng::TERM_PREMIUM_TAG as u64, index).next_normal();
        Some(self.term_premium_keep() * self.term_premium + self.params.treasury_term_premium_sd * z)
    }

    /// The term premium for a snapshot and the state hash, `[x, key high
    /// word, key low word]`, or `None` with `treasury_term_premium_sd` at 0.0.
    pub fn term_premium_words(&self) -> Option<[f64; 3]> {
        if !self.carries_term_premium() {
            return None;
        }
        Some([self.term_premium, (self.term_premium_key >> 32) as f64, (self.term_premium_key & 0xFFFF_FFFF) as f64])
    }

    /// The 10-year's own term premium now, percentage points
    /// (`treasury_term_premium_sd`); 0.0 off.
    pub fn term_premium_level(&self) -> f64 {
        self.term_premium
    }

    /// Put the term premium back from [`Engine::term_premium_words`].
    pub fn set_term_premium_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.carries_term_premium()) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                let whole = |v: f64| v.is_finite() && v >= 0.0 && v <= u32::MAX as f64 && v.fract() == 0.0;
                if w.len() != 3 || !w[0].is_finite() || !whole(w[1]) || !whole(w[2]) {
                    return Err(format!(
                        "this snapshot's term_premium is {w:?}: it is the 10-year's own term premium \
                         and its key's two 32-bit words"));
                }
                self.term_premium = w[0];
                self.term_premium_key = ((w[1] as u64) << 32) | (w[2] as u64);
                Ok(())
            }
            (None, true) => Err(
                "this model sets treasury_term_premium_sd and the snapshot carries no term_premium".to_string()),
            (Some(_), false) => Err(
                "this snapshot carries term_premium and this model's treasury_term_premium_sd is 0".to_string()),
        }
    }

    /// Put core inflation's trend back from [`Engine::core_trend_words`].
    pub fn set_core_trend_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.params.core_trend_sd != 0.0) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                let whole = |v: f64| v.is_finite() && v >= 0.0 && v <= u32::MAX as f64 && v.fract() == 0.0;
                if w.len() != 3 || !w[0].is_finite() || !whole(w[1]) || !whole(w[2]) {
                    return Err(format!(
                        "this snapshot's core_trend is {w:?}: it is core inflation's trend and its \
                         key's two 32-bit words"));
                }
                self.core_trend = w[0];
                self.core_trend_key = ((w[1] as u64) << 32) | (w[2] as u64);
                Ok(())
            }
            (None, true) => Err("this model sets core_trend_sd and the snapshot carries no core_trend".to_string()),
            (Some(_), false) => Err("this snapshot carries core_trend and this model's core_trend_sd is 0".to_string()),
        }
    }

    /// The eve's decision for a snapshot and the state hash, `[present,
    /// change]`, or `None` with `fed_eve_decision` at 0.0.
    pub fn eve_decision_words(&self) -> Option<[f64; 2]> {
        if self.params.fed_eve_decision == 0.0 {
            return None;
        }
        Some(match self.eve_decision {
            Some(c) => [1.0, c],
            None => [0.0, 0.0],
        })
    }

    /// Put the eve's decision back from [`Engine::eve_decision_words`].
    pub fn set_eve_decision_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.params.fed_eve_decision != 0.0) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                if w.len() != 2 || !(w[0] == 0.0 || w[0] == 1.0) || !w[1].is_finite() {
                    return Err(format!(
                        "this snapshot's eve_decision is {w:?}: it is a presence flag and the eve's \
                         decision"));
                }
                self.eve_decision = if w[0] == 1.0 { Some(w[1]) } else { None };
                Ok(())
            }
            (None, true) => Err("this model sets fed_eve_decision and the snapshot carries no eve_decision".to_string()),
            (Some(_), false) => Err("this snapshot carries eve_decision and this model's fed_eve_decision is 0".to_string()),
        }
    }

    /// Put the oil long factor back from [`Engine::oil_drift_words`].
    pub fn set_oil_drift_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        match (words, self.params.oil_target_drift_sd != 0.0) {
            (None, false) => Ok(()),
            (Some(w), true) => {
                let whole = |v: f64| v.is_finite() && v >= 0.0 && v <= u32::MAX as f64 && v.fract() == 0.0;
                if w.len() != 3 || !w[0].is_finite() || !whole(w[1]) || !whole(w[2]) {
                    return Err(format!(
                        "this snapshot's oil_target_drift is {w:?}: it is the long factor's log \
                         level and its key's two 32-bit words"));
                }
                self.oil_target_drift = w[0];
                self.oil_drift_key = ((w[1] as u64) << 32) | (w[2] as u64);
                Ok(())
            }
            (None, true) => Err("this model runs oil's long factor (oil_target_drift_sd) and \
                                 the snapshot carries no oil_target_drift".to_string()),
            (Some(_), false) => Err("this snapshot carries oil_target_drift and this model's \
                                      oil_target_drift_sd is 0".to_string()),
        }
    }

    /// Whether this engine's model carries the published VIX's stress
    /// memory, which is when the snapshot and the state hash carry it: only
    /// with `vix_stress_premium` non-zero, which no preset sets.
    pub fn carries_vix_stress_memory(&self) -> bool {
        self.params.vix_stress_premium != 0.0
    }

    pub fn vix_stress_memory(&self) -> f64 {
        self.vix_stress_memory
    }

    pub fn set_vix_stress_memory(&mut self, value: f64) {
        self.vix_stress_memory = value;
    }

    /// Whether this engine's model carries the VIX's fear memory, which is
    /// when the snapshot and the state hash carry it: only with
    /// `vix_fear_uptake` non-zero, which no preset sets.
    pub fn carries_vix_fear(&self) -> bool {
        self.params.vix_fear_uptake != 0.0
    }

    /// The VIX's fear memory in log units (`vix_fear_uptake`); 0.0 with the
    /// switch off.
    pub fn vix_fear(&self) -> f64 {
        self.vix_fear
    }

    pub fn set_vix_fear(&mut self, value: f64) {
        self.vix_fear = value;
    }

    /// Whether this engine's model keeps the published VIX's log average
    /// (`forecast_vix_level_nowcast`), which is when the snapshot and the
    /// state hash carry it.
    pub fn carries_vix_log_average(&self) -> bool {
        self.params.forecast_vix_level_nowcast != 0.0
    }

    /// The published VIX's log average (`forecast_vix_level_nowcast`); 0.0
    /// with the dial off or before the first close.
    pub fn vix_log_average(&self) -> f64 {
        self.vix_log_average
    }

    pub fn set_vix_log_average(&mut self, value: f64) {
        self.vix_log_average = value;
    }

    /// A close's step of the published VIX's log average: from
    /// `forecast_vix_level_nowcast_centre` at the first close, then toward
    /// the log of the VIX the close published at the dial's half-life.
    fn advance_vix_log_average(&mut self) {
        let p = &self.params;
        if p.forecast_vix_level_nowcast == 0.0 {
            return;
        }
        let published = self.published_vix();
        if !(published > 0.0) {
            return;
        }
        let from = if self.vix_log_average == 0.0 { p.forecast_vix_level_nowcast_centre } else { self.vix_log_average };
        let alpha = if p.forecast_vix_level_nowcast_half_life == 0.0 {
            1.0
        } else {
            1.0 - crate::mathx::pow(0.5, 1.0 / p.forecast_vix_level_nowcast_half_life)
        };
        self.vix_log_average = from + alpha * (crate::mathx::log(published) - from);
    }

    /// The VIX's slow level as the published VIX's log average nowcasts it,
    /// `g (x - c)` (`forecast_vix_level_nowcast`); 0.0 with the dial off or
    /// before the first close.
    pub(crate) fn vix_level_nowcast(&self) -> f64 {
        let p = &self.params;
        if p.forecast_vix_level_nowcast == 0.0 || self.vix_log_average == 0.0 {
            return 0.0;
        }
        p.forecast_vix_level_nowcast * (self.vix_log_average - p.forecast_vix_level_nowcast_centre)
    }

    /// Put a premium on the VIX target, in VIX points, that fades at
    /// `half_life_sessions` (0.0 holds it until it is written again).
    ///
    /// For fear the macro model does not carry: a bankruptcy's contagion, a
    /// geopolitical escalation. The premium joins the target beside the
    /// inflation and shock terms, inside `vix_target_shock_cap`, so the
    /// engine's own reversion carries the VIX up to it and back down as it
    /// fades, where a one-off write of the VIX itself is reverted to the
    /// engine's target within days. Each close reads the premium and then
    /// fades it by `0.5^(1 / half_life)`; one under a millionth of a point
    /// is cleared to 0.0.
    ///
    /// This REPLACES the premium standing. A host adding an event to one
    /// still fading reads [`Engine::vix_target_premium`] and writes the sum,
    /// at whichever half-life it wants the whole to fade on.
    ///
    /// Consumes no draws. With nothing written the target is bit for bit
    /// the one the engine computes.
    pub fn set_vix_target_premium(&mut self, points: f64, half_life_sessions: f64) -> Result<(), String> {
        if !points.is_finite() {
            return Err(format!("the VIX target premium must be finite, got {points}"));
        }
        if !(half_life_sessions.is_finite() && half_life_sessions >= 0.0) {
            return Err(format!(
                "the VIX target premium's half-life must be 0.0 (held) or a finite number of \
                 sessions above 0, got {half_life_sessions}"
            ));
        }
        if points == 0.0 {
            self.vix_target_premium = 0.0;
            self.vix_target_premium_half_life = 0.0;
        } else {
            self.vix_target_premium = points;
            self.vix_target_premium_half_life = half_life_sessions;
        }
        self.after_vix_target_write();
        Ok(())
    }

    /// A host wrote the VIX target's premium or floor: what reads the next
    /// close's target is re-read now, as after a pin. The live VIX's
    /// projection is dropped for the next tick to recompute, the forecast
    /// re-read (`forecast_horizon_sessions`), and between sessions the
    /// night's path moved (`night_session_steps`). Nothing with those off.
    fn after_vix_target_write(&mut self) {
        self.vix_live = None;
        self.refresh_forecast(None);
        self.futures_retarget();
    }

    /// The host's premium on the VIX target in points, and the half-life it
    /// fades at: `(0.0, 0.0)` with none standing.
    pub fn vix_target_premium(&self) -> (f64, f64) {
        (self.vix_target_premium, self.vix_target_premium_half_life)
    }

    /// Put a floor under the VIX target, or clear it with `None`.
    ///
    /// For a period in which the host's world holds fear at a level, an
    /// election campaign for one: each close's target is at least the
    /// floor, so the VIX reverts toward it at the engine's own rate, and its
    /// noise, jumps and ceiling still apply. The floor holds until it is
    /// cleared. Consumes no draws.
    pub fn set_vix_target_floor(&mut self, floor: Option<f64>) -> Result<(), String> {
        if let Some(f) = floor {
            if !(f.is_finite() && f > 0.0) {
                return Err(format!("the VIX target floor must be finite and above 0, got {f}"));
            }
        }
        self.vix_target_floor = floor;
        self.after_vix_target_write();
        Ok(())
    }

    /// The host's floor under the VIX target, `None` with none set.
    pub fn vix_target_floor(&self) -> Option<f64> {
        self.vix_target_floor
    }

    /// Put the host's input to the VIX target back as a snapshot carries it
    /// (checked by the restore), touching nothing the setters re-read: the
    /// forecast and the live VIX are the snapshot's own.
    pub(crate) fn restore_vix_target_input(&mut self, premium: (f64, f64), floor: Option<f64>) {
        (self.vix_target_premium, self.vix_target_premium_half_life) = premium;
        self.vix_target_floor = floor;
    }

    /// Whether the host's VIX target premium is standing, which is when the
    /// snapshot and the state hash carry it.
    pub fn carries_vix_target_premium(&self) -> bool {
        self.vix_target_premium != 0.0
    }

    /// The premium one close on: faded at its half-life, cleared under a
    /// millionth of a point. Nothing with none standing, or one held.
    fn fade_vix_target_premium(&mut self) {
        let h = self.vix_target_premium_half_life;
        if self.vix_target_premium == 0.0 || h == 0.0 {
            return;
        }
        let faded = self.vix_target_premium * crate::mathx::pow(0.5, 1.0 / h);
        if faded.abs() < VIX_TARGET_PREMIUM_CLEARED_UNDER {
            self.vix_target_premium = 0.0;
            self.vix_target_premium_half_life = 0.0;
        } else {
            self.vix_target_premium = faded;
        }
    }

    /// A caller wrote the VIX: the stress memory restarts from 0.0, so the
    /// quote publishes the pin now and through tonight's close (which keeps
    /// it at 0.0 on a pinned session). Nothing with `vix_stress_premium` at
    /// 0.0.
    pub fn note_vix_pinned(&mut self) {
        if self.params.vix_stress_premium != 0.0 {
            self.vix_stress_memory = 0.0;
        }
    }

    /// The stress premium the published VIX carries over the state, in log
    /// units: `cap * (1 - exp(-g * max(0, m - knee) / cap))` on the stress
    /// memory `m`. Exactly 0.0 with `vix_stress_premium` at 0.0 or the
    /// memory at or below the knee. See `ModelParams::vix_stress_premium`.
    pub fn vix_stress_premium_now(&self) -> f64 {
        self.vix_stress_premium_at(self.vix_stress_memory)
    }

    /// [`Engine::vix_stress_premium_now`] on a stress memory `memory`, for
    /// a projection of tonight's quote (`vix_intraday_live`).
    fn vix_stress_premium_at(&self, memory: f64) -> f64 {
        let p = &self.params;
        if p.vix_stress_premium == 0.0 {
            return 0.0;
        }
        let excess = memory - p.vix_stress_premium_knee;
        if !(excess > 0.0) {
            return 0.0;
        }
        // The anchor's pull undone above the knee (`vix_stress_premium_undo`);
        // guarded, so with the switch at 0.0 this is the capped hinge exactly.
        if p.vix_stress_premium_undo != 0.0 {
            return p.vix_anchor_weight * excess;
        }
        let cap = p.vix_stress_premium_cap;
        cap * (1.0 - crate::mathx::exp(-p.vix_stress_premium * excess / cap))
    }

    /// THE VIX AS PUBLISHED: what `macro_fields`, `macro_state`, the macro
    /// table and its Arrow batch, and the wasm getter report. The engine's
    /// VIX state (`economy().vix`, `state_snapshot()["economy"]["vix"]`),
    /// which every internal reader reads, times `exp(premium)` under
    /// `vix_stress_premium` and never above `vix_ceiling`; the state itself,
    /// bit for bit, with the dial at 0.0, which every preset carries.
    pub fn published_vix(&self) -> f64 {
        self.published_vix_at(self.economy.vix, self.vix_stress_memory)
    }

    /// The quote [`Engine::published_vix`] makes of a VIX state `vix` with
    /// the stress memory at `memory`: the same expressions, for a projection
    /// of tonight's quote (`vix_intraday_live`) and the forecast.
    pub(crate) fn published_vix_at(&self, vix: f64, memory: f64) -> f64 {
        let premium = self.vix_stress_premium_at(memory);
        if !(premium > 0.0) {
            return vix;
        }
        let quote = vix * crate::mathx::exp(premium);
        if quote > self.params.vix_ceiling {
            crate::mathx::max(self.params.vix_ceiling, vix)
        } else {
            quote
        }
    }

    /// The VIX level's per-session innovation AS APPLIED, after the
    /// variance loop's own transmission has been divided out.
    ///
    /// `vix_level_sigma` is the spread of the tape's yearly medians of log
    /// VIX, which is a spread of the VIX and not of the latent multiplier
    /// the engine holds; the loop carries the multiplier to the VIX with a
    /// gain, so the dialled figure has to be divided by that gain before
    /// the level is driven with it. See
    /// `ModelParams::vix_level_loop_gain`, which is 0.0 on every preset
    /// through pt-v18 and returns the dial's own f64 here with no arithmetic
    /// run at all.
    fn vix_level_sigma_applied(&self) -> f64 {
        if self.params.vix_level_loop_gain == 0.0 {
            self.params.vix_level_sigma
        } else {
            self.params.vix_level_sigma / self.params.vix_level_loop_gain
        }
    }

    /// The phase's target for the cycle multiplier, in logs: `ln(k_e)`
    /// outside a contraction or a trough and `ln(R k_e)` in one, read on
    /// the TRUE phase. `k_e` is `market_vol_cycle_expansion`, or at 0.0 the
    /// value that leaves the stationary share-weighted factor variance
    /// unchanged: `1 / sqrt(1 - s + R^2 s)`, `s` the contraction-and-trough
    /// share of the cycle's days. See `ModelParams::market_vol_cycle_ratio`.
    pub fn market_vol_cycle_target_log(&self) -> f64 {
        self.market_vol_cycle_target_log_for(self.economy.cycle_phase)
    }

    /// [`Engine::market_vol_cycle_target_log`] in `phase`: the same
    /// arithmetic, for the forecast, which weighs each phase by the market's
    /// belief rather than reading the true one.
    pub(crate) fn market_vol_cycle_target_log_for(&self, phase: crate::economy::CyclePhase) -> f64 {
        use crate::economy::CyclePhase;
        let r = self.params.market_vol_cycle_ratio;
        let ke = if self.params.market_vol_cycle_expansion != 0.0 {
            self.params.market_vol_cycle_expansion
        } else {
            let (mean, cycle) =
                crate::economy::cycle::stationary_phase_shares_for(&self.cycle_spec());
            let mut s = 0.0;
            for (k, phase) in crate::economy::cycle::phase_cycle().iter().enumerate() {
                if matches!(phase, CyclePhase::Contraction | CyclePhase::Trough) {
                    s += mean[k] / cycle;
                }
            }
            1.0 / crate::mathx::sqrt(1.0 - s + r * r * s)
        };
        // THE RALLY OFF THE LOW (`market_vol_cycle_recovery_release`): in a
        // contraction or a trough the excess is scaled by `1 - g s`, `s`
        // the index's rise off its low over the drawdown's window. A branch
        // at 0.0, every preset, that reads nothing.
        let g_rally = self.params.market_vol_cycle_recovery_release;
        if g_rally != 0.0
            && matches!(phase, CyclePhase::Contraction | CyclePhase::Trough)
        {
            let share = match phase {
                CyclePhase::Trough => 1.0 - self.params.market_vol_cycle_trough_release,
                _ => 1.0,
            };
            let s = crate::mathx::min(
                1.0, self.index_rally_off_low() / self.params.market_vol_cycle_recovery_scale);
            return crate::mathx::log(ke) + share * (1.0 - g_rally * s) * crate::mathx::log(r);
        }
        let k = match phase {
            CyclePhase::Contraction => r * ke,
            // The turn: the trough gives back the share
            // `market_vol_cycle_trough_release` of the contraction's excess
            // (in logs); at 0.0, every preset, it is the contraction's.
            CyclePhase::Trough => {
                let g = self.params.market_vol_cycle_trough_release;
                if g == 0.0 {
                    r * ke
                } else {
                    return crate::mathx::log(ke) + (1.0 - g) * crate::mathx::log(r);
                }
            }
            _ => ke,
        };
        crate::mathx::log(k)
    }

    /// The index's log rise from its lowest close since its highest close
    /// of the last [`DRAWDOWN_WINDOW`] sessions, read on the drawdown's
    /// window (total public market cap, through the last close booked):
    /// 0.0 at a new high or a new low, and 0.0 with no window kept.
    pub fn index_rally_off_low(&self) -> f64 {
        let (mut level, mut high, mut low) = (0.0, 0.0, 0.0);
        for r in self.drawdown_returns.iter() {
            level += r;
            if level >= high {
                high = level;
                low = level;
            } else if level < low {
                low = level;
            }
        }
        level - low
    }

    /// Step the cycle multiplier toward its phase's value and return it
    /// (in logs). The first step, and every step at a half-life of 0.0,
    /// lands on the target. Called once a close, only with
    /// `market_vol_cycle_ratio` set.
    fn step_market_vol_cycle(&mut self) -> f64 {
        let l = self.market_vol_cycle_next_log();
        self.market_vol_cycle_log = Some(l);
        l
    }

    /// The cycle multiplier tonight's step would leave (in logs), without
    /// taking the step: [`Engine::step_market_vol_cycle`]'s arithmetic, for
    /// it and for the live rate mark's projection of the close
    /// (`rate_intraday_live`), which must read the close's own multiplier.
    fn market_vol_cycle_next_log(&self) -> f64 {
        // On a session a caller's pins cover, the multiplier relaxes toward
        // one (a log of 0.0), which is what the session applies, so a run
        // the pins release steps from there toward its phase's value at
        // the half-life instead of jumping to it
        // (`market_vol_cycle_pin_neutral`, `market_vol_cycle_pin_phase`).
        let target = if self.market_vol_cycle_pinned() {
            0.0
        } else {
            self.market_vol_cycle_held_target(self.market_vol_cycle_target_log())
        };
        // The release half-life (`market_vol_cycle_release_half_life`)
        // while the multiplier falls toward a lower target, as a phase
        // turns up; the one half-life otherwise, and always at 0.0.
        let release = self.params.market_vol_cycle_release_half_life;
        let hl = match self.market_vol_cycle_log {
            Some(prev) if release != 0.0 && target < prev => release,
            _ => self.params.market_vol_cycle_half_life,
        };
        match self.market_vol_cycle_log {
            Some(prev) if hl > 0.0 => {
                let a = 1.0 - crate::mathx::exp(-std::f64::consts::LN_2 / hl);
                prev + a * (target - prev)
            }
            _ => target,
        }
    }

    /// The time-held fade's clock as tonight's close will leave it
    /// (`market_vol_cycle_hold_onset`): the phase this session closes in and
    /// its age in closes, tonight's included. `None` with the dial at 0.0.
    /// It reads the phase in the economy, which is the one a host wrote
    /// through `economy_mut`, pinned or left to the engine's own cycle, and
    /// is a function of the state alone, so the live marks' projection of the
    /// close reads the age the close does.
    fn cycle_hold_tonight(&self) -> Option<(crate::economy::CyclePhase, i64)> {
        if self.params.market_vol_cycle_hold_onset == 0.0 {
            return None;
        }
        let phase = self.economy.cycle_phase;
        Some(match self.cycle_hold {
            Some((held, age)) if held == phase => (phase, age.saturating_add(1)),
            _ => (phase, 1),
        })
    }

    /// Closes past its onset for a contraction or trough held `hold` long:
    /// the age less the onset, the `market_vol_cycle_hold_onset` quantile of
    /// the phase's own sojourn on this engine's table
    /// (`economy::cycle::sojourn_quantile_days_for`). `None` in any other
    /// phase, at or before the onset, and with the dial at 0.0.
    fn cycle_hold_past_onset(&self, hold: Option<(crate::economy::CyclePhase, i64)>) -> Option<i64> {
        use crate::economy::CyclePhase;
        let (phase, age) = hold?;
        if !matches!(phase, CyclePhase::Contraction | CyclePhase::Trough) {
            return None;
        }
        let onset = crate::economy::cycle::sojourn_quantile_days_for(
            phase, &self.cycle_spec(), self.params.market_vol_cycle_hold_onset);
        if age > onset {
            Some(age - onset)
        } else {
            None
        }
    }

    /// The cycle multiplier's target (in logs) once a held contraction or
    /// trough has faded (`market_vol_cycle_hold_fade`): `l (1 - f (1 -
    /// 0.5^(x / H)))` at `x` closes past the onset while `l > 0`, `H` being
    /// `market_vol_cycle_hold_half_life` (the whole share at 0.0). `target`
    /// itself, bit for bit, with the fade at 0.0, every shipped preset, at
    /// or under one, and before the onset.
    fn market_vol_cycle_held_target(&self, target: f64) -> f64 {
        let f = self.params.market_vol_cycle_hold_fade;
        if f == 0.0 || target <= 0.0 {
            return target;
        }
        match self.cycle_hold_past_onset(self.cycle_hold_tonight()) {
            Some(past) => {
                let h = self.params.market_vol_cycle_hold_half_life;
                let gone = if h == 0.0 {
                    1.0
                } else {
                    1.0 - crate::mathx::pow(0.5, past as f64 / h)
                };
                target * (1.0 - f * gone)
            }
            None => target,
        }
    }

    /// The held VIX's slow log average tonight's close would leave, and the
    /// coupling denominator it would apply, given the close's own
    /// denominator `d` (`market_vol_vix_hold_half_life`). Past a
    /// contraction's or trough's onset the average opens at `log d` and
    /// steps toward the log VIX at the half-life, and the denominator is
    /// `max(d, exp(s))`; `(None, d)` before the onset, in any other phase,
    /// with the half-life at 0.0, and `d` itself on a forced close (which
    /// applies no phase scale either). Reads the state and writes nothing.
    fn vix_hold_next(&self, d: f64) -> (Option<f64>, f64) {
        let h = self.params.market_vol_vix_hold_half_life;
        if h == 0.0 || self.cycle_hold_past_onset(self.cycle_hold_tonight()).is_none() {
            return (None, d);
        }
        let s = match self.vix_hold_log {
            Some(s) => {
                let a = 1.0 - crate::mathx::pow(0.5, 1.0 / h);
                s + a * (crate::mathx::log(self.economy.vix) - s)
            }
            None => crate::mathx::log(d),
        };
        if self.vix_sets_variance_pending {
            (Some(s), d)
        } else {
            (Some(s), crate::mathx::max(d, crate::mathx::exp(s)))
        }
    }

    /// Advance the time-held fade's clock and the held VIX to tonight's
    /// close, and return the coupling's denominator: [`Engine::vix_hold_next`]
    /// applied. Called once a close, only with `market_vol_cycle_hold_onset`
    /// set.
    fn step_cycle_hold(&mut self, d: f64) -> f64 {
        let (s, out) = self.vix_hold_next(d);
        self.cycle_hold = self.cycle_hold_tonight();
        self.vix_hold_log = s;
        out
    }

    /// Whether this engine's model carries the time-held fade's clock and
    /// the held VIX, which is when the snapshot and the state hash carry
    /// them: only with `market_vol_cycle_hold_onset` set, and once a close
    /// has started the clock. Off on every shipped preset.
    pub fn carries_cycle_hold(&self) -> bool {
        self.params.market_vol_cycle_hold_onset != 0.0 && self.cycle_hold.is_some()
    }

    /// The time-held fade's state for a snapshot and the state hash: `[the
    /// phase's place in the cycle (0 expansion to 4 recovery), its age in
    /// closes, 1 if the held VIX's average is kept and 0 if not, the average
    /// or 0]`. `None` where [`Engine::carries_cycle_hold`] is false.
    pub fn cycle_hold_words(&self) -> Option<[f64; 4]> {
        if !self.carries_cycle_hold() {
            return None;
        }
        let (phase, age) = self.cycle_hold?;
        let place = crate::economy::cycle::phase_cycle().iter().position(|p| *p == phase)?;
        Some(match self.vix_hold_log {
            Some(s) => [place as f64, age as f64, 1.0, s],
            None => [place as f64, age as f64, 0.0, 0.0],
        })
    }

    /// Put the time-held fade's state back from [`Engine::cycle_hold_words`].
    /// `None` is a model without it, or one no close has started.
    pub fn set_cycle_hold_state(&mut self, words: Option<&[f64]>) -> Result<(), String> {
        let Some(w) = words else {
            self.cycle_hold = None;
            self.vix_hold_log = None;
            return Ok(());
        };
        if self.params.market_vol_cycle_hold_onset == 0.0 {
            return Err(
                "this snapshot carries cycle_hold and this model's market_vol_cycle_hold_onset is 0"
                    .to_string());
        }
        let phases = crate::economy::cycle::phase_cycle();
        let whole = |v: f64, hi: f64| v.is_finite() && v >= 0.0 && v <= hi && v.fract() == 0.0;
        if w.len() != 4 || !whole(w[0], (phases.len() - 1) as f64) || !whole(w[1], 9.0e15) || w[1] < 1.0
            || !(w[2] == 0.0 || w[2] == 1.0) || !w[3].is_finite() || (w[2] == 0.0 && w[3] != 0.0)
        {
            return Err(format!(
                "this snapshot's cycle_hold is {w:?}: it is the held phase's place in the cycle \
                 (0 to 4), its age in closes (1 or more), 1 or 0 for whether the held VIX's \
                 average is kept, and the average (0 when it is not)"));
        }
        self.cycle_hold = Some((phases[w[0] as usize], w[1] as i64));
        self.vix_hold_log = if w[2] == 1.0 { Some(w[3]) } else { None };
        Ok(())
    }

    /// `exp(d l)`: the cycle multiplier to a power, which the VIX
    /// coupling's denominator, the anchor's slow memory and the anchor level
    /// are scaled by. The power is `market_vol_cycle_relative` while the
    /// multiplier is at or over one (`l >= 0`, a stormier phase than
    /// normal) and `market_vol_cycle_relative_calm` while it is under one
    /// (a calmer phase). Exactly 1.0 with `market_vol_cycle_ratio` at 0.0,
    /// with the side's power at 0.0, and before the first close.
    ///
    /// Floored at `VIX_STATE_FLOOR / vix_anchor`, so the scaled denominator
    /// and anchor never fall below the level the VIX itself is floored at.
    /// Without the floor a multiplier under about 0.5 at a power of 1 put
    /// the denominator under the VIX's floor of 10, the VIX-over-denominator
    /// ratio ran away, and the factor variance went to its ceiling: a
    /// quiet phase read as a panic (bear-dynamics review: a 0.05 multiplier
    /// at power 1 gave index volatility of 64 per cent). The floor binds
    /// only there; at the shipped candidates the scale is 0.8 or more
    /// against a floor of about 0.48.
    fn market_vol_cycle_vix_scale(&self) -> f64 {
        self.market_vol_cycle_vix_scale_at(
            self.market_vol_cycle_log.map(|l| self.market_vol_cycle_applied_log(l)))
    }

    /// [`Engine::market_vol_cycle_vix_scale`] at an explicit multiplier.
    fn market_vol_cycle_vix_scale_at(&self, log: Option<f64>) -> f64 {
        match log {
            Some(l) if self.params.market_vol_cycle_ratio != 0.0 => {
                let d = if l < 0.0 {
                    self.params.market_vol_cycle_relative_calm
                } else {
                    self.params.market_vol_cycle_relative
                };
                if d == 0.0 {
                    1.0
                } else {
                    let floor = if self.vix_anchor > 0.0 {
                        crate::params::VIX_STATE_FLOOR / self.vix_anchor
                    } else {
                        0.0
                    };
                    crate::mathx::max(crate::mathx::exp(d * l), floor)
                }
            }
            _ => 1.0,
        }
    }

    /// The factor `fair_value_market_vol_cap`'s ceiling is scaled by:
    /// `exp(p l)` while the cycle multiplier is over one (`l > 0`), `p`
    /// being `market_vol_cycle_cap_relative`, so a stormier phase's own
    /// normal volatility is not read as a fear regime. Exactly 1.0 with
    /// `market_vol_cycle_ratio` or the power at 0.0, in a calmer phase and
    /// before the first close.
    pub fn market_vol_cycle_cap_scale(&self) -> f64 {
        let p = self.params.market_vol_cycle_cap_relative;
        match self.market_vol_cycle_log.map(|l| self.market_vol_cycle_applied_log(l)) {
            Some(l) if self.params.market_vol_cycle_ratio != 0.0 && p != 0.0 && l > 0.0 => {
                crate::mathx::exp(p * l)
            }
            _ => 1.0,
        }
    }

    /// Whether a caller's pins withhold the cycle multiplier today: a
    /// session whose VIX a caller pinned under
    /// `market_vol_cycle_pin_neutral`, or whose cycle phase a caller pinned
    /// under `market_vol_cycle_pin_phase`. Always false with both switches
    /// at 0.0, every preset.
    ///
    /// A pinned VIX or phase is the caller's statement of the state (a
    /// replay's 2020 is 2020 whatever phase the engine's own cycle happens
    /// to be in; a scenario's recession arrives with its own VIX, credit
    /// and earnings transmission), so scaling the variance by the engine's
    /// own phase on top double-counts or contradicts it. See
    /// `ModelParams::market_vol_cycle_pin_neutral`.
    fn market_vol_cycle_pinned(&self) -> bool {
        (self.params.market_vol_cycle_pin_neutral != 0.0 && self.macro_pins_today & PIN_VIX != 0)
            || (self.params.market_vol_cycle_pin_phase != 0.0
                && self.macro_pins_today & PIN_CYCLE != 0)
    }

    /// The cycle multiplier as today's session and tonight's close APPLY
    /// it, in logs, given the multiplier `l` itself: 0.0 (a multiplier of
    /// one) on a session a caller's pins cover
    /// ([`Engine::market_vol_cycle_pinned`]), and `l` itself, bit for bit,
    /// on every other.
    fn market_vol_cycle_applied_log(&self, l: f64) -> f64 {
        if self.market_vol_cycle_pinned() {
            0.0
        } else {
            l
        }
    }

    /// Whether this engine's model carries the cycle's volatility
    /// multiplier, which is when the snapshot and the state hash carry it:
    /// only with `market_vol_cycle_ratio` set, and once a close has set it.
    /// Off on every shipped preset.
    pub fn carries_market_vol_cycle(&self) -> bool {
        self.params.market_vol_cycle_ratio != 0.0 && self.market_vol_cycle_log.is_some()
    }

    /// The cycle's multiplier on the market factor's volatility, in logs;
    /// `None` before the first close with the dial set. See
    /// [`crate::params::ModelParams::market_vol_cycle_ratio`].
    pub fn market_vol_cycle_log(&self) -> Option<f64> {
        self.market_vol_cycle_log
    }

    /// Put the cycle multiplier back (a restore). `None` is a model without
    /// it, or one no close has run on.
    pub fn set_market_vol_cycle_log(&mut self, value: Option<f64>) {
        self.market_vol_cycle_log = value;
    }

    /// The multiplier the VIX's slow level applies to what the VIX prices:
    /// exactly 1.0 at sigma 0.0, mean one otherwise.
    fn vix_level_multiplier(&self) -> f64 {
        if self.params.vix_level_sigma == 0.0 || self.vix_log_level == 0.0 {
            return 1.0;
        }
        let phi = self.params.vix_level_persistence;
        let one_minus = 1.0 - phi * phi;
        // The SAME dispersion the recursion runs on, which is the point of
        // reading it from one place: the log-normal correction below and
        // the opening draw would otherwise disagree about how wide the
        // level is the moment the gain is turned on.
        let sigma = self.vix_level_sigma_applied();
        let stationary_var = if one_minus > 0.0 { sigma * sigma / one_minus } else { 0.0 };
        crate::mathx::exp(self.vix_log_level - 0.5 * stationary_var)
    }

    pub fn set_market_vol_log_level(&mut self, level: f64) {
        self.market_vol_log_level = level;
    }

    pub fn forced_flow_spent(&self) -> f64 {
        self.forced_flow_spent
    }

    pub fn set_forced_flow_spent(&mut self, spent: f64) {
        self.forced_flow_spent = spent;
    }

    /// Nominal output when this engine was built, for checkpoints and
    /// forks. See the field's own comment for what depends on it.
    pub fn nominal_output_base(&self) -> f64 {
        self.nominal_output_base
    }

    /// Put a restored engine back on the base its snapshot was taken
    /// under. A restore that reset the economy and left this alone would
    /// price a market against output it never had.
    pub fn set_nominal_output_base(&mut self, base: f64) {
        self.nominal_output_base = base;
    }

    /// One day of the library's own loop, numbered: the trading day `day`,
    /// counted from zero, as one session of `ticks` minutes from 09:30, then
    /// the close and the macro step into `day + 1`. It is
    /// [`Engine::set_current_day`], [`Engine::open_market`],
    /// [`Engine::run_session`] and [`Engine::close_day`] in that order, the
    /// loop the Python package's `run_days` runs, and what the WebAssembly
    /// `Sim` and [`fixed_simulation_digest`] call, so neither numbers its
    /// days differently from the other.
    pub(crate) fn run_numbered_day(&mut self, day: i64, ticks: usize, buffer: &mut SessionBuffer) {
        self.set_current_day(day);
        self.open_market();
        // `reopen: false`, since the day is opened above, once, and
        // `close_at_end: false`, since `close_day` below settles the day and
        // steps the macro chain; a session that closed itself would settle
        // the day without that step. `day_of_week: 3` is the Python
        // surface's default.
        self.run_session(&SessionRequest::new(crate::market::GameTime::new(9, 30, 3), ticks), buffer);
        self.close_day(day + 1);
    }

    /// Close the trading day and step the economy to the next one.
    ///
    /// This settles the day the ticks just traded: it feeds each name's
    /// accumulated noise to GARCH as the day's innovation, rolls the daily
    /// open and close, then runs [`Engine::advance_macro_day`] for
    /// `game_day` (the macro chain, the central bank and the rates).
    ///
    /// It does not trade and draws no prices. Called on its own, with no
    /// session since the last close, it leaves every price where it was. A
    /// day that moves the market is [`Engine::set_current_day`], then
    /// [`Engine::open_market`], then [`Engine::run_session`] (or
    /// [`Engine::tick`] over the session's 390 minutes), then this.
    pub fn close_day(&mut self, game_day: i64) {
        self.close_day_with_shocks(game_day, &[]);
    }

    /// [`Engine::close_day`] with economic shocks active in tonight's macro
    /// step: the close a host that passes shocks runs.
    ///
    /// The shocks reach the step as
    /// [`DayAdvanceRequest::active_shocks`] would, and everything else is
    /// `close_day`'s: the GARCH innovations and sector variances the engine
    /// holds, the market P/E written before the step, the re-mark of each
    /// price to the macro data the step publishes, and the rate indices'
    /// close. A host that instead calls [`Engine::close_market`] and
    /// [`Engine::advance_day`] itself gets none of those last three. With
    /// no shocks this is `close_day`, draw for draw.
    ///
    /// Every preset was fitted with no shocks, so any shock passed here is
    /// outside the fitted flow; [`crate::flow::ExternalFlow`] measures how
    /// far.
    pub fn close_day_with_shocks(&mut self, game_day: i64, active_shocks: &[EconomicShock]) {
        let noise = self.daily_innovation_column();
        let innovations: Vec<Option<f64>> = noise.into_iter().map(Some).collect();
        let variances = self.sector_base_variances();
        self.close_market(&DayCloseRequest {
            daily_innovations: &innovations,
            sector_base_variances: &variances,
            avg_volume: crate::market::AvgVolumePolicy::Hold,
        });
        self.advance_macro_day_with_shocks(game_day, active_shocks);
    }

    /// Step the macro chain into the next day.
    ///
    /// Inputs assembled the way the reference implementation's day
    /// transition assembles them:
    ///
    /// - `market_pe`: market-cap-weighted trailing PE over public, solvent,
    ///   positive-earnings names, with the same `0 < pe < 200` filter.
    ///   Written BEFORE the step because the cycle-transition logic reads
    ///   it; the reference implementation computes it in the same breath.
    /// - `market_return_pct`: the reference feeds the average of its
    ///   indices' per-TICK `changePercent` (percent units, so a routine
    ///   value is a few hundredths). There is no index state on this
    ///   surface, so the closest faithful quantity is the cap-weighted
    ///   cross-section of the roster's final tick, in percent. Feeding the
    ///   DAY return instead would run two orders of magnitude hot against
    ///   the +-0.03 clamp the VIX update applies to this input.
    /// - `volatility`: 1.0, `update_economy_daily`'s own default. The game
    ///   scales this by difficulty (0.3 to 0.9); the library has no
    ///   difficulty setting and takes the function's default.
    /// - no active shocks: [`Engine::advance_macro_day_with_shocks`] takes
    ///   them.
    pub fn advance_macro_day(&mut self, game_day: i64) -> DayAdvanceOutcome {
        self.advance_macro_day_with_shocks(game_day, &[])
    }

    /// [`Engine::advance_macro_day`] with economic shocks active in the
    /// step. With none it is `advance_macro_day`, draw for draw.
    pub fn advance_macro_day_with_shocks(
        &mut self,
        game_day: i64,
        active_shocks: &[EconomicShock],
    ) -> DayAdvanceOutcome {
        let mut total_mcap = 0.0;
        let mut weighted_pe = 0.0;
        let mut last_tick_mcap = 0.0;
        let mut last_tick_return_pct = 0.0;
        // The trailing PE is a price over an EARNINGS figure, and under
        // `earnings_nominal_growth` the earnings the model believes a
        // company has are its day-0 figure restated in today's price level
        // and output. Reading the day-0 figure against a price that grew
        // with nominal output would report a multiple that rose because
        // the mechanism ran, and the expansion hazard in `economy::cycle`
        // adds `min(0.1, (pe - 28) * 0.005)` a day above a multiple of 28.
        //
        // Measured on one trajectory, pt-v18 on `Universe.random(40,
        // seed=111)` seed 1 over 1008 days: the restated multiple ends at
        // 30.214 and crosses 28 on 32 days for a summed hazard of 0.2536,
        // and the day-0 denominator on the same tape gives 32.284, 42 days
        // and 0.6344. Inside a certified year neither reaches the gate:
        // the multiple at day 251 is 21.671 at a ratio of 1.0448, so this
        // is worth nothing over 252 days and a third of the expansion
        // hazard over four years. Exactly 1.0 under every preset before
        // pt-v18.
        let nominal = crate::market::tick::nominal_scale(
            &self.params, &self.economy, self.nominal_output_base);
        for c in self.companies() {
            if !c.is_public || c.is_bankrupt {
                continue;
            }
            if let Some(eps) = c.eps {
                if eps > 0.0 {
                    // A branch, as at the valuation, so the arithmetic
                    // before pt-v18 is the arithmetic it always was.
                    let earnings = if nominal == 1.0 { eps } else { eps * nominal };
                    // The earnings the valuation holds carry the name's own
                    // fair-value level (`fair_value_news_share`), so the
                    // market P/E reads the earnings the price does. A branch:
                    // `None` on every preset through pt-v19.
                    let earnings = match c.stock.fair_value_offset {
                        Some(v) if v != 0.0 => earnings * crate::mathx::exp(v),
                        _ => earnings,
                    };
                    // `market_pe_buybacks`: the earnings the valuation holds
                    // also carry the buyback term (`market::tick`), so a
                    // multiple read without it rises by the buyback yield
                    // every year. A branch, so 0.0 is the line that stood.
                    let earnings = if self.params.market_pe_buybacks != 0.0 {
                        earnings * crate::market::tick::buyback_factor(
                            &self.params, crate::market::dividends::buyback_share(&self.params, c),
                            Some(earnings), c.stock.price, self.elapsed_days,
                            c.stock.buyback_log_shares)
                    } else {
                        earnings
                    };
                    let pe = c.stock.price / earnings;
                    if pe > 0.0 && pe < 200.0 {
                        total_mcap += c.stock.market_cap;
                        weighted_pe += pe * c.stock.market_cap;
                    }
                }
            }
            if let Some(prev) = c.stock.previous_tick_price {
                if prev > 0.0 {
                    let ret_pct = (c.stock.price - prev) / prev * 100.0;
                    last_tick_return_pct += ret_pct * c.stock.market_cap;
                    last_tick_mcap += c.stock.market_cap;
                }
            }
        }
        if total_mcap > 0.0 {
            self.economy_mut().market_pe = Some(weighted_pe / total_mcap);
        }
        let market_return_pct = if last_tick_mcap > 0.0 {
            last_tick_return_pct / last_tick_mcap
        } else {
            0.0
        };
        // The fair values the market stands on BEFORE the step, taken only
        // with `macro_publication_repricing` on: the step's decision is
        // readable the moment it ends, so the price takes it then.
        let marks = self.published_macro_marks();
        // The state before the step, kept only to split the re-mark's
        // growth from its news (`remark_growth_open_share`). A branch.
        let before = if marks.is_some() && self.params.remark_growth_open_share != 0.0 {
            Some(self.economy.clone())
        } else {
            None
        };
        let hold = self.carries_earnings_withheld();
        let level_before = self.economy.earnings_cycle + self.economy.earnings_anticipation;
        let outcome = self.advance_day(&DayAdvanceRequest {
            volatility: 1.0,
            active_shocks,
            market_return_pct,
            game_day,
            timestamp: game_day * 24 * 60,
        });
        // The re-mark's growth part, on the names' figures as the step left
        // them and before any of the cycle is held back below.
        let growth = match (&before, &marks) {
            (Some(old), Some(fv0)) => Some(self.remark_growth(old, fv0)),
            _ => None,
        };
        // THE CYCLE A NAME LEARNS FROM ITS OWN REPORT
        // (`earnings_cycle_report_share`): the share of the step's move in
        // the cycle's level that each traded name's fair value holds back
        // until its next report. Before the re-mark below, so tonight's
        // price takes only the rest. A branch: off, nothing runs.
        if hold {
            let delta = self.economy.earnings_cycle + self.economy.earnings_anticipation
                - level_before;
            self.withhold_cycle(delta);
        }
        self.reprice_marks(marks, match growth {
            Some(g) => RemarkGrowth::Part(g),
            None => RemarkGrowth::None,
        });
        // THE RATE INDICES AT THE SAME CLOSE (`rate_close_remark`): the curve
        // the step just published reaches their levels now, beside the
        // equities' re-mark, and not at the next open. A branch.
        self.rate_live = None;
        self.vix_live = None;
        if self.params.rate_close_remark != 0.0 && !self.rates.is_empty() {
            self.rates.remark_now(&self.economy);
        }
        // The published VIX's log average the slow level's nowcast reads
        // (`forecast_vix_level_nowcast`). Nothing with the dial at 0.0.
        self.advance_vix_log_average();
        // The forecast on the state the close leaves (`forecast_horizon_sessions`).
        // Nothing with the dial at 0.0.
        self.refresh_forecast(Some(game_day));
        // The VIX futures' marks on that forecast and the VIX the close
        // published (`futures_vix_listed`). Nothing with the switch off.
        self.vix_futures_close_marks();
        // The rate futures on the policy rate the close set and the forecast
        // (`futures_rates_listed`): a period that ends here settles, and each
        // contract is marked. Nothing with the switch off.
        self.rate_futures_close_marks();
        // The oil futures' marks on the forecast (`futures_oil_listed`).
        self.oil_futures_close_marks();
        // The contracts' margin on every family's marks
        // (`margin_scan_coverage`). Nothing with the dial off.
        self.margin_close_update();
        // The single-name options at the close, on that forecast
        // (`options_names_listed`): exercises and assignments, the expiries
        // due settle on the closing prints, the next are listed.
        self.name_options_close();
        // The night's path for the index futures, to the next open as a copy
        // runs it (`night_session_steps`). Nothing with the dial at 0.0.
        self.futures_night_setup();
        outcome
    }

    /// A pin wrote the curve: re-mark the rate indices to it now, as the
    /// equities are (`rate_close_remark`), and drop the live mark's
    /// projections, which the next tick recomputes on the pinned state.
    /// Nothing with the switch off or without rate instruments.
    pub fn remark_rates_after_pin(&mut self) {
        self.rate_live = None;
        // The live VIX's projection was of the state before the pin; the next
        // tick projects the pinned one (`vix_intraday_live`).
        self.vix_live = None;
        if self.params.rate_close_remark != 0.0 && !self.rates.is_empty() {
            self.rates.remark_now(&self.economy);
        }
        // The forecast, re-read on the pinned state (`forecast_horizon_sessions`).
        self.refresh_forecast(None);
        // Between sessions, the night's path moves by what the pin does to
        // the next open (`night_session_steps`); nothing otherwise.
        self.futures_retarget();
    }

    /// The live mark's move over the published curve, `now - base`, while a
    /// session carries one (`rate_intraday_live`); `None` otherwise.
    pub fn rate_live_curve(&self) -> Option<crate::rates::LiveCurve> {
        if self.params.rate_intraday_live == 0.0 {
            return None;
        }
        let m = self.rate_live?;
        Some(crate::rates::LiveCurve { d2y: m[3] - m[0], d10y: m[4] - m[1], dcorp: m[5] - m[2] })
    }

    /// Each rate instrument's level and yield (a fraction) as it is marked
    /// now: the live mark's during a session under `rate_intraday_live`, the
    /// committed level and yield otherwise.
    pub fn rate_marks(&self) -> Vec<(f64, f64)> {
        let live = self.rate_live_curve();
        self.rates
            .instruments
            .iter()
            .map(|i| match live {
                Some(c) => {
                    let dy = c.dy(i.spec.point);
                    (i.level_at(dy), i.marked_yield + dy)
                }
                None => (i.level, i.marked_yield),
            })
            .collect()
    }

    /// The live mark's projections, for the snapshot: `Some` only while
    /// `rate_intraday_live` is set and a session carries them.
    pub fn rate_live_marks(&self) -> Option<[f64; 6]> {
        if self.params.rate_intraday_live == 0.0 {
            return None;
        }
        self.rate_live
    }

    /// For a restore. Refused with the dial off, where no engine writes them.
    pub fn set_rate_live_marks(&mut self, marks: Option<[f64; 6]>) -> Result<(), String> {
        if marks.is_some() && self.params.rate_intraday_live == 0.0 {
            return Err("this snapshot carries the rate indices' live mark (rate_live_marks), \
                        which only an engine with rate_intraday_live on writes, and this \
                        engine's model has it off. Restore it into the model it was taken from."
                .to_string());
        }
        self.rate_live = marks;
        Ok(())
    }

    /// The stress level the next meeting reads (`fed_stress_cut`), for the
    /// snapshot: `Some` only while the cut is set.
    pub fn stress_vix_max(&self) -> Option<f64> {
        if self.params.fed_stress_cut == 0.0 {
            None
        } else {
            Some(self.stress_vix_max)
        }
    }

    /// The stress hold's clock (`fed_stress_hold`), for the snapshot:
    /// `Some` only while the dial is set.
    pub fn stress_hold_age(&self) -> Option<f64> {
        if self.params.fed_stress_hold == 0.0 {
            None
        } else {
            Some(self.stress_hold_age)
        }
    }

    /// For a restore. Refused with the dial off, where no engine writes it.
    pub fn set_stress_hold_age(&mut self, age: Option<f64>) -> Result<(), String> {
        match age {
            Some(_) if self.params.fed_stress_hold == 0.0 => Err(
                "this snapshot carries the central bank's stress-hold clock \
                 (fed_stress_hold_age), which only an engine with fed_stress_hold on writes, \
                 and this engine's model has it off. Restore it into the model it was taken \
                 from."
                    .to_string()),
            Some(v) => {
                self.stress_hold_age = v;
                Ok(())
            }
            None => {
                self.stress_hold_age = STRESS_HOLD_NEVER;
                Ok(())
            }
        }
    }

    /// The drawdown's window and its base (`fed_drawdown_hold`), for the
    /// snapshot: `Some` only while the dial is set.
    pub fn drawdown_state(&self) -> Option<(Vec<f64>, f64)> {
        if !self.keeps_drawdown_window() {
            None
        } else {
            Some((self.drawdown_returns.iter().copied().collect(), self.drawdown_mcap_prev))
        }
    }

    /// For a restore. Refused with the dial off, where no engine writes it,
    /// and for a window longer than [`DRAWDOWN_WINDOW`].
    pub fn set_drawdown_state(&mut self, state: Option<(Vec<f64>, f64)>) -> Result<(), String> {
        match state {
            Some(_) if !self.keeps_drawdown_window() => Err(
                "this snapshot carries the drawdown hold's window (fed_drawdown_returns), \
                 which only an engine with fed_drawdown_hold or \
                 market_vol_cycle_recovery_release on writes, and this engine's model has \
                 both off. Restore it into the model it was taken from."
                    .to_string()),
            Some((returns, _)) if returns.len() > DRAWDOWN_WINDOW => Err(format!(
                "this snapshot's fed_drawdown_returns carries {} sessions; the window is {}.",
                returns.len(), DRAWDOWN_WINDOW)),
            Some((returns, prev)) => {
                self.drawdown_returns = returns.into_iter().collect();
                self.drawdown_mcap_prev = prev;
                Ok(())
            }
            None => {
                self.drawdown_returns.clear();
                self.drawdown_mcap_prev = 0.0;
                Ok(())
            }
        }
    }

    /// The market's forecast of the policy path (`treasury_path_pricing`),
    /// for the snapshot: `Some` only while the dial is set.
    pub fn policy_path(&self) -> Option<f64> {
        if self.params.treasury_path_pricing == 0.0 {
            None
        } else {
            Some(self.policy_path)
        }
    }

    /// For a restore. Refused with the dial off, where no engine writes it.
    pub fn set_policy_path(&mut self, path: Option<f64>) -> Result<(), String> {
        match path {
            Some(_) if self.params.treasury_path_pricing == 0.0 => Err(
                "this snapshot carries the market's forecast of the policy path \
                 (treasury_policy_path), which only an engine with treasury_path_pricing on \
                 writes, and this engine's model has it off. Restore it into the model it was \
                 taken from."
                    .to_string()),
            Some(v) => {
                self.policy_path = v;
                Ok(())
            }
            None => {
                self.policy_path = 0.0;
                Ok(())
            }
        }
    }

    /// The policy path the curve prices tonight (`treasury_path_pricing`),
    /// percentage points, signed: the dial times the market's forecast.
    /// 0.0 with the dial off.
    fn priced_policy_path(&self) -> f64 {
        if self.params.treasury_path_pricing == 0.0 {
            0.0
        } else {
            self.params.treasury_path_pricing * self.policy_path
        }
    }

    /// What the curve prices of the next meeting (`policy_anticipation`),
    /// for the snapshot: `Some` only while the dial is set.
    pub fn policy_anticipation_priced(&self) -> Option<f64> {
        if self.params.policy_anticipation == 0.0 {
            None
        } else {
            Some(self.policy_anticipation_priced)
        }
    }

    /// For a restore. Refused with the dial off, where no engine writes it.
    pub fn set_policy_anticipation_priced(&mut self, priced: Option<f64>) -> Result<(), String> {
        match priced {
            Some(_) if self.params.policy_anticipation == 0.0 => Err(
                "this snapshot carries what the curve prices of the next meeting \
                 (policy_anticipation_priced), which only an engine with policy_anticipation \
                 on writes, and this engine's model has it off. Restore it into the model it \
                 was taken from."
                    .to_string()),
            Some(v) => {
                self.policy_anticipation_priced = v;
                Ok(())
            }
            None => {
                self.policy_anticipation_priced = 0.0;
                Ok(())
            }
        }
    }

    /// THE ANTICIPATED PATH (`treasury_path_anticipated`): what a meeting
    /// tonight moves the priced path by, the part of the eve's priced change
    /// beyond the next decision, `P (a - 1) / a`, with `a` the anticipation.
    /// `None` with the switch off: the meeting steps the path by the rate's
    /// change, as it stood.
    fn anticipated_path_carry(&self) -> Option<f64> {
        if self.params.treasury_path_anticipated == 0.0 {
            None
        } else if self.params.policy_anticipation == 0.0 {
            Some(0.0)
        } else {
            let a = self.params.policy_anticipation;
            Some(self.policy_anticipation_priced * (a - 1.0) / a)
        }
    }

    /// The next decision the curve prices tonight (`policy_anticipation`),
    /// percentage points, signed. 0.0 with the dial off.
    fn priced_anticipation(&self) -> f64 {
        if self.params.policy_anticipation == 0.0 {
            0.0
        } else {
            self.policy_anticipation_priced
        }
    }

    /// The change the next meeting would make if it were held at tonight's
    /// close, as the market can forecast it: the meeting function on the
    /// economy as published (the published phase, growth and VIX; inflation,
    /// unemployment and the rate as they stand), with tonight's options, on
    /// a silent draw source. No stream is touched and nothing is written.
    fn shadow_meeting_change(&self, request: &DayAdvanceRequest, options: &crate::economy::PolicyOptions) -> f64 {
        let published = self.shadow_published_change(request, options);
        // THE FED'S GUIDANCE (`policy_anticipation_guidance` q): the market
        // prices q of the decision the Fed's own reading (the economy the
        // meeting reads) implies, and 1 - q of the published one's. A second
        // silent shadow, no draw. A branch: at 0.0 the change is the
        // published shadow's, as it stood.
        let q = self.params.policy_anticipation_guidance;
        if q != 0.0 {
            let own = self.shadow_own_change(request, options);
            (1.0 - q) * published + q * own
        } else {
            published
        }
    }

    /// The meeting function's change on the economy as published, as
    /// [`Engine::shadow_meeting_change`] reads it.
    pub(crate) fn shadow_published_change(
        &self,
        request: &DayAdvanceRequest,
        options: &crate::economy::PolicyOptions,
    ) -> f64 {
        let mut economy = self.economy.clone();
        economy.cycle_phase = self.published_cycle_phase();
        economy.gdp_growth = self.published_gdp_growth();
        economy.vix = self.published_vix();
        let shadow = update_central_bank_with(
            &self.shadow_bank(request), &economy, request.timestamp, &mut SilentDraws, &self.shadow_options(options));
        shadow.economy.federal_funds_rate - self.economy.federal_funds_rate
    }

    /// The meeting function's change on the economy the meeting reads (the
    /// Fed's own reading), as [`Engine::shadow_meeting_change`] reads it.
    pub(crate) fn shadow_own_change(&self, request: &DayAdvanceRequest, options: &crate::economy::PolicyOptions) -> f64 {
        let own = update_central_bank_with(
            &self.shadow_bank(request), &self.economy, request.timestamp, &mut SilentDraws, &self.shadow_options(options));
        own.economy.federal_funds_rate - self.economy.federal_funds_rate
    }

    fn shadow_bank(&self, request: &DayAdvanceRequest) -> CentralBankState {
        let mut cb = self.central_bank;
        cb.next_meeting_date = request.timestamp;
        cb
    }

    fn shadow_options(&self, options: &crate::economy::PolicyOptions) -> crate::economy::PolicyOptions {
        crate::economy::PolicyOptions {
            stress_level: self.stress_vix_max,
            stress_hold: self.stress_hold_now(),
            path_before: self.priced_policy_path(),
            eve_change: None,
            ..*options
        }
    }

    /// `policy_anticipation` at the close: `P = a w S`, with `S` the shadow
    /// meeting's change (a cut times `policy_anticipation_cut_share`) and `w`
    /// the share of the interval from the last meeting to the next already
    /// elapsed. The curve holds `f P` above what it would be without it,
    /// `f` the pass-through the priced path has (`1 - d` on the 10-year and
    /// the corporate yield, `0.85 + 0.15 (1 - d)` on the 2-year, with `d`
    /// `treasury_policy_damping`): the daily anchor keeps it, and a meeting,
    /// which re-anchors the 10-year halfway to a target that does not carry
    /// it and sets the 2-year from the rate, leaves half of it on the 10-year
    /// and the corporate yield and 0.15 of that on the 2-year. So the close
    /// moves each by what it should price less what it holds. A pinned
    /// field is left where the pin holds it.
    fn reprice_anticipated_meeting(
        &mut self,
        request: &DayAdvanceRequest,
        options: &crate::economy::PolicyOptions,
        meeting_held: bool,
        pins_today: u16,
    ) {
        let a = self.params.policy_anticipation;
        let change = self.shadow_meeting_change(request, options);
        let change = if change < 0.0 { change * self.params.policy_anticipation_cut_share } else { change };
        let (last, next) = (self.central_bank.last_meeting_date, self.central_bank.next_meeting_date);
        let w = if next > last {
            crate::mathx::min(1.0, crate::mathx::max(0.0, (request.timestamp - last) as f64 / (next - last) as f64))
        } else {
            1.0
        };
        let priced = a * w * change;
        let before = self.policy_anticipation_priced;
        // THE PRICED PATH'S MEMORY (`policy_anticipation_half_life`): between
        // meetings the priced change moves toward the shadow's at the dial's
        // half-life, an average of the closes the market has seen rather than
        // the last one; a meeting's close sets it. A branch: at 0.0 the priced
        // change is the shadow's, as it stood.
        let h = self.params.policy_anticipation_half_life;
        let priced = if h != 0.0 && !meeting_held {
            before + (1.0 - crate::mathx::pow(0.5, 1.0 / h)) * (priced - before)
        } else {
            priced
        };
        self.policy_anticipation_priced = priced;
        let f10 = 1.0 - self.params.treasury_policy_damping;
        let f2 = 0.85 + 0.15 * f10;
        // A meeting that keeps the 2-year's gap (`treasury_2y_meeting_carry`
        // c) leaves it holding `0.85 c` of the anticipation besides the 10-
        // year's share. A branch: at 0.0 the held part is as it stood.
        let carry = self.params.treasury_2y_meeting_carry;
        let held2_meeting = if carry != 0.0 {
            0.075 * f10 * before + 0.85 * carry * before
        } else {
            0.075 * f10 * before
        };
        let (held10, held2) = if meeting_held { (0.5 * f10 * before, held2_meeting) } else { (f10 * before, f2 * before) };
        let (d10, d2) = (f10 * priced - held10, f2 * priced - held2);
        let e = &mut self.economy;
        if pins_today & PIN_T10 == 0 {
            e.treasury_yield_10y = crate::mathx::clamp_via_min_max(e.treasury_yield_10y + d10, 0.5, 12.0);
        }
        if pins_today & PIN_T2 == 0 {
            e.treasury_yield_2y = crate::mathx::clamp_via_min_max(e.treasury_yield_2y + d2, 0.0, 12.0);
        }
        if pins_today & (PIN_CORPORATE | PIN_SPREAD | PIN_T10) == 0 {
            e.corporate_bond_yield += d10;
        }
    }

    /// For a restore. Refused with the cut off, where no engine writes it.
    pub fn set_stress_vix_max(&mut self, level: Option<f64>) -> Result<(), String> {
        match level {
            Some(_) if self.params.fed_stress_cut == 0.0 => Err(
                "this snapshot carries the central bank's stress level (fed_stress_vix_max), \
                 which only an engine with fed_stress_cut on writes, and this engine's model \
                 has it off. Restore it into the model it was taken from."
                    .to_string()),
            Some(v) => {
                self.stress_vix_max = v;
                Ok(())
            }
            None => {
                self.stress_vix_max = 0.0;
                Ok(())
            }
        }
    }

    /// The session's cap-weighted return so far, in per cent: the quantity
    /// the close's step reads as `market_day_return_pct`, by the same loop.
    fn session_return_pct(&self) -> f64 {
        let (mut acc, mut mcap) = (0.0, 0.0);
        for c in self.companies.iter() {
            if !c.is_public || c.is_bankrupt || c.stock.previous_close <= 0.0 {
                continue;
            }
            let d = (c.stock.price - c.stock.previous_close) / c.stock.previous_close * 100.0;
            acc += d * c.stock.market_cap;
            mcap += c.stock.market_cap;
        }
        if mcap > 0.0 { acc / mcap } else { 0.0 }
    }

    /// Each slot's GARCH variance as tonight's close is expected to leave
    /// it: the close's own update (`market::daily::close_day_with`) on the
    /// session's innovation so far (none when `empty`), with the rest of the
    /// session's innovation, sd `sqrt(variance * remaining)`, integrated by
    /// three-point Gauss-Hermite, which is exact for the quadratic update.
    fn projected_name_variances(&self, empty: bool, remaining: f64) -> Vec<f64> {
        let innovations = if empty { Vec::new() } else { self.daily_innovation_column() };
        let bases = self.sector_base_variances();
        let r3 = crate::mathx::sqrt(3.0);
        self.companies
            .iter()
            .enumerate()
            .map(|(i, c)| {
                let stock = &c.stock;
                let innovation = if empty {
                    0.0
                } else {
                    let daily_return = if stock.previous_close > 0.0 {
                        (stock.price - stock.previous_close) / stock.previous_close
                    } else {
                        0.0
                    };
                    match innovations.get(i) {
                        Some(&x) if x.is_finite() => x,
                        _ => daily_return,
                    }
                };
                let beta = crate::market::garch::garch_beta_for(&self.params, stock.market_cap);
                let base = bases.get(i).copied().unwrap_or(stock.garch_variance);
                let base_variance = if self.params.garch_vix_coupling == 0.0 {
                    base
                } else {
                    let ratio = self.economy.vix / self.vix_anchor;
                    let cpl = self.params.garch_vix_coupling;
                    base * (1.0 - cpl + crate::market::garch::vix_coupled_response(&self.params, cpl, ratio))
                };
                let one = |eps: f64| {
                    if self.params.garch_cascade_components >= 1.0 {
                        let mut cascade = stock.garch_cascade;
                        crate::market::garch::update_garch_cascade(
                            &self.params, beta, &mut cascade, eps, base_variance)
                    } else {
                        crate::market::garch::update_garch_variance_for(
                            &self.params, beta, stock.garch_variance, eps, base_variance)
                    }
                };
                let sd = crate::mathx::sqrt(crate::mathx::max(0.0, stock.garch_variance * remaining));
                if sd > 0.0 {
                    (one(innovation - r3 * sd) + 4.0 * one(innovation) + one(innovation + r3 * sd)) / 6.0
                } else {
                    one(innovation)
                }
            })
            .collect()
    }

    /// The (2-year, 10-year, corporate) yields, in per cent, and the VIX as
    /// published that tonight's step would give if the session ended now
    /// with the index at `session_pct`, the market factor's state `mv` and
    /// the names' GARCH variances `garch` (empty off the identity): the
    /// close's factor update on a copy, the index variance, the anchor's
    /// slow memory and the published VIX's stress memory as the close
    /// computes them, and the step's VIX and yields
    /// (`economy::daily::project_close_state`) with its draws at their means
    /// and no meeting. Reads the state and writes nothing.
    fn projected_close(
        &self,
        session_pct: f64,
        mv: MarketVarianceState,
        garch: &[f64],
        yields: bool,
    ) -> [f64; 4] {
        let mut mv = mv;
        let denominator = if self.params.market_vol_vix_excursion == 0.0 {
            self.vix_anchor
        } else {
            let implied = crate::market::index_var::vix_from_variance(
                self.params.vix_variance_premium,
                self.index_conditional_variance_terms_now().total(),
            );
            if implied > 0.0 { implied } else { self.vix_anchor }
        };
        let level = if self.params.market_vol_level_sigma == 0.0 {
            1.0
        } else {
            let phi = self.params.market_vol_level_persistence;
            let sigma = self.params.market_vol_level_sigma;
            let one_minus = 1.0 - phi * phi;
            let stationary_var = if one_minus > 0.0 { sigma * sigma / one_minus } else { 0.0 };
            crate::mathx::exp(self.market_vol_log_level - 0.25 * stationary_var)
        };
        // The cycle's volatility multiplier as the close will step and apply
        // it (`market_vol_cycle_ratio`): a branch at 0.0 that reads nothing,
        // and not applied on a forced close, as there.
        let (level, denominator, cycle_vix_scale) = if self.params.market_vol_cycle_ratio == 0.0 {
            (level, denominator, 1.0)
        } else {
            let l = self.market_vol_cycle_applied_log(self.market_vol_cycle_next_log());
            let scale = self.market_vol_cycle_vix_scale_at(Some(l));
            if self.vix_sets_variance_pending {
                (level, denominator, scale)
            } else {
                let m = crate::mathx::exp(l);
                (level * m * m, denominator * scale, scale)
            }
        };
        // The held VIX as the close will apply it
        // (`market_vol_vix_hold_half_life`); a branch at 0.0.
        let denominator = if self.params.market_vol_cycle_hold_onset == 0.0 {
            denominator
        } else {
            self.vix_hold_next(denominator).1
        };
        let _ = mv.close_day_scaled(&self.params, denominator, self.economy.vix, level);
        let index_variance = if self.params.vix_level_identity == 0.0 {
            0.0
        } else {
            let g = if garch.is_empty() { None } else { Some(garch) };
            self.index_conditional_variance_terms_with(mv.variance(), mv.prev_day_down(), g).total()
        };
        // The anchor's slow memory and the published VIX's stress memory, as
        // `advance_day_with` steps them before the economy's step.
        let (slow, stress) = if self.params.vix_anchor_memory != 0.0 && self.params.vix_level_identity != 0.0 {
            let mult = self.vix_level_multiplier();
            let implied = crate::market::index_var::vix_from_variance(
                self.params.vix_variance_premium, index_variance) * mult;
            let anchor = self.vix_anchor * mult * cycle_vix_scale;
            let anchor = if self.params.vix_anchor_centre != 0.0 {
                anchor * crate::mathx::exp(-self.params.vix_anchor_centre)
            } else {
                anchor
            };
            if implied > 0.0 && anchor > 0.0 {
                let h = self.params.vix_anchor_memory;
                let slow = (1.0 - h) * self.vix_anchor_slow + h * crate::mathx::log(implied / anchor);
                let stress = if self.params.vix_stress_premium == 0.0 {
                    self.vix_stress_memory
                } else if self.macro_pins_today & PIN_VIX != 0 {
                    0.0
                } else {
                    let hs = self.params.vix_stress_memory_rate();
                    (1.0 - hs) * self.vix_stress_memory + hs * crate::mathx::log(implied / anchor)
                };
                (slow, stress)
            } else {
                let stress = if self.params.vix_stress_premium != 0.0
                    && self.macro_pins_today & PIN_VIX != 0
                {
                    0.0
                } else {
                    self.vix_stress_memory
                };
                (self.vix_anchor_slow, stress)
            }
        } else {
            (self.vix_anchor_slow, self.vix_stress_memory)
        };
        let request = DayAdvanceRequest {
            volatility: 1.0,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day: self.elapsed_days + 1,
            timestamp: (self.elapsed_days + 1) * 24 * 60,
        };
        let mut inputs = self.daily_inputs(&request, session_pct, index_variance, slow, cycle_vix_scale);
        // The fear memory as the close will advance it (`vix_fear_uptake`).
        if self.params.vix_fear_uptake != 0.0 {
            inputs.vix_fear = crate::economy::daily::advance_vix_fear(
                self.vix_fear, &inputs, self.economy.vix, self.params.vix_fear_half_life);
        }
        if !yields {
            // The VIX alone (`vix_intraday_live` without rate indices): the
            // same step's VIX half, with the yields left at 0.0.
            let vix = crate::economy::daily::vix_close(
                &self.economy, &inputs, 0.0, &mut crate::economy::daily::MeanDraws);
            return [0.0, 0.0, 0.0, self.published_vix_at(vix, stress)];
        }
        let next = crate::economy::daily::project_close_state(&self.economy, &inputs);
        [
            next.treasury_yield_2y,
            next.treasury_yield_10y,
            next.corporate_bond_yield,
            self.published_vix_at(next.vix, stress),
        ]
    }

    /// E[tonight's (2-year, 10-year, corporate) yields and published VIX |
    /// the session so far], with `remaining` of the session's variance still
    /// to come: the index at `session_pct` plus the rest of its move, sd the
    /// index's conditional sd times `sqrt(remaining)`, and the factor's day
    /// accumulation at `day_factor` plus the same node times the factor's
    /// own, integrated by the eight equally likely nodes of
    /// [`live_mark_nodes`]. `empty` projects a session with nothing in it.
    fn expected_close(
        &self,
        session_pct: f64,
        day_factor: f64,
        empty: bool,
        remaining: f64,
        yields: bool,
    ) -> [f64; 4] {
        let garch = if self.params.vix_level_identity != 0.0 {
            self.projected_name_variances(empty, remaining)
        } else {
            Vec::new()
        };
        let sd_factor = crate::mathx::sqrt(crate::mathx::max(0.0, self.market_vol.variance() * remaining));
        if !(sd_factor > 0.0) {
            return self.projected_close(
                session_pct, self.market_vol.with_day_factor(day_factor), &garch, yields);
        }
        let sd_index = crate::mathx::sqrt(crate::mathx::max(
            0.0, self.index_conditional_variance_terms_now().total() * remaining));
        let mut out = [0.0; 4];
        for &z in live_mark_nodes().iter() {
            let y = self.projected_close(
                session_pct + 100.0 * z * sd_index,
                self.market_vol.with_day_factor(day_factor + z * sd_factor),
                &garch,
                yields,
            );
            for (o, v) in out.iter_mut().zip(y.iter()) {
                *o += v / 8.0;
            }
        }
        out
    }

    /// Recompute the live marks on the state now standing, with `remaining`
    /// of the session's variance to come: the rate indices' two projections
    /// (`rate_intraday_live`, `rates`), the open's only when none is held
    /// (the open, or the first tick after a pin), and the live VIX
    /// (`vix_intraday_live`, `vix`). One projection of the session serves
    /// both. No draw.
    fn refresh_live_marks(&mut self, remaining: f64, rates: bool, vix: bool) {
        if !rates && !vix {
            return;
        }
        let now = self.expected_close(
            self.session_return_pct(), self.market_vol.day_factor(), false, remaining, rates);
        if rates {
            let base = match self.rate_live {
                Some(m) => [m[0], m[1], m[2], 0.0],
                None => self.expected_close(0.0, 0.0, true, 1.0, true),
            };
            self.rate_live = Some([base[0], base[1], base[2], now[0], now[1], now[2]]);
        }
        if vix {
            self.vix_live = Some(now[3]);
        }
    }

    /// Take `earnings_cycle_report_share` of a move `delta` in the earnings
    /// cycle's log level out of every traded name's fair-value level and
    /// keep it for the name's next report. See [`Engine::advance_macro_day`].
    fn withhold_cycle(&mut self, delta: f64) {
        let held = self.params.earnings_cycle_report_share * delta;
        if held == 0.0 {
            return;
        }
        if self.earnings_withheld.len() != self.companies.len() {
            self.earnings_withheld.resize(self.companies.len(), 0.0);
        }
        for (i, c) in self.companies.iter_mut().enumerate() {
            if c.is_bankrupt || !c.is_public || c.stock.mispricing_s.is_none() {
                continue;
            }
            let v = c.stock.fair_value_offset.unwrap_or(0.0);
            c.stock.fair_value_offset = Some(v - held);
            self.earnings_withheld[i] += held;
        }
    }

    /// Every name's fair value as the tick would read it on the state now
    /// standing, at its current price and day, for
    /// [`Engine::reprice_to_published_macro`]. `None`, computing nothing,
    /// with `macro_publication_repricing` off. NaN for a name the re-mark
    /// leaves alone: bankrupt, private, or not yet traded (no `s`), whose
    /// first tick adopts its premium lazily and so moves nothing anyway.
    pub fn published_macro_marks(&self) -> Option<Vec<f64>> {
        if self.params.macro_publication_repricing == 0.0 {
            return None;
        }
        Some(self.fair_value_marks_now())
    }

    /// [`Engine::published_macro_marks`]'s fair values whatever
    /// `macro_publication_repricing` says: the marks the aggregate level's
    /// re-mark (`fair_value_aggregate_remark`) takes before its shift.
    fn fair_value_marks_now(&self) -> Vec<f64> {
        self.companies
            .iter()
            .map(|c| {
                if c.is_bankrupt || !c.is_public || c.stock.mispricing_s.is_none()
                    || !(c.stock.price > 0.0)
                {
                    f64::NAN
                } else {
                    crate::market::tick::tick_fair_value(
                        &self.params, &self.economy, self.nominal_output_base,
                        self.elapsed_days, c, c.stock.price)
                }
            })
            .collect()
    }

    /// Every company's fair value as the next tick starts from it, in roster
    /// order: [`crate::market::tick::tick_fair_value`] on the state now
    /// standing, at the name's current price and the current day. NaN for a
    /// bankrupt or private name, which the tick does not value.
    pub fn fair_values(&self) -> Vec<f64> {
        self.companies
            .iter()
            .map(|c| {
                if c.is_bankrupt || !c.is_public {
                    f64::NAN
                } else {
                    crate::market::tick::tick_fair_value(
                        &self.params, &self.economy, self.nominal_output_base,
                        self.elapsed_days, c, c.stock.price)
                }
            })
            .collect()
    }

    /// THE PRICE TAKES A MACRO DECISION WHEN IT IS PUBLISHED
    /// (`macro_publication_repricing`).
    ///
    /// The close's macro step -- the economy, the cycle, the central bank's
    /// meeting, the corporate yield it re-anchors -- is readable from the
    /// moment it ends (`macro_fields`, `macro_state`, a World's trace), but
    /// fair value only reached the price at the next session's first tick.
    /// Everything in between, the evening and the opening step of any
    /// harness, traded at the price from before the decision. Here each
    /// name that has traded is re-marked as the step ends to the price its
    /// premium over fair value implies on the published state: `price =
    /// (last / fv_before) * fv_after(price)`, both fair values computed as
    /// the tick computes them (`market::tick::tick_fair_value`) on the same
    /// day, `fv_after` at the new price because the buyback term reads it.
    /// The name's mispricing `s` is left as it was. The next tick therefore
    /// starts on the model price the published state implies, and the move
    /// sits between the day's last print and
    /// the next open, which is where a decision announced after the close
    /// lands on a real tape. `pin_macro` re-marks the same way, so a
    /// scenario's written state is priced the moment it is readable too.
    ///
    /// No draw, no new state: the price is the state it writes, with the
    /// market cap and the day's high and low beside it (the latter are
    /// reset at the next open when the re-mark comes after a close).
    /// `marks` is [`Engine::published_macro_marks`] taken before the change;
    /// `None` does nothing.
    pub fn reprice_to_published_macro(&mut self, marks: Option<Vec<f64>>) {
        self.reprice_marks(marks, RemarkGrowth::None);
    }

    /// The close's macro step's growth part for each name, the log change
    /// in its fair value from `fv0` (the marks before the step) on the state
    /// before the step `old` with only nominal output (`gdp`, `cpi`) brought
    /// forward, on the name's figures now. NaN where there is no mark. See
    /// `ModelParams::remark_growth_open_share`. No draw.
    fn remark_growth(&self, old: &crate::economy::EconomyState, fv0: &[f64]) -> Vec<f64> {
        let mut mixed = old.clone();
        mixed.gdp = self.economy.gdp;
        mixed.cpi = self.economy.cpi;
        self.companies
            .iter()
            .zip(fv0.iter())
            .map(|(c, &f0)| {
                if !(f0 > 0.0) || c.is_bankrupt || !c.is_public {
                    return f64::NAN;
                }
                let fg = crate::market::tick::tick_fair_value(
                    &self.params, &mixed, self.nominal_output_base, self.elapsed_days, c, c.stock.price);
                if fg > 0.0 { crate::mathx::log(fg / f0) } else { f64::NAN }
            })
            .collect()
    }

    /// [`Engine::reprice_to_published_macro`], with `growth` the part of
    /// each name's move the next opening print takes at
    /// `remark_growth_open_share` rather than the re-mark. With the dial at
    /// 0.0, or `RemarkGrowth::None`, it is the re-mark as it stood.
    fn reprice_marks(&mut self, marks: Option<Vec<f64>>, growth: RemarkGrowth) {
        let Some(before) = marks else {
            return;
        };
        let share = self.params.remark_growth_open_share;
        let p = &self.params;
        let economy = &self.economy;
        let base = self.nominal_output_base;
        let day = self.elapsed_days;
        let pending = &mut self.repriced_pending;
        for (slot, (c, &fv0)) in self.companies.iter_mut().zip(before.iter()).enumerate() {
            if !(fv0 > 0.0) || c.is_bankrupt || !c.is_public {
                continue;
            }
            let last = c.stock.price;
            let fv1 = crate::market::tick::tick_fair_value(p, economy, base, day, c, last);
            if !(fv1 > 0.0) || fv1 == fv0 {
                continue;
            }
            // The price the name's mispricing implies on the published
            // state: `price = (last / fv0) * fv(price)`. Fair value reads
            // the price itself through the buyback term (the yield is
            // earnings over price), so the first step, `last * fv1 / fv0`,
            // is exact only with `buyback_payout_share` at 0.0; with it on,
            // a re-mark that stopped there leaves the next tick to raise
            // fair value by the buyback yield the lower price implies, and
            // measured on pt-v20 with its leading dials that handed back 14
            // bp of a 79 bp hike in the first 65 minutes. The fixed point
            // is a contraction (the term's elasticity is the buyback yield
            // times the years elapsed, well under one), so a few steps
            // settle it; the loop stops when a step moves nothing, or at
            // sixteen, and takes no draw. Under `buyback_accrual` fair value
            // does not read the price, so the first step is exact and the
            // loop stops at its first comparison.
            let ratio = last / fv0;
            let clamp = |x: f64| crate::mathx::min(crate::mathx::max(x, 0.01), p.price_hard_cap);
            let mut price = clamp(ratio * fv1);
            if p.buyback_payout_share != 0.0 {
                for _ in 0..16 {
                    let fv = crate::market::tick::tick_fair_value(p, economy, base, day, c, price);
                    let next = clamp(ratio * fv);
                    if next == price {
                        break;
                    }
                    price = next;
                }
            }
            // The growth part's share left to the next opening print: the
            // price only, so `s` stands and the open prices it on the night.
            let held = if share == 0.0 {
                0.0
            } else {
                match &growth {
                    RemarkGrowth::None => 0.0,
                    RemarkGrowth::All => share * crate::mathx::log(fv1 / fv0),
                    RemarkGrowth::Part(g) => match g.get(slot) {
                        Some(&x) if x.is_finite() => share * x,
                        _ => 0.0,
                    },
                }
            };
            let price = if held == 0.0 { price } else { clamp(price * crate::mathx::exp(-held)) };
            let stock = &mut c.stock;
            stock.price = price;
            stock.high = crate::mathx::max(stock.high, price);
            stock.low = crate::mathx::min(stock.low, price);
            stock.market_cap = price * stock.shares_outstanding;
            // For the tape: the next print's `repriced` books this move, so
            // the print decomposition still sums to the move from the last
            // print. Recording only; the price above is the whole effect.
            if let Some(v) = pending.get_mut(slot) {
                *v += crate::mathx::log(price / last);
            }
        }
    }

    // ── State access ──────────────────────────────────────────────────────

    pub fn economy(&self) -> &EconomyState {
        &self.economy
    }

    /// Mutable access to the macro state, for driving a scenario.
    ///
    /// A scenario is a PATH, not a feature: a rate shock is `federal_funds_rate`
    /// stepping from 2.5 to 5 over N days, supplied by whoever is running the
    /// study. Expressing that needs the embedder to be able to write the macro
    /// state between days, which is what this is for.
    ///
    /// Writing it MID-SESSION is legal and is not checked, because the engine
    /// cannot tell a deliberate intraday shock from a mistake. It is worth
    /// knowing that the tick reads these fields as it goes, so a write between
    /// two ticks of the same session takes effect immediately and for the rest
    /// of that session — which is either exactly what was wanted or a
    /// surprise, depending on who wrote it.
    pub fn economy_mut(&mut self) -> &mut EconomyState {
        &mut self.economy
    }
    pub fn central_bank(&self) -> &CentralBankState {
        &self.central_bank
    }
    /// Mutable access to the central bank, for restoring a snapshot.
    ///
    /// Exists for the same reason as [`Engine::economy_mut`]: now that the
    /// macro chain advances between days, a fork that did not carry the
    /// bank's meeting calendar would hold a meeting its parent never held.
    pub fn central_bank_mut(&mut self) -> &mut CentralBankState {
        &mut self.central_bank
    }
    pub fn companies(&self) -> &[TickCompany] {
        &self.companies
    }
    pub fn companies_mut(&mut self) -> &mut [TickCompany] {
        &mut self.companies
    }
    pub fn len(&self) -> usize {
        self.companies.len()
    }
    pub fn is_empty(&self) -> bool {
        self.companies.is_empty()
    }

    /// Install rate instruments, marked at the curve the economy holds now.
    ///
    /// Call once, after construction and before the first open: an engine
    /// built with a settled opening has already run its burn-in by then, so
    /// the instruments are marked at the curve the run starts from. They
    /// never draw and never write the economy, so installing them changes no
    /// equity price. See `crate::rates`.
    pub fn set_rate_instruments(&mut self, instruments: Vec<crate::rates::RateInstrument>) {
        self.rates = crate::rates::RateBook::new(instruments, &self.economy);
    }

    pub fn rates(&self) -> &crate::rates::RateBook {
        &self.rates
    }

    /// A caller has written the corporate yield: re-mark the corporate
    /// index's spread to it, even if the value did not change. Nothing on an
    /// engine without rate instruments. See `crate::rates`.
    pub fn remark_credit_spread(&mut self) {
        if !self.rates.is_empty() {
            self.rates.remark_credit_spread(&self.economy);
        }
    }

    /// For a restore, which writes the book back whole.
    pub fn rates_mut(&mut self) -> &mut crate::rates::RateBook {
        &mut self.rates
    }

    /// Equities plus rate instruments: the width of every per-instrument
    /// surface. Equal to [`Engine::len`] on an engine without rate
    /// instruments.
    pub fn instrument_count(&self) -> usize {
        self.companies.len() + self.rates.len()
    }

    /// One field for the rate instruments, positional against
    /// `rates().instruments`, in the units [`Engine::column`] uses.
    ///
    /// NaN where the field does not exist for an index (`garch_variance`,
    /// `beta`, `last_daily_return`). The mispricing fields are zero: an
    /// index level is its own fair value, and the mispricing process never
    /// runs on it.
    pub fn rate_column(&self, field: PriceField) -> Vec<f64> {
        self.rates
            .instruments
            .iter()
            .map(|i| match field {
                PriceField::Price => i.price,
                PriceField::PreviousClose => i.previous_close,
                PriceField::Open => i.open,
                PriceField::High => i.high,
                PriceField::Low => i.low,
                PriceField::Volume => i.volume,
                PriceField::MarketCap => i.market_cap(),
                PriceField::MispricingS => 0.0,
                PriceField::MakerInventory => i.maker_inventory,
                PriceField::GarchVariance => f64::NAN,
                PriceField::PreviousTickPrice => f64::NAN,
                PriceField::MispricingSPrevClose => 0.0,
                PriceField::MispricingMomentum => 0.0,
                PriceField::LastDailyReturn => f64::NAN,
                PriceField::AvgVolume => i.avg_volume,
                PriceField::Beta => f64::NAN,
                PriceField::ShortInterest => 0.0,
                PriceField::FloatShares => i.units_outstanding,
            })
            .collect()
    }

    // ── Roster mutation ───────────────────────────────────────────────────
    //
    // A listed universe is not static: companies IPO in, go bankrupt, and are
    // acquired. An engine that could not represent that would force the
    // embedder to choose between rebuilding — which resets the generator and
    // destroys reproducibility — and pretending, which silently attaches
    // results to the wrong companies because every column is positional.
    //
    // # What these guarantee, and what they cannot
    //
    // They do NOT keep the rest of the market unchanged. They cannot: the tick
    // draws per company, so changing `n` shifts every subsequent draw and the
    // whole market moves from that point. That is a property of the model, not
    // a limitation of the implementation, and `Engine::new` already says so
    // about roster size.
    //
    // What they DO guarantee is reproducibility, which is the property that
    // actually matters: the generator carries forward across the change, so
    // one seed plus the same sequence of roster edits, applied at the same
    // ticks, reproduces the same market exactly. Replay works; invariance was
    // never on offer.

    /// Append a company. Returns its index.
    ///
    /// Appends rather than inserting, because index order is the draw order:
    /// inserting into the middle would renumber every company after it and
    /// change which draws they receive. An embedder that needs a particular
    /// ordering must establish it before the first tick.
    ///
    /// # The five per-slot arrays it carries
    ///
    /// `attribution`, `tick_components`, `tick_fundamental`, `tick_anchor`
    /// and `volume_idio` are all positional against `companies`, so each one
    /// gains a slot here. The new company's `volume_idio` slot is 0.0, which
    /// is what `Engine::with_params` gives every company at construction: a
    /// listing starts with no idiosyncratic volume state.
    ///
    /// `volume_idio` was left out until 2026-09-02 (issue #148), and the
    /// consequence reached two things. `update_volume_idio` draws once per
    /// SLOT, so the draw count per day was the array's stale width rather
    /// than the roster's; from this mutation onward it is the roster's
    /// width. And the tick reads each company's state at its own index, so
    /// under a preset with a non-zero `volume_idio_sigma` every company past
    /// the new one read a state that was not its own.
    pub fn add_company(&mut self, company: TickCompany) -> usize {
        // The index's level before the listing, which the divisor keeps
        // (`index_level_listed`); `None`, and nothing done, with it off.
        let index_before = self.index_before_change();
        self.base_fundamentals
            .push([company.eps, company.book_value_per_share, company.revenue_growth]);
        self.base_shares.push(company.stock.shares_outstanding);
        self.companies.push(company);
        self.attribution.push([0.0; crate::market::factors::COMPONENT_COUNT]);
        self.noise_parts.push([0.0; 3]);
        self.noise_own_scale2.push(0.0);
        if !self.innovation_day.is_empty() {
            self.innovation_day.push(0.0);
        }
        self.tick_components.push([0.0; crate::market::factors::TICK_COMPONENT_COUNT]);
        self.tick_fundamental.push(f64::NAN);
        self.tick_anchor.push(f64::NAN);
        self.volume_idio.push(0.0);
        self.jump_move.push(0.0);
        // The book's per-slot row, only once the book has been used: an
        // engine that never saw an agent's order keeps its empty table.
        if !self.book.taken.is_empty() {
            self.book.taken.push([0.0; crate::agent_book::TAKEN_WIDTH]);
        }
        // The metaorder memory's row likewise, only once it holds any.
        if !self.book.memory.is_empty() {
            self.book.memory.push([0.0; crate::agent_book::MEMORY_WIDTH]);
        }
        // The per-name jump excitation follows the roster for the reason
        // `volume_idio` above does, and it was left out for the same
        // reason: it landed after this function was written. A name that
        // joins has never jumped, so its excitation is 0.0.
        self.jump_excitation.push(0.0);
        // The earnings calendar's per-name columns: a name that joins has
        // realised no report and holds back nothing.
        self.earnings_moves.push(0.0);
        self.earnings_withheld.push(0.0);
        // The idiosyncratic variance state likewise: a name that joins is
        // unseeded (reads 1.0) and has no jump pending.
        for v in [
            &mut self.idio_variance,
            &mut self.idio_jump_pending,
            &mut self.idio_jump_var_pending,
            &mut self.idio_jump_today,
            &mut self.idio_jump_var_today,
        ] {
            v.push(0.0);
        }
        // The print decomposition, on the same argument as everything above
        // it: these are per-SLOT columns, and a roster edit that grew
        // `companies` without growing them would leave the new name reading
        // as a company that never moved.
        self.tick_shock.push(0.0);
        self.tick_absorbed.push(0.0);
        self.tick_clamp.push(0.0);
        self.tick_repriced.push(0.0);
        // A name that joins has had nothing written to its price.
        self.repriced_pending.push(0.0);
        // A name that joins has no earlier close than its listing price.
        let listing = self.companies.last().map_or(f64::NAN, |c| c.stock.price);
        self.last_closes.push(listing);
        self.prior_closes.push(listing);
        // Only where the arm is running. Empty means no arm, and pushing
        // into an empty pair would turn that into a one-row column.
        if !self.tick_unbounded_print.is_empty() {
            self.tick_unbounded_print.push(f64::NAN);
            self.tick_liquidity_share.push(0.0);
        }
        self.index_rebase(index_before);
        // A host's write between sessions moves the night's path at once
        // (`night_session_steps`); nothing otherwise.
        self.futures_retarget();
        self.companies.len() - 1
    }

    /// Remove the company at `index`, returning it.
    ///
    /// `Vec::remove`, so the tail shifts down by one and keeps its relative
    /// order. `swap_remove` would be cheaper and is wrong: it moves the last
    /// company into the hole, silently reordering the roster, and roster order
    /// is contractual.
    ///
    /// Returns `None` for an out-of-range index rather than panicking — a
    /// removal racing a bankruptcy is an embedder bug worth reporting, not a
    /// reason to abort the module and take the session with it.
    ///
    /// # The five per-slot arrays it carries
    ///
    /// The same five [`Engine::add_company`] appends to, each losing the slot
    /// at `index` so the tail shifts down with the roster it is positional
    /// against. `volume_idio` was left out until 2026-09-02 (issue #148),
    /// which left the survivors reading their old neighbours' states and
    /// held the per-day draw count at the pre-removal width; from this
    /// mutation onward the count is the roster's width.
    pub fn remove_company(&mut self, index: usize) -> Option<TickCompany> {
        if index >= self.companies.len() {
            return None;
        }
        // The index's level before the delisting, which the divisor keeps
        // (`index_level_listed`).
        let index_before = self.index_before_change();
        if index < self.tick_components.len() {
            self.tick_components.remove(index);
        }
        if index < self.tick_fundamental.len() {
            self.tick_fundamental.remove(index);
        }
        if index < self.tick_anchor.len() {
            self.tick_anchor.remove(index);
        }
        if index < self.attribution.len() {
            self.attribution.remove(index);
        }
        if index < self.noise_parts.len() {
            self.noise_parts.remove(index);
        }
        if index < self.innovation_day.len() {
            self.innovation_day.remove(index);
        }
        if index < self.noise_own_scale2.len() {
            self.noise_own_scale2.remove(index);
        }
        if index < self.volume_idio.len() {
            self.volume_idio.remove(index);
        }
        if index < self.jump_move.len() {
            self.jump_move.remove(index);
        }
        // A name that leaves takes its book with it: its waiting orders are
        // cancelled and its unapplied flow is dropped, since there is no
        // market left for either to reach.
        if index < self.book.taken.len() {
            self.book.taken.remove(index);
        }
        if index < self.book.memory.len() {
            self.book.memory.remove(index);
        }
        // Its options are withdrawn and paid at their intrinsic value
        // (`options_names_listed`), while the slot is still its own.
        self.name_options_remove(index);
        let leaving = self.companies[index].ticker.clone();
        self.book.orders.retain(|o| o.ticker != leaving);
        self.book.flow.retain(|(_, t, _, _)| *t != leaving);
        // `Vec::remove` for the reason the line above uses it: the tail
        // shifts down and keeps its relative order, so every remaining
        // name keeps its own excitation.
        if index < self.jump_excitation.len() {
            self.jump_excitation.remove(index);
        }
        if index < self.earnings_moves.len() {
            self.earnings_moves.remove(index);
        }
        if index < self.earnings_withheld.len() {
            self.earnings_withheld.remove(index);
        }
        for v in [
            &mut self.idio_variance,
            &mut self.idio_jump_pending,
            &mut self.idio_jump_var_pending,
            &mut self.idio_jump_today,
            &mut self.idio_jump_var_today,
        ] {
            if index < v.len() {
                v.remove(index);
            }
        }
        // Removed rather than left behind, because the tail shifts down by
        // one and a column that did not shift with it would report every
        // remaining company's move against its neighbour's slot.
        if index < self.tick_shock.len() {
            self.tick_shock.remove(index);
        }
        if index < self.tick_absorbed.len() {
            self.tick_absorbed.remove(index);
        }
        if index < self.tick_clamp.len() {
            self.tick_clamp.remove(index);
        }
        if index < self.tick_repriced.len() {
            self.tick_repriced.remove(index);
        }
        if index < self.repriced_pending.len() {
            self.repriced_pending.remove(index);
        }
        if index < self.last_closes.len() {
            self.last_closes.remove(index);
        }
        if index < self.prior_closes.len() {
            self.prior_closes.remove(index);
        }
        if index < self.tick_unbounded_print.len() {
            self.tick_unbounded_print.remove(index);
        }
        if index < self.tick_liquidity_share.len() {
            self.tick_liquidity_share.remove(index);
        }
        if index < self.base_fundamentals.len() {
            self.base_fundamentals.remove(index);
        }
        if index < self.base_shares.len() {
            self.base_shares.remove(index);
        }
        let leaving = self.companies.remove(index);
        self.index_rebase(index_before);
        // A host's write between sessions moves the night's path at once
        // (`night_session_steps`); nothing otherwise.
        self.futures_retarget();
        Some(leaving)
    }

    /// Find a company's index by id.
    ///
    /// Linear, because the roster is ~100 names and a map would be a second
    /// structure to keep in step with the ordering that actually matters.
    pub fn index_of(&self, id: &str) -> Option<usize> {
        self.companies.iter().position(|c| c.id == id)
    }

    /// Build the executable order book for one instrument, right now.
    ///
    /// # This is the book the tick itself settles through
    ///
    /// Not a display copy or an approximation of one. The same
    /// `build_live_book` call the price settlement uses, on the same state, so
    /// the depth a caller reads is the depth they would actually trade
    /// against. That is what makes slippage emergent: a large order pays worse
    /// prices because it consumed real levels, not because a coefficient said
    /// large orders cost more.
    ///
    /// Rebuilt per call rather than persisted, which is correct rather than
    /// merely convenient: a market maker re-quotes every tick anyway, so the
    /// book is a pure function of fair value, spread and resting orders. That
    /// keeps the tick pure and replay deterministic for free.
    ///
    /// Returns `None` for an out-of-range index.
    ///
    /// With any of the agent-facing book's dials on, or once an agent has
    /// used the book, this is that book instead
    /// (`crate::agent_book::agent_book`): the ladder less what agents have
    /// taken, the latent depth behind it, and every agent's resting orders.
    /// Otherwise it is the maker's ladder, built exactly as it always was.
    pub fn book_for(&self, index: usize) -> Option<crate::order_book::OrderBook> {
        // Listed contracts sit after the rate instruments, in listing order
        // (`futures_index_listed`); `Engine::contract_book` reads one by
        // symbol.
        if index >= self.instrument_count() {
            return self.contract_book_at(index - self.instrument_count());
        }
        // Rate instruments sit after the equities, so their index is the
        // equity count plus their place in the rate book.
        if index >= self.companies.len() {
            let j = index - self.companies.len();
            let inst = self.rates.instruments.get(j)?;
            // Around the live mark during a session under
            // `rate_intraday_live`, which is what the minute's print quotes.
            return Some(match self.rate_live_curve() {
                Some(c) => inst.book_at(inst.level_at(c.dy(inst.spec.point)), self.economy.vix),
                None => inst.book(self.economy.vix),
            });
        }
        if self.agent_view() {
            return self.agent_book_at(index, None);
        }
        let company = self.companies.get(index)?;
        Some(crate::microstructure::build_live_book(
            &company.micro_view(company.stock.price),
            &crate::microstructure::LiveBookOptions {
                spread_size_smoothness: 0.0,
                spread_size_exponent: crate::microstructure::SPREAD_SIZE_EXPONENT,
                vix: self.economy.vix,
                ..Default::default()
            },
        ))
    }

    /// Company ids, in roster order. The mapping every column is positional against.
    pub fn ids(&self) -> Vec<String> {
        self.companies.iter().map(|c| c.id.clone()).collect()
    }

    /// Company sectors, in roster order. Positional against [`Engine::ids`].
    ///
    /// Exists for the information-transfer channel: news naming a company
    /// has to be resolvable to that company's sector before its peers can be
    /// found, and a caller writing `News(ticker="AWS", price_impact=0.05)`
    /// should not have to restate the sector the roster already knows.
    pub fn sectors(&self) -> Vec<String> {
        self.companies.iter().map(|c| c.sector.clone()).collect()
    }

    /// Columnar read of one field, for the FFI boundary.
    ///
    /// A single contiguous `f64` buffer crosses a WASM boundary as one view;
    /// 108 marshalled objects do not. The `Vec` is built on demand rather than
    /// maintained, because the embedder reads far less often than the tick
    /// writes.
    /// Write one column back onto the roster.
    ///
    /// The inverse of [`Engine::column`], and the second half of a
    /// constant-time checkpoint: a saved market is its columns plus the
    /// generator position, and until now only the reading direction existed.
    ///
    /// # The match is exhaustive on purpose
    ///
    /// Every branch is spelled out with no wildcard, so adding a variant to
    /// [`PriceField`] fails to COMPILE until it is handled here. That is the
    /// difference between this and the "restore everything" method the
    /// docstring on `set_rng_state` warns against: the compiler keeps the two
    /// directions in step rather than a reviewer remembering to.
    ///
    /// # NaN means absent, matching the read side
    ///
    /// The optional fields round-trip through NaN in both directions. Writing
    /// a NaN clears the field rather than storing a NaN, so a column read out
    /// and written back reproduces the original `Option` exactly -- including
    /// the difference between "no mispricing yet" and "a mispricing of zero",
    /// which are different markets.
    ///
    /// Returns an error when the slice length does not match the roster.
    /// Silently writing a prefix would restore a market that was correct for
    /// the first N companies and stale for the rest.
    /// Write the three fair-value inputs back onto the roster.
    ///
    /// [`Engine::set_column`] covers the STOCK fields -- price, variance,
    /// inventory, the mispricing carry. These three live on the company
    /// rather than the stock, and until now nothing could set them after
    /// construction. That made them frozen for the life of an engine, which
    /// is fine for a batch sweep and wrong for an embedder whose companies
    /// report earnings: fair value is `eps * target_pe`, so a stale `eps` is
    /// a company valued on the fundamentals it had on day one, for ever.
    ///
    /// NaN clears the field, matching `set_column` and the columnar contract
    /// everywhere else -- zero is a real EPS (a company that broke exactly
    /// even) and cannot double as the absent marker. `None` here is not
    /// "unknown", it is what the valuation reads as "no earnings path", and
    /// the two must stay distinguishable.
    ///
    /// Consumes no draws, so it cannot move the generator: a caller may sync
    /// as often as it likes without changing the market's trajectory.
    ///
    /// Lengths are checked against the roster rather than truncated. Writing
    /// a prefix would leave a market correct for its first companies and
    /// stale for the rest -- the failure this method exists to end.
    pub fn set_fundamentals(
        &mut self,
        eps: &[f64],
        book_value_per_share: &[f64],
        revenue_growth: &[f64],
    ) -> Result<(), String> {
        let n = self.companies.len();
        for (name, len) in [
            ("eps", eps.len()),
            ("book_value_per_share", book_value_per_share.len()),
            ("revenue_growth", revenue_growth.len()),
        ] {
            if len != n {
                return Err(format!(
                    "{name} has {len} values for {n} companies"
                ));
            }
        }
        fn optional(v: f64) -> Option<f64> {
            if v.is_nan() {
                None
            } else {
                Some(v)
            }
        }
        for (i, company) in self.companies.iter_mut().enumerate() {
            company.eps = optional(eps[i]);
            company.book_value_per_share = optional(book_value_per_share[i]);
            company.revenue_growth = optional(revenue_growth[i]);
        }
        Ok(())
    }

    /// Write the two trading-status flags back onto the roster.
    ///
    /// The last per-company state an embedder could not update. A company that
    /// goes bankrupt or is taken private in the game kept trading here: the
    /// tick skips a company only when it reads `is_bankrupt || !is_public`
    /// (`market/tick.rs`), and the index excludes bankrupt constituents
    /// (`market/index_value.rs`). Without a setter, neither could ever become
    /// true after construction, so a failed company went on printing prices
    /// and went on counting toward the index.
    ///
    /// Narrower than [`Engine::set_fundamentals`] because it does not
    /// compound -- a stale `eps` misprices a company a little more every
    /// quarter, while a stale flag is wrong from the moment it changes and no
    /// worse afterwards. Both are wrong, so both are settable.
    ///
    /// `&[bool]` rather than the f64 columnar convention used elsewhere,
    /// deliberately: there is no "absent" flag for NaN to mean, and a boolean
    /// carried as a float invites a caller to pass 0.5.
    ///
    /// Consumes no draws.
    pub fn set_status(
        &mut self,
        is_bankrupt: &[bool],
        is_public: &[bool],
    ) -> Result<(), String> {
        let n = self.companies.len();
        for (name, len) in [
            ("is_bankrupt", is_bankrupt.len()),
            ("is_public", is_public.len()),
        ] {
            if len != n {
                return Err(format!("{name} has {len} values for {n} companies"));
            }
        }
        // A bankruptcy or a name taken private leaves the index at its last
        // price, which the divisor keeps (`index_level_listed`).
        let index_before = self.index_before_change();
        for (i, company) in self.companies.iter_mut().enumerate() {
            company.is_bankrupt = is_bankrupt[i];
            company.is_public = is_public[i];
        }
        self.index_rebase(index_before);
        // A host's write between sessions moves the night's path at once
        // (`night_session_steps`); nothing otherwise.
        self.futures_retarget();
        Ok(())
    }

    /// The two trading-status flags, in roster order.
    pub fn status(&self) -> (Vec<bool>, Vec<bool>) {
        (
            self.companies.iter().map(|c| c.is_bankrupt).collect(),
            self.companies.iter().map(|c| c.is_public).collect(),
        )
    }

    /// The three fair-value inputs, NaN where absent.
    ///
    /// The read side of [`Engine::set_fundamentals`], so a caller can check
    /// what the engine is actually valuing on rather than assuming its own
    /// copy is what arrived.
    pub fn fundamentals(&self) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        let nan = f64::NAN;
        (
            self.companies.iter().map(|c| c.eps.unwrap_or(nan)).collect(),
            self.companies
                .iter()
                .map(|c| c.book_value_per_share.unwrap_or(nan))
                .collect(),
            self.companies
                .iter()
                .map(|c| c.revenue_growth.unwrap_or(nan))
                .collect(),
        )
    }

    /// Whether any company's fair-value inputs differ from the ones it was
    /// built or listed with, to the bit. What decides whether a snapshot
    /// carries them and whether the state hash covers them, so an engine
    /// `set_fundamentals` never moved snapshots and hashes as it did before
    /// they were carried.
    pub fn fundamentals_changed(&self) -> bool {
        fn bits(v: Option<f64>) -> Option<u64> {
            v.map(f64::to_bits)
        }
        self.companies.len() != self.base_fundamentals.len()
            || self.companies.iter().zip(&self.base_fundamentals).any(|(c, b)| {
                bits(c.eps) != bits(b[0])
                    || bits(c.book_value_per_share) != bits(b[1])
                    || bits(c.revenue_growth) != bits(b[2])
            })
    }

    /// Put every company's fair-value inputs back to the ones it was built
    /// or listed with. What a restore does when the snapshot carries none:
    /// such a snapshot was taken on an engine whose inputs had not moved.
    pub fn reset_fundamentals(&mut self) {
        for (c, b) in self.companies.iter_mut().zip(&self.base_fundamentals) {
            c.eps = b[0];
            c.book_value_per_share = b[1];
            c.revenue_growth = b[2];
        }
    }

    /// Write each company's share count, for a host whose companies buy
    /// back stock or issue it (#274).
    ///
    /// Without it the count was the one each company was built or listed
    /// with for the life of the engine, and every tick sets the market cap
    /// to `price * shares_outstanding`, so a host that wrote `MarketCap`
    /// saw it overwritten at the next print. Everything the engine weights
    /// by its own capitalisation -- the market factor's loadings, the
    /// roster beta normalisation, the cap-weighted market P/E, the index
    /// variance -- read the opening counts.
    ///
    /// Each market cap follows at once, at the price standing. The listed
    /// index (`index_level_listed`) keeps its level: the divisor is reset
    /// across the write, as it is across a listing, a delisting or a change
    /// of status, so a buyback is not a fall in the index. The float is the
    /// host's to keep in step (`set_column(FloatShares, ..)`); it is not
    /// moved here.
    ///
    /// Every count must be finite and above 0, and the length must be the
    /// roster's, on the same rule as [`Engine::set_fundamentals`]. A
    /// refused write changes nothing. Consumes no draws.
    pub fn set_shares_outstanding(&mut self, shares: &[f64]) -> Result<(), String> {
        let n = self.companies.len();
        if shares.len() != n {
            return Err(format!("shares_outstanding has {} values for {n} companies", shares.len()));
        }
        if let Some((i, v)) = shares.iter().enumerate().find(|(_, v)| !(v.is_finite() && **v > 0.0)) {
            return Err(format!(
                "shares_outstanding[{i}] ({}) is {v}; a share count is finite and above 0",
                self.companies[i].id
            ));
        }
        let index_before = self.index_before_change();
        for (company, &count) in self.companies.iter_mut().zip(shares) {
            company.stock.shares_outstanding = count;
            company.stock.market_cap = company.stock.price * count;
        }
        self.index_rebase(index_before);
        // A host's write between sessions moves the night's path at once
        // (`night_session_steps`); nothing otherwise.
        self.futures_retarget();
        Ok(())
    }

    /// Each company's share count, in roster order.
    pub fn shares_outstanding(&self) -> Vec<f64> {
        self.companies.iter().map(|c| c.stock.shares_outstanding).collect()
    }

    /// Whether any company's share count differs from the one it was built
    /// or listed with, to the bit. What decides whether a snapshot carries
    /// the counts and whether the state hash covers them, so an engine
    /// `set_shares_outstanding` never moved snapshots and hashes as it did
    /// before they were carried.
    pub fn shares_outstanding_changed(&self) -> bool {
        self.companies.len() != self.base_shares.len()
            || self
                .companies
                .iter()
                .zip(&self.base_shares)
                .any(|(c, b)| c.stock.shares_outstanding.to_bits() != b.to_bits())
    }

    /// Put every company's share count back to the one it was built or
    /// listed with. What a restore does when the snapshot carries none.
    /// Moves nothing else: the market caps and the index divisor a restore
    /// writes come from the snapshot.
    pub fn reset_shares_outstanding(&mut self) {
        for (c, b) in self.companies.iter_mut().zip(&self.base_shares) {
            c.stock.shares_outstanding = *b;
        }
    }

    /// Write the share counts a snapshot carries, raw: no market cap is
    /// recomputed and the index divisor is not reset, because a restore
    /// writes both from the snapshot. The length must be the roster's.
    pub fn restore_shares_outstanding(&mut self, shares: &[f64]) -> Result<(), String> {
        if shares.len() != self.companies.len() {
            return Err(format!(
                "this snapshot carries {} share counts and the roster holds {} \
                 companies. They are positional against the roster, so this \
                 restore is refused rather than padded or truncated.",
                shares.len(),
                self.companies.len()
            ));
        }
        if let Some((i, v)) = shares.iter().enumerate().find(|(_, v)| !(v.is_finite() && **v > 0.0)) {
            return Err(format!(
                "this snapshot's shares_outstanding[{i}] is {v}; a share count is \
                 finite and above 0"
            ));
        }
        for (c, &v) in self.companies.iter_mut().zip(shares) {
            c.stock.shares_outstanding = v;
        }
        Ok(())
    }

    pub fn set_column(&mut self, field: PriceField, values: &[f64]) -> Result<(), String> {
        if values.len() != self.companies.len() {
            return Err(format!(
                "expected {} values for {} companies, got {}",
                self.companies.len(),
                self.companies.len(),
                values.len()
            ));
        }
        // NaN in, `None` out. Zero is a real value for every optional field
        // here, so it cannot double as the absent marker.
        fn optional(v: f64) -> Option<f64> {
            if v.is_nan() {
                None
            } else {
                Some(v)
            }
        }
        for (company, &value) in self.companies.iter_mut().zip(values) {
            let stock = &mut company.stock;
            match field {
                PriceField::Price => stock.price = value,
                PriceField::PreviousClose => stock.previous_close = value,
                PriceField::Open => stock.open = value,
                PriceField::High => stock.high = value,
                PriceField::Low => stock.low = value,
                PriceField::Volume => stock.volume = value,
                PriceField::MarketCap => stock.market_cap = value,
                PriceField::MispricingS => stock.mispricing_s = optional(value),
                PriceField::MakerInventory => stock.maker_inventory = optional(value),
                PriceField::GarchVariance => stock.garch_variance = value,
                PriceField::PreviousTickPrice => {
                    stock.previous_tick_price = optional(value)
                }
                PriceField::MispricingSPrevClose => {
                    stock.mispricing_s_prev_close = optional(value)
                }
                PriceField::MispricingMomentum => {
                    stock.mispricing_momentum = optional(value)
                }
                PriceField::LastDailyReturn => stock.last_daily_return = optional(value),
                PriceField::AvgVolume => stock.avg_volume = value,
                PriceField::Beta => stock.beta = optional(value),
                PriceField::ShortInterest => stock.short_interest = value,
                PriceField::FloatShares => stock.float = value,
            }
        }
        Ok(())
    }

    /// One field for every company, in roster order. A field a company does
    /// not have yet (a return before its first close, say) is NaN.
    pub fn column(&self, field: PriceField) -> Vec<f64> {
        self.companies
            .iter()
            .map(|c| match field {
                PriceField::Price => c.stock.price,
                PriceField::PreviousClose => c.stock.previous_close,
                PriceField::Open => c.stock.open,
                PriceField::High => c.stock.high,
                PriceField::Low => c.stock.low,
                PriceField::Volume => c.stock.volume,
                PriceField::MarketCap => c.stock.market_cap,
                PriceField::MispricingS => c.stock.mispricing_s.unwrap_or(f64::NAN),
                PriceField::MakerInventory => c.stock.maker_inventory.unwrap_or(f64::NAN),
                PriceField::GarchVariance => c.stock.garch_variance,
                PriceField::PreviousTickPrice => {
                    c.stock.previous_tick_price.unwrap_or(f64::NAN)
                }
                PriceField::MispricingSPrevClose => {
                    c.stock.mispricing_s_prev_close.unwrap_or(f64::NAN)
                }
                PriceField::MispricingMomentum => {
                    c.stock.mispricing_momentum.unwrap_or(f64::NAN)
                }
                PriceField::LastDailyReturn => c.stock.last_daily_return.unwrap_or(f64::NAN),
                PriceField::AvgVolume => c.stock.avg_volume,
                PriceField::Beta => c.stock.beta.unwrap_or(f64::NAN),
                PriceField::ShortInterest => c.stock.short_interest,
                PriceField::FloatShares => c.stock.float,
            })
            .collect()
    }

    /// Every company's current price, in roster order. Rate indices are not
    /// in it. A copy, so it does not follow later ticks.
    pub fn prices(&self) -> Vec<f64> {
        self.column(PriceField::Price)
    }

    /// Overwrite prices from a columnar buffer.
    ///
    /// For the embedder to apply an effect this engine does not model — an
    /// earnings gap, a corporate action. It does NOT recompute `s`, so a
    /// caller changing the price must also decide what that means for the
    /// mispricing, exactly as the reference implementation's earnings path does.
    pub fn write_prices(&mut self, prices: &[f64]) {
        assert_eq!(prices.len(), self.companies.len(), "one price per company");
        for (company, &price) in self.companies.iter_mut().zip(prices) {
            company.stock.price = price;
            company.stock.market_cap = price * company.stock.shares_outstanding;
        }
    }

    /// This engine's state as one 32-byte digest: the per-day ledger leaf.
    ///
    /// # What it measures
    ///
    /// sha256 over every field `state_snapshot` carries, in one fixed order,
    /// with one encoding per type. Two engines whose hashes agree hold the
    /// same market state to the bit: the same columns, the same generator
    /// positions, the same day accumulators, the same macro chain and the
    /// same central bank. `market_digest` covers nine columns and the draw
    /// count; this covers the macro chain and the generators as well, which
    /// is what a day-by-day ledger needs, because two runs can agree on
    /// today's prices and hold different state for tomorrow's.
    ///
    /// # What it cannot say
    ///
    /// It is a hash of STATE, so it says nothing about history: the order
    /// log and the recorded tape are outside it, exactly as they are
    /// outside `state_snapshot`. Two engines that reached
    /// the same state by different routes hash the same, which is what makes
    /// a replayed day checkable against a recorded one at all.
    ///
    /// # Why the two arguments
    ///
    /// The snapshot carries `market_open` and `day_count` and this struct
    /// holds neither: the session flag and the day counter live in the
    /// binding, which passes the day into `close_day`. The caller supplies
    /// them so the digest covers the whole snapshot, rather than this method
    /// hashing a subset of it and calling that the state.
    ///
    /// # The encoding
    ///
    /// Every float is eight bytes big-endian with `0x7ff8000000000000` for
    /// any NaN, the rule `manifest._f64` and `tests/known_answer.py` share,
    /// so neither a decimal formatter nor a platform's NaN payload can reach
    /// the digest.
    ///
    /// The generator states are the one exception and it is load-bearing. A
    /// PCG32 state is a `u64`, and the snapshot carries it as an `f64` bit
    /// pattern because a `u64` does not survive a Python float. About one
    /// `u64` in a thousand reads as a NaN, so canonicalising those would map
    /// distinct generator positions onto one digest and a tampered position
    /// would verify. They are hashed as raw big-endian `u64` bit patterns.
    ///
    /// A string is length-prefixed, `u32` big-endian then UTF-8, so "AB"
    /// followed by "C" cannot hash as "A" followed by "BC". A bool is one
    /// byte. An `Option` is a presence byte and then the value when present.
    /// `pending_jump` and `pending_overnight` are the day's boundary moves
    /// waiting for the tape row that carries them. They live on the Python
    /// wrapper rather than here, because they are recording state, and they
    /// are passed IN rather than left out: a snapshot carries them, and
    /// `manifest.state_hash` -- the twin this must agree with byte for byte
    /// -- refuses a snapshot holding a field it does not hash. Two states
    /// alike in every column and holding different pending jumps write
    /// different tapes tomorrow, so calling them equal would overclaim.
    ///
    /// Empty slices for a caller that has none, which is every core-only
    /// caller and every test below.
    pub fn state_hash(&self, day_count: u32, market_open: bool) -> [u8; 32] {
        self.state_hash_with_pending(day_count, market_open, &[], &[], &[], &[])
    }

    /// [`Engine::state_hash`], carrying the wrapper's pending tape state.
    pub fn state_hash_with_pending(
        &self,
        day_count: u32,
        market_open: bool,
        pending_jump: &[f64],
        pending_overnight: &[f64],
        pending_fair_value: &[f64],
        pending_dividend: &[f64],
    ) -> [u8; 32] {
        use sha2::{Digest, Sha256};

        let n = self.companies.len();
        let mut buf: Vec<u8> = Vec::new();

        // The eighteen columns, instrument by instrument. Read once each
        // rather than per instrument, because `column` builds a Vec.
        let columns: Vec<Vec<f64>> = STATE_HASH_COLUMNS
            .iter()
            .map(|field| self.column(*field))
            .collect();
        for i in 0..n {
            for column in &columns {
                hash_f64(&mut buf, column[i]);
            }
        }

        // The ten generator states, in the order the snapshot's flat `rng`
        // array carries them. Raw bit patterns, per the encoding note above.
        let rng = self.rng_state();
        for state in [
            rng.market, rng.economy, rng.external, rng.jumps, rng.volume,
            rng.news, rng.volume_idio, rng.overnight, rng.market_vol_level,
            rng.crisis_epicentre,
        ] {
            hash_u64(&mut buf, state.state);
            hash_u64(&mut buf, state.increment);
            hash_bits(&mut buf, state.spare.unwrap_or(f64::NAN));
        }

        // The roster and the model, which the snapshot carries as its two
        // identity guards. Hashed because a leaf that ignored them would let
        // a day taken on another roster, or under another preset, verify as
        // this one: `restore_state` refuses both, and a ledger written
        // outside it would not.
        hash_u32(&mut buf, n as u32);
        for id in self.ids() {
            hash_str(&mut buf, &id);
        }
        hash_str(&mut buf, self.model_fingerprint());

        // The day accumulators, in snapshot order. The fair-value shift's
        // slot, the last in both, follows the rest and only on an engine that
        // carries fair-value offsets, the one place it can be non-zero, so
        // every engine without them hashes as it did before the slot.
        // Also wherever a slot is non-zero, so an edited slot on an engine
        // without offsets is still covered.
        let fv_hashed = self.carries_fair_value_offsets()
            || self.attribution.iter().any(|r| r[crate::market::factors::FAIR_VALUE_SLOT] != 0.0)
            || self.tick_components.iter().any(|r| r[crate::market::factors::TICK_FAIR_VALUE] != 0.0);
        for row in &self.attribution {
            for value in &row[..crate::market::factors::FAIR_VALUE_SLOT] {
                hash_f64(&mut buf, *value);
            }
        }
        for row in &self.tick_components {
            for value in &row[..crate::market::factors::TICK_FAIR_VALUE] {
                hash_f64(&mut buf, *value);
            }
        }
        if fv_hashed {
            for row in &self.attribution {
                hash_f64(&mut buf, row[crate::market::factors::FAIR_VALUE_SLOT]);
            }
            for row in &self.tick_components {
                hash_f64(&mut buf, row[crate::market::factors::TICK_FAIR_VALUE]);
            }
        }
        // The dividend's slot, after the attribution's last, on the same
        // rule: only on a model that pays dividends or where it is non-zero,
        // so every other engine hashes as it did before the slot.
        if self.carries_dividends()
            || self.attribution.iter().any(|r| r[crate::market::factors::DIVIDEND_SLOT] != 0.0)
        {
            for row in &self.attribution {
                hash_f64(&mut buf, row[crate::market::factors::DIVIDEND_SLOT]);
            }
        }
        for value in &self.tick_fundamental {
            hash_f64(&mut buf, *value);
        }
        for value in &self.tick_anchor {
            hash_f64(&mut buf, *value);
        }
        // The day's noise split and the scale its idiosyncratic part was
        // drawn at, hashed beside the accumulators above and for their
        // reason: off zero on `garch_innovation_commensurate` they decide
        // the innovation tonight's close feeds the per-name GJR, so two
        // engines alike in every column and holding different splits close
        // the day differently.
        for row in &self.noise_parts {
            for value in row {
                hash_f64(&mut buf, *value);
            }
        }
        for value in &self.noise_own_scale2 {
            hash_f64(&mut buf, *value);
        }
        // The day's GJR sum under a night split, the only engines that keep
        // one, so every other engine hashes as it did.
        if !self.innovation_day.is_empty() {
            hash_u32(&mut buf, self.innovation_day.len() as u32);
            for value in &self.innovation_day {
                hash_f64(&mut buf, *value);
            }
        }
        // The jump waiting to be traded in, which the volume scale reads on
        // the session after the close that booked it.
        for value in &self.jump_move {
            hash_f64(&mut buf, *value);
        }
        hash_bool(&mut buf, market_open);

        // The engine-level dials.
        let (variance, day_factor, fast_variance, slow_variance,
             prev_day_factor, smoothed_vix) = self.market_variance_state();
        for value in [variance, day_factor, fast_variance, slow_variance,
                      prev_day_factor, smoothed_vix] {
            hash_f64(&mut buf, value);
        }
        hash_f64(&mut buf, self.volume_state);
        for value in &self.volume_idio {
            hash_f64(&mut buf, *value);
        }
        // The two states the composed vector turned on. LENGTH-PREFIXED, the
        // sector one because it follows the sector table rather than the
        // roster and the name one because it is empty on an engine built
        // with no companies -- the same reason the pending buffers below
        // carry their lengths.
        //
        // Unhashed, these let a resumed day verify against a leaf it should
        // not: two engines alike in every column and holding different
        // sector variance revert to different sector targets tonight, and
        // `test_sampled_verification` failed on exactly that.
        hash_u32(&mut buf, self.sector_variance.len() as u32);
        for value in &self.sector_variance {
            hash_f64(&mut buf, *value);
        }
        hash_u32(&mut buf, self.jump_excitation.len() as u32);
        for value in &self.jump_excitation {
            hash_f64(&mut buf, *value);
        }
        // The sector state's two per-DAY companions, hashed for the reason
        // `attribution` and `tick_components` above are: they are zero at a
        // day boundary and they are the day's accumulated sector factor and
        // the scale it was drawn at MID-DAY, which is where a fork is taken.
        hash_u32(&mut buf, self.sector_day_factor.len() as u32);
        for value in &self.sector_day_factor {
            hash_f64(&mut buf, *value);
        }
        hash_f64(&mut buf, self.sector_target_day);
        hash_f64(&mut buf, self.universe_stress);
        hash_f64(&mut buf, self.forced_flow_spent);
        hash_f64(&mut buf, self.market_vol_log_level);
        hash_f64(&mut buf, self.vix_log_level);
        // Only when it can move, so every preset's state hash is the one it
        // was before the field existed.
        if self.params.vix_anchor_memory != 0.0 {
            hash_f64(&mut buf, self.vix_anchor_slow);
        }
        // The market factor's return memory, on the same rule.
        if self.carries_market_vol_leverage() {
            hash_f64(&mut buf, self.market_vol.leverage_memory());
        }
        // The fast return memory, on the same rule.
        if self.carries_market_vol_leverage_fast() {
            hash_f64(&mut buf, self.market_vol.leverage_fast_memory());
        }
        // The cycle's volatility multiplier, on the same rule: only with
        // `market_vol_cycle_ratio` set, and once a close has set it.
        if self.params.market_vol_cycle_ratio != 0.0 {
            if let Some(l) = self.market_vol_cycle_log {
                hash_f64(&mut buf, l);
            }
        }
        // The published VIX's stress memory, on the same rule.
        if self.carries_vix_stress_memory() {
            hash_f64(&mut buf, self.vix_stress_memory);
        }
        // The aggregate earnings cycle, on the same rule.
        if self.params.earnings_cycle_depth != 0.0 {
            hash_f64(&mut buf, self.economy.earnings_cycle);
        }
        // The volatility feedback's smoothed exposure, on the same rule.
        if self.carries_vix_feedback() {
            hash_f64(&mut buf, self.economy.vix_feedback);
        }
        // The accrued buyback share-count reductions, on the same rule.
        if self.carries_buyback_log_shares() {
            for c in &self.companies {
                hash_f64(&mut buf, c.stock.buyback_log_shares.unwrap_or(0.0));
            }
        }
        // The earnings calendar's key, on the same rule: only with the
        // calendar on, so every other engine hashes as it did.
        if self.carries_earnings() {
            hash_u64(&mut buf, self.earnings_key);
        }
        // What names hold back of the earnings cycle, on the same rule,
        // LENGTH-PREFIXED like the other per-name states added after the
        // roster columns.
        if self.carries_earnings_withheld() {
            hash_u32(&mut buf, self.earnings_withheld.len() as u32);
            for value in &self.earnings_withheld {
                hash_f64(&mut buf, *value);
            }
        }
        // Tonight's market draw, on the same rule: only while the session's
        // live lagged wire reads it.
        if self.carries_night_market_factor() {
            hash_f64(&mut buf, self.night_market_factor);
        }
        // The Fed put's four fields, on the same rule.
        if self.carries_fed_put() {
            hash_f64(&mut buf, self.economy.intermeeting_return);
            hash_f64(&mut buf, self.economy.fed_put);
            hash_f64(&mut buf, self.economy.fed_put_owed);
            hash_f64(&mut buf, self.economy.fed_put_mcap_prev);
        }
        // Credit's leverage gap, on the same rule: only with
        // `corporate_spread_equity_gain` set.
        if self.carries_spread_equity_gap() {
            hash_f64(&mut buf, self.economy.spread_equity_gap);
        }
        // The gap credit has priced, only with `corporate_spread_equity_lag`
        // and the gain set.
        if self.carries_spread_priced_gap() {
            hash_f64(&mut buf, self.economy.spread_priced_gap);
        }
        // The gap the 10-year has priced, only with `treasury_equity_lag` and
        // its gain set.
        if self.carries_treasury_equity_lag() {
            hash_f64(&mut buf, self.economy.treasury_equity_priced_gap);
        }
        // The fair-value levels and the unspent opening draws, on the same
        // rule: only when a dial can move them.
        if self.carries_fair_value_offsets() {
            for c in &self.companies {
                hash_f64(&mut buf, c.stock.fair_value_offset.unwrap_or(0.0));
            }
            hash_u32(&mut buf, self.opening_z.len() as u32);
            for value in &self.opening_z {
                hash_f64(&mut buf, *value);
            }
            // The prehistory's carried opening, only while one waits: empty
            // on every shipped preset, so their hashes are the ones they were.
            if !self.opening_carry.is_empty() {
                hash_u32(&mut buf, self.opening_carry.len() as u32);
                for value in &self.opening_carry {
                    hash_f64(&mut buf, *value);
                }
            }
        }
        // The dividend states, on the same rule: only on a model that pays
        // them. NaN (one pattern) for a name without one.
        if self.carries_dividends() {
            for value in self.dividend_states() {
                hash_f64(&mut buf, value);
            }
        }
        // The per-name idiosyncratic variance state, only while it runs, so
        // every preset hashes as it did. Length-prefixed, each vector.
        if self.carries_idio_vol_state() {
            for v in [&self.idio_variance, &self.idio_jump_pending, &self.idio_jump_var_pending] {
                hash_u32(&mut buf, v.len() as u32);
                for value in v {
                    hash_f64(&mut buf, *value);
                }
            }
        }
        // The crisis episode. Hashed for the reason every field here is:
        // two engines alike in every column, one of them three sessions
        // into a financial-services episode and the other not in an episode
        // at all, price tomorrow differently. The pin goes in beside the
        // state because it is what the NEXT episode will be drawn -- or not
        // drawn -- from, which is the same class of divergence one session
        // later. `-2` for an absent pin, which is no sector index and not
        // the `-1` that means `none`.
        hash_bool(&mut buf, self.crisis_in_episode);
        hash_f64(&mut buf, self.crisis_sessions_under as f64);
        hash_f64(&mut buf, self.crisis_epicentre as f64);
        hash_f64(&mut buf, self.crisis_epicentre_pin.unwrap_or(-2) as f64);
        // A forced close pending tonight. Only while true, so every engine
        // that was never forced hashes as it did before the mark existed.
        if self.vix_sets_variance_pending {
            hash_bool(&mut buf, true);
        }
        // Today's pins, likewise only while any is set, behind a tag so the
        // marks cannot hash as the forced close's.
        if self.macro_pins_today != 0 {
            hash_f64(&mut buf, 7.0);
            hash_f64(&mut buf, self.macro_pins_today as f64);
            if self.macro_pins_today & PIN_SPREAD != 0 {
                hash_f64(&mut buf, self.pinned_corporate_spread);
            }
        }
        // The central bank's stress level, only while `fed_stress_cut` is
        // set, behind its own tag.
        if let Some(level) = self.stress_vix_max() {
            hash_f64(&mut buf, 8.0);
            hash_f64(&mut buf, level);
        }
        // The stress hold's clock and the priced path's forecast, each only
        // while its dial is set, behind its own tag.
        if let Some(age) = self.stress_hold_age() {
            hash_f64(&mut buf, 10.0);
            hash_f64(&mut buf, age);
        }
        if let Some(path) = self.policy_path() {
            hash_f64(&mut buf, 11.0);
            hash_f64(&mut buf, path);
        }
        // The drawdown hold's window and base, only while `fed_drawdown_hold`
        // is set, behind its own tag and length-prefixed.
        if let Some((returns, prev)) = self.drawdown_state() {
            hash_f64(&mut buf, 32.0);
            hash_f64(&mut buf, returns.len() as f64);
            for r in returns {
                hash_f64(&mut buf, r);
            }
            hash_f64(&mut buf, prev);
        }
        // What the curve prices of the next meeting, only while
        // `policy_anticipation` is set, behind its own tag.
        if let Some(priced) = self.policy_anticipation_priced() {
            hash_f64(&mut buf, 31.0);
            hash_f64(&mut buf, priced);
        }
        // The rate indices' live mark, only while `rate_intraday_live` is set
        // and a session holds one, behind its own tag.
        if let Some(marks) = self.rate_live_marks() {
            hash_f64(&mut buf, 9.0);
            for value in marks {
                hash_f64(&mut buf, value);
            }
        }
        // Today's priced VIX move, only while a pin has made one
        // (`pinned_vix_variance_share`), behind its own tag.
        if self.pinned_vix_jump != 0.0 {
            hash_f64(&mut buf, 10.0);
            hash_f64(&mut buf, self.pinned_vix_jump);
        }
        // Today's conditional move and the draws' mean (`pinned_vix_return`),
        // only while a pin has made one, behind their own tags.
        if self.pinned_vix_move != 0.0 {
            hash_f64(&mut buf, 94.0);
            hash_f64(&mut buf, self.pinned_vix_move);
        }
        if self.pinned_vix_mean != 0.0 {
            hash_f64(&mut buf, 95.0);
            hash_f64(&mut buf, self.pinned_vix_mean);
        }
        // The day's market t scale (`market_day_tail_df`), only between an
        // open that drew one and the close, behind its own tag.
        if self.market_day_scale != 1.0 {
            hash_f64(&mut buf, 12.0);
            hash_f64(&mut buf, self.market_day_scale);
        }
        // The price index's divisor and close level, only while
        // `index_level_listed` is set, behind its own tag.
        if let Some([divisor, close]) = self.index_state() {
            hash_f64(&mut buf, 41.0);
            hash_f64(&mut buf, divisor);
            hash_f64(&mut buf, close);
        }
        // The live VIX's projection, only while `vix_intraday_live` is set and
        // a session holds one, behind its own tag.
        if let Some(mark) = self.vix_live_mark() {
            hash_f64(&mut buf, 42.0);
            hash_f64(&mut buf, mark);
        }
        // The forecast, only while `forecast_horizon_sessions` is set and a
        // close has computed one, behind its own tag and length-prefixed.
        if let Some(forecast) = self.forecast() {
            let words = forecast.to_words();
            hash_f64(&mut buf, 43.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The index futures, only while `futures_index_listed` is set, behind
        // their own tag: their generator, then their numbers,
        // length-prefixed; their book behind another tag once an agent has
        // traded a contract; the night's path behind a third while one is
        // walked (`night_session_steps`).
        if let (Some(rng), Some(words)) = (self.derivatives_rng_state(), self.futures_words()) {
            hash_f64(&mut buf, 44.0);
            hash_u64(&mut buf, rng.state);
            hash_u64(&mut buf, rng.increment);
            hash_bits(&mut buf, rng.spare.unwrap_or(f64::NAN));
            hash_f64(&mut buf, rng.uniforms as f64);
            hash_f64(&mut buf, rng.normals as f64);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.futures_book_state() {
            hash_f64(&mut buf, 45.0);
            hash_book(&mut buf, book);
        }
        if let Some(words) = self.night_bridge_words() {
            hash_f64(&mut buf, 46.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The VIX's fear memory, only while `vix_fear_uptake` is set, behind
        // its own tag.
        if self.carries_vix_fear() {
            hash_f64(&mut buf, 47.0);
            hash_f64(&mut buf, self.vix_fear);
        }
        // The published VIX's log average, only while
        // `forecast_vix_level_nowcast` is set, behind its own tag.
        if self.carries_vix_log_average() {
            hash_f64(&mut buf, 93.0);
            hash_f64(&mut buf, self.vix_log_average);
        }
        // The VIX futures, only while `futures_vix_listed` is set, behind
        // their own tag, length-prefixed; their book behind another once an
        // agent has traded one.
        if let Some(words) = self.vix_futures_words() {
            hash_f64(&mut buf, 48.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.vix_futures_book_state() {
            hash_f64(&mut buf, 49.0);
            hash_book(&mut buf, book);
        }
        // The rate futures, only while `futures_rates_listed` is set, behind
        // their own tag, length-prefixed; their book behind another once an
        // agent has traded one.
        if let Some(words) = self.rate_futures_words() {
            hash_f64(&mut buf, 50.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.rate_futures_book_state() {
            hash_f64(&mut buf, 51.0);
            hash_book(&mut buf, book);
        }
        // The oil futures, only while `futures_oil_listed` is set, behind
        // their own tag, length-prefixed; their book behind another once an
        // agent has traded one.
        if let Some(words) = self.oil_futures_words() {
            hash_f64(&mut buf, 52.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.oil_futures_book_state() {
            hash_f64(&mut buf, 53.0);
            hash_book(&mut buf, book);
        }
        // The contracts' margin, only while `margin_scan_coverage` is set,
        // behind its own tag, length-prefixed.
        if let Some(words) = self.margin_words() {
            hash_f64(&mut buf, 54.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // Oil's long factor, only while `oil_target_drift_sd` is set, behind
        // its own tag: its log level and its key's two words.
        if let Some(words) = self.oil_drift_words() {
            hash_f64(&mut buf, 55.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // Core inflation's trend, only while `core_trend_sd` is set, behind
        // its own tag (57: 56 is the r* process's on ptv22/rstar-process).
        if let Some(words) = self.core_trend_words() {
            hash_f64(&mut buf, 57.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The eve's decision, only while `fed_eve_decision` is set, behind its
        // own tag.
        if let Some(words) = self.eve_decision_words() {
            hash_f64(&mut buf, 58.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // Credit's own state, only while `corporate_spread_noise_sd` is set,
        // behind its own tag.
        if let Some(words) = self.spread_noise_words() {
            hash_f64(&mut buf, 59.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The 10-year's own term premium, only while
        // `treasury_term_premium_sd` is set, behind its own tag.
        if let Some(words) = self.term_premium_words() {
            hash_f64(&mut buf, 60.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The aggregate earnings level, only while
        // `fair_value_aggregate_jensen` or `fair_value_aggregate_half_life`
        // is set, behind its own tag.
        if let Some(words) = self.aggregate_level_words() {
            hash_f64(&mut buf, 62.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The session's expected variance of the aggregate earnings level,
        // only while `fair_value_aggregate_expected` and
        // `fair_value_aggregate_jensen` are set, behind its own tag.
        if let Some(words) = self.aggregate_variance_words() {
            hash_f64(&mut buf, 61.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The earnings look-through's written level and anchor, only while
        // `earnings_lookthrough_half_life` is set, behind its own tag.
        if let Some(words) = self.earnings_lookthrough_words() {
            hash_f64(&mut buf, 65.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The time-held fade's clock and the held VIX, only while
        // `market_vol_cycle_hold_onset` is set and a close has started the
        // clock, behind its own tag.
        if let Some(words) = self.cycle_hold_words() {
            hash_f64(&mut buf, 84.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The index options and the surface's clock, only while
        // `surface_ssvi` is set, behind their own tag, length-prefixed; their
        // book behind another once an agent has traded an option.
        if let Some(words) = self.options_words() {
            hash_f64(&mut buf, 80.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.options_book_state() {
            hash_f64(&mut buf, 81.0);
            hash_book(&mut buf, book);
        }
        // The skew premium's noise and its key, only while
        // `surface_skew_noise_sd` is set, behind their own tag.
        if let Some(words) = self.skew_noise_words() {
            hash_f64(&mut buf, 82.0);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        // The single-name options, only while `options_names_listed` is
        // set, behind their own tag (70 to 72: 55 to 60 are taken on other
        // pt-v22 branches), length-prefixed; their book behind
        // another once an agent has traded one; their instructions and
        // exercises behind a third once there are any.
        if let Some(words) = self.name_options_words() {
            hash_f64(&mut buf, 70.0);
            hash_u32(&mut buf, words.len() as u32);
            for value in words {
                hash_f64(&mut buf, value);
            }
        }
        if let Some(book) = self.name_options_book_state() {
            hash_f64(&mut buf, 71.0);
            hash_book(&mut buf, book);
        }
        if let Some((instructions, events)) = self.name_options_exercise_state() {
            hash_f64(&mut buf, 72.0);
            hash_name_exercise(&mut buf, &instructions, events);
        }
        // LENGTH-PREFIXED, because these two are EMPTY between the tape row
        // that consumes them and the close that fills them again, where
        // every per-slot array above always follows the roster. An empty
        // buffer and a roster-length one of zeros are different states.
        for pending in [pending_jump, pending_overnight] {
            hash_u32(&mut buf, pending.len() as u32);
            for value in pending {
                hash_f64(&mut buf, *value);
            }
        }
        // The jump's fair-value shift waiting for its tape row, beside the
        // jump. Only while non-empty, which only a preset with fair-value
        // offsets can make it, so every other engine hashes as it did.
        if !pending_fair_value.is_empty() {
            hash_u32(&mut buf, pending_fair_value.len() as u32);
            for value in pending_fair_value {
                hash_f64(&mut buf, *value);
            }
        }
        // The ex-date's move in `s` waiting for its tape row, on the same
        // rule: only while non-empty, which only a model paying dividends
        // can make it.
        if !pending_dividend.is_empty() {
            hash_u32(&mut buf, pending_dividend.len() as u32);
            for value in pending_dividend {
                hash_f64(&mut buf, *value);
            }
        }
        // The growth term's base, in snapshot order. A constant of the run
        // rather than state that advances, and covered for the reason
        // every other field here is: two engines that agree on today's
        // prices and hold different bases price tomorrow differently.
        hash_f64(&mut buf, self.nominal_output_base);

        // The day's endogenous news. Generated at `open_market` and cleared
        // at the next one, so a close-boundary leaf carries the day's events
        // rather than an empty list.
        hash_u32(&mut buf, self.session_news.len() as u32);
        for event in &self.session_news {
            hash_opt_str(&mut buf, event.company_id.as_deref());
            hash_opt_str(&mut buf, event.sector.as_deref());
            hash_opt_f64(&mut buf, event.price_impact);
        }
        // Their release minutes, carried only under
        // `news_session_release_share`: nothing is hashed while it is 0.0.
        for minute in &self.session_news_release {
            hash_f64(&mut buf, *minute);
        }

        // The economy, in the order `state_snapshot` declares its fields.
        // `qe_assets_ratio` is not hashed. The snapshot carries it only
        // under `qe_pe_stock_gain`, the one dial that reads it, and covering
        // it would move every leaf a run under that dial has written.
        let e = &self.economy;
        for value in [
            e.federal_funds_rate, e.prime_rate, e.corporate_bond_yield,
            e.treasury_yield_10y, e.treasury_yield_2y, e.mortgage_rate_30y,
            e.cpi, e.inflation_rate, e.core_inflation,
            e.gdp_growth, e.gdp,
            e.unemployment_rate, e.jobs_created, e.labor_force_participation,
            e.usd_index, e.oil_price, e.gold_price, e.copper_price,
            e.housing_index, e.home_starts_monthly,
            e.housing_transaction_volume,
            e.long_term_unemployment_rate, e.structural_unemployment,
            e.consumer_confidence, e.business_confidence, e.fear_greed_index,
            e.vix,
            e.tariff_rate, e.trade_balance,
            e.oil_inventory_level,
        ] {
            hash_f64(&mut buf, value);
        }
        hash_i64(&mut buf, e.oil_last_opec_day);
        for value in [e.wage_growth, e.previous_day_market_return,
                      e.rolling_market_return_30d] {
            hash_f64(&mut buf, value);
        }
        hash_opt_f64(&mut buf, e.market_pe);
        for value in [e.qe_pe_boost, e.fiscal_stimulus,
                      e.government_debt_to_gdp, e.months_in_current_phase,
                      e.phase_gdp_target, e.recession_probability] {
            hash_f64(&mut buf, value);
        }
        for value in e.gdp_trend {
            hash_f64(&mut buf, value);
        }
        hash_str(&mut buf, e.cycle_phase.as_str());
        // The market's nowcast and its generator's position, only while
        // `cycle_nowcast_accuracy` is set.
        if self.params.cycle_nowcast_accuracy != 0.0 {
            for v in self.cycle_nowcast {
                hash_f64(&mut buf, v);
            }
            let s = self.cycle_nowcast_rng.snapshot();
            hash_u64(&mut buf, s.state);
            hash_u64(&mut buf, s.increment);
            hash_bits(&mut buf, s.spare.unwrap_or(f64::NAN));
            hash_f64(&mut buf, s.uniforms as f64);
            hash_f64(&mut buf, s.normals as f64);
        }
        // The published-phase history, only while `cycle_publication_lag`
        // keeps one, so every other engine's hash is the one it was.
        if self.carries_cycle_history() {
            hash_u32(&mut buf, self.cycle_history.len() as u32);
            for phase in &self.cycle_history {
                hash_str(&mut buf, phase.as_str());
            }
        }
        // Unemployment's impulse, only while
        // `unemployment_adjustment_half_life` is set.
        if self.params.unemployment_adjustment_half_life != 0.0 {
            hash_f64(&mut buf, e.unemployment_impulse);
        }
        // The oil pushes' part of the price, only while
        // `oil_pushes_in_target` is set.
        if self.params.oil_pushes_in_target != 0.0 {
            hash_f64(&mut buf, e.oil_push_level);
        }
        // The dollar's safe-haven bid, only while `usd_mean_reversion` is set.
        if self.params.usd_mean_reversion != 0.0 {
            hash_f64(&mut buf, e.usd_haven_level);
        }
        // The oil pass-through's part of headline, only while
        // `fed_core_inflation` or `treasury_core_inflation` is set.
        if self.carries_oil_inflation_level() {
            hash_f64(&mut buf, e.oil_inflation_level);
        }
        // The put's unmet asks, only while `treasury_zlb_path` is set.
        if self.params.treasury_zlb_path != 0.0 {
            hash_f64(&mut buf, e.zlb_unmet);
        }
        // The published GDP growth figure's state, only while
        // `gdp_publication_lag` is set, so every other engine's hash is the
        // one it was.
        if self.params.gdp_publication_lag != 0.0 {
            let p = &self.gdp_publication;
            hash_f64(&mut buf, p.published);
            hash_i64(&mut buf, p.quarter);
            hash_u32(&mut buf, p.count);
            hash_f64(&mut buf, p.sum);
            hash_u32(&mut buf, p.pending.len() as u32);
            for &(release, value) in &p.pending {
                hash_i64(&mut buf, release);
                hash_f64(&mut buf, value);
            }
        }
        // The drawn publication schedule, only while
        // `cycle_publication_lag_draw` is set: the key, the published and
        // last true phases, the two counters, and the pending turns
        // LENGTH-PREFIXED, each its close then its phase.
        if self.params.cycle_publication_lag_draw != 0.0 {
            let p = &self.cycle_publication;
            hash_u64(&mut buf, p.key);
            hash_str(&mut buf, p.published.as_str());
            hash_str(&mut buf, p.last_true.as_str());
            hash_i64(&mut buf, p.closes);
            hash_u64(&mut buf, p.turns);
            hash_u32(&mut buf, p.pending.len() as u32);
            for &(close, phase) in &p.pending {
                hash_i64(&mut buf, close);
                hash_str(&mut buf, phase.as_str());
            }
        }
        // The anticipation's left-out drift `D` and the last `A - e`, only
        // while `earnings_anticipation_drift_share` is set.
        if self.carries_anticipation_drift() {
            hash_f64(&mut buf, self.anticipation_drift);
            hash_f64(&mut buf, self.anticipation_raw);
        }

        // The central bank.
        let bank = &self.central_bank;
        hash_i64(&mut buf, bank.last_meeting_date);
        hash_i64(&mut buf, bank.next_meeting_date);
        hash_f64(&mut buf, bank.target_inflation);
        hash_f64(&mut buf, bank.target_unemployment);
        hash_bool(&mut buf, bank.qe_active);
        hash_f64(&mut buf, bank.qe_monthly_purchases);
        hash_f64(&mut buf, bank.hawkish_dovish_score);
        hash_str(&mut buf, bank.forward_guidance.as_str());

        hash_u32(&mut buf, day_count);

        // The draw-addressing layer's two snapshot fields. The counts are
        // the seven streams' uniform and normal positions, flattened the
        // way the snapshot flattens them; the overlay is the table of
        // substitutions installed on them, walked stream by stream in the
        // same order.
        //
        // Both are hashed because both decide what the engine does next. Two
        // engines alike in every column but differing by one patched draw
        // take different days from here, and a leaf that skipped the table
        // would commit to a state that does not describe them. The hash
        // refuses a snapshot carrying a field it does not know for exactly
        // this reason, and these two arrived after it was written.
        for (uniforms, normals) in self.stream_positions() {
            hash_f64(&mut buf, uniforms as f64);
            hash_f64(&mut buf, normals as f64);
        }
        let mut overlay: Vec<(u32, u8, u64, f64)> = Vec::new();
        for id in 0..stream::COUNT as u32 {
            if let Some(table) = self.draw_overlay(id) {
                for ((kind, index), value) in &table.table {
                    overlay.push((id, *kind as u8, *index, *value));
                }
            }
        }
        hash_u32(&mut buf, overlay.len() as u32);
        for (stream, kind, index, value) in overlay {
            hash_u32(&mut buf, stream);
            hash_u32(&mut buf, u32::from(kind));
            hash_u64(&mut buf, index);
            hash_f64(&mut buf, value);
        }

        // The rate instruments, after every equity field and before the
        // agent-facing book, and only when there are any, so every engine
        // without them hashes exactly as it did before they existed.
        // In the order the snapshot's `rates` block carries them, which is
        // the order `manifest.state_hash` reads. The per-tick print
        // decomposition is output, not state, and is left out as the
        // equities' is.
        if !self.rates.is_empty() {
            hash_str(&mut buf, "rates");
            hash_u32(&mut buf, self.rates.len() as u32);
            for inst in &self.rates.instruments {
                hash_str(&mut buf, inst.spec.ticker);
                for value in [
                    inst.level, inst.marked_yield, inst.price, inst.previous_close,
                    inst.open, inst.high, inst.low, inst.volume, inst.avg_volume,
                    inst.units_outstanding, inst.maker_inventory, inst.day_carry,
                    inst.day_duration, inst.day_convexity,
                ] {
                    hash_f64(&mut buf, value);
                }
            }
            hash_f64(&mut buf, self.rates.ig_spread);
            hash_f64(&mut buf, self.rates.last_corporate);
            hash_bool(&mut buf, self.rates.closed_since_open);
        }

        // The agent-facing book, after the rate instruments, and only when
        // it has been used, so every state that never saw an agent's order
        // hashes as it did before the book existed. Everything in it decides what the next
        // tick or the next agent meets, the undelivered fills included:
        // two engines alike in every column but owing an agent different
        // fills are not the same state. `manifest.state_hash` writes the
        // same bytes from the snapshot's `book` entry.
        if !self.book.is_pristine() {
            hash_book(&mut buf, &self.book);
        }

        // Four fields added after the book, each behind its own name and
        // each only where it can differ from what an engine that never
        // moved it holds, so every state hashed before them hashes the same.
        //
        // The day's label and the valuation's clock, each only when it is not
        // the day `day_count` and the session flag give (`default_day`). A
        // run that numbers its days from the counter never writes either.
        // The label is here although nothing prices off it, because the
        // book stamps fills with it and two engines owing different stamps
        // would hash apart one fill later.
        let usual = default_day(day_count, market_open);
        if self.current_day != usual {
            hash_str(&mut buf, "current_day");
            hash_i64(&mut buf, self.current_day);
        }
        if self.elapsed_days != usual {
            hash_str(&mut buf, "elapsed_days");
            hash_i64(&mut buf, self.elapsed_days);
        }
        // The fair-value inputs, once `set_fundamentals` has moved them off
        // the ones each company was built or listed with. Every price
        // values off them, so two engines alike in every column and valuing
        // different earnings are not the same state; before this they
        // hashed equal and a restore brought back the construction figures.
        if self.fundamentals_changed() {
            hash_str(&mut buf, "fundamentals");
            hash_u32(&mut buf, n as u32);
            for c in &self.companies {
                hash_f64(&mut buf, c.eps.unwrap_or(f64::NAN));
                hash_f64(&mut buf, c.book_value_per_share.unwrap_or(f64::NAN));
                hash_f64(&mut buf, c.revenue_growth.unwrap_or(f64::NAN));
            }
        }
        // The share counts, once `set_shares_outstanding` has moved them off
        // the ones each company was built or listed with: every tick's market
        // cap is the price times the count.
        if self.shares_outstanding_changed() {
            hash_str(&mut buf, "shares_outstanding");
            hash_u32(&mut buf, n as u32);
            for c in &self.companies {
                hash_f64(&mut buf, c.stock.shares_outstanding);
            }
        }
        // The host's input to the VIX target, each behind its name and only
        // while it stands (`set_vix_target_premium`, `set_vix_target_floor`),
        // so an engine no host wrote one on hashes as it did before.
        if self.carries_vix_target_premium() {
            hash_str(&mut buf, "vix_target_premium");
            hash_f64(&mut buf, self.vix_target_premium);
            hash_f64(&mut buf, self.vix_target_premium_half_life);
        }
        if let Some(floor) = self.vix_target_floor {
            hash_str(&mut buf, "vix_target_floor");
            hash_f64(&mut buf, floor);
        }
        // The variance cascade's components, only on a model that runs it.
        if self.carries_garch_cascade() {
            let values = self.garch_cascade();
            hash_str(&mut buf, "garch_cascade");
            hash_u32(&mut buf, values.len() as u32);
            for value in values {
                hash_f64(&mut buf, value);
            }
        }
        // The population, last and only on an engine that holds one: its
        // fingerprint, its roster and every number of its state.
        if let Some(pop) = self.population.as_deref() {
            hash_str(&mut buf, "population");
            hash_str(&mut buf, &pop.fingerprint);
            hash_u32(&mut buf, pop.tickers.len() as u32);
            for t in &pop.tickers {
                hash_str(&mut buf, t);
            }
            let flat = pop.to_flat();
            hash_u32(&mut buf, flat.len() as u32);
            for value in flat {
                hash_f64(&mut buf, value);
            }
        }

        let mut hasher = Sha256::new();
        hasher.update(&buf);
        let out = hasher.finalize();
        let mut digest = [0u8; 32];
        digest.copy_from_slice(&out);
        digest
    }
}

/// Fields exposed columnar-wise across the FFI boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PriceField {
    Price,
    PreviousClose,
    Open,
    High,
    Low,
    Volume,
    MarketCap,
    /// `NaN` for a company that has never ticked, since a column cannot carry
    /// `None`. The embedder should read it as "unset", not as a number.
    MispricingS,
    /// `NaN` when the maker has never quoted this company.
    ///
    /// Not zero. Zero is a REAL inventory -- a maker holding nothing -- and
    /// before the first tick every company read 0.0, which said "flat" about a
    /// book that did not exist yet. Absence is not zero anywhere else in this
    /// library and it should not have been here.
    MakerInventory,
    GarchVariance,
    /// The previous tick's print. `NaN` before the second tick.
    PreviousTickPrice,
    /// `s` as it stood at the last close, and the momentum term carried from
    /// it. Both `NaN` before the first close: they are day-boundary state, so
    /// a session that has not crossed one has no value to report.
    MispricingSPrevClose,
    MispricingMomentum,
    /// The last completed day's return. `NaN` before the first close.
    LastDailyReturn,
    /// Cross-sectional characteristics, constant through a run. Exposed as
    /// columns so a factor study can join them to `bars` without carrying the
    /// universe alongside.
    AvgVolume,
    Beta,
    ShortInterest,
    FloatShares,
}

/// The columns [`Engine::state_hash`] walks, in the order
/// `python_engine::COLUMN_FIELDS` declares them, so the Rust digest and the
/// Python twin that reads a snapshot see one order.
pub const STATE_HASH_COLUMNS: [PriceField; 18] = [
    PriceField::Price,
    PriceField::PreviousClose,
    PriceField::PreviousTickPrice,
    PriceField::Open,
    PriceField::High,
    PriceField::Low,
    PriceField::Volume,
    PriceField::AvgVolume,
    PriceField::MarketCap,
    PriceField::MispricingS,
    PriceField::MispricingSPrevClose,
    PriceField::MispricingMomentum,
    PriceField::LastDailyReturn,
    PriceField::MakerInventory,
    PriceField::GarchVariance,
    PriceField::Beta,
    PriceField::ShortInterest,
    PriceField::FloatShares,
];

/// The day an engine's two day numbers hold when nothing has moved them
/// off the binding's counter: `day_count` while a session is open, the day
/// just closed once it has closed (`day_count - 1`), and 0 before the first
/// open. [`Engine::state_hash`] covers the label and the valuation's clock
/// only where they differ from this, and a restore that finds neither in
/// its snapshot sets both to it.
///
/// `run_session(close_at_end=True)` closes the day and leaves the session
/// flag set, so after it the two read one day behind this and the snapshot
/// carries them.
pub fn default_day(day_count: u32, market_open: bool) -> i64 {
    let count = i64::from(day_count);
    if market_open || count == 0 {
        count
    } else {
        count - 1
    }
}

/// Where a column sits in [`Engine::state_hash`].
///
/// Exhaustive on `PriceField`, the same discipline `set_column` uses: a
/// nineteenth column fails to compile here until someone decides where it
/// goes, rather than being dropped from every leaf a ledger holds. The test
/// below walks [`STATE_HASH_COLUMNS`] and holds the two in agreement.
pub fn state_hash_position(field: PriceField) -> usize {
    match field {
        PriceField::Price => 0,
        PriceField::PreviousClose => 1,
        PriceField::PreviousTickPrice => 2,
        PriceField::Open => 3,
        PriceField::High => 4,
        PriceField::Low => 5,
        PriceField::Volume => 6,
        PriceField::AvgVolume => 7,
        PriceField::MarketCap => 8,
        PriceField::MispricingS => 9,
        PriceField::MispricingSPrevClose => 10,
        PriceField::MispricingMomentum => 11,
        PriceField::LastDailyReturn => 12,
        PriceField::MakerInventory => 13,
        PriceField::GarchVariance => 14,
        PriceField::Beta => 15,
        PriceField::ShortInterest => 16,
        PriceField::FloatShares => 17,
    }
}

/// The one NaN pattern any digest in this crate writes. IEEE-754 leaves the
/// sign and payload of a NaN to the platform, so hashing the bits as they
/// arrive would make a digest depend on the machine rather than the model.
const CANONICAL_NAN: [u8; 8] = [0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

/// One f64 in canonical digest form: big-endian, one NaN pattern.
fn hash_f64(buf: &mut Vec<u8>, value: f64) {
    if value.is_nan() {
        buf.extend_from_slice(&CANONICAL_NAN);
    } else {
        buf.extend_from_slice(&value.to_be_bytes());
    }
}

/// One f64 as its raw bit pattern, NaN included.
///
/// For a value that is a bit pattern wearing a float, which is how a
/// generator state crosses into Python. Canonicalising one of those would
/// collapse distinct states onto one digest.
fn hash_bits(buf: &mut Vec<u8>, value: f64) {
    buf.extend_from_slice(&value.to_bits().to_be_bytes());
}

fn hash_u64(buf: &mut Vec<u8>, value: u64) {
    buf.extend_from_slice(&value.to_be_bytes());
}

fn hash_i64(buf: &mut Vec<u8>, value: i64) {
    buf.extend_from_slice(&value.to_be_bytes());
}

fn hash_u32(buf: &mut Vec<u8>, value: u32) {
    buf.extend_from_slice(&value.to_be_bytes());
}

fn hash_bool(buf: &mut Vec<u8>, value: bool) {
    buf.push(u8::from(value));
}

/// A string, length-prefixed so two adjacent strings cannot be re-split.
fn hash_str(buf: &mut Vec<u8>, value: &str) {
    hash_u32(buf, value.len() as u32);
    buf.extend_from_slice(value.as_bytes());
}

fn hash_opt_f64(buf: &mut Vec<u8>, value: Option<f64>) {
    match value {
        Some(v) => {
            hash_bool(buf, true);
            hash_f64(buf, v);
        }
        None => hash_bool(buf, false),
    }
}

/// The agent-facing book, in the order the snapshot's `book` entry holds it.
fn hash_book(buf: &mut Vec<u8>, book: &crate::agent_book::BookState) {
    use crate::order_book::Side;
    let side = |s: Side| match s {
        Side::Buy => "buy",
        Side::Sell => "sell",
    };
    hash_str(buf, "book");
    hash_u64(buf, book.sequence);
    hash_u64(buf, book.fill_sequence);
    hash_u32(buf, book.taken.len() as u32);
    for row in &book.taken {
        for v in row {
            hash_f64(buf, *v);
        }
    }
    hash_u32(buf, book.orders.len() as u32);
    for o in &book.orders {
        hash_str(buf, &o.id);
        hash_str(buf, &o.agent);
        hash_str(buf, &o.ticker);
        hash_str(buf, side(o.side));
        hash_f64(buf, o.limit);
        hash_f64(buf, o.quantity);
        hash_f64(buf, o.remaining);
        hash_u64(buf, o.sequence);
        hash_str(buf, o.mode.as_str());
    }
    hash_u32(buf, book.flow.len() as u32);
    for (agent, ticker, bought, sold) in &book.flow {
        hash_str(buf, agent);
        hash_str(buf, ticker);
        hash_f64(buf, *bought);
        hash_f64(buf, *sold);
    }
    hash_u32(buf, book.fills.len() as u32);
    for f in &book.fills {
        hash_str(buf, &f.agent);
        hash_str(buf, &f.order_id);
        hash_str(buf, &f.ticker);
        hash_str(buf, side(f.side));
        hash_f64(buf, f.quantity);
        hash_f64(buf, f.price);
        hash_str(buf, f.liquidity.as_str());
        hash_str(buf, &f.counterparty);
        hash_f64(buf, f.reference);
        hash_i64(buf, f.day);
        // Not `tick`: it is a label counted from this engine's own open,
        // and a restore clears the day marks it is counted from, so a
        // restored engine would hash apart from the one it copied on a
        // label alone. The fill's sequence orders it.
        hash_u64(buf, f.sequence);
    }
    hash_u32(buf, book.impacts.len() as u32);
    for r in &book.impacts {
        hash_str(buf, &r.agent);
        hash_str(buf, &r.ticker);
        hash_f64(buf, r.bought);
        hash_f64(buf, r.sold);
        hash_f64(buf, r.permanent);
        // Only while the metaorder memory is on, so every state without it
        // hashes as it did before it existed.
        if let Some(x) = r.transient {
            hash_str(buf, "transient");
            hash_f64(buf, x);
        }
        hash_i64(buf, r.day);
    }
    // The metaorder memory, after everything else and only while it holds
    // something.
    if !book.memory.is_empty() {
        hash_str(buf, "memory");
        hash_u32(buf, book.memory.len() as u32);
        for row in &book.memory {
            for v in row {
                hash_f64(buf, *v);
            }
        }
    }
}

/// The single-name options' exercise state (`options_names_listed`), in the
/// order the snapshot's `name_options_exercise` entry holds it: the
/// instructions waiting for the close, then every exercise, assignment and
/// delisting settlement, each its session, event, party, symbol, contracts,
/// shares, cash, value and reference.
fn hash_name_exercise(
    buf: &mut Vec<u8>,
    instructions: &[(String, String, f64)],
    events: &[crate::derivatives::Settlement],
) {
    hash_str(buf, "name_options_exercise");
    hash_u32(buf, instructions.len() as u32);
    for (agent, symbol, quantity) in instructions {
        hash_str(buf, agent);
        hash_str(buf, symbol);
        hash_f64(buf, *quantity);
    }
    hash_u32(buf, events.len() as u32);
    for e in events {
        hash_i64(buf, e.session);
        hash_str(buf, e.event.as_str());
        hash_str(buf, e.agent.as_deref().unwrap_or(""));
        hash_str(buf, &e.symbol);
        hash_f64(buf, e.quantity);
        hash_f64(buf, e.shares);
        hash_f64(buf, e.cash);
        hash_f64(buf, e.value);
        hash_f64(buf, e.reference);
    }
}

fn hash_opt_str(buf: &mut Vec<u8>, value: Option<&str>) {
    match value {
        Some(v) => {
            hash_bool(buf, true);
            hash_str(buf, v);
        }
        None => hash_bool(buf, false),
    }
}

/// Index of `random_noise` among the components, found rather than written.
///
/// Every component is an f64, so a literal index would keep compiling and
/// start feeding the variance process the crowd lean the day a component is
/// inserted ahead of it.
fn random_noise_index() -> usize {
    crate::market::factors::S_COMPONENT_KEYS
        .iter()
        .position(|name| *name == "random_noise")
        .expect("random_noise is one of the components")
}

/// Counts draws as they are taken.
///
/// The obvious alternative — clone the generator, replay uniforms until the
/// clone catches up — does NOT work on a mixed stream, and the reason is worth
/// recording because it looks like it should. `next_normal` is Box-Muller: it
/// consumes two PCG steps and leaves a spare cached, and a later normal
/// consumes zero. A probe pulling uniforms never reproduces that spare, so the
/// two states are never equal and the search runs to its bound.
///
/// Counting at the call is exact, costs a `usize` increment, and does not care
/// what kind of draw it was.
struct Counting<'a> {
    inner: &'a mut GameRng,
    count: usize,
}

impl Rng for Counting<'_> {
    fn next_f64(&mut self) -> f64 {
        self.count += 1;
        self.inner.next_f64()
    }
    fn next_normal(&mut self) -> f64 {
        self.count += 1;
        self.inner.next_normal()
    }
    fn site(&mut self, site: Site, tag: u32) {
        self.inner.site(site, tag);
    }
}

/// What a session needs beyond the engine's own state.
///
/// [`SessionRequest::new`] gives a quiet session (no news, no orders,
/// volatility multiplier 1.0) that leaves opening and closing the day to the
/// caller. Set the other fields on the value it returns.
///
/// Outside this crate a `SessionRequest` can only come from `new`, because
/// the struct is `#[non_exhaustive]`: a later patch release can add a field
/// without breaking your code. 0.8.5 added `fills`, and every struct literal
/// written for 0.8.1 stopped compiling. So this does not compile:
///
/// ```compile_fail,E0639
/// use tradefloor::engine::SessionRequest;
/// use tradefloor::market::GameTime;
///
/// let request = SessionRequest {
///     start: GameTime::new(9, 30, 3),
///     ticks: 390,
///     volatility_multiplier: 1.0,
///     news: &[],
///     news_impact_queue: &[],
///     order_volumes: &[],
///     fills: &[],
///     close_at_end: false,
///     reopen: false,
///     daily_innovations: &[],
///     sector_base_variances: &[],
///     stop: None,
/// };
/// ```
#[non_exhaustive]
pub struct SessionRequest<'a> {
    /// The clock at the first tick. Each tick is one minute after the last.
    pub start: GameTime,
    /// How many one-minute ticks to run. 390 is a full session, 09:30 to
    /// 16:00, and is the Python package's default.
    pub ticks: usize,
    /// Scales the per-tick noise. 1.0 is the model as calibrated.
    pub volatility_multiplier: f64,
    /// News events that reach the factor model this session.
    pub news: &'a [NewsEvent],
    /// News already in flight from earlier sessions, still being absorbed.
    pub news_impact_queue: &'a [NewsImpactEntry],
    /// Order volume held on EVERY tick of the session: a standing rate, in
    /// shares per minute, for a program that trades all session long.
    pub order_volumes: &'a [(String, OrderVolume)],
    /// Order volume that reaches the market ONCE, on the session's first
    /// tick: the trades an agent filled at the step boundary just before it.
    ///
    /// # Why this is not `order_volumes`
    ///
    /// Until 0.8.5 every harness handed an agent's fills to the session as
    /// `order_volumes`, so one order was counted on every tick of the step:
    /// 65 times at six steps a day, 390 at one. It also landed only after the
    /// agent had filled at the pre-trade book, so the agent never paid its
    /// own permanent impact and collected it instead. A spec mean reversion
    /// rule beat buy-and-hold on 20 of 20 suite markets by a median 42
    /// points in 60 days on that alone (measured on pt-v19 on 2026-09-24).
    ///
    /// # When it lands, and who pays for it
    ///
    /// The fill is priced against the book standing at the step boundary,
    /// which charges the spread and the depth the order walks. The order's
    /// permanent impact then moves `s` once, on the first tick after the
    /// fill, and the next trader meets the moved price. That is the
    /// discrete Almgren-Chriss convention: a trade executes at the
    /// pre-trade price less its temporary impact, and its permanent impact
    /// reaches the trades after it. The convention is free of a round-trip
    /// profit only while the book charges at least the permanent impact an
    /// order causes, and `tests/test_agent_flow.py` holds that on every
    /// shipped preset: a fill's premium over the print it traded at is
    /// never below the move its own flow leaves behind.
    ///
    /// Summed with `order_volumes` per ticker on the first tick when both
    /// name one. Empty is bit-identical to the session this field did not
    /// exist in: the first tick reads `order_volumes` untouched.
    pub fills: &'a [(String, OrderVolume)],
    /// Run the close bookkeeping when the session finishes normally.
    pub close_at_end: bool,
    /// Open the market before the first tick.
    ///
    /// True is the reference's behaviour and the right default: it runs one
    /// session per day, so opening inside the session and opening the day are
    /// the same act.
    ///
    /// They stop being the same act when a day is made of SEVERAL sessions,
    /// which is what agent stepping does -- act, run some ticks, act again.
    /// `open_market` resets the attribution accumulator and re-anchors the
    /// daily open, so re-opening per step silently made both per-step:
    ///
    /// - `attribution` documents itself as per-DAY and was per-session. A
    ///   large buy in step 0 of a six-step day moved the market -- the tape
    ///   records it -- and `attribution("order_flow_impact")` read exactly
    ///   zero at the close. An agent's own impact was erased from the ground
    ///   truth that scores it.
    /// - Worse, it is not only reporting. `close_at_end` feeds
    ///   `attribution_column(random_noise)` to GARCH as the day's innovation,
    ///   so a stepped day updated variance from the LAST STEP's noise rather
    ///   than the day's.
    ///
    /// Set false for the second and later sessions of one day. A day of one
    /// session is unaffected either way, which is why no parity vector moves.
    pub reopen: bool,
    /// Read only when `close_at_end` is set: each name's daily innovation for
    /// GARCH. A `None` slot takes the noise the engine accumulated this
    /// session, which is what [`Engine::close_day`] always uses.
    pub daily_innovations: &'a [Option<f64>],
    /// Read only when `close_at_end` is set: each sector's base variance.
    pub sector_base_variances: &'a [f64],
    /// Stop early when a condition is met, for event-driven advancement.
    pub stop: Option<StopCondition>,
}

impl<'a> SessionRequest<'a> {
    /// A quiet session of `ticks` minutes starting at `start`.
    ///
    /// No news, no order flow, volatility multiplier 1.0, no stop
    /// condition. It neither opens the market (`reopen` is false) nor
    /// closes it (`close_at_end` is false), so it fits the day loop
    /// [`Engine::open_market`], [`Engine::run_session`], [`Engine::close_day`]
    /// that the WebAssembly binding runs. Set any field on the result to add
    /// news or flow.
    ///
    /// ```
    /// use tradefloor::engine::SessionRequest;
    /// use tradefloor::market::GameTime;
    ///
    /// let open = GameTime::new(9, 30, 3);
    /// let request = SessionRequest::new(open, 390);
    /// assert_eq!(request.ticks, 390);
    /// assert!(!request.reopen && !request.close_at_end);
    /// ```
    pub fn new(start: GameTime, ticks: usize) -> Self {
        SessionRequest {
            start,
            ticks,
            volatility_multiplier: 1.0,
            news: &[],
            news_impact_queue: &[],
            order_volumes: &[],
            fills: &[],
            close_at_end: false,
            reopen: false,
            daily_innovations: &[],
            sector_base_variances: &[],
            stop: None,
        }
    }
}

/// One flow per ticker: `standing` with `once` added to it.
///
/// The tick finds a ticker's flow with the FIRST matching entry, so two
/// entries for one name would drop the second rather than add it. Standing
/// entries keep their order and take any fills for the same name; fills for
/// names the standing flow does not carry follow, in their own order. Both
/// inputs arrive sorted from the bindings, and neither order reaches a price.
pub fn merge_order_volumes(
    standing: &[(String, OrderVolume)],
    once: &[(String, OrderVolume)],
) -> Vec<(String, OrderVolume)> {
    let mut out: Vec<(String, OrderVolume)> = standing.to_vec();
    for (ticker, v) in once {
        match out.iter_mut().find(|(t, _)| t == ticker) {
            Some((_, have)) => {
                have.buy += v.buy;
                have.sell += v.sell;
            }
            None => out.push((ticker.clone(), *v)),
        }
    }
    out
}

/// Why a session might end before its last tick.
///
/// Deliberately limited to what the ENGINE can decide from its own state.
/// Stopping on a fill needs the order book and the caller's resting orders,
/// neither of which this engine owns — that belongs with whoever owns order
/// state, and inventing a half-version here would be worse than not having it.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum StopCondition {
    /// A named company's price leaves a band. `None` on a side means unbounded.
    PriceOutside {
        company: usize,
        below: Option<f64>,
        above: Option<f64>,
    },
}

impl StopCondition {
    fn triggered(&self, _tick: usize, companies: &[TickCompany]) -> bool {
        match *self {
            StopCondition::PriceOutside {
                company,
                below,
                above,
            } => {
                let Some(c) = companies.get(company) else {
                    return false;
                };
                let price = c.stock.price;
                below.is_some_and(|b| price < b) || above.is_some_and(|a| price > a)
            }
        }
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SessionOutcome {
    pub draws_consumed: usize,
    /// `Some(tick)` when a [`StopCondition`] fired. The close is NOT run in
    /// that case — the day is not over, the caller interrupted it.
    pub halted_at: Option<usize>,
}

/// A reusable per-day columnar buffer.
///
/// Row-major, `tick * companies + company`, so one company's path is a strided
/// read and one tick's cross-section is contiguous. The cross-section is the
/// hot direction: emission is per tick.
///
/// `f64` only. See [`Engine::run_session`] for why there is no `f32` variant.
#[non_exhaustive]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct SessionBuffer {
    pub companies: usize,
    /// How many ticks the last session actually wrote — less than capacity
    /// when a [`StopCondition`] fired.
    pub ticks_written: usize,
    pub prices: Vec<f64>,
    pub volumes: Vec<f64>,
    pub mispricing_s: Vec<f64>,
    pub fundamental: Vec<f64>,
    pub anchor: Vec<f64>,
    /// The component columns, each `ticks * companies`: the eight
    /// `S_COMPONENT_KEYS` in order, then the tick's fair-value shift
    /// (`TICK_COMPONENT_COUNT` in all). Flat buffers rather than one of
    /// `[f64; TICK_COMPONENT_COUNT]`, because each becomes an Arrow column
    /// and a column wants a contiguous run of its own values.
    pub components: [Vec<f64>; crate::market::factors::TICK_COMPONENT_COUNT],
    /// The print decomposition, each `ticks * companies`: the shock that
    /// arrived and the depth that absorbed it, in log units.
    pub shock: Vec<f64>,
    pub absorbed: Vec<f64>,
    /// How far the print breaker moved each print. `absorbed - clamp` is the
    /// book's own share of the distance from the model price to the tape.
    pub clamp: Vec<f64>,
    /// What was written to each price between its last print and the
    /// tick, in log units. `repriced + shock + absorbed` is the print's log
    /// move from the previous print.
    pub repriced: Vec<f64>,
    /// The depth counterfactual, each `ticks * companies` when it ran and
    /// EMPTY when it did not. Emptiness is the signal, which is why these
    /// two are not resized alongside the columns above.
    pub unbounded_print: Vec<f64>,
    pub liquidity_share: Vec<f64>,
}

/// One tick's ground truth, as the engine holds it, handed to
/// `SessionBuffer::write_tick`.
///
/// A struct rather than nine positional slices. The call site passes nine
/// same-typed buffers and a transposition there would compile, run, and
/// mislabel every row of every column it touched.
#[non_exhaustive]
pub struct TickTruth<'a> {
    pub components: &'a [[f64; crate::market::factors::TICK_COMPONENT_COUNT]],
    pub fundamental: &'a [f64],
    pub anchor: &'a [f64],
    pub shock: &'a [f64],
    pub absorbed: &'a [f64],
    pub clamp: &'a [f64],
    /// What was written to each price between its last print and this
    /// tick (`Engine::tick_repriced`).
    pub repriced: &'a [f64],
    pub unbounded_print: &'a [f64],
    pub liquidity_share: &'a [f64],
}

impl SessionBuffer {
    /// An empty buffer. [`Engine::run_session`] sizes it to the session on
    /// each call, so one buffer serves every day of a run.
    pub fn new() -> Self {
        Self::default()
    }

    fn resize(&mut self, ticks: usize, companies: usize) {
        let needed = ticks * companies;
        if self.prices.len() != needed {
            self.prices.resize(needed, 0.0);
            self.volumes.resize(needed, 0.0);
            self.mispricing_s.resize(needed, 0.0);
            self.fundamental.resize(needed, f64::NAN);
            self.anchor.resize(needed, f64::NAN);
            self.shock.resize(needed, 0.0);
            self.absorbed.resize(needed, 0.0);
            self.clamp.resize(needed, 0.0);
            self.repriced.resize(needed, 0.0);
            for column in self.components.iter_mut() {
                column.resize(needed, 0.0);
            }
        }
        self.companies = companies;
        self.ticks_written = ticks;
    }

    /// Size the counterfactual columns for a session that is about to run
    /// with the arm on, or empty them for one that is not.
    ///
    /// Separate from [`SessionBuffer::resize`] because these two columns are
    /// the only ones whose PRESENCE carries information. Resizing them
    /// unconditionally would leave a run with the arm off holding a full
    /// column of zeros, which reads as "liquidity moved nothing" rather than
    /// as "nobody asked".
    fn resize_counterfactual(&mut self, on: bool) {
        let needed = if on { self.prices.len() } else { 0 };
        if self.unbounded_print.len() != needed {
            self.unbounded_print.clear();
            self.liquidity_share.clear();
            self.unbounded_print.resize(needed, f64::NAN);
            self.liquidity_share.resize(needed, 0.0);
        }
    }

    fn write_tick(&mut self, tick: usize, companies: &[TickCompany], truth: &TickTruth<'_>) {
        let base = tick * self.companies;
        for (i, c) in companies.iter().enumerate() {
            self.prices[base + i] = c.stock.price;
            self.volumes[base + i] = c.stock.volume;
            // NaN for a company that has never ticked — a column cannot carry
            // `None`, and zero would be a real mispricing.
            self.mispricing_s[base + i] = c.stock.mispricing_s.unwrap_or(f64::NAN);
            self.fundamental[base + i] = truth.fundamental.get(i).copied().unwrap_or(f64::NAN);
            self.anchor[base + i] = truth.anchor.get(i).copied().unwrap_or(f64::NAN);
            self.shock[base + i] = truth.shock.get(i).copied().unwrap_or(0.0);
            self.absorbed[base + i] = truth.absorbed.get(i).copied().unwrap_or(0.0);
            self.clamp[base + i] = truth.clamp.get(i).copied().unwrap_or(0.0);
            self.repriced[base + i] = truth.repriced.get(i).copied().unwrap_or(0.0);
            // Guarded on the buffer rather than on the source: a session that
            // sized these columns and then met an engine with the arm off
            // would otherwise write NaN into a column it had promised.
            if !self.unbounded_print.is_empty() {
                self.unbounded_print[base + i] = truth
                    .unbounded_print
                    .get(i)
                    .copied()
                    .unwrap_or(c.stock.price);
                self.liquidity_share[base + i] =
                    truth.liquidity_share.get(i).copied().unwrap_or(0.0);
            }
            let row = truth.components.get(i).copied().unwrap_or([0.0; crate::market::factors::TICK_COMPONENT_COUNT]);
            for (k, column) in self.components.iter_mut().enumerate() {
                column[base + i] = row[k];
            }
        }
    }

    /// Write the rate instruments' rows for one tick, after the equities'.
    ///
    /// The truth columns carry what exists for an index: the model price is
    /// the index level (as both `fundamental` and `anchor`), `s` is zero, the
    /// equity components are zero, and the print decomposition is the book's.
    /// The depth counterfactual never runs on an index, so its print there is
    /// the print and its share zero.
    fn write_rates(&mut self, tick: usize, first: usize, rates: &crate::rates::RateBook) {
        let base = tick * self.companies + first;
        for (j, inst) in rates.instruments.iter().enumerate() {
            let at = base + j;
            self.prices[at] = inst.price;
            self.volumes[at] = inst.volume;
            self.mispricing_s[at] = 0.0;
            self.fundamental[at] = inst.level;
            self.anchor[at] = inst.level;
            self.shock[at] = inst.tick_shock;
            self.absorbed[at] = inst.tick_absorbed;
            self.clamp[at] = 0.0;
            // What the close's re-mark, a pin's and the open wrote to the
            // price since the last print (`rate_close_remark`); zero on
            // every engine without it, where nothing writes between prints
            // but the open, which this column has never booked for a rate
            // row.
            self.repriced[at] = inst.tick_repriced;
            if !self.unbounded_print.is_empty() {
                self.unbounded_print[at] = inst.price;
                self.liquidity_share[at] = 0.0;
            }
            for column in self.components.iter_mut() {
                column[at] = 0.0;
            }
        }
    }

    /// One tick's cross-section, contiguous.
    pub fn tick_prices(&self, tick: usize) -> &[f64] {
        let base = tick * self.companies;
        &self.prices[base..base + self.companies]
    }
}

/// A fixed simulation, hashed — the cross-binding determinism probe.
///
/// Every binding calls THIS rather than hashing state on its own side. A
/// check implemented twice is a fork of the check: the first attempt at
/// this compared a wasm digest against one rebuilt in Python, and the two
/// disagreed because the Python surface reports rates as fractions while
/// the core carries percent. That is a units bug in the harness reported as
/// a determinism failure, which is precisely the confusion a shared
/// implementation removes.
///
/// Hashing rules follow `tests/known_answer.py`: raw big-endian IEEE-754
/// bytes, no decimal formatting anywhere, because a float formatter would
/// make the digest depend on something other than the simulation.
///
/// Prices are tick-rounded to cents, so a price-only digest can agree while
/// two builds have drifted below that. The macro state is carried at full
/// precision and sits downstream of the whole day chain, so it is the part
/// of this probe that can see a low-bit difference.
///
/// Each day runs the core's numbered day, the loop the WebAssembly `Sim`
/// runs: [`Engine::set_current_day`] with the day counted from zero,
/// [`Engine::open_market`], one session and [`Engine::close_day`]. Through
/// 0.10.x
/// the days were not numbered, so on a preset that reads the day (the
/// buyback yield from pt-v18, the earnings and dividend calendars on pt-v21)
/// a run of more than one day hashes differently from those releases.
///
/// Returns `None` for an unknown preset.
pub fn fixed_simulation_digest(
    size: usize,
    universe_seed: u64,
    seed: u64,
    days: usize,
    ticks: usize,
    preset: &str,
) -> Option<String> {
    use sha2::{Digest, Sha256};

    let params = crate::params::ModelParams::preset(preset)?;
    let generated = crate::universe::random_universe(size, universe_seed);
    let companies: Vec<TickCompany> = generated
        .iter()
        .enumerate()
        .map(|(i, g)| g.to_init().to_tick_company(i))
        .collect();
    let mut engine = Engine::with_params(
        seed,
        companies,
        crate::economy::create_initial_economy_state(
            &crate::economy::InitialEconomyOptions::default()),
        crate::economy::create_initial_central_bank_state(0),
        crate::sectors::keys().iter().map(|s| s.to_string()).collect(),
        params,
    );
    let mut buffer = SessionBuffer::new();
    for day in 0..days {
        engine.run_numbered_day(day as i64, ticks, &mut buffer);
    }

    // NaN IS THE ONE PLACE WEBASSEMBLY IS LOOSE.
    //
    // The wasm specification pins IEEE-754 exactly for add, subtract,
    // multiply, divide and square root, which is why a browser build can
    // agree with a native one at all. It deliberately does NOT pin NaN
    // payload bits. So hashing a NaN would compare a bit pattern the spec
    // permits two engines to choose differently -- a digest that could
    // disagree for a reason that is not the model, which is precisely the
    // failure this probe exists to detect.
    //
    // Measured: zero NaN and zero infinities across 16,800 price samples
    // (seven seeds x sixty days x forty names) plus the macro state. So this
    // does not fire today. It is a guard rather than a hope, because "we
    // have never seen one" is not the same claim as "there cannot be one",
    // and the failure it prevents is a determinism report that is wrong in
    // the reassuring direction.
    let mut hasher = Sha256::new();
    for price in engine.prices() {
        if !price.is_finite() {
            return None;
        }
        hasher.update(price.to_be_bytes());
    }
    let e = engine.economy();
    for v in [e.vix, e.federal_funds_rate, e.inflation_rate,
              e.corporate_bond_yield, e.fear_greed_index] {
        if !v.is_finite() {
            return None;
        }
        hasher.update(v.to_be_bytes());
    }
    Some(format!("{:x}", hasher.finalize()))
}


/// Divide each public name's beta by `B^power`, `B` the roster's
/// cap-weighted beta at the opening caps (`market_beta_normalise`).
///
/// `B` is taken over the names that are public and not bankrupt, with a
/// positive finite market cap, reading an absent beta as 1.0 (the loading
/// every reader gives it). A roster with no such name, or a `B` that is not
/// positive and finite, is returned as it came: there is no index to
/// normalise against. The names outside the sum keep their betas too, so a
/// name that lists later carries the beta it was given, as `add_company`'s
/// does.
pub(crate) fn normalise_roster_beta(mut companies: Vec<TickCompany>, power: f64) -> Vec<TickCompany> {
    let mut cap = 0.0;
    let mut weighted = 0.0;
    for c in companies.iter() {
        if !c.is_public || c.is_bankrupt {
            continue;
        }
        let m = c.stock.market_cap;
        if !(m.is_finite() && m > 0.0) {
            continue;
        }
        cap += m;
        weighted += m * c.stock.beta.unwrap_or(1.0);
    }
    if !(cap > 0.0) {
        return companies;
    }
    let b = weighted / cap;
    if !(b.is_finite() && b > 0.0) {
        return companies;
    }
    let k = if power == 1.0 { 1.0 / b } else { crate::mathx::pow(b, -power) };
    for c in companies.iter_mut() {
        if !c.is_public || c.is_bankrupt {
            continue;
        }
        let m = c.stock.market_cap;
        if !(m.is_finite() && m > 0.0) {
            continue;
        }
        c.stock.beta = Some(c.stock.beta.unwrap_or(1.0) * k);
    }
    companies
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::economy::{
        create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions,
    };
    use crate::market::TickStock;

    fn sectors() -> Vec<String> {
        ["technology", "energy", "healthcare"]
            .iter()
            .map(|s| s.to_string())
            .collect()
    }

    fn company(id: &str, price: f64) -> TickCompany {
        TickCompany {
            id: id.to_string(),
            ticker: id.to_string(),
            sector: "technology".to_string(),
            is_bankrupt: false,
            is_public: true,
            sector_volatility: Some(1.0),
            sector_avg_pe: Some(32.0),
            eps: Some(4.0),
            book_value_per_share: Some(20.0),
            revenue_growth: Some(0.1),
            stock: TickStock {
                price,
                previous_close: price,
                previous_tick_price: None,
                open: price,
                high: price,
                low: price,
                volume: 0.0,
                avg_volume: 1e6,
                shares_outstanding: 1e8,
                market_cap: price * 1e8,
                mispricing_s: None,
                mispricing_s_prev_close: None,
                mispricing_momentum: None,
                fair_value_offset: None,
                buyback_log_shares: None,
                dividend: None,
                maker_inventory: None,
                garch_variance: 0.015 * 0.015,
                garch_cascade: [0.015 * 0.015; crate::market::garch::CASCADE_MAX],
                last_daily_return: None,
                beta: Some(1.0),
                short_interest: 0.0,
                float: 1e8,
            },
        }
    }

    fn engine(seed: u64) -> Engine {
        Engine::new(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
        )
    }

    /// [`engine`] under pt-v19: the snapshot-priced flow path, where an
    /// agent's fills reach the first tick's `order_volumes`. pt-v20 and
    /// pt-v21 turn `fill_impact_coefficient` on and routes them
    /// through the agent-facing book's pending flow instead.
    fn engine_v19(seed: u64) -> Engine {
        Engine::with_params(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            crate::params::PT_V19,
        )
    }

    /// [`engine`] under pt-v20 by name: the default until 0.10.0, and the
    /// preset with every one of pt-v21's dials off, which the tests of those
    /// dials read as "off". They read `engine(seed)` while pt-v20 was the
    /// default.
    fn engine_v20(seed: u64) -> Engine {
        Engine::with_params(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            crate::params::PT_V20,
        )
    }

    /// The business cycle in the market factor's volatility
    /// (`market_vol_cycle_ratio` and its three companions).
    /// `market_day_tail_df` and `market_day_tail_state_share`: off, the day
    /// scale is never drawn, read or hashed; on, each open draws one on the
    /// overnight stream alone, the session's market sigma carries its root,
    /// the close clears it, and the state hash covers it while it is live.
    /// `market_beta_normalise`: off, every beta is the one the company came
    /// with; on, the roster's cap-weighted beta is divided out at
    /// construction, names outside the index keep theirs, and the market
    /// moves.
    mod market_beta_normalise {
        use super::*;

        fn roster() -> Vec<TickCompany> {
            let mut a = company("A", 100.0);
            let mut b = company("B", 50.0);
            let mut c = company("C", 220.0);
            let mut d = company("D", 80.0);
            a.stock.beta = Some(1.3);
            b.stock.beta = Some(0.8);
            c.stock.beta = None; // read as 1.0
            d.stock.beta = Some(2.0);
            d.is_bankrupt = true; // outside the index, keeps its beta
            vec![a, b, c, d]
        }

        fn with(d: f64) -> Engine {
            let mut params = crate::params::PT_V20;
            params.market_beta_normalise = d;
            Engine::with_params(
                13,
                roster(),
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
            )
        }

        fn cap_weighted_beta(e: &Engine) -> f64 {
            let (mut cap, mut w) = (0.0, 0.0);
            for c in e.companies().iter().filter(|c| c.is_public && !c.is_bankrupt) {
                cap += c.stock.market_cap;
                w += c.stock.market_cap * c.stock.beta.unwrap_or(1.0);
            }
            w / cap
        }

        #[test]
        fn off_every_beta_is_the_one_given() {
            let e = with(0.0);
            let got: Vec<Option<f64>> = e.companies().iter().map(|c| c.stock.beta).collect();
            let want: Vec<Option<f64>> = roster().iter().map(|c| c.stock.beta).collect();
            assert_eq!(got, want);
        }

        #[test]
        fn on_the_roster_beta_is_one_and_the_ratios_hold() {
            let before = {
                let r = roster();
                let (mut cap, mut w) = (0.0, 0.0);
                for c in r.iter().filter(|c| !c.is_bankrupt) {
                    cap += c.stock.market_cap;
                    w += c.stock.market_cap * c.stock.beta.unwrap_or(1.0);
                }
                w / cap
            };
            assert!((before - 1.0).abs() > 0.05, "{before}");
            let e = with(1.0);
            assert!((cap_weighted_beta(&e) - 1.0).abs() < 1e-12, "{}", cap_weighted_beta(&e));
            let b: Vec<f64> = e.companies().iter().map(|c| c.stock.beta.unwrap()).collect();
            assert!((b[0] / b[1] - 1.3 / 0.8).abs() < 1e-12);
            assert!((b[2] - 1.0 / before).abs() < 1e-12, "an absent beta reads 1.0");
            assert_eq!(b[3], 2.0, "a bankrupt name is outside the index and keeps its beta");
            // Half the power leaves half the log distance.
            let h = with(0.5);
            assert!((cap_weighted_beta(&h) - before.sqrt()).abs() < 1e-12);
        }

        #[test]
        fn on_the_market_moves() {
            let mut on = with(1.0);
            let mut off = with(0.0);
            for e in [&mut on, &mut off] {
                e.open_market();
                for m in 0..10 {
                    e.tick(&request(10, m));
                }
            }
            assert_ne!(on.prices(), off.prices());
        }
    }

    mod market_day_tail {
        use super::*;

        fn with(dials: impl FnOnce(&mut crate::params::ModelParams)) -> Engine {
            let mut params = crate::params::PT_V20;
            dials(&mut params);
            Engine::with_params(
                13,
                vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
            )
        }

        fn session_and_close(e: &mut Engine) {
            for m in 0..10 {
                e.tick(&request(10, m));
            }
            let innovations: Vec<Option<f64>> =
                e.daily_innovation_column().into_iter().map(Some).collect();
            let variances = e.sector_base_variances();
            e.close_market(&DayCloseRequest {
                daily_innovations: &innovations,
                sector_base_variances: &variances,
                avg_volume: crate::market::AvgVolumePolicy::Hold,
            });
        }

        #[test]
        fn off_the_day_scale_is_never_drawn_or_hashed() {
            let mut off = with(|_| {});
            assert_eq!(off.params().market_day_tail_df, 0.0);
            let mut twin = off.clone();
            for _ in 0..3 {
                off.open_market();
                assert_eq!(off.market_day_scale(), 1.0);
                assert_eq!(off.market_sigma_today(), off.market_sigma_before_day_scale());
                session_and_close(&mut off);
            }
            // The state share alone reads nothing.
            let mut share = with(|p| p.market_day_tail_state_share = 1.0);
            for _ in 0..3 {
                twin.open_market();
                share.open_market();
                session_and_close(&mut twin);
                session_and_close(&mut share);
            }
            assert_eq!(share.prices(), twin.prices());
        }

        #[test]
        fn on_the_open_draws_a_scale_the_session_reads_and_the_close_clears() {
            let mut on = with(|p| p.market_day_tail_df = 6.0);
            let mut off = with(|_| {});
            on.open_market();
            off.open_market();
            let m = on.market_day_scale();
            assert!(m > 0.0 && m != 1.0 && m.is_finite(), "{m}");
            let sigma = on.market_sigma_before_day_scale();
            assert!((on.market_sigma_today() - sigma * m.sqrt()).abs() <= 1e-15 * sigma.max(1.0));
            // Only the overnight stream moved.
            assert_eq!(on.market_rng.snapshot(), off.market_rng.snapshot());
            assert_ne!(on.overnight_rng.snapshot(), off.overnight_rng.snapshot());
            // Hashed while live: the same state with another scale hashes apart.
            let mut other = on.clone();
            other.set_market_day_scale(m * 1.5);
            assert_ne!(other.state_hash(0, false), on.state_hash(0, false));
            session_and_close(&mut on);
            assert_eq!(on.market_day_scale(), 1.0);
        }

        #[test]
        fn the_scale_has_mean_one() {
            let nu = 10.0;
            let mut e = with(|p| p.market_day_tail_df = nu);
            let n = 4000;
            let mut sum = 0.0;
            for _ in 0..n {
                e.draw_market_day_scale();
                sum += e.market_day_scale();
            }
            let mean = sum / n as f64;
            // Inverse gamma: variance 2 / (nu - 4).
            let se = (2.0 / (nu - 4.0) / n as f64).sqrt();
            assert!((mean - 1.0).abs() < 4.0 * se, "{mean}");
        }
    }

    mod market_vol_cycle {
        use super::*;
        use crate::economy::CyclePhase;

        fn with(dials: impl FnOnce(&mut crate::params::ModelParams)) -> Engine {
            let mut params = crate::params::PT_V20;
            dials(&mut params);
            Engine::with_params(
                11,
                vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
            )
        }

        /// The four dials, with `relative` as the power on both sides of
        /// one (`market_vol_cycle_relative` and
        /// `market_vol_cycle_relative_calm` alike).
        fn on(ratio: f64, expansion: f64, half_life: f64, relative: f64) -> Engine {
            on_split(ratio, expansion, half_life, relative, relative)
        }

        fn on_split(ratio: f64, expansion: f64, half_life: f64, relative: f64, calm: f64) -> Engine {
            with(|p| {
                p.market_vol_cycle_ratio = ratio;
                p.market_vol_cycle_expansion = expansion;
                p.market_vol_cycle_half_life = half_life;
                p.market_vol_cycle_relative = relative;
                p.market_vol_cycle_relative_calm = calm;
            })
        }

        /// A session of ticks and the market's close, without the macro
        /// step, so the economy the close read is still there to compare.
        fn session_and_close(e: &mut Engine) {
            e.open_market();
            for m in 0..10 {
                e.tick(&request(10, m));
            }
            let innovations: Vec<Option<f64>> =
                e.daily_innovation_column().into_iter().map(Some).collect();
            let variances = e.sector_base_variances();
            e.close_market(&DayCloseRequest {
                daily_innovations: &innovations,
                sector_base_variances: &variances,
                avg_volume: crate::market::AvgVolumePolicy::Hold,
            });
        }

        #[test]
        fn off_the_multiplier_is_never_set_read_or_hashed() {
            // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
            // this test reads as off. Each `engine_v20` here read `engine`.
            let mut e = engine_v20(11);
            assert_eq!(e.params().market_vol_cycle_ratio, 0.0);
            for day in 1..=5 {
                e.close_day(day);
            }
            assert_eq!(e.market_vol_cycle_log(), None);
            assert!(!e.carries_market_vol_cycle());
            assert_eq!(e.market_vol_cycle_vix_scale(), 1.0);
            // The companions alone are unread.
            let mut c = on(0.0, 0.75, 21.0, 1.0);
            for day in 1..=5 {
                c.close_day(day);
            }
            assert_eq!(c.market_vol_cycle_log(), None);
            let mut d = engine_v20(11);
            for day in 1..=5 {
                d.close_day(day);
            }
            assert_eq!(c.market_variance_state(), d.market_variance_state());
        }

        #[test]
        fn the_target_reads_the_true_phase() {
            let mut e = on(2.0, 0.75, 21.0, 1.0);
            for (phase, k) in [
                (CyclePhase::Expansion, 0.75),
                (CyclePhase::Peak, 0.75),
                (CyclePhase::Contraction, 1.5),
                (CyclePhase::Trough, 1.5),
                (CyclePhase::Recovery, 0.75),
            ] {
                e.economy_mut().cycle_phase = phase;
                let got = e.market_vol_cycle_target_log();
                assert!((got - crate::mathx::log(k)).abs() < 1e-15, "{phase:?}: {got}");
            }
        }

        #[test]
        fn the_derived_expansion_multiplier_keeps_the_share_weighted_variance() {
            let mut e = on(2.0, 0.0, 0.0, 1.0);
            let (mean, cycle) =
                crate::economy::cycle::stationary_phase_shares_for(&e.cycle_spec());
            let mut weighted = 0.0;
            for (k, phase) in crate::economy::cycle::phase_cycle().iter().enumerate() {
                e.economy_mut().cycle_phase = *phase;
                let m = crate::mathx::exp(e.market_vol_cycle_target_log());
                weighted += mean[k] / cycle * m * m;
            }
            assert!((weighted - 1.0).abs() < 1e-12, "{weighted}");
            // And the contraction's is R times the expansion's.
            e.economy_mut().cycle_phase = CyclePhase::Expansion;
            let exp = e.market_vol_cycle_target_log();
            e.economy_mut().cycle_phase = CyclePhase::Trough;
            let tr = e.market_vol_cycle_target_log();
            assert!((tr - exp - crate::mathx::log(2.0)).abs() < 1e-14);
            assert!(exp < 0.0);
        }

        #[test]
        fn the_first_step_lands_on_the_target_and_the_gap_halves_at_the_half_life() {
            let mut e = on(2.0, 0.75, 10.0, 1.0);
            e.economy_mut().cycle_phase = CyclePhase::Expansion;
            let first = e.step_market_vol_cycle();
            assert_eq!(first, crate::mathx::log(0.75));
            e.economy_mut().cycle_phase = CyclePhase::Contraction;
            let target = crate::mathx::log(1.5);
            let mut l = first;
            for _ in 0..10 {
                l = e.step_market_vol_cycle();
            }
            let half = 0.5 * (target - first);
            assert!(((target - l) - half).abs() < 1e-12, "{l}");
            // Instant at a half-life of 0.
            let mut i = on(2.0, 0.75, 0.0, 1.0);
            i.economy_mut().cycle_phase = CyclePhase::Expansion;
            i.step_market_vol_cycle();
            i.economy_mut().cycle_phase = CyclePhase::Trough;
            assert_eq!(i.step_market_vol_cycle(), target);
        }

        #[test]
        fn the_vix_scale_is_the_multiplier_to_the_power_relative() {
            let mut e = on(2.0, 0.75, 21.0, 0.5);
            assert_eq!(e.market_vol_cycle_vix_scale(), 1.0, "before the first close");
            e.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(e.market_vol_cycle_vix_scale(), crate::mathx::exp(0.2));
            let mut z = on(2.0, 0.75, 21.0, 0.0);
            z.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(z.market_vol_cycle_vix_scale(), 1.0);
        }

        /// The power on each side of one: `relative` at or over it, and
        /// `relative_calm` under it.
        #[test]
        fn the_vix_scale_takes_the_calm_power_under_one() {
            let mut e = on_split(2.0, 0.75, 21.0, 0.8, 0.3);
            e.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(e.market_vol_cycle_vix_scale(), crate::mathx::exp(0.8 * 0.4));
            e.set_market_vol_cycle_log(Some(-0.25));
            assert_eq!(e.market_vol_cycle_vix_scale(), crate::mathx::exp(0.3 * -0.25));
            e.set_market_vol_cycle_log(Some(0.0));
            assert_eq!(e.market_vol_cycle_vix_scale(), 1.0);
            // A calm power of 0 leaves a calm phase's fear on the
            // unconditional level, and a stormy one still scaled.
            let mut c = on_split(2.0, 0.75, 21.0, 1.0, 0.0);
            c.set_market_vol_cycle_log(Some(-0.25));
            assert_eq!(c.market_vol_cycle_vix_scale(), 1.0);
            c.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(c.market_vol_cycle_vix_scale(), crate::mathx::exp(0.4));
        }

        /// The fair-value cap's ceiling is scaled by the multiplier to the
        /// power `market_vol_cycle_cap_relative` over one only, and the
        /// permanent share reads the scaled ceiling.
        #[test]
        fn the_cap_scale_reads_a_stormy_phase_only() {
            let mut e = with(|p| {
                p.market_vol_cycle_ratio = 2.0;
                p.market_vol_cycle_expansion = 0.75;
                p.market_vol_cycle_cap_relative = 0.5;
            });
            assert_eq!(e.market_vol_cycle_cap_scale(), 1.0, "before the first close");
            e.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(e.market_vol_cycle_cap_scale(), crate::mathx::exp(0.2));
            e.set_market_vol_cycle_log(Some(-0.3));
            assert_eq!(e.market_vol_cycle_cap_scale(), 1.0);
            let mut z = on(2.0, 0.75, 21.0, 1.0);
            z.set_market_vol_cycle_log(Some(0.4));
            assert_eq!(z.market_vol_cycle_cap_scale(), 1.0, "power 0");
            let p = e.params().clone();
            let base = p.fair_value_market_vol_cap * p.market_factor_sigma;
            let sigma = 1.5 * base;
            let unscaled = crate::market::tick::market_permanent_share(&p, sigma, 1.0);
            assert!((unscaled - p.fair_value_market_share / 1.5).abs() < 1e-15);
            // A ceiling scaled past the sigma leaves the whole share.
            assert_eq!(crate::market::tick::market_permanent_share(&p, sigma, 1.6),
                       p.fair_value_market_share);
        }

        /// The scale never takes the denominator or the anchor under the
        /// VIX's own floor.
        #[test]
        fn the_vix_scale_is_floored_at_the_vix_floor_over_the_anchor() {
            let mut e = on(0.05, 1.0, 0.0, 1.0);
            // A close derives the anchor and sets the multiplier.
            e.close_day(1);
            assert!(e.vix_anchor > 0.0);
            let floor = crate::params::VIX_STATE_FLOOR / e.vix_anchor;
            e.set_market_vol_cycle_log(Some(crate::mathx::log(0.05)));
            assert_eq!(e.market_vol_cycle_vix_scale(), floor);
            // Above the floor it is the power, untouched.
            e.set_market_vol_cycle_log(Some(-0.1));
            assert_eq!(e.market_vol_cycle_vix_scale(), crate::mathx::exp(-0.1));
        }

        /// A free close scales the variance baseline by the multiplier
        /// squared: at `relative` 0 the coupling reads the same ratio, so
        /// the fast target is the off engine's times `m^2`. The first
        /// session trades identically on both engines (nothing is applied
        /// before a close), so the close reads the same state.
        #[test]
        fn a_free_close_scales_the_baseline_by_the_multiplier_squared() {
            // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
            // this test reads as off. Each `engine_v20` here read `engine`.
            let mut off = engine_v20(11);
            let mut e = on(2.0, 0.75, 21.0, 0.0);
            let phase = e.economy().cycle_phase;
            assert_eq!(phase, off.economy().cycle_phase);
            let k: f64 = match phase {
                CyclePhase::Contraction | CyclePhase::Trough => 1.5,
                _ => 0.75,
            };
            session_and_close(&mut off);
            session_and_close(&mut e);
            let (t_off, _) = off.market_variance_target().unwrap();
            let (t_on, _) = e.market_variance_target().unwrap();
            assert!((t_on / t_off - k * k).abs() < 1e-12, "{} against {}", t_on / t_off, k * k);
            assert_ne!(e.market_variance_state(), off.market_variance_state());
            // At `relative` 1 the coupling reads fear against the phase's
            // level instead, which is a different target.
            let mut d1 = on(2.0, 0.75, 21.0, 1.0);
            session_and_close(&mut d1);
            let (t_d1, _) = d1.market_variance_target().unwrap();
            assert_ne!(t_d1, t_on);
        }

        /// The anchor's slow memory reads the day's fear against the
        /// SCALED anchor. A forced close writes the same variance on both
        /// engines, so the read-back is the same, and the memory of an
        /// engine at power 1 then differs from one at power 0 by exactly
        /// `-h ln(scale)`, `h` being `vix_anchor_memory`.
        #[test]
        fn the_anchor_memory_reads_fear_against_the_scaled_anchor() {
            let mut a = on(2.0, 0.75, 0.0, 1.0);
            let mut b = on(2.0, 0.75, 0.0, 0.0);
            assert!(a.params().vix_anchor_memory > 0.0);
            for e in [&mut a, &mut b] {
                e.set_vix_sets_variance_pending(true);
                session_and_close(e);
            }
            assert_eq!(a.market_variance_state(), b.market_variance_state());
            assert_eq!(a.vix_anchor_slow(), b.vix_anchor_slow());
            let scale = a.market_vol_cycle_vix_scale();
            assert_ne!(scale, 1.0);
            assert_eq!(b.market_vol_cycle_vix_scale(), 1.0);
            a.advance_macro_day(1);
            b.advance_macro_day(1);
            let want = -a.params().vix_anchor_memory * crate::mathx::log(scale);
            let got = a.vix_anchor_slow() - b.vix_anchor_slow();
            assert!((got - want).abs() < 1e-12, "{got} against {want}");
        }

        /// The level the VIX reverts to is the scaled anchor. Without the
        /// slow memory the VIX's target is the geometric blend of the
        /// read-back and the anchor level, so after a forced close (the
        /// same read-back on both engines) a scale under one gives a lower
        /// VIX and a scale over one a higher one.
        #[test]
        fn the_vix_reverts_toward_the_scaled_anchor_level() {
            let mk = |d: f64| {
                with(|p| {
                    p.market_vol_cycle_ratio = 2.0;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_relative = d;
                    p.market_vol_cycle_relative_calm = d;
                    p.vix_anchor_memory = 0.0;
                })
            };
            let (mut a, mut b) = (mk(1.0), mk(0.0));
            for e in [&mut a, &mut b] {
                e.set_vix_sets_variance_pending(true);
                session_and_close(e);
            }
            assert_eq!(a.market_variance_state(), b.market_variance_state());
            let scale = a.market_vol_cycle_vix_scale();
            assert_ne!(scale, 1.0);
            a.advance_macro_day(1);
            b.advance_macro_day(1);
            assert_ne!(a.economy().vix, b.economy().vix);
            assert_eq!(a.economy().vix < b.economy().vix, scale < 1.0);
        }

        /// A forced close (a VIX a scenario pinned) moves the multiplier
        /// but writes the variance the off engine writes.
        #[test]
        fn a_forced_close_moves_the_multiplier_but_does_not_apply_it() {
            // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
            // this test reads as off. Each `engine_v20` here read `engine`.
            let mut off = engine_v20(11);
            let mut e = on(2.0, 0.75, 21.0, 1.0);
            off.set_vix_sets_variance_pending(true);
            e.set_vix_sets_variance_pending(true);
            session_and_close(&mut off);
            session_and_close(&mut e);
            assert_eq!(e.market_variance_state(), off.market_variance_state());
            assert_eq!(e.market_variance_target(), off.market_variance_target());
            assert!(e.market_vol_cycle_log().is_some());
        }

        /// The trough gives back the share `market_vol_cycle_trough_release`
        /// of the contraction's excess, in logs; at 0.0 it is the
        /// contraction's, and the contraction's own is untouched.
        #[test]
        fn the_trough_gives_back_its_share_of_the_contraction() {
            for g in [0.0, 0.25, 1.0] {
                let mut e = with(|p| {
                    p.market_vol_cycle_ratio = 2.0;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_trough_release = g;
                });
                e.economy_mut().cycle_phase = CyclePhase::Trough;
                let got = e.market_vol_cycle_target_log();
                let want = crate::mathx::log(0.75) + (1.0 - g) * crate::mathx::log(2.0);
                assert!((got - want).abs() < 1e-14, "g {g}: {got} against {want}");
                e.economy_mut().cycle_phase = CyclePhase::Contraction;
                assert_eq!(e.market_vol_cycle_target_log(), crate::mathx::log(1.5));
            }
            // At 0.0 the trough's target is the contraction's to the bit.
            let mut z = on(2.0, 0.75, 21.0, 1.0);
            z.economy_mut().cycle_phase = CyclePhase::Trough;
            assert_eq!(z.market_vol_cycle_target_log(), crate::mathx::log(1.5));
        }

        /// The rally off the low gives back `g s` of the excess in a
        /// contraction and of the trough's own share in a trough, `s` the
        /// index's rise off its lowest close since its high over the scale,
        /// capped at one; outside the two phases, and with the dial at 0.0,
        /// the target is as it was.
        #[test]
        fn the_rally_off_the_low_gives_back_its_share() {
            let mk = |g: f64, trough: f64| {
                with(|p| {
                    p.market_vol_cycle_ratio = 2.0;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_trough_release = trough;
                    p.market_vol_cycle_recovery_release = g;
                    p.market_vol_cycle_recovery_scale = 0.2;
                })
            };
            // Up 0.1 to the high, down 0.3 to the low, up 0.05 off it.
            let path = [0.05, 0.05, -0.2, -0.1, 0.02, 0.03];
            let (lk, lr) = (crate::mathx::log(0.75), crate::mathx::log(2.0));
            for (g, trough) in [(1.0, 0.0), (0.5, 0.0), (1.0, 0.4)] {
                let mut e = mk(g, trough);
                assert!(e.keeps_drawdown_window());
                assert_eq!(e.index_rally_off_low(), 0.0);
                e.drawdown_returns = path.iter().copied().collect();
                let rally = e.index_rally_off_low();
                assert!((rally - 0.05).abs() < 1e-12, "{rally}");
                let s = rally / 0.2;
                e.economy_mut().cycle_phase = CyclePhase::Contraction;
                let want = lk + (1.0 - g * s) * lr;
                assert!((e.market_vol_cycle_target_log() - want).abs() < 1e-14);
                e.economy_mut().cycle_phase = CyclePhase::Trough;
                let want = lk + (1.0 - trough) * (1.0 - g * s) * lr;
                assert!((e.market_vol_cycle_target_log() - want).abs() < 1e-14);
                e.economy_mut().cycle_phase = CyclePhase::Expansion;
                assert_eq!(e.market_vol_cycle_target_log(), lk);
                // A rally past the scale, still under the high, gives back
                // all of g.
                e.drawdown_returns.push_back(0.2);
                e.economy_mut().cycle_phase = CyclePhase::Contraction;
                let want = lk + (1.0 - g) * lr;
                assert!((e.market_vol_cycle_target_log() - want).abs() < 1e-14);
                // A new high: no rally, the contraction's own target.
                e.drawdown_returns.push_back(0.5);
                assert_eq!(e.index_rally_off_low(), 0.0);
                assert!((e.market_vol_cycle_target_log() - (lk + lr)).abs() < 1e-14);
            }
            // At 0.0 no window is kept and a window read changes nothing.
            let mut z = mk(0.0, 0.0);
            assert!(!z.keeps_drawdown_window());
            assert!(z.drawdown_state().is_none());
            z.drawdown_returns = path.iter().copied().collect();
            z.economy_mut().cycle_phase = CyclePhase::Contraction;
            assert_eq!(z.market_vol_cycle_target_log(), crate::mathx::log(1.5));
        }

        /// The release half-life is read only while the multiplier falls;
        /// a rising multiplier, and every step at 0.0, takes the one
        /// half-life.
        #[test]
        fn the_release_half_life_is_read_only_on_the_way_down() {
            let mk = |release: f64| {
                with(|p| {
                    p.market_vol_cycle_ratio = 2.0;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_half_life = 20.0;
                    p.market_vol_cycle_release_half_life = release;
                })
            };
            let step = |h: f64, from: f64, to: f64| {
                from + (1.0 - crate::mathx::exp(-std::f64::consts::LN_2 / h)) * (to - from)
            };
            let (up, down) = (crate::mathx::log(1.5), crate::mathx::log(0.75));
            // Down: from the contraction's level to the expansion's.
            let mut e = mk(5.0);
            e.economy_mut().cycle_phase = CyclePhase::Expansion;
            e.set_market_vol_cycle_log(Some(up));
            assert_eq!(e.step_market_vol_cycle(), step(5.0, up, down));
            // Up: the one half-life.
            e.economy_mut().cycle_phase = CyclePhase::Contraction;
            e.set_market_vol_cycle_log(Some(down));
            assert_eq!(e.step_market_vol_cycle(), step(20.0, down, up));
            // Off: the one half-life both ways.
            let mut o = mk(0.0);
            o.economy_mut().cycle_phase = CyclePhase::Expansion;
            o.set_market_vol_cycle_log(Some(up));
            assert_eq!(o.step_market_vol_cycle(), step(20.0, up, down));
        }

        /// `market_vol_cycle_pin_neutral`: on a session whose VIX a caller
        /// pinned, the close writes the variance target an engine without
        /// the multiplier writes, and the multiplier lands on one (its
        /// first step lands on its target, which a pin makes one). Without
        /// the switch the same pinned session applies it.
        #[test]
        fn a_pinned_vix_session_does_not_apply_the_multiplier_under_the_switch() {
            let run = |ratio: f64, switch: f64| {
                let mut e = with(|p| {
                    p.market_vol_cycle_ratio = ratio;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_half_life = 21.0;
                    p.market_vol_cycle_relative = 1.0;
                    p.market_vol_cycle_relative_calm = 1.0;
                    p.market_vol_cycle_cap_relative = 1.0;
                    p.market_vol_cycle_pin_neutral = switch;
                });
                e.mark_macro_pins_today(PIN_VIX);
                assert_ne!(e.macro_pins_today() & PIN_VIX, 0, "the pin is kept");
                session_and_close(&mut e);
                e
            };
            let off = run(0.0, 0.0);
            let on = run(2.0, 1.0);
            assert_eq!(on.market_variance_target(), off.market_variance_target());
            assert_eq!(on.market_variance_state(), off.market_variance_state());
            assert_eq!(on.market_vol_cycle_log(), Some(0.0));
            assert_eq!(on.market_vol_cycle_vix_scale(), 1.0);
            assert_eq!(on.market_vol_cycle_cap_scale(), 1.0);
            let unswitched = run(2.0, 0.0);
            assert_ne!(unswitched.market_variance_target(), off.market_variance_target());
            assert_ne!(unswitched.market_vol_cycle_log(), Some(0.0));
            // An unpinned session under the switch applies the multiplier.
            let mut free = with(|p| {
                p.market_vol_cycle_ratio = 2.0;
                p.market_vol_cycle_expansion = 0.75;
                p.market_vol_cycle_pin_neutral = 1.0;
            });
            let mut plain = with(|p| {
                p.market_vol_cycle_ratio = 2.0;
                p.market_vol_cycle_expansion = 0.75;
            });
            session_and_close(&mut free);
            session_and_close(&mut plain);
            assert_eq!(free.market_variance_target(), plain.market_variance_target());
            assert_eq!(free.market_vol_cycle_log(), plain.market_vol_cycle_log());
        }

        /// `market_vol_cycle_pin_phase`: the same rule on a session whose
        /// phase a caller pinned; a pinned VIX alone does not trigger it.
        #[test]
        fn a_pinned_phase_session_does_not_apply_the_multiplier_under_its_switch() {
            let run = |ratio: f64, bits: u16| {
                let mut e = with(|p| {
                    p.market_vol_cycle_ratio = ratio;
                    p.market_vol_cycle_expansion = 0.75;
                    p.market_vol_cycle_pin_phase = 1.0;
                });
                e.mark_macro_pins_today(bits);
                session_and_close(&mut e);
                e
            };
            let off = run(0.0, PIN_CYCLE);
            let on = run(2.0, PIN_CYCLE);
            assert_eq!(on.market_variance_target(), off.market_variance_target());
            assert_eq!(on.market_vol_cycle_log(), Some(0.0));
            let vix_only = run(2.0, PIN_VIX);
            assert_ne!(vix_only.market_variance_target(), off.market_variance_target());
        }

        /// Under a pin the multiplier relaxes toward one at its half-life,
        /// so a released run steps from there rather than jumping to its
        /// phase's value.
        #[test]
        fn a_pinned_session_relaxes_the_multiplier_toward_one() {
            let mut e = with(|p| {
                p.market_vol_cycle_ratio = 2.0;
                p.market_vol_cycle_expansion = 0.75;
                p.market_vol_cycle_half_life = 10.0;
                p.market_vol_cycle_pin_neutral = 1.0;
            });
            e.economy_mut().cycle_phase = CyclePhase::Contraction;
            e.set_market_vol_cycle_log(Some(0.4));
            e.mark_macro_pins_today(PIN_VIX);
            let a = 1.0 - crate::mathx::exp(-std::f64::consts::LN_2 / 10.0);
            assert_eq!(e.step_market_vol_cycle(), 0.4 + a * (0.0 - 0.4));
        }

        #[test]
        fn the_state_hash_carries_the_multiplier_only_while_set() {
            let mut e = on(2.0, 0.75, 21.0, 1.0);
            assert!(!e.carries_market_vol_cycle(), "unset before the first close");
            e.close_day(1);
            assert!(e.carries_market_vol_cycle());
            let before = e.state_hash(1, false);
            let mut other = e.clone();
            other.set_market_vol_cycle_log(Some(e.market_vol_cycle_log().unwrap() + 0.01));
            assert_ne!(other.state_hash(1, false), before);
        }
    }

    /// The time-held fade (`market_vol_cycle_hold_onset`,
    /// `market_vol_cycle_hold_fade`, `market_vol_vix_hold_half_life`): the
    /// clock counts a phase the host writes, the onset is the phase's own
    /// sojourn quantile, and off it nothing is kept, read or hashed.
    mod cycle_hold {
        use super::*;
        use crate::economy::CyclePhase;

        fn with(dials: impl FnOnce(&mut crate::params::ModelParams)) -> Engine {
            let mut params = crate::params::PT_V21;
            dials(&mut params);
            params.invariants().unwrap();
            Engine::with_params(
                11,
                vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
            )
        }

        fn on(fade: f64, fade_half_life: f64, vix_half_life: f64) -> Engine {
            with(|p| {
                p.market_vol_cycle_hold_onset = 0.9;
                p.market_vol_cycle_hold_fade = fade;
                p.market_vol_cycle_hold_half_life = fade_half_life;
                p.market_vol_vix_hold_half_life = vix_half_life;
            })
        }

        #[test]
        fn the_onset_is_the_phases_own_ninetieth_percentile() {
            let e = on(0.335, 295.0, 198.0);
            let spec = e.cycle_spec();
            let q = |p| crate::economy::cycle::sojourn_quantile_days_for(p, &spec, 0.9);
            // pt-v21's US table on the session calendar: 158 and 111 sessions.
            assert_eq!(q(CyclePhase::Contraction), 158);
            assert_eq!(q(CyclePhase::Trough), 111);
            // No sooner than the phase's minimum (four and two months).
            assert!(crate::economy::cycle::sojourn_quantile_days_for(CyclePhase::Trough, &spec, 0.0001) >= 42);
        }

        #[test]
        fn off_nothing_is_counted_kept_or_hashed() {
            let mut e = with(|_| {});
            let plain = with(|_| {});
            for day in 1..=5 {
                e.economy_mut().cycle_phase = CyclePhase::Trough;
                e.close_day(day);
            }
            assert_eq!(e.cycle_hold, None);
            assert_eq!(e.vix_hold_log, None);
            assert!(e.cycle_hold_words().is_none());
            assert_eq!(e.market_vol_cycle_held_target(0.4).to_bits(), 0.4f64.to_bits());
            assert_eq!(plain.vix_hold_next(23.0), (None, 23.0));
        }

        #[test]
        fn a_written_phase_is_counted_and_a_turn_restarts_the_clock() {
            let mut e = on(0.335, 295.0, 198.0);
            for day in 1..=30 {
                // Written as a host writes it, before the close; the economy's
                // own step may turn it after.
                e.economy_mut().cycle_phase = CyclePhase::Trough;
                e.close_day(day);
                assert_eq!(e.cycle_hold, Some((CyclePhase::Trough, day)));
            }
            e.economy_mut().cycle_phase = CyclePhase::Recovery;
            e.close_day(31);
            assert_eq!(e.cycle_hold, Some((CyclePhase::Recovery, 1)));
        }

        #[test]
        fn the_multiplier_fades_only_past_the_onset() {
            let mut e = on(0.5, 0.0, 198.0);
            e.economy_mut().cycle_phase = CyclePhase::Trough;
            // Tonight's age is the held age plus one.
            e.cycle_hold = Some((CyclePhase::Trough, 110));
            assert_eq!(e.market_vol_cycle_held_target(0.4), 0.4, "age 111 is the onset itself");
            e.cycle_hold = Some((CyclePhase::Trough, 111));
            assert_eq!(e.market_vol_cycle_held_target(0.4), 0.2, "the whole share at half-life 0");
            assert_eq!(e.market_vol_cycle_held_target(-0.1), -0.1, "a calm multiplier is left alone");
            e.economy_mut().cycle_phase = CyclePhase::Recovery;
            assert_eq!(e.market_vol_cycle_held_target(0.4), 0.4, "a new phase starts its own clock");
            // At a half-life, half the share is gone that many closes past.
            let mut h = on(0.5, 10.0, 198.0);
            h.economy_mut().cycle_phase = CyclePhase::Trough;
            h.cycle_hold = Some((CyclePhase::Trough, 120));
            assert!((h.market_vol_cycle_held_target(0.4) - 0.4 * (1.0 - 0.5 * 0.5)).abs() < 1e-15);
        }

        #[test]
        fn the_denominator_follows_a_held_vix_and_never_falls_below_its_own() {
            let mut e = on(0.335, 295.0, 10.0);
            e.economy_mut().cycle_phase = CyclePhase::Trough;
            e.cycle_hold = Some((CyclePhase::Trough, 50));
            assert_eq!(e.vix_hold_next(23.0), (None, 23.0), "before the onset");
            e.cycle_hold = Some((CyclePhase::Trough, 111));
            e.economy.vix = 34.0;
            let (s, d) = e.vix_hold_next(23.0);
            assert_eq!((s, d), (Some(crate::mathx::log(23.0)), 23.0), "opens at the close's own denominator");
            e.vix_hold_log = s;
            e.cycle_hold = Some((CyclePhase::Trough, 112));
            let (s2, d2) = e.vix_hold_next(23.0);
            let a = 1.0 - crate::mathx::pow(0.5, 0.1);
            let want = crate::mathx::log(23.0) + a * (crate::mathx::log(34.0) - crate::mathx::log(23.0));
            assert_eq!(s2, Some(want));
            assert_eq!(d2, crate::mathx::exp(want));
            // A VIX under the denominator moves the average but never the
            // denominator below its own.
            e.economy.vix = 12.0;
            let (_, d3) = e.vix_hold_next(23.0);
            assert_eq!(d3, 23.0);
        }

        #[test]
        fn the_state_round_trips_and_is_hashed() {
            let mut e = on(0.335, 295.0, 198.0);
            assert!(!e.carries_cycle_hold(), "unset before the first close");
            e.economy_mut().cycle_phase = CyclePhase::Trough;
            e.close_day(1);
            assert!(e.carries_cycle_hold());
            e.cycle_hold = Some((CyclePhase::Trough, 140));
            e.vix_hold_log = Some(3.3);
            let words = e.cycle_hold_words().unwrap();
            assert_eq!(words, [3.0, 140.0, 1.0, 3.3]);
            let before = e.state_hash(1, false);
            let mut other = e.clone();
            other.set_cycle_hold_state(Some(&words)).unwrap();
            assert_eq!(other.state_hash(1, false), before);
            other.vix_hold_log = Some(3.31);
            assert_ne!(other.state_hash(1, false), before);
            assert!(other.set_cycle_hold_state(Some(&[5.0, 1.0, 0.0, 0.0])).is_err());
            assert!(other.set_cycle_hold_state(Some(&[3.0, 0.0, 0.0, 0.0])).is_err());
            assert!(other.set_cycle_hold_state(Some(&[3.0, 2.0, 0.0, 1.0])).is_err());
            let mut plain = with(|_| {});
            assert!(plain.set_cycle_hold_state(Some(&words)).is_err());
        }

        #[test]
        fn the_fades_need_the_onset() {
            let mut p = crate::params::PT_V21;
            p.market_vol_cycle_hold_fade = 0.335;
            assert!(p.invariants().is_err());
            let mut p = crate::params::PT_V21;
            p.market_vol_vix_hold_half_life = 198.0;
            assert!(p.invariants().is_err());
            let mut p = crate::params::PT_V21;
            p.market_vol_cycle_hold_onset = 1.0;
            assert!(p.invariants().is_err());
        }
    }

    /// `cycle_publication_lag`: off, the published phase is the true one and
    /// no history is kept or hashed; on, the published phase is the phase
    /// of `lag` closes before, the opening phase until then, and a pin is
    /// the true phase at once and published `lag` closes after the close
    /// that carried it.
    #[test]
    fn the_published_phase_lags_the_true_one_only_under_the_dial() {
        use crate::economy::CyclePhase;
        // "Off" is pt-v19: the default, pt-v20, sets the lag since its
        // graded arm (2026-09-26). Was `engine(7)`, the default.
        let off = engine_v19(7);
        assert!(off.cycle_history().is_empty());
        assert_eq!(off.published_cycle_phase(), off.economy().cycle_phase);

        let lag = 4usize;
        let params = crate::params::ModelParams {
            cycle_publication_lag: lag as f64,
            ..crate::params::PT_V20
        };
        let mut e = Engine::with_params(
            7,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            params,
        );
        let opening = e.economy().cycle_phase;
        assert_eq!(e.cycle_history().len(), lag + 1);
        let turned = if opening == CyclePhase::Trough { CyclePhase::Peak } else { CyclePhase::Trough };
        e.economy_mut().cycle_phase = turned;
        assert_eq!(e.published_cycle_phase(), opening);
        let mut truth = vec![opening];
        for day in 1..=12i64 {
            e.advance_macro_day(day);
            truth.push(e.economy().cycle_phase);
            let d = day as usize;
            assert_eq!(e.published_cycle_phase(), truth[d.saturating_sub(lag)], "day {d}");
        }
        assert_ne!(truth[1], opening);
        // Hashed while set: the same engine with one phase of history
        // changed hashes apart.
        let before = e.state_hash(12, false);
        let mut other = e.clone();
        let mut history: Vec<CyclePhase> = other.cycle_history().iter().copied().collect();
        history[0] = if history[0] == CyclePhase::Expansion {
            CyclePhase::Recovery
        } else {
            CyclePhase::Expansion
        };
        other.set_cycle_history(history).unwrap();
        assert_ne!(other.state_hash(12, false), before);
        assert!(other.set_cycle_history(vec![opening; lag]).is_err());
        assert!(engine_v19(7).clone().set_cycle_history(vec![opening]).is_err());
    }

    /// The engine the nowcast tests read: pt-v20 by name (the default until
    /// 0.10.0; pt-v21 sets both dials), with
    /// `cycle_nowcast_accuracy` and `corporate_spread_cycle` as given.
    fn engine_nowcast(seed: u64, q: f64, s: f64) -> Engine {
        let params = crate::params::ModelParams {
            cycle_nowcast_accuracy: q,
            corporate_spread_cycle: s,
            ..crate::params::PT_V20
        };
        Engine::with_params(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            params,
        )
    }

    /// `cycle_nowcast_accuracy`: off, no belief is kept and the stream is
    /// never drawn; on, the belief opens one-hot on the opening phase, a
    /// true turn nobody announced moves neither the belief nor the
    /// anticipation, each close leaves five non-negative weights summing to
    /// one, a pin is public (one-hot, and held through that close), and the
    /// state hash covers the belief.
    #[test]
    fn the_cycle_nowcast_prices_a_belief_not_the_true_phase_only_under_the_dial() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let phases = crate::economy::cycle::phase_cycle();
        let off = engine_v20(7);
        assert_eq!(off.cycle_nowcast(), [0.0; 5]);
        assert_eq!(off.cycle_nowcast_terms, ([0.0; 5], 0.0));
        assert!(off.clone().set_cycle_nowcast([1.0, 0.0, 0.0, 0.0, 0.0], None).is_err());

        let mut e = engine_nowcast(7, 0.4, 0.75);
        let k0 = phases.iter().position(|p| *p == e.economy().cycle_phase).unwrap();
        let mut onehot = [0.0; 5];
        onehot[k0] = 1.0;
        assert_eq!(e.cycle_nowcast(), onehot);
        // The blend's occupancy mean of the US table, about 1.22.
        assert!((e.cycle_nowcast_terms.1 - 1.22).abs() < 0.05, "{:?}", e.cycle_nowcast_terms);

        // A true turn nobody announced: what the market prices stays put.
        let a0 = e.economy().earnings_anticipation;
        e.economy_mut().cycle_phase = phases[(k0 + 1) % 5];
        e.refresh_earnings_anticipation();
        assert_eq!(e.economy().earnings_anticipation, a0);
        assert_eq!(e.cycle_nowcast(), onehot);

        let draws = e.cycle_nowcast_rng_state().uniforms;
        for day in 1..=40i64 {
            e.advance_macro_day(day);
            let pi = e.cycle_nowcast();
            assert!(pi.iter().all(|v| v.is_finite() && *v >= 0.0), "{pi:?}");
            assert!((pi.iter().sum::<f64>() - 1.0).abs() < 1e-12, "{pi:?}");
        }
        assert_eq!(e.cycle_nowcast_rng_state().uniforms, draws + 40);
        assert_ne!(e.cycle_nowcast(), onehot);

        // A pin is public: one-hot on the pinned phase, and the close that
        // follows takes no report, so the belief holds through it.
        let pinned = phases[(k0 + 2) % 5];
        e.economy_mut().cycle_phase = pinned;
        e.cycle_phase_pinned();
        let kp = phases.iter().position(|p| *p == pinned).unwrap();
        let mut on_pin = [0.0; 5];
        on_pin[kp] = 1.0;
        assert_eq!(e.cycle_nowcast(), on_pin);
        let draws = e.cycle_nowcast_rng_state().uniforms;
        e.advance_macro_day(41);
        assert_eq!(e.cycle_nowcast(), on_pin);
        assert_eq!(e.cycle_nowcast_rng_state().uniforms, draws);

        // Hashed while set.
        let before = e.state_hash(41, false);
        let mut other = e.clone();
        other.set_cycle_nowcast([0.2; 5], None).unwrap();
        assert_ne!(other.state_hash(41, false), before);
        assert!(other.set_cycle_nowcast([0.5, 0.5, 0.5, 0.0, 0.0], None).is_err());
        assert!(other.set_cycle_nowcast([f64::NAN, 1.0, 0.0, 0.0, 0.0], None).is_err());
    }

    /// Over a long macro path the belief's time-mean matches the true
    /// phase's occupancy, and neither dial adds a draw to the economy
    /// stream (the report is on its own stream).
    #[test]
    fn the_cycle_nowcast_tracks_the_occupancy_on_its_own_stream() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let phases = crate::economy::cycle::phase_cycle();
        let days = 5040i64;
        let mut e = engine_nowcast(11, 0.4, 0.75);
        let mut control = engine_v20(11);
        let mut mean = [0.0; 5];
        let mut occupancy = [0.0; 5];
        let mut turns = 0;
        let mut phase = e.economy().cycle_phase;
        for day in 1..=days {
            e.advance_macro_day(day);
            control.advance_macro_day(day);
            let pi = e.cycle_nowcast();
            for j in 0..5 {
                mean[j] += pi[j] / days as f64;
            }
            let now = e.economy().cycle_phase;
            if now != phase {
                turns += 1;
                phase = now;
            }
            occupancy[phases.iter().position(|p| *p == now).unwrap()] += 1.0 / days as f64;
        }
        assert!(turns >= 5, "only {turns} true turns in {days} sessions");
        for j in 0..5 {
            assert!((mean[j] - occupancy[j]).abs() < 0.05, "{mean:?} against {occupancy:?}");
        }
        assert_eq!(e.draws.economy, control.draws.economy);
    }

    /// `market_prehistory_sessions`: at 0.0 the engine opens as it stood; on,
    /// it opens with the run's own generators, prices and economy (the VIX
    /// apart) and a copy's volatility state, the same for the same seed;
    /// and a caller's named opening runs no prehistory.
    #[test]
    fn the_market_prehistory_hands_back_the_volatility_state_alone() {
        let off = engine_macro_clock(11, &[]);
        let zero = engine_macro_clock(11, &[("market_prehistory_sessions", 0.0)]);
        assert_eq!(off.state_hash(0, false), zero.state_hash(0, false));
        let on = engine_macro_clock(11, &[("market_prehistory_sessions", 21.0)]);
        let again = engine_macro_clock(11, &[("market_prehistory_sessions", 21.0)]);
        assert_eq!(on.state_hash(0, false), again.state_hash(0, false));
        assert_ne!(on.state_hash(0, false), off.state_hash(0, false));
        // The run's own generators and draw counts are untouched.
        assert_eq!(on.rng_state(), off.rng_state());
        assert_eq!(on.draws_consumed(), off.draws_consumed());
        // Prices and the economy but its VIX open where they would.
        for (a, b) in on.companies().iter().zip(off.companies().iter()) {
            assert_eq!(a.stock.price.to_bits(), b.stock.price.to_bits());
            assert_eq!(a.stock.market_cap.to_bits(), b.stock.market_cap.to_bits());
        }
        let mut econ = on.economy().clone();
        assert_ne!(econ.vix, off.economy().vix);
        econ.vix = off.economy().vix;
        assert_eq!(&econ, off.economy());
        // The factor variance is the copy's, not the constructor's baseline.
        assert_ne!(on.market_variance_state(), off.market_variance_state());
        let base = off.params().market_factor_sigma * off.params().market_factor_sigma;
        assert_eq!(off.market_variance_state().0, base);
        // A named opening is a statement: no prehistory runs.
        let named = |dials: &[(&str, f64)]| {
            let mut params = crate::params::PT_V20;
            for (name, value) in dials {
                params = params.with_override(name, *value).unwrap();
            }
            Engine::with_params_from_opening(
                11,
                vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
                false,
            )
        };
        let a = named(&[]);
        let b = named(&[("market_prehistory_sessions", 21.0)]);
        assert_eq!(a.market_variance_state(), b.market_variance_state());
        assert_eq!(a.economy(), b.economy());
    }

    /// `market_prehistory_valuation`: at 0.0 nothing but the volatility state
    /// is carried; on, the run opens on its own generators, draws and prices
    /// with the copy's valuation state, each name's carried mispricing waits
    /// for the opening, the economy moves only in the carried fields, no
    /// opening price moves at the first tick, the same seed opens the same,
    /// and the switch is refused without a prehistory or off {0, 1}.
    #[test]
    fn the_prehistory_valuation_carries_the_valuation_state_and_moves_no_price() {
        let dials: &[(&str, f64)] = &[
            ("market_prehistory_sessions", 63.0),
            ("fed_put_gain", 3.0),
            ("fed_put_half_life", 126.0),
            ("corporate_spread_equity_gain", 2.0),
            ("corporate_spread_equity_half_life", 126.0),
            ("earnings_anticipation_drift_share", 0.9),
            ("earnings_anticipation_drift_half_life", 252.0),
            ("fair_value_vix_release_half_life", 504.0),
            ("treasury_path_pricing", 1.0),
            ("treasury_path_half_life", 63.0),
            ("treasury_policy_damping", 0.5),
        ];
        let with = |on: f64| {
            let mut d = dials.to_vec();
            d.push(("market_prehistory_valuation", on));
            engine_macro_clock(11, &d)
        };
        let off = with(0.0);
        let on = with(1.0);
        let again = with(1.0);
        assert!(off.opening_carry().is_empty());
        assert_eq!(on.state_hash(0, false), again.state_hash(0, false));
        assert_ne!(on.state_hash(0, false), off.state_hash(0, false));
        assert_eq!(on.rng_state(), off.rng_state());
        assert_eq!(on.draws_consumed(), off.draws_consumed());
        assert_eq!(on.market_variance_state(), off.market_variance_state());
        for (a, b) in on.companies().iter().zip(off.companies().iter()) {
            assert_eq!(a.stock.price.to_bits(), b.stock.price.to_bits());
            assert!(a.stock.mispricing_s.is_none() && a.stock.fair_value_offset.is_none());
        }
        let carry = on.opening_carry().to_vec();
        assert_eq!(carry.len(), on.companies().len());
        assert!(carry.iter().all(|v| v.is_finite()));
        // The economy moves only in the carried fields.
        let mut econ = on.economy().clone();
        let o = off.economy();
        for (field, a, b) in [
            ("federal_funds_rate", econ.federal_funds_rate, o.federal_funds_rate),
            ("corporate_bond_yield", econ.corporate_bond_yield, o.corporate_bond_yield),
        ] {
            assert!(a <= b + 1.0, "{field}");
        }
        econ.vix_feedback = o.vix_feedback;
        econ.earnings_cycle = o.earnings_cycle;
        econ.earnings_anticipation = o.earnings_anticipation;
        econ.spread_equity_gap = o.spread_equity_gap;
        econ.fed_put = o.fed_put;
        econ.fed_put_owed = o.fed_put_owed;
        econ.federal_funds_rate = o.federal_funds_rate;
        econ.prime_rate = o.prime_rate;
        econ.treasury_yield_10y = o.treasury_yield_10y;
        econ.treasury_yield_2y = o.treasury_yield_2y;
        econ.corporate_bond_yield = o.corporate_bond_yield;
        econ.mortgage_rate_30y = o.mortgage_rate_30y;
        assert_eq!(&econ, o);
        let moved = on.economy().federal_funds_rate - o.federal_funds_rate;
        assert!((on.economy().fed_put_owed + moved).abs() < 1e-12);
        let path = on.policy_path().unwrap() - off.policy_path().unwrap();
        assert!(path != 0.0);
        assert!(((on.economy().treasury_yield_10y - o.treasury_yield_10y) - (moved + 0.5 * path)).abs() < 1e-12);
        assert!(((on.economy().mortgage_rate_30y - o.mortgage_rate_30y) - (moved + 0.5 * path)).abs() < 1e-12);
        // The first tick opens every name where it stood: the opening's split
        // booked the carried state into the fair-value level.
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut first = [off.clone(), on.clone()];
        for e in first.iter_mut() {
            let mut buf = SessionBuffer::new();
            e.run_session(&session(1, &innovations, &variances), &mut buf);
        }
        for (i, &ci) in carry.iter().enumerate().take(3) {
            let a = first[0].companies()[i].stock.price;
            let b = first[1].companies()[i].stock.price;
            assert!((a / b).ln().abs() < 1e-3, "name {i}: {a} against {b}");
            // One tick from the copy's mispricing, not from a draw.
            let s = first[1].companies()[i].stock.mispricing_s.unwrap();
            assert!((s - ci).abs() < 0.01, "name {i}: {s} against {ci}");
        }
        assert!(first[1].opening_carry().is_empty());
        // Refused without a prehistory, and off the switch.
        let base = crate::params::PT_V20;
        let refused = |p: Result<ModelParams, String>| p.and_then(|p| p.invariants()).is_err();
        assert!(refused(base.with_override("market_prehistory_valuation", 1.0)));
        let pre = base.with_override("market_prehistory_sessions", 21.0).unwrap();
        assert!(refused(pre.with_override("market_prehistory_valuation", 0.5)));
        assert!(!refused(pre.with_override("market_prehistory_valuation", 1.0)));
    }

    /// The engine the macro-clock tests read: pt-v20 by name (the default
    /// until 0.10.0), with the
    /// dials given.
    fn engine_macro_clock(seed: u64, dials: &[(&str, f64)]) -> Engine {
        let mut params = crate::params::PT_V20;
        for (name, value) in dials {
            params = params.with_override(name, *value).unwrap();
        }
        Engine::with_params(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            params,
        )
    }

    /// `earnings_anticipation_drift_share`: off, no `D` is kept or hashed and
    /// the valuation reads `A - e`; on, `D` opens at zero after the burn-in,
    /// each close takes `D <- D 2^(-1/h_D) + share rho (A - e)` with `A - e`
    /// as the last refresh left it, the valuation reads `(A - e) - D`, the
    /// half-life 0.0 reads 1260, and the state hash covers `D`.
    #[test]
    fn the_anticipation_drift_is_left_out_only_under_the_share() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let off = engine_v20(7);
        assert!(!off.carries_anticipation_drift());
        assert_eq!(off.anticipation_drift(), (0.0, 0.0));
        assert!(off.clone().set_anticipation_drift(0.1, 0.0).is_err());

        for (share, hd, h_read) in [(1.0, 252.0, 252.0), (0.5, 0.0, 1260.0)] {
            let mut e = engine_macro_clock(
                7,
                &[("earnings_anticipation_drift_share", share),
                  ("earnings_anticipation_drift_half_life", hd)],
            );
            assert!(e.params().macro_burn_in_days > 0.0);
            let rho = std::f64::consts::LN_2 / e.params().earnings_anticipation_half_life;
            let decay = crate::mathx::exp(-std::f64::consts::LN_2 / h_read);
            // Zeroed after the burn-in, so the opening prices `A - e`.
            let (d0, raw0) = e.anticipation_drift();
            assert_eq!(d0, 0.0);
            assert_ne!(raw0, 0.0);
            assert_eq!(e.economy().earnings_anticipation, raw0);
            let (mut d, mut raw) = (d0, raw0);
            for day in 1..=60i64 {
                e.advance_macro_day(day);
                let (d1, raw1) = e.anticipation_drift();
                assert_eq!(d1, d * decay + share * rho * raw, "day {day}");
                assert_eq!(e.economy().earnings_anticipation, raw1 - d1, "day {day}");
                d = d1;
                raw = raw1;
            }
            assert!(d.abs() > 1e-4, "{d}");
            // Hashed while set; a restore puts back both and the anticipation.
            let before = e.state_hash(60, false);
            let mut other = e.clone();
            other.set_anticipation_drift(d + 0.01, raw).unwrap();
            assert_ne!(other.state_hash(60, false), before);
            assert_eq!(other.economy().earnings_anticipation, raw - (d + 0.01));
            other.set_anticipation_drift(d, raw).unwrap();
            assert_eq!(other.state_hash(60, false), before);
            assert!(other.set_anticipation_drift(f64::NAN, raw).is_err());
            // The seeding zeroes D and prices A - e again.
            other.seed_anticipation_drift();
            assert_eq!(other.anticipation_drift().0, 0.0);
            assert_eq!(other.economy().earnings_anticipation, other.anticipation_drift().1);
        }
    }

    /// `corporate_spread_cycle` at 1.0 without the nowcast: the multiplier
    /// the meeting and the daily path price is the occupancy mean in every
    /// phase, and at 0.0 it is the phase's own.
    #[test]
    fn the_full_spread_blend_prices_the_occupancy_mean_in_every_phase() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let e = engine_macro_clock(7, &[("corporate_spread_cycle", 1.0)]);
        let off = engine_v20(7);
        let mbar = e.cycle_nowcast_terms.1;
        let (mean, cycle) = crate::economy::cycle::stationary_phase_shares_for(&e.cycle_spec());
        let phases = crate::economy::cycle::phase_cycle();
        let mut direct = 0.0;
        for k in 0..5 {
            direct += mean[k] / cycle * crate::economy::central_bank::spread_multiplier_of(phases[k]);
        }
        assert!((mbar - direct).abs() < 1e-12);
        assert!(mbar > 1.0 && mbar < 3.5, "{mbar}");
        for phase in phases {
            assert!((e.priced_spread_multiplier(phase) - mbar).abs() < 1e-12);
            assert_eq!(off.priced_spread_multiplier(phase),
                       crate::economy::central_bank::spread_multiplier_of(phase));
        }
    }

    /// The lag of one turn: whole sessions, the whole range reachable, the
    /// peak and contraction range for those two phases and the trough range
    /// for the other three.
    #[test]
    fn a_publication_lag_spans_its_range() {
        use crate::economy::CyclePhase;
        let top = 1.0 - f64::EPSILON / 2.0;
        for (phase, lo, hi) in [(CyclePhase::Peak, 84, 252), (CyclePhase::Contraction, 84, 252),
                                (CyclePhase::Trough, 168, 441), (CyclePhase::Recovery, 168, 441),
                                (CyclePhase::Expansion, 168, 441)] {
            assert_eq!(cycle_publication_lag_of(phase, 0.0), lo);
            assert_eq!(cycle_publication_lag_of(phase, top), hi);
            let mut seen = std::collections::BTreeSet::new();
            for k in 0..20_000u64 {
                let lag = cycle_publication_lag_of(phase, crate::rng::publication_uniform(99, k));
                assert!((lo..=hi).contains(&lag));
                seen.insert(lag);
            }
            assert_eq!(seen.len() as i64, hi - lo + 1);
        }
    }

    /// `cycle_publication_lag_draw`: off, no schedule is kept or hashed; on,
    /// the fixed history is not kept, each true turn is published at
    /// `max(pi_prev, tau + L)` with `L` in [84, 252] into a peak or a
    /// contraction and [168, 441] otherwise, announcements come in the
    /// true turns' order, the schedule is a function of the seed, and it
    /// takes no draw from any stream.
    #[test]
    fn the_drawn_publication_schedule_publishes_each_turn_in_order() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        use crate::economy::CyclePhase;
        let off = engine_v20(7);
        assert!(off.carries_cycle_history());
        assert!(off.clone().set_cycle_publication(off.cycle_publication().clone()).is_err());

        let days = 5040i64;
        let run = |seed: u64| {
            let mut e = engine_macro_clock(seed, &[("cycle_publication_lag_draw", 1.0)]);
            let mut control = engine_v20(seed);
            assert!(!e.carries_cycle_history());
            assert!(e.cycle_history().is_empty());
            assert!(e.clone().set_cycle_history(vec![e.economy().cycle_phase; 253]).is_err());
            let opening = e.economy().cycle_phase;
            assert_eq!(e.published_cycle_phase(), opening);
            let mut truth = vec![opening];
            let mut published = vec![opening];
            for day in 1..=days {
                e.advance_macro_day(day);
                control.advance_macro_day(day);
                truth.push(e.economy().cycle_phase);
                published.push(e.published_cycle_phase());
                assert_eq!(e.economy().cycle_phase, control.economy().cycle_phase, "day {day}");
            }
            // No stream moved: the schedule's draws are stateless.
            assert_eq!(e.draws.economy, control.draws.economy);
            (e, truth, published)
        };
        let (e, truth, published) = run(7);

        // The true turns, and each one's publication close from the formula.
        let mut turns: Vec<(i64, CyclePhase)> = Vec::new();
        for c in 1..truth.len() {
            if truth[c] != truth[c - 1] {
                turns.push((c as i64, truth[c]));
            }
        }
        assert!(turns.len() >= 10, "only {} turns", turns.len());
        let key = e.cycle_publication().key;
        assert_eq!(key, crate::rng::publication_key(7));
        let mut last = i64::MIN;
        let mut expected = vec![truth[0]; truth.len()];
        let mut schedule = Vec::new();
        for (k, &(tau, phase)) in turns.iter().enumerate() {
            let lag = cycle_publication_lag_of(phase, crate::rng::publication_uniform(key, k as u64));
            match phase {
                CyclePhase::Peak | CyclePhase::Contraction => assert!((84..=252).contains(&lag)),
                _ => assert!((168..=441).contains(&lag)),
            }
            let pi = if tau + lag > last { tau + lag } else { last };
            assert!(pi >= last);
            last = pi;
            schedule.push((pi, phase));
        }
        for (c, slot) in expected.iter_mut().enumerate().take(truth.len()) {
            if let Some(&(_, phase)) = schedule.iter().rev().find(|&&(pi, _)| pi <= c as i64) {
                *slot = phase;
            }
        }
        assert_eq!(published, expected);
        // Announcements follow the true turns' order: the published path's
        // changes are a subsequence of the true turns' phases.
        let announced: Vec<CyclePhase> = published.windows(2)
            .filter(|w| w[0] != w[1]).map(|w| w[1]).collect();
        let mut it = turns.iter().map(|&(_, p)| p);
        for phase in &announced {
            assert!(it.any(|p| p == *phase), "{phase:?} out of order");
        }
        assert!(announced.len() >= 8);
        // The state carried agrees with the formula.
        let state = e.cycle_publication();
        assert_eq!(state.closes, days);
        assert_eq!(state.turns, turns.len() as u64);
        assert_eq!(state.last_true, *truth.last().unwrap());
        let pending: Vec<(i64, CyclePhase)> = schedule.iter().copied()
            .filter(|&(pi, _)| pi > days).collect();
        assert_eq!(state.pending.iter().copied().collect::<Vec<_>>(), pending);

        // Reproducible from the seed, and another seed draws other lags.
        let (_, _, again) = run(7);
        assert_eq!(again, published);
        let (other, _, _) = run(8);
        assert_ne!(other.cycle_publication().key, key);

        // Hashed while set, and a restore refuses a schedule out of order.
        let before = e.state_hash(days as u32, false);
        let mut changed = e.clone();
        let mut state = e.cycle_publication().clone();
        state.turns += 1;
        changed.set_cycle_publication(state).unwrap();
        assert_ne!(changed.state_hash(days as u32, false), before);
        let mut bad = e.cycle_publication().clone();
        bad.pending = vec![(10, CyclePhase::Peak), (5, CyclePhase::Contraction)].into();
        assert!(changed.set_cycle_publication(bad).is_err());
    }

    /// `gdp_publication_lag`: off, the published growth is the true one and
    /// no state is kept or hashed; on, it is the mean of each macro-calendar
    /// quarter's true growth (day 0, the opening, included), released `lag`
    /// closes after the quarter's last day, and the opening growth until then.
    #[test]
    fn the_published_growth_is_the_quarter_mean_released_late_only_under_the_dial() {
        // "Off" is pt-v19: the default, pt-v20, sets the lag since its
        // graded arm (2026-09-26). Was `engine(7)`, the default.
        let off = engine_v19(7);
        assert_eq!(off.gdp_publication(), &GdpPublication::default());
        assert_eq!(off.published_gdp_growth(), off.economy().gdp_growth);

        let lag = 5i64;
        let params = crate::params::ModelParams {
            gdp_publication_lag: lag as f64,
            ..crate::params::PT_V20
        };
        let mut e = Engine::with_params(
            7,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            params,
        );
        let q = e.macro_calendar().days_per_quarter();
        let opening = e.economy().gdp_growth;
        assert_eq!(e.published_gdp_growth(), opening);
        let mut truth = vec![opening];
        let mut published = vec![opening];
        for day in 1..=(2 * q + lag) {
            e.advance_macro_day(day);
            truth.push(e.economy().gdp_growth);
            published.push(e.published_gdp_growth());
        }
        let mean = |k: i64| -> f64 {
            let mut sum = 0.0;
            for d in (k * q)..((k + 1) * q) {
                sum += truth[d as usize];
            }
            sum / q as f64
        };
        for d in 0..published.len() as i64 {
            let want = if d >= 2 * q - 1 + lag {
                mean(1)
            } else if d >= q - 1 + lag {
                mean(0)
            } else {
                opening
            };
            assert_eq!(published[d as usize], want, "day {d}");
        }
        // Hashed while set: the same engine with one pending figure
        // changed hashes apart.
        let before = e.state_hash(12, false);
        let mut other = e.clone();
        let mut state = other.gdp_publication().clone();
        state.sum += 1.0;
        other.set_gdp_publication(state).unwrap();
        assert_ne!(other.state_hash(12, false), before);
        let mut empty = e.gdp_publication().clone();
        empty.count = 0;
        assert!(e.clone().set_gdp_publication(empty).is_err());
        assert!(engine_v19(7).clone().set_gdp_publication(GdpPublication {
            count: 1,
            ..GdpPublication::default()
        }).is_err());
    }

    fn request(hour: i64, minute: i64) -> TickRequest<'static> {
        TickRequest {
            time: GameTime {
                hour,
                minute,
                day_of_week: 3,
            },
            volatility_multiplier: 0.7,
            news: &[],
            news_impact_queue: &[],
            order_volumes: &[],
        }
    }

    #[test]
    fn the_same_seed_produces_the_same_market() {
        // The property the whole port exists to preserve.
        let run = || {
            let mut e = engine(4242);
            e.open_market();
            for m in 0..60 {
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
            }
            (e.prices(), e.draws_consumed())
        };
        let (a, da) = run();
        let (b, db) = run();
        assert_eq!(da, db, "draw counts must match");
        for (i, (x, y)) in a.iter().zip(&b).enumerate() {
            assert_eq!(x.to_bits(), y.to_bits(), "company {i}");
        }
    }

    #[test]
    fn different_seeds_produce_different_markets() {
        // The companion assertion: reproducible-because-constant would pass
        // the test above and be worthless.
        //
        // Compares the WHOLE price vector over four seeds, not company A over
        // two. Prices settle to the cent, so a single name colliding across a
        // single pair of seeds is a coincidence rather than a collapse -- and
        // one duly happened at the pt-v12 boundary, where company A landed on
        // 101.54 for both seed 1 and seed 2 after sixty minutes while every
        // other name differed. A test that reads one number cannot tell the
        // two apart, which is the whole point of the property it is named
        // for.
        let run = |seed| {
            let mut e = engine(seed);
            e.open_market();
            for m in 0..60 {
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
            }
            e.prices()
        };
        let seeds: Vec<Vec<f64>> = (1u64..=4).map(run).collect();
        for (i, a) in seeds.iter().enumerate() {
            for b in seeds.iter().skip(i + 1) {
                assert!(
                    a.iter().zip(b).any(|(x, y)| x.to_bits() != y.to_bits()),
                    "two seeds produced an identical market: {a:?} vs {b:?}"
                );
            }
        }
    }

    #[test]
    fn a_closed_market_costs_nothing() {
        let mut e = engine(7);
        let before_prices = e.prices();
        // The DELTA, not the lifetime total. A default preset with a macro
        // burn-in draws during construction -- pt-v18 runs 755 days of it --
        // and this test is about what the operation costs, not about what
        // building an engine costs. Asserting the total made the claim
        // depend on a dial in a different subsystem.
        let before_draws = e.draws_consumed();
        let out = e.tick(&TickRequest {
            time: GameTime {
                hour: 11,
                minute: 0,
                day_of_week: 6,
            },
            ..request(11, 0)
        });
        assert_eq!(out.market_status, MarketStatus::Closed);
        assert_eq!(
            out.draws_consumed, 0,
            "a closed market must not advance the stream"
        );
        assert_eq!(e.draws_consumed() - before_draws, 0);
        assert_eq!(e.prices(), before_prices);
    }

    #[test]
    fn the_draw_schedule_is_what_the_documentation_claims() {
        // 1 market normal + one per sector + 2 per active company, plus 4 more
        // each at settlement when the book runs.
        let mut e = engine(11);
        e.open_market();

        let open = e.tick(&request(10, 0));
        assert_eq!(open.market_status, MarketStatus::Open);
        assert_eq!(open.draws_consumed, 1 + 3 + 2 * 3 + 4 * 3);

        let extended = e.tick(&request(8, 0));
        assert_eq!(extended.market_status, MarketStatus::PreMarket);
        assert_eq!(
            extended.draws_consumed,
            1 + 3 + 2 * 3,
            "extended hours must not settle through the book"
        );
    }

    #[test]
    fn an_inactive_company_costs_no_draws() {
        let mut e = engine(13);
        e.companies_mut()[1].is_bankrupt = true;
        e.open_market();
        let out = e.tick(&request(10, 0));
        assert_eq!(out.active_indices, vec![0, 2]);
        assert_eq!(out.draws_consumed, 1 + 3 + 2 * 2 + 4 * 2);
    }

    #[test]
    fn embedder_draws_leave_the_market_bit_identical() {
        // The CUTOVER half of the stream split. Under the shared stream this
        // test's inverse held — one embedder draw shifted every subsequent
        // market draw, and the old assertion here demanded exactly that. Now
        // the embedder's consumption lives on its own substream, so a game
        // that adds an event roll no longer invalidates every seeded market.
        let run = |extra_draws: usize| {
            let mut e = engine(99);
            e.open_market();
            for _ in 0..extra_draws {
                e.draw_uniform();
                e.draw_normal();
            }
            for m in 0..5i64 {
                e.tick(&request(10, m));
            }
            (e.prices(), e.rng_state())
        };
        let (without, state_without) = run(0);
        let (with_draws, state_with) = run(1000);
        for (i, (a, b)) in without.iter().zip(&with_draws).enumerate() {
            assert_eq!(
                a.to_bits(),
                b.to_bits(),
                "company {i} moved because the embedder drew"
            );
        }
        // Bit-identical POSITION, not merely price: the market stream never
        // saw the embedder's draws at all.
        assert_eq!(state_without.market, state_with.market);
        assert_ne!(
            state_without.external, state_with.external,
            "the embedder's draws were not taken from the external stream"
        );
    }

    #[test]
    fn embedder_draws_are_seed_determined_and_reproducible() {
        // Isolation must not cost reproducibility: the external stream is
        // derived from the same root seed, so an embedder replaying a run
        // gets its own draws back too.
        let a: Vec<f64> = {
            let mut e = engine(7);
            (0..16).map(|_| e.draw_normal()).collect()
        };
        let b: Vec<f64> = {
            let mut e = engine(7);
            (0..16).map(|_| e.draw_normal()).collect()
        };
        assert_eq!(a, b);
    }

    #[test]
    fn macro_branch_differences_leave_the_market_stream_untouched() {
        // The PINNED-VERSUS-BASELINE half of the stream split. The macro
        // chain's draw count depends on macro state — a cycle sitting in
        // contraction rolls a shock the expansion never draws — so two runs
        // whose macro paths branch differently consume different economy
        // draws. Under the shared stream that shifted every market draw
        // after the day boundary; a pinned run and its baseline never saw
        // the same noise again. Now the market stream's position is
        // identical whatever the macro chain consumed.
        // On pt-v19. On the default since pt-v20 took its graded arm
        // (2026-09-26) the two phase ages happen to draw the same number of
        // economy normals at this seed, so the precondition below found
        // nothing to test; the stream split it tests is the same on every
        // preset. Was `engine(4242)`, the default.
        let run = |fresh_phase: bool| {
            let mut e = engine_v19(4242);
            // A phase that changed TODAY draws the phase-change shock
            // uniform; one 0.9 months in draws neither that (window passed)
            // nor the transition roll (min_months not reached). The counts
            // differ by construction, which is the shape of the hazard: a
            // pinned macro path and its baseline sit in different phases and
            // stop consuming in step.
            e.economy_mut().months_in_current_phase = if fresh_phase { 0.0 } else { 0.9 };
            e.advance_day(&DayAdvanceRequest {
                volatility: 1.0,
                active_shocks: &[],
                market_return_pct: 0.0,
                game_day: 1,
                timestamp: 24 * 60,
            });
            // The market's draws, taken AFTER the diverging macro step.
            e.open_market();
            for m in 0..10i64 {
                e.tick(&request(10, m));
            }
            e
        };
        let flat = run(false);
        let shocked = run(true);
        // The precondition that makes the assertion mean something: the two
        // macro chains really did consume different numbers of draws.
        assert_ne!(
            flat.draws_by_stream().economy,
            shocked.draws_by_stream().economy,
            "the two macro paths drew in step; the test constructed nothing"
        );
        assert_eq!(
            flat.rng_state().market,
            shocked.rng_state().market,
            "the macro chain moved the market stream"
        );
        assert_eq!(flat.draws_by_stream().market, shocked.draws_by_stream().market);
    }

    #[test]
    fn a_pinned_macro_run_and_its_baseline_see_identical_market_noise() {
        // The consumer's actual workflow: world A advances the macro chain
        // endogenously; world B replays a PINNED macro path — it never runs
        // the chain at all, it writes the day's values directly. Before the
        // split, world B's skipped macro draws shifted the market stream and
        // the two worlds' intraday noise had nothing to do with each other.
        // Now: pin the same values the endogenous chain produced, and the
        // sessions are bit-identical — which is what makes "the difference
        // is the macro path and nothing else" a guarantee rather than an
        // approximation when the pinned values DO differ.
        let day = |e: &mut Engine| {
            e.open_market();
            for m in 0..30i64 {
                e.tick(&request(10, m));
            }
            e.close_market(&DayCloseRequest {
                daily_innovations: &[None, None, None],
                sector_base_variances: &[0.000225; 3],
                avg_volume: AvgVolumePolicy::default(),
            });
        };

        // World A: the chain runs.
        let mut endogenous = engine(2026);
        endogenous.advance_day(&DayAdvanceRequest {
            volatility: 1.0,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day: 1,
            timestamp: 24 * 60,
        });
        let evolved = endogenous.economy().clone();
        day(&mut endogenous);

        // World B: no chain — the evolved values are pinned directly, as a
        // replay of a recorded macro series would.
        let mut pinned = engine(2026);
        // The DELTA, not the lifetime total. A default preset with a macro
        // burn-in draws during construction -- pt-v18 runs 755 days of it --
        // and this test is about what the operation costs, not about what
        // building an engine costs. Asserting the total made the claim
        // depend on a dial in a different subsystem.
        let before_economy = pinned.draws_by_stream().economy;
        *pinned.economy_mut() = evolved;
        day(&mut pinned);

        assert_eq!(
            pinned.draws_by_stream().economy - before_economy,
            0,
            "the pinned world must not run the macro chain"
        );
        for (i, (a, b)) in endogenous
            .prices()
            .iter()
            .zip(&pinned.prices())
            .enumerate()
        {
            assert_eq!(
                a.to_bits(),
                b.to_bits(),
                "company {i}: pinning the macro path perturbed the market noise"
            );
        }
        assert_eq!(endogenous.rng_state().market, pinned.rng_state().market);
    }

    #[test]
    fn the_cumulative_draw_count_includes_embedder_draws() {
        let mut e = engine(5);
        // The DELTA, not the lifetime total. A default preset with a macro
        // burn-in draws during construction -- pt-v18 runs 755 days of it --
        // and this test is about what the operation costs, not about what
        // building an engine costs. Asserting the total made the claim
        // depend on a dial in a different subsystem.
        let before = e.draws_consumed();
        e.draw_uniform();
        e.draw_normal();
        assert_eq!(e.draws_consumed() - before, 2);
        e.open_market();
        let out = e.tick(&request(10, 0));
        assert_eq!(e.draws_consumed() - before, 2 + out.draws_consumed);
    }

    /// A draw source that records the call sites it is told about, in
    /// order, and counts the draws taken at each. `Site` is a no-op on
    /// every generator that is not the logged one, so the ORDER of the
    /// three macro stages is otherwise unobservable from outside
    /// `advance_day_with`.
    struct SiteTrace {
        inner: GameRng,
        seen: Vec<(Site, usize)>,
    }

    impl Rng for SiteTrace {
        fn next_f64(&mut self) -> f64 {
            if let Some(last) = self.seen.last_mut() {
                last.1 += 1;
            }
            self.inner.next_f64()
        }
        fn next_normal(&mut self) -> f64 {
            if let Some(last) = self.seen.last_mut() {
                last.1 += 1;
            }
            self.inner.next_normal()
        }
        fn site(&mut self, site: Site, tag: u32) {
            self.seen.push((site, 0));
            self.inner.site(site, tag);
        }
    }

    /// The daily step visits the macro chain, then the cycle roll, then the
    /// central bank, in that order, and the first of the three moves state.
    ///
    /// # What this test used to assert, and why that assertion is gone
    ///
    /// It read the VIX before and after and required it to have changed.
    /// Under `vix_level_identity`, which pt-v19 turns on, that is a
    /// property the model deliberately gave up. The VIX's target is the
    /// index's own conditional variance read back in points, less the
    /// closed form of the fear excursion's mean, and neither depends on the
    /// calendar. Its innovation is `vix * sqrt(s0^2 + (s_r * r)^2)` with
    /// `vix_innovation_sigma` derived to 0.0, so on a session whose return
    /// is zero the noise term is exactly zero too. pt-v19 also runs a
    /// 755-day macro burn-in, which drives the VIX onto the floating-point
    /// fixed point of that map before the test's first call. The engine
    /// this module builds never opens a session, so its day return is 0.0
    /// and the VIX reproduces to the bit, day after day. Measured here: it
    /// holds at 23.768839023837387 across eleven consecutive calls.
    ///
    /// The economy still steps, which was the thing being probed. Gold, the
    /// dollar, oil, the ten-year and the greed index all move on the same
    /// call. So the probe is `gold_price`, which carries an unconditional
    /// `random_normal` term and cannot sit still while the chain runs.
    ///
    /// The VIX law that replaced the old one is asserted at the end: the
    /// VIX is a function of the session, so a flat session leaves it
    /// bit-identical and a down session raises it.
    #[test]
    fn the_daily_step_runs_economy_then_cycle_then_the_bank() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let day = DayAdvanceRequest {
            volatility: 0.7,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day: 1,
            timestamp: 24 * 60,
        };

        let mut e = engine_v20(21);
        let before = e.economy().clone();
        let mut trace = SiteTrace {
            inner: GameRng::new(21, stream::ECONOMY),
            seen: Vec::new(),
        };
        e.advance_day_with(&day, &mut trace);

        let sites: Vec<Site> = trace.seen.iter().map(|(s, _)| *s).collect();
        assert_eq!(
            sites,
            vec![Site::EconomyDaily, Site::EconomyCycle, Site::CentralBank],
            "the daily step must visit the three stages once each, in order"
        );
        assert!(
            trace.seen[0].1 > 0,
            "the macro chain must draw; it took {} draws",
            trace.seen[0].1
        );
        assert_ne!(
            e.economy().gold_price,
            before.gold_price,
            "the macro chain ran without moving the state it draws for"
        );

        // The draw count the counting entry point reports is the same work.
        let mut counted = engine_v20(21);
        let out = counted.advance_day(&day);
        assert!(out.draws_consumed > 0, "the daily macro step must draw");

        // THE VIX LAW THIS TEST NOW CARRIES. The flat engine above took the
        // same call and its VIX did not move by one bit, because under the
        // identity a VIX at rest with a zero session return has nothing to
        // move it. Open a session and take the index down five per cent and
        // the same call moves it, which is the direction the fear channel
        // is wired in.
        assert_eq!(
            e.economy().vix.to_bits(),
            before.vix.to_bits(),
            "a flat session moved the VIX, which the identity has no term for"
        );
        let mut down = engine_v20(21);
        down.open_market();
        for c in down.companies_mut().iter_mut() {
            c.stock.price *= 0.95;
        }
        down.advance_day(&day);
        assert!(
            down.economy().vix > before.vix + 1.0,
            "a five per cent down session must raise the VIX; it read {}",
            down.economy().vix
        );
    }

    #[test]
    fn the_open_reset_anchors_the_breaker_to_todays_open() {
        let mut e = engine(3);
        e.companies_mut()[0].stock.price = 137.0;
        e.open_market();
        assert_eq!(e.column(PriceField::PreviousClose)[0], 137.0);
        assert_eq!(e.column(PriceField::Volume)[0], 0.0);
    }

    #[test]
    fn the_close_rolls_momentum_and_takes_no_draws() {
        let mut e = engine(17);
        e.open_market();
        for m in 0..10 {
            e.tick(&request(10, m));
        }
        let before = e.draws_consumed();
        e.close_market(&DayCloseRequest {
            daily_innovations: &[None, None, None],
            sector_base_variances: &[0.000225; 3],
            avg_volume: AvgVolumePolicy::default(),
        });
        assert_eq!(e.draws_consumed(), before, "the close must not draw");
        // `s` has moved during the session, so the roll must record it.
        assert!(e.companies()[0].stock.mispricing_momentum.is_some());
    }

    // ── Day-chunked stepping ──────────────────────────────────────────────

    fn session<'a>(
        ticks: usize,
        innovations: &'a [Option<f64>],
        variances: &'a [f64],
    ) -> SessionRequest<'a> {
        SessionRequest {
            start: GameTime {
                hour: 9,
                minute: 30,
                day_of_week: 3,
            },
            ticks,
            volatility_multiplier: 0.7,
            news: &[],
            news_impact_queue: &[],
            order_volumes: &[],
            fills: &[],
            close_at_end: true,
            // These tests run one session per day, where opening inside the
            // session and opening the day are the same act. True preserves
            // exactly what they measured before `reopen` existed.
            reopen: true,
            daily_innovations: innovations,
            sector_base_variances: variances,
            stop: None,
        }
    }

    // ── Fills: an agent's trades reach the market once ────────────────────

    /// A session of `ticks` from `start_minute` past 09:30, inside a day
    /// already opened, carrying `standing` on every tick and `fills` once.
    fn stepped<'a>(
        ticks: usize,
        start_minute: i64,
        standing: &'a [(String, OrderVolume)],
        fills: &'a [(String, OrderVolume)],
        innovations: &'a [Option<f64>],
        variances: &'a [f64],
    ) -> SessionRequest<'a> {
        let at = 30 + start_minute;
        SessionRequest {
            start: GameTime {
                hour: 9 + at / 60,
                minute: at % 60,
                day_of_week: 3,
            },
            order_volumes: standing,
            fills,
            close_at_end: false,
            reopen: false,
            ..session(ticks, innovations, variances)
        }
    }

    /// `fills` IS one tick of `order_volumes` followed by the rest of the
    /// session without it: the same prices to the bit and the same draws.
    /// That is the whole contract, stated as the market the tick loop
    /// already defines, so it cannot drift into a second meaning.
    #[test]
    fn fills_are_one_tick_of_flow_at_the_start_of_the_session() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let fills = vec![("A".to_string(), OrderVolume { buy: 40_000.0, sell: 0.0 })];

        let mut once = engine_v19(11);
        once.open_market();
        let mut buf = SessionBuffer::new();
        once.run_session(&stepped(65, 0, &[], &fills, &innovations, &variances), &mut buf);

        let mut spelled = engine_v19(11);
        spelled.open_market();
        spelled.tick(&TickRequest {
            order_volumes: &fills,
            ..request(9, 30)
        });
        let mut buf2 = SessionBuffer::new();
        spelled.run_session(&stepped(64, 1, &[], &[], &innovations, &variances), &mut buf2);

        assert_eq!(once.draws_consumed(), spelled.draws_consumed());
        for (i, (x, y)) in once.prices().iter().zip(spelled.prices()).enumerate() {
            assert_eq!(x.to_bits(), y.to_bits(), "company {i}");
        }
        let flow = crate::market::factors::S_COMPONENT_KEYS
            .iter()
            .position(|f| *f == "order_flow_impact")
            .unwrap();
        let a = once.attribution_column(flow);
        assert!(a[0] > 0.0, "the buy reached A: {a:?}");
        assert_eq!(a[1], 0.0);
        assert_eq!(a[2], 0.0);
    }

    /// The same flow held on every tick is counted on every tick. This is
    /// what every harness did with an agent's fills until 0.8.5, and the
    /// ratio is the 65 the investigation measured, not an approximation of
    /// it: a name's flow impact depends on its average volume, which does
    /// not move inside a day.
    #[test]
    fn a_standing_flow_is_counted_on_every_tick_and_fills_once() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let flow = vec![("A".to_string(), OrderVolume { buy: 40_000.0, sell: 0.0 })];
        let column = crate::market::factors::S_COMPONENT_KEYS
            .iter()
            .position(|f| *f == "order_flow_impact")
            .unwrap();

        let run = |standing: &[(String, OrderVolume)], fills: &[(String, OrderVolume)]| {
            let mut e = engine_v19(11);
            e.open_market();
            let mut buf = SessionBuffer::new();
            e.run_session(&stepped(65, 0, standing, fills, &innovations, &variances), &mut buf);
            e.attribution_column(column)[0]
        };
        let once = run(&[], &flow);
        let held = run(&flow, &[]);
        assert!(once > 0.0);
        let ratio = held / once;
        assert!((ratio - 65.0).abs() < 1e-9, "held / once = {ratio}");
    }

    /// With no fills the session is the one that existed before the field:
    /// the first tick reads `order_volumes` untouched. Bit-identical rather
    /// than equal, since the untraded known-answer digest rests on it.
    #[test]
    fn empty_fills_change_nothing() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let standing = vec![("B".to_string(), OrderVolume { buy: 0.0, sell: 9_000.0 })];
        let run = |fills: &[(String, OrderVolume)]| {
            let mut e = engine(5);
            e.open_market();
            let mut buf = SessionBuffer::new();
            e.run_session(&stepped(90, 0, &standing, fills, &innovations, &variances), &mut buf);
            (e.prices(), e.draws_consumed())
        };
        let (a, da) = run(&[]);
        // A zero fill for a name is a no-op too: zero volume is the literal
        // 0.0 imbalance under either impact law.
        let (b, db) = run(&[("C".to_string(), OrderVolume { buy: 0.0, sell: 0.0 })]);
        assert_eq!(da, db);
        for (i, (x, y)) in a.iter().zip(&b).enumerate() {
            assert_eq!(x.to_bits(), y.to_bits(), "company {i}");
        }
    }

    /// One entry per ticker on the first tick, standing and fills summed.
    /// The tick reads the FIRST entry for a name, so a second entry would
    /// be dropped rather than added.
    #[test]
    fn standing_flow_and_fills_merge_per_ticker() {
        let standing = vec![
            ("A".to_string(), OrderVolume { buy: 1.0, sell: 2.0 }),
            ("C".to_string(), OrderVolume { buy: 7.0, sell: 0.0 }),
        ];
        let fills = vec![
            ("A".to_string(), OrderVolume { buy: 3.0, sell: 4.0 }),
            ("B".to_string(), OrderVolume { buy: 5.0, sell: 0.0 }),
        ];
        let merged = merge_order_volumes(&standing, &fills);
        assert_eq!(
            merged,
            vec![
                ("A".to_string(), OrderVolume { buy: 4.0, sell: 6.0 }),
                ("C".to_string(), OrderVolume { buy: 7.0, sell: 0.0 }),
                ("B".to_string(), OrderVolume { buy: 5.0, sell: 0.0 }),
            ]
        );
    }

    // ── The depth counterfactual ──────────────────────────────────────────

    /// The arm is inert to the market. Same seed, same session, one engine
    /// with the second settlement and one without, and every price agrees to
    /// the bit along with the draw count.
    ///
    /// This is the claim the known-answer digest checks at the package level;
    /// asserting it here as well means a build that broke it says so in
    /// seconds rather than at the end of a wheel build.
    #[test]
    fn the_depth_counterfactual_leaves_the_market_and_the_draws_alone() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let run = |on: bool| {
            let mut e = engine(4242);
            e.set_settle_depth_counterfactual(on);
            let mut buf = SessionBuffer::new();
            for _ in 0..3 {
                e.run_session(&session(60, &innovations, &variances), &mut buf);
            }
            (e.draws_by_stream(), buf.prices.clone(), buf.shock.clone())
        };
        let (draws_off, prices_off, shock_off) = run(false);
        let (draws_on, prices_on, shock_on) = run(true);
        assert_eq!(draws_off, draws_on, "the arm must take no draw");
        for (i, (a, b)) in prices_off.iter().zip(prices_on.iter()).enumerate() {
            assert_eq!(a.to_bits(), b.to_bits(), "price {i} moved");
        }
        for (i, (a, b)) in shock_off.iter().zip(shock_on.iter()).enumerate() {
            assert_eq!(a.to_bits(), b.to_bits(), "shock {i} moved");
        }
    }

    /// The columns arrive only when they are asked for, and they are the
    /// length of the table when they do.
    #[test]
    fn the_counterfactual_columns_are_present_only_when_the_arm_runs() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];

        let mut off = engine(7);
        let mut buf_off = SessionBuffer::new();
        off.run_session(&session(60, &innovations, &variances), &mut buf_off);
        assert!(buf_off.unbounded_print.is_empty());
        assert!(buf_off.liquidity_share.is_empty());
        assert_eq!(buf_off.shock.len(), buf_off.prices.len());

        let mut on = engine(7);
        on.set_settle_depth_counterfactual(true);
        let mut buf_on = SessionBuffer::new();
        on.run_session(&session(60, &innovations, &variances), &mut buf_on);
        assert_eq!(buf_on.unbounded_print.len(), buf_on.prices.len());
        assert_eq!(buf_on.liquidity_share.len(), buf_on.prices.len());
    }

    /// Every print splits into a shock and an absorption that add back up to
    /// the move, and liquidity's share is never an infinity.
    ///
    /// Seed 42 over a full session rather than a short one, because the two
    /// branches worth guarding are both rare. On this session 51 rows carry a
    /// counterfactual print that differs from the real one and 2 carry a
    /// share of NaN; a 60-tick session at another seed reached neither, so
    /// the assertions below ran over rows that could not fail them. Both
    /// counts are asserted, so a session that stops reaching a branch fails
    /// here rather than going quiet.
    ///
    /// The fixture NAMES pt-v18, because those counts are pt-v18's at seed
    /// 42 and the property is arithmetic on every print rather than a fact
    /// about the default: when the default moved to pt-v19 at 0.8.0 the same
    /// seed produced no unmoved print at all, and the NaN branch went
    /// unguarded -- which is exactly the failure the counts exist to raise,
    /// and the remedy is a fixture that reaches both branches, not a weaker
    /// assertion.
    #[test]
    fn every_print_decomposes_into_its_shock_and_its_absorption() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = Engine::with_params(
            42,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            crate::params::PT_V18,
        );
        e.set_settle_depth_counterfactual(true);
        let mut buf = SessionBuffer::new();
        e.run_session(&session(390, &innovations, &variances), &mut buf);

        let n = buf.companies;
        let mut checked = 0usize;
        let mut undefined = 0usize;
        let mut bound_moved = 0usize;
        // From tick one, so every row has a previous print on the same tape.
        for t in 1..buf.ticks_written {
            for i in 0..n {
                let row = t * n + i;
                let previous = buf.prices[(t - 1) * n + i];
                let moved = crate::mathx::log(buf.prices[row] / previous);
                let split = buf.shock[row] + buf.absorbed[row];
                assert!(
                    (split - moved).abs() < 1e-12,
                    "row {row}: shock + absorbed is {split}, the move is {moved}"
                );
                if buf.unbounded_print[row] != buf.prices[row] {
                    bound_moved += 1;
                }
                // Never an infinity. A share is NaN only where there was no
                // move to apportion, which is a statement about the row
                // rather than an escaped division.
                let share = buf.liquidity_share[row];
                assert!(!share.is_infinite(), "row {row}: liquidity share is {share}");
                if share.is_nan() {
                    undefined += 1;
                    assert_eq!(
                        buf.prices[row], previous,
                        "row {row}: a NaN share needs a print that did not move"
                    );
                    assert_ne!(
                        buf.unbounded_print[row], buf.prices[row],
                        "row {row}: a NaN share needs a counterfactual that moved"
                    );
                }
                checked += 1;
            }
        }
        assert!(checked > 0, "the session wrote no rows to check");
        assert!(
            bound_moved > 0,
            "no row had the depth bound move its print, so the counterfactual \
             assertions above ran over rows that could not fail them"
        );
        assert!(
            undefined > 0,
            "no row carried an undefined share, so the NaN branch is unguarded"
        );
    }

    #[test]
    fn a_chunked_session_matches_driving_the_ticks_by_hand() {
        // The whole point of `run_session` is that it is the SAME simulation,
        // just without the boundary crossings. If it diverged from the manual
        // loop it would be a second engine.
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];

        let chunked = {
            let mut e = engine(4242);
            let mut buf = SessionBuffer::new();
            e.run_session(&session(60, &innovations, &variances), &mut buf);
            (e.prices(), e.draws_consumed())
        };

        let by_hand = {
            let mut e = engine(4242);
            e.open_market();
            for m in 0..60i64 {
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
            }
            e.close_market(&DayCloseRequest {
                daily_innovations: &innovations,
                sector_base_variances: &variances,
                avg_volume: AvgVolumePolicy::default(),
            });
            (e.prices(), e.draws_consumed())
        };

        assert_eq!(chunked.1, by_hand.1, "draw counts must match");
        for (i, (a, b)) in chunked.0.iter().zip(&by_hand.0).enumerate() {
            assert_eq!(a.to_bits(), b.to_bits(), "company {i}");
        }
    }

    #[test]
    fn the_buffer_holds_every_tick_of_the_session() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = engine(11);
        let mut buf = SessionBuffer::new();
        e.run_session(&session(40, &innovations, &variances), &mut buf);

        assert_eq!(buf.ticks_written, 40);
        assert_eq!(buf.companies, 3);
        assert_eq!(buf.prices.len(), 40 * 3);
        // The last written cross-section is the engine's current state.
        assert_eq!(buf.tick_prices(39), e.prices().as_slice());
        assert!(buf.prices.iter().all(|p| p.is_finite() && *p > 0.0));
    }

    #[test]
    fn the_buffer_is_reused_across_days_rather_than_growing() {
        // Nothing accumulates Rust-side: a year of tick-grain output would be
        // millions of rows, and the buffer is one day.
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = engine(7);
        let mut buf = SessionBuffer::new();

        for _ in 0..5 {
            e.run_session(&session(30, &innovations, &variances), &mut buf);
            assert_eq!(buf.prices.len(), 30 * 3, "the buffer grew across days");
        }
    }

    #[test]
    fn a_stop_condition_halts_the_session_and_leaves_the_day_open() {
        // Event-driven advancement. The close must NOT run: the day is not
        // over, the caller interrupted it, and running the close would roll
        // momentum on a half-day.
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = engine(31337);
        let mut buf = SessionBuffer::new();

        let mut req = session(390, &innovations, &variances);
        // A band tight enough that any movement trips it.
        let start = e.prices()[0];
        req.stop = Some(StopCondition::PriceOutside {
            company: 0,
            below: Some(start * 0.9999),
            above: Some(start * 1.0001),
        });

        let out = e.run_session(&req, &mut buf);
        assert!(out.halted_at.is_some(), "the stop condition never fired");
        let halted = out.halted_at.unwrap();
        assert!(
            halted < 389,
            "halted at the very end, so nothing was skipped"
        );
        assert_eq!(buf.ticks_written, halted + 1);
        // The close did not run. Probed via `last_daily_return`, which ONLY
        // `close_day` writes — `mispricing_momentum` would be the obvious
        // check and is wrong, because the tick lazy-initialises it to
        // Some(0.0) on a company that has never ticked.
        assert!(
            e.companies()[0].stock.last_daily_return.is_none(),
            "the close ran on an interrupted day"
        );
    }

    #[test]
    fn a_stop_condition_that_never_fires_runs_the_whole_session() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = engine(5);
        let mut buf = SessionBuffer::new();
        let mut req = session(50, &innovations, &variances);
        req.stop = Some(StopCondition::PriceOutside {
            company: 0,
            below: Some(0.01),
            above: Some(1e9),
        });
        let out = e.run_session(&req, &mut buf);
        assert_eq!(out.halted_at, None);
        assert_eq!(buf.ticks_written, 50);
    }

    #[test]
    fn the_session_reports_the_draws_it_consumed() {
        let innovations = vec![None; 3];
        let variances = vec![0.000225; 3];
        let mut e = engine(13);
        // The DELTA, not the lifetime total. A default preset with a macro
        // burn-in draws during construction -- pt-v18 runs 755 days of it --
        // and this test is about what the operation costs, not about what
        // building an engine costs. Asserting the total made the claim
        // depend on a dial in a different subsystem.
        let before = e.draws_consumed();
        let mut buf = SessionBuffer::new();
        let out = e.run_session(&session(10, &innovations, &variances), &mut buf);
        // 10 open ticks at 1 + 3 sectors + 2 and 4 per company.
        assert_eq!(out.draws_consumed, 10 * (1 + 3 + 2 * 3 + 4 * 3));
        assert_eq!(e.draws_consumed() - before, out.draws_consumed);
    }

    #[test]
    fn columns_are_aligned_with_the_company_order() {
        let e = engine(1);
        let prices = e.column(PriceField::Price);
        assert_eq!(prices.len(), e.len());
        for (i, c) in e.companies().iter().enumerate() {
            assert_eq!(prices[i], c.stock.price);
        }
    }

    #[test]
    fn writing_prices_updates_market_cap_with_them() {
        let mut e = engine(1);
        e.write_prices(&[1.0, 2.0, 3.0]);
        assert_eq!(e.prices(), vec![1.0, 2.0, 3.0]);
        assert_eq!(e.column(PriceField::MarketCap)[0], 1.0 * 1e8);
    }

    /// `idio_vol_alpha`: the VIX identity's idiosyncratic term reads the
    /// per-name ratio, as the tick's own draw does. A ratio of 4 on every
    /// name is exactly 4 times the term (a power of two scales each
    /// summand without rounding); one name at 4 lands in between; a ratio
    /// of one is the term with the state off. Every other term is the
    /// same. Dropping the ratio from `index_var.rs`, or passing the engine's
    /// ratios as an empty slice, fails here.
    #[test]
    fn the_vix_identity_reads_the_idiosyncratic_variance_ratio() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        let off = engine_v20(7).index_conditional_variance_terms_now();
        let params = crate::params::ModelParams {
            idio_vol_alpha: 0.2,
            idio_vol_beta: 0.5,
            ..crate::params::PT_V20
        };
        let mut e = Engine::with_params(
            7,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            params,
        );
        let one = e.index_conditional_variance_terms_now();
        assert_eq!(one, off);
        e.set_idio_vol_state(&[4.0; 3], &[0.0; 3], &[0.0; 3]).unwrap();
        let four = e.index_conditional_variance_terms_now();
        assert!(one.idio_raw > 0.0);
        assert_eq!(four.idio_raw, 4.0 * one.idio_raw);
        assert_eq!(
            crate::market::index_var::IndexVarianceTerms { idio_raw: one.idio_raw, ..four },
            one
        );
        e.set_idio_vol_state(&[4.0, 1.0, 1.0], &[0.0; 3], &[0.0; 3]).unwrap();
        let first = e.index_conditional_variance_terms_now();
        assert!(first.idio_raw > one.idio_raw && first.idio_raw < four.idio_raw);
    }

    #[test]
    fn a_full_session_runs_and_stays_bounded() {
        // 390 ticks with the day's boundaries, as an embedder would drive it.
        let mut e = engine(31337);
        e.open_market();
        for m in 0..390i64 {
            e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
        }
        e.close_market(&DayCloseRequest {
            daily_innovations: &[None; 3],
            sector_base_variances: &[0.000225; 3],
            avg_volume: AvgVolumePolicy::default(),
        });

        for (i, price) in e.prices().iter().enumerate() {
            assert!(
                price.is_finite() && *price > 0.0,
                "company {i} priced at {price}"
            );
            // The session breaker bounds every print against the open.
            let open = e.companies()[i].stock.previous_close;
            assert!(
                *price <= open * 1.25 + 1e-9 && *price >= (open * 0.75).max(0.01) - 1e-9,
                "company {i} escaped the session band: {price} against an open of {open}"
            );
        }
    }

    #[test]
    fn fundamentals_round_trip_including_the_absent_marker() {
        let mut e = engine(7);
        let n = e.len();
        assert!(n >= 3);

        e.set_fundamentals(
            &[8.0, f64::NAN, 0.0],
            &[25.0, 30.0, f64::NAN],
            &[0.2, 0.3, 0.4],
        )
        .expect("one value per company");

        let (eps, book, growth) = e.fundamentals();
        assert_eq!(eps[0], 8.0);
        assert!(eps[1].is_nan(), "NaN must survive as absent");
        // Zero is a real EPS -- a company that broke exactly even -- and must
        // NOT be confused with absent.
        assert_eq!(eps[2], 0.0);
        assert!(e.companies()[2].eps == Some(0.0));
        assert!(e.companies()[1].eps.is_none());
        assert_eq!(book[1], 30.0);
        assert!(book[2].is_nan());
        assert_eq!(growth[0], 0.2);
    }

    #[test]
    fn a_length_mismatch_is_refused_rather_than_truncated() {
        let mut e = engine(7);
        let n = e.len();
        assert!(e.set_fundamentals(&vec![1.0; n - 1], &vec![1.0; n], &vec![1.0; n]).is_err());
        assert!(e.set_fundamentals(&vec![1.0; n], &vec![1.0; n + 1], &vec![1.0; n]).is_err());
        assert!(e.set_fundamentals(&vec![1.0; n], &vec![1.0; n], &vec![1.0; n]).is_ok());
    }

    #[test]
    fn stale_earnings_move_the_price_which_is_why_this_exists() {
        // The claim the whole sync rests on: fair value is `eps * target_pe`,
        // so an engine that never hears about an earnings revision prices the
        // company on the fundamentals it was built with.
        //
        // Two identical engines, one told that every company doubled its
        // earnings. If the prices came out the same, syncing fundamentals
        // would be pointless work.
        //
        // The revision lands after the open. Before it, pt-v20's stationary
        // opening (pt-v20 and pt-v21) adopts any premium into the
        // mispricing so the opening price holds, and a revision there moved
        // no price inside a 60-tick session; after the open it moves the
        // price on every preset.
        let mut stale = engine(11);
        let mut fresh = engine(11);
        for e in [&mut stale, &mut fresh] {
            e.open_market();
            e.run_session(&session(30, &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());
        }

        let n = fresh.len();
        let (eps, book, growth) = fresh.fundamentals();
        let doubled: Vec<f64> = eps.iter().map(|v| v * 2.0).collect();
        fresh
            .set_fundamentals(&doubled, &book, &growth)
            .expect("one value per company");

        for e in [&mut stale, &mut fresh] {
            e.run_session(&stepped(60, 30, &[], &[], &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());
        }

        let a = stale.prices();
        let b = fresh.prices();
        assert_eq!(a.len(), n);
        assert!(
            a.iter().zip(b.iter()).any(|(x, y)| x != y),
            "doubling every company's earnings changed no price at all"
        );
        // And it is the RNG-free difference: both engines drew the same
        // number of times, so the divergence is the valuation rather than a
        // shifted stream.
        assert_eq!(stale.draws_consumed(), fresh.draws_consumed());
    }

    #[test]
    fn the_relative_knee_pulls_only_a_name_past_it_toward_it() {
        // `fair_value_relative_knee`: at the close, a name whose level sits
        // more than the knee below the equal-weighted mean moves toward the
        // knee by `1 - 0.5^(1/h)` of the gap; every other name is untouched.
        let mut p = crate::params::PT_V20;
        p.fair_value_relative_knee = 2.0;
        p.fair_value_relative_half_life = 10.0;
        let mut e = Engine::with_params(
            5,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            p,
        );
        assert!(e.carries_fair_value_offsets());
        for c in e.companies.iter_mut() {
            c.stock.mispricing_s = Some(0.0);
        }
        e.set_fair_value_offsets(&[-6.0, 0.3, -0.3]).unwrap();
        e.pull_relative_levels();
        let v = e.fair_value_offsets();
        let mean = (-6.0 + 0.3 - 0.3) / 3.0;
        let pull = 1.0 - crate::mathx::pow(0.5, 1.0 / 10.0);
        assert_eq!(v[0], -6.0 + pull * ((mean - 2.0) - (-6.0)));
        assert_eq!(v[1], 0.3);
        assert_eq!(v[2], -0.3);
        // Off at 0.0: nothing moves, whatever the half-life.
        e.params.fair_value_relative_knee = 0.0;
        e.set_fair_value_offsets(&[-6.0, 0.3, -0.3]).unwrap();
        e.pull_relative_levels();
        assert_eq!(e.fair_value_offsets(), vec![-6.0, 0.3, -0.3]);
    }

    #[test]
    fn a_bankrupt_company_stops_ticking_once_the_engine_is_told() {
        // The reason `set_status` exists. The tick skips a company only when
        // it reads `is_bankrupt || !is_public`, so before there was a setter
        // a failed company went on printing prices for ever.
        let mut e = engine(5);
        e.open_market();
        e.run_session(&session(30, &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());
        let before = e.prices()[0];

        e.set_status(&[true, false, false], &[true, true, true])
            .expect("one flag per company");
        e.run_session(&session(30, &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());

        assert_eq!(
            e.prices()[0],
            before,
            "company 0 was marked bankrupt and kept printing"
        );
        // And its neighbours did keep moving, so the test is not observing a
        // dead market.
        assert!(
            e.prices()[1] != before || e.prices()[2] != before,
            "nothing moved at all; this proves nothing about the flag"
        );
    }

    #[test]
    fn a_company_taken_private_stops_ticking_too() {
        let mut e = engine(5);
        e.open_market();
        e.run_session(&session(30, &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());
        let before = e.prices()[1];

        e.set_status(&[false; 3], &[true, false, true])
            .expect("one flag per company");
        e.run_session(&session(30, &[None; 3], &[0.000225; 3]), &mut SessionBuffer::new());
        assert_eq!(e.prices()[1], before, "an unlisted company kept printing");
    }

    #[test]
    fn status_round_trips_and_refuses_a_length_mismatch() {
        let mut e = engine(5);
        e.set_status(&[true, false, true], &[false, true, false])
            .expect("one flag per company");
        let (bankrupt, public) = e.status();
        assert_eq!(bankrupt, vec![true, false, true]);
        assert_eq!(public, vec![false, true, false]);
        assert!(e.set_status(&[true; 2], &[true; 3]).is_err());
        assert!(e.set_status(&[true; 3], &[true; 4]).is_err());
    }

    #[test]
    fn no_preset_opens_with_its_macro_calendars_in_the_future() {
        // The defect this guards, measured on pt-v18 before the fix: the
        // 755-day burn-in ran on the caller's own day numbering, so it left
        // `next_meeting_date` at day 781 and `oil_last_opec_day` at day 720.
        // A run starts at day 1, both are read as differences against the
        // current day, and so the first central-bank meeting fell on day 781
        // against pt-v16's day 45 and the first OPEC decision on day 810
        // against day 90. The certified 252-day horizon held neither.
        //
        // Asserted as BEHAVIOUR over the certified horizon rather than as
        // clock values, because the clock values are an implementation of
        // this and the horizon is the claim. Every shipped preset, so a
        // preset that switches the burn-in on later cannot reintroduce it.
        for name in crate::params::ModelParams::preset_names() {
            let params = crate::params::ModelParams::preset(name)
                .expect("a listed preset resolves");
            let mut e = Engine::with_params(
                3,
                vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
                create_initial_economy_state(&InitialEconomyOptions::default()),
                create_initial_central_bank_state(0),
                sectors(),
                params,
            );
            assert!(
                e.economy().oil_last_opec_day <= 0,
                "{name}: the run opens with its last OPEC decision on day {},                  which is in the future of a run that starts at day 1",
                e.economy().oil_last_opec_day
            );
            let mut meetings = 0;
            let mut opec_days = Vec::new();
            let mut last_opec = e.economy().oil_last_opec_day;
            // The certified horizon, 252 days (`envelope.CERTIFIED_HORIZON_DAYS`).
            for day in 1..=252i64 {
                let out = e.advance_macro_day(day);
                if out.meeting_held {
                    meetings += 1;
                }
                if e.economy().oil_last_opec_day != last_opec {
                    last_opec = e.economy().oil_last_opec_day;
                    opec_days.push(day);
                }
            }
            assert!(
                meetings > 0,
                "{name}: no central-bank meeting in the certified 252 days, so \n                 monetary policy is inert across the whole window every \n                 published figure is measured on"
            );
            assert!(
                !opec_days.is_empty(),
                "{name}: no OPEC decision in the certified 252 days"
            );
        }
    }

    #[test]
    fn a_held_meeting_reports_which_decision_produced_the_rate_move() {
        // `update_central_bank` computes a `Decision` and an announcement
        // variant, and `advance_day` used to reduce both to a boolean. An
        // embedder saw the rate move with nothing able to say why, and the
        // obvious workaround -- classifying from the rate delta -- cannot
        // separate `StagflationHike` from `LaborEmergencyCut`, which differ
        // by the context that selected them rather than the size of the move.
        let mut e = engine(3);
        let mut held = 0;
        let mut quiet = 0;
        for day in 1..=400i64 {
            let out = e.advance_macro_day(day);
            if out.meeting_held {
                held += 1;
                assert!(
                    out.decision.is_some(),
                    "day {day}: a meeting was held with no decision reported"
                );
                assert!(
                    out.announcement_variant.is_some(),
                    "day {day}: a meeting was held with no announcement variant"
                );
                assert!(out.announcement_variant.unwrap() < 6, "variant out of range");
            } else {
                quiet += 1;
                assert!(out.decision.is_none());
                assert!(out.announcement_variant.is_none());
            }
        }
        assert!(held > 0, "no meeting in 400 days, so the test proved nothing");
        assert!(quiet > 0, "every day was a meeting, so the None case is untested");
    }

    // ----------------------------------------------------------------------
    // The per-day state hash (P9)
    // ----------------------------------------------------------------------

    #[test]
    fn the_hash_column_order_and_the_position_table_agree() {
        // Two declarations of one order. `state_hash_position` is exhaustive
        // on `PriceField`, so a nineteenth column fails to compile there; this
        // is what stops the two drifting apart once it does.
        for (i, field) in STATE_HASH_COLUMNS.iter().enumerate() {
            assert_eq!(
                state_hash_position(*field),
                i,
                "{field:?} sits at {i} in STATE_HASH_COLUMNS"
            );
        }
        let mut seen: Vec<usize> = STATE_HASH_COLUMNS
            .iter()
            .map(|f| state_hash_position(*f))
            .collect();
        seen.sort_unstable();
        seen.dedup();
        assert_eq!(seen.len(), 18, "every column has its own position");
    }

    #[test]
    fn a_pt_v20_engine_walks_the_cycle_shares_once() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        // `refresh_earnings_anticipation` asks for the cycle's stationary
        // shares on every close, burn-in included, and pt-v20 is the first
        // preset that turns anticipation on. Each answer is a survival walk
        // of about a thousand `pow` calls a phase. Before the memo, building
        // this engine took 756 walks, 96 per cent of its construction time.
        let start = crate::economy::cycle::share_walks();
        let mut e = engine_v20(7);
        assert_ne!(e.params().earnings_anticipation_half_life, 0.0);
        for day in 1..=3i64 {
            e.open_market();
            for m in 0..10 {
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
            }
            e.close_day(day);
        }
        let walks = crate::economy::cycle::share_walks() - start;
        assert!(walks <= 1, "{walks} survival walks for one fixed cycle spec");
    }

    #[test]
    fn the_state_hash_works_out_the_model_fingerprint_once() {
        // pt-v20 by name, the default until 0.10.0: pt-v21 sets the dials
        // this test reads as off. Each `engine_v20` here read `engine`.
        // The sandbox's tamper guard hashes the state before and after every
        // call into agent code. The hash folds in the model's fingerprint,
        // which was twenty digests of the preset surface each time and about
        // 90 per cent of the hash's cost.
        let e = engine_v20(7);
        let first = e.state_hash(0, false);
        let taken = crate::params::digests_taken();
        for _ in 0..5 {
            assert_eq!(e.state_hash(0, false), first);
        }
        let copy = e.clone();
        assert_eq!(copy.state_hash(0, false), first);
        assert_eq!(crate::params::digests_taken(), taken, "the fingerprint was worked out again");
        assert_eq!(e.model_fingerprint(), e.params().fingerprint());
        assert_eq!(e.model_fingerprint(), "pt-v20");
        // And the default engine, which is pt-v22 from 0.11.0 (pt-v21 from 0.10.0).
        assert_eq!(engine(8).model_fingerprint(), "pt-v22");
        assert_eq!(engine_v19(7).model_fingerprint(), "pt-v19");
    }

    #[test]
    fn a_clone_hashes_the_same_and_a_tick_moves_the_hash() {
        let mut e = engine(77);
        e.open_market();
        for m in 0..30 {
            e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
        }
        let copy = e.clone();
        assert_eq!(
            e.state_hash(3, true),
            copy.state_hash(3, true),
            "a clone is the same state, so it is the same leaf"
        );

        let before = e.state_hash(3, true);
        e.tick(&request(10, 5));
        assert_ne!(
            before,
            e.state_hash(3, true),
            "a tick moved prices and the generator, so the leaf must move"
        );
    }

    #[test]
    fn the_two_binding_owned_fields_reach_the_hash() {
        // `day_count` and `market_open` are in the snapshot and not on this
        // struct, so they arrive as arguments. A hash that ignored them would
        // let a leaf taken before a close pass for one taken after it.
        let e = engine(9);
        assert_ne!(e.state_hash(3, false), e.state_hash(4, false));
        assert_ne!(e.state_hash(3, false), e.state_hash(3, true));
    }

    #[test]
    fn two_generator_states_a_float_digest_would_collapse_hash_apart() {
        // The reason the generator states are hashed as `u64` bit patterns
        // rather than through the canonical-NaN float rule. Both states below
        // read as NaN when interpreted as an f64, so a float digest would
        // write the same eight bytes for two different generator positions
        // and a tampered position would verify.
        let left_bits = 0x7ff8_0000_0000_0001u64;
        let right_bits = 0x7ff8_0000_0000_0002u64;
        assert!(f64::from_bits(left_bits).is_nan());
        assert!(f64::from_bits(right_bits).is_nan());

        let mut a = engine(5);
        let mut b = engine(5);
        let mut sa = a.rng_state();
        sa.market.state = left_bits;
        a.set_rng_state(sa);
        let mut sb = b.rng_state();
        sb.market.state = right_bits;
        b.set_rng_state(sb);

        assert_ne!(
            a.state_hash(0, false),
            b.state_hash(0, false),
            "the two generator positions must hash apart"
        );
    }

    #[test]
    fn the_hash_follows_the_macro_chain_and_the_central_bank() {
        // `market_digest` covers nine columns and the draw count, so a macro
        // difference alone leaves it unmoved. This leaf has to see it: the
        // whole reason a ledger hashes state rather than prices.
        let mut e = engine(11);
        let before = e.state_hash(0, false);
        e.economy_mut().vix = e.economy().vix + 1.0;
        assert_ne!(before, e.state_hash(0, false), "the VIX moved");

        let mut f = engine(11);
        let before = f.state_hash(0, false);
        f.central_bank_mut().hawkish_dovish_score += 0.5;
        assert_ne!(before, f.state_hash(0, false), "the bank's score moved");
    }

    #[test]
    fn the_hash_separates_two_days_of_one_run() {
        // What a ledger needs: consecutive leaves of one run are distinct, so
        // a day swapped for its neighbour fails on its own leaf.
        let mut e = engine(31);
        let mut leaves = Vec::new();
        for day in 1..=4i64 {
            e.open_market();
            for m in 0..40 {
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
            }
            e.close_day(day);
            leaves.push(e.state_hash(day as u32, false));
        }
        for i in 0..leaves.len() {
            for j in (i + 1)..leaves.len() {
                assert_ne!(leaves[i], leaves[j], "days {i} and {j} share a leaf");
            }
        }
    }

    /// pt-v20 with every branch of the valuation live: the buyback term,
    /// the earnings cycle and its anticipation, and the re-mark itself.
    fn engine_repricing(seed: u64, on: bool) -> Engine {
        let mut p = ModelParams::preset("pt-v20").expect("pt-v20 ships");
        p.buyback_payout_share = 0.75;
        p.earnings_anticipation_half_life = 126.0;
        p.rate_pe_sensitivity = 3.0;
        p.macro_publication_repricing = if on { 1.0 } else { 0.0 };
        Engine::with_params(
            seed,
            vec![company("A", 100.0), company("B", 50.0), company("C", 220.0)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            sectors(),
            p,
        )
    }

    #[test]
    fn tick_fair_value_is_the_ticks_own_fundamental_to_the_bit() {
        // The re-mark reads fair value through `tick_fair_value`, a copy of
        // the tick's phase 2. If the two ever part, the re-mark prices a
        // state the tick does not, and the first tick moves again.
        //
        // The tick's `fundamental` is the fair value AFTER its own
        // permanent share of the minute's shocks (`fair_value_news_share`),
        // which the re-mark has no reason to read, so both shares are off
        // here and the column is the valuation the tick started from. The
        // opening's levels, the nominal path, the earnings cycle and the
        // buyback term (the day set, so it is live) all stay on.
        let mut e = engine_repricing(21, false);
        e.params.fair_value_news_share = 0.0;
        e.params.fair_value_market_share = 0.0;
        for day in 1..=3i64 {
            e.set_current_day(day);
            e.open_market();
            for m in 0..30 {
                let start = e.clone();
                e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
                for (i, c) in start.companies().iter().enumerate() {
                    // A name's first tick runs the opening, which writes its
                    // fair-value level inside the tick; the re-mark never
                    // reads a name that has not traded.
                    if c.stock.mispricing_s.is_none() {
                        continue;
                    }
                    let fv = crate::market::tick::tick_fair_value(
                        start.params(), start.economy(), start.nominal_output_base(),
                        start.current_day(), c, c.stock.price);
                    assert_eq!(fv.to_bits(), e.tick_fundamental()[i].to_bits(),
                               "day {day} minute {m} name {i}");
                }
            }
            e.close_day(day);
        }
    }

    #[test]
    fn the_close_prices_what_its_macro_step_publishes() {
        let mut on = engine_repricing(33, true);
        let mut off = engine_repricing(33, false);
        for day in 1..=6i64 {
            for e in [&mut on, &mut off] {
                // The day set, so the buyback term reads the price.
                e.set_current_day(day);
                e.open_market();
                for m in 0..40 {
                    e.tick(&request(9 + (30 + m) / 60, (30 + m) % 60));
                }
                e.close_market(&DayCloseRequest {
                    daily_innovations: &[None, None, None],
                    sector_base_variances: &[0.0004, 0.0004, 0.0004],
                    avg_volume: crate::market::AvgVolumePolicy::Hold,
                });
            }
            // Pinned apart so every day's step moves fair value.
            let bump = if day % 2 == 0 { 0.5 } else { -0.4 };
            on.economy_mut().corporate_bond_yield += bump;
            off.economy_mut().corporate_bond_yield += bump;
            let last: Vec<f64> = on.prices();
            let marks = on.published_macro_marks().expect("on");
            assert!(off.published_macro_marks().is_none(), "off computes nothing");
            let s_before: Vec<Option<f64>> =
                on.companies().iter().map(|c| c.stock.mispricing_s).collect();
            on.advance_macro_day(day);
            off.advance_macro_day(day);
            for (i, c) in on.companies().iter().enumerate() {
                // The re-marked price is the one the name's mispricing
                // implies on the published state, fair value read at that
                // very price (the buyback term reads it).
                let fv1 = crate::market::tick::tick_fair_value(
                    on.params(), on.economy(), on.nominal_output_base(),
                    on.current_day(), c, c.stock.price);
                let want = last[i] / marks[i] * fv1;
                assert!((c.stock.price / want - 1.0).abs() < 1e-13,
                        "day {day} name {i}: {} against {want}", c.stock.price);
                assert_eq!(c.stock.mispricing_s, s_before[i], "the re-mark leaves s alone");
                assert!((c.stock.price / last[i] - 1.0).abs() > 1e-6, "the step moved fair value");
            }
            // Off, the close's price is the last print, as it always was.
            let off_prices = off.prices();
            let off_last: Vec<f64> = off.companies().iter().map(|c| c.stock.price).collect();
            assert_eq!(off_prices, off_last);
            // Put the pair back on one market for the next day, so each day
            // tests the step alone.
            let synced: Vec<f64> = on.prices();
            for (c, p) in off.companies_mut().iter_mut().zip(synced.iter()) {
                c.stock.price = *p;
                c.stock.market_cap = *p * c.stock.shares_outstanding;
            }
        }
    }
}


/// What a host that drives the engine relies on: whose opening it is, where
/// the previous close is, and where the crisis line sits.
#[cfg(test)]
mod host_tests {
    use super::*;
    use crate::economy::{create_initial_central_bank_state, create_initial_economy_state};

    fn roster(seed: u64) -> Vec<TickCompany> {
        crate::universe::random_universe(6, seed)
            .iter()
            .enumerate()
            .map(|(i, g)| g.to_init().to_tick_company(i))
            .collect()
    }

    fn sector_keys() -> Vec<String> {
        crate::sectors::keys().iter().map(|s| s.to_string()).collect()
    }

    /// A host's own opening: a crisis VIX, a 5 per cent policy rate and a
    /// contraction, none of which the default economy holds.
    fn host_economy() -> EconomyState {
        let mut e = create_initial_economy_state(&Default::default());
        e.vix = 45.0;
        e.federal_funds_rate = 5.0;
        e.cycle_phase = crate::economy::CyclePhase::Contraction;
        e
    }

    #[test]
    fn with_params_settles_a_host_economy_and_keeping_opening_keeps_it() {
        let p = ModelParams::preset("pt-v20").unwrap();
        let settled = Engine::with_params(
            7, roster(7), host_economy(), create_initial_central_bank_state(0),
            sector_keys(), p.clone(),
        );
        let kept = Engine::with_params_keeping_opening(
            7, roster(7), host_economy(), create_initial_central_bank_state(0),
            sector_keys(), p,
        );
        eprintln!(
            "pt-v20 host opening VIX 45.0, policy rate 5.00, contraction: \
             with_params opens at VIX {:.2}, rate {:.2}, {:?}; \
             with_params_keeping_opening at VIX {:.2}, rate {:.2}, {:?}",
            settled.economy().vix, settled.economy().federal_funds_rate,
            settled.economy().cycle_phase,
            kept.economy().vix, kept.economy().federal_funds_rate,
            kept.economy().cycle_phase,
        );
        assert!(settled.opening_settled());
        assert!((settled.economy().vix - 45.0).abs() > 5.0, "the burn-in relaxes the VIX");
        assert!(!kept.opening_settled());
        assert_eq!(kept.economy().vix, 45.0);
        assert_eq!(kept.economy().federal_funds_rate, 5.0);
        assert_eq!(kept.economy().cycle_phase, crate::economy::CyclePhase::Contraction);
    }

    #[test]
    fn a_preset_with_nothing_to_settle_reports_an_unsettled_opening() {
        // pt-v1 has no burn-in and no stationary draw, so `with_params`
        // keeps the economy too, and says so.
        let e = Engine::with_params(
            7, roster(7), host_economy(), create_initial_central_bank_state(0),
            sector_keys(), ModelParams::preset("pt-v1").unwrap(),
        );
        assert!(!e.opening_settled());
        assert_eq!(e.economy().vix, 45.0);
    }

    #[test]
    fn keeping_the_opening_is_from_opening_with_settle_false() {
        let p = ModelParams::preset("pt-v20").unwrap();
        let mut a = Engine::with_params_keeping_opening(
            9, roster(9), host_economy(), create_initial_central_bank_state(0),
            sector_keys(), p.clone(),
        );
        let mut b = Engine::with_params_from_opening(
            9, roster(9), host_economy(), create_initial_central_bank_state(0),
            sector_keys(), p, false,
        );
        let mut buffer = crate::engine::SessionBuffer::new();
        for day in 1..=3 {
            for e in [&mut a, &mut b] {
                e.open_market();
                let bell = crate::market::GameTime::new(9, 30, 3);
                e.run_session(&SessionRequest::new(bell, 390), &mut buffer);
                e.close_day(day);
            }
        }
        assert_eq!(a.state_hash(3, false), b.state_hash(3, false));
    }

    fn run_day(e: &mut Engine, day: i64, buffer: &mut SessionBuffer) -> Vec<f64> {
        e.open_market();
        let bell = crate::market::GameTime::new(9, 30, 3);
        e.run_session(&SessionRequest::new(bell, 390), buffer);
        let last_print = e.prices();
        e.close_day(day);
        last_print
    }

    fn lookthrough_params() -> ModelParams {
        let mut p = ModelParams::preset("pt-v20").unwrap();
        p.earnings_lookthrough_half_life = 598.0;
        p.earnings_lookthrough_gain = 0.1605;
        p.earnings_lookthrough_discount_half_life = 5267.0;
        p
    }

    fn lookthrough_engine(p: ModelParams) -> Engine {
        Engine::with_params(
            13, roster(13), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(), p,
        )
    }

    #[test]
    fn earnings_lookthrough_is_inert_off_and_without_a_write() {
        // Off: a noted write changes nothing. On: a run with no write prints
        // what the run with the dials off prints, bit for bit.
        let mut off = lookthrough_engine(ModelParams::preset("pt-v20").unwrap());
        let mut noted = lookthrough_engine(ModelParams::preset("pt-v20").unwrap());
        let mut on = lookthrough_engine(lookthrough_params());
        noted.note_market_earnings(-0.2).unwrap();
        assert_eq!(noted.economy().earnings_lookthrough, 0.0);
        assert_eq!(noted.earnings_lookthrough_words(), None);
        let mut buffer = SessionBuffer::new();
        for day in 1..=4 {
            let a = run_day(&mut off, day, &mut buffer);
            let b = run_day(&mut noted, day, &mut buffer);
            let c = run_day(&mut on, day, &mut buffer);
            assert_eq!(a, b);
            assert_eq!(a, c);
        }
        assert_eq!(on.earnings_lookthrough_words(), Some([0.0, 0.0]));
        assert_eq!(on.economy().earnings_lookthrough, 0.0);
        assert_eq!(off.state_hash(4, false), noted.state_hash(4, false));
    }

    #[test]
    fn earnings_lookthrough_values_a_share_of_a_write_and_its_anchor_follows() {
        let p = lookthrough_params();
        let (h, k, hr) = (p.earnings_lookthrough_half_life, p.earnings_lookthrough_gain,
                          p.earnings_lookthrough_discount_half_life);
        let c = h / (h + hr);
        let mut e = lookthrough_engine(p);
        e.note_market_earnings(-0.2).unwrap();
        let [m, a] = e.earnings_lookthrough_words().unwrap();
        assert_eq!(m, -0.2);
        assert!((a - (-0.2 * k)).abs() < 1e-15);
        // The valuation reads a + c (m - a): the look-through is -(1 - c)(m - a).
        let look = e.economy().earnings_lookthrough;
        assert!((look - (1.0 - c) * (1.0 - k) * 0.2).abs() < 1e-15, "{look}");
        // A close moves the anchor by K of the reversion expected and not seen.
        let mut buffer = SessionBuffer::new();
        run_day(&mut e, 1, &mut buffer);
        let [m1, a1] = e.earnings_lookthrough_words().unwrap();
        let expected = a + k * (1.0 - 0.5f64.powf(1.0 / h)) * (m - a);
        assert_eq!(m1, m);
        assert!((a1 - expected).abs() < 1e-15);
        assert!(e.note_market_earnings(f64::NAN).is_err());
    }

    #[test]
    fn earnings_lookthrough_survives_a_restore_and_is_hashed() {
        let mut a = lookthrough_engine(lookthrough_params());
        let mut b = lookthrough_engine(lookthrough_params());
        let mut buffer = SessionBuffer::new();
        run_day(&mut a, 1, &mut buffer);
        run_day(&mut b, 1, &mut buffer);
        assert_eq!(a.state_hash(1, false), b.state_hash(1, false));
        a.note_market_earnings(-0.1).unwrap();
        assert_ne!(a.state_hash(1, false), b.state_hash(1, false));
        let saved = a.snapshot().to_bytes();
        let mut resumed = lookthrough_engine(lookthrough_params());
        resumed.restore(&crate::snapshot::EngineSnapshot::from_bytes(&saved).unwrap()).unwrap();
        assert_eq!(resumed.earnings_lookthrough_words(), a.earnings_lookthrough_words());
        assert_eq!(resumed.economy().earnings_lookthrough, a.economy().earnings_lookthrough);
        assert_eq!(resumed.state_hash(1, false), a.state_hash(1, false));
        let x = run_day(&mut a, 2, &mut buffer);
        let y = run_day(&mut resumed, 2, &mut buffer);
        assert_eq!(x, y);
        // A model without the dial refuses the key, and one with it needs it.
        let mut plain = lookthrough_engine(ModelParams::preset("pt-v20").unwrap());
        assert!(plain.restore(&crate::snapshot::EngineSnapshot::from_bytes(&saved).unwrap()).is_err());
    }

    #[test]
    fn prior_closes_are_the_last_print_and_previous_close_is_the_open() {
        let mut e = Engine::with_params(
            11, roster(11), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(),
            ModelParams::preset("pt-v20").unwrap(),
        );
        let built = e.prices();
        assert_eq!(e.last_closes(), built.as_slice());
        assert_eq!(e.prior_closes(), built.as_slice());

        let mut buffer = SessionBuffer::new();
        let mut last_print = run_day(&mut e, 1, &mut buffer);
        assert_eq!(e.last_closes(), last_print.as_slice());
        let mut differed = 0;
        for day in 2..=6 {
            e.open_market();
            assert_eq!(e.prior_closes(), last_print.as_slice());
            let previous_close = e.column(PriceField::PreviousClose);
            // pt-v20 re-marks at the close, so the open the session band is
            // anchored on is not the last print.
            differed += previous_close
                .iter()
                .zip(&last_print)
                .filter(|(a, b)| a != b)
                .count();
            let bell = crate::market::GameTime::new(9, 30, 3);
            e.run_session(&SessionRequest::new(bell, 390), &mut buffer);
            last_print = e.prices();
            e.close_day(day);
            // Still the day's reference after its close, until the next open.
            assert_ne!(e.prior_closes(), last_print.as_slice());
            assert_eq!(e.last_closes(), last_print.as_slice());
        }
        assert!(differed > 0, "previous_close equalled the last print on every name and day");
    }

    #[test]
    fn closes_follow_the_roster_and_restore_checks_lengths() {
        let mut e = Engine::with_params(
            12, roster(12), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(),
            ModelParams::preset("pt-v20").unwrap(),
        );
        let n = e.len();
        let mut joiner = roster(99).remove(0);
        joiner.id = "JOIN".into();
        joiner.ticker = "JOIN".into();
        let price = joiner.stock.price;
        e.add_company(joiner);
        assert_eq!(e.last_closes().len(), n + 1);
        assert_eq!(e.prior_closes()[n], price);
        let second = e.last_closes()[1];
        e.remove_company(0);
        assert_eq!(e.last_closes().len(), n);
        assert_eq!(e.last_closes()[0], second);

        assert!(e.restore_closes(&[1.0], &[1.0]).is_err());
        let ones = vec![1.0; n];
        let twos = vec![2.0; n];
        e.restore_closes(&ones, &twos).unwrap();
        assert_eq!(e.last_closes(), ones.as_slice());
        assert_eq!(e.prior_closes(), twos.as_slice());
    }

    #[test]
    fn closes_change_no_price_and_no_hash() {
        // Restoring nonsense closes moves nothing the trajectory reads.
        let build = || Engine::with_params(
            13, roster(13), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(),
            ModelParams::preset("pt-v20").unwrap(),
        );
        let (mut a, mut b) = (build(), build());
        let n = b.len();
        b.restore_closes(&vec![1e9; n], &vec![-1.0; n]).unwrap();
        let mut buffer = SessionBuffer::new();
        for day in 1..=3 {
            run_day(&mut a, day, &mut buffer);
            run_day(&mut b, day, &mut buffer);
        }
        assert_eq!(a.prices(), b.prices());
        assert_eq!(a.state_hash(3, false), b.state_hash(3, false));
    }

    #[test]
    fn the_crisis_threshold_is_the_presets_and_follows_an_override() {
        let p = ModelParams::preset("pt-v20").unwrap();
        let mut e = Engine::with_params_keeping_opening(
            5, roster(5), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(), p.clone(),
        );
        assert_eq!(e.crisis_vix_threshold(), 30.88325108);
        assert_eq!(e.crisis_vix_threshold(), e.params().crisis_vix_threshold);
        e.economy_mut().vix = 30.88325108;
        assert!(!e.vix_above_crisis_threshold(), "the gate is a strict >");
        e.economy_mut().vix = 30.9;
        assert!(e.vix_above_crisis_threshold());

        let moved = p.with_override("crisis_vix_threshold", 30.0).unwrap();
        let mut e = Engine::with_params_keeping_opening(
            5, roster(5), create_initial_economy_state(&Default::default()),
            create_initial_central_bank_state(0), sector_keys(), moved,
        );
        assert_eq!(e.crisis_vix_threshold(), 30.0);
        e.economy_mut().vix = 30.5;
        assert!(e.vix_above_crisis_threshold());
        assert!(e.model_fingerprint().starts_with("custom-"));
    }
}