nmbrs-workload 0.4.0

Uniform workload specification parsing and processing for nmbrs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! YAML workload parser and normalizer.
//!
//! Parses a YAML workload definition and normalizes all shorthand
//! forms into the canonical `ParsedOp` model.

use crate::model::{
    BindingsDef, ContinueIfSpec, MetricSpec, ParsedOp, ScenarioNode, ScopeLevel, StopConditionSpec,
    Workload, WorkloadPhase,
};
use crate::template::expand_templates;
use polydat::iteration::comprehension::Comprehension;
use polydat::iteration::comprehension::spec::{ComprehensionSpec, ForSpec, parse_inline};
use serde_json::Value as JVal;
use std::collections::HashMap;

/// Parse a YAML workload string into a normalized Workload.
///
/// In-memory entry point: callers that have already resolved the
/// source text. **Rejects `extends:`** because there is no
/// resolution context for the relative path (no including-file
/// directory). Callers that need `extends:` support must use
/// [`parse_workload_from_path`].
pub fn parse_workload(
    yaml_source: &str,
    params: &HashMap<String, String>,
) -> Result<Workload, String> {
    // Stage 1: TEMPLATE expansion
    let expanded = expand_templates(yaml_source, params);

    // Stage 2: Parse YAML into generic Value
    let mut doc: JVal =
        serde_yaml::from_str(&expanded).map_err(|e| format!("YAML parse error: {e}"))?;

    // SRD-72: `extends:` requires a resolution context. The
    // text-only entry point has no including-file directory, so
    // a top-level `extends:` here is unresolvable. Direct the
    // caller to `parse_workload_from_path` instead.
    if doc.get("extends").is_some() {
        return Err(
            "workload declares `extends:` but parse_workload was called \
             without a file path; use parse_workload_from_path instead"
                .to_string(),
        );
    }

    // Stage 2.5: op templates. Every op that `uses:` a template becomes
    // the template's body with its own keys folded in, and
    // `op_templates:` leaves the document, so every later stage sees
    // ordinary ops (see `crate::op_templates`). After the `extends:`
    // check: a library's templates reach a workload only through the
    // merged document.
    let instantiated = crate::op_templates::instantiate(
        doc.as_object_mut()
            .ok_or("workload must be a YAML mapping")?,
    )?;

    let obj = doc.as_object().ok_or("workload must be a YAML mapping")?;

    // Stage 3: Extract top-level fields
    let description = obj
        .get("description")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    let mut scenario_parse_errors: Vec<String> = Vec::new();
    let mut scenarios = parse_scenarios(obj.get("scenarios"), &mut scenario_parse_errors);
    // Resolve `scenario: <name>` includes after every scenario
    // has been parsed so forward references work and cycles are
    // detected with the full graph available.
    resolve_scenario_includes(&mut scenarios)?;

    let doc_bindings = extract_bindings(obj.get("bindings"));
    let doc_params = extract_value_map(obj.get("params"));
    let doc_tags = extract_string_map(obj.get("tags"));

    // Stage 4: Parse ops from blocks or top-level
    let mut all_ops = Vec::new();

    // SRD-13f Push D: workload-level `bindings:` live ONLY on
    // `Workload.bindings` and compile directly to the
    // workload-root Polydat Kernel. They no longer fold into ops at
    // parse time — descendant ops resolve workload-level wires
    // through the Polydat Kernel chain (workload-root → ... → op
    // kernel) via the SRD-13f cell-on-outputs cascade. So we
    // pass an empty `BindingsDef` to every op-producing path
    // here: block-level YAML bindings (parse_blocks) are the
    // only remaining parser-time "sugar" that expands into ops.
    if let Some(blocks_val) = obj.get("blocks") {
        parse_blocks(blocks_val, &doc_params, &doc_tags, &mut all_ops)?;
    }

    // Top-level ops (no blocks): no block sugar to inline.
    for key in ["ops", "op", "operations", "statements", "statement"] {
        if let Some(ops_val) = obj.get(key)
            && obj.get("blocks").is_none()
        {
            parse_ops_field(
                ops_val,
                "block0",
                &BindingsDef::default(),
                &doc_params,
                &doc_tags,
                &mut all_ops,
            )?;
        }
    }

    // Stage 5: Parse phases
    let (mut phases, phase_order) = parse_phases(obj.get("phases"), &doc_params, &doc_tags)?;

    // Stage 6: Auto-tag all ops (top-level and phase inline ops)
    for op in &mut all_ops {
        if !op.tags.contains_key("name") {
            op.tags.insert("name".to_string(), op.name.clone());
        }
        if !op.tags.contains_key("op") {
            op.tags.insert("op".to_string(), op.name.clone());
        }
    }

    // Stage 6.5 (SRD-108 Part A, completing SRD-20): a phase with
    // no inline ops and a `tags:` selector resolves its ops HERE,
    // from the merged document's top-level/block op pool. Parse
    // time is deliberate: everything downstream — synthesis,
    // validation, the SRD-107 config digest — sees a phase with
    // ordinary resolved ops, exactly as if they were inline. This
    // is the ad-hoc composition seam: an implementation workload
    // `extends:` a blueprint and contributes tagged block ops
    // that these selectors pick up.
    for (phase_name, phase) in phases.iter_mut() {
        let Some(selector) = phase.tags.clone() else {
            continue;
        };
        if !phase.ops.is_empty() {
            return Err(format!(
                "phase '{phase_name}' declares both inline ops and a \
                 `tags:` selector — a phase has exactly one source of \
                 ops. Drop the selector or move the ops into a tagged \
                 block."
            ));
        }
        let mut selected = crate::tags::TagFilter::filter_ops(&all_ops, &selector)
            .map_err(|e| format!("phase '{phase_name}' `tags:` selector: {e}"))?;
        if selected.is_empty() {
            return Err(format!(
                "phase '{phase_name}' `tags:` selector '{selector}' \
                 matched no ops ({} in the workload's op pool). A \
                 selector-only phase must bind at least one op — \
                 check the tag vocabulary against the blocks this \
                 workload (or its extends chain) declares.",
                all_ops.len()
            ));
        }
        // Mirror the inline-op auto-tagging: selected clones gain
        // the `phase` tag their inline siblings get at parse.
        for op in &mut selected {
            op.tags.insert("phase".to_string(), phase_name.clone());
        }
        phase.ops = selected;
    }

    // Stage 7: Resolve workload parameters
    // Priority: CLI params > workload defaults > env vars
    let yaml_params = extract_string_map(obj.get("params"));

    // Stage 7.5: ops instantiated from op templates carry the
    // template's interface as a BOUND interface; every `needs` wire
    // must be supplied here, and synthesis type-checks them.
    {
        let declared: Vec<String> = yaml_params.keys().cloned().collect();
        crate::op_templates::bind_and_check(
            &instantiated,
            &mut phases,
            &mut all_ops,
            &declared,
            &doc_bindings,
            &scenarios,
        )?;
    }
    let mut resolved_params = HashMap::new();
    for (key, default_value) in &yaml_params {
        let resolved = if let Some(cli_value) = params.get(key) {
            // CLI override — coerce to the declared default's type. The
            // workload default is the source of truth for type, so a
            // numeric default makes a suffixed override (`10m`, `4Ki`)
            // resolve numerically rather than landing as a string; a
            // non-numeric default leaves the override untouched.
            crate::magnitude::coerce_param_override(default_value, cli_value)
        } else if let Some(env_name) = default_value.strip_prefix("env:") {
            // Environment variable lookup
            std::env::var(env_name).unwrap_or_else(|_| default_value.clone())
        } else {
            default_value.clone()
        };
        resolved_params.insert(key.clone(), resolved);
    }
    // Also include CLI params that aren't in the workload defaults
    // (ad-hoc parameters passed on the command line)
    for (key, value) in params {
        if !resolved_params.contains_key(key) {
            resolved_params.insert(key.clone(), value.clone());
        }
    }

    let declared_params: Vec<String> = yaml_params.keys().cloned().collect();

    // Legacy `summary:` and `plot:` keys: removed, no shim.
    // Operators must migrate to the unified `report:` block
    // (SRD-46). The error message names both new homes.
    if obj.contains_key("summary") || obj.contains_key("summaries") {
        return Err("`summary:` / `summaries:` removed; use `report:` with \
             `table <name> ...` directives instead (SRD-46)"
            .to_string());
    }
    if obj.contains_key("plot") || obj.contains_key("plots") {
        return Err("`plot:` / `plots:` removed; use `report:` with \
             `plot <name> ...` directives instead (SRD-46)"
            .to_string());
    }

    // ── Construction-model enforcement gate ──
    // The document must validate against the enumerable
    // construction grammar (docs/guide/construction_model.md):
    // unknown elements on closed node kinds, value-form
    // violations, and missing required elements are load errors,
    // reported together with their document paths. Runs on the
    // extends-merged document, after the targeted legacy-key
    // rejections above (their migration messages are better than
    // a generic unknown-element finding).
    {
        let doc = serde_json::Value::Object(obj.clone());
        let violations =
            crate::construction::validate_workload(&doc, crate::construction::Mode::Complete);
        if !violations.is_empty() {
            let mut lines: Vec<String> = violations
                .iter()
                .map(|v| format!("  {}: {}", v.path, v.message))
                .collect();
            lines.sort();
            return Err(format!(
                "workload construction: {} violation(s) \
                 (docs/guide/construction_model.md):\n{}",
                violations.len(),
                lines.join("\n")
            ));
        }
    }

    // Unified `report:` block (SRD-46) — plots, tables, defaults,
    // groups. Parser is in `crate::report::parse_report`. The
    // returned warnings are stashed on the Workload for
    // strict-mode promotion downstream (SRD-15).
    let (report, report_warnings) = if let Some(val) = obj.get("report") {
        let parsed = crate::report::parse_report(val).map_err(|e| format!("report: {e}"))?;
        (parsed.report, parsed.warnings)
    } else {
        (crate::report::Report::default(), Vec::new())
    };

    // SRD 21 §"Parameter Resolution": CLI overrides are the
    // outermost layer. Each op has already absorbed the
    // doc → block → op closest-wins merge for YAML-declared
    // params; now overlay the CLI map so `nmbrs run ...
    // concurrency=200` replaces any inherited block-level
    // value. Workload-level `resolved_params` was already
    // CLI-resolved above (line 66–87); this pass extends the
    // same rule down to per-op params.
    if !params.is_empty() {
        for op in &mut all_ops {
            for (key, value) in params {
                // Same SRD-32a exclusion as the inherited-params merge: a CLI
                // `rate=…` / `cycles=…` is a phase/activity override, not an op
                // field — don't leak it into op params (would trip rate).
                if ACTIVITY_PARAM_KEYS.contains(&key.as_str()) {
                    continue;
                }
                op.params
                    .insert(key.clone(), serde_json::Value::String(value.clone()));
            }
        }
    }

    // SRD-44 §"Resume protocol": a phase declared
    // `checkpoint: idempotent` that lives inside a do_while /
    // do_until loop is rejected at workload load time. The
    // do-loop iterates the same phase many times under one
    // checkpoint identity; checkpointing presumes each phase
    // execution is a discrete unit, which the loop directly
    // contradicts. Operators who really want a do-loop'd phase
    // to skip on resume must wrap the loop with explicit
    // identity, not lean on the phase's `checkpoint:` flag.
    for (scenario_name, nodes) in &scenarios {
        let mut bad: Vec<String> = Vec::new();
        collect_idempotent_under_do_loop(nodes, false, &phases, &mut bad);
        if !bad.is_empty() {
            return Err(format!(
                "scenario '{scenario_name}': phase{plural} {names} \
                 declared `checkpoint: idempotent` while nested inside \
                 a do_while / do_until loop. The loop iterates the \
                 same phase identity multiple times, which contradicts \
                 the per-execution unit checkpointing assumes. Either \
                 remove the `checkpoint:` declaration or restructure \
                 the loop. (SRD-44 §\"Resume protocol\".)",
                plural = if bad.len() == 1 { "" } else { "s" },
                names = bad.join(", "),
            ));
        }
    }

    // Doc-root `status_metrics:` — workload-wide default that
    // any phase without its own `status_metrics:` inherits.
    // Same accept-list shapes as the per-phase parser: list of
    // strings, single string, or comma-separated string.
    let doc_status_metrics: Vec<String> = match obj.get("status_metrics") {
        None => Vec::new(),
        Some(JVal::Array(items)) => items
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.trim().to_string()))
            .filter(|s| !s.is_empty())
            .collect(),
        Some(JVal::String(s)) => s
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(String::from)
            .collect(),
        Some(other) => {
            return Err(format!(
                "status_metrics: must be a list of names/patterns, a \
             comma-separated string, or omitted; got {other:?}"
            ));
        }
    };
    if !doc_status_metrics.is_empty() {
        for phase in phases.values_mut() {
            if phase.status_metrics.is_empty() {
                phase.status_metrics = doc_status_metrics.clone();
            }
        }
    }

    // Doc-root `readouts:` block (SRD-63 §5.0). Three
    // accepted shapes:
    //   A. Single scalar string  → bound at on_update.
    //   B. Mapping of slot → name | body string.
    //   C. Mapping of slot → list of (name | body) strings.
    // The slot keys must match the lower-cased
    // Event::slot_name values (`on_update`, `on_phase_end`, …).
    // Inline body strings keep their full text — the
    // body-grammar parser in nmbrs-runtime::readouts::parse
    // bakes them at activity-init time.
    let readouts = parse_readouts_block(obj.get("readouts"))?;

    // SRD-83 — top-level `stop_when:` (workload-shell conditions), same
    // shape as the per-phase block.
    let stop_when: Vec<crate::model::StopConditionSpec> = match obj.get("stop_when") {
        Some(v) => serde_json::from_value(v.clone())
            .map_err(|e| format!("invalid top-level `stop_when` block: {e}"))?,
        None => Vec::new(),
    };
    // SRD-83 Part 5 — the effect verb is a closed vocabulary; an unknown
    // verb would silently resolve to the shell default at the trip site.
    for sc in &stop_when {
        sc.validate()
            .map_err(|e| format!("top-level `stop_when`: {e}"))?;
    }

    // SRD-108 Part B — `implements:` names the blueprint this
    // document provides op bodies for. A non-string value is
    // rejected rather than ignored.
    let implements: Option<String> = match obj.get("implements") {
        Some(v) => Some(
            v.as_str()
                .ok_or_else(|| {
                    format!("`implements:` must be a workload reference string, got {v}")
                })?
                .to_string(),
        ),
        None => None,
    };

    // SRD-106 Part 3 — `stick_session:` top-level bool. A non-bool
    // value is rejected rather than ignored (never-ignore-silently).
    let stick_session: Option<bool> = match obj.get("stick_session") {
        Some(v) => Some(
            v.as_bool()
                .ok_or_else(|| format!("`stick_session:` must be a boolean, got {v}"))?,
        ),
        None => None,
    };

    Ok(Workload {
        description,
        scenarios,
        ops: all_ops,
        bindings: doc_bindings,
        params: resolved_params,
        phases,
        phase_order,
        declared_params,
        stop_when,
        report,
        report_warnings,
        scenario_parse_errors,
        resolution_warnings: Vec::new(),
        status_metrics: doc_status_metrics,
        readouts,
        wrappers: None,
        implements,
        stick_session,
    })
}

/// Path-based entry point: load a workload YAML from disk,
/// follow its `extends:` chain (SRD-72), and parse the merged
/// result into a [`Workload`].
///
/// `path` MUST be an existing file. Relative paths are resolved
/// against the cwd before being passed to the loader.
pub fn parse_workload_from_path(
    path: &std::path::Path,
    params: &HashMap<String, String>,
) -> Result<Workload, String> {
    let (merged_yaml, resolution_warnings) = crate::extends::load_and_merge(path)?;
    let mut workload = parse_workload(&merged_yaml, params)?;
    // Reference-resolution warnings ride the same surfacing
    // channel as report warnings: stashed here, logged (or
    // strict-promoted) by the runner.
    workload.resolution_warnings.extend(resolution_warnings);
    Ok(workload)
}

/// Parse the workload's `readouts:` block per SRD-63 §5.0.
/// Returns the populated bindings struct or a load-time
/// error.
fn parse_readouts_block(value: Option<&JVal>) -> Result<crate::model::ReadoutsBindings, String> {
    use crate::model::ReadoutsBindings;
    let mut out = ReadoutsBindings::default();
    let Some(value) = value else {
        return Ok(out);
    };

    // Form A — scalar shorthand for `on_update`.
    if let JVal::String(s) = value {
        let trimmed = s.trim();
        if trimmed.is_empty() {
            return Ok(out);
        }
        out.on_update.push(trimmed.to_string());
        return Ok(out);
    }

    // Forms B / C — mapping with slot keys.
    let JVal::Object(map) = value else {
        return Err(format!(
            "readouts: must be a scalar (sugar for on_update) or a mapping \
             of slot name → readout body; got {value:?}"
        ));
    };
    for (key, val) in map {
        let bodies: Vec<String> = match val {
            JVal::String(s) => vec![s.trim().to_string()],
            JVal::Array(items) => items
                .iter()
                .map(|item| match item {
                    JVal::String(s) => Ok(s.trim().to_string()),
                    other => Err(format!(
                        "readouts.{key}: list entries must be strings; got {other:?}"
                    )),
                })
                .collect::<Result<Vec<_>, _>>()?,
            JVal::Null => continue,
            other => {
                return Err(format!(
                    "readouts.{key}: must be a string or list of strings; got {other:?}"
                ));
            }
        };
        let bodies: Vec<String> = bodies.into_iter().filter(|s| !s.is_empty()).collect();
        // Wildcard expansion (SRD-63 §4.1.1). The yaml key
        // can match a family rather than a single slot:
        //   `each_*`    → on_each_start + on_each_end
        //   `phase_*`   → on_phase_start + on_phase_end
        //   `scope_*`   → on_scope_start + on_scope_end
        //   `session_*` → on_session_start + on_session_end
        //   `*`         → every slot
        // Wildcard bindings are duplicated into each
        // matching slot so the binder's resolution doesn't
        // need a separate wildcard list. Render order:
        // explicit bindings first in declaration order
        // (handled by parser top-down), then any wildcard
        // expansions appended.
        let target_slots: Vec<&str> = match key.as_str() {
            "on_session_start" => vec!["on_session_start"],
            "on_session_end" => vec!["on_session_end"],
            "on_phase_start" => vec!["on_phase_start"],
            "on_phase_end" => vec!["on_phase_end"],
            "on_each_start" => vec!["on_each_start"],
            "on_each_end" => vec!["on_each_end"],
            "on_scope_start" => vec!["on_scope_start"],
            "on_scope_end" => vec!["on_scope_end"],
            "on_update" => vec!["on_update"],
            "session_*" => vec!["on_session_start", "on_session_end"],
            "phase_*" => vec!["on_phase_start", "on_phase_end"],
            "each_*" => vec!["on_each_start", "on_each_end"],
            "scope_*" => vec!["on_scope_start", "on_scope_end"],
            "*" => vec![
                "on_session_start",
                "on_session_end",
                "on_phase_start",
                "on_phase_end",
                "on_each_start",
                "on_each_end",
                "on_scope_start",
                "on_scope_end",
                "on_update",
            ],
            other => {
                return Err(format!(
                    "readouts: unknown slot '{other}'. Known: \
                 on_session_start/end, on_phase_start/end, \
                 on_each_start/end, on_scope_start/end, on_update; \
                 wildcards: each_*, phase_*, scope_*, session_*, *"
                ));
            }
        };
        for slot in target_slots {
            let target: &mut Vec<String> = match slot {
                "on_session_start" => &mut out.on_session_start,
                "on_session_end" => &mut out.on_session_end,
                "on_phase_start" => &mut out.on_phase_start,
                "on_phase_end" => &mut out.on_phase_end,
                "on_each_start" => &mut out.on_each_start,
                "on_each_end" => &mut out.on_each_end,
                "on_scope_start" => &mut out.on_scope_start,
                "on_scope_end" => &mut out.on_scope_end,
                "on_update" => &mut out.on_update,
                _ => unreachable!(),
            };
            target.extend(bodies.iter().cloned());
        }
    }
    Ok(out)
}

/// Walk a scenario tree and collect names of phases declared
/// `checkpoint: idempotent` that live under a `do_while` /
/// `do_until` ancestor. Used by the workload-load validation
/// step above (SRD-44).
fn collect_idempotent_under_do_loop(
    nodes: &[crate::model::ScenarioNode],
    in_do_loop: bool,
    phases: &HashMap<String, crate::model::WorkloadPhase>,
    out: &mut Vec<String>,
) {
    use crate::model::ScenarioNode;
    for node in nodes {
        match node {
            ScenarioNode::Phase(name) => {
                if in_do_loop {
                    let idempotent = phases
                        .get(name)
                        .and_then(|p| p.checkpoint.as_ref())
                        .map(|c| c.idempotent)
                        .unwrap_or(false);
                    if idempotent {
                        out.push(format!("'{name}'"));
                    }
                }
            }
            ScenarioNode::DoWhile { children, .. } | ScenarioNode::DoUntil { children, .. } => {
                collect_idempotent_under_do_loop(children, true, phases, out);
            }
            ScenarioNode::Comprehension { children, .. } => {
                collect_idempotent_under_do_loop(children, in_do_loop, phases, out);
            }
            ScenarioNode::IncludedScenario { children, .. } => {
                collect_idempotent_under_do_loop(children, in_do_loop, phases, out);
            }
            ScenarioNode::Bindings { children, .. } => {
                collect_idempotent_under_do_loop(children, in_do_loop, phases, out);
            }
        }
    }
}

/// Parse a YAML source into just the list of normalized ParsedOps.
/// Normalise an op-level `if:` clause so callers can write
/// expressions naturally. The downstream pipeline expects
/// the condition to be either a binding-name reference
/// (`{name}`) or an inline expression (`{{expr}}`). When
/// the operator writes a bare expression like
/// `cql_dialect == 'cass'`, that doesn't match either
/// form and the conditional dispenser fails at init when
/// it tries to look up a Polydat binding literally named
/// `cql_dialect == 'cass'`.
///
/// Heuristic: if the trimmed value already starts with `{`
/// (any braced form), or is a plain identifier
/// (`[A-Za-z_][A-Za-z0-9_]*`), pass through unchanged.
/// Otherwise treat as an inline expression and wrap with
/// `{{...}}` so the existing inline-expression machinery
/// in `nmbrs-runtime::scope::build_scope` synthesises a
/// hidden binding (`__expr_N := <expr>`) and rewrites the
/// condition to reference it.
pub fn normalize_condition_clause(s: &str) -> String {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return trimmed.to_string();
    }
    // Already in any braced form — pass through. The
    // downstream extractor handles `{{expr}}`, `{:=expr:=}`,
    // `{name}`, and `{expr-with-operators}` itself.
    if trimmed.starts_with('{') {
        return trimmed.to_string();
    }
    // Plain identifier — leave as-is so the legacy "if:
    // points at a single binding name" form keeps working.
    let is_plain_ident = trimmed
        .chars()
        .next()
        .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
        && trimmed
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_');
    if is_plain_ident {
        return trimmed.to_string();
    }
    // Anything else (operators, quotes, parens, …) → wrap
    // as an inline expression.
    format!("{{{{{trimmed}}}}}")
}

pub fn parse_ops(yaml_source: &str) -> Result<Vec<ParsedOp>, String> {
    let workload = parse_workload(yaml_source, &HashMap::new())?;
    Ok(workload.ops)
}

