cobre-sddp 0.8.2

Stochastic Dual Dynamic Programming (SDDP) for hydrothermal dispatch and energy planning
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
//! Dynamic Cut Selection (DCS) hyperparameters as a hot-path value type.
//!
//! DCS is a per-solve lazy selection loop: rather than deactivating cuts at the
//! pool level, it grows the LP's active cut set lazily within each solve until
//! no candidate cut is violated by more than `epsilon_viol` (or a bounded
//! inner-iteration cap is hit). See
//! `docs/design/dynamic-cut-selection-design.md` for the full design.
//!
//! [`DcsParams`] is a small, `Copy`, allocation-free carrier for the DCS
//! hyperparameters. It mirrors the values stored on
//! [`CutSelectionStrategy::Dynamic`]
//! (the serde-stable config representation) but is intentionally decoupled from
//! it so the hot path is not coupled to config parsing. Construct one from a
//! strategy via [`DcsParams::from_strategy`].
//!
//! This module defines the value type only — wiring it into a context struct or
//! a pass is the responsibility of later DCS work.

use std::time::Instant;

use cobre_solver::{
    Basis, ProfiledSolver, RowBatch, SolutionView, SolverError, SolverInterface, StageTemplate,
};

use crate::basis_reconstruct::{
    ReconstructionTarget, enforce_basic_count_invariant, reconstruct_basis_uniform_basic,
};
use crate::cut::row::append_slots_to_lp;
use crate::cut::{CutPool, CutRowMap};
use crate::cut_selection::CutSelectionStrategy;
use crate::error::SddpError;
use crate::gemm::gemm_block;
use crate::indexer::StageIndexer;
use crate::workspace::CapturedBasis;

/// Dynamic Cut Selection hyperparameters.
///
/// A `Copy`, allocation-free value type. Field invariants (`nadic >= 1`,
/// `epsilon_viol > 0`, `k1` either `None` or `Some(>= 1)`) are enforced by the
/// config-parse step that produces
/// [`CutSelectionStrategy::Dynamic`];
/// `DcsParams` trusts its inputs and documents the invariants here.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DcsParams {
    /// Candidate-recency window.
    ///
    /// `None` ⇒ `∞`: every pool cut is a candidate. This is the
    /// exactness-preserving default. `Some(n)` ⇒ only cuts whose
    /// `iteration_generated` is within the last `n` SDDP iterations are
    /// candidates (windowed, deliberately non-exact). Consumed by the scoring
    /// kernel.
    pub k1: Option<u32>,

    /// Initial-set history window: how far back to seed the active set at the
    /// start of a lazy solve.
    pub k2: u32,

    /// Maximum number of cuts added per inner iteration (`>= 1`).
    pub nadic: u32,

    /// Violation tolerance for accepting a candidate cut (`> 0`, and at least
    /// the LP dual-feasibility tolerance).
    pub epsilon_viol: f64,

    /// First SDDP iteration (1-based) at which DCS is applied.
    pub start_iteration: u64,

    /// Bounded inner-loop cap: maximum number of lazy add-and-resolve rounds
    /// before falling back to a terminating-condition solve.
    pub max_inner_iterations: u32,
}

impl Default for DcsParams {
    fn default() -> Self {
        Self {
            k1: None,
            k2: 5,
            nadic: 10,
            epsilon_viol: 1e-10,
            start_iteration: 2,
            max_inner_iterations: 50,
        }
    }
}

impl DcsParams {
    /// Build [`DcsParams`] from a cut-selection strategy.
    ///
    /// Returns `Some` only for the
    /// [`Dynamic`](crate::cut_selection::CutSelectionStrategy::Dynamic) variant,
    /// copying its five fields (`k1`, `k2`, `nadic`, `epsilon_viol`,
    /// `start_iteration`) and taking `max_inner_iterations` from
    /// [`DcsParams::default`]. Returns `None` for every other variant.
    #[must_use]
    pub fn from_strategy(strategy: &CutSelectionStrategy) -> Option<Self> {
        match strategy {
            CutSelectionStrategy::Dynamic {
                k1,
                k2,
                nadic,
                epsilon_viol,
                start_iteration,
            } => Some(Self {
                k1: *k1,
                k2: *k2,
                nadic: *nadic,
                epsilon_viol: *epsilon_viol,
                start_iteration: *start_iteration,
                max_inner_iterations: Self::default().max_inner_iterations,
            }),
            CutSelectionStrategy::Level1 { .. }
            | CutSelectionStrategy::Lml1 { .. }
            | CutSelectionStrategy::Dominated { .. } => None,
        }
    }

    /// Return whether DCS is active at the given 1-based SDDP `iteration`.
    ///
    /// `true` when `iteration >= self.start_iteration`.
    #[must_use]
    pub fn is_active(&self, iteration: u64) -> bool {
        iteration >= self.start_iteration
    }
}

// ---------------------------------------------------------------------------
// DcsScoringScratch
// ---------------------------------------------------------------------------

/// Reusable scratch buffers for [`score_violated_candidates`].
///
/// Held across solves so the scoring kernel allocates nothing on the hot path.
/// Grow capacities once up front via [`DcsScoringScratch::reserve`]; the kernel
/// only clears and re-fills.
#[derive(Default)]
pub struct DcsScoringScratch {
    /// Unscaled (raw) state vector at the current LP optimum, one entry per
    /// state index (`indexer.n_state` long after fill).
    pub unscaled_state: Vec<f64>,

    /// Gathered coefficient rows of the eligible candidates for a single scoring
    /// pass, row-major `k_rows × n_state`. Cleared and re-filled each pass; the
    /// single batched `gemm_block` reads it as the GEMM left operand. Reserved
    /// to `pool_capacity * n_state` so even an all-eligible pass needs no growth.
    pub cand_coef_block: Vec<f64>,

    /// Per-candidate activity output of the single batched GEMM, one entry per
    /// gathered candidate (`alpha[i] = ∇·x*_raw` for candidate `i`, before the
    /// per-candidate intercept is added). Length tracks the gathered candidate
    /// count each pass.
    pub alpha: Vec<f64>,

    /// Slot id of each gathered candidate, parallel to the rows of
    /// `cand_coef_block` and (after the GEMM) to `alpha`. Lets the post-GEMM
    /// violation scan recover each activity entry's slot id (and, via
    /// `pool.intercepts`, its intercept).
    pub cand_slots: Vec<u32>,

    /// `(violation_magnitude, slot)` for each violated candidate, before the
    /// top-`nadic` truncation.
    pub violations: Vec<(f64, u32)>,
}

impl DcsScoringScratch {
    /// Grow the scratch buffers to hold `n_state` state entries and up to
    /// `pool_capacity` candidate cuts, without shrinking existing capacity
    /// (growth-only, mirroring `workspace.rs`).
    ///
    /// `cand_coef_block` is reserved to `pool_capacity * n_state` — the worst
    /// case where every active cut is an eligible (non-resident, in-window)
    /// candidate — while `alpha`, `cand_slots`, and `violations` are reserved to
    /// `pool_capacity` entries each.
    pub fn reserve(&mut self, n_state: usize, pool_capacity: usize) {
        if self.unscaled_state.capacity() < n_state {
            self.unscaled_state
                .reserve(n_state - self.unscaled_state.capacity());
        }
        let coef_capacity = pool_capacity * n_state;
        if self.cand_coef_block.capacity() < coef_capacity {
            self.cand_coef_block
                .reserve(coef_capacity - self.cand_coef_block.capacity());
        }
        if self.alpha.capacity() < pool_capacity {
            self.alpha.reserve(pool_capacity - self.alpha.capacity());
        }
        if self.cand_slots.capacity() < pool_capacity {
            self.cand_slots
                .reserve(pool_capacity - self.cand_slots.capacity());
        }
        if self.violations.capacity() < pool_capacity {
            self.violations
                .reserve(pool_capacity - self.violations.capacity());
        }
    }
}

// ---------------------------------------------------------------------------
// score_violated_candidates
// ---------------------------------------------------------------------------

