qx-execution 0.4.4

牵星统一执行事实归约边界
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
//! 统一执行层边界。
//!
//! Venue 只返回 `VenueEvent`,执行层负责把这些事件转换成运行时标准事实,
//! 再通过本 crate `application` 子模块的执行端口交给 EventLog、订单状态和 Ledger。Paper、
//! Binance 以及未来连接器都必须复用这里的事实转换,不能在 CLI 中各写一套。
//! 本 crate 不依赖任何具体运行时或存储后端。

pub mod algo_execution;
pub mod application;
mod spread_group_barrier;
pub use algo_execution::{
    AlgoExecutionState, AlgoExecutionStatus, AlgoExecutionStep, AlgoParentOrder,
    ALGO_EXECUTION_STATE_SCHEMA_VERSION,
};
pub use application::*;
use qx_control::{cancel_order_target_from_command, order_from_submit_command, ControlCommand};
use qx_core::{DecimalRounding, ExactDecimal, FeeModel};
use qx_guanxing::QuoteTick;
use qx_risk::OrderRiskPosition;
pub use qx_zhenlu::algo::{
    plan_twap, plan_vwap, AlgoChildSlice, AlgoExecutionError, VwapVolumeBucket, MAX_ALGO_CHILDREN,
};
use qx_zhenlu::{
    OrderGroupStore, PaperVenue, RiskContext, SpreadOrderGroup, SpreadOrderGroupStatus,
    SpreadOrderGroupStore, Venue, VenueEvent,
};

pub fn spread_group_barrier(
    store: Option<&dyn SpreadOrderGroupStore>,
    command: &ControlCommand,
) -> Result<(), String> {
    spread_group_barrier::check_spread_group_barrier(store, command)
}

pub struct RiskExecutionContext<'a> {
    pub risk: &'a RiskContext,
    pub position: &'a OrderRiskPosition,
}

/// Required inputs for the shared order submission boundary. The group store stays
/// explicit so a caller cannot accidentally bypass the multi-leg barrier.
pub struct SubmitOrderWithRiskRequest<'a, V: Venue, P: ExecutionEventPort> {
    pub command: &'a ControlCommand,
    pub venue: &'a mut V,
    pub pipeline: &'a mut P,
    pub worker_id: &'a str,
    pub now: u64,
    pub source_seq: &'a mut u64,
    pub context: &'a RiskExecutionContext<'a>,
    pub spread_store: Option<&'a dyn SpreadOrderGroupStore>,
}

/// Required Paper execution inputs. Every safety and pricing decision is a named,
/// mandatory field; this type intentionally has no `Default` implementation.
pub struct PaperSubmitRequest<'a, P: ExecutionEventPort + application::LedgerProbe> {
    pub command: &'a ControlCommand,
    pub pipeline: &'a mut P,
    pub now: u64,
    pub risk: Option<RiskContext>,
    pub position: Option<OrderRiskPosition>,
    pub market_quote: Option<QuoteTick>,
    pub fee_model: Box<dyn FeeModel + Send>,
    pub allow_synthetic_quote: bool,
    pub spread_store: Option<&'a dyn SpreadOrderGroupStore>,
}

/// 把当前账户快照适配为应用层 RiskPort,便于回测、Paper 和实盘执行器
/// 替换风控实现而不改动 `ExecutionGateway` 的订单副作用边界。
pub struct RiskContextPort<'a> {
    pub risk: &'a RiskContext,
    pub position: &'a OrderRiskPosition,
}

/// 直接适配 `qx-risk::OrderRiskContext` 的应用层端口。
///
/// `RiskContextPort` 保留 qx-zhenlu 的兼容快照入口;新执行器可以直接注入
/// canonical context,避免回测、Paper 和实盘再各自复制一份规格/保证金/
/// 投影持仓转换逻辑。
pub struct CanonicalRiskPort<'a> {
    pub context: &'a qx_risk::OrderRiskContext,
}

/// 将现有 Venue 事实适配为应用层 VenuePort。它只做事实类型转换,不写
/// EventLog/OMS;真正的副作用仍由 `ExecutionGateway`(`PortExecutionService`)统一编排。
pub struct VenuePortAdapter<V: Venue> {
    venue: V,
    accept_any_venue: bool,
}

/// 把仍使用旧版 `Venue` trait 的连接器借用到统一 `VenuePort`,使只持有可变
/// 借用的执行入口(Paper worker、多腿编排)也走同一个 `ExecutionGateway`。
pub struct BorrowedVenuePort<'a, V: Venue + ?Sized> {
    venue: &'a mut V,
    accept_any_venue: bool,
}

impl<'a, V: Venue + ?Sized> BorrowedVenuePort<'a, V> {
    pub fn new(venue: &'a mut V) -> Self {
        Self {
            venue,
            accept_any_venue: false,
        }
    }

    pub fn new_for_any_venue(venue: &'a mut V) -> Self {
        Self {
            venue,
            accept_any_venue: true,
        }
    }
}

impl<V: Venue> VenuePortAdapter<V> {
    pub fn new(venue: V) -> Self {
        Self {
            venue,
            accept_any_venue: false,
        }
    }

    /// Paper/模拟路由可以把多个底层 Instrument Venue 映射到同一个虚拟
    /// Venue;实盘适配器必须继续使用 `new`,保持严格路由校验。
    pub fn new_for_any_venue(venue: V) -> Self {
        Self {
            venue,
            accept_any_venue: true,
        }
    }

    pub fn into_inner(self) -> V {
        self.venue
    }

    pub fn venue(&self) -> &V {
        &self.venue
    }
}

fn venue_events_to_application(events: Vec<VenueEvent>) -> Vec<ExecutionEvent> {
    events
        .into_iter()
        .map(|event| match event {
            VenueEvent::Accepted {
                client_order_id,
                venue_order_id,
                ..
            } => ExecutionEvent::Accepted {
                client_order_id,
                venue_order_id,
            },
            VenueEvent::Rejected {
                client_order_id,
                reason,
                ..
            } => ExecutionEvent::Rejected {
                client_order_id,
                reason,
            },
            VenueEvent::Fill(fill) => ExecutionEvent::Fill(fill),
            VenueEvent::FeeAdjustment {
                account_id,
                venue_id,
                order_id,
                instrument,
                fees,
                ..
            } => ExecutionEvent::FeeAdjustment {
                account_id,
                venue_id,
                order_id,
                instrument,
                fees,
            },
            VenueEvent::Cancelled {
                client_order_id, ..
            } => ExecutionEvent::Cancelled { client_order_id },
        })
        .collect()
}

fn venue_group_submission_to_application(
    submission: qx_zhenlu::VenueOrderGroupSubmission,
) -> OrderGroupSubmission {
    OrderGroupSubmission {
        venue_group_id: submission.venue_group_id,
        group_status: submission.group_status,
        events: venue_events_to_application(submission.events),
    }
}

impl<V: Venue> VenuePort for VenuePortAdapter<V> {
    fn venue_id(&self) -> &str {
        self.venue.id()
    }

    fn capabilities(&self) -> qx_zhenlu::venue_capabilities::VenueCapabilities {
        *self.venue.capabilities()
    }

    fn execution_readiness(&self) -> Result<(), String> {
        self.venue
            .execution_readiness()
            .map_err(|error| format!("Venue {} 尚未就绪: {error:?}", self.venue_id()))
    }

    fn validate_submit_order_group(
        &mut self,
        group: &qx_core::OrderGroupState,
    ) -> Result<(), String> {
        self.venue
            .validate_order_group(group)
            .map_err(|error| format!("Venue 订单组预检失败: {error:?}"))
    }

    fn submit_order_group(
        &mut self,
        group: &qx_core::OrderGroupState,
        ts: u64,
    ) -> Result<OrderGroupSubmission, String> {
        self.venue
            .submit_order_group(group, ts)
            .map(venue_group_submission_to_application)
            .map_err(|error| format!("Venue 订单组提交结果未知: {error:?}"))
    }