// -----------------------------------------------------------------
// Scenarios
// -----------------------------------------------------------------

fn parse_scenarios(
    val: Option<&JVal>,
    errors: &mut Vec<String>,
) -> HashMap<String, Vec<ScenarioNode>> {
    let mut scenarios = HashMap::new();
    let Some(val) = val else {
        return scenarios;
    };
    let Some(obj) = val.as_object() else {
        return scenarios;
    };

    for (scenario_name, steps_val) in obj {
        let nodes = parse_scenario_nodes_with_errors(steps_val, scenario_name, errors);
        scenarios.insert(scenario_name.clone(), nodes);
    }
    scenarios
}

/// Wrapper around the legacy `parse_scenario_nodes` that
/// accumulates `unknown-key` errors against a per-call sink.
/// Used by `parse_scenarios` so the top-level builder can
/// inspect the result and refuse to dispatch a malformed
/// workload (per "Never Ignore Silently"). The legacy
/// non-erroring shape is still exposed for the in-file
/// recursive callers that pre-date the error contract;
/// those paths quietly drop the unknown-key information,
/// but the top-level entry now sees it.
fn parse_scenario_nodes_with_errors(
    val: &JVal,
    scenario_name: &str,
    errors: &mut Vec<String>,
) -> Vec<ScenarioNode> {
    match val {
        JVal::Array(arr) => arr
            .iter()
            .flat_map(|item| parse_scenario_nodes_with_errors(item, scenario_name, errors))
            .collect(),
        JVal::Object(obj) => {
            if has_recognised_scenario_key(obj) {
                return parse_scenario_nodes(val);
            }
            // No recognized scenario-node key. Two legitimate
            // shapes can land here:
            //
            // 1. **Legacy command-string form** — `{ name: "run
            //    ..." }`. Each entry maps a step name to a CLI
            //    command string; the catch-all in
            //    `parse_scenario_nodes` produces one
            //    `ScenarioNode::Phase(<name>)` per key. The
            //    values are always strings (CLI command lines).
            //    Accepted.
            //
            // 2. **Malformed unknown-key node** — `{ iterate: {
            //    phases: [...] } }`, `{ for_with_typo: ... }`,
            //    etc. The value is a map or array, which means
            //    the author was trying to express structure the
            //    parser doesn't understand. Reject loudly per
            //    the project's "Never Ignore Silently" rule —
            //    silently dropping these used to cause
            //    confusing downstream errors (`phase 'iterate'
            //    not found` masking a typoed `for_each` key).
            let bad: Vec<&String> = obj
                .iter()
                .filter(|(_, v)| !matches!(v, JVal::String(_) | JVal::Null))
                .map(|(k, _)| k)
                .collect();
            if !bad.is_empty() {
                let bad_names: Vec<&str> = bad.iter().map(|s| s.as_str()).collect();
                errors.push(format!(
                    "scenario '{scenario_name}': unrecognised scenario-node key(s) \
                     {bad_names:?} carry non-string values (a map or array). \
                     Expected one of: `for_each` / `for`, `scenarios`, \
                     `for_combinations`, `do_while`, `do_until`, `bindings`, \
                     `set`, `scenario`. (The legacy `name: \"run ...\"` \
                     command-string form is still accepted when the value is \
                     a plain string.)"
                ));
                return Vec::new();
            }
            // All values are strings — legacy command-string
            // form. Route through the legacy catch-all.
            parse_scenario_nodes(val)
        }
        _ => parse_scenario_nodes(val),
    }
}

/// True iff this scenario-node object has at least one of the
/// recognized keys handled by `parse_scenario_nodes`. Used by
/// the error-aware wrapper to distinguish "malformed node"
/// (no recognized key) from "legitimate node" before the
/// silent-catchall in the legacy path can fire.
fn has_recognised_scenario_key(obj: &serde_json::Map<String, JVal>) -> bool {
    crate::vocab::scenario_node_keys()
        .iter()
        .any(|k| obj.contains_key(*k))
}

/// Emit the polydat RHS for a scenario `set:` value. A **bare identifier is a
/// wire REFERENCE**, consistent with comprehension r-values (SRD-18f): so
/// `set: { x: mnc }` binds `mnc`'s value, and an unresolved bare name is a
/// hard error at scope synthesis. A number/bool is that literal; a YAML
/// sequence is a list (of references / literals). A STRING literal must be
/// written explicitly as a polydat-quoted scalar — `'"verbose"'` in YAML —
/// because the pipeline's serde round-trips strip ordinary YAML quotes,
/// leaving a bare word indistinguishable from a reference.
fn emit_set_value_literal(value: &JVal) -> String {
    match value {
        JVal::Number(n) => n.to_string(),
        JVal::Bool(b) => b.to_string(),
        JVal::Null => "\"\"".to_string(),
        // A YAML sequence is a list literal; its elements are references
        // (bare) or literals.
        JVal::Array(elems) => {
            let parts: Vec<String> = elems.iter().map(emit_set_array_element).collect();
            format!("[{}]", parts.join(", "))
        }
        JVal::String(s) => {
            let t = s.trim();
            if is_polydat_quoted_string(t) {
                // Explicit polydat string literal (`'"verbose"'` in YAML).
                t.to_string()
            } else if crate::bindpoints::is_bare_identifier(t) {
                // Bare identifier ⇒ a wire reference (unquoted; polydat
                // resolves it against the in-scope chain).
                t.to_string()
            } else {
                // Free text / `{name}` templates ⇒ a string literal.
                let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
                format!("\"{escaped}\"")
            }
        }
        other => {
            let escaped = other.to_string().replace('\\', "\\\\").replace('"', "\\\"");
            format!("\"{escaped}\"")
        }
    }
}

/// One element of a `set:` sequence value: a bare-identifier element is a
/// reference (unquoted), a number/bool is its literal, any other string is a
/// quoted string element.
fn emit_set_array_element(e: &JVal) -> String {
    match e {
        JVal::String(s) if crate::bindpoints::is_bare_identifier(s.trim()) => s.trim().to_string(),
        JVal::Number(n) => n.to_string(),
        JVal::Bool(b) => b.to_string(),
        JVal::String(s) => {
            let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
            format!("\"{escaped}\"")
        }
        other => other.to_string(),
    }
}

/// Recursively parse scenario nodes from YAML.
///
/// Handles:
/// - String: phase name
/// - Object with `for_each` + `phases`: for_each loop (phases parsed recursively)
/// - Array: list of nodes
///
/// Map a `ScopeLevel` keyword (the `each:` vocabulary) from YAML text.
fn parse_scope_level(s: &str) -> Option<ScopeLevel> {
    match s {
        "self" => Some(ScopeLevel::SelfScope),
        "op" => Some(ScopeLevel::Op),
        "phase" => Some(ScopeLevel::Phase),
        "scenario" => Some(ScopeLevel::Scenario),
        "workload" => Some(ScopeLevel::Workload),
        _ => None,
    }
}

/// SRD-101 — parse a `continue_if:` value into a [`ContinueIfSpec`]. Accepts
/// the short string form (`continue_if: "end_of(p) <= max"` → `each: scenario`)
/// and the long map form (`{ when, each }`, where `each` is a single level or a
/// list). `each` defaults to `scenario` (the enclosing sweep).
/// Render a JSON scalar (string or number) as a duration string for the
/// `tries: {backoff: {min, max}}` map. A string passes through (`"100ms"`);
/// a bare number becomes its decimal text (`100` → `"100"`, taken as ms by
/// the runtime's duration parser). Non-scalars yield `None`.
fn json_scalar_to_dur_string(v: &JVal) -> Option<String> {
    match v {
        JVal::String(s) => Some(s.clone()),
        JVal::Number(n) => Some(n.to_string()),
        _ => None,
    }
}

fn parse_continue_if(val: Option<&JVal>) -> Option<ContinueIfSpec> {
    match val? {
        JVal::String(when) => Some(ContinueIfSpec {
            when: when.clone(),
            each: vec![ScopeLevel::Scenario],
        }),
        JVal::Object(map) => {
            let when = map.get("when").and_then(|v| v.as_str())?.to_string();
            let each: Vec<ScopeLevel> = match map.get("each") {
                Some(JVal::String(s)) => parse_scope_level(s).into_iter().collect(),
                Some(JVal::Array(arr)) => arr
                    .iter()
                    .filter_map(|v| v.as_str().and_then(parse_scope_level))
                    .collect(),
                _ => Vec::new(),
            };
            let each = if each.is_empty() {
                vec![ScopeLevel::Scenario]
            } else {
                each
            };
            Some(ContinueIfSpec { when, each })
        }
        _ => None,
    }
}

fn parse_scenario_nodes(val: &JVal) -> Vec<ScenarioNode> {
    match val {
        JVal::String(s) => vec![ScenarioNode::Phase(s.clone())],
        JVal::Array(arr) => arr.iter().flat_map(parse_scenario_nodes).collect(),
        JVal::Object(obj) => {
            let children = obj
                .get("phases")
                .map(parse_scenario_nodes)
                .unwrap_or_default();
            let counter = obj
                .get("counter")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            // `for_each` is the canonical key; `for` is accepted as
            // a shorter synonym ("for k in 10,100" reads more
            // naturally and matches the Polydat comprehension text
            // grammar). Both keys are interchangeable; if both
            // appear, `for_each` wins so misconfigured workloads
            // don't silently change shape.
            if let Some(for_each_val) = obj.get("for_each").or_else(|| obj.get("for")) {
                // for_each supports three YAML shapes (string,
                // array, object) that collapse into one of three
                // semantic variants (Cartesian / Union / single-
                // clause). The detection rule lives in
                // `ComprehensionSpec::into_legacy` (and ultimately
                // in `comprehension_from_subspaces` underneath) —
                // single source of truth.
                //
                // YAML-shape → ForSpec mapping:
                //   "x in 1, y in 2"               → Inline (string)
                //   "x in 1, x in 2"               → Inline (string,
                //                                    name repeat ⇒ Union)
                //   ["x in 1", "y in 2"]           → UnionOfClauseLists
                //                                    (one entry per
                //                                    sub-space; name
                //                                    repeat across
                //                                    entries ⇒ Union;
                //                                    no repeat ⇒
                //                                    Cartesian after
                //                                    flattening)
                //   { x: "1", y: "2" }             → Inline (string,
                //                                    assembled from
                //                                    key=value pairs)
                let for_spec: Option<ForSpec> = match for_each_val {
                    JVal::String(spec) => Some(ForSpec::Inline(spec.clone())),
                    JVal::Array(arr) => {
                        // One sub-space per array entry. Each
                        // entry can hold multiple comma-separated
                        // clauses (cartesian within); cross-entry
                        // is unioned. Wrap each entry in a
                        // singleton inner-list so the
                        // UnionOfClauseLists shape carries the
                        // "one entry = one sub-space" semantic.
                        let groups: Vec<Vec<String>> = arr
                            .iter()
                            .filter_map(|item| item.as_str().map(|s| vec![s.to_string()]))
                            .collect();
                        if groups.is_empty() {
                            None
                        } else {
                            Some(ForSpec::UnionOfClauseLists(groups))
                        }
                    }
                    JVal::Object(map) => {
                        // Map form is always a single sub-space
                        // (keys are unique). Assemble into an
                        // inline string and route through
                        // ForSpec::Inline.
                        if map.is_empty() {
                            None
                        } else {
                            let inline = map
                                .iter()
                                .map(|(k, v)| format!("{k} in {}", v.as_str().unwrap_or("")))
                                .collect::<Vec<_>>()
                                .join(", ");
                            Some(ForSpec::Inline(inline))
                        }
                    }
                    _ => None,
                };

                match for_spec {
                    None => vec![],
                    Some(r#for) => {
                        let spec = ComprehensionSpec {
                            r#for,
                            // Optional `where:` key carries a
                            // filter predicate evaluated per
                            // emitted tuple.
                            r#where: obj.get("where").and_then(|v| v.as_str()).map(String::from),
                            // Optional `order:` key carries a
                            // traversal order spec (GK text form,
                            // e.g. "extrema/1").
                            order: obj.get("order").and_then(|v| v.as_str()).map(String::from),
                        };
                        match spec.into_algebra() {
                            Ok(comprehension) => vec![ScenarioNode::Comprehension {
                                comprehension,
                                children,
                                continue_if: parse_continue_if(obj.get("continue_if")),
                                anchor: obj
                                    .get("anchor")
                                    .and_then(|v| v.as_str())
                                    .map(String::from),
                            }],
                            Err(e) => {
                                eprintln!("warning: comprehension: {e}");
                                vec![]
                            }
                        }
                    }
                }
            } else if let Some(scenario_val) = obj.get("scenario").and_then(|v| v.as_str()) {
                // `scenario: <name>` — logical inclusion of
                // another scenario at this point in the tree.
                // Children remain empty here; resolution happens
                // post-parse via `resolve_scenario_includes`,
                // once every scenario in the workload is known.
                vec![ScenarioNode::IncludedScenario {
                    name: scenario_val.to_string(),
                    children: Vec::new(),
                }]
            } else if let Some(scenarios_val) = obj.get("scenarios") {
                // `scenarios: [name, name, ...]` — plural form
                // for composing several named scenarios at one
                // node in the tree. Each list entry expands to
                // its own `IncludedScenario`; resolution happens
                // post-parse via `resolve_scenario_includes`.
                // Reads more naturally than repeating
                // `- scenario: foo` for each entry; both forms
                // are interchangeable.
                //
                // Map / object entries (`{ scenario: foo }`) are
                // also accepted so a list can mix bare-string
                // includes with other scenario-node shapes
                // already supported by `parse_scenario_nodes`.
                match scenarios_val {
                    JVal::Array(arr) => arr
                        .iter()
                        .flat_map(|item| {
                            match item {
                                JVal::String(s) => vec![ScenarioNode::IncludedScenario {
                                    name: s.clone(),
                                    children: Vec::new(),
                                }],
                                // Anything else (object with
                                // `scenario:`, `for_each:`, etc.)
                                // routes through the standard parse
                                // path so list entries can be
                                // heterogeneous.
                                _ => parse_scenario_nodes(item),
                            }
                        })
                        .collect(),
                    JVal::String(s) => vec![ScenarioNode::IncludedScenario {
                        name: s.clone(),
                        children: Vec::new(),
                    }],
                    _ => Vec::new(),
                }
            } else if let Some(combo_val) = obj.get("for_combinations") {
                // Explicit for_combinations keyword (alias for
                // multi-clause for_each). Route through
                // ComprehensionSpec so the for_combinations branch
                // shares the for_each branch's single chokepoint.
                // Distinct-var input (the typical case) parses as
                // Cartesian via the name-repetition detection rule
                // — same semantics as the legacy direct
                // `Comprehension::cartesian(...)` path.
                let specs = parse_combination_specs(combo_val);
                let inline = specs
                    .iter()
                    .map(|(v, e)| format!("{v} in {e}"))
                    .collect::<Vec<_>>()
                    .join(", ");
                let spec = ComprehensionSpec {
                    r#for: ForSpec::Inline(inline),
                    r#where: obj.get("where").and_then(|v| v.as_str()).map(String::from),
                    order: obj.get("order").and_then(|v| v.as_str()).map(String::from),
                };
                match spec.into_algebra() {
                    Ok(comprehension) => vec![ScenarioNode::Comprehension {
                        comprehension,
                        children,
                        continue_if: parse_continue_if(obj.get("continue_if")),
                        anchor: obj.get("anchor").and_then(|v| v.as_str()).map(String::from),
                    }],
                    Err(e) => {
                        eprintln!("warning: for_combinations: {e}");
                        vec![]
                    }
                }
            } else if let Some(cond) = obj.get("do_while").and_then(|v| v.as_str()) {
                vec![ScenarioNode::DoWhile {
                    condition: cond.to_string(),
                    counter,
                    children,
                }]
            } else if let Some(cond) = obj.get("do_until").and_then(|v| v.as_str()) {
                vec![ScenarioNode::DoUntil {
                    condition: cond.to_string(),
                    counter,
                    children,
                }]
            } else if let Some(bindings_val) = obj.get("bindings") {
                // Scenario-tree-level `bindings:` — arbitrary GK
                // matter that installs as a scope-tree layer over
                // the parent. The source text is whatever the
                // author wrote (interpretation happens at kernel
                // build time via the canonical scope synthesizer).
                let source = match bindings_val {
                    JVal::String(s) => s.clone(),
                    JVal::Object(map) => {
                        // Map form: each `name: expr` produces
                        // one `name := <expr>` line. The GK
                        // compiler classifies the modifier from
                        // any leading `final`/`init`/`shared`
                        // keyword in `name`; the bare-name case
                        // becomes a cycle binding.
                        let mut out = String::new();
                        for (name, value) in map {
                            let v_text = match value {
                                JVal::String(s) => s.clone(),
                                JVal::Bool(b) => b.to_string(),
                                JVal::Number(n) => n.to_string(),
                                JVal::Null => String::new(),
                                other => other.to_string(),
                            };
                            out.push_str(&format!("{name} := {v_text}\n"));
                        }
                        out
                    }
                    _ => String::new(),
                };
                if source.trim().is_empty() {
                    Vec::new()
                } else {
                    if children.is_empty() {
                        eprintln!(
                            "warning: scenario-tree `bindings:` block has no \
                             `phases:` body — this is a no-op (the scope is \
                             entered and immediately exited with no descendants \
                             reading any of its declared names). If you meant \
                             to publish these bindings to a subtree, add a \
                             `phases:` block; if you meant to declare workload-\
                             level bindings, move them to the top-level \
                             `bindings:` field of the workload."
                        );
                    }
                    vec![ScenarioNode::Bindings { source, children }]
                }
            } else if let Some(set_val) = obj.get("set") {
                // `set: { name: value, ... }` — convenience sugar
                // that desugars to a `Bindings` node carrying GK
                // matter of the shape `const NAME := <polydat-literal>`.
                // The Polydat compiler handles workload-param
                // interpolation, string-literal interpolation,
                // and full const-expression evaluation at kernel
                // build time, so this form composes with every
                // other Polydat feature (no separate two-pass
                // evaluator). The `Bindings` node carries the
                // synthesized source; downstream code never sees
                // a SetParam-specific shape.
                //
                // String shorthand `set: name=value` is also
                // accepted for the single-override one-liner case.
                //
                // Map iteration order is preserved by serde_yaml
                // (insertion order); declaration order wins on
                // collision via the standard Polydat shadow semantics
                // for the same source.
                let pairs: Vec<(String, JVal)> = match set_val {
                    JVal::Object(map) => map.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
                    JVal::String(s) => match s.split_once('=') {
                        Some((k, v)) => {
                            vec![(k.trim().to_string(), JVal::String(v.trim().to_string()))]
                        }
                        None => {
                            eprintln!(
                                "warning: scenario `set:` string form must be \
                                     `name=value`, got `{s}` — ignoring"
                            );
                            Vec::new()
                        }
                    },
                    _ => Vec::new(),
                };
                if pairs.is_empty() {
                    Vec::new()
                } else if children.is_empty() {
                    // Same no-op condition as the explicit
                    // `bindings:` form: a `set:` with no
                    // `phases:` body publishes overrides to
                    // nothing. Almost certainly an author error.
                    let names: Vec<&str> = pairs.iter().map(|(k, _)| k.as_str()).collect();
                    eprintln!(
                        "warning: scenario-tree `set:` block (overriding {:?}) \
                         has no `phases:` body — this is a no-op. Add a `phases:` \
                         block listing what the override applies to.",
                        names
                    );
                    Vec::new()
                } else {
                    // Synthesize the Polydat source body. Each pair
                    // becomes `const NAME := <polydat-literal>` — the
                    // single canonical effectively-const binding
                    // shape. The compiler tries const-fold at
                    // compile time (pure literals fold; values
                    // containing `{name}` interpolation depend on
                    // extern slots that aren't populated until
                    // materialize-wiring) and falls back to
                    // scope-init pull when fold isn't possible.
                    // Either way the value is materialized once
                    // and then immutable for the scope's
                    // lifetime — author doesn't need to think
                    // about which path the runtime takes.
                    //
                    // Each value lowers to `const NAME := <rhs>` via
                    // `emit_set_value_literal`. A BARE identifier is a wire
                    // reference (consistent with comprehension r-values,
                    // SRD-18f) — `set: { x: mnc }` binds mnc's value, and an
                    // unresolved bare name is a hard error at scope synthesis.
                    // Numbers/bools are literals; a YAML sequence is a list. A
                    // string literal is the explicit polydat-quoted form
                    // `'"verbose"'` (ordinary YAML quotes don't survive the
                    // pipeline's serde round-trips).
                    let mut source = String::new();
                    for (name, value) in &pairs {
                        source.push_str(&format!(
                            "const {name} := {}\n",
                            emit_set_value_literal(value),
                        ));
                    }
                    vec![ScenarioNode::Bindings { source, children }]
                }
            } else {
                obj.iter()
                    .map(|(name, _cmd)| ScenarioNode::Phase(name.clone()))
                    .collect()
            }
        }
        _ => Vec::new(),
    }
}

/// Resolve every `IncludedScenario { name, children: [] }` node
/// produced by [`parse_scenario_nodes`] into one whose `children`
/// hold a clone of the referenced scenario's resolved nodes.
///
/// Two failure modes are surfaced as parse errors:
///
/// 1. **Unknown scenario name** — `scenario: foo` where no
///    `scenarios.foo` exists.
/// 2. **Cycle** — `A` includes `B` which (transitively) includes
///    `A`. The error names the full cycle path so the operator
///    can fix the offending edge.
///
/// Resolution is depth-first with memoization so each scenario
/// is resolved at most once regardless of how many places
/// reference it. After this pass the workload model carries no
/// unresolved `IncludedScenario` nodes; downstream consumers
/// (scope tree, executor, runner) can treat the variant as a
/// fully-formed wrapper scope.
pub fn resolve_scenario_includes(
    scenarios: &mut HashMap<String, Vec<ScenarioNode>>,
) -> Result<(), String> {
    use std::collections::HashSet;

    // Snapshot the input so resolution reads from a stable map
    // while we mutate the output. Each resolved scenario is
    // recorded back into `out`.
    let input: HashMap<String, Vec<ScenarioNode>> = scenarios.clone();
    let mut out: HashMap<String, Vec<ScenarioNode>> = HashMap::new();

    fn resolve_nodes(
        nodes: &[ScenarioNode],
        input: &HashMap<String, Vec<ScenarioNode>>,
        out: &mut HashMap<String, Vec<ScenarioNode>>,
        stack: &mut Vec<String>,
    ) -> Result<Vec<ScenarioNode>, String> {
        let mut resolved = Vec::with_capacity(nodes.len());
        for n in nodes {
            resolved.push(resolve_one(n, input, out, stack)?);
        }
        Ok(resolved)
    }

    fn resolve_one(
        node: &ScenarioNode,
        input: &HashMap<String, Vec<ScenarioNode>>,
        out: &mut HashMap<String, Vec<ScenarioNode>>,
        stack: &mut Vec<String>,
    ) -> Result<ScenarioNode, String> {
        match node {
            ScenarioNode::Phase(name) => Ok(ScenarioNode::Phase(name.clone())),
            ScenarioNode::IncludedScenario { name, .. } => {
                if stack.iter().any(|s| s == name) {
                    let mut path = stack.clone();
                    path.push(name.clone());
                    return Err(format!(
                        "scenario include cycle detected: {}",
                        path.join(" -> "),
                    ));
                }
                let target = input.get(name).ok_or_else(|| {
                    format!(
                        "scenario include 'scenario: {name}' references an unknown \
                     scenario. Known scenarios: {}",
                        {
                            let mut names: Vec<&str> = input.keys().map(|s| s.as_str()).collect();
                            names.sort();
                            names.join(", ")
                        },
                    )
                })?;
                stack.push(name.clone());
                let children = resolve_nodes(target, input, out, stack)?;
                stack.pop();
                // Memoize the resolved scenario for any later
                // include reference. Idempotent: equivalent
                // resolved children produced regardless of
                // entry point.
                out.entry(name.clone()).or_insert_with(|| children.clone());
                Ok(ScenarioNode::IncludedScenario {
                    name: name.clone(),
                    children,
                })
            }
            ScenarioNode::Comprehension {
                comprehension,
                children,
                continue_if,
                anchor,
            } => Ok(ScenarioNode::Comprehension {
                comprehension: comprehension.clone(),
                children: resolve_nodes(children, input, out, stack)?,
                continue_if: continue_if.clone(),
                anchor: anchor.clone(),
            }),
            ScenarioNode::DoWhile {
                condition,
                counter,
                children,
            } => Ok(ScenarioNode::DoWhile {
                condition: condition.clone(),
                counter: counter.clone(),
                children: resolve_nodes(children, input, out, stack)?,
            }),
            ScenarioNode::DoUntil {
                condition,
                counter,
                children,
            } => Ok(ScenarioNode::DoUntil {
                condition: condition.clone(),
                counter: counter.clone(),
                children: resolve_nodes(children, input, out, stack)?,
            }),
            ScenarioNode::Bindings { source, children } => Ok(ScenarioNode::Bindings {
                source: source.clone(),
                children: resolve_nodes(children, input, out, stack)?,
            }),
        }
    }

    let mut visited: HashSet<String> = HashSet::new();
    let names: Vec<String> = scenarios.keys().cloned().collect();
    for name in names {
        if visited.contains(&name) {
            continue;
        }
        let mut stack = vec![name.clone()];
        let resolved = resolve_nodes(&input[&name], &input, &mut out, &mut stack)?;
        out.insert(name.clone(), resolved);
        visited.insert(name);
    }
    *scenarios = out;
    Ok(())
}