/// Score the non-resident cut candidates at the current LP optimum and select
/// the most-violated `nadic` to add.
///
/// # Sign & scale contract
///
/// Cut coefficients are stored **raw** (`∂Q/∂x`); the LP solves in **scaled**
/// space. The cut row is `−∇·x + θ ≥ intercept` (see `.claude/rules/sddp.md`),
/// so the cut's `θ`-floor at the raw optimum `x*_raw` is
/// `alpha = intercept + ∇·x*_raw`. A candidate is **violated** when the current
/// LP `θ*_raw` falls below that floor by more than `epsilon_viol`, i.e.
/// `v = alpha − theta_raw > epsilon_viol` (strict).
///
/// Both `θ*` and every state column are unscaled by `col_scale` before scoring
/// (`x_raw = col_scale[c] · x_scaled`); mixing scaled state with raw
/// coefficients is the classic silent bug. An empty `col_scale` means no
/// scaling (factor 1.0).
///
/// State index → LP column mapping uses
/// [`StageIndexer::state_to_lp_column`], identical to the cut-row builder in
/// `cut::row::build_cut_row_batch_into`, so scoring and row construction
/// reference the same columns.
///
/// # Batched scoring
///
/// A single pass over [`CutPool::active_cuts`] (ascending-slot order) applies
/// the filters below and **gathers** each surviving candidate's `coefficients`
/// slice into the contiguous row-major `scratch.cand_coef_block`
/// (`k_rows × n_state`), recording its slot in `scratch.cand_slots`. One
/// `gemm_block` then fills `scratch.alpha[0..k_rows]` with every candidate's
/// `∇·x*_raw` in a single dispatch. Because `gemm_block` computes each output
/// row's dot product independently, a candidate's activity is **bit-identical**
/// whether scored alone (`k_rows = 1`) or in a batch (`k_rows = N`); the
/// per-candidate intercept (`pool.intercepts[slot]`) is added afterward, outside
/// the GEMM. The violation scan, sort, and `nadic` truncation are unchanged.
///
/// # `k1` candidate-recency window
///
/// Applied first. A cut at slot `s` is eligible only when `params.k1` is `None`
/// (∞ — every cut a candidate, the exactness path) or its age
/// `current_iteration.saturating_sub(pool.metadata[s].iteration_generated)` is
/// `< k1`. Warm-start cuts carry `iteration_generated == u64::MAX`, so their
/// saturating age is `0` and they are always inside any finite window. Cuts
/// outside the window are skipped entirely — never scored, never added.
///
/// # Selection & determinism
///
/// Violated candidates are sorted by violation **descending**, ties broken by
/// **ascending slot id** (via [`f64::total_cmp`], which is panic-free and
/// NaN-stable — never `partial_cmp().unwrap()`). The first `nadic` slot ids are
/// written to `out_selected` (cleared first). The choice is a deterministic
/// function of `(pool contents, x*, ε_viol, nadic, k1, current_iteration)`.
///
/// # Returns
///
/// The total count of violated candidates (≥ `out_selected.len()`). The caller
/// uses this to detect the no-violation stop and drive the TC fallback.
///
/// # Allocation
///
/// Allocation-free beyond growth of the pre-reserved `scratch` vectors. Call
/// [`DcsScoringScratch::reserve`] before the hot loop.
pub fn score_violated_candidates(
    pool: &CutPool,
    indexer: &StageIndexer,
    primal: &[f64],
    col_scale: &[f64],
    resident: &CutRowMap,
    params: &DcsParams,
    current_iteration: u64,
    scratch: &mut DcsScoringScratch,
    out_selected: &mut Vec<u32>,
) -> usize {
    let n_state = indexer.n_state;
    let theta = indexer.theta;

    // `primal.len() >= theta + 1` (i.e. the theta column is in range), written
    // as `> theta` to satisfy clippy::int_plus_one.
    debug_assert!(
        primal.len() > theta,
        "score_violated_candidates: primal.len() {} <= theta ({theta})",
        primal.len(),
    );
    debug_assert_eq!(
        pool.state_dimension, n_state,
        "score_violated_candidates: pool.state_dimension {} != indexer.n_state {}",
        pool.state_dimension, n_state,
    );
    // When scaling is active, `col_scale` is per-column and must cover every LP
    // column read below — both `col_scale[theta]` and `col_scale[c]` for each
    // state column `c = state_to_lp_column(j)`. It is sized like `primal`
    // (one entry per column), so a single length check covers both accesses.
    debug_assert!(
        col_scale.is_empty() || col_scale.len() == primal.len(),
        "score_violated_candidates: col_scale.len() {} != primal.len() {} (non-empty col_scale \
         must be per-column)",
        col_scale.len(),
        primal.len(),
    );

    out_selected.clear();
    scratch.violations.clear();
    scratch.cand_coef_block.clear();
    scratch.cand_slots.clear();
    scratch.alpha.clear();

    // Unscale θ*: scaled space ⇒ raw via col_scale[theta] (empty ⇒ factor 1.0).
    let theta_raw = if col_scale.is_empty() {
        primal[theta]
    } else {
        col_scale[theta] * primal[theta]
    };

    // Unscale the raw state vector at the LP state columns. The column mapping
    // mirrors `cut::row::build_cut_row_batch_into` exactly.
    scratch.unscaled_state.clear();
    for j in 0..n_state {
        let c = indexer.state_to_lp_column(j);
        let x_raw = if col_scale.is_empty() {
            primal[c]
        } else {
            col_scale[c] * primal[c]
        };
        scratch.unscaled_state.push(x_raw);
    }

    // Gather pass: iterate active cuts in ascending-slot order, apply the k1
    // window then the resident-skip, and copy each surviving candidate's
    // coefficient row into the contiguous `cand_coef_block` block (recording its
    // slot in `cand_slots`). The intercept is recovered later via
    // `pool.intercepts[slot]`, so it need not be gathered here.
    for (slot, _intercept, coefficients) in pool.active_cuts() {
        // k1 candidate-recency window (applied first). saturating_sub keeps
        // warm-start cuts (iteration_generated == u64::MAX, age 0) eligible.
        if let Some(k1) = params.k1 {
            let age = current_iteration.saturating_sub(pool.metadata[slot].iteration_generated);
            if age >= u64::from(k1) {
                continue;
            }
        }

        // Skip slots already resident in the LP.
        if resident.lp_row_for_slot(slot).is_some() {
            continue;
        }

        scratch.cand_coef_block.extend_from_slice(coefficients);
        #[allow(clippy::cast_possible_truncation)]
        scratch.cand_slots.push(slot as u32);
    }

    let k_rows = scratch.cand_slots.len();

    // One batched GEMM for the whole pass: alpha[i] = ∇_i · x*_raw for each
    // gathered candidate i (single trial point, m_len = 1). gemm_block computes
    // each output row independently, so each alpha is bit-identical to the
    // per-candidate (k_rows = 1) call it replaces. A zero candidate count is a
    // gemm_block no-op (it returns immediately for k_rows == 0).
    scratch.alpha.clear();
    scratch.alpha.resize(k_rows, 0.0);
    gemm_block(
        &scratch.cand_coef_block,
        &scratch.unscaled_state,
        k_rows,
        n_state,
        1,
        &mut scratch.alpha,
    );

    // Violation scan over the batched activities: v = (intercept + ∇·x*_raw) −
    // θ_raw, with the intercept recovered per candidate from `pool.intercepts`.
    for (i, &slot) in scratch.cand_slots.iter().enumerate() {
        let alpha = pool.intercepts[slot as usize] + scratch.alpha[i];
        let v = alpha - theta_raw;
        if v > params.epsilon_viol {
            scratch.violations.push((v, slot));
        }
    }

    let violated_count = scratch.violations.len();

    // Sort by violation descending, ties by ascending slot id. total_cmp is
    // panic-free and NaN-stable (no partial_cmp().unwrap()).
    scratch
        .violations
        .sort_unstable_by(|a, b| b.0.total_cmp(&a.0).then(a.1.cmp(&b.1)));

    let take = (params.nadic as usize).min(violated_count);
    out_selected.extend(scratch.violations[..take].iter().map(|&(_, slot)| slot));

    violated_count
}

// ---------------------------------------------------------------------------
// DcsSolveContext
// ---------------------------------------------------------------------------

/// Per-(stage, solve) context for [`lazy_solve_preloaded`], mirroring the error-context
/// fields `run_stage_solve` uses.
#[derive(Clone, Copy, Debug)]
pub struct DcsSolveContext {
    /// Stage index `t`.
    pub stage_index: usize,
    /// Scenario index `m`.
    pub scenario_index: usize,
    /// Training iteration (1-based) feeding the `k1` recency window, or `None`
    /// off the training path (e.g. simulation). `None` is treated as `0`, which
    /// disables the window: `0.saturating_sub(iteration_generated) == 0 < k1`,
    /// so every cut is eligible.
    pub iteration: Option<u64>,

    /// Continue from a carried LP instead of starting a fresh solve.
    ///
    /// `false` (the default for every fresh solve): reset the scratch
    /// [`CutRowMap`], append `initial_resident`, and run the initial solve
    /// (warm if a `stored_basis` was supplied, else cold). This is the only mode
    /// the forward, simulation, and per-opening backward paths use.
    ///
    /// `true` (backward opening-reuse path, openings 1..): the solver already
    /// holds the previous opening's cut rows (tracked in the carried scratch
    /// `CutRowMap`) and a warm basis; only the opening's noise bounds changed.
    /// Skip the reset / `initial_resident` append / model reload entirely; just
    /// warm-`solve(None)` to re-optimize under the new bounds, then run the lazy
    /// loop, which appends only the cuts this opening additionally violates.
    /// `initial_resident` and `stored_basis` are ignored in this mode.
    pub continue_carry: bool,
}

// ---------------------------------------------------------------------------
// DcsSolveScratch
// ---------------------------------------------------------------------------

/// Reusable buffers for [`lazy_solve_preloaded`], held across solves so the
/// steady-state hot path allocates nothing.
///
/// These buffers are held outside `SolverWorkspace` so `lazy_solve_preloaded`
/// is testable in isolation.
pub struct DcsSolveScratch {
    /// Add-row construction buffer for `add_rows`.
    pub batch: RowBatch,
    /// Candidate-scoring scratch (see [`DcsScoringScratch`]).
    pub scoring: DcsScoringScratch,
    /// Selected slot ids from the most recent scoring call.
    pub out_selected: Vec<u32>,
    /// Destination basis for the initial uniform-BASIC reconstruction.
    pub recon_basis: Basis,
    /// Slot→LP-row map for the cut rows resident in the loaded LP.
    ///
    /// Held in scratch (rather than allocated per call) so the steady-state hot
    /// path does not allocate a fresh `vec![None; populated_count]` every solve,
    /// and so the backward opening-reuse path can carry residency across a trial
    /// point's openings: a fresh solve resets it (see
    /// [`DcsSolveContext::continue_carry`]), a continued solve leaves it intact.
    pub row_map: CutRowMap,
    /// Owned copy of the final solve's primal vector (one entry per LP column).
    ///
    /// [`lazy_solve_preloaded`] returns `Result<()>` rather than a borrowing
    /// `SolutionView`: on the terminating path it copies the live solve's
    /// solution into these caller-owned `res_*` buffers, and the caller rebuilds
    /// a zero-cost view over them via [`DcsSolveScratch::result_view`]. This
    /// avoids returning a loan obtained inside the lazy loop (rejected by stable
    /// NLL) without keeping the redundant exit re-solve.
    pub res_primal: Vec<f64>,
    /// Owned copy of the final solve's dual vector. On the DCS path this is the
    /// FULL dual — structural rows followed by the resident cut rows — and is
    /// copied verbatim (never truncated): the simulation reader relies on the
    /// structural-row prefix and ignores the trailing cut-row entries.
    pub res_dual: Vec<f64>,
    /// Owned copy of the final solve's reduced-cost vector (one entry per LP
    /// column).
    pub res_reduced_costs: Vec<f64>,
    /// Owned copy of the final solve's objective value.
    pub res_objective: f64,
    /// Owned copy of the final solve's simplex iteration count.
    pub res_iterations: u64,
    /// Owned copy of the final solve's wall-clock solve time, in seconds.
    pub res_solve_time_seconds: f64,
    /// Cumulative wall time, in seconds, spent inside candidate-scoring
    /// (`score_violated_candidates`) across every [`lazy_solve_preloaded`] call
    /// that used this scratch. Grows monotonically and is never reset by the
    /// solve loop, so a per-worker accumulator survives across all (stage,
    /// solve) pairs. Read by instrumentation after a run to compute the
    /// scoring-versus-solve split; pure measurement, off the steady-state hot
    /// path when no observer reads it.
    pub scoring_time_seconds: f64,
    /// Cumulative sum of the resident cut-row count (`row_map.total_cut_rows()`)
    /// over every solve that used this scratch — one term per
    /// [`lazy_solve_preloaded`] completion (see `Self::store_result`). With
    /// [`Self::rows_in_lp_count`] this yields the mean rows-in-LP per solve.
    /// Grows monotonically; never reset by the solve loop (a per-worker
    /// accumulator surviving across all (stage, solve) pairs, like
    /// [`Self::scoring_time_seconds`]). Pure measurement, off the steady-state
    /// hot path.
    pub rows_in_lp_sum: u64,
    /// Number of solves folded into [`Self::rows_in_lp_sum`] (one per
    /// `Self::store_result` call). The denominator for the mean.
    pub rows_in_lp_count: u64,
    /// Largest resident cut-row count observed across every solve that used this
    /// scratch.
    pub rows_in_lp_max: u64,
}

impl Default for DcsSolveScratch {
    fn default() -> Self {
        Self {
            batch: RowBatch {
                num_rows: 0,
                row_starts: Vec::new(),
                col_indices: Vec::new(),
                values: Vec::new(),
                row_lower: Vec::new(),
                row_upper: Vec::new(),
            },
            scoring: DcsScoringScratch::default(),
            out_selected: Vec::new(),
            recon_basis: Basis::new(0, 0),
            res_primal: Vec::new(),
            res_dual: Vec::new(),
            res_reduced_costs: Vec::new(),
            res_objective: 0.0,
            res_iterations: 0,
            res_solve_time_seconds: 0.0,
            scoring_time_seconds: 0.0,
            rows_in_lp_sum: 0,
            rows_in_lp_count: 0,
            rows_in_lp_max: 0,
            row_map: CutRowMap::new(0, 0),
        }
    }
}

impl DcsSolveScratch {
    /// Grow the inner buffers to fit `n_state` state entries and a pool of
    /// `pool_capacity` cuts, without shrinking (growth-only).
    pub fn reserve(&mut self, n_state: usize, pool_capacity: usize) {
        self.scoring.reserve(n_state, pool_capacity);
        if self.out_selected.capacity() < pool_capacity {
            self.out_selected
                .reserve(pool_capacity - self.out_selected.capacity());
        }
        // Pre-size the slot→row map so the first solve does not reallocate; the
        // `base_row_offset` here is a placeholder (each fresh solve resets it to
        // the loaded core's row count in `lazy_solve_preloaded`).
        self.row_map.reset(pool_capacity, 0);
        // Pre-grow the result buffers to a conservative non-zero capacity. Their
        // exact lengths (num_cols for primal/reduced_costs; structural rows + cut
        // rows for dual) depend on the loaded LP, not on these arguments, so the
        // first fill may still grow them; from then on `clear()` + `extend_from_slice`
        // reuses the warmed capacity (alloc-free steady state).
        for buf in [
            &mut self.res_primal,
            &mut self.res_dual,
            &mut self.res_reduced_costs,
        ] {
            if buf.capacity() < pool_capacity {
                buf.reserve(pool_capacity - buf.capacity());
            }
        }
    }