    fn submit_order(
        &mut self,
        order: qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.validate_submit_capabilities(&order)?;
        self.venue
            .submit(order, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue submit 失败: {error:?}"))
    }

    fn cancel_order(
        &mut self,
        client_order_id: u64,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.capabilities().validate_cancel()?;
        self.venue
            .cancel(client_order_id, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue cancel 失败: {error:?}"))
    }

    fn validate_amend_order(
        &mut self,
        current: &qx_core::Order,
        amended: &qx_core::Order,
    ) -> Result<(), String> {
        self.venue
            .validate_amendment(current, amended)
            .map_err(|error| format!("Venue amend preflight 失败: {error:?}"))
    }

    fn amend_order(
        &mut self,
        current: &qx_core::Order,
        amended: &qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.venue
            .amend(current, amended, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue amend 结果未知: {error:?}"))
    }
}

impl<V: Venue + ?Sized> VenuePort for BorrowedVenuePort<'_, V> {
    fn venue_id(&self) -> &str {
        self.venue.id()
    }

    fn capabilities(&self) -> qx_zhenlu::venue_capabilities::VenueCapabilities {
        *self.venue.capabilities()
    }

    fn execution_readiness(&self) -> Result<(), String> {
        self.venue
            .execution_readiness()
            .map_err(|error| format!("Venue {} 尚未就绪: {error:?}", self.venue_id()))
    }

    fn validate_submit_order_group(
        &mut self,
        group: &qx_core::OrderGroupState,
    ) -> Result<(), String> {
        self.venue
            .validate_order_group(group)
            .map_err(|error| format!("Venue 订单组预检失败: {error:?}"))
    }

    fn submit_order_group(
        &mut self,
        group: &qx_core::OrderGroupState,
        ts: u64,
    ) -> Result<OrderGroupSubmission, String> {
        self.venue
            .submit_order_group(group, ts)
            .map(venue_group_submission_to_application)
            .map_err(|error| format!("Venue 订单组提交结果未知: {error:?}"))
    }

    fn submit_order(
        &mut self,
        order: qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.validate_submit_capabilities(&order)?;
        self.venue
            .submit(order, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue submit 失败: {error:?}"))
    }

    fn cancel_order(
        &mut self,
        client_order_id: u64,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.capabilities().validate_cancel()?;
        self.venue
            .cancel(client_order_id, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue cancel 失败: {error:?}"))
    }

    fn validate_amend_order(
        &mut self,
        current: &qx_core::Order,
        amended: &qx_core::Order,
    ) -> Result<(), String> {
        self.venue
            .validate_amendment(current, amended)
            .map_err(|error| format!("Venue amend preflight 失败: {error:?}"))
    }

    fn amend_order(
        &mut self,
        current: &qx_core::Order,
        amended: &qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        self.venue
            .amend(current, amended, ts)
            .map(venue_events_to_application)
            .map_err(|error| format!("Venue amend 结果未知: {error:?}"))
    }
}

impl<V: Venue + ?Sized> VenueRouterPort for BorrowedVenuePort<'_, V> {
    fn capabilities_for(
        &self,
        venue_id: &str,
    ) -> Option<qx_zhenlu::venue_capabilities::VenueCapabilities> {
        (self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id()))
        .then(|| self.capabilities())
    }

    fn execution_readiness_for(&self, venue_id: &str) -> Result<(), String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::execution_readiness(self)
        } else {
            Err(format!(
                "Venue readiness 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }

    fn submit_order(
        &mut self,
        venue_id: &str,
        order: qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::submit_order(self, order, ts)
        } else {
            Err(format!(
                "Venue 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }

    fn cancel_order(
        &mut self,
        venue_id: &str,
        client_order_id: u64,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::cancel_order(self, client_order_id, ts)
        } else {
            Err(format!(
                "Venue 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }
}

impl<V: Venue> VenueRouterPort for VenuePortAdapter<V> {
    fn capabilities_for(
        &self,
        venue_id: &str,
    ) -> Option<qx_zhenlu::venue_capabilities::VenueCapabilities> {
        (self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id()))
        .then(|| self.capabilities())
    }

    fn execution_readiness_for(&self, venue_id: &str) -> Result<(), String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::execution_readiness(self)
        } else {
            Err(format!(
                "Venue readiness 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }

    fn submit_order(
        &mut self,
        venue_id: &str,
        order: qx_core::Order,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::submit_order(self, order, ts)
        } else {
            Err(format!(
                "Venue 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }

    fn cancel_order(
        &mut self,
        venue_id: &str,
        client_order_id: u64,
        ts: u64,
    ) -> Result<Vec<ExecutionEvent>, String> {
        if self.accept_any_venue
            || venue_id.trim().is_empty()
            || venue_id.eq_ignore_ascii_case(self.venue_id())
        {
            VenuePort::cancel_order(self, client_order_id, ts)
        } else {
            Err(format!(
                "Venue 路由不匹配: requested={venue_id} configured={}",
                self.venue_id()
            ))
        }
    }
}

impl RiskPort for RiskContextPort<'_> {
    fn evaluate_order(&self, order: &qx_core::Order) -> Result<RiskVerdict, String> {
        let decision = self.risk.evaluate_order(order, self.position);
        if decision.allowed {
            Ok(RiskVerdict::Allow)
        } else {
            Ok(RiskVerdict::Reject {
                reason: format!(
                    "RiskContext {}: {}",
                    decision.rule_set_version,
                    decision.violations.join("; ")
                ),
            })
        }
    }
}

impl RiskPort for CanonicalRiskPort<'_> {
    fn evaluate_order(&self, order: &qx_core::Order) -> Result<RiskVerdict, String> {
        let decision = qx_risk::RiskEngine::evaluate_order(self.context, order);
        if decision.allowed {
            Ok(RiskVerdict::Allow)
        } else {
            Ok(RiskVerdict::Reject {
                reason: format!(
                    "CanonicalRiskEngine {}: {}",
                    decision.rule_set_version,
                    decision.violations.join("; ")
                ),
            })
        }
    }
}

/// 完全基于 Application Ports 的单腿执行器。
///
/// 这是回测、Paper、CCXT 及未来券商适配器共用的最小副作用边界:订单先
/// 注册到 `OrderStore`,再调用 `VenuePort`,最后把标准执行事实写入
/// `ExecutionEventPort`。它不依赖 `LiveEventPipeline` 或 `Venue`,便于在
/// 不同运行时、进程和语言绑定中复用同一套未知结果保护。
pub struct PortExecutionService<'a, V: VenuePort, P: ExecutionEventPort> {
    venue: &'a mut V,
    events: &'a mut P,
    worker_id: &'a str,
    now: u64,
    source_seq: &'a mut u64,
    registration_correlation: Option<String>,
    cancel_operation_id: Option<String>,
    instrument_spec: Option<TradingInstrumentSpec>,
    spread_store: Option<&'a dyn SpreadOrderGroupStore>,
    order_group_store: Option<&'a mut dyn OrderGroupStore>,
    submission_group_id: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PortExecutionResult {
    pub message: String,
    pub event_count: usize,
}

/// 统一执行网关名称。新运行时、Paper、回测和语言绑定应依赖这个端口化
/// 入口;保留 `PortExecutionService` 作为兼容的具体类型名,避免已有适配器
/// 在迁移期间被迫同时修改。
pub type ExecutionGateway<'a, V, P> = PortExecutionService<'a, V, P>;

impl<'a, V: VenuePort, P: ExecutionEventPort> PortExecutionService<'a, V, P> {
    pub fn new(
        venue: &'a mut V,
        events: &'a mut P,
        worker_id: &'a str,
        now: u64,
        source_seq: &'a mut u64,
    ) -> Self {
        Self {
            venue,
            events,
            worker_id,
            now,
            source_seq,
            registration_correlation: None,
            cancel_operation_id: None,
            instrument_spec: None,
            spread_store: None,
            order_group_store: None,
            submission_group_id: None,
        }
    }

    /// 绑定多腿订单组快照存储:只有注入组存储的提交路径才可能提交带
    /// `spread_group_id` 的腿,其余路径对这类命令一律 fail-closed(见
    /// [`spread_group_barrier`])。屏障判定住在网关而不是 CLI,是为了让任何
    /// 绕过 CLI 的调用方(回测、语言绑定、未来执行器)也无法跳过它。
    pub fn with_spread_group_store(mut self, store: &'a dyn SpreadOrderGroupStore) -> Self {
        self.spread_store = Some(store);
        self
    }

    pub fn with_order_group_store(mut self, store: &'a mut dyn OrderGroupStore) -> Self {
        self.order_group_store = Some(store);
        self
    }

    pub fn with_instrument_spec(mut self, spec: TradingInstrumentSpec) -> Self {
        self.instrument_spec = Some(spec);
        self
    }

    /// 统一的控制命令提交入口。命令解析、订单校验、风险(如调用方继续使用
    /// `submit_with_risk`)和 Venue 副作用均在同一个端口化服务中完成。
    pub fn submit_command(
        &mut self,
        command: &ControlCommand,
    ) -> Result<PortExecutionResult, String> {
        spread_group_barrier(self.spread_store, command)?;
        let order = order_from_submit_command(command)
            .map_err(|error| format!("SubmitOrder 订单载荷非法: {error:?}"))?;
        self.registration_correlation = Some(format!("control:{}", command.command_id));
        self.submission_group_id = command.payload.get("spread_group_id").cloned();
        let result = self.submit(order);
        self.registration_correlation = None;
        self.submission_group_id = None;
        result
    }

    pub fn submit_command_with_risk<R: RiskPort>(
        &mut self,
        command: &ControlCommand,
        risk: &R,
    ) -> Result<PortExecutionResult, String> {
        spread_group_barrier(self.spread_store, command)?;
        let order = order_from_submit_command(command)
            .map_err(|error| format!("SubmitOrder 订单载荷非法: {error:?}"))?;
        self.registration_correlation = Some(format!("control:{}", command.command_id));
        self.submission_group_id = command.payload.get("spread_group_id").cloned();
        let result = self.submit_with_risk(order, risk);
        self.registration_correlation = None;
        self.submission_group_id = None;
        result
    }

    pub fn submit(&mut self, order: qx_core::Order) -> Result<PortExecutionResult, String> {
        self.validate_order(&order)?;
        if self.events.orders().iter().any(|existing| {
            existing.account_id == order.account_id
                && self.events.has_pending_amendment(existing.client_id)
        }) {
            return Err("账户存在改单结果未知的订单,必须先对账,禁止新增订单".into());
        }
        self.venue.validate_submit_capabilities(&order)?;
        let existing_order = self
            .events
            .orders()
            .into_iter()
            .find(|existing| existing.client_id == order.client_id);
        if let Some(existing) = existing_order.as_ref() {
            if !existing.same_submission_identity(&order) {
                return Err(format!(
                    "client_order_id {} 已绑定到不同的账户、标的、方向、数量或执行策略,拒绝复用",
                    order.client_id
                ));
            }
            if existing.status.is_terminal()
                || matches!(
                    existing.status,
                    qx_core::OrderStatus::Accepted
                        | qx_core::OrderStatus::Working
                        | qx_core::OrderStatus::PartiallyFilled
                        | qx_core::OrderStatus::Filled
                )
            {
                return Ok(PortExecutionResult {
                    message: format!(
                        "ALREADY_APPLIED_FROM_EVENT_LOG status={:?}",
                        existing.status
                    ),
                    event_count: 0,
                });
            }
            // `CancelPending` 必须和 `Submitted`/`Unknown` 同一档:状态迁移表里
            // `CancelPending` 只能走到 `Cancelled`/`Filled`/`Unknown`,`Submitted` 非法,
            // 所以对一条撤单在途的订单补发 `venue.submit_order` 只会用同一个
            // `client_order_id` 再下一单,不是"重试"。
            // 正式的 LiveEventPipeline 会在网络副作用前把新订单持久化为 Submitted。
            // 其它 OrderStore 可能仍保留 PendingSubmit;该状态同样不能证明远端未收到,
            // 因而已有 PendingSubmit/Submitted 都必须先对账,不能把重试当作恢复。
            if matches!(
                existing.status,
                qx_core::OrderStatus::Submitted
                    | qx_core::OrderStatus::PendingSubmit
                    | qx_core::OrderStatus::CancelPending
                    | qx_core::OrderStatus::Unknown
            ) {
                self.append(
                    ExecutionEvent::ReconcileRequired {
                        client_order_id: existing.client_id,
                    },
                    format!("{}:ambiguous-submit:{}", self.worker_id, existing.client_id),
                )?;
                return Err(format!(
                    "订单 {} 状态为 {:?},先决条件不满足:已有远端事实但未确认,或撤单在途,必须先对账,禁止自动补单",
                    existing.client_id, existing.status
                ));
            }
        }
        if existing_order.is_none() {
            // A known disconnected/snapshotting venue is a safe local refusal. Check it before
            // creating a PendingSubmit record so callers can retry after the connector recovers.
            self.venue.execution_readiness()?;
            if let Some(group_id) = self.submission_group_id.as_deref() {
                let store = self.spread_store.ok_or_else(|| {
                    format!(
                        "FAIL_CLOSED: 多腿订单组 {group_id} 未绑定组状态存储,拒绝提交订单 {}",
                        order.client_id
                    )
                })?;
                store.begin_leg_submission(group_id, order.client_id)?;
            }
            self.events.register_order(
                order.clone(),
                self.now,
                Some(
                    self.registration_correlation.clone().unwrap_or_else(|| {
                        format!("{}:submit:{}", self.worker_id, order.client_id)
                    }),
                ),
            )?;
        }

        let registered = self
            .events
            .orders()
            .into_iter()
            .find(|existing| existing.client_id == order.client_id)
            .ok_or_else(|| "订单注册后无法读取订单".to_string())?;
        let facts = match self.venue.submit_order(registered, self.now) {
            Ok(facts) if facts.is_empty() => {
                self.mark_reconcile(order.client_id, "empty-venue-response")?;
                return Err("Venue 未返回任何订单事实,结果未知,必须先对账".into());
            }
            Ok(facts) => facts,
            Err(error) => {
                self.mark_reconcile(order.client_id, "submit-error")?;
                return Err(format!(
                    "Venue submit 失败/结果未知: {error};订单已标记为待对账"
                ));
            }
        };
        // 提交同步回包与用户流回报必须过同一道闸门(形状契约 + 产品规格精度),
        // 否则"越界回报只能转待对账"就只剩半条纪律:同一笔成交走被推下来的路径
        // 会被拦下、不记账,走提交回包却直接入账。
        if let Err(violation) = gate_submit_facts(&order, &facts, self.instrument_spec.as_ref()) {
            self.mark_reconcile(order.client_id, violation.reason_tag())?;
            return Err(violation.to_string());
        }
        let event_count = facts.len();
        let has_reconcile = facts
            .iter()
            .any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
        // 关联号必须含客户单号 **且** 含回报自身身份:EventLog 以
        // `correlation_id:source_seq` 去重,而每条命令都从 0 重新计数,只用
        // `(worker, venue)` 会让后一笔订单的 Accepted/ReconcileRequired 被判为
        // 重放而静默丢弃;只用客户单号又会让同一订单的第二笔成交被吞掉。
        let venue_prefix = match self
            .registration_correlation
            .as_deref()
            .filter(|correlation_id| !correlation_id.trim().is_empty())
        {
            Some(correlation_id) => format!(
                "{correlation_id}:venue:{}:{}",
                self.venue.venue_id(),
                order.client_id
            ),
            None => format!(
                "{}:venue:{}:{}",
                self.worker_id,
                self.venue.venue_id(),
                order.client_id
            ),
        };
        for fact in facts {
            let correlation_id = fact_correlation(&venue_prefix, &fact);
            self.append(fact, correlation_id)?;
        }
        if has_reconcile {
            return Err("Venue 返回待对账事实,禁止继续自动提交".into());
        }
        Ok(PortExecutionResult {
            message: format!("SUBMITTED venue_events={event_count}"),
            event_count,
        })
    }

    /// 风控前置的统一提交入口。RiskPort 拒绝发生在订单登记、Venue 副作用和
    /// EventLog 事实追加之前;因此拒单不会留下“已提交但未执行”的伪订单。两条通道
    /// 不混用(V13 §9.12 #172):端口给出拒绝才播报「拒绝订单」并带违规清单,端口
    /// 给不出裁决才播报「执行失败」。
    pub fn submit_with_risk<R: RiskPort>(
        &mut self,
        order: qx_core::Order,
        risk: &R,
    ) -> Result<PortExecutionResult, String> {
        self.validate_order(&order)?;
        match risk
            .evaluate_order(&order)
            .map_err(|error| format!("账户级 RiskPort 执行失败: {error}"))?
        {
            RiskVerdict::Allow => {}
            RiskVerdict::Reject { reason } => {
                return Err(format!("账户级 RiskPort 拒绝订单: {reason}"))
            }
        }
        self.submit(order)
    }

    /// Submit an OCO/Bracket exactly once through the shared execution boundary.
    /// Each leg intent is registered before the single native group RPC; any
    /// ambiguous outcome fences the group for reconciliation and is never retried.
    pub fn submit_order_group<R: RiskPort>(
        &mut self,
        intent: qx_core::OrderGroupIntent,
        risk: &R,
    ) -> Result<PortExecutionResult, String> {
        let group = qx_core::OrderGroupState::from_intent(intent)?;
        self.venue.validate_submit_order_group(&group)?;
        for leg in &group.legs {
            self.validate_order(&leg.order)?;
            match risk
                .evaluate_order(&leg.order)
                .map_err(|error| format!("订单组账户级风控执行失败: {error}"))?
            {
                RiskVerdict::Allow => {}
                RiskVerdict::Reject { reason } => {
                    return Err(format!(
                        "订单组 {:?} 子腿 {} 被账户级风控拒绝: {reason}",
                        leg.role, leg.order.client_id
                    ));
                }
            }
        }
        self.venue.execution_readiness()?;
        if self.order_group_store.is_none() {
            return Err("FAIL_CLOSED: 原生订单组提交路径未注入订单组持久化存储".into());
        }
        let existing_orders = self.events.orders();
        if let Some(existing) = group.legs.iter().find_map(|leg| {
            existing_orders
                .iter()
                .find(|order| order.client_id == leg.order.client_id)
        }) {
            return Err(format!(
                "订单组子腿 {} 已在 OMS 中,必须先恢复或对账,禁止重放组提交",
                existing.client_id
            ));
        }
        {
            let store = self
                .order_group_store
                .as_deref_mut()
                .ok_or_else(|| "订单组存储不可用".to_string())?;
            store.create(&group)?;
            if !store.begin_submission(&group.intent.group_id)? {
                return Err(format!(
                    "订单组 {} 已启动过提交或需对账,禁止重放 Venue 副作用",
                    group.intent.group_id
                ));
            }
        }
        for leg in &group.legs {
            if let Err(error) = self.events.register_order(
                leg.order.clone(),
                self.now,
                Some(format!(
                    "{}:group:{}:register:{}",
                    self.worker_id, group.intent.group_id, leg.order.client_id
                )),
            ) {
                if let Some(store) = self.order_group_store.as_deref_mut() {
                    let _ = store.require_reconciliation(&group.intent.group_id);
                }
                return Err(format!(
                    "订单组 {} 子腿 {} 注册失败,订单组已围栏待对账: {error}",
                    group.intent.group_id, leg.order.client_id
                ));
            }
        }
        {
            let orders = self.events.orders();
            let store = self
                .order_group_store
                .as_deref_mut()
                .ok_or_else(|| "订单组存储不可用".to_string())?;
            for leg in &group.legs {
                let snapshot = orders
                    .iter()
                    .find(|order| order.client_id == leg.order.client_id)
                    .ok_or_else(|| {
                        format!("已注册订单组子腿 {} 无法从 OMS 读取", leg.order.client_id)
                    })?;
                store.apply_order_snapshot(&group.intent.group_id, snapshot, None)?;
            }
        }

        let submission = match self.venue.submit_order_group(&group, self.now) {
            Ok(submission) => submission,
            Err(error) => {
                self.mark_group_reconcile(&group, "group-submit-unknown")?;
                return Err(format!("Venue 订单组提交结果未知,已围栏待对账: {error}"));
            }
        };
        if let Some(venue_group_id) = submission.venue_group_id {
            let result = self
                .order_group_store
                .as_deref_mut()
                .ok_or_else(|| "订单组存储不可用".to_string())?
                .record_venue_group_id(&group.intent.group_id, venue_group_id);
            if let Err(error) = result {
                self.mark_group_reconcile(&group, "venue-group-id-conflict")?;
                return Err(format!("交易所订单组 ID 无法持久化,已围栏待对账: {error}"));
            }
        }
        let group_status = submission.group_status;
        let facts = submission.events;
        if let Some(group_status) = group_status {
            if group_status == qx_core::OrderGroupStatus::Active && facts.is_empty() {
                self.order_group_store
                    .as_deref_mut()
                    .ok_or_else(|| "订单组存储不可用".to_string())?
                    .activate_native_group(&group.intent.group_id)?;
                return Ok(PortExecutionResult {
                    message: format!(
                        "ORDER_GROUP_ACTIVE group_id={} venue_resource=accepted_without_child_orders",
                        group.intent.group_id
                    ),
                    event_count: 0,
                });
            }
            self.mark_group_reconcile(&group, "native-group-state-requires-reconcile")?;
            return Err(format!(
                "Venue 原生订单组状态 {:?} 尚不能安全归约到子订单,已围栏待对账",
                group_status
            ));
        }
        let mut per_leg = Vec::with_capacity(group.legs.len());
        for leg in &group.legs {
            let leg_facts: Vec<_> = facts
                .iter()
                .filter(|fact| execution_event_client_order_id(fact) == Some(leg.order.client_id))
                .cloned()
                .collect();
            if let Err(error) = validate_submit_events(&leg.order, &leg_facts) {
                self.mark_group_reconcile(&group, "invalid-group-facts")?;
                return Err(format!(
                    "Venue 订单组子腿 {} 回报无效,订单组已围栏待对账: {error}",
                    leg.order.client_id
                ));
            }
            if let Err(error) =
                gate_submit_facts(&leg.order, &leg_facts, self.instrument_spec.as_ref())
            {
                self.mark_group_reconcile(&group, error.reason_tag())?;
                return Err(error.to_string());
            }
            per_leg.push((leg.order.client_id, leg_facts));
        }
        if facts
            .iter()
            .any(|fact| execution_event_client_order_id(fact).is_none())
        {
            self.mark_group_reconcile(&group, "unattributed-group-fact")?;
            return Err("Venue 订单组返回无法归属子腿的事实,已围栏待对账".into());
        }

        let event_count = facts.len();
        let mut has_reconcile = false;
        for (client_order_id, leg_facts) in per_leg {
            let venue_prefix = format!(
                "{}:venue:{}:{}:group:{}",
                self.worker_id,
                self.venue.venue_id(),
                client_order_id,
                group.intent.group_id
            );
            for fact in leg_facts {
                has_reconcile |= matches!(fact, ExecutionEvent::ReconcileRequired { .. });
                let correlation_id = fact_correlation(&venue_prefix, &fact);
                self.append(fact, correlation_id)?;
            }
        }
        let orders = self.events.orders();
        {
            let store = self
                .order_group_store
                .as_deref_mut()
                .ok_or_else(|| "订单组存储不可用".to_string())?;
            if has_reconcile {
                store.require_reconciliation(&group.intent.group_id)?;
            }
            for leg in &group.legs {
                let snapshot = orders
                    .iter()
                    .find(|order| order.client_id == leg.order.client_id)
                    .ok_or_else(|| {
                        format!("订单组子腿 {} 回报后无法从 OMS 读取", leg.order.client_id)
                    })?;
                let venue_order_id = facts.iter().find_map(|fact| match fact {
                    ExecutionEvent::Accepted {
                        client_order_id,
                        venue_order_id,
                    } if *client_order_id == leg.order.client_id => Some(venue_order_id.clone()),
                    _ => None,
                });
                if has_reconcile {
                    store.record_order_snapshot_during_reconciliation(
                        &group.intent.group_id,
                        snapshot,
                        venue_order_id,
                    )?;
                } else if store.load(&group.intent.group_id)?.is_some_and(|current| {
                    current.status == qx_core::OrderGroupStatus::ReconcileRequired
                }) {
                    // A complete response can transiently look unresolved while its legs are
                    // reduced one at a time (for example Rejected before peer Accepted).
                    // Reapply each known venue fact through the barrier-aware reducer so the
                    // final group state reflects the whole response instead of a leg-order race.
                    store.reconcile_order_snapshot(
                        &group.intent.group_id,
                        snapshot,
                        venue_order_id,
                    )?;
                } else {
                    store.apply_order_snapshot(&group.intent.group_id, snapshot, venue_order_id)?;
                }
            }
        }
        if has_reconcile {
            return Err("Venue 订单组回报要求对账,已禁止继续自动执行".into());
        }
        Ok(PortExecutionResult {
            message: format!(
                "ORDER_GROUP_SUBMITTED group_id={} venue_events={event_count}",
                group.intent.group_id
            ),
            event_count,
        })
    }

    fn mark_group_reconcile(
        &mut self,
        group: &qx_core::OrderGroupState,
        reason: &str,
    ) -> Result<(), String> {
        for leg in &group.legs {
            self.mark_reconcile(leg.order.client_id, reason)?;
        }
        let orders = self.events.orders();
        let store = self
            .order_group_store
            .as_deref_mut()
            .ok_or_else(|| "订单组存储不可用".to_string())?;
        store.require_reconciliation(&group.intent.group_id)?;
        for leg in &group.legs {
            let snapshot = orders
                .iter()
                .find(|order| order.client_id == leg.order.client_id)
                .ok_or_else(|| {
                    format!("待对账订单组子腿 {} 无法从 OMS 读取", leg.order.client_id)
                })?;
            store.record_order_snapshot_during_reconciliation(
                &group.intent.group_id,
                snapshot,
                None,
            )?;
        }
        Ok(())
    }

    /// Fail closed on frozen market-spec identity, lot size, minimum quantity,
    /// and price tick before risk registration or any venue side effect.
    pub fn validate_order(&self, order: &qx_core::Order) -> Result<(), String> {
        order
            .validate()
            .map_err(|error| format!("订单校验失败: {error}"))?;
        if let Some(spec) = &self.instrument_spec {
            if order.instrument != spec.instrument {
                return Err(format!(
                    "订单 market spec 标的与订单不匹配: order={} spec={}",
                    order.instrument, spec.instrument
                ));
            }
            if let Some(amounts) = order.exact_amounts {
                spec.validate_exact_order_precision(amounts.quantity, amounts.limit_price)
                    .map_err(|error| format!("精确订单不满足 market spec: {error}"))?;
            } else {
                spec.validate_order(order.qty.raw(), order.limit.map(|price| price.raw()))
                    .map_err(|error| format!("订单不满足 market spec: {error}"))?;
            }
        }
        Ok(())
    }

    /// Submit with a stable caller correlation id. Algorithm children use
    /// `parent_id/child_index` so an event-log replay can identify the same
    /// child after restart without inventing a second execution path.
    pub fn submit_with_risk_and_correlation<R: RiskPort>(
        &mut self,
        order: qx_core::Order,
        risk: &R,
        correlation_id: impl Into<String>,
    ) -> Result<PortExecutionResult, String> {
        self.registration_correlation = Some(correlation_id.into());
        let result = self.submit_with_risk(order, risk);
        self.registration_correlation = None;
        result
    }

    /// Read the authoritative OMS snapshot for a client order id.
    pub fn order(&self, client_order_id: u64) -> Option<qx_core::Order> {
        self.events
            .orders()
            .into_iter()
            .find(|order| order.client_id == client_order_id)
    }

    pub fn cancel(&mut self, client_order_id: u64) -> Result<PortExecutionResult, String> {
        self.venue.capabilities().validate_cancel()?;
        if self.events.has_pending_amendment(client_order_id) {
            return Err(format!(
                "订单 {client_order_id} 存在改单结果未知的操作,必须先对账后才能撤单"
            ));
        }
        let order = self
            .order(client_order_id)
            .ok_or_else(|| format!("订单不存在: {client_order_id}"))?;
        match order.status {
            qx_core::OrderStatus::Cancelled => {
                return Ok(PortExecutionResult {
                    message: "ALREADY_CANCELLED".into(),
                    event_count: 0,
                });
            }
            qx_core::OrderStatus::Filled
            | qx_core::OrderStatus::Rejected
            | qx_core::OrderStatus::Expired => {
                return Err(format!(
                    "订单 {} 已处于终态 {:?},不能撤销",
                    client_order_id, order.status
                ));
            }
            qx_core::OrderStatus::PendingSubmit => {
                return Err(format!(
                    "订单 {} 尚未提交到交易所,不能撤销",
                    client_order_id
                ));
            }
            qx_core::OrderStatus::CancelPending => {
                return Err(format!("订单 {} 已有撤单在途,必须先对账", client_order_id));
            }
            qx_core::OrderStatus::Unknown => {
                return Err(format!(
                    "订单 {} 状态未知,必须先对账后才能撤单",
                    client_order_id
                ));
            }
            qx_core::OrderStatus::Submitted
            | qx_core::OrderStatus::Accepted
            | qx_core::OrderStatus::Working
            | qx_core::OrderStatus::PartiallyFilled => {}
        }
        self.venue.execution_readiness()?;
        let operation_id = self.cancel_operation_id.clone().unwrap_or_else(|| {
            format!("{}:cancel:{}:{}", self.worker_id, client_order_id, self.now)
        });
        let append_outcome = self.append_with_outcome(
            ExecutionEvent::CancelPending { client_order_id },
            format!("{operation_id}:pending:{client_order_id}"),
        )?;
        if append_outcome == EventAppendOutcome::Duplicate {
            return Err(format!(
                "订单 {} 的撤单意图已由同一操作持久化,禁止重复调用 Venue;必须先对账",
                client_order_id
            ));
        }
        let facts = match self.venue.cancel_order(client_order_id, self.now) {
            Ok(facts) if facts.is_empty() => {
                self.mark_reconcile_for_operation(
                    client_order_id,
                    "empty-cancel-response",
                    &operation_id,
                )?;
                return Err("Venue 未返回取消事实,结果未知,必须先对账".into());
            }
            Ok(facts) => facts,
            Err(error) => {
                self.mark_reconcile_for_operation(client_order_id, "cancel-error", &operation_id)?;
                return Err(format!(
                    "Venue cancel 失败/结果未知: {error};订单已标记为待对账"
                ));
            }
        };
        if let Err(error) = validate_cancel_events(client_order_id, &facts) {
            self.mark_reconcile_for_operation(
                client_order_id,
                "invalid-cancel-response",
                &operation_id,
            )?;
            return Err(format!(
                "Venue 返回非法撤单事实,结果未知,必须先对账: {error}"
            ));
        }
        let event_count = facts.len();
        let has_cancelled = facts.iter().any(|fact| {
            matches!(
                fact,
                ExecutionEvent::Cancelled {
                    client_order_id: id
                } if *id == client_order_id
            )
        });
        let has_reconcile = facts
            .iter()
            .any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
        for fact in facts {
            let prefix = format!("{operation_id}:response:{client_order_id}");
            let correlation = fact_correlation(&prefix, &fact);
            self.append(fact, correlation)?;
        }
        if has_reconcile {
            return Err("Venue 返回撤单待对账事实,禁止重复撤单".into());
        }
        if !has_cancelled {
            return Err(format!(
                "订单 {client_order_id} 在撤单竞态中已全部成交,成交事实已入账"
            ));
        }
        Ok(PortExecutionResult {
            message: format!("CANCELLED venue_events={event_count}"),
            event_count,
        })
    }

    /// Apply an audited CancelOrder command only after binding its declared routing scope to the
    /// authoritative OMS order. The venue never receives an account or instrument supplied only
    /// by the caller.
    pub fn cancel_command(
        &mut self,
        command: &ControlCommand,
    ) -> Result<PortExecutionResult, String> {
        let target = cancel_order_target_from_command(command)
            .map_err(|error| format!("CancelOrder 命令无效: {error:?}"))?;
        let order = self
            .order(target.client_order_id)
            .ok_or_else(|| format!("订单不存在: {}", target.client_order_id))?;
        if order.account_id != target.account_id
            || !order
                .instrument
                .venue
                .as_str()
                .eq_ignore_ascii_case(&target.venue_id)
            || !self.venue.venue_id().eq_ignore_ascii_case(&target.venue_id)
        {
            return Err("CancelOrder 路由账户/交易所与 OMS 订单或执行 Venue 不一致".into());
        }
        self.cancel_operation_id = Some(format!(
            "control:cancel:{}:{}",
            command.command_id, target.client_order_id
        ));
        let result = self.cancel(target.client_order_id);
        self.cancel_operation_id = None;
        result
    }

    /// Apply a durable control-plane amendment. The adapter is preflighted before the
    /// pending fact is written; after that fact exists a retry is forbidden until a
    /// reconciler proves the exchange result.
    pub fn amend_command(
        &mut self,
        command: &ControlCommand,
    ) -> Result<PortExecutionResult, String> {
        let target = qx_control::amend_order_target_from_command(command)
            .map_err(|error| format!("AmendOrder 命令无效: {error:?}"))?;
        let current = self
            .order(target.client_order_id)
            .ok_or_else(|| format!("订单不存在: {}", target.client_order_id))?;
        if current.account_id != target.account_id
            || !current
                .instrument
                .venue
                .as_str()
                .eq_ignore_ascii_case(&target.venue_id)
            || !self.venue.venue_id().eq_ignore_ascii_case(&target.venue_id)
        {
            return Err("AmendOrder 路由账户/交易所与 OMS 订单或执行 Venue 不一致".into());
        }
        if self.events.has_pending_amendment(current.client_id) {
            return Err(format!(
                "订单 {} 存在改单结果未知的操作,禁止重发;必须先对账",
                current.client_id
            ));
        }
        if current.filled != target.order.filled
            || current.status != target.order.status
            || current.instrument != target.order.instrument
            || current.side != target.order.side
            || current.account_id != target.order.account_id
            || current.trace != target.order.trace
            || current.policy != target.order.policy
        {
            return Err("AmendOrder 请求基于过期或身份不一致的订单快照".into());
        }
        self.venue.validate_amend_order(&current, &target.order)?;
        self.events.refresh_order_state()?;
        let refreshed = self
            .order(target.client_order_id)
            .ok_or_else(|| format!("订单在改单预检期间从 OMS 消失: {}", target.client_order_id))?;
        if refreshed != current {
            return Err(format!(
                "订单 {} 在改单预检期间出现新成交或状态变化,拒绝基于旧快照改单",
                target.client_order_id
            ));
        }
        let operation_id = format!(
            "control:amend:{}:{}",
            command.command_id, target.client_order_id
        );
        let pending = self.append_with_outcome(
            ExecutionEvent::OrderAmendmentPending {
                operation_id: operation_id.clone(),
                order: Box::new(target.order.clone()),
            },
            format!("{operation_id}:pending:{}", target.client_order_id),
        )?;
        if pending == EventAppendOutcome::Duplicate {
            return Err(format!(
                "订单 {} 的改单意图已持久化,禁止重复触达 Venue;必须先对账",
                target.client_order_id
            ));
        }
        let facts = match self.venue.amend_order(&current, &target.order, self.now) {
            Ok(facts) => facts,
            Err(error) => {
                self.mark_reconcile_for_operation(
                    target.client_order_id,
                    "amend-error",
                    &operation_id,
                )?;
                return Err(format!(
                    "Venue amend 失败/结果未知: {error};订单已标记为待对账"
                ));
            }
        };
        if facts.iter().any(|fact| match fact {
            ExecutionEvent::Fill(fill) => fill.order_id != target.client_order_id,
            ExecutionEvent::FillWithSpec { fill, .. } => fill.order_id != target.client_order_id,
            ExecutionEvent::FeeAdjustment { order_id, .. } => *order_id != target.client_order_id,
            ExecutionEvent::Cancelled { client_order_id }
            | ExecutionEvent::CancelPending { client_order_id }
            | ExecutionEvent::ReconcileRequired { client_order_id } => {
                *client_order_id != target.client_order_id
            }
            ExecutionEvent::Accepted { .. }
            | ExecutionEvent::Rejected { .. }
            | ExecutionEvent::OrderAmendmentPending { .. }
            | ExecutionEvent::OrderAmended { .. }
            | ExecutionEvent::MarketQuote { .. } => true,
        }) {
            self.mark_reconcile_for_operation(
                target.client_order_id,
                "invalid-amend-response",
                &operation_id,
            )?;
            return Err("Venue amend 返回了非法或其他订单的事实,结果未知,必须先对账".into());
        }

        // The adapter reports success only after an authoritative fetch confirms the
        // requested terms. Commit those terms before any concurrent fill facts so the
        // OMS reduces fills against the amended total quantity.
        self.append(
            ExecutionEvent::OrderAmended {
                operation_id: operation_id.clone(),
                order: Box::new(target.order.clone()),
            },
            format!("{operation_id}:confirmed:{}", target.client_order_id),
        )?;
        let fact_count = facts.len();
        let has_reconcile = facts
            .iter()
            .any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
        for fact in facts {
            let correlation = fact_correlation(
                &format!("{operation_id}:response:{}", target.client_order_id),
                &fact,
            );
            self.append(fact, correlation)?;
        }
        if has_reconcile {
            return Err("改单已确认但成交回报要求对账,账户进入待对账状态".into());
        }
        Ok(PortExecutionResult {
            message: format!("AMENDED venue_events={fact_count}"),
            event_count: fact_count + 1,
        })
    }

    fn mark_reconcile(&mut self, client_order_id: u64, reason: &str) -> Result<(), String> {
        self.append(
            ExecutionEvent::ReconcileRequired { client_order_id },
            format!("{}:{reason}:{client_order_id}", self.worker_id),
        )
    }

    fn mark_reconcile_for_operation(
        &mut self,
        client_order_id: u64,
        reason: &str,
        operation_id: &str,
    ) -> Result<(), String> {
        self.append(
            ExecutionEvent::ReconcileRequired { client_order_id },
            format!("{operation_id}:reconcile:{reason}:{client_order_id}"),
        )
    }

    fn append(&mut self, event: ExecutionEvent, correlation_id: String) -> Result<(), String> {
        self.append_with_outcome(event, correlation_id).map(|_| ())
    }

    fn append_with_outcome(
        &mut self,
        event: ExecutionEvent,
        correlation_id: String,
    ) -> Result<EventAppendOutcome, String> {
        *self.source_seq = self.source_seq.saturating_add(1);
        let event_ts = match &event {
            ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => fill.ts,
            _ => self.now,
        };
        let event = match (self.instrument_spec.as_ref(), event) {
            (Some(spec), ExecutionEvent::Fill(fill)) => ExecutionEvent::FillWithSpec {
                fill,
                spec: Box::new(spec.clone()),
            },
            (_, event) => event,
        };
        self.events
            .append_execution_event_with_outcome(ExecutionEventEnvelope {
                event,
                event_ts,
                receive_ts: self.now,
                source_seq: *self.source_seq,
                correlation_id,
            })
    }
}

/// Explicit recovery result for a spread group whose confirmed exposure needs
/// to be offset. The worker never submits a hedge for an unknown original
/// leg; that case remains `ReconcileRequired` and must be resolved by the
/// venue reconciliation flow first.
#[derive(Clone, Debug)]
pub struct HedgeRecoveryOutcome {
    pub group: SpreadOrderGroup,
    pub attempted: usize,
    pub completed: bool,
    pub errors: Vec<String>,
}

const HEDGE_RECOVERY_LEASE_MS: u64 = 30_000;

pub trait HedgeOrderValidator {
    fn validate(&self, order: &Order) -> Result<(), String>;

    /// 补偿提交必须与正常提交带着同一份冻结产品规格:这个 guard 就是运行时按
    /// `worker.instrument_spec_path` 解析出来的那个来源(见 qx-cli 的 `RecoveryGuard`),
    /// 所以补偿腿既不会被按乘数 1 记账,也不会绕过提交侧精度闸门。
    /// 返回 `Ok(None)` 只表示该 worker 没有配置规格(现货 `contract_size=1` 的既有口径),
    /// **解析失败**(文件缺失、币种与账簿不符等)必须返回 `Err`,由 worker 转待对账。
    fn instrument_spec(&self, _order: &Order) -> Result<Option<TradingInstrumentSpec>, String> {
        Ok(None)
    }
}

impl<F> HedgeOrderValidator for F
where
    F: Fn(&Order) -> Result<(), String>,
{
    fn validate(&self, order: &Order) -> Result<(), String> {
        self(order)
    }
}

/// Restart-safe single-node hedge recovery worker.
///
/// Compensation client ids are deterministic for `(group_id, leg_id)`, so a
/// retry after a process restart reuses the same order identity. The event
/// port remains the source of truth for the compensation order status; the
/// group snapshot only records that all confirmed exposure has been hedged.
pub struct HedgeRecoveryWorker<'a, R: VenueRouterPort, P: ExecutionEventPort> {
    router: &'a mut R,
    events: &'a mut P,
    store: &'a mut dyn SpreadOrderGroupStore,
    group: SpreadOrderGroup,
    worker_id: &'a str,
    claim_owner: String,
    now: u64,
    source_seq: &'a mut u64,
    account_venue_scope: Option<(Option<String>, Option<String>)>,
}

const MAX_HEDGE_ATTEMPTS: u32 = 3;

impl<'a, R: VenueRouterPort, P: ExecutionEventPort> HedgeRecoveryWorker<'a, R, P> {
    pub fn new(
        group: SpreadOrderGroup,
        store: &'a mut dyn SpreadOrderGroupStore,
        router: &'a mut R,
        events: &'a mut P,
        worker_id: &'a str,
        now: u64,
        source_seq: &'a mut u64,
    ) -> qx_core::QxResult<Self> {
        group.validate_persisted()?;
        Ok(Self {
            router,
            events,
            store,
            group,
            worker_id,
            claim_owner: format!("{}:{}:{}", worker_id, std::process::id(), now),
            now,
            source_seq,
            account_venue_scope: None,
        })
    }

    /// Scope a live recovery pass to the exact account and venue served by this worker.
    pub fn for_account_venue(mut self, account_id: &str, venue_id: &str) -> Result<Self, String> {
        let account_id = account_id.trim();
        let venue_id = venue_id.trim();
        if account_id.is_empty() || venue_id.is_empty() {
            return Err("实盘多腿恢复必须绑定 account_id 与 venue_id".into());
        }
        self.account_venue_scope = Some((
            Some(account_id.to_string()),
            Some(venue_id.to_ascii_uppercase()),
        ));
        Ok(self)
    }

    pub fn for_account(mut self, account_id: &str) -> Result<Self, String> {
        let account_id = account_id.trim();
        if account_id.is_empty() {
            return Err("实盘多腿恢复必须绑定 account_id".into());
        }
        self.account_venue_scope = Some((Some(account_id.to_string()), None));
        Ok(self)
    }

    fn load_group(&mut self) -> Result<(), String> {
        if let Some(saved) = self.store.load(&self.group.group_id)? {
            if saved.strategy_id != self.group.strategy_id
                || saved.legs.len() != self.group.legs.len()
                || saved
                    .legs
                    .iter()
                    .zip(&self.group.legs)
                    .any(|(left, right)| {
                        left.leg_id != right.leg_id
                            || left.venue_id != right.venue_id
                            || left.order.account_id != right.order.account_id
                            || left.order.instrument != right.order.instrument
                    })
            {
                return Err("Hedge 恢复快照与请求的策略、腿或 Venue 路由不一致".into());
            }
            self.group = saved;
        }
        Ok(())
    }

    /// Execute all confirmed compensation targets once, then persist Hedged
    /// only when the event port confirms every deterministic hedge order is
    /// Filled. Active orders are left pending and are never submitted twice.
    pub fn execute(self) -> Result<HedgeRecoveryOutcome, String> {
        self.execute_with_validator(None)
    }

    /// 在实际补偿单提交前注入账户级风控校验。恢复 worker 本身不依赖
    /// `qx-risk` 的具体快照来源,由运行时按当前 Venue/账户提供校验器;
    /// 未提供时仍保留库级 `Order::validate` 和 reduce-only 约束。
    pub fn execute_with_validator(
        mut self,
        validator: Option<&dyn HedgeOrderValidator>,
    ) -> Result<HedgeRecoveryOutcome, String> {
        self.load_group()?;
        match self.group.status {
            SpreadOrderGroupStatus::HedgeRequired => {}
            SpreadOrderGroupStatus::Hedged => {
                return Ok(HedgeRecoveryOutcome {
                    group: self.group,
                    attempted: 0,
                    completed: true,
                    errors: Vec::new(),
                });
            }
            SpreadOrderGroupStatus::ReconcileRequired => {
                return Ok(HedgeRecoveryOutcome {
                    group: self.group,
                    attempted: 0,
                    completed: false,
                    errors: vec![
                        "原始多腿订单组存在未知 Venue 状态,必须先完成对账,禁止自动对冲".into(),
                    ],
                });
            }
            status => {
                return Ok(HedgeRecoveryOutcome {
                    group: self.group,
                    attempted: 0,
                    completed: false,
                    errors: vec![format!(
                        "订单组当前为 {status:?},只有 HedgeRequired 才能执行恢复"
                    )],
                });
            }
        }

        let mut targets = self.group.compensation_targets();
        if let Some((account_id, venue_id)) = &self.account_venue_scope {
            targets.retain(|target| {
                account_id
                    .as_deref()
                    .is_none_or(|account_id| target.account_id == account_id)
                    && venue_id
                        .as_deref()
                        .is_none_or(|venue_id| target.venue_id.eq_ignore_ascii_case(venue_id))
            });
        }
        if targets.is_empty() {
            // A crash can happen after the final compensation fill and progress
            // snapshot are durable, but before the group status is changed to
            // Hedged. On restart there are no outstanding targets, so finish
            // that state transition under the same group claim used by normal
            // recovery. An account-scoped worker must still verify the whole
            // group before it can close the group-wide state.
            if self.group.all_confirmed_exposure_hedged() {
                let Some(claim_token) = self.store.try_claim(
                    &self.group.group_id,
                    &self.claim_owner,
                    self.now,
                    HEDGE_RECOVERY_LEASE_MS,
                )?
                else {
                    return Ok(HedgeRecoveryOutcome {
                        group: self.group,
                        attempted: 0,
                        completed: false,
                        errors: vec!["订单组正在由其他恢复 owner 处理,跳过本轮".into()],
                    });
                };
                if !self.store.verify_claim(
                    &self.group.group_id,
                    &self.claim_owner,
                    claim_token,
                    self.now,
                )? {
                    return Ok(HedgeRecoveryOutcome {
                        group: self.group,
                        attempted: 0,
                        completed: false,
                        errors: vec!["多腿恢复 claim 已过期或已被其他 owner 接管".into()],
                    });
                }
                self.group
                    .mark_hedged()
                    .map_err(|error| format!("标记多腿订单组已对冲失败: {error:?}"))?;
                self.store
                    .save_claimed(&self.group, &self.claim_owner, claim_token, self.now)?;
                self.store
                    .release_claim(&self.group.group_id, &self.claim_owner, claim_token)?;
                return Ok(HedgeRecoveryOutcome {
                    group: self.group,
                    attempted: 0,
                    completed: true,
                    errors: Vec::new(),
                });
            }
            return Ok(HedgeRecoveryOutcome {
                group: self.group,
                attempted: 0,
                completed: false,
                errors: Vec::new(),
            });
        }
        let Some(claim_token) = self.store.try_claim(
            &self.group.group_id,
            &self.claim_owner,
            self.now,
            HEDGE_RECOVERY_LEASE_MS,
        )?
        else {
            return Ok(HedgeRecoveryOutcome {
                group: self.group,
                attempted: 0,
                completed: false,
                errors: vec!["订单组正在由其他恢复 owner 处理,跳过本轮".into()],
            });
        };
        let mut attempted = 0;
        let mut errors = Vec::new();
        let mut all_filled = true;
        for target in targets {
            if !self.store.verify_claim(
                &self.group.group_id,
                &self.claim_owner,
                claim_token,
                self.now,
            )? {
                errors.push("多腿恢复 claim 已过期或已被其他 owner 接管".into());
                all_filled = false;
                break;
            }
            let source_leg = self
                .group
                .leg(&target.leg_id)
                .map_err(|error| format!("读取补偿来源腿失败: {error:?}"))?;
            if source_leg.venue_id.trim().is_empty() {
                errors.push(format!(
                    "补偿腿 {} 缺少 Venue 路由,拒绝使用默认交易所",
                    target.leg_id
                ));
                all_filled = false;
                continue;
            }
            let client_id = compensation_client_id(&self.group, &target.leg_id, target.attempt);
            let existing = self
                .events
                .orders()
                .into_iter()
                .find(|order| order.client_id == client_id);
            let order = build_compensation_order(&self.group, source_leg, &target, client_id)?;
            if let Some(existing) = existing {
                if !same_compensation_order(&existing, &order) {
                    errors.push(format!(
                        "补偿 client_order_id={} 已被其他订单占用,拒绝覆盖",
                        client_id
                    ));
                    self.group.require_reconciliation();
                    all_filled = false;
                    continue;
                }
                if existing.status == OrderStatus::Filled {
                    if !existing.filled_decimal().is_positive() {
                        errors.push(format!(
                            "补偿订单 {} 报告 Filled 但成交数量为零,必须对账",
                            client_id
                        ));
                        self.group.require_reconciliation();
                        all_filled = false;
                        continue;
                    }
                    self.group
                        .record_compensation_fill_exact(&target.leg_id, existing.filled_decimal())
                        .map_err(|error| format!("恢复补偿成交进度失败: {error:?}"))?;
                    if existing
                        .filled_decimal()
                        .checked_cmp(target.quantity_decimal())
                        .is_some_and(|ordering| ordering.is_lt())
                    {
                        let next_attempt = self
                            .group
                            .advance_compensation_attempt(&target.leg_id)
                            .map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
                        if next_attempt >= MAX_HEDGE_ATTEMPTS {
                            self.group.require_reconciliation();
                            errors.push(format!(
                                "补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
                                target.leg_id, MAX_HEDGE_ATTEMPTS
                            ));
                        }
                    }
                    continue;
                }
                if matches!(
                    existing.status,
                    OrderStatus::PendingSubmit
                        | OrderStatus::Submitted
                        | OrderStatus::Accepted
                        | OrderStatus::Working
                        | OrderStatus::PartiallyFilled
                        | OrderStatus::CancelPending
                ) {
                    all_filled = false;
                    continue;
                }
                if matches!(
                    existing.status,
                    OrderStatus::Cancelled | OrderStatus::Expired
                ) {
                    if existing.filled_decimal().is_positive() {
                        self.group
                            .record_compensation_fill_exact(
                                &target.leg_id,
                                existing.filled_decimal(),
                            )
                            .map_err(|error| format!("归约终态补偿部分成交失败: {error:?}"))?;
                    }
                    let next_attempt = self
                        .group
                        .advance_compensation_attempt(&target.leg_id)
                        .map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
                    if next_attempt >= MAX_HEDGE_ATTEMPTS {
                        self.group.require_reconciliation();
                        errors.push(format!(
                            "补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
                            target.leg_id, MAX_HEDGE_ATTEMPTS
                        ));
                    }
                    errors.push(format!(
                        "补偿订单 {} 已终止于 {:?},剩余敞口将在下一恢复轮处理",
                        client_id, existing.status
                    ));
                    all_filled = false;
                    continue;
                }
                errors.push(format!(
                    "补偿订单 {} 状态 {:?} 不能安全重试,必须对账",
                    client_id, existing.status
                ));
                self.group.require_reconciliation();
                all_filled = false;
                continue;
            }

            let mut leg_spec: Option<TradingInstrumentSpec> = None;
            if let Some(validator) = validator {
                if let Err(error) = validator.validate(&order) {
                    errors.push(format!(
                        "补偿腿 {} 未通过账户级风控,拒绝自动对冲: {error}",
                        target.leg_id
                    ));
                    all_filled = false;
                    continue;
                }
                // 规格解析必须先于登记与提交:没有冻结规格就既过不了精度闸门,也
                // 会让衍生腿被按乘数 1 记账。解析失败是配置问题而不是未知结果,
                // 所以留组于 HedgeRequired 等下一轮,而不是留下一条假的待对账事实。
                match validator.instrument_spec(&order) {
                    Ok(spec) => leg_spec = spec,
                    Err(error) => {
                        errors.push(format!(
                            "补偿腿 {} 未解析到产品规格,拒绝自动对冲: {error}",
                            target.leg_id
                        ));
                        all_filled = false;
                        continue;
                    }
                }
            }
            let Some(capabilities) = self.router.capabilities_for(&source_leg.venue_id) else {
                errors.push(format!(
                    "补偿腿 {} 的 Venue {} 未声明能力或路由不存在,拒绝注册",
                    target.leg_id, source_leg.venue_id
                ));
                all_filled = false;
                continue;
            };
            if let Err(error) = capabilities.validate_submit(&order, &source_leg.venue_id) {
                errors.push(format!("补偿腿 {} 能力校验失败: {error}", target.leg_id));
                all_filled = false;
                continue;
            }
            if let Err(error) = self.router.execution_readiness_for(&source_leg.venue_id) {
                errors.push(format!("补偿腿 {} Venue 尚未就绪: {error}", target.leg_id));
                all_filled = false;
                continue;
            }
            self.events
                .register_order(
                    order.clone(),
                    self.now,
                    Some(format!(
                        "{}:hedge:{}:{}:register",
                        self.worker_id, self.group.group_id, target.leg_id
                    )),
                )
                .map_err(|error| format!("注册补偿订单 {} 失败: {error}", client_id))?;
            attempted += 1;
            let facts =
                match self
                    .router
                    .submit_order(&source_leg.venue_id, order.clone(), self.now)
                {
                    Ok(facts) if facts.is_empty() => {
                        errors.push(format!(
                            "补偿腿 {} 未返回事实,结果未知,必须对账",
                            target.leg_id
                        ));
                        self.group.require_reconciliation();
                        all_filled = false;
                        continue;
                    }
                    Ok(facts) => facts,
                    Err(error) => {
                        errors.push(format!(
                            "补偿腿 {} 提交失败/结果未知: {error}",
                            target.leg_id
                        ));
                        self.group.require_reconciliation();
                        all_filled = false;
                        continue;
                    }
                };
            // 补偿提交也是提交:回包必须过与 `PortExecutionService::submit` 同一道
            // 闸门。这里绕过闸门,"越界回报只能转待对账"就只剩半条纪律。
            if let Err(violation) = gate_submit_facts(&order, &facts, leg_spec.as_ref()) {
                errors.push(format!("补偿腿 {} {violation}", target.leg_id));
                self.append_fact(
                    ExecutionEvent::ReconcileRequired {
                        client_order_id: client_id,
                    },
                    format!(
                        "{}:hedge:{}:{}:{}",
                        self.worker_id,
                        self.group.group_id,
                        target.leg_id,
                        violation.reason_tag()
                    ),
                    None,
                )?;
                self.group.require_reconciliation();
                all_filled = false;
                continue;
            }
            let hedge_prefix = format!(
                "{}:hedge:{}:{}",
                self.worker_id, self.group.group_id, target.leg_id
            );
            for fact in facts {
                let correlation_id = fact_correlation(&hedge_prefix, &fact);
                self.append_fact(fact, correlation_id, leg_spec.as_ref())?;
            }
            let persisted_hedge = self
                .events
                .orders()
                .into_iter()
                .find(|candidate| candidate.client_id == client_id);
            match persisted_hedge {
                Some(hedge)
                    if hedge.status == OrderStatus::Filled
                        && hedge.filled_decimal().is_positive() =>
                {
                    self.group
                        .record_compensation_fill_exact(&target.leg_id, hedge.filled_decimal())
                        .map_err(|error| format!("归约补偿成交进度失败: {error:?}"))?;
                    if hedge
                        .filled_decimal()
                        .checked_cmp(target.quantity_decimal())
                        .is_some_and(|ordering| ordering.is_lt())
                    {
                        let next_attempt = self
                            .group
                            .advance_compensation_attempt(&target.leg_id)
                            .map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
                        if next_attempt >= MAX_HEDGE_ATTEMPTS {
                            self.group.require_reconciliation();
                            errors.push(format!(
                                "补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
                                target.leg_id, MAX_HEDGE_ATTEMPTS
                            ));
                        }
                    }
                }
                Some(hedge)
                    if matches!(hedge.status, OrderStatus::Cancelled | OrderStatus::Expired) =>
                {
                    if hedge.filled_decimal().is_positive() {
                        self.group
                            .record_compensation_fill_exact(&target.leg_id, hedge.filled_decimal())
                            .map_err(|error| format!("归约终态补偿部分成交失败: {error:?}"))?;
                    }
                    let next_attempt = self
                        .group
                        .advance_compensation_attempt(&target.leg_id)
                        .map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
                    if next_attempt >= MAX_HEDGE_ATTEMPTS {
                        self.group.require_reconciliation();
                        errors.push(format!(
                            "补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
                            target.leg_id, MAX_HEDGE_ATTEMPTS
                        ));
                    }
                    all_filled = false;
                }
                Some(hedge)
                    if matches!(hedge.status, OrderStatus::Rejected | OrderStatus::Unknown) =>
                {
                    self.group.require_reconciliation();
                    errors.push(format!(
                        "补偿订单 {} 进入 {:?},必须人工对账",
                        client_id, hedge.status
                    ));
                    all_filled = false;
                }
                _ => all_filled = false,
            }
        }

        if all_filled
            && errors.is_empty()
            && self.group.all_confirmed_exposure_hedged()
            && self.group.status == SpreadOrderGroupStatus::HedgeRequired
        {
            self.group
                .mark_hedged()
                .map_err(|error| format!("标记多腿订单组已对冲失败: {error:?}"))?;
        }
        self.store
            .save_claimed(&self.group, &self.claim_owner, claim_token, self.now)?;
        self.store
            .release_claim(&self.group.group_id, &self.claim_owner, claim_token)?;
        let completed =
            self.group.status == SpreadOrderGroupStatus::Hedged && all_filled && errors.is_empty();
        Ok(HedgeRecoveryOutcome {
            group: self.group,
            attempted,
            completed,
            errors,
        })
    }

    /// 追加一条补偿路径事实。`spec` 与 `PortExecutionService::append` 同源:成交必须
    /// 自带它那份冻结规格落库,否则账簿会退回 `LegacyMultiplier(1)`,衍生腿的仓位与
    /// 保证金按乘数 1 计算,对冲方向对、数量却差一个合约面值。
    fn append_fact(
        &mut self,
        event: ExecutionEvent,
        correlation_id: String,
        spec: Option<&TradingInstrumentSpec>,
    ) -> Result<(), String> {
        *self.source_seq = self.source_seq.saturating_add(1);
        let event_ts = match &event {
            ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => fill.ts,
            _ => self.now,
        };
        let event = match (spec, event) {
            (Some(spec), ExecutionEvent::Fill(fill)) => ExecutionEvent::FillWithSpec {
                fill,
                spec: Box::new(spec.clone()),
            },
            (_, event) => event,
        };
        self.events.append_execution_event(ExecutionEventEnvelope {
            event,
            event_ts,
            receive_ts: self.now,
            source_seq: *self.source_seq,
            correlation_id,
        })
    }
}

fn build_compensation_order(
    group: &SpreadOrderGroup,
    source_leg: &qx_zhenlu::SpreadOrderLeg,
    target: &qx_zhenlu::SpreadCompensationTarget,
    client_id: u64,
) -> Result<Order, String> {
    let mut policy = source_leg.order.policy.unwrap_or_default();
    policy.reduce_only = true;
    let exact_amounts = target.qty_exact.map(|quantity| qx_core::ExactOrderAmounts {
        quantity,
        limit_price: None,
        filled: qx_core::ExactDecimal::ZERO,
        quote_budget: None,
        quote_filled: qx_core::ExactDecimal::ZERO,
        market_reference_price: source_leg.order.market_reference_price_decimal(),
        conditional: None,
    });
    let order = Order {
        client_id,
        instrument: target.instrument.clone(),
        side: target.side,
        qty: if exact_amounts.is_some() {
            qx_core::Quantity::ZERO
        } else {
            target.qty
        },
        limit: None,
        status: OrderStatus::PendingSubmit,
        filled: Quantity::ZERO,
        account_id: target.account_id.clone(),
        trace: Some(OrderTrace {
            run_id: None,
            strategy_id: Some(group.strategy_id.clone()),
            signal_id: None,
            intent_id: None,
            rule_version: Some("spread-hedge-v1".into()),
        }),
        policy: Some(policy),
        exact_amounts,
    };
    order.validate()?;
    Ok(order)
}

fn same_compensation_order(left: &Order, right: &Order) -> bool {
    left.same_submission_identity(right)
}

fn compensation_client_id(group: &SpreadOrderGroup, leg_id: &str, attempt: u32) -> u64 {
    let mut hash = 0xcbf29ce484222325_u64;
    let attempt_bytes = attempt.to_le_bytes();
    for byte in group
        .group_id
        .as_bytes()
        .iter()
        .chain([0_u8].iter())
        .chain(leg_id.as_bytes().iter())
        .chain([0_u8].iter())
        .chain(attempt_bytes.iter())
    {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x100000001b3_u64);
    }
    let mut candidate = hash | (1_u64 << 63);
    if candidate == 0 {
        candidate = 1_u64 << 63;
    }
    while group
        .legs
        .iter()
        .any(|leg| leg.order.client_id == candidate)
    {
        candidate = candidate.wrapping_add(1);
    }
    candidate
}

/// 提交**同步返回**的事实进入账本前的统一闸门:先形状契约,再产品规格精度。
///
/// 两处提交入口(`PortExecutionService::submit` 与对冲补偿 worker)必须共用它。
/// 各写一份就会重演"越界回报只能转待对账"只剩半条纪律:同一笔成交走用户流被
/// `ingest_venue_events_with_pipeline` 拦下、不记账,走提交回包却直接入账。
/// 规格为 `None` 时只过形状闸门 —— 没有冻结规格就没有可核对的 tick/step,
/// 与归约入口一样绝不凭空造一份。
enum SubmitFactViolation {
    /// 事实与订单不匹配(归属、数量、空回报、混入行情)。
    Shape(String),
    /// 成交落在冻结产品规格的 tick/step 之外。
    Precision(qx_core::QxError),
}

impl SubmitFactViolation {
    /// 写进 `ReconcileRequired` 关联号的原因段,保持两类未知结果可分别检索。
    fn reason_tag(&self) -> &'static str {
        match self {
            Self::Shape(_) => "invalid-submit-response",
            Self::Precision(_) => "submit-fill-out-of-spec",
        }
    }
}

impl std::fmt::Display for SubmitFactViolation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Shape(error) => {
                write!(f, "Venue 返回非法下单事实,结果未知,必须先对账: {error}")
            }
            Self::Precision(error) => write!(
                f,
                "Venue 提交返回的成交回报违反产品规格,结果未知,必须先对账: {error:?}"
            ),
        }
    }
}

fn gate_submit_facts(
    order: &Order,
    facts: &[ExecutionEvent],
    spec: Option<&TradingInstrumentSpec>,
) -> Result<(), SubmitFactViolation> {
    validate_submit_events(order, facts).map_err(SubmitFactViolation::Shape)?;
    // 规格优先取回报自带的那份:`append` 落库并交给账簿的就是它,校验另一份是假同源。
    let violation = facts.iter().find_map(|fact| {
        let (fill, embedded) = match fact {
            ExecutionEvent::Fill(fill) => (fill.as_ref(), None),
            ExecutionEvent::FillWithSpec { fill, spec } => (fill.as_ref(), Some(&**spec)),
            _ => return None,
        };
        let spec = embedded.or(spec)?;
        validate_fill_against_spec(fill, spec)
    });
    match violation {
        Some(error) => Err(SubmitFactViolation::Precision(error)),
        None => Ok(()),
    }
}

/// 成交事实的稳定身份段。
///
/// 身份必须取自成交内容本身,不能用进程内的 `source_seq`:一次性 Paper 命令
/// 每个进程都从 1 重新计数,同一订单的第二笔成交会撞上第一笔已落库的事实、被
/// EventLog 静默去重,订单停在 Accepted 而资金不动,调用方却收到 `fills=2`。
/// 字段选择与 `qx-runtime` 的 `fill_key` 同源,因此同一批回报重放仍然幂等。
fn fill_tag(fill: &qx_core::Fill) -> String {
    format!(
        "fill:{}:{}:{}:{}:{}:{}{}",
        fill.order_id,
        fill.ts,
        fill.qty.raw(),
        fill.price.raw(),
        fill.fee.raw(),
        fill.venue_order_id.clone().unwrap_or_default(),
        fill.exact_identity_suffix(),
    )
}

fn validate_fill_against_spec(
    fill: &qx_core::Fill,
    spec: &TradingInstrumentSpec,
) -> Option<qx_core::QxError> {
    match fill.exact_amounts {
        Some(amounts) => spec
            .validate_exact_fill(amounts.quantity, amounts.price)
            .err(),
        None => spec.validate_fill(fill.qty.raw(), fill.price.raw()).err(),
    }
}

/// 一条 Venue 回报在其关联号中的稳定身份段。
fn fact_tag(fact: &ExecutionEvent) -> String {
    match fact {
        ExecutionEvent::Accepted {
            client_order_id, ..
        } => format!("accepted:{client_order_id}"),
        ExecutionEvent::Rejected {
            client_order_id,
            reason,
        } => {
            format!("rejected:{client_order_id}:{reason}")
        }
        ExecutionEvent::CancelPending { client_order_id } => {
            format!("cancel-pending:{client_order_id}")
        }
        ExecutionEvent::OrderAmendmentPending {
            operation_id,
            order,
        } => format!("amend-pending:{}:{}", order.client_id, operation_id),
        ExecutionEvent::OrderAmended {
            operation_id,
            order,
        } => format!("amended:{}:{}", order.client_id, operation_id),
        ExecutionEvent::Cancelled { client_order_id } => format!("cancelled:{client_order_id}"),
        ExecutionEvent::FeeAdjustment {
            account_id,
            venue_id,
            order_id,
            instrument,
            fees,
        } => {
            let mut fees = fees.clone();
            fees.sort_by(|left, right| {
                left.currency
                    .as_deref()
                    .unwrap_or_default()
                    .cmp(right.currency.as_deref().unwrap_or_default())
                    .then_with(|| left.amount.raw().cmp(&right.amount.raw()))
            });
            let identity = fees
                .iter()
                .map(|fee| {
                    format!(
                        "{}={}#{}",
                        fee.currency.as_deref().unwrap_or_default(),
                        fee.amount.raw(),
                        {
                            let mut trade_ids = fee.source_trade_ids.clone();
                            trade_ids.sort();
                            trade_ids.join(";")
                        }
                    )
                })
                .collect::<Vec<_>>()
                .join(",");
            format!("fee-adjustment:{account_id}:{venue_id}:{order_id}:{instrument}:{identity}")
        }
        ExecutionEvent::ReconcileRequired { client_order_id } => {
            format!("reconcile:{client_order_id}")
        }
        ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => {
            fill_tag(fill.as_ref())
        }
        ExecutionEvent::MarketQuote { instrument, quote } => {
            format!("market:{instrument}:{}:{}", quote.source_seq, quote.ts)
        }
    }
}

fn execution_event_client_order_id(event: &ExecutionEvent) -> Option<u64> {
    match event {
        ExecutionEvent::Accepted {
            client_order_id, ..
        }
        | ExecutionEvent::Rejected {
            client_order_id, ..
        }
        | ExecutionEvent::CancelPending { client_order_id }
        | ExecutionEvent::Cancelled { client_order_id }
        | ExecutionEvent::ReconcileRequired { client_order_id } => Some(*client_order_id),
        ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => {
            Some(fill.order_id)
        }
        ExecutionEvent::FeeAdjustment { order_id, .. } => Some(*order_id),
        ExecutionEvent::OrderAmendmentPending { order, .. }
        | ExecutionEvent::OrderAmended { order, .. } => Some(order.client_id),
        ExecutionEvent::MarketQuote { .. } => None,
    }
}

/// 在调用方的来源前缀(worker/venue/group/leg)后追加回报身份。
///
/// `RuntimePipeline::ingest` 对 Accepted/Cancelled/ReconcileRequired/
/// FillWithSpec 按 correlation 去重——重启后 `source_seq` 会重新计数,只靠它
/// 挡不住重放。因此前缀必须唯一到"这一批回报属于哪条腿",再叠加事实自身的
/// 身份;否则第二个订单/第二条腿会撞上第一个已落库的事实而被静默丢弃。
fn fact_correlation(prefix: &str, fact: &ExecutionEvent) -> String {
    format!("{prefix}:{}", fact_tag(fact))
}

/// 将 Venue 返回的订单事实统一归约到执行事件端口(EventLog/Ledger 管线)。
///
/// `source_seq` 由调用方持有;重复回报仍由端口的幂等语义去重。Paper、Live 与
/// 任何自定义运行时共用这一条实现,不允许再按后端分叉。
pub fn ingest_venue_events<P: ExecutionEventPort>(
    pipeline: &mut P,
    events: impl IntoIterator<Item = VenueEvent>,
    worker_id: &str,
    receive_ts: u64,
    source_seq: &mut u64,
) -> Result<usize, String> {
    ingest_venue_events_with_pipeline(pipeline, events, worker_id, receive_ts, source_seq, None)
        .map_err(|error| format!("Venue 执行回报事实归约失败: {error}"))
}

/// 将含有冻结产品规格的 Venue 成交归约为衍生品记账事实。
///
/// 规格不会被写进 `Filled` 事件本身;完整的衍生品 LedgerEntry 会随同一批
/// `LedgerApplied` 事实写入 EventLog,因此重启后的 ReplayVerifier 仍可准确重建。
pub fn ingest_venue_events_with_spec<P: ExecutionEventPort>(
    pipeline: &mut P,
    events: impl IntoIterator<Item = VenueEvent>,
    worker_id: &str,
    receive_ts: u64,
    source_seq: &mut u64,
    spec: &TradingInstrumentSpec,
) -> Result<usize, String> {
    ingest_venue_events_with_pipeline(
        pipeline,
        events,
        worker_id,
        receive_ts,
        source_seq,
        Some(spec),
    )
    .map_err(|error| format!("带产品规格的 Venue 成交事实归约失败: {error}"))
}

fn ingest_venue_events_with_pipeline<P: ExecutionEventPort>(
    pipeline: &mut P,
    events: impl IntoIterator<Item = VenueEvent>,
    worker_id: &str,
    receive_ts: u64,
    source_seq: &mut u64,
    spec: Option<&TradingInstrumentSpec>,
) -> Result<usize, String> {
    let mut event_count = 0_usize;
    let mut rejections = Vec::new();
    for event in events {
        event_count += 1;
        *source_seq = source_seq.saturating_add(1);
        let (event, event_ts, correlation_id) = match event {
            VenueEvent::Accepted {
                client_order_id,
                venue_order_id,
                ts,
            } => (
                ExecutionEvent::Accepted {
                    client_order_id,
                    venue_order_id,
                },
                ts,
                format!("{worker_id}:accepted:{client_order_id}"),
            ),
            VenueEvent::Rejected {
                client_order_id,
                reason,
                ts,
            } => (
                ExecutionEvent::Rejected {
                    client_order_id,
                    reason,
                },
                ts,
                format!("{worker_id}:rejected:{client_order_id}"),
            ),
            VenueEvent::Fill(fill) => {
                let fill = *fill;
                let event_ts = fill.ts;
                let fill_order_id = fill.order_id;
                let seq = *source_seq;
                // 成交回报的关联号取自成交内容:`source_seq` 是进程内计数,
                // 重启后重新计数会让同一订单的第二笔成交撞上第一笔已落库事实
                // 而被静默去重(订单停在 Accepted、资金不动)。
                let content_correlation = format!("{worker_id}:{}", fill_tag(&fill));
                // 精度闸门:带冻结产品规格的回报若落在 tick/step 之外,就是与规格冲突的
                // 事实,只能转成待对账(不记账、不改状态),并把原因回传给 worker。
                let violation = spec.and_then(|spec| validate_fill_against_spec(&fill, spec));
                if let Some(error) = violation {
                    rejections.push(format!(
                        "订单 {fill_order_id} 成交回报违反产品规格: {error:?}"
                    ));
                    (
                        ExecutionEvent::ReconcileRequired {
                            client_order_id: fill_order_id,
                        },
                        event_ts,
                        format!("{worker_id}:precision:{fill_order_id}:{seq}"),
                    )
                } else {
                    let event = match spec {
                        Some(spec) => ExecutionEvent::FillWithSpec {
                            fill: Box::new(fill),
                            spec: Box::new(spec.clone()),
                        },
                        None => ExecutionEvent::Fill(Box::new(fill)),
                    };
                    (event, event_ts, content_correlation)
                }
            }
            VenueEvent::Cancelled {
                client_order_id,
                ts,
            } => (
                ExecutionEvent::Cancelled { client_order_id },
                ts,
                format!("{worker_id}:cancelled:{client_order_id}"),
            ),
            VenueEvent::FeeAdjustment {
                account_id,
                venue_id,
                order_id,
                instrument,
                fees,
                ts,
            } => {
                let fact = ExecutionEvent::FeeAdjustment {
                    account_id,
                    venue_id,
                    order_id,
                    instrument,
                    fees,
                };
                let correlation = fact_correlation(worker_id, &fact);
                (fact, ts, correlation)
            }
        };
        pipeline.append_execution_event(ExecutionEventEnvelope {
            event,
            event_ts,
            receive_ts,
            source_seq: *source_seq,
            correlation_id,
        })?;
    }
    if !rejections.is_empty() {
        return Err(format!(
            "成交回报精度越界,已转为待对账事实: {}",
            rejections.join("; ")
        ));
    }
    Ok(event_count)
}

/// 提交一条已通过控制面审计的 SubmitOrder:框架唯一的订单提交入口。
///
/// 这里不负责账户/Venue 拓扑授权,也不负责 ControlPlane 的 Accepted/终态回写;
/// 它只负责订单事实注册、未知结果保护、Venue submit 和标准回报归约。这样
/// Paper、Binance 和未来执行 worker 可以共享同一副作用边界。
///
/// `spread_store` 是多腿订单组快照来源:带 `spread_group_id` 的命令必须由注入了
/// 组存储的提交路径执行,否则 [`spread_group_barrier`] 以 fail-closed 拒绝。
///
/// 账户级风控走 `context`(快照与市场规格),供 Paper/CCXT/Binance worker 统一使用;
/// Venue 仍只接收通过预检的订单。
pub fn submit_order_with_risk<V: Venue, P: ExecutionEventPort>(
    request: SubmitOrderWithRiskRequest<'_, V, P>,
) -> Result<String, String> {
    let SubmitOrderWithRiskRequest {
        command,
        venue,
        pipeline,
        worker_id,
        now,
        source_seq,
        context,
        spread_store,
    } = request;
    spread_group_barrier(spread_store, command)?;
    let mut venue = BorrowedVenuePort::new(venue);
    let mut gateway = ExecutionGateway::new(&mut venue, pipeline, worker_id, now, source_seq);
    if let Some(store) = spread_store {
        gateway = gateway.with_spread_group_store(store);
    }
    let result = if let Some(spec) = context.risk.instrument_spec.clone() {
        gateway
            .with_instrument_spec(spec)
            .submit_command_with_risk(
                command,
                &RiskContextPort {
                    risk: context.risk,
                    position: context.position,
                },
            )?
    } else {
        gateway.submit_command_with_risk(
            command,
            &RiskContextPort {
                risk: context.risk,
                position: context.position,
            },
        )?
    };
    Ok(result.message)
}

/// 把“结果未知,必须先对账”写成运行时事实的端口适配器(对账判定唯一口径在 qx-genglu 的 `order_reconcile_verdict`,本端口只落事实、不复制判定)。
///
/// Paper、CCXT 与 Binance worker 共用同一条写入路径:`source_seq` 由端口独占推进,
/// correlation id 形如 `<worker>:<tag>:<client_order_id>`(`tag` 默认 `reconcile`,
/// 各 worker 用自己的分类标签以保持既有审计口径)。`reason` 不会进入事件载荷
/// (EventLog schema 不变),但必须非空,并在写入失败时随错误一起返回,避免
/// “未知结果”连原因都留不下。
pub struct EventLogReconcilePort<'a, P: EventAppender> {
    pipeline: &'a mut P,
    worker_id: &'a str,
    now: u64,
    source_seq: &'a mut u64,
    event_ts: u64,
    tag: &'static str,
}

impl<'a, P: EventAppender> EventLogReconcilePort<'a, P> {
    pub fn new(pipeline: &'a mut P, worker_id: &'a str, now: u64, source_seq: &'a mut u64) -> Self {
        Self {
            pipeline,
            worker_id,
            now,
            source_seq,
            event_ts: now,
            tag: "reconcile",
        }
    }

    /// 事件时间取远端回报时间,`now` 仍是本地接收时间。
    pub fn at_event_ts(mut self, event_ts: u64) -> Self {
        self.event_ts = event_ts;
        self
    }

    /// 覆盖 correlation id 的分类标签,保持各 worker 既有口径不变。
    pub fn with_tag(mut self, tag: &'static str) -> Self {
        self.tag = tag;
        self
    }

    /// 把「结果未知,必须先对账」落成运行时事实。私有在 trait 里时编译器不会把它当
    /// 公共面,现在 trait 已删(全仓无 `dyn ReconcilePort`、无 `T: ReconcilePort` 约束,
    /// 三个调用点都拿具体类型 `EventLogReconcilePort` 直接调),所以方法必须显式 `pub`。
    pub fn require_reconcile(&mut self, client_order_id: u64, reason: &str) -> Result<(), String> {
        let reason = reason.trim();
        if reason.is_empty() {
            return Err(format!(
                "标记订单 {client_order_id} 待对账失败: 缺少未知结果的原因"
            ));
        }
        *self.source_seq = self.source_seq.saturating_add(1);
        let source_seq = *self.source_seq;
        self.pipeline
            .append_execution_event(ExecutionEventEnvelope {
                event: ExecutionEvent::ReconcileRequired { client_order_id },
                event_ts: self.event_ts,
                receive_ts: self.now,
                source_seq,
                correlation_id: format!("{}:{}:{client_order_id}", self.worker_id, self.tag),
            })
            .map_err(|error| format!("{error};待对账原因: {reason}"))
    }
}

/// Paper 执行的统一单轨入口:风控预检、行情门禁与事实写入语义只由执行端口决定。
///
/// 具体 EventLog 后端(文件/分段、SQLite、PostgreSQL)由调用方按运行时配置打开并
/// 注入,本 crate 不再感知任何后端与特性开关,因此 Paper、Live 与自定义运行时共用
/// 同一条判定链。`allow_synthetic_quote` 是**唯一**的伪造价开关,只允许离线 smoke
/// fixture 显式传 `true`;生产 worker 必须保持 `false` 并提供 `Some(market_quote)`。
/// `fee_model` 同理:成本口径由调用方显式给出,不留"默认零费"的口子(V11 §4.1)。
pub fn execute_paper_submit_effect<P: ExecutionEventPort + application::LedgerProbe>(
    request: PaperSubmitRequest<'_, P>,
) -> Result<String, String> {
    let PaperSubmitRequest {
        command,
        pipeline,
        now,
        risk,
        position,
        market_quote,
        fee_model,
        allow_synthetic_quote,
        spread_store,
    } = request;
    // 多腿屏障由网关统一裁决:带 `spread_group_id` 的腿若提交路径没有注入组存储,
    // 无法证明组干净,直接 fail-closed(见 [`spread_group_barrier`])。
    spread_group_barrier(spread_store, command)?;
    let order = order_from_submit_command(command)
        .map_err(|error| format!("Paper SubmitOrder 订单载荷非法: {error:?}"))?;
    // fail-closed:风控上下文与持仓快照必须成对提供。`(None, None)` 过去会跳过
    // 账户级预检直接下单,等价于风控缺失时 fail-open,现已禁止。消息保留
    // `FAIL_CLOSED:` 前缀,供 CLI 与监控按前缀分类拒绝原因。
    let (risk_context, position_snapshot) = match (risk.as_ref(), position.as_ref()) {
        (Some(risk), Some(position)) => (risk, position),
        _ => {
            return Err(
                "FAIL_CLOSED: risk context missing: RiskContext and OrderRiskPosition required"
                    .into(),
            )
        }
    };
    // fail-closed:撮合行情必须来自 EventLog 的真实行情事实;缺行情时不再伪造
    // `100` 盘口价。`allow_synthetic_quote` 只允许离线 smoke fixture 显式开启。
    if market_quote.is_none() && !allow_synthetic_quote {
        return Err("FAIL_CLOSED: market quote missing: EventLog quote is required".into());
    }
    let quote = market_quote.unwrap_or_else(|| {
        // WARN: 合成盘口仅供 Paper smoke fixture 使用(显式 opt-in)。生产路径
        // 必须传入最新行情事实,否则上面的分支已经以 fail-closed 拒绝撮合。
        let ask = order.limit.unwrap_or_else(|| Price::from_i64(100));
        let bid = Price::from_raw(ask.raw().saturating_sub(qx_core::SCALE));
        QuoteTick::new(
            now.saturating_add(1),
            bid,
            Quantity::from_i64(1_000),
            ask,
            Quantity::from_i64(1_000),
            1,
        )
    });
    validate_paper_quote(&quote)?;
    let mut venue = PaperVenue::new("paper", fee_model);
    if order.exact_amounts.is_some() {
        let spec = risk_context.instrument_spec.clone().ok_or_else(|| {
            "FAIL_CLOSED: Paper exact order 缺少冻结 market spec 和结算币种".to_string()
        })?;
        venue
            .set_instrument_spec(spec)
            .map_err(|error| format!("Paper 冻结 market spec 配置失败: {error:?}"))?;
    }
    let mut source_seq = 0_u64;
    // Paper 与 worker/实盘共用同一副作用编排(`ExecutionGateway` = `PortExecutionService`):
    // 空的 Venue 回报同样要转成"未知结果 → 待对账",不允许第二套实现各自解释。
    let risk_port = RiskContextPort {
        risk: risk_context,
        position: position_snapshot,
    };
    let submit_result = {
        let mut venue_port = BorrowedVenuePort::new(&mut venue);
        let mut gateway = ExecutionGateway::new(
            &mut venue_port,
            pipeline,
            "paper-execution",
            now,
            &mut source_seq,
        );
        if let Some(store) = spread_store {
            gateway = gateway.with_spread_group_store(store);
        }
        let message = match risk_context.instrument_spec.clone() {
            Some(spec) => {
                gateway
                    .with_instrument_spec(spec)
                    .submit_command_with_risk(command, &risk_port)?
                    .message
            }
            None => {
                gateway
                    .submit_command_with_risk(command, &risk_port)?
                    .message
            }
        };
        message
    };
    if submit_result.starts_with("ALREADY_APPLIED_FROM_EVENT_LOG") {
        return Ok(submit_result);
    }
    let quote_seq = if quote.source_seq == 0 {
        1
    } else {
        quote.source_seq
    };
    pipeline.append_execution_event(ExecutionEventEnvelope {
        event: ExecutionEvent::MarketQuote {
            instrument: order.instrument.clone(),
            quote,
        },
        event_ts: now.saturating_add(1),
        receive_ts: now.saturating_add(1),
        source_seq: quote_seq,
        correlation_id: format!("paper-execution:market:{}:{quote_seq}", order.instrument),
    })?;
    let fills = venue
        .try_on_quote(&order.instrument, quote)
        .map_err(|error| format!("Paper 精确撮合失败: {error:?}"))?;
    let fill_count = fills.len();
    if let Some(spec) = risk_context.instrument_spec.as_ref() {
        ingest_venue_events_with_spec(
            pipeline,
            fills,
            "paper-execution",
            quote.ts,
            &mut source_seq,
            spec,
        )?;
    } else {
        ingest_venue_events(
            pipeline,
            fills,
            "paper-execution",
            quote.ts,
            &mut source_seq,
        )?;
    }
    Ok(format!(
        "PAPER_EXECUTED fills={} ledger_entries={}",
        fill_count,
        pipeline.ledger_entry_count()
    ))
}

fn validate_paper_quote(quote: &QuoteTick) -> Result<(), String> {
    let project = |raw: i128, exact: Option<ExactDecimal>, label: &str| match exact {
        Some(value) => {
            if !value.is_positive() {
                return Err(format!("Paper {label} 精确值必须为正数"));
            }
            let projected = value
                .checked_mul_at_scale(ExactDecimal::ONE, 9, DecimalRounding::TowardZero)
                .and_then(|value| value.to_raw(9).ok());
            if projected != Some(raw) {
                return Err(format!("Paper {label} 精确值与兼容投影不一致"));
            }
            Ok(value)
        }
        None if raw > 0 => ExactDecimal::from_parts(raw, 9)
            .map_err(|error| format!("Paper {label} 兼容值非法: {error}")),
        None => Err(format!("Paper {label} 缺少正数行情")),
    };
    let bid = project(quote.bid.raw(), quote.bid_exact, "bid")?;
    let ask = project(quote.ask.raw(), quote.ask_exact, "ask")?;
    if bid.checked_cmp(ask).is_none_or(|ordering| ordering.is_gt()) {
        return Err("Paper 行情买卖价交叉或无法比较".into());
    }
    project(quote.bid_qty.raw(), quote.bid_qty_exact, "bid quantity")?;
    project(quote.ask_qty.raw(), quote.ask_qty_exact, "ask quantity")?;
    Ok(())
}

#[cfg(test)]
mod tests;