/// Parse combination specs from any of three YAML forms:
///
/// **Map form** (keys = variables, values = expressions):
/// ```yaml
/// for_combinations:
///   profile: "matching_profiles('{dataset}', '{prefix}')"
///   k: "{k_values}"
/// ```
///
/// **List form** (reuses for_each "var in expr" syntax):
/// ```yaml
/// for_combinations:
///   - "profile in matching_profiles('{dataset}', '{prefix}')"
///   - "k in {k_values}"
/// ```
///
/// **Inline form** (compact comma-separated):
/// ```yaml
/// for_combinations: "profile in profiles, k in {k_values}"
/// ```
fn parse_combination_specs(val: &JVal) -> Vec<(String, String)> {
    match val {
        // Map form: { "profile": "expr", "k": "expr" }
        JVal::Object(map) => map
            .iter()
            .map(|(key, val)| {
                let expr = val.as_str().unwrap_or("").to_string();
                (key.clone(), expr)
            })
            .collect(),
        // List form: ["profile in expr", "k in expr"]
        JVal::Array(arr) => arr
            .iter()
            .filter_map(|item| {
                let s = item.as_str()?;
                clause_pairs_of(s)
            })
            .flatten()
            .collect(),
        // Inline form: "profile in expr, k in expr"
        // Split on commas that are NOT inside parentheses (respects
        // function calls like `matching_profiles('{dataset}', '{prefix}')`).
        JVal::String(s) => clause_pairs_of(s).unwrap_or_default(),
        _ => {
            eprintln!("warning: for_combinations value must be a map, list, or string");
            Vec::new()
        }
    }
}

/// Parse clause text (`"var in expr"`, or a comma-separated list of
/// them) through polydat's spec entry and read the `(var, expr)` pairs
/// back off the algebra, warning and yielding `None` on a parse error.
fn clause_pairs_of(text: &str) -> Option<Vec<(String, String)>> {
    match parse_inline(text) {
        Ok(comp) => {
            let mut pairs = Vec::new();
            collect_clause_pairs(&comp, &mut pairs);
            Some(pairs)
        }
        Err(e) => {
            eprintln!("warning: for_combinations: {e}");
            None
        }
    }
}

/// Every leaf clause of `c`, in order, as `(var, source text)`.
fn collect_clause_pairs(c: &Comprehension, out: &mut Vec<(String, String)>) {
    match c {
        Comprehension::Clause { name, source } => match source.to_text() {
            Some(text) => out.push((name.clone(), text)),
            None => eprintln!("warning: for_combinations: clause '{name}' has no text form"),
        },
        Comprehension::Cartesian { children }
        | Comprehension::Zip { children, .. }
        | Comprehension::Union { children } => {
            for child in children {
                collect_clause_pairs(child, out);
            }
        }
        Comprehension::Filter { child, .. } | Comprehension::Order { child, .. } => {
            collect_clause_pairs(child, out);
        }
    }
}

// -----------------------------------------------------------------
// Phases
// -----------------------------------------------------------------

/// Parse the `phases:` section of a workload YAML.
///
/// Each phase is a named map with optional `cycles`, `concurrency`,
/// `rate`, `adapter`, `errors`, `tags`, and `ops` fields.
/// Returns the phase map and a Vec preserving YAML definition order.
fn parse_phases(
    val: Option<&JVal>,
    doc_params: &HashMap<String, JVal>,
    doc_tags: &HashMap<String, String>,
) -> Result<(HashMap<String, WorkloadPhase>, Vec<String>), String> {
    let mut phases = HashMap::new();
    let mut phase_order = Vec::new();
    let Some(val) = val else {
        return Ok((phases, phase_order));
    };
    let Some(obj) = val.as_object() else {
        return Ok((phases, phase_order));
    };

    for (phase_name, phase_val) in obj {
        let Some(phase_obj) = phase_val.as_object() else {
            continue;
        };

        let cycles = phase_obj.get("cycles").map(|v| match v {
            JVal::Number(n) => n.to_string(),
            JVal::String(s) => s.clone(),
            other => other.to_string(),
        });

        let concurrency = phase_obj.get("concurrency").map(|v| match v {
            JVal::Number(n) => n.to_string(),
            JVal::String(s) => s.clone(),
            other => other.to_string(),
        });

        // A number or a `{param}` / iter-var reference (resolved at
        // the phase gather, the `timeout:` discipline). Anything
        // else is a load error — a malformed rate must never
        // silently become "unrated".
        let rate = match phase_obj.get("rate") {
            None => None,
            Some(JVal::Number(n)) => Some(n.to_string()),
            Some(JVal::String(s)) => Some(s.clone()),
            Some(other) => {
                return Err(format!(
                    "phase '{phase_name}': `rate` must be a number (ops/sec) \
                 or a {{param}} reference; got: {other}"
                ));
            }
        };

        // SRD-82 Part 6 — daemon phase (runs concurrently with foreground
        // siblings, stopped when they complete). Accept bool / 0|1 / on|off.
        let daemon = phase_obj
            .get("daemon")
            .map(|v| match v {
                JVal::Bool(b) => *b,
                JVal::Number(n) => n.as_u64().map(|u| u != 0).unwrap_or(false),
                JVal::String(s) => matches!(
                    s.trim().to_ascii_lowercase().as_str(),
                    "true" | "on" | "yes" | "1"
                ),
                _ => false,
            })
            .unwrap_or(false);

        let adapter = phase_obj
            .get("adapter")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let errors = phase_obj
            .get("errors")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let error_rate_max = phase_obj.get("error_rate_max").and_then(|v| v.as_f64());

        // SRD-83 governance `timeout:` (GAP-12). A duration string, a
        // bare number (fractional seconds), or a `{param}` reference.
        // Param-bearing values resolve at the phase gather; literal
        // values must at least LOOK like a time value (non-empty, and
        // either numeric-leading or brace-opening) so a structural typo
        // fails at load, not at first dispatch. Full duration parsing
        // stays runtime-side (single parser: `timeval::parse_time_ms`).
        let timeout = match phase_obj.get("timeout") {
            None => None,
            Some(v) => {
                let s = match v {
                    serde_json::Value::String(s) => s.clone(),
                    n if n.is_u64() || n.is_f64() => n.to_string(),
                    other => {
                        return Err(format!(
                            "phase '{phase_name}': `timeout` must be a duration \
                         string (e.g. \"2.5h\"), a number of seconds, or a \
                         {{param}} reference; got: {other}"
                        ));
                    }
                };
                let t = s.trim();
                if t.is_empty()
                    || !(t.starts_with('{') || t.starts_with(|c: char| c.is_ascii_digit()))
                {
                    return Err(format!(
                        "phase '{phase_name}': `timeout: \"{s}\"` is not a \
                         duration ('{{param}}', \"2.5h\", \"150ms\", or \
                         seconds)"
                    ));
                }
                Some(s)
            }
        };

        // Per-phase total-attempts budget — the phase-level surface of the
        // `tries` sigil (SRD-82 Part 3b). Inherits down to the phase's ops;
        // absent everywhere → no tries wrapper (single attempt). Two forms:
        // the sugared number (`tries: 20`) and the map form carrying retry
        // backoff (`tries: {count: 20, backoff: {ratio, min, max}}`).
        let (tries, tries_backoff) = match phase_obj.get("tries") {
            None => (None, None),
            Some(v) if v.is_u64() || v.is_i64() => (v.as_u64().map(|n| n as u32), None),
            Some(serde_json::Value::Object(m)) => {
                let count = m.get("count").and_then(|c| c.as_u64()).map(|n| n as u32);
                let backoff = m.get("backoff").and_then(|b| b.as_object()).map(|bo| {
                    crate::model::BackoffSpec {
                        ratio: bo.get("ratio").and_then(|r| r.as_f64()),
                        min: bo.get("min").and_then(json_scalar_to_dur_string),
                        max: bo.get("max").and_then(json_scalar_to_dur_string),
                    }
                });
                (count, backoff)
            }
            Some(other) => {
                return Err(format!(
                    "phase '{phase_name}': `tries` must be a number or a map \
                 {{count, backoff: {{ratio, min, max}}}}, got {other}"
                ));
            }
        };

        // SRD-83 — `stop_when:` is a list of {when, trigger?, effect?}.
        // StopConditionSpec derives Deserialize, so deserialize the
        // sub-tree directly; surface a malformed block as a parse error.
        // SRD-83 §throttle — adaptive backpressure governor: bool
        // sugar or the full spec map (serde, deny_unknown_fields).
        let throttle: Option<crate::model::ThrottleField> = match phase_obj.get("throttle") {
            Some(v) => Some(
                serde_json::from_value(v.clone())
                    .map_err(|e| format!("phase '{phase_name}' invalid `throttle` block: {e}"))?,
            ),
            None => None,
        };
        let stop_when: Vec<StopConditionSpec> = match phase_obj.get("stop_when") {
            Some(v) => serde_json::from_value(v.clone())
                .map_err(|e| format!("invalid `stop_when` block: {e}"))?,
            None => Vec::new(),
        };
        // SRD-83 Part 5 — reject unknown effect verbs at load (a typo'd
        // verb would otherwise silently become the shell default).
        for sc in &stop_when {
            sc.validate()
                .map_err(|e| format!("phase `stop_when`: {e}"))?;
        }

        let tags = phase_obj
            .get("tags")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        // SRD-13f Push D: phase-level AND workload-level
        // `bindings:` are captured on their own scope's AST
        // only — they do NOT fold into per-op bindings.
        // Workload bindings live on `Workload.bindings` and
        // compile to the workload-root kernel; phase bindings
        // live on `WorkloadPhase.bindings` and compile to the
        // phase kernel. Both reach ops through the Polydat Kernel
        // chain (cell-on-outputs cascade, SRD-13f Push B.2),
        // not parser-time concat.
        let phase_bindings_only = extract_bindings(phase_obj.get("bindings"));

        // Parse inline ops if present
        let mut inline_ops = Vec::new();
        for key in ["ops", "op", "operations", "statements", "statement"] {
            if let Some(ops_val) = phase_obj.get(key) {
                let phase_tags = {
                    let mut t = doc_tags.clone();
                    t.insert("phase".to_string(), phase_name.clone());
                    t
                };
                // Phase inline ops carry zero "outer YAML
                // bindings sugar" — they're directly under the
                // phase, no block wrapper. Workload + phase
                // bindings reach them via the Polydat Kernel chain.
                parse_ops_field(
                    ops_val,
                    phase_name,
                    &BindingsDef::default(),
                    doc_params,
                    &phase_tags,
                    &mut inline_ops,
                )?;
                break;
            }
        }

        // Auto-tag inline ops
        for op in &mut inline_ops {
            if !op.tags.contains_key("name") {
                op.tags.insert("name".to_string(), op.name.clone());
            }
            if !op.tags.contains_key("op") {
                op.tags.insert("op".to_string(), op.name.clone());
            }
        }

        let for_each = phase_obj
            .get("for_each")
            .or_else(|| phase_obj.get("for"))
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        // SRD-101 — phase-level `continue_if` gate (bounds a `for_each` sweep).
        let continue_if = parse_continue_if(phase_obj.get("continue_if"));

        let loop_scope = phase_obj
            .get("loop_scope")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let iter_scope = phase_obj
            .get("iter_scope")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        // Phase-level `summary:` is gone (SRD-46 made `report:`
        // the canonical surface). Reject explicitly so silent
        // drops don't mislead operators migrating workloads.
        if phase_obj.contains_key("summary") {
            return Err(format!(
                "phase '{phase_name}': `summary:` is removed at phase level; \
                 use a `report:` block with `table <name> ...` items instead \
                 (SRD-46)"
            ));
        }
        // Per-phase `checkpoint:` declaration. Three forms
        // (short string, bool/none, full mapping) handled by
        // [`Checkpoint`]'s custom deserialize. Absent → None →
        // phase always re-runs on resume (per SRD-44 §"No
        // workload-level default").
        let checkpoint = phase_obj
            .get("checkpoint")
            .map(|v| serde_json::from_value::<crate::model::Checkpoint>(v.clone()))
            .transpose()
            .map_err(|e| format!("phase '{phase_name}' checkpoint: {e}"))?;

        // Phase-level `poll:` block (SRD-75). When present,
        // the phase's cycle execution runs in a wall-clock
        // loop until a Polydat predicate over captures returns
        // `true` or `timeout_ms` elapses. Distinct from the
        // OP-level `poll:` field (which lives on a single op
        // and wraps a `PollingDispenser` — SRD-32). The two
        // forms coexist; phase-poll is the synchronizer
        // pattern (SRD-75 driver use case), per-op poll is
        // the await-emptiness pattern.
        let phase_poll = match phase_obj.get("poll") {
            None => None,
            Some(v) => {
                let map = v.as_object().ok_or_else(|| {
                    format!(
                        "phase '{phase_name}': phase-level `poll:` must be a \
                     mapping with at least `until: <expr>`. Got a non-object \
                     value; if you intended an OP-level `poll:` flag, attach \
                     it to a specific op under `ops:` instead. SRD-75."
                    )
                })?;
                let until = map
                    .get("until")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| {
                        format!(
                            "phase '{phase_name}': phase-level `poll:` requires \
                         `until: <polydat-boolean-expression>`. SRD-75."
                        )
                    })?
                    .to_string();
                // Numbers or `{param}` / iter-var references
                // (resolved at the phase gather); anything else is
                // a load error — a malformed bound must never
                // silently fall back to the default.
                let numeric_or_ref = |field: &str| -> Result<Option<String>, String> {
                    match map.get(field) {
                        None => Ok(None),
                        Some(JVal::Number(n)) => Ok(Some(n.to_string())),
                        Some(JVal::String(s)) => Ok(Some(s.clone())),
                        Some(other) => Err(format!(
                            "phase '{phase_name}' poll: `{field}` must be a \
                             number or a {{param}} reference; got: {other}. \
                             SRD-75."
                        )),
                    }
                };
                let interval_ms = numeric_or_ref("interval_ms")?;
                let timeout_ms = numeric_or_ref("timeout_ms")?;
                let max_error_retries = numeric_or_ref("max_error_retries")?;
                let metric_name = map
                    .get("metric_name")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string());
                // SRD-75 §"Open questions" → §"on_timeout" — what
                // to do when the deadline fires. Closed vocabulary:
                // `error` (default; phase fails, scenario continues
                // by default error-routing policy) or `abort`
                // (request session stop; the whole run terminates).
                let on_timeout = match map.get("on_timeout") {
                    None => None,
                    Some(v) => {
                        let s = v.as_str().ok_or_else(|| {
                            format!(
                                "phase '{phase_name}' poll: `on_timeout` must be \
                             a string (`error` or `abort`). SRD-75."
                            )
                        })?;
                        let normalized = s.trim().to_ascii_lowercase();
                        if !matches!(normalized.as_str(), "error" | "abort") {
                            return Err(format!(
                                "phase '{phase_name}' poll: `on_timeout` must \
                                 be `error` or `abort`, got '{s}'. SRD-75."
                            ));
                        }
                        Some(normalized)
                    }
                };
                // SRD-75 (C5) — `require:` strict-gate selectors: a
                // single selector string or a list of them; each must
                // be a non-empty string.
                let require: Vec<String> = match map.get("require") {
                    None => Vec::new(),
                    Some(serde_json::Value::String(s)) => vec![s.clone()],
                    Some(serde_json::Value::Array(items)) => items
                        .iter()
                        .map(|v| {
                            v.as_str().map(str::to_string).ok_or_else(|| {
                                format!(
                                    "phase '{phase_name}' poll: `require` entries must \
                             be metric-selector strings. SRD-75."
                                )
                            })
                        })
                        .collect::<Result<Vec<_>, _>>()?,
                    Some(_) => {
                        return Err(format!(
                            "phase '{phase_name}' poll: `require` must be a \
                         selector string or a list of them. SRD-75."
                        ));
                    }
                };
                if let Some(bad) = require.iter().find(|s| s.trim().is_empty()) {
                    let _ = bad;
                    return Err(format!(
                        "phase '{phase_name}' poll: `require` entries must be \
                         non-empty metric selectors. SRD-75."
                    ));
                }
                // Reject keys outside the documented surface — a
                // typo like `tinerval_ms:` should fail loudly, not
                // silently default. SRD-30 unknown-field hygiene.
                let allowed = crate::vocab::phase_poll_fields();
                for k in map.keys() {
                    if !allowed.contains(&k.as_str()) {
                        return Err(format!(
                            "phase '{phase_name}' poll: unknown key '{k}'. \
                             Allowed: [{}]. SRD-75.",
                            allowed.join(", "),
                        ));
                    }
                }
                Some(crate::model::PhasePollSpec {
                    until,
                    interval_ms,
                    timeout_ms,
                    max_error_retries,
                    metric_name,
                    on_timeout,
                    require,
                })
            }
        };
        // SRD-75 §"Concurrency": phase-poll is sequential
        // within one phase activation — the predicate's
        // evaluation depends on a serial sequence of capture
        // writes. `concurrency > 1` against this shape doesn't
        // have a meaningful semantic; reject at parse time
        // rather than producing surprising runtime behavior.
        if phase_poll.is_some() {
            if let Some(ref c) = concurrency {
                let trimmed = c.trim();
                if trimmed != "1" && !trimmed.is_empty() {
                    return Err(format!(
                        "phase '{phase_name}': `poll:` (SRD-75) is \
                         incompatible with `concurrency: {c}` — phase-poll \
                         is sequential within one activation (the predicate \
                         depends on a serial sequence of capture writes). \
                         Drop `concurrency` or set it to 1."
                    ));
                }
            }
            if inline_ops.is_empty() {
                return Err(format!(
                    "phase '{phase_name}': `poll:` requires at least one op \
                     under `ops:` — captures are written by op execution; \
                     a phase with `poll:` and no ops has nothing to do. \
                     SRD-75."
                ));
            }
        }

        // `status_metrics:` — names of relevancy aggregates to
        // surface on the inline progress line and the per-phase
        // ✓ DONE summary. Accepts a YAML list (`[name, name]`),
        // a single string, or a comma-separated string. Empty /
        // absent → no metrics tail (nothing presumed present).
        let status_metrics: Vec<String> = match phase_obj.get("status_metrics") {
            None => Vec::new(),
            Some(JVal::Array(items)) => items
                .iter()
                .filter_map(|v| v.as_str().map(|s| s.trim().to_string()))
                .filter(|s| !s.is_empty())
                .collect(),
            Some(JVal::String(s)) => s
                .split(',')
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(String::from)
                .collect(),
            Some(other) => {
                return Err(format!(
                    "phase '{phase_name}' status_metrics: must be a list of metric \
                 names, a comma-separated string, or omitted; got {other:?}"
                ));
            }
        };

        // Phase-level `metrics:` — same schema as op `metrics:`,
        // but evaluated once at phase completion. Raw `value:`
        // expressions are preserved (no auto-inject into bindings):
        // the phase synthesiser emits `volatile __metric_<name> :=
        // <value>` directly, so a nondeterministic value such as
        // `phase_elapsed(phase_start)` is volatility-acknowledged
        // for strict mode in one place.
        let metrics = parse_phase_metrics_field(phase_obj.get("metrics"), phase_name)
            .map_err(|e| format!("phase '{phase_name}' metrics: {e}"))?;

        // Phase-level `dimensions:` — label names this phase introduces,
        // whose VALUES arrive from data through a metric's `cell:`. The
        // name is the structural declaration; declaring it here is what
        // lets a `cell:` reference be checked against the program before
        // any cycle runs, instead of surfacing as an attach-time panic on
        // the component tree.
        let dimensions = parse_dimensions_field(phase_obj.get("dimensions"), phase_name)?;

        // SRD-86 — phase `optimize:` block (workload-local config). A bare
        // string is sugar for `{ objective: <string> }` (see
        // `OptimizeBlock::from_yaml_value`).
        let optimize = match phase_obj.get("optimize") {
            Some(v) => Some(
                crate::model::OptimizeBlock::from_yaml_value(v)
                    .map_err(|e| format!("phase '{phase_name}' invalid `optimize` block: {e}"))?,
            ),
            None => None,
        };
        // A `cell:` must name a DECLARED dimension. This is the payoff for
        // reifying dimensions: the reference is checked against the program
        // at load, so a typo'd or undeclared dimension is a workload error
        // here rather than a component-tree surprise at attach time (or, in
        // the label-ownership case, a runtime panic).
        validate_cell_dimensions(phase_name, &dimensions, &inline_ops, &metrics)?;

        // SRD-109 — key-metric designations. Aggregate qualification is
        // MANDATORY: an unqualified family is a parse error carrying the
        // vocabulary, because there are no implied aggregates anywhere in
        // the reporting pipeline.
        let key_metrics: Vec<crate::model::KeyMetric> = match phase_obj.get("key_metrics") {
            None => Vec::new(),
            Some(v) => {
                let map = v.as_object().ok_or_else(|| {
                    format!(
                        "phase '{phase_name}': key_metrics must be a mapping of \
                     column: \"agg(family)\""
                    )
                })?;
                let mut out = Vec::new();
                for (col, spec) in map {
                    let spec = spec.as_str().ok_or_else(|| {
                        format!(
                            "phase '{phase_name}' key_metrics.{col}: expected a \
                         string \"agg(family)\""
                        )
                    })?;
                    let (agg_name, family) = spec
                        .trim()
                        .strip_suffix(')')
                        .and_then(|s| s.split_once('('))
                        .ok_or_else(|| {
                            format!(
                                "phase '{phase_name}' key_metrics.{col}: '{spec}' — \
                             aggregate qualification required; write agg(family). \
                             Aggregates: {}",
                                crate::model::KeyAgg::VOCAB
                            )
                        })?;
                    let agg = crate::model::KeyAgg::parse(agg_name.trim()).ok_or_else(|| {
                        format!(
                            "phase '{phase_name}' key_metrics.{col}: unknown \
                             aggregate '{}'. Aggregates: {}",
                            agg_name.trim(),
                            crate::model::KeyAgg::VOCAB
                        )
                    })?;
                    let family = family.trim().to_string();
                    match agg {
                        crate::model::KeyAgg::Span if !family.is_empty() => {
                            return Err(format!(
                                "phase '{phase_name}' key_metrics.{col}: span() is \
                                 family-less — it measures the activation wall clock"
                            ));
                        }
                        crate::model::KeyAgg::Span => {}
                        _ if family.is_empty() => {
                            return Err(format!(
                                "phase '{phase_name}' key_metrics.{col}: {}() needs \
                                 a family name",
                                agg_name.trim()
                            ));
                        }
                        _ => {}
                    }
                    out.push(crate::model::KeyMetric {
                        column: col.clone(),
                        agg,
                        family,
                    });
                }
                out
            }
        };

        phases.insert(
            phase_name.clone(),
            WorkloadPhase {
                dimensions,
                cycles,
                concurrency,
                rate,
                daemon,
                adapter,
                errors,
                tries,
                tries_backoff,
                interval: phase_obj
                    .get("interval")
                    .and_then(|v| v.as_str().map(str::to_string)),
                repeat: phase_obj.get("repeat").and_then(|v| v.as_u64()),
                error_rate_max,
                timeout,
                stop_when,
                throttle,
                tags,
                ops: inline_ops,
                for_each,
                continue_if,
                loop_scope,
                iter_scope,
                checkpoint,
                status_metrics,
                bindings: phase_bindings_only,
                metrics,
                poll: phase_poll,
                optimize,
                key_metrics,
            },
        );
        phase_order.push(phase_name.clone());
    }

    Ok((phases, phase_order))
}