    /// Rebuild a zero-copy [`SolutionView`] over the result buffers filled by the
    /// most recent [`lazy_solve_preloaded`] call.
    ///
    /// The view borrows `self` immutably, so it composes with the other immutable
    /// reads a caller performs after the solve (e.g. the resident `row_map`). It
    /// is only meaningful immediately after a successful `lazy_solve_preloaded`
    /// call that filled the buffers; the slices are exactly the final solve's
    /// solution (the one the redundant exit re-solve used to recompute).
    #[must_use]
    pub fn result_view(&self) -> SolutionView<'_> {
        SolutionView {
            objective: self.res_objective,
            primal: &self.res_primal,
            dual: &self.res_dual,
            reduced_costs: &self.res_reduced_costs,
            iterations: self.res_iterations,
            solve_time_seconds: self.res_solve_time_seconds,
        }
    }

    /// Copy a live solve's solution into the reused result buffers.
    ///
    /// `clear()` + `extend_from_slice` keeps the steady state allocation-free
    /// (capacity persists across calls); the full slices are copied verbatim —
    /// the dual in particular is NOT truncated, so the simulation reader still
    /// sees the structural-row prefix it depends on.
    fn store_result(&mut self, view: &SolutionView<'_>) {
        self.res_objective = view.objective;
        self.res_iterations = view.iterations;
        self.res_solve_time_seconds = view.solve_time_seconds;
        self.res_primal.clear();
        self.res_primal.extend_from_slice(view.primal);
        self.res_dual.clear();
        self.res_dual.extend_from_slice(view.dual);
        self.res_reduced_costs.clear();
        self.res_reduced_costs.extend_from_slice(view.reduced_costs);

        // Per-solve resident-set size (rows-in-LP): the number of cut rows
        // resident in the LP at this solve's completion. Folded here because
        // `store_result` is the single completion point of every lazy solve
        // (both the exact-optimum and TC-fallback exits). Mirrors the
        // `scoring_time_seconds` accumulator — cumulative, never reset.
        let rows_in_lp = self.row_map.total_cut_rows() as u64;
        self.rows_in_lp_sum += rows_in_lp;
        self.rows_in_lp_count += 1;
        self.rows_in_lp_max = self.rows_in_lp_max.max(rows_in_lp);
    }
}

// ---------------------------------------------------------------------------
// lazy_solve_preloaded
// ---------------------------------------------------------------------------

/// Map a [`SolverError`] to an [`SddpError`] with stage/scenario/iteration
/// context — the same contract `stage_solve::run_stage_solve` uses
/// (`Infeasible` carries the context; everything else is `SddpError::Solver`).
fn map_solver_error(e: SolverError, ctx: DcsSolveContext) -> SddpError {
    match e {
        SolverError::Infeasible => SddpError::Infeasible {
            stage: ctx.stage_index,
            iteration: ctx.iteration.unwrap_or(0),
            scenario: ctx.scenario_index,
        },
        other => SddpError::Solver(other),
    }
}

/// Solve one (stage, solve) lazily under Dynamic Cut Selection, given an
/// already-loaded core LP.
///
/// **The caller owns the model load and any bounds patch.** Before calling,
/// the caller must have run `solver.load_model(core)` (the cut-free core LP)
/// and applied every per-solve bound (state pinning, opening noise, etc.) on
/// that loaded model. This routine does NOT call `load_model`; it only appends
/// cut rows and solves. `core` is consulted for dimensions only
/// (`num_rows` → cut-row offset and reconstruction `base_row_count`;
/// `num_cols` → reconstruction `num_cols`). Because every cut row is appended
/// to the loaded, already-patched LP and every inner re-solve is a warm
/// `solve(None)` (never another `load_model`), the caller's bound patch
/// survives the entire loop.
///
/// Pass-agnostic: it builds the cut set incrementally and runs the lazy inner
/// loop. On the terminating path it copies the final solve's solution into the
/// caller-owned result buffers in `scratch` and returns `Ok(())`; the caller
/// rebuilds a zero-cost [`SolutionView`] over those buffers via
/// [`DcsSolveScratch::result_view`]. Returning by buffer rather than by borrowing
/// view is what lets the no-violation arm avoid a redundant exit re-solve without
/// returning a loan obtained inside the loop (which stable NLL rejects). Post-solve
/// extraction (backward = dual, forward/simulation = primal) is the caller's job
/// and is NOT done here.
///
/// # Algorithm
///
/// 1. Build a fresh `CutRowMap::new(pool.populated_count, core.num_rows)` for
///    the already-loaded core.
/// 2. Append `initial_resident` (active, not-yet-resident slots) via
///    [`append_slots_to_lp`].
/// 3. Seed the warm basis: if `stored_basis` is `Some`, reconstruct it
///    uniform-BASIC ([`reconstruct_basis_uniform_basic`]), repair the
///    basic-count invariant ([`enforce_basic_count_invariant`]), and
///    `solve(Some(..))`; else `solve(None)` (cold).
/// 4. Inner loop, up to `params.max_inner_iterations`: score the omitted cuts
///    at the live `view.primal` via [`score_violated_candidates`] (using
///    `ctx.iteration.unwrap_or(0)` for the `k1` window); when none is violated
///    (the **exact** optimum) copy the live view into the `res_*` buffers and
///    return — **no re-solve**; else add the top-`nadic` violated slots, drop the
///    view, and warm-`solve(None)` again.
/// 5. **TC fallback**: if the cap is hit with violations remaining, add **all**
///    remaining violated candidates and solve once (the LP changed, so this final
///    solve is legitimate), copy that view into the `res_*` buffers, and return —
///    preserving exactness.
///
/// A cold mid-loop re-solve (a `solve(None)` whose retry ladder discarded the
/// warm basis) is normal and tolerated — the loop never reads a stale basis
/// between solves.
///
/// # Errors
///
/// Propagates solver failures via the same mapping as `run_stage_solve`:
/// `SolverError::Infeasible` → [`SddpError::Infeasible`] with stage/scenario/
/// iteration context; every other variant → [`SddpError::Solver`].
///
/// # Allocation
///
/// Steady-state allocation-free: all working buffers come from `scratch` (grow
/// once via [`DcsSolveScratch::reserve`]); the result copy reuses the `res_*`
/// buffers via `clear()` + `extend_from_slice`. The per-call `CutRowMap`
/// allocation mirrors the per-(stage, solve) reset and is acceptable here.
// The argument list is the spec-mandated signature: each item is a distinct
// read-only input or scratch buffer with no natural grouping (the context-struct
// rule already bundles the per-(stage, solve) scalars into `ctx`/`params`).
#[allow(clippy::too_many_arguments)]
pub fn lazy_solve_preloaded<S: SolverInterface>(
    solver: &mut ProfiledSolver<S>,
    core: &StageTemplate,
    pool: &CutPool,
    indexer: &StageIndexer,
    col_scale: &[f64],
    stored_basis: Option<&CapturedBasis>,
    initial_resident: &[u32],
    params: &DcsParams,
    scratch: &mut DcsSolveScratch,
    ctx: DcsSolveContext,
) -> Result<(), SddpError> {
    let current_iteration = ctx.iteration.unwrap_or(0);

    // Steps 1-3: prepare the LP and run the initial solve, producing the live
    // `view` the lazy loop holds. Two modes (see `DcsSolveContext::continue_carry`).
    let mut view = if ctx.continue_carry {
        // CONTINUE (backward opening-reuse, openings 1..): the solver already
        // holds the previous opening's cut rows (tracked in `scratch.row_map`)
        // and a warm basis; the caller patched only the new opening's bounds. Do
        // NOT reset the map, append the seed, or reload — just re-solve warm to
        // re-optimize under the new bounds and fall through to the lazy loop,
        // which appends only the cuts this opening additionally violates.
        // `initial_resident` and `stored_basis` are ignored in this mode.
        solver.solve(None).map_err(|e| map_solver_error(e, ctx))?
    } else {
        // FRESH: reset the carried row map for the already-loaded core (the
        // caller has run `load_model(core)` and applied any bounds patch; this
        // routine must NOT reload — that would discard the patch), append the
        // initial resident subset, then run the initial solve (warm uniform-
        // BASIC if a stored basis exists, else cold).
        scratch.row_map.reset(pool.populated_count, core.num_rows);
        append_slots_to_lp(
            solver,
            pool,
            initial_resident,
            indexer,
            col_scale,
            &mut scratch.row_map,
            &mut scratch.batch,
        );

        let cut_rows = scratch.row_map.total_cut_rows();
        if let Some(stored) = stored_basis {
            let target = ReconstructionTarget {
                base_row_count: core.num_rows,
                num_cols: core.num_cols,
            };
            reconstruct_basis_uniform_basic(stored, target, cut_rows, &mut scratch.recon_basis);
            enforce_basic_count_invariant(
                &mut scratch.recon_basis,
                core.num_rows + cut_rows,
                core.num_rows,
            );
            solver
                .solve(Some(&scratch.recon_basis))
                .map_err(|e| map_solver_error(e, ctx))?
        } else {
            solver.solve(None).map_err(|e| map_solver_error(e, ctx))?
        }
    };

    // Step 4/5: bounded lazy inner loop with TC fallback. The loop body scores
    // the LIVE `view.primal` in place (no copy): `score_violated_candidates`
    // borrows `scratch.scoring`/`scratch.row_map`, not the solver, so it composes
    // with the held view. On the no-violation arm it copies the live view into the
    // `res_*` buffers and returns `Ok(())` — NO redundant re-solve. On the add arm
    // it drops the view, appends the selected slots, and re-solves to refresh it.
    //
    // Because the function returns `Result<()>` (not a borrowing view), `view`
    // never escapes the loop, so holding it across the iteration is accepted by
    // stable NLL — the Polonius "return a loan from a loop" gap is sidestepped.
    for _ in 0..params.max_inner_iterations {
        // Cumulative scoring-time instrumentation: `scratch.scoring` (the inner
        // scoring buffers) is borrowed during the call; the `+=` on
        // `scratch.scoring_time_seconds` runs only after the call returns and
        // touches a distinct field, so there is no borrow conflict. `view.primal`
        // borrows the solver, which `score_violated_candidates` does not touch.
        let t0 = Instant::now();
        let violated = score_violated_candidates(
            pool,
            indexer,
            view.primal,
            col_scale,
            &scratch.row_map,
            params,
            current_iteration,
            &mut scratch.scoring,
            &mut scratch.out_selected,
        );
        scratch.scoring_time_seconds += t0.elapsed().as_secs_f64();

        if violated == 0 {
            // Exact: the current resident subset reproduces the all-cuts optimum
            // and the live view already holds it. Copy it into the result buffers
            // and return — the LP is unchanged and re-solving would recompute the
            // same point.
            scratch.store_result(&view);
            return Ok(());
        }

        // A cut will be added, so the held view is stale: end its solver borrow
        // here (`SolutionView` is `Copy`, so the borrow lasts only to its last
        // use), then append the selected top-`nadic` slots and re-solve (warm;
        // may escalate to cold — never assume the warm basis survived).
        let _ = view;
        append_slots_to_lp(
            solver,
            pool,
            &scratch.out_selected,
            indexer,
            col_scale,
            &mut scratch.row_map,
            &mut scratch.batch,
        );
        view = solver.solve(None).map_err(|e| map_solver_error(e, ctx))?;
    }

    // TC fallback: the cap was hit with violations still present. Collect ALL
    // remaining violated candidates (effective nadic = ∞) from the live primal,
    // add them, and solve once. This degrades to a terminating-condition
    // (all-cuts) solve for this (stage, solve) but preserves exactness.
    let mut all_params = *params;
    all_params.nadic = u32::MAX;
    // Same cumulative-scoring instrumentation as the inner loop above.
    let t0 = Instant::now();
    let remaining = score_violated_candidates(
        pool,
        indexer,
        view.primal,
        col_scale,
        &scratch.row_map,
        &all_params,
        current_iteration,
        &mut scratch.scoring,
        &mut scratch.out_selected,
    );
    scratch.scoring_time_seconds += t0.elapsed().as_secs_f64();
    // The live view's solver borrow must end before the appending re-solve
    // (`SolutionView` is `Copy`, so the borrow lasts only to this last use).
    let _ = view;
    if remaining > 0 {
        append_slots_to_lp(
            solver,
            pool,
            &scratch.out_selected,
            indexer,
            col_scale,
            &mut scratch.row_map,
            &mut scratch.batch,
        );
    }
    let view = solver.solve(None).map_err(|e| map_solver_error(e, ctx))?;
    scratch.store_result(&view);
    Ok(())
}