// -----------------------------------------------------------------
// Blocks
// -----------------------------------------------------------------

fn parse_blocks(
    blocks_val: &JVal,
    doc_params: &HashMap<String, JVal>,
    doc_tags: &HashMap<String, String>,
    all_ops: &mut Vec<ParsedOp>,
) -> Result<(), String> {
    match blocks_val {
        JVal::Object(map) => {
            for (block_name, block_val) in map {
                parse_single_block(block_name, block_val, doc_params, doc_tags, all_ops)?;
            }
        }
        JVal::Array(arr) => {
            for (i, block_val) in arr.iter().enumerate() {
                let name = block_val
                    .get("name")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| format!("block{}", i + 1));
                parse_single_block(&name, block_val, doc_params, doc_tags, all_ops)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn parse_single_block(
    block_name: &str,
    block_val: &JVal,
    doc_params: &HashMap<String, JVal>,
    doc_tags: &HashMap<String, String>,
    all_ops: &mut Vec<ParsedOp>,
) -> Result<(), String> {
    let obj = match block_val.as_object() {
        Some(o) => o,
        None => return Ok(()),
    };

    // SRD-13f Push D: workload-level `bindings:` no longer
    // merge into block-level. Blocks are YAML organizational
    // sugar (not a Polydat scope), so a block's own `bindings:` is
    // *syntactic sugar* expanded into each enclosed op's
    // `op.bindings` at parse time — it does not cross any
    // kernel boundary, and the only "merge" left is this
    // block-sugar → op inlining inside `normalize_op_object`.
    let block_bindings = extract_bindings(obj.get("bindings"));
    let block_params = merge_value_maps(doc_params, &extract_value_map(obj.get("params")));
    let mut block_tags = merge_string_maps(doc_tags, &extract_string_map(obj.get("tags")));
    block_tags.insert("block".to_string(), block_name.to_string());

    // Find ops field
    for key in ["ops", "op", "operations", "statements", "statement"] {
        if let Some(ops_val) = obj.get(key) {
            parse_ops_field(
                ops_val,
                block_name,
                &block_bindings,
                &block_params,
                &block_tags,
                all_ops,
            )?;
            return Ok(());
        }
    }

    // If no ops field, check if the block value itself is a string (single op)
    if let Some(s) = block_val.as_str() {
        let mut op = ParsedOp::simple("stmt1", s);
        op.bindings = block_bindings;
        op.params = block_params;
        op.tags = block_tags;
        all_ops.push(op);
    }

    Ok(())
}

// -----------------------------------------------------------------
// Ops
// -----------------------------------------------------------------

fn parse_ops_field(
    ops_val: &JVal,
    block_name: &str,
    bindings: &BindingsDef,
    params: &HashMap<String, JVal>,
    tags: &HashMap<String, String>,
    all_ops: &mut Vec<ParsedOp>,
) -> Result<(), String> {
    // SRD-32a: activity/phase-scope param keys (cycles / concurrency /
    // rate / errors / error_rate_max) are consumed at phase/activity scope,
    // never as op fields. Strip them from the inherited params before they
    // reach any op, so an inherited `rate` doesn't collide with the `rate`
    // wrapper's field-ownership guard. A genuine op-level `rate:` reaches the
    // op via the typed `ParsedOp.rate` field, which is untouched.
    let op_scope_params = exclude_activity_keys(params);
    let params = &op_scope_params;
    let mut op_counter = 0;

    match ops_val {
        // Single string: op: "SELECT ..."
        JVal::String(s) => {
            op_counter += 1;
            let name = format!("stmt{op_counter}");
            let mut op = ParsedOp::simple(&name, s);
            op.bindings = bindings.clone();
            op.params = params.clone();
            op.tags = tags.clone();
            op.tags.insert("block".to_string(), block_name.to_string());
            all_ops.push(op);
        }

        // List of ops
        JVal::Array(arr) => {
            for item in arr {
                op_counter += 1;
                let auto_name = format!("stmt{op_counter}");
                let op = normalize_op_item(item, &auto_name, block_name, bindings, params, tags)?;
                all_ops.push(op);
            }
        }

        // Map of named ops
        JVal::Object(map) => {
            for (key, val) in map {
                let op = normalize_op_entry(key, val, block_name, bindings, params, tags)?;
                all_ops.push(op);
            }
        }

        _ => {}
    }

    Ok(())
}

/// Normalize a single op from a list item.
fn normalize_op_item(
    item: &JVal,
    auto_name: &str,
    block_name: &str,
    bindings: &BindingsDef,
    params: &HashMap<String, JVal>,
    tags: &HashMap<String, String>,
) -> Result<ParsedOp, String> {
    match item {
        JVal::String(s) => {
            let mut op = ParsedOp::simple(auto_name, s);
            op.bindings = bindings.clone();
            op.params = params.clone();
            op.tags = tags.clone();
            op.tags.insert("block".to_string(), block_name.to_string());
            Ok(op)
        }
        JVal::Object(map) => {
            // Check if first entry is name:stmt pattern
            if let Some((first_key, first_val)) = map.iter().next()
                && map.len() == 1
                && first_val.is_string()
            {
                let mut op = ParsedOp::simple(first_key, first_val.as_str().unwrap());
                op.bindings = bindings.clone();
                op.params = params.clone();
                op.tags = tags.clone();
                op.tags.insert("block".to_string(), block_name.to_string());
                return Ok(op);
            }
            // Full op object
            normalize_op_object(map, auto_name, block_name, bindings, params, tags)
        }
        _ => Ok(ParsedOp::simple(auto_name, "")),
    }
}

/// Normalize a named op from a map entry.
fn normalize_op_entry(
    key: &str,
    val: &JVal,
    block_name: &str,
    bindings: &BindingsDef,
    params: &HashMap<String, JVal>,
    tags: &HashMap<String, String>,
) -> Result<ParsedOp, String> {
    match val {
        JVal::String(s) => {
            let mut op = ParsedOp::simple(key, s);
            op.bindings = bindings.clone();
            op.params = params.clone();
            op.tags = tags.clone();
            op.tags.insert("block".to_string(), block_name.to_string());
            Ok(op)
        }
        JVal::Object(map) => normalize_op_object(map, key, block_name, bindings, params, tags),
        JVal::Array(arr) => {
            // Array at op level → moved to op.stmt
            let mut op_fields = HashMap::new();
            op_fields.insert("stmt".to_string(), JVal::Array(arr.clone()));
            let mut op = ParsedOp {
                traverse: None,
                name: key.to_string(),
                description: None,
                op: op_fields,
                bindings: bindings.clone(),
                params: params.clone(),
                tags: tags.clone(),
                condition: None,
                delay: None,
                metrics: HashMap::new(),
                result: None,
                wrappers: None,
                captures: Vec::new(),
                abstract_interface: None,
                interface_bound: false,
                daemon: crate::model::DaemonSpec::Disabled,
                daemon_cancel_grace_ms: None,
                while_cond: None,
                rate: None,
            };
            op.tags.insert("block".to_string(), block_name.to_string());
            Ok(op)
        }
        _ => Ok(ParsedOp::simple(key, "")),
    }
}

/// Human-readable name for a JSON value kind, for parse-time
/// error messages. ("string", "number", "array", etc.)
fn eval_value_kind(v: &JVal) -> &'static str {
    match v {
        JVal::Null => "null",
        JVal::Bool(_) => "bool",
        JVal::Number(_) => "number",
        JVal::String(_) => "string",
        JVal::Array(_) => "array",
        JVal::Object(_) => "mapping",
    }
}

/// Sub-keys allowed inside an op-template's `evaluations:`
/// block. The block is a reserved closed-vocab wrapper for
/// post-execution validation / scoring config — distinct from
/// per-adapter op fields. Anything else inside it is rejected at
/// parse time so silent-ignore traps (a misspelled `relevency:`,
/// a misplaced wrapper) cannot hide a misconfigured op. New
/// evaluation kinds are added to the construction registry.
fn evaluations_vocab() -> Vec<&'static str> {
    crate::vocab::evaluation_kinds()
}

/// Normalize a full op object (map of fields).
fn normalize_op_object(
    map: &serde_json::Map<String, JVal>,
    default_name: &str,
    block_name: &str,
    parent_bindings: &BindingsDef,
    parent_params: &HashMap<String, JVal>,
    parent_tags: &HashMap<String, String>,
) -> Result<ParsedOp, String> {
    let name = map
        .get("name")
        .and_then(|v| v.as_str())
        .unwrap_or(default_name)
        .to_string();

    let description = map
        .get("description")
        .or_else(|| map.get("desc"))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    // SRD-13f Push D: workload-level and phase-level
    // `bindings:` no longer touch ops at parse time — they
    // compile to their own kernels and reach ops via the GK
    // kernel chain. `parent_bindings` here carries ONLY
    // block-level YAML sugar (blocks are not a Polydat scope; their
    // `bindings:` field is a copy-paste reducer over each
    // enclosed op's bindings). The expansion below is the only
    // remaining parser-time inlining.
    let mut op_bindings =
        inline_block_sugar_into_op(parent_bindings, &extract_bindings(map.get("bindings")));
    let op_params = merge_value_maps(parent_params, &extract_value_map(map.get("params")));
    let mut op_tags = merge_string_maps(parent_tags, &extract_string_map(map.get("tags")));
    op_tags.insert("block".to_string(), block_name.to_string());

    // Determine op payload
    //
    // `reserved` lists keys handled by the workload model itself
    // (name, bindings, etc.) — they never reach the adapter.
    // `evaluations` is in this list because it's a closed-vocab
    // wrapper for validation/scoring config (relevancy, verify)
    // — its sub-keys are extracted and hoisted into `op_params`
    // below so downstream consumers
    // (`crate::validation::parse_relevancy` etc.) find them at
    // the same address whether the workload writes the
    // canonical wrapped form or the legacy top-level shorthand.
    // `metrics` and `result` are CORE op-template fields (SRD-40b
    // and SRD-66 respectively) extracted into ParsedOp.metrics /
    // ParsedOp.result; they must be kept out of `op_fields` so
    // adapters with a closed-vocabulary `known_op_fields()` (HTTP,
    // testkit) don't reject them as unknown. The CQL adapter
    // returns `None` from `known_op_fields()` (open vocabulary)
    // which masked this for the existing workloads.
    let reserved = crate::vocab::op_model_fields();
    let op_field_names = crate::vocab::op_stmt_fields();
    // Activity-level params excised from op fields before the
    // adapter sees them. `relevancy` / `verify` stay listed here
    // for the legacy top-level shorthand
    // (`relevancy: { ... }` directly under the op); the canonical
    // form puts them inside `evaluations:` and is handled
    // separately below.
    let activity_params = [
        "ratio",
        "adapter",
        "driver",
        "space",
        "instrument",
        "start-timers",
        "stop-timers",
        "verify",
        "relevancy",
        "strict",
        "poll",
        "poll_interval_ms",
        "timeout_ms",
        "poll_metric_name",
        "emit",
        "batch",
        "max_batch_size",
        "batchtype",
        "memo",
        "gutter",
        // SRD-63 op-level status visibility. `readout: visible` opts the
        // op into its own timed status leaf; excised into params so the
        // `readout` wrapper's trigger (which reads params) sees it rather
        // than the field falling through to the adapter op payload.
        "readout",
        // SRD-82 op shell — a per-op error-routing override (`errors:
        // "<pattern>:<actions>"`), resolved into a child of the phase policy
        // and pinned to this op's dispenser, and the op-level `tries:`
        // total-attempts sigil for the conditional tries wrapper. Both are
        // excised from op fields so they never reach the adapter as
        // op-payload keys.
        "errors",
        "tries",
        // Tries-wrapper companion knobs (SRD-82 Part 3b): retry
        // pacing and retry-error exemplar sampling (`exec_events`).
        // Consumed at wrapper build from op params; excised here so
        // the documented op-level standalone form actually lands in
        // params instead of leaking to the adapter as payload keys.
        "retry_backoff",
        "retry_backoff_max",
        "retry_backoff_ratio",
        "retry_exemplar_rate",
        "retry_exemplar_max_hz",
        "retry_advisory",
    ];

    let mut op_fields = if let Some(explicit_op) = op_field_names.iter().find_map(|k| map.get(*k)) {
        let mut m: HashMap<String, JVal> = match explicit_op {
            JVal::String(s) => {
                let mut m = HashMap::new();
                m.insert("stmt".to_string(), JVal::String(s.clone()));
                m
            }
            JVal::Object(o) => o.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
            other => {
                let mut m = HashMap::new();
                m.insert("stmt".to_string(), other.clone());
                m
            }
        };
        // Preserve sibling op-level fields so adapter-specific
        // extras (e.g. testkit's `result-latency`, `result-capacity`)
        // aren't silently dropped when the user writes shorthand:
        //
        //     insert:
        //       stmt: "INSERT ..."
        //       result-latency: "5ms"
        //
        // Without this loop the whole object would collapse to just
        // `stmt` and the sibling fields would never reach the adapter.
        // Keys already present in the explicit op payload win, so an
        // `op:` sub-object still has final say over its own shape.
        for (k, v) in map.iter() {
            if reserved.contains(&k.as_str())
                || op_field_names.contains(&k.as_str())
                || activity_params.contains(&k.as_str())
            {
                continue;
            }
            m.entry(k.clone()).or_insert_with(|| v.clone());
        }
        m
    } else {
        // All non-reserved, non-activity-param fields become op fields
        map.iter()
            .filter(|(k, _)| {
                !reserved.contains(&k.as_str())
                    && !op_field_names.contains(&k.as_str())
                    && !activity_params.contains(&k.as_str())
            })
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    };

    // Excise activity-level params from op fields into params
    let mut op_params = op_params;
    for ap in &activity_params {
        if let Some(val) = map.get(*ap) {
            // Activity params excised from op fields into params map
            op_params.insert(ap.to_string(), val.clone());
        }
    }

    // Canonical `evaluations:` wrapper — closed-vocab
    // validation/scoring config. Sub-keys are extracted and
    // hoisted into `op_params` so downstream consumers (e.g.
    // `crate::validation::parse_relevancy`,
    // `crate::validation::parse_assertions`) find them at the
    // same address whether the workload uses this canonical
    // form or the legacy top-level shorthand. Anything inside
    // `evaluations:` that isn't in `EVALUATIONS_VOCAB` is
    // rejected up front — the whole point of the wrapper is to
    // catch misspellings (`relevency:`) and misplaced wrappers
    // that the silent-routing path would otherwise drop on the
    // floor.
    if let Some(eval_val) = map.get("evaluations") {
        let eval_obj = eval_val.as_object().ok_or_else(|| {
            format!(
                "op '{name}' (block '{block_name}'): `evaluations:` must be a \
             mapping, got {kind}. Expected shape: \
             `evaluations: {{ relevancy: {{...}}, verify: [...] }}`.",
                kind = eval_value_kind(eval_val),
            )
        })?;
        for (k, v) in eval_obj.iter() {
            if !evaluations_vocab().contains(&k.as_str()) {
                return Err(format!(
                    "op '{name}' (block '{block_name}'): unknown key \
                     '{k}' under `evaluations:`. Allowed keys: [{}]. \
                     Each entry under `evaluations:` is a distinct \
                     post-execution evaluation kind — typos and \
                     misplaced wrappers are rejected here so silent \
                     skipped recall / verify can't happen.",
                    evaluations_vocab().join(", "),
                ));
            }
            // Top-level shorthand wins on collision so users
            // who already have `relevancy: {...}` at the op
            // level don't see their config replaced if they
            // also added `evaluations: { relevancy: {...} }`.
            // Warn so the duplicate is visible.
            if op_params.contains_key(k.as_str()) {
                eprintln!(
                    "warning: op '{name}' has '{k}' both at top level \
                     and under `evaluations:` — top-level wins. Pick \
                     one form.",
                );
                continue;
            }
            op_params.insert(k.clone(), v.clone());
        }
    }

    // SRD-108 Part B (+ SRD-109 Part 3) — `abstract:` declares
    // this op as a typed slot: `{ needs: {name: type,…}, yields:
    // {name: type,…}, results: {name: type,…} }`. Unknown keys
    // are rejected; types are polydat DSL type names, verified
    // against the compiled op-template program at pre-map
    // synthesis.
    let abstract_interface: Option<crate::model::OpInterface> = match map.get("abstract") {
        None => None,
        Some(v) => {
            let obj = v.as_object().ok_or_else(|| format!(
                    "op '{name}': `abstract:` must be a mapping with                      `needs:` / `yields:` / `results:` maps of wire-name -> type"))?;
            let mut iface = crate::model::OpInterface::default();
            for (k, section) in obj {
                let target = match k.as_str() {
                    "needs" => &mut iface.needs,
                    "yields" => &mut iface.yields,
                    "results" => &mut iface.results,
                    other => {
                        return Err(format!(
                            "op '{name}': unknown key '{other}' under                              `abstract:` (allowed: needs, yields, results)"
                        ));
                    }
                };
                let entries = section.as_object().ok_or_else(|| format!(
                        "op '{name}': `abstract.{k}:` must be a mapping                          of wire-name -> type"))?;
                for (wire, typ) in entries {
                    let type_name = typ.as_str().ok_or_else(|| format!(
                            "op '{name}': `abstract.{k}.{wire}:` type                              must be a string, got {typ}"))?;
                    target.insert(wire.clone(), type_name.to_string());
                }
            }
            Some(iface)
        }
    };

    let condition = map
        .get("if")
        .and_then(|v| v.as_str())
        .map(normalize_condition_clause);

    let delay = match map.get("delay") {
        None => None,
        Some(v) => {
            Some(crate::model::parse_delay_spec_value(v).map_err(|e| format!("op '{name}': {e}"))?)
        }
    };

    // Daemon-op declaration. Parses bool / int / "on"/"off" /
    // "true"/"false" via `parse_daemon_spec_value`. When set
    // to MaxFibers(N), the cycle-pool dispatch spawns the op
    // on a daemon fiber instead of awaiting inline; the cap
    // is enforced at spawn time.
    let daemon = match map.get("daemon") {
        Some(v) => crate::model::parse_daemon_spec_value(v)
            .map_err(|e| format!("op '{name}' (block '{block_name}'): {e}",))?,
        None => crate::model::DaemonSpec::Disabled,
    };
    let daemon_cancel_grace_ms = map.get("daemon_cancel_grace_ms").and_then(|v| {
        v.as_u64()
            .or_else(|| v.as_str().and_then(|s| s.parse::<u64>().ok()))
    });
    let daemon_enabled = !daemon.is_disabled();
    if daemon_enabled {
        // `cycles:` and `ratio:` on a daemon op are workload-shape
        // errors — the daemon's dispatch cadence is governed by
        // the cycle-pool walks + per-op `rate:`, not by cycles or
        // ratios. Silently accepting them would hide a mis-shaped
        // workload; reject at parse so the operator sees the
        // contradiction immediately.
        if let Some(c) = op_params.get("cycles") {
            return Err(format!(
                "op '{name}' (block '{block_name}'): `daemon:` and \
                 `cycles: {c}` are mutually exclusive. A daemon op's \
                 dispatch cadence is governed by the cycle-pool's \
                 stanza walk + per-op `rate:`, not by `cycles:`."
            ));
        }
        if let Some(r) = op_params.get("ratio") {
            return Err(format!(
                "op '{name}' (block '{block_name}'): `daemon:` and \
                 `ratio: {r}` are mutually exclusive — a daemon op \
                 does not participate in cycle-pool ratio scheduling."
            ));
        }
    }
    if !daemon_enabled && daemon_cancel_grace_ms.is_some() {
        return Err(format!(
            "op '{name}' (block '{block_name}'): \
             `daemon_cancel_grace_ms` is only meaningful when \
             `daemon:` is enabled (true / N). Either enable `daemon:` \
             or drop the grace field."
        ));
    }

    // Loop / rate primitives (apply to both cycle-pool and
    // daemon ops, though the typical use case is daemon+while+rate).
    let while_cond = map
        .get("while")
        .and_then(|v| v.as_str())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty());
    let rate = map.get("rate").and_then(|v| {
        v.as_str()
            .map(|s| s.to_string())
            .or_else(|| v.as_u64().map(|n| n.to_string()))
            .or_else(|| v.as_f64().map(|f| f.to_string()))
    });
    // Rate without while- or per-cycle dispatch is meaningless —
    // an op that fires once-per-cycle can't be rate-limited in
    // any observable way. Warn at parse rather than silently
    // accepting a no-op field.
    if rate.is_some() && while_cond.is_none() && daemon.is_disabled() {
        // Soft warn via stderr; this is a workload-shape smell
        // but not always an error (a workload-author may be
        // configuring forward-compatible op templates).
        eprintln!(
            "warning: op '{name}' (block '{block_name}'): `rate:` is set \
             but the op has neither `while:` nor `daemon:`. The rate \
             limit will only fire on each cycle-pool dispatch — likely \
             not the intended behavior. Add `while:` for a loop or \
             `daemon:` to enable fiber-kind dispatch.",
        );
    }

    let metrics = parse_metrics_field(map.get("metrics"), &name, &mut op_bindings)
        .map_err(|e| format!("op '{name}' metrics: {e}"))?;
    let traverse = parse_traverse_field(map.get("traverse"), &name)?;
    let result = parse_result_field(map.get("result"), &name)
        .map_err(|e| format!("op '{name}' result: {e}"))?;

    // Extract capture-point specs from every string-valued entry in
    // `op` and replace each value with the bracket-stripped form.
    // Adapters consume the cleaned text; `TraversingDispenser` and
    // other capture-aware wrappers read the harvested specs from
    // `ParsedOp.captures` directly — no re-parse at wrap-time.
    //
    // De-duplicates by `as_name` across multiple op-fields: the
    // same wire name appearing in two fields means the same
    // capture, not two separate writes.
    let mut captures: Vec<crate::bindpoints::CapturePoint> = Vec::new();
    // Declarative `capture:` map block — pulls JSON-Pointer-keyed
    // values out of structured response bodies (e.g. Jolokia
    // bulk-POST arrays). Each entry is a path string addressed
    // by [`serde_json::Value::pointer`]; a trailing `:count`
    // collapses the addressed sub-tree to a u64 count instead
    // of capturing it as-is. This complements the legacy
    // bracket form `[name]` embedded in op text — that form
    // still works for adapters whose statements have column
    // references; the declarative form is for adapters whose
    // responses are JSON and need positional / nested access.
    if let Some(cap_val) = map.get("capture") {
        let cap_obj = cap_val.as_object().ok_or_else(|| {
            format!(
                "op '{name}' (block '{block_name}'): `capture:` must be a \
             mapping of <wire-name> → <json-pointer-path>. Got {kind}.",
                kind = eval_value_kind(cap_val),
            )
        })?;
        for (wire_name, spec_val) in cap_obj.iter() {
            let raw = spec_val.as_str().ok_or_else(|| {
                format!(
                    "op '{name}' (block '{block_name}'): `capture.{wire_name}` \
                 must be a string (JSON-Pointer path, optionally with a \
                 `:count` suffix). Got {kind}.",
                    kind = eval_value_kind(spec_val),
                )
            })?;
            let (path, count, agg, row_filter) = match raw.strip_suffix(":count") {
                Some(p) => (p.to_string(), true, None, None),
                None => match parse_capture_agg_suffix(raw) {
                    Some((p, a, f)) => (p, false, Some(a), f),
                    None => (raw.to_string(), false, None, None),
                },
            };
            // Two accepted spellings, one meaning — but never a guess in
            // between:
            //
            //   `/value`, `/0/name`  RFC 6901 JSON-Pointer, used verbatim.
            //   `value`              a bare top-level member name, which is
            //                        exactly `/value` and normalized to it
            //                        here, so runtime has one form to resolve.
            //   ``                   the root document.
            //
            // A bare name may contain neither `/` nor `~`, and that is the
            // whole anti-ambiguity rule: `a/b` could be a pointer someone
            // forgot to lead with '/', or a member literally named "a/b"
            // (RFC 6901 spells that `/a~1b`), and `~0`/`~1` are pointer
            // escapes. Rather than pick a reading, name both and let the
            // author say which. `verify:`'s `field:` is a bare name by
            // definition, so an author moving between the two blocks lands
            // on the bare form naturally — it now works rather than erroring.
            let path = if path.is_empty() || path.starts_with('/') {
                path
            } else if !path.contains('/') && !path.contains('~') {
                format!("/{path}")
            } else {
                return Err(format!(
                    "op '{name}' (block '{block_name}'): \
                     `capture.{wire_name}` path '{path}' is ambiguous. Write \
                     a JSON-Pointer starting with '/' (e.g. '/{first}/...', \
                     escaping '~' as '~0' and '/' as '~1'), or a bare \
                     top-level member name containing neither '/' nor '~'. \
                     An empty path addresses the root document.",
                    first = path.split('/').next().unwrap_or(&path),
                ));
            };
            captures.push(crate::bindpoints::CapturePoint {
                row_filter,
                source_name: wire_name.clone(),
                as_name: wire_name.clone(),
                cast_type: None,
                slurp: false,
                path: Some(path),
                count,
                agg,
            });
        }
    }
    for value in op_fields.values_mut() {
        if let serde_json::Value::String(s) = value {
            let parsed = crate::bindpoints::parse_capture_points(s);
            if parsed.captures.is_empty() {
                continue;
            }
            for cap in parsed.captures {
                if !captures
                    .iter()
                    .any(|existing| existing.as_name == cap.as_name)
                {
                    captures.push(cap);
                }
            }
            *s = parsed.raw_template;
        }
    }

    Ok(ParsedOp {
        traverse,
        name,
        description,
        op: op_fields,
        bindings: op_bindings,
        params: op_params,
        tags: op_tags,
        condition,
        delay,
        metrics,
        result,
        wrappers: None,
        captures,
        abstract_interface,
        interface_bound: false,
        daemon,
        daemon_cancel_grace_ms,
        while_cond,
        rate,
    })
}

/// SRD-40b §1 + §2: parse the `metrics:` field on an op
/// template. Three YAML shapes accepted, dispatched on the
/// value's type:
///
/// - **Scalar** (bare string, §2.1): one metric with the
///   string as both family and `value:`.
/// - **Sequence** (list, §2.2): each entry is a bare-name
///   string OR a `name := <Polydat expression>` wire-expression.
///   Wire expressions are auto-injected into the op's
///   `bindings:` block; the metric is then a bare-name
///   reference to the new wire.
/// - **Mapping** (object, §2.3): canonical full-shape form
///   keyed by metric name. Each value is either a string
///   (treated as `value:`) or a full `MetricSpec` mapping.
/// Parses a `:min(field)` / `:max(field)` / `:sum(field)` suffix on a
/// declarative capture path, returning `(path_prefix, agg)`. The path
/// prefix may be empty (root = the whole rows array). Returns `None`
/// when the spec carries no recognized aggregation suffix.
/// Split an optional `where <field>='<value>'` predicate off the tail of an
/// aggregate's argument, returning `(field_expr, predicate)`.
///
/// `progress where kind='secondary index build'` →
/// `("progress", Some(("kind", "secondary index build")))`.
///
/// Single or double quotes; the value may contain spaces, which is why it is
/// quoted rather than whitespace-delimited.
fn split_capture_row_filter(arg: &str) -> (String, Option<(String, String)>) {
    let lower = arg.to_ascii_lowercase();
    let Some(idx) = lower.find(" where ") else {
        return (arg.trim().to_string(), None);
    };
    let field = arg[..idx].trim().to_string();
    let pred = arg[idx + " where ".len()..].trim();
    let Some((k, v)) = pred.split_once('=') else {
        return (arg.trim().to_string(), None);
    };
    let k = k.trim();
    let v = v.trim();
    let unquoted = v
        .strip_prefix('\'')
        .and_then(|r| r.strip_suffix('\''))
        .or_else(|| v.strip_prefix('"').and_then(|r| r.strip_suffix('"')));
    match unquoted {
        Some(val) if !k.is_empty() => (field, Some((k.to_string(), val.to_string()))),
        // An unquoted or malformed predicate is left alone rather than
        // silently half-applied — the caller then treats the whole string as a
        // field name and the unknown-field error names it.
        _ => (arg.trim().to_string(), None),
    }
}

fn parse_capture_agg_suffix(
    raw: &str,
) -> Option<(
    String,
    crate::bindpoints::CaptureAgg,
    Option<(String, String)>,
)> {
    use crate::bindpoints::CaptureAgg;
    if !raw.ends_with(')') {
        return None;
    }
    for (tag, make) in [
        (":min(", CaptureAgg::Min as fn(String) -> CaptureAgg),
        (":max(", CaptureAgg::Max as fn(String) -> CaptureAgg),
        (":sum(", CaptureAgg::Sum as fn(String) -> CaptureAgg),
    ] {
        if let Some(idx) = raw.rfind(tag) {
            let arg = &raw[idx + tag.len()..raw.len() - 1];
            let (field, row_filter) = split_capture_row_filter(arg);
            if !field.is_empty() && field.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                return Some((raw[..idx].to_string(), make(field), row_filter));
            }
        }
    }
    None
}

fn parse_metrics_field(
    val: Option<&JVal>,
    op_name: &str,
    op_bindings: &mut BindingsDef,
) -> Result<HashMap<String, MetricSpec>, String> {
    use crate::model::MetricSpec;
    let Some(v) = val else {
        return Ok(HashMap::new());
    };
    let mut out: HashMap<String, MetricSpec> = HashMap::new();
    match v {
        JVal::String(s) => {
            let name = s.trim().to_string();
            if name.is_empty() {
                return Err("scalar form requires a metric name".into());
            }
            out.insert(
                name.clone(),
                MetricSpec {
                    value: name,
                    family: None,
                    kind: None,
                    unit: None,
                    format: None,
                    cell: Default::default(),
                },
            );
        }
        JVal::Array(items) => {
            for (idx, item) in items.iter().enumerate() {
                let raw = item.as_str().ok_or_else(|| {
                    format!(
                        "metrics list entry {idx}: must be a string \
                     (bare name or `name := <polydat expr>`)"
                    )
                })?;
                let trimmed = raw.trim();
                if let Some((name, expr)) = trimmed.split_once(":=") {
                    // Wire-expression form: declare the binding +
                    // register the metric.
                    let name = name.trim();
                    let expr = expr.trim();
                    if name.is_empty() || expr.is_empty() {
                        return Err(format!(
                            "metrics list entry {idx} '{raw}': wire \
                             expression must be `name := <expression>`"
                        ));
                    }
                    inject_wire_into_bindings(op_bindings, name, expr, op_name)?;
                    if out.contains_key(name) {
                        return Err(format!("duplicate metric wire '{name}' in metrics list"));
                    }
                    out.insert(
                        name.to_string(),
                        MetricSpec {
                            value: name.to_string(),
                            family: None,
                            kind: None,
                            unit: None,
                            format: None,
                            cell: Default::default(),
                        },
                    );
                } else {
                    // Bare-name form.
                    if trimmed.is_empty() {
                        return Err(format!("metrics list entry {idx}: empty name"));
                    }
                    if out.contains_key(trimmed) {
                        return Err(format!("duplicate metric '{trimmed}' in metrics list"));
                    }
                    out.insert(
                        trimmed.to_string(),
                        MetricSpec {
                            value: trimmed.to_string(),
                            family: None,
                            kind: None,
                            unit: None,
                            format: None,
                            cell: Default::default(),
                        },
                    );
                }
            }
        }
        JVal::Object(map) => {
            for (key, val) in map {
                if out.contains_key(key) {
                    return Err(format!("duplicate metric key '{key}' in metrics map"));
                }
                let mut spec = parse_metric_spec_value(val, key)?;
                // SRD-13d Phase 9 mapping-form auto-inject: if
                // `value:` isn't a bare name, promote it to an
                // op-template binding `<key> := <value>` and
                // replace the spec's `value:` with the bare key.
                // Mirrors the list-form `name := expr` flow.
                let value_trimmed = spec.value.trim();
                let bare = !value_trimmed.is_empty()
                    && value_trimmed
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_');
                if !bare {
                    if !is_valid_ident(key) {
                        return Err(format!(
                            "metric '{key}' value '{value}' is a non-bare \
                             expression so the metric key must itself be a \
                             valid identifier (alphanumerics + underscore, \
                             not starting with a digit) so it can be used \
                             as a binding name. Rename the metric key, or \
                             move the expression into `bindings:` and set \
                             `value:` to the bare name.",
                            value = spec.value
                        ));
                    }
                    inject_wire_into_bindings(op_bindings, key, value_trimmed, op_name)?;
                    spec.value = key.clone();
                }
                out.insert(key.clone(), spec);
            }
        }
        _ => {
            return Err(format!(
                "metrics: expected scalar, sequence, or mapping; got {v:?}"
            ));
        }
    }
    Ok(out)
}

/// Parse a phase-level `metrics:` field. Same three YAML shapes as
/// the op-level [`parse_metrics_field`] (scalar / sequence / mapping)
/// and the same [`MetricSpec`] schema, with one deliberate
/// difference: phase metrics do **not** auto-inject non-bare value
/// expressions into a `bindings:` block. Op metrics inject so the
/// closure-binding-economy walker can allocate magic-extern slots
/// (`body`/`count`/`ok`) for the value expression; phase metrics have
/// no result body and are pulled directly by the executor from
/// `__metric_<name>`, so the phase synthesiser emits
/// `volatile __metric_<name> := <value>` straight from the raw
/// `value:` expression preserved here.
/// Parse a phase-level `dimensions:` block.
///
/// ```yaml
/// dimensions:
///   tier: { type: str }
///   tier: str            # shorthand
/// ```
///
/// Unknown keys are rejected rather than dropped: a silently-ignored
/// dimension declaration would make a metric's `cell:` reference fail its
/// existence check for a reason nothing points at.
/// Check every `cell:` reference in a phase against its declared dimensions.
///
/// Both tiers are covered: the phase's own `metrics:` and each inline op's.
/// The error names the declared set, because the common failure is a name
/// declared one tier away rather than a nonsense name.
fn validate_cell_dimensions(
    phase_name: &str,
    dimensions: &std::collections::BTreeMap<String, crate::model::DimensionSpec>,
    ops: &[ParsedOp],
    phase_metrics: &HashMap<String, MetricSpec>,
) -> Result<(), String> {
    let declared = || {
        if dimensions.is_empty() {
            "none are declared on this phase".to_string()
        } else {
            format!(
                "declared here: {}",
                dimensions.keys().cloned().collect::<Vec<_>>().join(", ")
            )
        }
    };
    let check = |site: &str, metric: &str, spec: &MetricSpec| -> Result<(), String> {
        for dim in spec.cell.keys() {
            if !dimensions.contains_key(dim) {
                return Err(format!(
                    "phase '{phase_name}' {site} metric '{metric}': cell \
                     dimension '{dim}' is not declared. A dimension is a label \
                     name owned by exactly one tier, so declare it on the phase \
                     ({dim}: str) before placing a metric in it — {}",
                    declared()
                ));
            }
        }
        Ok(())
    };
    for (name, spec) in phase_metrics {
        check("phase-level", name, spec)?;
    }
    for op in ops {
        for (name, spec) in &op.metrics {
            check(&format!("op '{}'", op.name), name, spec)?;
        }
    }
    Ok(())
}

fn parse_dimensions_field(
    val: Option<&JVal>,
    phase_name: &str,
) -> Result<std::collections::BTreeMap<String, crate::model::DimensionSpec>, String> {
    use crate::model::{DimensionSpec, DimensionType};
    let mut out = std::collections::BTreeMap::new();
    let Some(v) = val else { return Ok(out) };
    let JVal::Object(map) = v else {
        return Err(format!(
            "phase '{phase_name}' dimensions: expected a mapping of \
             <name>: <declaration>, got {v:?}"
        ));
    };
    for (name, decl) in map {
        if !is_valid_ident(name) {
            return Err(format!(
                "phase '{phase_name}' dimension '{name}': a dimension name \
                 becomes a metric label, so it must be a valid identifier \
                 (alphanumerics + underscore, not starting with a digit)"
            ));
        }
        let value_type = match decl {
            // Shorthand: `tier: str`
            JVal::String(s) => parse_dimension_type(s, phase_name, name)?,
            JVal::Object(obj) => {
                for k in obj.keys() {
                    if k != "type" {
                        return Err(format!(
                            "phase '{phase_name}' dimension '{name}': unknown \
                             field `{k}`. Recognised fields: type"
                        ));
                    }
                }
                match obj.get("type") {
                    None => DimensionType::default(),
                    Some(JVal::String(s)) => parse_dimension_type(s, phase_name, name)?,
                    Some(other) => {
                        return Err(format!(
                            "phase '{phase_name}' dimension '{name}' type: \
                         expected a string, got {other:?}"
                        ));
                    }
                }
            }
            other => {
                return Err(format!(
                    "phase '{phase_name}' dimension '{name}': expected a type \
                 name or a mapping, got {other:?}"
                ));
            }
        };
        out.insert(name.clone(), DimensionSpec { value_type });
    }
    Ok(out)
}

fn parse_dimension_type(
    s: &str,
    phase_name: &str,
    dim: &str,
) -> Result<crate::model::DimensionType, String> {
    match s.trim().to_ascii_lowercase().as_str() {
        "str" | "string" => Ok(crate::model::DimensionType::Str),
        other => Err(format!(
            "phase '{phase_name}' dimension '{dim}' type '{other}': label \
             values are strings; `str` is the only supported type"
        )),
    }
}

fn parse_phase_metrics_field(
    val: Option<&JVal>,
    phase_name: &str,
) -> Result<HashMap<String, MetricSpec>, String> {
    use crate::model::MetricSpec;
    let Some(v) = val else {
        return Ok(HashMap::new());
    };
    let mut out: HashMap<String, MetricSpec> = HashMap::new();
    match v {
        JVal::String(s) => {
            // Scalar: a bare wire name used as both family and value.
            let name = s.trim().to_string();
            if name.is_empty() {
                return Err("scalar form requires a metric name".into());
            }
            out.insert(
                name.clone(),
                MetricSpec {
                    value: name,
                    family: None,
                    kind: None,
                    unit: None,
                    format: None,
                    cell: Default::default(),
                },
            );
        }
        JVal::Array(items) => {
            // Sequence: bare wire names only (no `name := expr` form —
            // phase metrics don't inject bindings).
            for (idx, item) in items.iter().enumerate() {
                let raw = item
                    .as_str()
                    .ok_or_else(|| format!("metrics list entry {idx}: must be a bare wire name"))?;
                let name = raw.trim();
                if name.is_empty() {
                    return Err(format!("metrics list entry {idx}: empty name"));
                }
                if name.contains(":=") {
                    return Err(format!(
                        "metrics list entry {idx} '{raw}': the `name := expr` \
                         form is op-only; for a phase, declare the wire in the \
                         phase `bindings:` block and list its bare name here, \
                         or use the mapping form `{{ {name}: {{ value: <expr> }} }}`"
                    ));
                }
                if out.contains_key(name) {
                    return Err(format!("duplicate metric '{name}' in metrics list"));
                }
                out.insert(
                    name.to_string(),
                    MetricSpec {
                        value: name.to_string(),
                        family: None,
                        kind: None,
                        unit: None,
                        format: None,
                        cell: Default::default(),
                    },
                );
            }
        }
        JVal::Object(map) => {
            // Mapping: canonical full-shape form. Raw `value:` kept.
            for (key, val) in map {
                if out.contains_key(key) {
                    return Err(format!("duplicate metric key '{key}' in metrics map"));
                }
                out.insert(key.clone(), parse_metric_spec_value(val, key)?);
            }
        }
        _ => {
            return Err(format!(
                "phase '{phase_name}' metrics: expected scalar, sequence, or \
             mapping; got {v:?}"
            ));
        }
    }
    Ok(out)
}

/// Parse one entry under the mapping form of `metrics:`.
/// Accepts a bare string (treated as `value:`) or a full
/// `MetricSpec` object.
fn parse_metric_spec_value(v: &JVal, key: &str) -> Result<crate::model::MetricSpec, String> {
    use crate::model::MetricSpec;
    match v {
        JVal::String(s) => Ok(MetricSpec {
            value: s.clone(),
            family: None,
            kind: None,
            unit: None,
            format: None,
            cell: Default::default(),
        }),
        JVal::Object(map) => {
            // SRD-30 unknown-field hygiene: reject any key outside the
            // MetricSpec surface rather than silently dropping it (a
            // dropped `kind:` would let a counter/histogram silently
            // default to gauge). The instrument-type discriminator is
            // `kind`, not `type` — `type` is the word OpenMetrics /
            // Prometheus use, so it's the predictable mistake; give it
            // a targeted hint.
            const KNOWN: &[&str] = &["value", "family", "kind", "unit", "format", "cell"];
            for k in map.keys() {
                if KNOWN.contains(&k.as_str()) {
                    continue;
                }
                let hint = if k == "type" {
                    " — the instrument-type discriminator is `kind` \
                     (gauge | histogram | counter)"
                } else {
                    ""
                };
                return Err(format!(
                    "metric '{key}': unknown field `{k}`{hint}. Recognised \
                     fields: value, family, kind, unit, format, cell"
                ));
            }
            let value = map
                .get("value")
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    format!(
                        "metric '{key}': required field `value:` missing or \
                     not a string"
                    )
                })?
                .to_string();
            let family = map.get("family").and_then(|v| v.as_str()).map(String::from);
            let unit = map.get("unit").and_then(|v| v.as_str()).map(String::from);
            let format = map.get("format").and_then(|v| v.as_str()).map(String::from);
            // Validate format syntax at parse time so the user
            // hears about a bad `#.##` pattern at workload
            // load, not first-cycle. SRD-40b §1.
            if let Some(f) = format.as_deref() {
                crate::metric_format::parse_format_spec(f)
                    .map_err(|e| format!("metric '{key}' format '{f}': {e}"))?;
            }
            let kind = match map.get("kind") {
                None => None,
                Some(JVal::String(s)) => Some(parse_metric_kind(s, key)?),
                Some(other) => {
                    return Err(format!(
                        "metric '{key}' kind: expected string, got {other:?}"
                    ));
                }
            };
            // `cell:` — dimensional placement. Each entry is
            // `<dimension>: <polydat expression>`; the expression is
            // reified by scope synthesis as a typed kernel binding, so a
            // coordinate is compiled and checkable rather than a string
            // assembled at runtime.
            let mut cell = std::collections::BTreeMap::new();
            match map.get("cell") {
                None => {}
                Some(JVal::Object(dims)) => {
                    if dims.is_empty() {
                        return Err(format!(
                            "metric '{key}' cell: empty. Omit `cell:` entirely, \
                             or name at least one dimension."
                        ));
                    }
                    for (dim, expr) in dims {
                        let expr = expr.as_str().ok_or_else(|| {
                            format!(
                                "metric '{key}' cell '{dim}': expected a polydat \
                             expression string, got {expr:?}"
                            )
                        })?;
                        if expr.trim().is_empty() {
                            return Err(format!("metric '{key}' cell '{dim}': empty expression"));
                        }
                        if !is_valid_ident(dim) {
                            return Err(format!(
                                "metric '{key}' cell '{dim}': a dimension name \
                                 becomes a metric label, so it must be a valid \
                                 identifier (alphanumerics + underscore, not \
                                 starting with a digit)"
                            ));
                        }
                        cell.insert(dim.clone(), expr.trim().to_string());
                    }
                }
                Some(other) => {
                    return Err(format!(
                        "metric '{key}' cell: expected a mapping of \
                     <dimension>: <expression>, got {other:?}"
                    ));
                }
            }
            Ok(MetricSpec {
                value,
                family,
                kind,
                unit,
                format,
                cell,
            })
        }
        _ => Err(format!(
            "metric '{key}': expected string or mapping, got {v:?}"
        )),
    }
}

fn parse_metric_kind(s: &str, key: &str) -> Result<crate::model::MetricKind, String> {
    use crate::model::MetricKind;
    match s.to_ascii_lowercase().as_str() {
        "gauge" => Ok(MetricKind::Gauge),
        "histogram" => Ok(MetricKind::Histogram),
        "counter" => Ok(MetricKind::Counter),
        other => Err(format!(
            "metric '{key}' kind '{other}': expected one of \
             gauge / histogram / counter"
        )),
    }
}