// ---------------------------------------------------------------------------
// build_initial_resident_set
// ---------------------------------------------------------------------------

/// Build the DCS initial resident-cut set from synchronized pool metadata.
///
/// The initial resident set is the warm-start hint [`lazy_solve_preloaded`] consumes as
/// `initial_resident`. By exactness the converged optimum is independent of it,
/// but the seed must be a deterministic function of the pool's per-slot
/// metadata only — never of any solve trace, worker id, or MPI rank — so that
/// results are invariant to the rank count. It reads only `last_active_iter`
/// and `iteration_generated`, both maintained deterministically each iteration
/// from the MPI-gathered visited states, so seeding from them preserves that
/// invariance.
///
/// Clears `out`, then for each populated slot `s` in ascending order
/// (`0..pool.populated_count`) includes `s` iff `pool.active[s]` AND either:
///
/// - the slot was active within the last `k2` iterations
///   (`current_iteration.saturating_sub(pool.metadata[s].last_active_iter) <= k2`),
///   or
/// - the slot was generated in the current iteration
///   (`pool.metadata[s].iteration_generated == current_iteration`) — these cuts
///   have not been tested yet and are always seeded (mirroring the
///   current-iteration protection in `cut_selection`).
///
/// `saturating_sub` is required because `last_active_iter` can exceed
/// `current_iteration` for current-iteration cuts; plain subtraction would
/// underflow.
///
/// Ascending slot order makes the result deterministic and declaration-order
/// invariant. Allocates nothing beyond growth of `out` (the caller pre-reserves
/// to `pool.populated_count`).
pub fn build_initial_resident_set(
    pool: &CutPool,
    current_iteration: u64,
    k2: u32,
    out: &mut Vec<u32>,
) {
    debug_assert!(
        pool.metadata.len() >= pool.populated_count,
        "build_initial_resident_set: metadata.len() {} < populated_count {}",
        pool.metadata.len(),
        pool.populated_count,
    );

    out.clear();
    let window = u64::from(k2);
    #[allow(clippy::cast_possible_truncation)]
    for s in 0..pool.populated_count {
        if !pool.active[s] {
            continue;
        }
        let meta = &pool.metadata[s];
        let within_window = current_iteration.saturating_sub(meta.last_active_iter) <= window;
        let is_current_iter = meta.iteration_generated == current_iteration;
        if within_window || is_current_iter {
            out.push(s as u32);
        }
    }
}

#[cfg(test)]
#[allow(clippy::doc_markdown)]
mod tests {
    use cobre_solver::{
        ActiveProfile, ActiveSolver, Basis, ProfiledSolver, RowBatch, SolverError, SolverInterface,
        SolverStatistics, StageTemplate,
    };

    use super::{
        DcsParams, DcsScoringScratch, DcsSolveContext, DcsSolveScratch, build_initial_resident_set,
        lazy_solve_preloaded, score_violated_candidates,
    };
    use crate::cut::{CutPool, CutRowMap};
    use crate::cut_selection::{CutMetadata, CutSelectionStrategy};
    use crate::indexer::StageIndexer;

    #[test]
    fn default_matches_spec() {
        let params = DcsParams::default();
        assert_eq!(
            params,
            DcsParams {
                k1: None,
                k2: 5,
                nadic: 10,
                epsilon_viol: 1e-10,
                start_iteration: 2,
                max_inner_iterations: 50,
            }
        );
        assert_eq!(params.k1, None);
    }

    #[test]
    fn from_strategy_dynamic_copies_fields() {
        // k1 = Some(n)
        let strategy = CutSelectionStrategy::Dynamic {
            k1: Some(20),
            k2: 7,
            nadic: 3,
            epsilon_viol: 1e-9,
            start_iteration: 4,
        };
        let params = DcsParams::from_strategy(&strategy)
            .expect("from_strategy must return Some for the Dynamic variant");
        assert_eq!(
            params,
            DcsParams {
                k1: Some(20),
                k2: 7,
                nadic: 3,
                epsilon_viol: 1e-9,
                start_iteration: 4,
                max_inner_iterations: 50,
            }
        );

        // k1 = None passes through as None.
        let strategy_inf = CutSelectionStrategy::Dynamic {
            k1: None,
            k2: 5,
            nadic: 10,
            epsilon_viol: 1e-10,
            start_iteration: 2,
        };
        let params_inf = DcsParams::from_strategy(&strategy_inf)
            .expect("from_strategy must return Some for the Dynamic variant");
        assert_eq!(params_inf.k1, None);
    }

    #[test]
    fn from_strategy_non_dynamic_is_none() {
        let level1 = CutSelectionStrategy::Level1 {
            check_frequency: 5,
            tie_tolerance: 1e-10,
        };
        assert!(DcsParams::from_strategy(&level1).is_none());

        let dominated = CutSelectionStrategy::Dominated {
            threshold: 1e-6,
            check_frequency: 10,
        };
        assert!(DcsParams::from_strategy(&dominated).is_none());
    }

    #[test]
    fn is_active_threshold() {
        let params = DcsParams {
            start_iteration: 2,
            ..DcsParams::default()
        };
        assert_eq!(
            [
                params.is_active(0),
                params.is_active(1),
                params.is_active(2),
                params.is_active(3),
            ],
            [false, false, true, true]
        );
    }

    #[test]
    fn dcs_params_is_copy() {
        fn assert_copy<T: Copy>() {}
        assert_copy::<DcsParams>();
    }

    // -----------------------------------------------------------------------
    // score_violated_candidates fixtures
    // -----------------------------------------------------------------------

    // All scoring tests use n_state = 2 (StageIndexer::new(2, 0)):
    //   - state columns 0, 1 (identity state_to_lp_column for j < hydro_count)
    //   - theta column 6 (= n * (3 + l) with n = 2, l = 0)
    // So `primal` must be at least length 7.
    const N_STATE: usize = 2;
    const THETA_COL: usize = 6;
    const PRIMAL_LEN: usize = THETA_COL + 1;

    fn indexer() -> StageIndexer {
        StageIndexer::new(2, 0)
    }

    /// A pool with capacity 16, state_dimension 2, forward_passes 16, no
    /// warm-start. `add_cut(0, slot, ..)` then maps to slot `slot`.
    fn empty_pool() -> CutPool {
        CutPool::new(16, N_STATE, 16, 0)
    }

    /// Insert a cut at `slot` with `intercept` and `coeffs`, and set its
    /// `iteration_generated`. Returns nothing; mutates `pool`.
    fn add(pool: &mut CutPool, slot: u32, intercept: f64, coeffs: &[f64], iter_generated: u64) {
        pool.add_cut(0, slot, intercept, coeffs);
        pool.metadata[slot as usize].iteration_generated = iter_generated;
    }

    /// An empty resident map (no slot is in the LP). base_row_offset is
    /// irrelevant to `lp_row_for_slot`, which returns `None` for every slot.
    fn empty_resident() -> CutRowMap {
        CutRowMap::new(16, 0)
    }

    fn params(nadic: u32, epsilon_viol: f64, k1: Option<u32>) -> DcsParams {
        DcsParams {
            k1,
            nadic,
            epsilon_viol,
            ..DcsParams::default()
        }
    }

    // -----------------------------------------------------------------------
    // score_violated_candidates tests
    // -----------------------------------------------------------------------