/// Auto-inject `name := expr` into the op template's
/// `bindings:` block (per SRD-40b §2.2). Conflicts with an
/// existing declaration of the same name are a strict
/// workload parse error per §2.2.
fn inject_wire_into_bindings(
    bindings: &mut BindingsDef,
    name: &str,
    expr: &str,
    op_name: &str,
) -> Result<(), String> {
    // Look for an existing same-name declaration to refuse
    // shadowing. The check is textual: a line beginning with
    // `<name>` followed by whitespace + `:=`. Prefix matching
    // would surface false positives for `foo` vs `foobar`,
    // hence the boundary check.
    let line_to_inject = format!("{name} := {expr}\n");
    // BindingsDef has no `Empty` variant — `Map(empty)` is the
    // default. Detect emptiness via the existing helper, then
    // promote to PolydatSource for injection (we're adding a real
    // Polydat statement, not a name→expr pair the legacy Map form
    // can't carry alone).
    if bindings.is_empty() {
        *bindings = BindingsDef::PolydatSource(line_to_inject);
        return Ok(());
    }
    match bindings {
        BindingsDef::PolydatSource(src) => {
            if has_binding_named(src, name) {
                return Err(format!(
                    "metric wire '{name}' (op '{op_name}') collides \
                     with existing `bindings:` declaration of the \
                     same name"
                ));
            }
            if !src.ends_with('\n') {
                src.push('\n');
            }
            src.push_str(&line_to_inject);
        }
        BindingsDef::Map(map) => {
            if map.contains_key(name) {
                return Err(format!(
                    "metric wire '{name}' (op '{op_name}') collides \
                     with existing `bindings:` declaration of the \
                     same name"
                ));
            }
            map.insert(name.to_string(), expr.to_string());
        }
    }
    Ok(())
}

/// True when `s` is a valid Polydat identifier: non-empty, first
/// char is a letter or underscore, remaining chars are
/// alphanumerics or underscore. Used by the mapping-form
/// metric auto-inject to confirm the metric key can stand in
/// as a binding name.
fn is_valid_ident(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_alphabetic() || c == '_' => {}
        _ => return false,
    }
    chars.all(|c| c.is_alphanumeric() || c == '_')
}

/// True when the Polydat source contains a binding line
/// `<name> := …` at the start of any (whitespace-trimmed)
/// line. Used by the wire-expression injection to detect
/// shadowing without parsing the Polydat grammar.
fn has_binding_named(src: &str, name: &str) -> bool {
    for raw in src.lines() {
        let line = raw.trim_start();
        if let Some(rest) = line.strip_prefix(name) {
            // Boundary: next char must be whitespace or `:=`.
            let rest = rest.trim_start();
            if rest.starts_with(":=") {
                return true;
            }
        }
    }
    false
}

/// SRD-66 §"Surface 1 §Schema": parse the vari-structured
/// `result:` field on an op template. Three shapes:
///
/// - **String** scalar — Polydat source block (multi-line or
///   single-line). Each `<name> := <expr>` declares one
///   wire.
/// - **List** sequence — each element is itself a
///   `ResultSpec` (recursively); fragments concatenate.
/// - **Mapping** — named-key short-forms; each value is a
///   string parsed as `count` / `ok` / path-expr / Polydat expr.
/// Parse an op's `traverse:` block.
///
/// Customises the always-installed traversal layer; it does not select it, so
/// absence means "defaults", not "no traversal". Unknown keys are rejected
/// rather than dropped — a silently ignored `on_missing:` would leave the
/// author believing absent captures are being policed when they are not.
fn parse_traverse_field(
    val: Option<&JVal>,
    op_name: &str,
) -> Result<Option<crate::model::TraverseSpec>, String> {
    use crate::model::{OnMissing, TraverseSpec};
    let Some(v) = val else { return Ok(None) };
    let JVal::Object(map) = v else {
        return Err(format!(
            "op '{op_name}' traverse: expected a mapping, got {v:?}"
        ));
    };
    const KNOWN: &[&str] = &["path", "on_missing"];
    for k in map.keys() {
        if !KNOWN.contains(&k.as_str()) {
            return Err(format!(
                "op '{op_name}' traverse: unknown field `{k}`. Recognised \
                 fields: path, on_missing"
            ));
        }
    }
    let path = match map.get("path") {
        None => None,
        Some(JVal::String(p)) => {
            let p = p.trim();
            if !p.is_empty() && !p.starts_with('/') {
                return Err(format!(
                    "op '{op_name}' traverse: path '{p}' must start with '/' \
                     (RFC 6901 JSON-Pointer); an empty path addresses the root"
                ));
            }
            Some(p.to_string())
        }
        Some(other) => {
            return Err(format!(
                "op '{op_name}' traverse: path expected a string, got {other:?}"
            ));
        }
    };
    let on_missing = match map.get("on_missing") {
        None => OnMissing::default(),
        Some(JVal::String(s)) => match s.trim().to_ascii_lowercase().as_str() {
            "ignore" => OnMissing::Ignore,
            "warn" => OnMissing::Warn,
            "error" | "fail" => OnMissing::Error,
            other => {
                return Err(format!(
                    "op '{op_name}' traverse: on_missing '{other}': expected one of \
                 ignore / warn / error"
                ));
            }
        },
        Some(other) => {
            return Err(format!(
                "op '{op_name}' traverse: on_missing expected a string, got {other:?}"
            ));
        }
    };
    Ok(Some(TraverseSpec { path, on_missing }))
}

fn parse_result_field(
    val: Option<&JVal>,
    op_name: &str,
) -> Result<Option<crate::model::ResultSpec>, String> {
    let Some(v) = val else {
        return Ok(None);
    };
    let spec = parse_result_spec(v, op_name)?;
    if spec.is_empty() {
        Ok(None)
    } else {
        Ok(Some(spec))
    }
}

fn parse_result_spec(v: &JVal, op_name: &str) -> Result<crate::model::ResultSpec, String> {
    use crate::model::ResultSpec;
    match v {
        JVal::Null => Ok(ResultSpec::String(String::new())),
        JVal::String(s) => Ok(ResultSpec::String(s.clone())),
        JVal::Array(items) => {
            let mut out: Vec<ResultSpec> = Vec::with_capacity(items.len());
            for item in items {
                out.push(parse_result_spec(item, op_name)?);
            }
            Ok(ResultSpec::List(out))
        }
        JVal::Object(map) => {
            let mut out: std::collections::BTreeMap<String, String> =
                std::collections::BTreeMap::new();
            for (key, val) in map {
                let source = match val {
                    JVal::String(s) => s.clone(),
                    JVal::Null => String::new(),
                    other => {
                        return Err(format!(
                            "op '{op_name}' result.{key}: expected string \
                         (short-form keyword `count`/`ok`, path \
                         expression, or Polydat expression); got {other}"
                        ));
                    }
                };
                if out.insert(key.clone(), source).is_some() {
                    return Err(format!("op '{op_name}' result: duplicate key '{key}'"));
                }
            }
            Ok(ResultSpec::Map(out))
        }
        _ => Err(format!(
            "op '{op_name}' result: expected string (GK source), \
             list (sequence of fragments), or mapping (named \
             short-forms); got {v}"
        )),
    }
}

// -----------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------

/// Extract bindings from a YAML value.
///
/// If the value is a string, it's native Polydat grammar source.
/// If it's a mapping, it's legacy name→expression pairs.
fn extract_bindings(val: Option<&JVal>) -> BindingsDef {
    match val {
        Some(JVal::String(s)) => BindingsDef::PolydatSource(s.clone()),
        Some(JVal::Object(obj)) => {
            let mut map = HashMap::new();
            for (k, v) in obj {
                if let Some(s) = v.as_str() {
                    map.insert(k.clone(), s.to_string());
                } else {
                    map.insert(k.clone(), v.to_string());
                }
            }
            BindingsDef::Map(map)
        }
        _ => BindingsDef::default(),
    }
}

/// SRD-13f Push D: inline a block's YAML-level `bindings:`
/// sugar into one of its enclosed ops.
///
/// Blocks are not a Polydat scope — they're YAML authoring sugar
/// (named groups for tag-filtering + shared defaults). A
/// block-level `bindings:` field is *syntactic sugar* meaning
/// "every op underneath has these bindings as part of its own
/// op-level bindings." This helper does that expansion at
/// parse time.
///
/// Semantics (preserves prior `merge_bindings` shape for the
/// only call site that still uses it):
/// - The op's own PolydatSource fully shadows the block's sugar
///   (the op declares its full binding set explicitly).
/// - Op Map merges with block Map (op keys override block).
/// - Empty op inherits the block sugar verbatim.
///
/// No cross-scope semantics: workload-level and phase-level
/// `bindings:` no longer flow through this helper. They reach
/// ops via the Polydat Kernel chain.
fn inline_block_sugar_into_op(block_sugar: &BindingsDef, op_own: &BindingsDef) -> BindingsDef {
    match (block_sugar, op_own) {
        (_, BindingsDef::PolydatSource(s)) if !s.trim().is_empty() => {
            BindingsDef::PolydatSource(s.clone())
        }
        (BindingsDef::Map(p), BindingsDef::Map(c)) => {
            let mut merged = p.clone();
            for (k, v) in c {
                merged.insert(k.clone(), v.clone());
            }
            BindingsDef::Map(merged)
        }
        (_, BindingsDef::Map(c)) if c.is_empty() => block_sugar.clone(),
        (_, child) => child.clone(),
    }
}

/// Render a YAML/JSON param value as the text form
/// `add_param_binding` expects.
///
/// Scalar shapes pass through RAW (no extra quoting): a YAML
/// `iter_count: "3"` and a YAML `iter_count: 3` BOTH come out as
/// the string `"3"` here — the downstream classifier sees
/// numeric-shape and emits a bare U64 binding either way. YAML's
/// quotes are presentation, not semantic, for scalars.
///
/// Array shape gets the polydat array literal form
/// (`[v1, v2, v3]`) — that's the new convention the workload
/// surface needs to support. Array ELEMENTS are formatted with
/// polydat literal grammar (strings explicitly quoted) since
/// polydat's parser requires quotes inside array literals;
/// `format_jval_in_array_context` handles the recursion.
///
/// Object shape (rare for params) falls back to JSON
/// serialization — there's no polydat literal form for objects,
/// so the value passes through whatever-it-is for callers
/// downstream to handle.
pub(crate) fn format_jval_as_polydat_literal(v: &JVal) -> String {
    match v {
        JVal::Null => String::new(),
        JVal::Bool(b) => b.to_string(),
        JVal::Number(n) => n.to_string(),
        // Scalar strings pass through unquoted to preserve the
        // legacy "YAML quotes are presentation" behavior. The
        // downstream classifier (`add_param_binding`) figures
        // out the actual type from the content — numeric-shape
        // becomes U64/F64, identifier-shape becomes a reference,
        // string-shape becomes a polydat-quoted Str.
        JVal::String(s) => s.clone(),
        JVal::Array(items) => {
            let elts: Vec<String> = items.iter().map(format_jval_in_array_context).collect();
            format!("[{}]", elts.join(", "))
        }
        JVal::Object(_) => v.to_string(),
    }
}

/// Element-context formatter: polydat array literals require
/// explicit quotes around string elements (`["a", "b"]`), unlike
/// the scalar-context formatter which leaves strings unquoted.
fn format_jval_in_array_context(v: &JVal) -> String {
    match v {
        JVal::String(s) => {
            let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
            format!("\"{escaped}\"")
        }
        JVal::Array(items) => {
            let elts: Vec<String> = items.iter().map(format_jval_in_array_context).collect();
            format!("[{}]", elts.join(", "))
        }
        _ => format_jval_as_polydat_literal(v),
    }
}

fn extract_string_map(val: Option<&JVal>) -> HashMap<String, String> {
    let mut map = HashMap::new();
    if let Some(JVal::Object(obj)) = val {
        for (k, v) in obj {
            // Format every YAML value as polydat-native source.
            // Strings come out quote-wrapped, arrays as
            // `[a, b, c]`, numbers / bools bare. The downstream
            // `add_param_binding` classifier reads this and emits
            // the const binding without re-quoting.
            map.insert(k.clone(), format_jval_as_polydat_literal(v));
        }
    }
    map
}

// Shared classifier helpers — the `set:` block parser routes
// every value through the same shape detection so the numeric /
// array-literal / quoted-string surface is consistent.

fn is_polydat_quoted_string(s: &str) -> bool {
    if s.len() < 2 {
        return false;
    }
    if !s.starts_with('"') || !s.ends_with('"') {
        return false;
    }
    let bytes = s.as_bytes();
    let mut i = 1;
    let last = bytes.len() - 1;
    while i < last {
        if bytes[i] == b'\\' {
            i += 2;
            continue;
        }
        if bytes[i] == b'"' {
            return false;
        }
        i += 1;
    }
    true
}

fn extract_value_map(val: Option<&JVal>) -> HashMap<String, JVal> {
    let mut map = HashMap::new();
    if let Some(JVal::Object(obj)) = val {
        for (k, v) in obj {
            map.insert(k.clone(), v.clone());
        }
    }
    map
}

fn merge_string_maps(
    parent: &HashMap<String, String>,
    child: &HashMap<String, String>,
) -> HashMap<String, String> {
    let mut merged = parent.clone();
    for (k, v) in child {
        merged.insert(k.clone(), v.clone());
    }
    merged
}

/// Activity/phase-scope param keys that must never be blast-merged onto ops.
/// They are consumed at phase/activity scope; leaking them into op `params`
/// makes an inherited `rate` collide with the `rate` wrapper's
/// field-ownership guard (SRD-32a). The op-level `rate:` field reaches ops via
/// the typed [`ParsedOp::rate`] path, not params, so this exclusion is safe.
const ACTIVITY_PARAM_KEYS: &[&str] = &["cycles", "concurrency", "rate", "errors", "error_rate_max"];

/// Clone `params` minus the activity/phase-scope keys ([`ACTIVITY_PARAM_KEYS`]).
fn exclude_activity_keys(params: &HashMap<String, JVal>) -> HashMap<String, JVal> {
    params
        .iter()
        .filter(|(k, _)| !ACTIVITY_PARAM_KEYS.contains(&k.as_str()))
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect()
}

fn merge_value_maps(
    parent: &HashMap<String, JVal>,
    child: &HashMap<String, JVal>,
) -> HashMap<String, JVal> {
    let mut merged = parent.clone();
    for (k, v) in child {
        merged.insert(k.clone(), v.clone());
    }
    merged
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn phase_metrics_mapping_form_preserves_raw_value() {
        // Phase-level `metrics:` keeps the raw `value:` expression
        // (no bare-key injection into bindings — the phase synthesiser
        // emits `volatile __metric_<name> := <value>` directly).
        let yaml = r#"
phases:
  build_index:
    metrics:
      time_to_index: { value: "current_epoch_millis() - phase_start", kind: gauge }
    ops:
      work: { stmt: "op" }
scenarios:
  default: [build_index]
"#;
        let wl = parse_workload(yaml, &HashMap::new()).expect("parse");
        let phase = wl.phases.get("build_index").expect("phase build_index");
        let m = phase
            .metrics
            .get("time_to_index")
            .expect("time_to_index metric");
        assert_eq!(
            m.value, "current_epoch_millis() - phase_start",
            "raw value must be preserved verbatim"
        );
        assert_eq!(m.kind, Some(crate::model::MetricKind::Gauge));
        assert!(
            phase.bindings.is_empty(),
            "phase metrics must NOT auto-inject into bindings: {:?}",
            phase.bindings
        );
    }

    #[test]
    fn tries_accepts_sugared_number_and_map_form() {
        // Sugared: a bare number sets the count, no backoff overrides.
        let sugar = r#"
phases:
  load:
    tries: 20
    ops: { work: { stmt: "op" } }
scenarios: { default: [load] }
"#;
        let wl = parse_workload(sugar, &HashMap::new()).expect("parse sugar");
        let p = wl.phases.get("load").unwrap();
        assert_eq!(p.tries, Some(20));
        assert_eq!(p.tries_backoff, None);

        // Map form: `count` + nested `backoff` (durations kept as strings).
        let map = r#"
phases:
  load:
    tries:
      count: 20
      backoff:
        ratio: 2.0
        min: 100ms
        max: 10s
    ops: { work: { stmt: "op" } }
scenarios: { default: [load] }
"#;
        let wl = parse_workload(map, &HashMap::new()).expect("parse map");
        let p = wl.phases.get("load").unwrap();
        assert_eq!(p.tries, Some(20));
        let bo = p.tries_backoff.as_ref().expect("backoff parsed");
        assert_eq!(bo.ratio, Some(2.0));
        assert_eq!(bo.min.as_deref(), Some("100ms"));
        assert_eq!(bo.max.as_deref(), Some("10s"));

        // A bad shape (string) is a loud parse error, not a silent drop.
        let bad = r#"
phases: { load: { tries: "lots", ops: { w: { stmt: "op" } } } }
scenarios: { default: [load] }
"#;
        assert!(
            parse_workload(bad, &HashMap::new()).is_err(),
            "non-number, non-map tries must fail to parse"
        );
    }

    #[test]
    fn metric_spec_rejects_unknown_field_type_with_hint() {
        // `type:` is the OpenMetrics word; ours is `kind`. Reject it
        // loudly with a hint rather than silently dropping it (which
        // would default the metric to gauge).
        let yaml = r#"
phases:
  p:
    metrics:
      m: { type: counter, value: "x" }
    ops:
      work: { stmt: "op" }
scenarios:
  default: [p]
"#;
        let err = parse_workload(yaml, &HashMap::new())
            .expect_err("unknown metric field `type` must be rejected");
        assert!(
            err.contains("unknown field `type`"),
            "must name the offending field; got: {err}"
        );
        assert!(
            err.contains("kind"),
            "must hint at the canonical `kind` field; got: {err}"
        );
    }

    #[test]
    fn metric_spec_rejects_arbitrary_unknown_field() {
        let yaml = r#"
phases:
  p:
    metrics:
      m: { value: "x", flavour: gauge }
    ops:
      work: { stmt: "op" }
scenarios:
  default: [p]
"#;
        let err = parse_workload(yaml, &HashMap::new())
            .expect_err("arbitrary unknown metric field must be rejected");
        assert!(
            err.contains("unknown field `flavour`"),
            "must name the offending field; got: {err}"
        );
    }

    #[test]
    fn metric_spec_accepts_all_known_fields() {
        // Regression guard: the unknown-field check must not reject any
        // legitimate field.
        let yaml = r#"
phases:
  p:
    metrics:
      m: { value: "x", family: fam, kind: counter, unit: bytes, format: "0.00" }
    ops:
      work: { stmt: "op" }
scenarios:
  default: [p]
"#;
        let wl = parse_workload(yaml, &HashMap::new()).expect("all known fields accepted");
        let m = wl.phases.get("p").unwrap().metrics.get("m").unwrap();
        assert_eq!(m.kind, Some(crate::model::MetricKind::Counter));
        assert_eq!(m.unit.as_deref(), Some("bytes"));
        assert_eq!(m.family.as_deref(), Some("fam"));
    }

    #[test]
    fn phase_metrics_list_form_rejects_wire_expression() {
        // The `name := expr` list form is op-only; for a phase the
        // author must use the phase `bindings:` block + a bare name,
        // or the mapping form. Reject loudly rather than silently.
        let yaml = r#"
phases:
  p:
    metrics:
      - "te := current_epoch_millis() - phase_start"
    ops:
      work: { stmt: "op" }
scenarios:
  default: [p]
"#;
        let err = parse_workload(yaml, &HashMap::new())
            .expect_err("list `name := expr` form must be rejected for phases");
        assert!(
            err.contains("op-only") || err.contains("mapping form"),
            "diagnostic should point at the op-only form; got: {err}"
        );
    }

    #[test]
    fn readouts_block_form_a_scalar_binds_on_update() {
        let workload: serde_yaml::Value =
            serde_yaml::from_str(r#"readouts: phase_status"#).unwrap();
        let json = serde_json::to_value(&workload).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        assert_eq!(r.on_update, vec!["phase_status".to_string()]);
        assert!(r.on_phase_end.is_empty());
    }

    #[test]
    fn readouts_block_form_b_mapping_binds_explicit_slots() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  on_phase_end: phase_outcome
  on_update: "phase_status lod=compact"
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        assert_eq!(r.on_phase_end, vec!["phase_outcome".to_string()]);
        assert_eq!(r.on_update, vec!["phase_status lod=compact".to_string()]);
    }

    #[test]
    fn readouts_block_form_c_list_composes() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  on_phase_end:
    - phase_outcome
    - phase_failure_hint
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        assert_eq!(
            r.on_phase_end,
            vec![
                "phase_outcome".to_string(),
                "phase_failure_hint".to_string(),
            ]
        );
    }

    #[test]
    fn readouts_block_each_wildcard_expands() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  each_*: scope_bracket
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        assert_eq!(r.on_each_start, vec!["scope_bracket".to_string()]);
        assert_eq!(r.on_each_end, vec!["scope_bracket".to_string()]);
        // Other slots untouched.
        assert!(r.on_phase_end.is_empty());
        assert!(r.on_update.is_empty());
    }

    #[test]
    fn readouts_block_phase_wildcard_expands() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  phase_*: trace
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        assert_eq!(r.on_phase_start, vec!["trace".to_string()]);
        assert_eq!(r.on_phase_end, vec!["trace".to_string()]);
        assert!(r.on_each_start.is_empty());
    }

    #[test]
    fn readouts_block_universal_wildcard_expands_to_all() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  "*": trace
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let r = parse_readouts_block(json.get("readouts")).unwrap();
        for slot in [
            &r.on_session_start,
            &r.on_session_end,
            &r.on_phase_start,
            &r.on_phase_end,
            &r.on_each_start,
            &r.on_each_end,
            &r.on_scope_start,
            &r.on_scope_end,
            &r.on_update,
        ] {
            assert_eq!(slot, &vec!["trace".to_string()]);
        }
    }

    #[test]
    fn readouts_block_unknown_slot_is_error() {
        let yaml = serde_yaml::from_str::<serde_yaml::Value>(
            r#"
readouts:
  on_unknown: phase_outcome
"#,
        )
        .unwrap();
        let json = serde_json::to_value(&yaml).unwrap();
        let err = parse_readouts_block(json.get("readouts")).unwrap_err();
        assert!(
            err.contains("unknown slot 'on_unknown'"),
            "wrong message: {err}"
        );
    }

    #[test]
    fn parse_single_string_op() {
        let ops = parse_ops("op: select * from bar.table;").unwrap();
        assert_eq!(ops.len(), 1);
        assert_eq!(ops[0].name, "stmt1");
        assert_eq!(ops[0].op["stmt"], "select * from bar.table;");
    }

    #[test]
    fn parse_ops_list_of_strings() {
        let yaml = r#"
ops:
  - select * from t1;
  - select * from t2;
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops.len(), 2);
        assert_eq!(ops[0].op["stmt"], "select * from t1;");
        assert_eq!(ops[1].op["stmt"], "select * from t2;");
    }

    #[test]
    fn parse_ops_map_of_strings() {
        let yaml = r#"
ops:
  read: select * from t1;
  write: insert into t1 values (1);
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops.len(), 2);
        let read = ops.iter().find(|o| o.name == "read").unwrap();
        assert_eq!(read.op["stmt"], "select * from t1;");
    }

    #[test]
    fn parse_named_blocks() {
        let yaml = r#"
blocks:
  schema:
    ops:
      create: "CREATE TABLE t (id int PRIMARY KEY);"
  main:
    ops:
      read: "SELECT * FROM t WHERE id={id};"
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops.len(), 2);
        let create = ops.iter().find(|o| o.name == "create").unwrap();
        assert_eq!(create.tags["block"], "schema");
        let read = ops.iter().find(|o| o.name == "read").unwrap();
        assert_eq!(read.tags["block"], "main");
    }

    #[test]
    fn parse_property_inheritance() {
        let yaml = r#"
bindings:
  id: Identity()
params:
  prepared: true
tags:
  workload: test
blocks:
  main:
    bindings:
      id: Hash()
    ops:
      op1: "SELECT * FROM t;"
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops.len(), 1);
        // Block-level binding overrides doc-level
        assert_eq!(ops[0].bindings.as_map()["id"], "Hash()");
        // Doc-level param inherited
        assert_eq!(ops[0].params["prepared"], true);
        // Doc-level tag inherited
        assert_eq!(ops[0].tags["workload"], "test");
        // Auto-tag
        assert_eq!(ops[0].tags["block"], "main");
    }

    #[test]
    fn parse_auto_naming() {
        let yaml = r#"
ops:
  - "first op"
  - "second op"
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops[0].name, "stmt1");
        assert_eq!(ops[1].name, "stmt2");
    }

    #[test]
    fn parse_auto_tagging() {
        let yaml = r#"
ops:
  myop: "SELECT 1;"
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops[0].tags["name"], "myop");
        assert_eq!(ops[0].tags["op"], "myop");
        assert_eq!(ops[0].tags["block"], "block0");
    }

    #[test]
    fn condition_clause_passthrough_for_identifier() {
        // Bare identifier — legacy "name a binding" form.
        assert_eq!(normalize_condition_clause("my_flag"), "my_flag");
        assert_eq!(normalize_condition_clause(" my_flag "), "my_flag");
    }

    #[test]
    fn condition_clause_passthrough_for_braced_forms() {
        assert_eq!(normalize_condition_clause("{my_flag}"), "{my_flag}");
        assert_eq!(normalize_condition_clause("{{x == 1}}"), "{{x == 1}}");
        assert_eq!(normalize_condition_clause("{:=x == 1:=}"), "{:=x == 1:=}");
    }

    #[test]
    fn condition_clause_wraps_bare_expressions() {
        assert_eq!(
            normalize_condition_clause("cql_dialect == 'cass'"),
            "{{cql_dialect == 'cass'}}",
        );
        assert_eq!(
            normalize_condition_clause("a > 0 && b < 10"),
            "{{a > 0 && b < 10}}",
        );
        assert_eq!(normalize_condition_clause("foo(bar)"), "{{foo(bar)}}",);
    }

    #[test]
    fn condition_clause_empty_passthrough() {
        assert_eq!(normalize_condition_clause(""), "");
        assert_eq!(normalize_condition_clause("   "), "");
    }

    #[test]
    fn parse_op_with_fields() {
        let yaml = r#"
ops:
  op1:
    field1: value1
    field2: value2
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops[0].op["field1"], "value1");
        assert_eq!(ops[0].op["field2"], "value2");
    }

    #[test]
    fn parse_explicit_op_field() {
        let yaml = r#"
ops:
  op1:
    op:
      stmt: "SELECT * FROM t;"
      type: query
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops[0].op["stmt"], "SELECT * FROM t;");
        assert_eq!(ops[0].op["type"], "query");
    }

    #[test]
    fn parse_scenarios() {
        let yaml = r#"
scenarios:
  default:
    schema: run driver=cql tags==block:schema threads==1
    main: run driver=cql tags==block:main cycles=1M
ops:
  op1: "test"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        let default = &workload.scenarios["default"];
        assert_eq!(default.len(), 2);
        // Legacy command-string format: names are preserved as Phase nodes
        assert!(matches!(&default[0], ScenarioNode::Phase(n) if n == "schema"));
        assert!(matches!(&default[1], ScenarioNode::Phase(n) if n == "main"));
    }

    #[test]
    fn parse_template_expansion() {
        let yaml = r#"
ops:
  op1: "SELECT * FROM t LIMIT TEMPLATE(limit, 100);"
"#;
        let ops = parse_ops(yaml).unwrap();
        assert_eq!(ops[0].op["stmt"], "SELECT * FROM t LIMIT 100;");
    }

    #[test]
    fn parse_description() {
        let yaml = r#"
description: |
  This is a test workload.
ops:
  op1: "test"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert!(workload.description.unwrap().contains("test workload"));
    }

    // ── Scenario malformed-node rejection (Never Ignore
    // Silently). The parser used to silently drop an
    // unrecognised scenario-node key like `iterate:` →
    // `{phases: [...]}`, leaving the scenario empty and
    // surfacing as a confusing downstream "phase not found"
    // error. These tests pin the loud-rejection behavior so a
    // refactor can't regress the silent-drop. ──

    #[test]
    fn scenario_node_with_unknown_map_key_collects_parse_error() {
        let yaml = r#"
scenarios:
  bogus:
    - iterate:
        phases:
          - phase_x
phases:
  phase_x:
    ops:
      noop: "x"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert!(
            !workload.scenario_parse_errors.is_empty(),
            "malformed `iterate:` node MUST surface a scenario_parse_error \
             — silent drop is the safety bug being prevented"
        );
        let msg = workload.scenario_parse_errors[0].as_str();
        assert!(
            msg.contains("bogus"),
            "error must name the offending scenario: {msg}"
        );
        assert!(
            msg.contains("iterate"),
            "error must name the bad key: {msg}"
        );
        // Bogus scenario must NOT have absorbed phase_x as a Phase
        // node via the legacy catch-all.
        assert_eq!(
            workload.scenarios.get("bogus").map(|v| v.len()),
            Some(0),
            "malformed node must NOT silently produce ScenarioNode::Phase"
        );
    }

    #[test]
    fn scenario_node_with_legacy_command_string_form_still_works() {
        // Pre-existing nosqlbench-style scenario shape: each
        // map key is a step name, value is a `run ...` CLI
        // string. The malformed-rejection refactor MUST NOT
        // break this — the discriminator is "map value is
        // a string" → legacy, "map value is a map/array" →
        // malformed.
        let yaml = r#"
scenarios:
  default:
    schema: run tags==block:schema
    main: run tags==block:main
ops:
  op1: "test"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert!(
            workload.scenario_parse_errors.is_empty(),
            "legacy command-string form must NOT be flagged as malformed: {:?}",
            workload.scenario_parse_errors
        );
        let default = &workload.scenarios["default"];
        assert_eq!(default.len(), 2);
        assert!(matches!(&default[0], ScenarioNode::Phase(n) if n == "schema"));
        assert!(matches!(&default[1], ScenarioNode::Phase(n) if n == "main"));
    }

    #[test]
    fn scenario_node_malformed_error_lists_recognised_keys() {
        // The error message must point the operator at the
        // recognised key vocabulary so they can self-correct
        // without grepping the source.
        let yaml = r#"
scenarios:
  s1:
    - typoed_for_each:
        phases:
          - phase_x
phases:
  phase_x:
    ops:
      noop: "x"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert_eq!(workload.scenario_parse_errors.len(), 1);
        let msg = &workload.scenario_parse_errors[0];
        // Must mention at least the load-bearing alternatives.
        for expected in ["for_each", "scenarios", "do_while", "bindings"] {
            assert!(
                msg.contains(expected),
                "error message must mention `{expected}` as a valid \
                 alternative; got: {msg}"
            );
        }
    }

    #[test]
    fn parse_polydat_source_bindings() {
        // SRD-13f Push D: workload-level `bindings:` live on
        // `Workload.bindings` and reach ops via the Polydat Kernel
        // chain at runtime — they are NOT folded into per-op
        // bindings at parse time.
        let yaml = r#"
bindings: |
  // Explicit wiring — every intermediate is named
  input cycle: u64
  h := hash(cycle)
  user_id := mod(h, 1000000)
  code_hash := hash(user_id)
  code := combinations(code_hash, '0-9A-Z')

  // Equivalent concise form (nested composition):
  // user_id := mod(hash(cycle), 1000000)
  // code := combinations(hash(user_id), '0-9A-Z')
ops:
  insert: "INSERT INTO users (id, code) VALUES ({user_id}, '{code}');"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        match &workload.bindings {
            BindingsDef::PolydatSource(src) => {
                assert!(src.contains("input cycle: u64"));
                assert!(src.contains("user_id := mod(h, 1000000)"));
            }
            BindingsDef::Map(_) => panic!("expected PolydatSource at workload level, got Map"),
        }
        assert_eq!(workload.ops.len(), 1);
        // Op carries no workload bindings — they reach it via
        // the Polydat Kernel chain, not via parse-time merge.
        assert!(workload.ops[0].bindings.is_empty());
    }

    #[test]
    fn parse_map_bindings_still_works() {
        // SRD-13f Push D: Map-form workload bindings live on
        // `Workload.bindings`, not on per-op bindings.
        let yaml = r#"
bindings:
  id: "Hash(); Mod(100)"
ops:
  op1: "SELECT * FROM t WHERE id={id};"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert_eq!(workload.bindings.as_map()["id"], "Hash(); Mod(100)");
        // The op itself carries no merged-in workload bindings.
        assert!(workload.ops[0].bindings.is_empty());
    }

    #[test]
    fn parse_phased_workload() {
        let yaml = r#"
scenarios:
  default:
    - schema
    - main

phases:
  schema:
    cycles: 1
    concurrency: 1
    ops:
      create_table:
        stmt: "CREATE TABLE t (id int PRIMARY KEY);"
  main:
    cycles: 1000
    concurrency: 10
    rate: 500.0
    ops:
      read:
        stmt: "SELECT * FROM t WHERE id={id};"
      write:
        stmt: "INSERT INTO t (id) VALUES ({id});"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();

        // Phases parsed
        assert_eq!(workload.phases.len(), 2);
        assert!(workload.phases.contains_key("schema"));
        assert!(workload.phases.contains_key("main"));

        // Phase order preserved
        assert_eq!(workload.phase_order, vec!["schema", "main"]);

        // Schema phase config
        let schema = &workload.phases["schema"];
        assert_eq!(schema.cycles.as_deref(), Some("1"));
        assert_eq!(schema.concurrency.as_deref(), Some("1"));
        assert_eq!(schema.rate, None);
        assert_eq!(schema.ops.len(), 1);
        assert_eq!(schema.ops[0].name, "create_table");

        // Main phase config
        let main = &workload.phases["main"];
        assert_eq!(main.cycles.as_deref(), Some("1000"));
        assert_eq!(main.concurrency.as_deref(), Some("10"));
        assert_eq!(main.rate.as_deref(), Some("500.0"));
        assert_eq!(main.ops.len(), 2);

        // Scenario parsed as phase name list
        let default = &workload.scenarios["default"];
        assert_eq!(default.len(), 2);
        assert!(matches!(&default[0], ScenarioNode::Phase(n) if n == "schema"));
        assert!(matches!(&default[1], ScenarioNode::Phase(n) if n == "main"));
    }

    #[test]
    fn parse_phased_workload_with_tags() {
        let yaml = r#"
blocks:
  schema:
    ops:
      create: "CREATE TABLE t (id int PRIMARY KEY);"
  main:
    ops:
      read: "SELECT * FROM t;"

phases:
  setup:
    tags: "block:schema"
    cycles: 1
  run:
    tags: "block:main"
    cycles: 1000
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert_eq!(workload.phases.len(), 2);

        // SRD-108 Part A: the selector resolves at parse time —
        // the phase carries the selected block ops as if inline.
        let setup = &workload.phases["setup"];
        assert_eq!(setup.tags.as_deref(), Some("block:schema"));
        assert_eq!(setup.ops.len(), 1);
        assert_eq!(setup.ops[0].name, "create");
        assert_eq!(
            setup.ops[0].tags.get("phase").map(String::as_str),
            Some("setup")
        );

        let run = &workload.phases["run"];
        assert_eq!(run.tags.as_deref(), Some("block:main"));
        assert_eq!(run.ops.len(), 1);
        assert_eq!(run.ops[0].name, "read");
    }

    /// SRD-108 Part A — a selector matching nothing is a load
    /// error naming the phase, never a dispatch-time panic.
    #[test]
    fn phase_tag_selector_matching_nothing_is_a_load_error() {
        let yaml = r#"
blocks:
  schema:
    ops:
      create: "CREATE TABLE t (id int PRIMARY KEY);"
phases:
  setup:
    tags: "block:nonexistent"
    cycles: 1
"#;
        let err = parse_workload(yaml, &HashMap::new()).unwrap_err();
        assert!(
            err.contains("setup") && err.contains("matched no ops"),
            "error must name the phase and the failure: {err}"
        );
    }

    /// SRD-108 Part A — inline ops and a selector together are
    /// rejected: one source of ops per phase.
    #[test]
    fn phase_with_inline_ops_and_selector_is_rejected() {
        let yaml = r#"
blocks:
  schema:
    ops:
      create: "CREATE TABLE t (id int PRIMARY KEY);"
phases:
  setup:
    tags: "block:schema"
    cycles: 1
    ops:
      also: "SELECT 1;"
"#;
        let err = parse_workload(yaml, &HashMap::new()).unwrap_err();
        assert!(
            err.contains("both inline ops and a"),
            "error must explain the conflict: {err}"
        );
    }

    #[test]
    fn parse_phased_workload_polydat_cycles() {
        let yaml = r#"
phases:
  rampup:
    cycles: "{train_count}"
    concurrency: 100
    ops:
      insert:
        stmt: "INSERT INTO t (id) VALUES ({id});"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        let rampup = &workload.phases["rampup"];
        assert_eq!(rampup.cycles.as_deref(), Some("{train_count}"));
    }

    // ── SRD-40b §1 + §2: `metrics:` discriminant on op template ──

    #[test]
    fn parse_metrics_full_mapping_form() {
        let yaml = r##"
phases:
  predict:
    bindings: |
      example_factor := 1.0 + 2.5
    ops:
      synth:
        stmt: "noop"
        metrics:
          example_factor:
            value: example_factor
            kind: gauge
            unit: ratio
            format: "#.##"
"##;
        let wl = parse_workload(yaml, &HashMap::new()).unwrap();
        let op = &wl.phases["predict"].ops[0];
        assert_eq!(op.name, "synth");
        let m = &op.metrics["example_factor"];
        assert_eq!(m.value, "example_factor");
        assert_eq!(m.kind, Some(crate::model::MetricKind::Gauge));
        assert_eq!(m.unit.as_deref(), Some("ratio"));
        assert_eq!(m.format.as_deref(), Some("#.##"));
    }

    #[test]
    fn parse_metrics_bare_string_sugar() {
        let yaml = r#"
phases:
  p:
    bindings: |
      overscan := 1.0
    ops:
      o:
        stmt: "noop"
        metrics: overscan
"#;
        let wl = parse_workload(yaml, &HashMap::new()).unwrap();
        let op = &wl.phases["p"].ops[0];
        let m = &op.metrics["overscan"];
        // Bare-string form: family + value both = "overscan"
        // (defaults), kind unset (defaults to gauge at runtime).
        assert_eq!(m.value, "overscan");
        assert_eq!(m.family, None);
        assert_eq!(m.kind, None);
    }

    #[test]
    fn parse_metrics_list_with_wire_expression() {
        let yaml = r#"
phases:
  p:
    ops:
      o:
        stmt: "noop"
        metrics:
          - latency_pred := 0.5 + 1.5 * pow(limit, -0.4)
          - already_bound
"#;
        let wl = parse_workload(yaml, &HashMap::new()).unwrap();
        let op = &wl.phases["p"].ops[0];
        // Both metric entries registered.
        assert!(op.metrics.contains_key("latency_pred"));
        assert!(op.metrics.contains_key("already_bound"));
        // Wire expression auto-injected into op bindings.
        match &op.bindings {
            BindingsDef::PolydatSource(src) => {
                assert!(
                    src.contains("latency_pred := 0.5 + 1.5 * pow(limit, -0.4)"),
                    "wire not injected; bindings: {src:?}"
                );
            }
            other => panic!("expected PolydatSource bindings, got {other:?}"),
        }
    }

    #[test]
    fn parse_metrics_mapping_form_with_wire_expression() {
        let yaml = r#"
phases:
  p:
    bindings: |
      base := 10
    ops:
      o:
        stmt: "noop"
        metrics:
          scaled:
            value: base * 2
            kind: gauge
"#;
        let wl = parse_workload(yaml, &HashMap::new()).unwrap();
        let op = &wl.phases["p"].ops[0];
        let m = &op.metrics["scaled"];
        // After auto-inject the spec's `value:` is the bare key.
        assert_eq!(m.value, "scaled");
        // The non-bare expression landed in op-template bindings.
        match &op.bindings {
            BindingsDef::PolydatSource(src) => {
                assert!(
                    src.contains("scaled := base * 2"),
                    "expression not injected; bindings: {src:?}"
                );
            }
            other => panic!("expected PolydatSource bindings, got {other:?}"),
        }
    }

    #[test]
    fn parse_metrics_mapping_form_invalid_key_for_non_bare_value() {
        // Non-bare value + key that can't be a binding name → reject.
        let yaml = r#"
phases:
  p:
    ops:
      o:
        stmt: "noop"
        metrics:
          "1bad":
            value: foo + 1
            kind: gauge
"#;
        let err = parse_workload(yaml, &HashMap::new()).unwrap_err();
        assert!(
            err.contains("must itself be a valid identifier"),
            "expected identifier diagnostic, got: {err}"
        );
    }

    #[test]
    fn parse_metrics_format_validation_runs_at_load() {
        let yaml = r##"
phases:
  p:
    ops:
      o:
        stmt: "noop"
        metrics:
          x:
            value: x
            format: "%3.2f"
"##;
        let err = parse_workload(yaml, &HashMap::new()).unwrap_err();
        assert!(
            err.contains("printf-style"),
            "format error not surfaced at parse time: {err}"
        );
    }

    #[test]
    fn parse_metrics_wire_expression_collision_errors() {
        let yaml = r#"
phases:
  p:
    ops:
      o:
        stmt: "noop"
        bindings: |
          foo := 1.0
        metrics:
          - foo := 2.0
"#;
        let err = parse_workload(yaml, &HashMap::new()).unwrap_err();
        assert!(err.contains("collides"), "collision not detected: {err}");
    }

    #[test]
    fn parse_phase_bindings_round_trip() {
        // SRD-13d Phase 1: phase-level `bindings:` block must
        // land on WorkloadPhase.bindings (not just merged into
        // ops) so HasGkMatter can classify it.
        let yaml = r#"
phases:
  p:
    bindings: |
      phase_factor := 7
    ops:
      o:
        stmt: "noop"
"#;
        let wl = parse_workload(yaml, &HashMap::new()).unwrap();
        match &wl.phases["p"].bindings {
            BindingsDef::PolydatSource(s) => assert!(s.contains("phase_factor := 7")),
            other => panic!("expected PolydatSource, got {other:?}"),
        }
    }

    #[test]
    fn parse_phased_workload_default_scenario_from_order() {
        // No scenarios section — phases should run in definition order
        let yaml = r#"
phases:
  alpha:
    cycles: 1
    ops:
      op1:
        stmt: "a"
  beta:
    cycles: 2
    ops:
      op2:
        stmt: "b"
  gamma:
    cycles: 3
    ops:
      op3:
        stmt: "c"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert_eq!(workload.phase_order, vec!["alpha", "beta", "gamma"]);
        assert!(workload.scenarios.is_empty());
    }

    #[test]
    fn parse_backward_compat_no_phases() {
        // Workload without phases should work exactly as before
        let yaml = r#"
ops:
  op1: "SELECT 1;"
  op2: "SELECT 2;"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        assert!(workload.phases.is_empty());
        assert!(workload.phase_order.is_empty());
        assert_eq!(workload.ops.len(), 2);
    }

    #[test]
    fn block_level_params_override_workload_default() {
        // SRD 21 §"Parameter Resolution": closest-wins. The DDL block declares
        // `consistency=1`, overriding the workload-level default `100` for ops
        // in that block. Uses an op-scope param (`consistency`); activity-scope
        // keys (`concurrency`/`rate`/`cycles`/`errors`) are deliberately NOT
        // merged onto ops (SRD-32a — see `ACTIVITY_PARAM_KEYS`), so this tests
        // op-param precedence with a key that actually reaches ops.
        let yaml = r#"
params:
  consistency: "100"
blocks:
  ddl:
    params:
      consistency: "1"
    ops:
      schema_create: "CREATE TABLE foo (id int PRIMARY KEY);"
  bulk:
    ops:
      insert: "INSERT INTO foo (id) VALUES (?);"
"#;
        let ops = parse_ops(yaml).unwrap();
        let ddl = ops.iter().find(|o| o.name == "schema_create").unwrap();
        let bulk = ops.iter().find(|o| o.name == "insert").unwrap();
        assert_eq!(
            ddl.params.get("consistency").and_then(|v| v.as_str()),
            Some("1"),
            "block-level override should win for ddl op",
        );
        assert_eq!(
            bulk.params.get("consistency").and_then(|v| v.as_str()),
            Some("100"),
            "non-overriding block inherits workload-level default",
        );
    }

    #[test]
    fn cli_overrides_block_level_params() {
        // CLI is the outermost layer per SRD 21 — it wins even over block-level
        // explicit overrides. Uses an op-scope param (`consistency`); see
        // `block_level_params_override_workload_default` for why not an
        // activity-scope key like `concurrency`.
        let yaml = r#"
params:
  consistency: "100"
blocks:
  ddl:
    params:
      consistency: "1"
    ops:
      schema_create: "CREATE TABLE foo (id int PRIMARY KEY);"
"#;
        let mut cli = HashMap::new();
        cli.insert("consistency".to_string(), "200".to_string());
        let workload = parse_workload(yaml, &cli).unwrap();
        let ddl = workload
            .ops
            .iter()
            .find(|o| o.name == "schema_create")
            .unwrap();
        assert_eq!(
            ddl.params.get("consistency").and_then(|v| v.as_str()),
            Some("200"),
            "CLI override should beat block-level",
        );
        // Workload-level params likewise reflect CLI.
        assert_eq!(
            workload.params.get("consistency").map(|s| s.as_str()),
            Some("200"),
        );
    }

    #[test]
    fn parse_polydat_source_overrides_parent_map() {
        // Block-level Polydat source completely replaces doc-level map bindings
        let yaml = r#"
bindings:
  id: "Hash()"
blocks:
  main:
    bindings: |
      input cycle: u64
      h := hash(cycle)
      id := mod(h, 1000)
      // Concise equivalent:
      // id := mod(hash(cycle), 1000)
    ops:
      op1: "SELECT * FROM t WHERE id={id};"
"#;
        let ops = parse_ops(yaml).unwrap();
        match &ops[0].bindings {
            BindingsDef::PolydatSource(src) => {
                assert!(src.contains("input cycle: u64"));
                assert!(src.contains("id := mod(h, 1000)"));
            }
            BindingsDef::Map(_) => panic!("expected PolydatSource, got Map"),
        }
    }

    #[test]
    fn parse_scenarios_plural_list_form() {
        // The plural `scenarios: [a, b, c]` form composes
        // several named scenarios at one node. Each list
        // entry expands to an `IncludedScenario` and resolves
        // post-parse. Equivalent to `[- scenario: a, -
        // scenario: b, ...]` but reads more naturally for the
        // "just compose these" case.
        let yaml = r#"
scenarios:
  rampup:
    - prep
  query:
    - run

  composed:
    - scenarios:
        - rampup
        - query

phases:
  prep:
    ops:
      create:
        raw: "select 1"
  run:
    ops:
      sel:
        raw: "select {cycle}"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        let composed = workload
            .scenarios
            .get("composed")
            .expect("composed scenario must parse");
        // After resolution the IncludedScenario wrappers carry
        // their resolved children. The scenario tree shape is:
        //   composed
        //     └── IncludedScenario("rampup") [Phase("prep")]
        //     └── IncludedScenario("query")  [Phase("run")]
        // Walk one level deep into each include to assert.
        assert_eq!(
            composed.len(),
            2,
            "scenarios: [a, b] should produce two top-level nodes"
        );
        let names: Vec<&str> = composed
            .iter()
            .filter_map(|n| match n {
                ScenarioNode::IncludedScenario { name, .. } => Some(name.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(names, vec!["rampup", "query"]);
        // First include resolves to its sole `Phase("prep")`.
        let first_children = match &composed[0] {
            ScenarioNode::IncludedScenario { children, .. } => children,
            _ => panic!("expected IncludedScenario at index 0"),
        };
        let first_phase = first_children.iter().find_map(|n| match n {
            ScenarioNode::Phase(p) => Some(p.as_str()),
            _ => None,
        });
        assert_eq!(first_phase, Some("prep"));
    }

    #[test]
    fn parse_scenarios_plural_mixes_with_other_node_shapes() {
        // List entries can be a mix of bare strings and other
        // scenario-node shapes (objects with `scenario:`,
        // `for_each:`, etc.). This matches the heterogeneous
        // shape `parse_scenario_nodes` already accepts at the
        // top level, so the plural form composes naturally
        // with everything else.
        let yaml = r#"
scenarios:
  rampup:
    - prep
  composed:
    - scenarios:
        - rampup
        - { scenario: rampup }

phases:
  prep:
    ops:
      create:
        raw: "select 1"
"#;
        let workload = parse_workload(yaml, &HashMap::new()).unwrap();
        let composed = workload.scenarios.get("composed").unwrap();
        // Both list entries should resolve to an IncludedScenario
        // wrapping the same `prep` phase.
        assert_eq!(composed.len(), 2);
        for node in composed {
            match node {
                ScenarioNode::IncludedScenario { name, children } => {
                    assert_eq!(name, "rampup");
                    assert!(
                        children
                            .iter()
                            .any(|c| matches!(c, ScenarioNode::Phase(p) if p == "prep"))
                    );
                }
                other => panic!("expected IncludedScenario, got {other:?}"),
            }
        }
    }

    // -----------------------------------------------------------------
    // Checkpoint declaration parsing — SRD-44 §"Forms"
    // -----------------------------------------------------------------

    fn parse_checkpoint_field(yaml: &str) -> Option<crate::model::Checkpoint> {
        let yaml = format!(
            "phases:\n  p:\n{}\n    ops:\n      - select 1;\n",
            yaml.lines()
                .map(|l| format!("    {l}"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        let v: serde_yaml::Value = serde_yaml::from_str(&yaml).expect("yaml parse");
        let json: serde_json::Value = serde_json::to_value(&v).expect("json convert");
        let phases_obj = json
            .get("phases")
            .and_then(|p| p.as_object())
            .expect("phases");
        let phase = phases_obj
            .get("p")
            .and_then(|p| p.as_object())
            .expect("phase p");
        phase.get("checkpoint").map(|v| {
            serde_json::from_value::<crate::model::Checkpoint>(v.clone()).expect("checkpoint parse")
        })
    }

    #[test]
    fn checkpoint_short_form_idempotent() {
        let cp = parse_checkpoint_field("checkpoint: idempotent").expect("present");
        assert!(cp.idempotent);
        assert!(cp.hashed);
        assert!(cp.verify.is_none());
    }

    #[test]
    fn checkpoint_short_form_none_disables_skip() {
        let cp = parse_checkpoint_field("checkpoint: none").expect("present");
        assert!(!cp.idempotent);
        // hashed default-true is preserved even when disabled —
        // the disabled state is about skip eligibility, not
        // about the hash field.
        assert!(cp.hashed);
        assert!(cp.verify.is_none());
    }

    #[test]
    fn checkpoint_short_form_no_and_false_and_off_all_disable() {
        for word in &["no", "false", "off"] {
            let cp = parse_checkpoint_field(&format!("checkpoint: {word}")).expect("present");
            assert!(!cp.idempotent, "expected disabled for '{word}'");
        }
    }

    #[test]
    fn checkpoint_bool_false_disables() {
        // YAML's bare `false` should map to disabled.
        let cp = parse_checkpoint_field("checkpoint: false").expect("present");
        assert!(!cp.idempotent);
    }

    #[test]
    fn checkpoint_full_form_all_explicit() {
        let cp = parse_checkpoint_field("checkpoint:\n  idempotent: true\n  hashed: false")
            .expect("present");
        assert!(cp.idempotent);
        assert!(!cp.hashed);
        assert!(cp.verify.is_none());
    }

    #[test]
    fn checkpoint_full_form_with_verify() {
        let cp = parse_checkpoint_field(
            "checkpoint:\n  idempotent: true\n  verify:\n    raw: 'SELECT 1'\n    poll: assert_one",
        )
        .expect("present");
        assert!(cp.idempotent);
        assert!(cp.hashed); // default
        let v = cp.verify.expect("verify body");
        assert_eq!(v.get("raw").and_then(|x| x.as_str()), Some("SELECT 1"));
        assert_eq!(v.get("poll").and_then(|x| x.as_str()), Some("assert_one"));
    }

    #[test]
    fn checkpoint_full_form_idempotent_false_equivalent_to_none() {
        let cp = parse_checkpoint_field("checkpoint:\n  idempotent: false\n  hashed: true")
            .expect("present");
        assert!(!cp.idempotent);
        assert!(cp.hashed);
    }

    #[test]
    fn checkpoint_unknown_short_form_errors() {
        // Should fail to parse — an unknown short string is a
        // workload bug, not silently treated as `none`.
        let yaml = "phases:\n  p:\n    checkpoint: maybe\n    ops:\n      - select 1;\n";
        let v: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
        let json: serde_json::Value = serde_json::to_value(&v).unwrap();
        let phases_obj = json.get("phases").and_then(|p| p.as_object()).unwrap();
        let phase = phases_obj.get("p").and_then(|p| p.as_object()).unwrap();
        let cp_val = phase.get("checkpoint").unwrap().clone();
        let err = serde_json::from_value::<crate::model::Checkpoint>(cp_val).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("unknown short form"),
            "expected unknown-short-form error, got: {msg}"
        );
        assert!(
            msg.contains("'maybe'"),
            "expected the bad token in error, got: {msg}"
        );
    }

    #[test]
    fn checkpoint_unknown_key_errors() {
        let yaml = "phases:\n  p:\n    checkpoint:\n      idempotent: true\n      bogus: yes\n    ops:\n      - select 1;\n";
        let v: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
        let json: serde_json::Value = serde_json::to_value(&v).unwrap();
        let cp_val = json.pointer("/phases/p/checkpoint").unwrap().clone();
        let err = serde_json::from_value::<crate::model::Checkpoint>(cp_val).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("unknown key 'bogus'"),
            "expected unknown-key error, got: {msg}"
        );
    }

    #[test]
    fn checkpoint_field_absent_yields_none() {
        let cp = parse_checkpoint_field("# no checkpoint declared\n");
        assert!(
            cp.is_none(),
            "absent declaration should yield None, not Default"
        );
    }

    /// SRD-44 validation: a phase declared `checkpoint:
    /// idempotent` inside a do_while loop must be rejected at
    /// workload-load time. The do-loop iterates the same phase
    /// identity multiple times, contradicting the per-execution
    /// unit checkpointing assumes.
    #[test]
    fn rejects_idempotent_phase_inside_do_while() {
        let yaml = r#"
scenarios:
  default:
    - do_while: "true"
      phases:
        - probe
phases:
  probe:
    checkpoint: idempotent
    cycles: 1
    ops:
      step:
        stmt: "probe"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("expected validation rejection");
        assert!(
            err.contains("checkpoint: idempotent"),
            "error should explain the rejection: {err}"
        );
        assert!(
            err.contains("'probe'"),
            "error should name the offending phase: {err}"
        );
        assert!(
            err.contains("do_while") || err.contains("do_until"),
            "error should mention the do-loop ancestor: {err}"
        );
    }

    /// `do_until` triggers the same rejection.
    #[test]
    fn rejects_idempotent_phase_inside_do_until() {
        let yaml = r#"
scenarios:
  default:
    - do_until: "false"
      phases:
        - probe
phases:
  probe:
    checkpoint: idempotent
    cycles: 1
    ops:
      step:
        stmt: "probe"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("expected validation rejection");
        assert!(err.contains("'probe'"));
    }

    /// A do_while'd phase WITHOUT checkpoint declaration is
    /// fine — only the combination with `checkpoint:
    /// idempotent` is rejected.
    #[test]
    fn allows_do_while_phase_without_checkpoint_declaration() {
        let yaml = r#"
scenarios:
  default:
    - do_while: "true"
      phases:
        - probe
phases:
  probe:
    cycles: 1
    ops:
      step:
        stmt: "probe"
"#;
        super::parse_workload(yaml, &HashMap::new()).expect("plain do_while phase should parse");
    }

    /// Idempotent phases NOT inside a do-loop are fine.
    #[test]
    fn allows_idempotent_phase_outside_do_loop() {
        let yaml = r#"
scenarios:
  default:
    - probe
phases:
  probe:
    checkpoint: idempotent
    cycles: 1
    ops:
      step:
        stmt: "probe"
"#;
        super::parse_workload(yaml, &HashMap::new())
            .expect("idempotent phase outside loop should parse");
    }

    /// Declarative `capture:` map block on an op produces
    /// CapturePoint entries with [`CapturePoint::path`] set.
    /// JSON-Pointer paths are not validated against the
    /// response shape (it's a runtime read), so the parser's
    /// only job is to forbid obviously-malformed inputs
    /// (non-string values, paths missing the leading `/`).
    #[test]
    fn parses_declarative_capture_block_with_json_pointer_paths() {
        let yaml = r#"
scenarios:
  default:
    - probe
phases:
  probe:
    cycles: 1
    ops:
      read_state:
        adapter: http
        method: POST
        uri: "http://h:8778/jolokia/"
        body: "[]"
        capture:
          sstables: "/0/value"
          active_count: "/1/value:count"
          pending_for_cf: "/2/value"
"#;
        let wl = super::parse_workload(yaml, &HashMap::new())
            .expect("workload with declarative captures should parse");
        let phase = wl.phases.get("probe").expect("probe phase");
        let op = phase
            .ops
            .iter()
            .find(|o| o.name == "read_state")
            .expect("read_state op");
        assert_eq!(
            op.captures.len(),
            3,
            "expected 3 declarative captures, got {:?}",
            op.captures
        );
        let by_name = |n: &str| {
            op.captures
                .iter()
                .find(|c| c.as_name == n)
                .unwrap_or_else(|| panic!("capture {n} missing"))
        };
        let sstables = by_name("sstables");
        assert_eq!(sstables.path.as_deref(), Some("/0/value"));
        assert!(!sstables.count);
        let active = by_name("active_count");
        assert_eq!(
            active.path.as_deref(),
            Some("/1/value"),
            ":count suffix should be stripped from stored path"
        );
        assert!(active.count, ":count suffix should set CapturePoint.count");
        let pending = by_name("pending_for_cf");
        assert_eq!(pending.path.as_deref(), Some("/2/value"));
        assert!(!pending.count);
    }

    /// SRD-75: phase-level `poll:` block parses into
    /// `WorkloadPhase.poll`. Distinct from the op-level
    /// `poll:` field which lives on a single op and routes
    /// through the `PollingDispenser` wrapper.
    #[test]
    fn parses_phase_level_poll_block() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      until: "sstables == 1 && active_for_cf == 0"
      interval_ms: 5000
      timeout_ms: 14400000
      max_error_retries: 3
      metric_name: ensure_wait_s
    ops:
      read_state:
        stmt: "noop"
"#;
        let wl =
            super::parse_workload(yaml, &HashMap::new()).expect("phase-poll block should parse");
        let phase = wl.phases.get("ensure").expect("ensure phase");
        let poll = phase.poll.as_ref().expect("phase.poll Some");
        assert_eq!(poll.until, "sstables == 1 && active_for_cf == 0");
        assert_eq!(poll.interval_ms.as_deref(), Some("5000"));
        assert_eq!(poll.timeout_ms.as_deref(), Some("14400000"));
        assert_eq!(poll.max_error_retries.as_deref(), Some("3"));
        assert_eq!(poll.metric_name.as_deref(), Some("ensure_wait_s"));
    }

    #[test]
    fn optimize_string_is_sugar_for_objective_block() {
        // SRD-86 — `optimize: <string>` is shorthand for
        // `optimize: { objective: <string> }` with every other field defaulted.
        let yaml = r#"
scenarios:
  default:
    - search
phases:
  search:
    cycles: 1
    for_each: "ef in 1.0 .. 5.0"
    optimize: |
      0 - (ef - 4) * (ef - 4)
    ops:
      probe:
        stmt: "probe ef={ef}"
"#;
        let wl = super::parse_workload(yaml, &HashMap::new())
            .expect("string-form optimize should parse");
        let opt = wl
            .phases
            .get("search")
            .and_then(|p| p.optimize.as_ref())
            .expect("optimize block present");
        // The whole string became the objective; the rest defaulted.
        assert_eq!(opt.objective.trim(), "0 - (ef - 4) * (ef - 4)");
        assert_eq!(opt.method, "sweep");
        assert!(opt.servo.is_empty());

        // ...and it is equivalent to the explicit map form.
        let map_yaml = yaml.replace(
            "    optimize: |\n      0 - (ef - 4) * (ef - 4)\n",
            "    optimize: { objective: \"0 - (ef - 4) * (ef - 4)\" }\n",
        );
        let wl2 = super::parse_workload(&map_yaml, &HashMap::new())
            .expect("map-form optimize should parse");
        let opt2 = wl2
            .phases
            .get("search")
            .and_then(|p| p.optimize.as_ref())
            .unwrap();
        assert_eq!(opt.objective.trim(), opt2.objective.trim());
        assert_eq!(opt.method, opt2.method);
    }

    /// SRD-75 §"Workload-load validation": phase-poll +
    /// concurrency > 1 is rejected at parse time. The
    /// predicate's evaluation depends on a serial sequence
    /// of capture writes; concurrent cycle execution has no
    /// well-defined semantic here.
    #[test]
    fn rejects_phase_poll_with_concurrency_gt_one() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    concurrency: 4
    poll:
      until: "done == 1"
    ops:
      op1:
        stmt: "noop"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("poll: + concurrency > 1 must error");
        assert!(
            err.contains("poll:") && err.contains("concurrency"),
            "expected error to name both poll: and concurrency; got: {err}"
        );
    }

    /// SRD-75 §"Workload-load validation": phase-poll with
    /// no ops is rejected — captures are produced by op
    /// execution; a poll-phase with no ops has nothing to
    /// drive the predicate.
    #[test]
    fn rejects_phase_poll_with_no_ops() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      until: "done == 1"
"#;
        let err =
            super::parse_workload(yaml, &HashMap::new()).expect_err("poll: without ops must error");
        assert!(
            err.contains("poll:") && err.contains("ops"),
            "expected error to name poll: and ops:; got: {err}"
        );
    }

    /// Unknown keys under `poll:` are typos waiting to
    /// silently default. Surface them at parse time.
    #[test]
    fn rejects_unknown_keys_under_phase_poll() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      until: "done == 1"
      tinerval_ms: 5000
    ops:
      op1:
        stmt: "noop"
"#;
        let err =
            super::parse_workload(yaml, &HashMap::new()).expect_err("typo under poll: must error");
        assert!(
            err.contains("tinerval_ms"),
            "expected error to name the offending key; got: {err}"
        );
    }

    /// SRD-75 §"on_timeout" — accepts the documented
    /// closed-vocabulary values and persists them on the
    /// parsed phase model. Both `error` and `abort` parse
    /// cleanly; case is normalised at parse time.
    #[test]
    fn parses_phase_poll_on_timeout_accepts_error_and_abort() {
        for (input, expected) in [
            ("error", "error"),
            ("abort", "abort"),
            ("ABORT", "abort"),
            ("Error", "error"),
        ] {
            let yaml = format!(
                r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      until: "done == 1"
      on_timeout: {input}
    ops:
      op1:
        stmt: "noop"
"#
            );
            let wl = super::parse_workload(&yaml, &HashMap::new())
                .expect("on_timeout value should parse");
            let phase = wl.phases.get("ensure").expect("ensure phase");
            let poll = phase.poll.as_ref().expect("phase.poll Some");
            assert_eq!(
                poll.on_timeout.as_deref(),
                Some(expected),
                "on_timeout '{input}' should normalise to '{expected}'"
            );
        }
    }

    /// SRD-75 §"on_timeout" — anything outside the
    /// closed vocabulary is rejected at parse time.
    /// Typos like `aborts:` or `fail:` should fail loudly,
    /// not silently default to `error` and obscure the
    /// workload-author's actual intent.
    #[test]
    fn rejects_phase_poll_unknown_on_timeout_value() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      until: "done == 1"
      on_timeout: fail
    ops:
      op1:
        stmt: "noop"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("unknown on_timeout value must error");
        assert!(
            err.contains("on_timeout") && err.contains("'fail'"),
            "expected error to name the offending value; got: {err}"
        );
    }

    /// `poll:` requires `until:` — the predicate is the
    /// loop's whole purpose.
    #[test]
    fn rejects_phase_poll_without_until() {
        let yaml = r#"
scenarios:
  default:
    - ensure
phases:
  ensure:
    cycles: 1
    poll:
      interval_ms: 5000
    ops:
      op1:
        stmt: "noop"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("poll: without until: must error");
        assert!(
            err.contains("until"),
            "expected error to require until:; got: {err}"
        );
    }

    /// SRD-83 Part 5 — the `effect:`/`action:` verb vocabulary is
    /// closed. An unknown verb used to fall silently to the shell
    /// default at the trip site; it is a load error now.
    #[test]
    fn rejects_unknown_stop_condition_effect() {
        let yaml = r#"
scenarios:
  default:
    - work
phases:
  work:
    cycles: 10
    stop_when:
      - when: "cycles_total > 5"
        effect: sotp
    ops:
      op1:
        stmt: "noop"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("unknown effect verb must be a load error");
        assert!(
            err.contains("sotp") && err.contains("stop|fail|abort"),
            "expected error to name the verb and the vocabulary; got: {err}"
        );
    }

    /// The three declared verbs — and the `action:` alias — all load.
    #[test]
    fn accepts_declared_stop_condition_effects() {
        for (key, verb) in [("effect", "stop"), ("effect", "fail"), ("action", "abort")] {
            let yaml = format!(
                r#"
scenarios:
  default:
    - work
phases:
  work:
    cycles: 10
    stop_when:
      - when: "cycles_total > 5"
        {key}: {verb}
    ops:
      op1:
        stmt: "noop"
"#
            );
            super::parse_workload(&yaml, &HashMap::new())
                .unwrap_or_else(|e| panic!("{key}: {verb} must load: {e}"));
        }
    }

    /// A path that contains `/` but doesn't begin with one is the
    /// ambiguous case: `0/value` is either a JSON-Pointer missing its
    /// leading slash (the classic transcription bug) or a member
    /// literally named "0/value" (which RFC 6901 spells `/0~1value`).
    /// Both readings are defensible, so the parser refuses rather than
    /// picking one — surfaced at parse time, not as silent
    /// capture-misses at runtime. Bare names WITHOUT a `/` are
    /// unambiguous and accepted; see the tests below.
    #[test]
    fn rejects_capture_path_without_leading_slash() {
        let yaml = r#"
scenarios:
  default:
    - probe
phases:
  probe:
    cycles: 1
    ops:
      read_state:
        adapter: http
        method: POST
        uri: "http://h:8778/"
        body: "[]"
        capture:
          bad: "0/value"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("path without leading / must error");
        assert!(
            err.contains("`capture.bad`") && err.contains("'/'"),
            "expected parse error to name the offending capture and require '/'; got: {err}"
        );
    }

    /// A bare top-level member name means exactly `/name`, and is
    /// normalized to it at parse time so runtime resolves one form. This
    /// is the spelling `verify:`'s `field:` uses, so an author moving
    /// between the two blocks on one op writes the same thing twice.
    #[test]
    fn accepts_bare_member_name_as_capture_path() {
        let yaml = r#"
scenarios:
  default:
    - probe
phases:
  probe:
    cycles: 1
    ops:
      read_state:
        adapter: http
        method: GET
        uri: "http://h:8778/"
        capture:
          bare: "value"
          pointer: "/value"
          rooted: ""
"#;
        let wl = super::parse_workload(yaml, &HashMap::new()).expect("both spellings must parse");
        let op = wl
            .phases
            .get("probe")
            .expect("phase")
            .ops
            .iter()
            .find(|o| o.name == "read_state")
            .expect("op");
        let path_of = |wire: &str| {
            op.captures
                .iter()
                .find(|c| c.as_name == wire)
                .unwrap_or_else(|| panic!("capture {wire} missing"))
                .path
                .clone()
                .expect("declarative capture carries a path")
        };
        assert_eq!(
            path_of("bare"),
            "/value",
            "a bare name normalizes to the equivalent pointer"
        );
        assert_eq!(
            path_of("pointer"),
            "/value",
            "and is indistinguishable from the pointer spelling downstream"
        );
        assert_eq!(
            path_of("rooted"),
            "",
            "empty still addresses the root document"
        );
    }

    /// `~` is a JSON-Pointer escape introducer (`~0` = `~`, `~1` = `/`), so
    /// a bare name containing one is ambiguous for the same reason `/` is.
    #[test]
    fn rejects_bare_name_containing_pointer_escape_char() {
        let yaml = r#"
scenarios:
  default:
    - probe
phases:
  probe:
    cycles: 1
    ops:
      read_state:
        adapter: http
        method: GET
        uri: "http://h:8778/"
        capture:
          bad: "a~1b"
"#;
        let err = super::parse_workload(yaml, &HashMap::new())
            .expect_err("a bare name with '~' must error");
        assert!(
            err.contains("`capture.bad`") && err.contains("ambiguous"),
            "expected an ambiguity error naming the capture; got: {err}"
        );
    }

    #[test]
    fn set_value_bare_is_reference() {
        // A bare `set:` value is a wire REFERENCE (consistent with
        // comprehension r-values); a string literal is the explicit
        // polydat-quoted form; a number/bool is a literal; a sequence is a list.
        fn set_source(pair: &str) -> String {
            let yaml = format!(
                "scenarios:\n  s:\n    - set: {{ {pair} }}\n      \
                 phases:\n        - p\nops:\n  p: \"test\"\n"
            );
            let wl = super::parse_workload(&yaml, &HashMap::new()).expect("parse");
            for n in &wl.scenarios["s"] {
                if let ScenarioNode::Bindings { source, .. } = n {
                    return source.trim().to_string();
                }
            }
            panic!("no Bindings node for `set: {{ {pair} }}`");
        }
        // bare identifier → wire reference (unquoted)
        assert_eq!(set_source("x: mnc"), "const x := mnc");
        assert_eq!(set_source("x: verbose"), "const x := verbose");
        // explicit polydat-quoted form → a string literal
        assert_eq!(set_source(r#"x: '"verbose"'"#), r#"const x := "verbose""#);
        // a YAML sequence → a list literal (of references)
        assert_eq!(set_source("x: [a, b]"), "const x := [a, b]");
        // numbers/bools are literals (serde distinguishes 8 from "8")
        assert_eq!(set_source("x: 8"), "const x := 8");
        assert_eq!(set_source(r#"x: "8""#), r#"const x := "8""#);
        assert_eq!(set_source("x: true"), "const x := true");
    }
}