    /// AC1: two non-resident active cuts, violations 5.0 (slot A) and 2.0
    /// (slot B). With coeffs [0,0] and theta_raw = 0, alpha == intercept, so
    /// the intercepts ARE the violations. nadic = 10 → both selected, ordered
    /// descending.
    #[test]
    fn scores_and_orders_two_violated_descending() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], 1); // slot 0: violation 5.0
        add(&mut pool, 1, 2.0, &[0.0, 0.0], 1); // slot 1: violation 2.0
        let primal = vec![0.0_f64; PRIMAL_LEN]; // x* = 0, theta_raw = 0
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 2);
        assert_eq!(
            out,
            vec![0, 1],
            "descending violation: slot 0 (5) then slot 1 (2)"
        );
    }

    /// AC2: same pool, nadic = 1. Full violated count is still 2, but only the
    /// top-1 slot is selected.
    #[test]
    fn respects_nadic_cap_returns_full_violated_count() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], 1);
        add(&mut pool, 1, 2.0, &[0.0, 0.0], 1);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(1, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 2, "full violated count regardless of nadic");
        assert_eq!(out, vec![0], "top-1 only");
    }

    /// AC3: equal violation 3.0 at slots 7 and 4, nadic = 1 → ascending slot id
    /// tie-break selects slot 4.
    #[test]
    fn tie_break_ascending_slot_id() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 7, 3.0, &[0.0, 0.0], 1);
        add(&mut pool, 4, 3.0, &[0.0, 0.0], 1);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(1, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 2);
        assert_eq!(out, vec![4], "equal violation → ascending slot id wins");
    }

    /// AC4: col_scale sign-flip. col_scale[theta] = 2.0, state-column scale 0.5.
    /// One cut: intercept 0.5, coeff [1.0, 0.0]. Scaled primal: x_scaled[0] = 2.0,
    /// theta_scaled = 1.0.
    ///   raw:    x_raw[0] = 0.5 * 2.0 = 1.0; theta_raw = 2.0 * 1.0 = 2.0.
    ///           alpha = 0.5 + 1.0*1.0 = 1.5; v = 1.5 - 2.0 = -0.5  → NOT violated.
    ///   if (buggy) NO unscaling: x[0] = 2.0; theta = 1.0.
    ///           alpha = 0.5 + 1.0*2.0 = 2.5; v = 2.5 - 1.0 = 1.5  → violated.
    /// The correct (unscaled) verdict is "not violated"; the verdict flips sign
    /// vs the unscaled-input bug.
    #[test]
    fn applies_col_scale_unscaling() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 0.5, &[1.0, 0.0], 1);
        let mut primal = vec![0.0_f64; PRIMAL_LEN];
        primal[0] = 2.0; // scaled x[0]
        primal[THETA_COL] = 1.0; // scaled theta
        // col_scale must cover all columns up to theta (index 6).
        let mut col_scale = vec![1.0_f64; PRIMAL_LEN];
        col_scale[0] = 0.5; // state column 0 scale
        col_scale[THETA_COL] = 2.0; // theta scale
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &col_scale,
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(
            count, 0,
            "with correct col_scale unscaling, v = -0.5 → not violated"
        );
        assert!(out.is_empty());
    }

    /// AC5: a resident slot is never selected, even when violated.
    #[test]
    fn skips_resident_slots() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], 1); // violated, but will be resident
        add(&mut pool, 1, 2.0, &[0.0, 0.0], 1); // violated, non-resident
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let mut resident = empty_resident();
        resident.insert(0); // slot 0 is in the LP
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 1, "resident slot 0 is excluded from scoring");
        assert_eq!(out, vec![1]);
    }

    /// AC6: k1 window. Two violated cuts: slot 0 generated at iteration 8
    /// (age 2), slot 1 generated at iteration 2 (age 8). current_iteration = 10,
    /// k1 = Some(5) → only the age-2 cut (slot 0) is in window. With k1 = None
    /// both are eligible.
    #[test]
    fn k1_window_filters_old_cuts() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], 8); // age 2 at iter 10
        add(&mut pool, 1, 2.0, &[0.0, 0.0], 2); // age 8 at iter 10
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        // Finite window k1 = 5: slot 1 (age 8 >= 5) skipped entirely.
        let p_finite = params(10, 1e-10, Some(5));
        let count_finite = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p_finite,
            10,
            &mut scratch,
            &mut out,
        );
        assert_eq!(count_finite, 1, "only the in-window cut is counted");
        assert_eq!(out, vec![0]);

        // k1 = None: exactness path, both eligible.
        let p_inf = params(10, 1e-10, None);
        let count_inf = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p_inf,
            10,
            &mut scratch,
            &mut out,
        );
        assert_eq!(count_inf, 2, "k1 = None ⇒ all cuts eligible");
        assert_eq!(out, vec![0, 1]);
    }

    /// k1 window keeps warm-start cuts (iteration_generated == u64::MAX) inside
    /// any finite window via saturating_sub (age 0).
    #[test]
    fn k1_window_keeps_warm_start_cuts() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], u64::MAX); // warm-start sentinel
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1e-10, Some(1)); // tightest finite window
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 1, "warm-start cut (age 0) is always in window");
        assert_eq!(out, vec![0]);
    }

    /// AC7: violation exactly equal to epsilon_viol is NOT counted (strict >).
    #[test]
    fn epsilon_viol_is_strict() {
        let idx = indexer();
        let mut pool = empty_pool();
        // alpha = 1.0, theta_raw = 0 → v = 1.0; epsilon_viol = 1.0 → v == eps.
        add(&mut pool, 0, 1.0, &[0.0, 0.0], 1);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1.0, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 0, "v == epsilon_viol is NOT strictly greater");
        assert!(out.is_empty());
    }

    /// No candidate violated → empty out_selected, return 0.
    #[test]
    fn no_violation_returns_empty() {
        let idx = indexer();
        let mut pool = empty_pool();
        // alpha = -1.0 < theta_raw = 0 → v = -1.0, not violated.
        add(&mut pool, 0, -1.0, &[0.0, 0.0], 1);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 0);
        assert!(out.is_empty());
    }

    // -----------------------------------------------------------------------
    // Batched-scoring tests: bit-identical vs per-row reference,
    // single GEMM per pass, growth-only scratch.
    // -----------------------------------------------------------------------

    use crate::gemm::gemm_block;

    /// Deterministic splitmix64 PRNG (no external rand dep in unit tests), with a
    /// helper that draws a finite f64 in roughly [-1.5, 0.5). Mirrors the
    /// `benches/cut_selection_kernel.rs` generator so the randomized fixture is
    /// reproducible.
    fn splitmix64(state: &mut u64) -> u64 {
        *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = *state;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    fn draw_f64(state: &mut u64) -> f64 {
        let r = splitmix64(state);
        let bits = (r >> 12) & ((1_u64 << 52) - 1);
        f64::from_bits((1023_u64 << 52) | bits) - 1.5
    }

    /// Per-row reference for the batched scorer: replays the EXACT filter / gather
    /// order of `score_violated_candidates`, but scores each surviving candidate
    /// with its own `gemm_block(coef, x*, 1, n_state, 1, ..)` call (the old
    /// per-candidate path). Returns `(alpha_bits_in_gather_order, out_selected)`.
    #[allow(clippy::cast_possible_truncation)]
    fn per_row_reference(
        pool: &CutPool,
        idx: &StageIndexer,
        primal: &[f64],
        col_scale: &[f64],
        resident: &CutRowMap,
        p: &DcsParams,
        current_iteration: u64,
    ) -> (Vec<u64>, Vec<u32>) {
        let n_state = idx.n_state;
        let theta = idx.theta;
        let theta_raw = if col_scale.is_empty() {
            primal[theta]
        } else {
            col_scale[theta] * primal[theta]
        };
        let mut unscaled_state = Vec::with_capacity(n_state);
        for j in 0..n_state {
            let c = idx.state_to_lp_column(j);
            let x_raw = if col_scale.is_empty() {
                primal[c]
            } else {
                col_scale[c] * primal[c]
            };
            unscaled_state.push(x_raw);
        }

        let mut alpha_bits = Vec::new();
        let mut violations: Vec<(f64, u32)> = Vec::new();
        for (slot, intercept, coefficients) in pool.active_cuts() {
            if let Some(k1) = p.k1 {
                let age = current_iteration.saturating_sub(pool.metadata[slot].iteration_generated);
                if age >= u64::from(k1) {
                    continue;
                }
            }
            if resident.lp_row_for_slot(slot).is_some() {
                continue;
            }
            let mut dot = [0.0_f64; 1];
            gemm_block(coefficients, &unscaled_state, 1, n_state, 1, &mut dot);
            // Record the GEMM's raw activity (∇·x*_raw), matching what the
            // batched scorer stores in `scratch.alpha` (intercept added after).
            alpha_bits.push(dot[0].to_bits());
            let alpha = intercept + dot[0];
            let v = alpha - theta_raw;
            if v > p.epsilon_viol {
                violations.push((v, slot as u32));
            }
        }
        violations.sort_unstable_by(|a, b| b.0.total_cmp(&a.0).then(a.1.cmp(&b.1)));
        let take = (p.nadic as usize).min(violations.len());
        let out_selected = violations[..take].iter().map(|&(_, s)| s).collect();
        (alpha_bits, out_selected)
    }

    /// Build a randomized capacity-`cap`, state-dim-2 pool: every slot active,
    /// random intercept + coefficients, random `iteration_generated` in
    /// `[1, 12]`. Deterministic in `seed`.
    #[allow(clippy::cast_possible_truncation)]
    fn random_pool(cap: usize, seed: u64) -> CutPool {
        let mut pool = CutPool::new(cap, N_STATE, cap as u32, 0);
        let mut state = seed;
        for slot in 0..cap {
            let intercept = draw_f64(&mut state);
            let coeffs = [draw_f64(&mut state), draw_f64(&mut state)];
            pool.add_cut(0, slot as u32, intercept, &coeffs);
            // iteration_generated in [1, 12].
            let gen_iter = 1 + (splitmix64(&mut state) % 12);
            pool.metadata[slot].iteration_generated = gen_iter;
        }
        pool
    }

    /// AC1: the batched scorer's per-candidate activities (`scratch.alpha`, by
    /// bits) and its `out_selected` are bit-identical to a per-row reference that
    /// scores each candidate with an individual `gemm_block(.., 1, n_state, 1, ..)`
    /// call — over a randomized pool with a non-trivial primal and col_scale, a
    /// mix of resident / non-resident slots, and a finite k1 window.
    #[test]
    fn batched_scoring_bit_identical_to_per_row_reference() {
        let idx = indexer();
        let cap = 64;
        let pool = random_pool(cap, 0x0BAD_F00D_C0FF_EE11);

        // Non-trivial primal: scaled x and theta drawn from the same PRNG stream.
        let mut prng = 0xDEAD_BEEF_1234_5678_u64;
        let mut primal = vec![0.0_f64; PRIMAL_LEN];
        for v in &mut primal {
            *v = draw_f64(&mut prng);
        }
        // Per-column scale covering every column up to theta.
        let mut col_scale = vec![1.0_f64; PRIMAL_LEN];
        for v in &mut col_scale {
            *v = 0.5 + (draw_f64(&mut prng) + 1.5) * 0.5; // strictly positive
        }
        // Drive theta_raw strongly negative so most candidates are violated
        // (v = alpha - theta_raw > 0): this makes the selection/sort/nadic path a
        // non-vacuous witness, not just the all-empty case. col_scale[theta] > 0,
        // so a negative scaled theta yields a negative theta_raw.
        primal[THETA_COL] = -50.0;

        // Mark roughly every third slot resident.
        let mut resident = CutRowMap::new(cap, 0);
        for slot in (0..cap).step_by(3) {
            resident.insert(slot);
        }

        let current_iteration = 10;
        // Finite k1 window so the recency filter is exercised alongside residency.
        let p = params(7, 1e-9, Some(5));

        let (ref_alpha_bits, ref_selected) = per_row_reference(
            &pool,
            &idx,
            &primal,
            &col_scale,
            &resident,
            &p,
            current_iteration,
        );

        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();
        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &col_scale,
            &resident,
            &p,
            current_iteration,
            &mut scratch,
            &mut out,
        );

        // The gathered candidate count must match the reference candidate count.
        assert_eq!(
            scratch.alpha.len(),
            ref_alpha_bits.len(),
            "batched and per-row reference must gather the same candidate set"
        );
        // Activities bit-identical, gather-order for gather-order.
        let batched_alpha_bits: Vec<u64> = scratch.alpha.iter().map(|a| a.to_bits()).collect();
        assert_eq!(
            batched_alpha_bits, ref_alpha_bits,
            "batched alpha must be bit-identical to the per-row reference"
        );
        // Selected slot ids AND order bit-identical.
        assert_eq!(
            out, ref_selected,
            "batched out_selected must match the per-row reference exactly"
        );
        assert!(count >= out.len());

        // Witness guard: the fixture must actually exercise the batched path —
        // multiple candidates gathered, at least one violation selected, and the
        // nadic cap engaged — so equality is non-vacuous.
        assert!(
            scratch.alpha.len() > 1,
            "fixture must gather >1 candidate (got {})",
            scratch.alpha.len()
        );
        assert!(
            !out.is_empty(),
            "fixture must select at least one violation"
        );
        assert!(
            count > out.len(),
            "fixture must have more violations ({count}) than the nadic cap ({})",
            out.len()
        );
    }

    /// AC2: a scoring pass issues exactly ONE batched GEMM over all `k` eligible
    /// candidates, not `k` single-row calls. Observed via the gather buffers: all
    /// `k` candidates are in `cand_slots` / `cand_coef_block` (k_rows × n_state)
    /// and `alpha` holds exactly `k` activities after the single call.
    #[test]
    fn batched_scoring_single_gemm_per_pass() {
        let idx = indexer();
        let mut pool = empty_pool();
        // Three active, non-resident, in-window candidates (k = 3).
        add(&mut pool, 0, 5.0, &[1.0, 0.0], 10);
        add(&mut pool, 1, 2.0, &[0.0, 1.0], 10);
        add(&mut pool, 2, 9.0, &[0.5, 0.5], 10);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let _ = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        let k = 3;
        assert_eq!(scratch.cand_slots.len(), k, "all k candidates gathered");
        assert_eq!(
            scratch.cand_slots,
            vec![0, 1, 2],
            "ascending-slot gather order"
        );
        assert_eq!(
            scratch.cand_coef_block.len(),
            k * N_STATE,
            "coef block is exactly k_rows × n_state — one batched GEMM input"
        );
        assert_eq!(
            scratch.alpha.len(),
            k,
            "alpha holds exactly k activities from the single batched GEMM"
        );
    }

    /// AC2 (filtered): only the eligible (non-resident, in-window) candidates are
    /// gathered into the single batched GEMM; filtered-out slots never enter the
    /// gather buffers.
    #[test]
    fn batched_scoring_gathers_only_eligible() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[0.0, 0.0], 8); // age 2 — in window
        add(&mut pool, 1, 2.0, &[0.0, 0.0], 2); // age 8 — OUT of window (skipped)
        add(&mut pool, 2, 9.0, &[0.0, 0.0], 9); // age 1 — in window, but resident
        add(&mut pool, 3, 4.0, &[0.0, 0.0], 9); // age 1 — in window, eligible
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let mut resident = empty_resident();
        resident.insert(2); // slot 2 resident
        let p = params(10, 1e-10, Some(5)); // k1 = 5
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let _ = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        // Only slots 0 and 3 survive both filters; the single GEMM scores them.
        assert_eq!(
            scratch.cand_slots,
            vec![0, 3],
            "out-of-window (1) and resident (2) slots excluded from the gather"
        );
        assert_eq!(scratch.alpha.len(), 2);
    }

    /// AC2 (empty): an empty candidate set produces an empty gather and no
    /// activities — the single GEMM is a no-op (k_rows = 0).
    #[test]
    fn batched_scoring_empty_candidates_no_op() {
        let idx = indexer();
        let pool = empty_pool(); // no cuts at all
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 0);
        assert!(scratch.cand_slots.is_empty());
        assert!(scratch.cand_coef_block.is_empty());
        assert!(scratch.alpha.is_empty());
        assert!(out.is_empty());
    }

    /// AC2 (all-resident): every active cut is resident → nothing is gathered and
    /// the single GEMM is a no-op, matching the empty-candidate behavior.
    #[test]
    fn batched_scoring_all_resident_no_candidates() {
        let idx = indexer();
        let mut pool = empty_pool();
        add(&mut pool, 0, 5.0, &[1.0, 0.0], 10);
        add(&mut pool, 1, 2.0, &[0.0, 1.0], 10);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let mut resident = empty_resident();
        resident.insert(0);
        resident.insert(1);
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        let count = score_violated_candidates(
            &pool,
            &idx,
            &primal,
            &[],
            &resident,
            &p,
            10,
            &mut scratch,
            &mut out,
        );

        assert_eq!(count, 0, "all candidates resident → none scored");
        assert!(scratch.cand_slots.is_empty(), "all-resident → empty gather");
        assert!(scratch.alpha.is_empty());
        assert!(out.is_empty());
    }

    /// AC3: the new scratch buffers are growth-only. After a `reserve` sized to
    /// the pool, repeated scoring passes never reallocate — capacities are stable
    /// and the per-pass `clear()` keeps lengths bounded by the eligible count.
    #[test]
    fn batched_scoring_scratch_is_growth_only() {
        let idx = indexer();
        let cap = 32;
        let pool = random_pool(cap, 0x00C0_FFEE_0BAD_CAFE);
        let primal = vec![0.0_f64; PRIMAL_LEN];
        let resident = empty_resident();
        let p = params(10, 1e-10, None);
        let mut scratch = DcsScoringScratch::default();
        let mut out = Vec::new();

        // Pre-reserve to the pool worst case.
        scratch.reserve(N_STATE, cap);
        let coef_cap = scratch.cand_coef_block.capacity();
        let alpha_cap = scratch.alpha.capacity();
        let slots_cap = scratch.cand_slots.capacity();
        let viol_cap = scratch.violations.capacity();
        let state_cap = scratch.unscaled_state.capacity();
        assert!(
            coef_cap >= cap * N_STATE,
            "coef block reserved to cap*n_state"
        );
        assert!(alpha_cap >= cap);
        assert!(slots_cap >= cap);

        // Several passes must not grow any capacity (no steady-state allocation).
        for _ in 0..5 {
            let _ = score_violated_candidates(
                &pool,
                &idx,
                &primal,
                &[],
                &resident,
                &p,
                10,
                &mut scratch,
                &mut out,
            );
            assert_eq!(scratch.cand_coef_block.capacity(), coef_cap);
            assert_eq!(scratch.alpha.capacity(), alpha_cap);
            assert_eq!(scratch.cand_slots.capacity(), slots_cap);
            assert_eq!(scratch.violations.capacity(), viol_cap);
            assert_eq!(scratch.unscaled_state.capacity(), state_cap);
        }
    }

    // -----------------------------------------------------------------------
    // lazy_solve fixtures
    // -----------------------------------------------------------------------

    // Synthetic LP for StageIndexer::new(1, 0): n_state = 1, theta = col 3.
    //   Columns: 0 = state x0 (pinned to 2.0), 1 and 2 fixed to 0, 3 = theta.
    //   No structural rows (num_rows = 0) — cuts are the only rows.
    //   Objective: minimize theta. So at the optimum theta equals the max cut
    //   floor `intercept + coeff*x0` over the resident cuts.
    const STATE_X0: f64 = 2.0;
    const LAZY_THETA_COL: usize = 3;

    fn lazy_indexer() -> StageIndexer {
        StageIndexer::new(1, 0)
    }

    /// Cut-free core template with x0 pinned to `STATE_X0` and theta free.
    fn core_template() -> StageTemplate {
        StageTemplate {
            num_cols: 4,
            num_rows: 0,
            num_nz: 0,
            col_starts: vec![0_i32; 5], // num_cols + 1, all zero (no structural nz)
            row_indices: Vec::new(),
            values: Vec::new(),
            // x0 pinned to STATE_X0; cols 1,2 fixed to 0; theta free (bounded box).
            col_lower: vec![STATE_X0, 0.0, 0.0, -1.0e6],
            col_upper: vec![STATE_X0, 0.0, 0.0, 1.0e6],
            objective: vec![0.0, 0.0, 0.0, 1.0], // minimize theta
            row_lower: Vec::new(),
            row_upper: Vec::new(),
            n_state: 1,
            n_transfer: 0,
            n_dual_relevant: 1,
            n_hydro: 1,
            max_par_order: 0,
            col_scale: Vec::new(),
            row_scale: Vec::new(),
        }
    }

    /// Three cuts (state_dim = 1), floors at x0 = 2.0:
    ///   slot 0: intercept 1, coeff [0]   → floor 1.0
    ///   slot 1: intercept 0, coeff [1]   → floor 2.0
    ///   slot 2: intercept 5, coeff [0]   → floor 5.0  (the binding cut)
    /// All-cuts optimum: theta = 5.0, objective = 5.0; binding slot = 2.
    fn make_three_cut_pool() -> CutPool {
        let mut pool = CutPool::new(16, 1, 16, 0);
        pool.add_cut(0, 0, 1.0, &[0.0]);
        pool.add_cut(0, 1, 0.0, &[1.0]);
        pool.add_cut(0, 2, 5.0, &[0.0]);
        for slot in 0..3 {
            pool.metadata[slot].iteration_generated = 1;
        }
        pool
    }

    /// Four cuts (state_dim = 1) whose binding cut depends on x0:
    ///   slot 0: intercept 1, coeff [0]   → floor 1
    ///   slot 1: intercept 0, coeff [1]   → floor x0
    ///   slot 2: intercept 5, coeff [0]   → floor 5
    ///   slot 3: intercept 0, coeff [2]   → floor 2·x0
    /// At x0 = 2 the binding cut is slot 2 (floor 5); at x0 = 10 it is slot 3
    /// (floor 20). Used to exercise the opening-reuse continue path.
    fn make_four_cut_pool() -> CutPool {
        let mut pool = CutPool::new(16, 1, 16, 0);
        pool.add_cut(0, 0, 1.0, &[0.0]);
        pool.add_cut(0, 1, 0.0, &[1.0]);
        pool.add_cut(0, 2, 5.0, &[0.0]);
        pool.add_cut(0, 3, 0.0, &[2.0]);
        for slot in 0..4 {
            pool.metadata[slot].iteration_generated = 1;
        }
        pool
    }

    fn active_profiled() -> ProfiledSolver<ActiveSolver> {
        ProfiledSolver::new(ActiveSolver::new().expect("ActiveSolver::new()"))
    }

    fn ctx() -> DcsSolveContext {
        DcsSolveContext {
            stage_index: 0,
            scenario_index: 0,
            iteration: Some(10),
            continue_carry: false,
        }
    }

    fn lazy_params(nadic: u32, max_inner: u32) -> DcsParams {
        DcsParams {
            k1: None,
            nadic,
            max_inner_iterations: max_inner,
            ..DcsParams::default()
        }
    }

    /// Reference all-cuts optimum: load core, append every active slot, solve.
    fn solve_all_cuts(pool: &CutPool, indexer: &StageIndexer) -> (f64, f64) {
        let mut solver = active_profiled();
        let core = core_template();
        solver.load_model(&core);
        let mut row_map = CutRowMap::new(pool.populated_count, core.num_rows);
        let mut batch = RowBatch {
            num_rows: 0,
            row_starts: Vec::new(),
            col_indices: Vec::new(),
            values: Vec::new(),
            row_lower: Vec::new(),
            row_upper: Vec::new(),
        };
        let all_slots: Vec<u32> = (0..pool.populated_count as u32).collect();
        crate::cut::row::append_slots_to_lp(
            &mut solver,
            pool,
            &all_slots,
            indexer,
            &[],
            &mut row_map,
            &mut batch,
        );
        let view = solver.solve(None).expect("all-cuts solve must succeed");
        (view.objective, view.primal[LAZY_THETA_COL])
    }

    // -----------------------------------------------------------------------
    // lazy_solve tests (real solver)
    // -----------------------------------------------------------------------

    /// Headline exactness gate: lazy_solve with an initial subset that omits the
    /// binding cut converges to the all-cuts optimum.
    #[test]
    fn lazy_solve_exact_matches_all_cuts() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let (all_obj, all_theta) = solve_all_cuts(&pool, &indexer);

        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        // Caller owns the model load (and, in production, any bounds patch).
        solver.load_model(&core);
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1], // omit binding slot 2
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must succeed");
        let view = scratch.result_view();

        assert!(
            (view.objective - all_obj).abs() < 1e-9,
            "objective {} != all-cuts {all_obj}",
            view.objective
        );
        assert!(
            (view.primal[LAZY_THETA_COL] - all_theta).abs() < 1e-9,
            "theta {} != all-cuts {all_theta}",
            view.primal[LAZY_THETA_COL]
        );
        // Bit-identical with trivial epsilon/scale.
        assert_eq!(view.objective, all_obj);
        assert_eq!(view.primal[LAZY_THETA_COL], all_theta);
    }

    /// With an initial subset that already contains the binding cut, the loop
    /// adds no further rows and returns after the first solve.
    ///
    /// AC1: the no-violation terminating path issues EXACTLY ONE LP solve (the
    /// initial solve), not two — the redundant exit re-solve is eliminated. The
    /// resident subset already reproduces the all-cuts optimum, so the live view
    /// is copied into the result buffers and returned with no re-solve.
    #[test]
    fn lazy_solve_no_violation_stops_immediately() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        // Initial subset includes the binding slot 2.
        solver.load_model(&core);
        let solves_before = solver.statistics().solve_count;
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1, 2],
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must succeed");
        let solve_delta = solver.statistics().solve_count - solves_before;
        let view = scratch.result_view();

        // The optimum is the binding cut floor; no growth needed.
        assert_eq!(view.primal[LAZY_THETA_COL], 5.0);
        // out_selected from the (single) scoring pass that found no violation.
        assert!(scratch.out_selected.is_empty());
        // AC1: exactly ONE solve (was 2 before the redundant exit re-solve was
        // removed): the initial solve, then the no-violation copy-and-return.
        assert_eq!(
            solve_delta, 1,
            "no-violation path must issue exactly 1 solve (no redundant exit re-solve)"
        );
    }

    /// Omitting the binding cut forces at least one inner add_rows, and the
    /// final theta reflects the now-resident binding cut.
    ///
    /// AC2: the one-addition path issues EXACTLY TWO LP solves (was 3: initial +
    /// add/resolve + redundant exit) — the initial solve, then the add-and-resolve
    /// whose rescore finds no violation and copies-and-returns with no third solve.
    #[test]
    fn lazy_solve_grows_to_include_binding_cut() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        solver.load_model(&core);
        let solves_before = solver.statistics().solve_count;
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1], // omit binding slot 2
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must succeed");
        let solve_delta = solver.statistics().solve_count - solves_before;
        let view = scratch.result_view();

        assert_eq!(
            view.primal[LAZY_THETA_COL], 5.0,
            "final theta must reflect the added binding cut"
        );
        // AC2: exactly TWO solves (initial + one add/resolve), no redundant exit.
        assert_eq!(
            solve_delta, 2,
            "one-addition path must issue exactly 2 solves (no redundant exit re-solve)"
        );
    }

    /// Opening-reuse continue path: load the cut-free core ONCE, solve a first
    /// "opening" fresh at x0 = 2, then re-pin x0 = 10 and solve a second opening
    /// by CONTINUING (`continue_carry = true`) — reusing the loaded LP, the
    /// carried resident cut rows, and the warm basis from the first solve. Each
    /// opening must converge to the all-cuts optimum for its own pinned state,
    /// and the second must add the cut (slot 3) that binds only at x0 = 10 on top
    /// of the carried residents {0,1,2}. This is the now-mandatory backward
    /// opening-reuse mechanism (ReuseContinue) exercised in isolation.
    ///
    /// AC3: a third opening C continues at x0 = 2 where the binding cut (slot 2)
    /// is ALREADY resident from the carried set, so the lazy loop adds nothing and
    /// must issue EXACTLY ONE solve on the continue-carry no-add path.
    #[test]
    fn lazy_solve_continue_carry_exact_across_openings() {
        let indexer = lazy_indexer();
        let pool = make_four_cut_pool();
        let core = core_template();
        let params = lazy_params(10, 50);
        let mut solver = active_profiled();
        // ONE scratch + ONE loaded model shared across the openings: the row map
        // and warm basis must persist across the continue calls.
        let mut scratch = DcsSolveScratch::default();
        solver.load_model(&core);

        // Opening A (x0 = 2, fresh): seed omits the binding slot 2; the lazy loop
        // discovers and adds it. All-cuts optimum at x0 = 2 is theta = 5.
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1],
            &params,
            &mut scratch,
            DcsSolveContext {
                continue_carry: false,
                ..ctx()
            },
        )
        .expect("opening A (fresh) must solve");
        assert_eq!(
            scratch.result_view().primal[LAZY_THETA_COL],
            5.0,
            "opening A theta (x0=2)"
        );

        // Re-pin the incoming state to x0 = 10 (what `patch_opening_bounds` does
        // per opening) — only the bounds change; the loaded LP and the carried
        // resident cut rows persist.
        solver.set_col_bounds(&[0], &[10.0], &[10.0]);

        // Opening B (x0 = 10, continue): no reload, no re-seed; warm-carry A's LP
        // and basis. The binding cut at x0 = 10 is slot 3 (floor 20), which is NOT
        // resident after A, so the continue loop must add it. All-cuts optimum at
        // x0 = 10 is theta = 20.
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[], // ignored in continue mode
            &params,
            &mut scratch,
            DcsSolveContext {
                continue_carry: true,
                ..ctx()
            },
        )
        .expect("opening B (continue) must solve");
        assert_eq!(
            scratch.result_view().primal[LAZY_THETA_COL],
            20.0,
            "opening B theta (x0=10) must reach the all-cuts optimum via the \
             carried-LP continue path (slot 3 added on top of the carried set)"
        );

        // Opening C (x0 = 2 again, continue, NO add): re-pin back to x0 = 2 where
        // the binding cut (slot 2, floor 5) is already resident from A. The
        // continue loop finds no violation and must issue EXACTLY ONE solve — the
        // continue-carry no-add path (AC3).
        solver.set_col_bounds(&[0], &[2.0], &[2.0]);
        let solves_before = solver.statistics().solve_count;
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[], // ignored in continue mode
            &params,
            &mut scratch,
            DcsSolveContext {
                continue_carry: true,
                ..ctx()
            },
        )
        .expect("opening C (continue, no add) must solve");
        let solve_delta = solver.statistics().solve_count - solves_before;
        assert_eq!(
            scratch.result_view().primal[LAZY_THETA_COL],
            5.0,
            "opening C theta (x0=2) reverts to the carried binding cut (slot 2)"
        );
        // AC3: continue-carry no-add path issues exactly ONE solve.
        assert_eq!(
            solve_delta, 1,
            "continue-carry no-add opening must issue exactly 1 solve (no redundant exit re-solve)"
        );
    }

    // -----------------------------------------------------------------------
    // scoring-time instrumentation tests
    // -----------------------------------------------------------------------

    /// A freshly-constructed scratch starts with a zero scoring-time accumulator.
    #[test]
    fn scoring_time_default_is_zero() {
        let scratch = DcsSolveScratch::default();
        assert_eq!(scratch.scoring_time_seconds, 0.0);
    }

    /// A solve whose seed omits the binding cut runs the inner loop (>= 2 scoring
    /// passes) and strictly increases the cumulative scoring accumulator.
    #[test]
    fn scoring_time_increases_when_inner_loop_runs() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        let before = scratch.scoring_time_seconds;
        solver.load_model(&core);
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1], // omit binding slot 2 → inner loop runs
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must succeed");

        assert!(
            scratch.scoring_time_seconds > before,
            "scoring accumulator {} must exceed its pre-call value {before}",
            scratch.scoring_time_seconds
        );
    }

    /// The rows-in-LP accumulators track the resident cut-row count per solve:
    /// they start at zero, fold one term per solve (cumulative, never reset
    /// between solves on the same scratch), and `max` tracks the peak. Seeding
    /// all three cuts resident gives a resident set of 3 with no growth, so two
    /// fresh solves on the same scratch yield count == 2, sum == 6, max == 3.
    #[test]
    fn rows_in_lp_tracks_resident_set_size_per_solve() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        // Defaults are zero before any solve.
        assert_eq!(scratch.rows_in_lp_sum, 0);
        assert_eq!(scratch.rows_in_lp_count, 0);
        assert_eq!(scratch.rows_in_lp_max, 0);

        for _ in 0..2 {
            solver.load_model(&core);
            lazy_solve_preloaded(
                &mut solver,
                &core,
                &pool,
                &indexer,
                &[],
                None,
                &[0, 1, 2], // all three cuts resident from the seed → no growth
                &params,
                &mut scratch,
                ctx(),
            )
            .expect("lazy_solve_preloaded must succeed");
        }

        assert_eq!(scratch.rows_in_lp_count, 2, "one term folded per solve");
        assert_eq!(
            scratch.rows_in_lp_sum, 6,
            "3 resident cut rows per solve, summed over 2 solves"
        );
        assert_eq!(
            scratch.rows_in_lp_max, 3,
            "peak resident set is the 3 seeded cuts"
        );
    }

    /// A solve whose seed already contains the binding cut performs exactly one
    /// scoring pass (no growth): the accumulator increases by that single pass
    /// and the objective/theta result is unchanged from the pre-instrumentation
    /// behavior (still the binding-cut optimum).
    #[test]
    fn scoring_time_single_pass_result_unchanged() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        let before = scratch.scoring_time_seconds;
        solver.load_model(&core);
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[0, 1, 2], // binding cut already resident → single scoring pass, no growth
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must succeed");
        let view = scratch.result_view();

        // Single scoring pass still advances the accumulator.
        assert!(
            scratch.scoring_time_seconds >= before,
            "scoring accumulator must not decrease"
        );
        // Result identical to the pre-instrumentation single-pass behavior: the
        // binding-cut floor, no inner growth.
        assert_eq!(
            view.primal[LAZY_THETA_COL], 5.0,
            "single-pass optimum must be the binding-cut floor"
        );
        assert_eq!(view.objective, 5.0);
        assert!(
            scratch.out_selected.is_empty(),
            "no violation → no slots selected for growth"
        );
    }

    /// max_inner_iterations = 1 forces the TC fallback after one inner add; it
    /// must terminate and still return the all-cuts-equivalent optimum.
    ///
    /// AC5: the cap-hit fallback scores the live primal, appends remaining
    /// violated slots, and solves once to return — preserving the all-cuts
    /// optimum. The TC path issues initial + one capped add/resolve + the final
    /// TC solve = 3.
    #[test]
    fn lazy_solve_tc_fallback_terminates() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let (all_obj, all_theta) = solve_all_cuts(&pool, &indexer);

        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        // nadic = 1 + cap = 1: the loop can add at most one slot before the cap
        // fires, then the TC fallback adds the rest.
        let params = lazy_params(1, 1);

        solver.load_model(&core);
        let solves_before = solver.statistics().solve_count;
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[], // omit ALL cuts to force ≥2 inner iterations without the cap
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded TC fallback must succeed");
        let solve_delta = solver.statistics().solve_count - solves_before;
        let view = scratch.result_view();

        assert!((view.objective - all_obj).abs() < 1e-9);
        assert!((view.primal[LAZY_THETA_COL] - all_theta).abs() < 1e-9);
        // AC5: TC fallback solve count is preserved (initial + capped add + final).
        assert_eq!(
            solve_delta, 3,
            "TC fallback issues initial + one capped add/resolve + final TC solve = 3"
        );
    }

    /// Determinism: identical inputs → identical objective across two runs.
    #[test]
    fn lazy_solve_is_deterministic() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let core = core_template();
        let params = lazy_params(10, 50);

        let run = || {
            let mut solver = active_profiled();
            let mut scratch = DcsSolveScratch::default();
            solver.load_model(&core);
            lazy_solve_preloaded(
                &mut solver,
                &core,
                &pool,
                &indexer,
                &[],
                None,
                &[0],
                &params,
                &mut scratch,
                ctx(),
            )
            .expect("lazy_solve_preloaded must succeed");
            scratch.result_view().objective
        };

        assert_eq!(run(), run(), "objective must be deterministic");
    }

    /// AC6: the reused `res_*` result buffers are growth-only. Once warmed to the
    /// LP's solution size, repeated `lazy_solve_preloaded` calls refill them via
    /// `clear()` + `extend_from_slice` without reallocating — capacities are
    /// stable (no steady-state heap allocation on the result-copy path). Also
    /// pins that `result_view()` round-trips the solution faithfully.
    ///
    /// Each call reloads the cut-free core first, exactly as the production
    /// forward/backward/sim paths do per (stage, solve): without the reload the
    /// solver model would accumulate cut rows across calls and the dual length
    /// (and thus `res_dual`) would grow — a fixture artifact, not a real leak.
    #[test]
    fn lazy_solve_result_buffers_growth_only() {
        let indexer = lazy_indexer();
        let pool = make_three_cut_pool();
        let mut solver = active_profiled();
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        let call = |solver: &mut ProfiledSolver<ActiveSolver>, scratch: &mut DcsSolveScratch| {
            // Reload the cut-free core so each call starts from a clean row count
            // (mirrors the caller's per-(stage, solve) `load_model`).
            solver.load_model(&core);
            lazy_solve_preloaded(
                solver,
                &core,
                &pool,
                &indexer,
                &[],
                None,
                &[0, 1], // omit binding slot 2 → at least one add, fills res_* twice
                &params,
                scratch,
                ctx(),
            )
            .expect("lazy_solve_preloaded must succeed");
        };

        // Two warm-up calls so the result buffers reach their steady-state
        // capacity before we start asserting stability.
        call(&mut solver, &mut scratch);
        call(&mut solver, &mut scratch);
        let primal_cap = scratch.res_primal.capacity();
        let dual_cap = scratch.res_dual.capacity();
        let rc_cap = scratch.res_reduced_costs.capacity();
        assert!(
            !scratch.res_primal.is_empty(),
            "result primal must be filled"
        );
        assert_eq!(
            scratch.result_view().primal[LAZY_THETA_COL],
            5.0,
            "warmed result must be the binding optimum"
        );

        // Several more calls must not grow any result-buffer capacity.
        for _ in 0..3 {
            call(&mut solver, &mut scratch);
            assert_eq!(
                scratch.res_primal.capacity(),
                primal_cap,
                "res_primal capacity must be stable (growth-only)"
            );
            assert_eq!(
                scratch.res_dual.capacity(),
                dual_cap,
                "res_dual capacity must be stable (growth-only)"
            );
            assert_eq!(
                scratch.res_reduced_costs.capacity(),
                rc_cap,
                "res_reduced_costs capacity must be stable (growth-only)"
            );
            // result_view() still reflects the same optimum each call.
            assert_eq!(scratch.result_view().primal[LAZY_THETA_COL], 5.0);
        }
    }

    // -----------------------------------------------------------------------
    // lazy_solve test: cold mid-loop re-solve tolerance (mock solver)
    // -----------------------------------------------------------------------

    /// A mock solver that returns one primal vector on the first solve and a
    /// different one on every subsequent solve, never reporting a usable warm
    /// basis (it ignores the passed basis entirely — simulating an escalation
    /// that cold-started). Both solves are valid (non-error).
    struct TwoPhaseMock {
        first: Vec<f64>,
        rest: Vec<f64>,
        call_count: usize,
        buf: Vec<f64>,
        empty: Vec<f64>,
    }

    impl TwoPhaseMock {
        fn new(first: Vec<f64>, rest: Vec<f64>) -> Self {
            Self {
                first,
                rest,
                call_count: 0,
                buf: Vec::new(),
                empty: Vec::new(),
            }
        }
    }

    impl SolverInterface for TwoPhaseMock {
        type Profile = ActiveProfile;
        fn apply_profile(&mut self, _profile: &ActiveProfile) {}
        fn solver_name_version(&self) -> String {
            "TwoPhaseMock 0.0.0".to_string()
        }
        fn load_model(&mut self, _template: &StageTemplate) {}
        fn add_rows(&mut self, _rows: &RowBatch) {}
        fn set_row_bounds(&mut self, _i: &[usize], _l: &[f64], _u: &[f64]) {}
        fn set_col_bounds(&mut self, _i: &[usize], _l: &[f64], _u: &[f64]) {}
        fn solve(
            &mut self,
            _basis: Option<&Basis>,
        ) -> Result<cobre_solver::SolutionView<'_>, SolverError> {
            // Always behave as a cold solve: ignore any passed basis.
            let src = if self.call_count == 0 {
                &self.first
            } else {
                &self.rest
            };
            self.call_count += 1;
            self.buf.clone_from(src);
            Ok(cobre_solver::SolutionView {
                objective: self.buf[LAZY_THETA_COL],
                primal: &self.buf,
                dual: &self.empty,
                reduced_costs: &self.empty,
                iterations: 0,
                solve_time_seconds: 0.0,
            })
        }
        fn get_basis(&mut self, _out: &mut Basis) {}
        fn statistics(&self) -> SolverStatistics {
            SolverStatistics {
                solve_count: self.call_count as u64,
                ..SolverStatistics::default()
            }
        }
        fn statistics_into(&self, out: &mut SolverStatistics) {
            out.copy_from(&self.statistics());
        }
        fn name(&self) -> &'static str {
            "TwoPhaseMock"
        }
    }

    /// A mid-loop `solve(None)` that discarded its warm basis (the mock ignores
    /// the basis entirely) must be tolerated: lazy_solve completes and reaches
    /// the no-violation stop.
    #[test]
    fn lazy_solve_tolerates_cold_mid_loop_resolve() {
        let indexer = lazy_indexer();
        // One cut: intercept 5, coeff [0] → floor 5.0 at x0 = 2.
        let mut pool = CutPool::new(16, 1, 16, 0);
        pool.add_cut(0, 0, 5.0, &[0.0]);
        pool.metadata[0].iteration_generated = 1;

        // Primal layout: [x0, c1, c2, theta]. First solve reports theta = 0
        // (cut 0 floor 5 > 0 → violated). After adding cut 0, the mock reports
        // theta = 5 (floor 5 == theta → not violated → stop).
        let first = vec![STATE_X0, 0.0, 0.0, 0.0];
        let rest = vec![STATE_X0, 0.0, 0.0, 5.0];
        let mut solver = ProfiledSolver::new(TwoPhaseMock::new(first, rest));
        let core = core_template();
        let mut scratch = DcsSolveScratch::default();
        let params = lazy_params(10, 50);

        solver.load_model(&core);
        lazy_solve_preloaded(
            &mut solver,
            &core,
            &pool,
            &indexer,
            &[],
            None,
            &[], // omit cut 0 so the loop must add it mid-loop
            &params,
            &mut scratch,
            ctx(),
        )
        .expect("lazy_solve_preloaded must tolerate a cold mid-loop re-solve");
        let view = scratch.result_view();

        assert_eq!(
            view.primal[LAZY_THETA_COL], 5.0,
            "loop reaches the no-violation stop on the second (cold) solve"
        );
    }

    // -----------------------------------------------------------------------
    // build_initial_resident_set fixtures
    // -----------------------------------------------------------------------

    /// Build a pool of `n` populated, state_dim-1 cuts with explicit per-slot
    /// `(active, iteration_generated, last_active_iter)` metadata. Local copy of
    /// the `cut_selection` test-helper pattern (kept private to this module).
    #[allow(clippy::cast_possible_truncation)]
    fn seed_pool(specs: &[(bool, u64, u64)]) -> CutPool {
        let n = specs.len();
        let mut pool = CutPool::new(n.max(1), 1, n.max(1) as u32, 0);
        for (i, &(active, iteration_generated, last_active_iter)) in specs.iter().enumerate() {
            pool.add_cut(0, i as u32, 0.0, &[0.0]);
            pool.metadata[i] = CutMetadata {
                iteration_generated,
                forward_pass_index: i as u32,
                active_count: 0,
                last_active_iter,
            };
            pool.active[i] = active;
        }
        pool.cached_active_count = specs.iter().filter(|&&(a, _, _)| a).count();
        pool
    }

    // -----------------------------------------------------------------------
    // build_initial_resident_set tests
    // -----------------------------------------------------------------------

    /// AC1: slots 0..4 with last_active_iter = [10, 8, 3, 10, 6], all active,
    /// all generated at iter 1, current = 10, k2 = 5. Slot 2 excluded
    /// (10 - 3 = 7 > 5); the rest are within the window.
    #[test]
    fn seeds_within_k2_window() {
        let pool = seed_pool(&[
            (true, 1, 10),
            (true, 1, 8),
            (true, 1, 3),
            (true, 1, 10),
            (true, 1, 6),
        ]);
        let mut out = Vec::new();
        build_initial_resident_set(&pool, 10, 5, &mut out);
        assert_eq!(out, vec![0, 1, 3, 4]);
    }

    /// AC2: a current-iteration cut (iteration_generated == current) is always
    /// seeded even when its last_active_iter is far in the past.
    #[test]
    fn always_seeds_current_iteration_cuts() {
        // Slot 0: out of window (10 - 1 = 9 > 2) but generated this iteration.
        // Slot 1: in window.
        let pool = seed_pool(&[(true, 10, 1), (true, 1, 9)]);
        let mut out = Vec::new();
        build_initial_resident_set(&pool, 10, 2, &mut out);
        assert_eq!(
            out,
            vec![0, 1],
            "current-iteration slot 0 seeded despite stale last_active_iter"
        );
    }

    /// AC3: an inactive slot within the k2 window is never included.
    #[test]
    fn excludes_inactive_slots() {
        // Slot 0 active in window; slot 1 inactive in window; slot 2 active.
        let pool = seed_pool(&[(true, 1, 10), (false, 1, 10), (true, 1, 10)]);
        let mut out = Vec::new();
        build_initial_resident_set(&pool, 10, 5, &mut out);
        assert_eq!(out, vec![0, 2], "inactive slot 1 excluded");
    }

    /// AC4: the result is strictly ascending and deterministic across repeated
    /// calls on the same metadata (no dependence on iteration order).
    #[test]
    fn result_is_ascending_and_deterministic() {
        let pool = seed_pool(&[
            (true, 1, 10),
            (true, 1, 9),
            (false, 1, 10),
            (true, 1, 8),
            (true, 1, 10),
        ]);
        let mut a = Vec::new();
        let mut b = Vec::new();
        build_initial_resident_set(&pool, 10, 5, &mut a);
        build_initial_resident_set(&pool, 10, 5, &mut b);
        assert_eq!(a, b, "two calls on identical metadata must match");
        assert!(
            a.windows(2).all(|w| w[0] < w[1]),
            "result must be strictly ascending, got {a:?}"
        );
        assert_eq!(a, vec![0, 1, 3, 4]);
    }

    /// AC3: the seed keys on **binding recency** via
    /// `last_active_iter`, NOT on generation iteration. A cut generated long ago
    /// (`iteration_generated` well outside the k2 window) but binding recently
    /// (`last_active_iter == i`) must be seeded at iteration `i + d` for
    /// `0 < d <= k2` — the §3.1 clause-1 behavior the binding-count maintenance
    /// restores. The companion "generated but never re-bound" cut, whose
    /// `last_active_iter` stayed at its old generation iteration, is correctly
    /// excluded once it falls outside the window.
    #[test]
    fn seeds_old_generation_cut_that_bound_recently() {
        // current = i + d = 12, k2 = 3 → window covers last_active_iter >= 9.
        //   slot 0: generated at iter 1 (age 11 ≫ k2), but bound at iter 11
        //           → 12 - 11 = 1 <= 3 → SEEDED (recently binding, old gen).
        //   slot 1: generated at iter 1, never re-bound (last_active 1)
        //           → 12 - 1 = 11 > 3, not current-iter → EXCLUDED.
        //   slot 2: generated at iter 1, bound at iter 9 (window edge)
        //           → 12 - 9 = 3 <= 3 → SEEDED.
        let pool = seed_pool(&[(true, 1, 11), (true, 1, 1), (true, 1, 9)]);
        let mut out = Vec::new();
        build_initial_resident_set(&pool, 12, 3, &mut out);
        assert_eq!(
            out,
            vec![0, 2],
            "old-generation cuts that bound within the last k2 iterations are \
             seeded by binding recency; the never-re-bound cut is excluded"
        );
    }

    /// AC: k2 = 0 boundary. Only slots active in the current iteration
    /// (last_active_iter >= current) plus current-iteration cuts are included.
    #[test]
    fn k2_zero_window_boundary() {
        // current = 10, k2 = 0:
        //   slot 0: last_active_iter 10 → 10 - 10 = 0 <= 0 → included.
        //   slot 1: last_active_iter 9  → 10 - 9  = 1 >  0 → excluded (not current-iter).
        //   slot 2: last_active_iter 11 → saturating_sub → 0 <= 0 → included.
        //   slot 3: generated this iteration (last_active_iter 2 stale) → included.
        let pool = seed_pool(&[(true, 1, 10), (true, 1, 9), (true, 1, 11), (true, 10, 2)]);
        let mut out = Vec::new();
        build_initial_resident_set(&pool, 10, 0, &mut out);
        assert_eq!(out, vec![0, 2, 3]);
    }
}