wdl-engine 0.16.0

Execution engine for Workflow Description Language (WDL) documents.
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
//! Implementation of engine configuration.

use std::borrow::Cow;
use std::fmt;
use std::fs;
use std::marker::PhantomData;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::thread::available_parallelism;

use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::ensure;
use bytesize::ByteSize;
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::Buffer;
use indexmap::IndexMap;
use rowan::GreenNode;
use secrecy::ExposeSecret;
use tokio::process::Command;
use toml_spanner::Arena;
use toml_spanner::ErrorKind;
use toml_spanner::Failed;
use toml_spanner::FromToml;
use toml_spanner::Item;
use toml_spanner::Table;
use toml_spanner::ToToml;
use toml_spanner::ToTomlError;
use toml_spanner::Toml;
use toml_spanner::helper::display;
use toml_spanner::helper::flatten_any;
use toml_spanner::helper::parse_string;
use tracing::error;
use tracing::warn;
use url::Url;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::diagnostics::unknown_name;
use wdl_analysis::diagnostics::unknown_type;
use wdl_analysis::document::Task;
use wdl_analysis::types::PrimitiveType;
use wdl_analysis::types::Type;
use wdl_analysis::types::v1::ExprTypeEvaluator;
use wdl_ast::AstNode;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::lexer::Lexer;
use wdl_ast::v1::Expr;
use wdl_grammar::construct_tree;
use wdl_grammar::grammar::v1;
use wdl_grammar::grammar::v1::Parser;

use crate::CancellationContext;
use crate::EvaluationContext;
use crate::EvaluationPath;
use crate::Events;
use crate::NoneValue;
use crate::Object;
use crate::SYSTEM;
use crate::Value;
use crate::backend::ExecuteTaskRequest;
use crate::backend::TaskExecutionBackend;
use crate::convert_unit_string;
use crate::diagnostics::unknown_enum_choice;
use crate::http::Transferer;
use crate::path::is_supported_url;
use crate::tree::SyntaxNode;
use crate::v1::DEFAULT_TASK_REQUIREMENT_MAX_RETRIES;
use crate::v1::ExprEvaluator;

/// The inclusive maximum number of task retries the engine supports.
pub(crate) const MAX_RETRIES: u64 = 100;

/// The default task shell.
pub(crate) const DEFAULT_TASK_SHELL: &str = "bash";

/// The default task container.
pub(crate) const DEFAULT_TASK_CONTAINER: &str = "ubuntu:latest";

/// The default backend name.
const DEFAULT_BACKEND_NAME: &str = "default";

/// The maximum size, in bytes, for an LSF job name prefix.
const MAX_LSF_JOB_NAME_PREFIX: usize = 100;

/// The string that replaces redacted serialization fields.
const REDACTED: &str = "<REDACTED>";

/// Configuration sentinel value indicating use a system cache directory.
const CACHE_DIR_SENTINEL: &str = "system";

/// The default for HTTP retries.
///
/// Same default as defined in `cloud_copy`
const DEFAULT_HTTP_RETRIES: u32 = 5;

/// The default Apptainer executable name.
const DEFAULT_APPTAINER_EXECUTABLE: &str = "apptainer";

/// The default number of elements to concurrently process for a scatter
/// statement.
const DEFAULT_SCATTER_CONCURRENCY: u64 = 1000;

/// Gets the default root cache directory for the user.
pub(crate) fn cache_dir() -> Result<PathBuf> {
    /// The subdirectory within the user's cache directory for all caches
    const CACHE_DIR_ROOT: &str = "sprocket";

    Ok(dirs::cache_dir()
        .context("failed to determine user cache directory")?
        .join(CACHE_DIR_ROOT))
}

/// Creates a mapping of byte indexes in an unescaped TOML string to the
/// corresponding index in the escaped TOML string.
///
/// Only indexes that immediately follow an escape sequence are included in the
/// set.
///
/// All other mapping indexes can be synthesized by doing a binary search for
/// the unescaped index and either using the found entry's escaped index or
/// offset the unescaped index by the difference between the escaped and
/// unescaped indexes for the immediately preceding entry in the map at the
/// binary search insertion index (or zero if the insertion index is 0).
///
/// This is used as part of generating diagnostics for WDL expressions stored as
/// TOML strings.
///
/// The returned list is guaranteed sorted in both index spaces.
///
/// Note that if the string ends with an escape sequence, an additional mapping
/// of the exclusive end of the string will be included in the set.
///
/// # Panics
///
/// Panics if the TOML string contains invalid escape sequences.
///
/// Only use this function after the TOML has been validated.
///
/// # Examples
///
/// `foo\tbar` -> [(4, 5)]
/// `\"foo\" == \"bar\"` -> [(1, 2), (5, 7), (10, 13), (14, 18)]
fn escape_mapping(toml: &str) -> Vec<(usize, usize)> {
    let mut iter = toml.char_indices();
    let mut mapping = Vec::new();
    let mut new = 0;
    while let Some((old, c)) = iter.next() {
        if c != '\\' {
            new += c.len_utf8();
            continue;
        }

        match iter.next() {
            Some((_, 'u')) => {
                let c = u32::from_str_radix(&toml[old + 2 /* \u */..old + 6 /* \uXXXX */], 16)
                    .map(char::from_u32)
                    .expect("invalid TOML escape sequence")
                    .expect("invalid TOML escape character");
                new += c.len_utf8();

                // Move past the rest of the sequence
                iter.nth(3);
                mapping.push((new, old + 6 /* \uXXXX */));
            }
            Some((_, 'U')) => {
                let c = u32::from_str_radix(&toml[old + 2 /* \U */..old + 10 /* \UXXXXXXXX */], 16)
                    .map(char::from_u32)
                    .expect("invalid TOML escape sequence")
                    .expect("invalid TOML escape character");
                new += c.len_utf8();

                // Move past the rest of the sequence
                iter.nth(7);
                mapping.push((new, old + 10 /* \UXXXXXXXX */));
            }
            Some(_) => {
                // All other escape sequences are single byte replacements
                new += 1;
                mapping.push((new, old + 2 /* \? */));
            }
            None => break,
        }
    }

    mapping
}

/// Represents a secret string that is, by default, redacted for serialization.
///
/// This type is a wrapper around [`secrecy::SecretString`].
#[derive(Default, Debug, Clone)]
pub struct SecretString {
    /// The inner secret string.
    ///
    /// This type is not serializable.
    inner: secrecy::SecretString,
    /// Whether or not the secret string is redacted for serialization.
    ///
    /// If `true`, `<REDACTED>` is serialized for the string's value.
    ///
    /// If `false`, the inner secret string is exposed for serialization.
    ///
    /// Defaults to unredacted; users should call `redacted` on the [`Config`]
    /// prior to serialization.
    redacted: bool,
}

impl SecretString {
    /// Redacts the secret for serialization.
    ///
    /// By default, a [`SecretString`] is unredacted; when redacted, the string
    /// is replaced with `<REDACTED>` when serialized.
    pub fn redact(mut self) -> Self {
        self.redacted = true;
        self
    }

    /// Gets the inner [`secrecy::SecretString`].
    pub fn inner(&self) -> &secrecy::SecretString {
        &self.inner
    }
}

impl From<String> for SecretString {
    fn from(s: String) -> Self {
        Self {
            inner: s.into(),
            redacted: false,
        }
    }
}

impl From<&str> for SecretString {
    fn from(s: &str) -> Self {
        Self {
            inner: s.into(),
            redacted: false,
        }
    }
}

impl<'de> FromToml<'de> for SecretString {
    fn from_toml(ctx: &mut toml_spanner::Context<'de>, item: &Item<'de>) -> Result<Self, Failed> {
        Ok(String::from_toml(ctx, item)?.into())
    }
}

impl ToToml for SecretString {
    fn to_toml<'a>(&'a self, arena: &'a Arena) -> Result<Item<'a>, ToTomlError> {
        use secrecy::ExposeSecret;

        if self.redacted {
            REDACTED.to_toml(arena)
        } else {
            self.inner.expose_secret().to_toml(arena)
        }
    }
}

impl PartialEq for SecretString {
    fn eq(&self, other: &Self) -> bool {
        use secrecy::ExposeSecret;

        // Compare just on the string, ignoring the redaction flag
        self.inner.expose_secret() == other.inner.expose_secret()
    }
}

impl Eq for SecretString {}

/// Represents how an evaluation error or cancellation should be handled by the
/// engine.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, Toml)]
#[toml(Toml, rename_all = "snake_case")]
pub enum FailureMode {
    /// When an error is encountered or evaluation is canceled, evaluation waits
    /// for any outstanding tasks to complete.
    #[default]
    Slow,
    /// When an error is encountered or evaluation is canceled, any outstanding
    /// tasks that are executing are immediately canceled and evaluation waits
    /// for cancellation to complete.
    Fast,
}

/// Helper functions for `IndexMap` TOML serialization.
mod index_map {
    use indexmap::IndexMap;
    use toml_spanner::Arena;
    use toml_spanner::Context;
    use toml_spanner::Failed;
    use toml_spanner::FromToml;
    use toml_spanner::Item;
    use toml_spanner::Key;
    use toml_spanner::Table;
    use toml_spanner::TableStyle;
    use toml_spanner::ToToml;
    use toml_spanner::ToTomlError;

    /// Helper function for serializing an `IndexMap` to TOML.
    pub fn from_toml<'de, V>(
        ctx: &mut Context<'de>,
        item: &Item<'de>,
    ) -> Result<IndexMap<String, V>, Failed>
    where
        V: FromToml<'de>,
    {
        let table = item.require_table(ctx)?;
        let mut map = IndexMap::default();
        let mut had_error = false;
        for (key, item) in table {
            match V::from_toml(ctx, item) {
                Ok(v) => {
                    map.insert(key.name.into(), v);
                }
                Err(_) => had_error = true,
            }
        }

        if had_error { Err(Failed) } else { Ok(map) }
    }

    /// Helper function for deserializing an `IndexMap` from TOML.
    pub fn to_toml<'a, V>(
        value: &'a IndexMap<String, V>,
        arena: &'a Arena,
    ) -> Result<Item<'a>, ToTomlError>
    where
        V: ToToml,
    {
        let Some(mut table) = Table::try_with_capacity(value.len(), arena) else {
            return Err(ToTomlError::from(
                "length of table exceeded maximum capacity",
            ));
        };

        table.set_style(TableStyle::Implicit);

        for (k, v) in value {
            table.insert_unique(Key::new(k), v.to_toml(arena)?, arena);
        }

        Ok(table.into_item())
    }
}

/// Represents WDL evaluation configuration.
///
/// <div class="warning">
///
/// By default, serialization of [`Config`] will not redact the values of
/// secrets.
///
/// Use the [`Config::redact`] method prior to serialization to redact secrets.
///
/// </div>
#[derive(Debug, Clone, Toml, PartialEq, Eq)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct Config {
    /// HTTP configuration.
    #[toml(default, style = Header)]
    pub http: HttpConfig,
    /// Workflow evaluation configuration.
    #[toml(default, style = Header)]
    pub workflow: WorkflowConfig,
    /// Task evaluation configuration.
    #[toml(default, style = Header)]
    pub task: TaskConfig,
    /// The name of the backend to use.
    #[toml(default = DEFAULT_BACKEND_NAME.into())]
    pub backend: String,
    /// Task execution backends configuration.
    ///
    /// If the collection is empty and `backend` has the default value, the
    /// engine default backend is used.
    #[toml(default, with = index_map)]
    pub backends: IndexMap<String, BackendConfig>,
    /// Storage configuration.
    #[toml(default, style = Header)]
    pub storage: StorageConfig,
    /// (Experimental) Avoid environment-specific output; default is `false`.
    ///
    /// If this option is `true`, selected error messages and log output will
    /// avoid emitting environment-specific output such as absolute paths
    /// and system resource counts.
    ///
    /// This is largely meant to support "golden testing" where a test's success
    /// depends on matching an expected set of outputs exactly. Cues that
    /// help users overcome errors, such as the path to a temporary
    /// directory or the number of CPUs available to the system, confound this
    /// style of testing. This flag is a best-effort experimental attempt to
    /// reduce the impact of these differences in order to allow a wider
    /// range of golden tests to be written.
    #[toml(default)]
    pub suppress_env_specific_output: bool,
    /// (Experimental) Whether experimental features are enabled; default is
    /// `false`.
    ///
    /// Experimental features are provided to users with heavy caveats about
    /// their stability and rough edges. Use at your own risk, but feedback
    /// is quite welcome.
    #[toml(default)]
    pub experimental_features_enabled: bool,
    /// The failure mode for workflow or task evaluation.
    ///
    /// A value of [`FailureMode::Slow`] will result in evaluation waiting for
    /// executing tasks to complete upon error or interruption.
    ///
    /// A value of [`FailureMode::Fast`] will immediately attempt to cancel
    /// executing tasks upon error or interruption.
    #[toml(default, rename = "fail")]
    pub failure_mode: FailureMode,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            http: Default::default(),
            workflow: Default::default(),
            task: Default::default(),
            backend: DEFAULT_BACKEND_NAME.into(),
            backends: Default::default(),
            storage: Default::default(),
            suppress_env_specific_output: Default::default(),
            experimental_features_enabled: Default::default(),
            failure_mode: Default::default(),
        }
    }
}

impl Config {
    /// Gets a builder for [`Config`].
    pub fn builder() -> ConfigBuilder<Self> {
        ConfigBuilder::default()
    }

    /// Validates the evaluation configuration.
    pub async fn validate(&self) -> Result<()> {
        self.http.validate()?;
        self.workflow.validate()?;
        self.task.validate()?;

        if self.backends.is_empty() && self.backend == DEFAULT_BACKEND_NAME {
            // we'll use the default
        } else {
            let backend = &self.backend;
            if !self.backends.contains_key(backend) {
                bail!("a backend named `{backend}` is not present in the configuration");
            }
        }

        for backend in self.backends.values() {
            backend.validate().await?;
        }

        self.storage.validate()?;

        if self.suppress_env_specific_output && !self.experimental_features_enabled {
            bail!("`suppress_env_specific_output` requires enabling experimental features");
        }

        Ok(())
    }

    /// Redacts the secrets contained in the configuration.
    ///
    /// By default, secrets are redacted for serialization.
    pub fn redact(mut self) -> Self {
        for backend in self.backends.values_mut() {
            *backend = std::mem::take(backend).redact();
        }

        if let Some(auth) = self.storage.azure.auth.take() {
            self.storage.azure.auth = Some(auth.redact());
        }

        if let Some(auth) = self.storage.s3.auth.take() {
            self.storage.s3.auth = Some(auth.redact());
        }

        if let Some(auth) = self.storage.google.auth.take() {
            self.storage.google.auth = Some(auth.redact());
        }

        self
    }

    /// Gets the backend configuration.
    ///
    /// Returns an error if the configuration specifies a named backend that
    /// isn't present in the configuration.
    pub fn backend(&self) -> Result<Cow<'_, BackendConfig>> {
        if !self.backends.is_empty() {
            let backend = &self.backend;
            return Ok(Cow::Borrowed(self.backends.get(backend).ok_or_else(
                || anyhow!("a backend named `{backend}` is not present in the configuration"),
            )?));
        }
        // Use the default
        Ok(Cow::Owned(BackendConfig::default()))
    }

    /// Creates a new task execution backend based on this configuration.
    pub(crate) async fn create_backend(
        self: &Arc<Self>,
        run_root_dir: &Path,
        events: Events,
        cancellation: CancellationContext,
    ) -> Result<Arc<dyn TaskExecutionBackend>> {
        use crate::backend::*;

        match self.backend()?.as_ref() {
            BackendConfig::Local { .. } => {
                warn!(
                    "the engine is configured to use the local backend: tasks will not be run \
                     inside of a container"
                );
                Ok(Arc::new(LocalBackend::new(
                    self.clone(),
                    events,
                    cancellation,
                )?))
            }
            BackendConfig::Docker { .. } => Ok(Arc::new(
                DockerBackend::new(self.clone(), events, cancellation).await?,
            )),
            BackendConfig::Tes { .. } => Ok(Arc::new(
                TesBackend::new(self.clone(), events, cancellation).await?,
            )),
            BackendConfig::LsfApptainer { .. } => Ok(Arc::new(LsfApptainerBackend::new(
                self.clone(),
                run_root_dir,
                events,
                cancellation,
            )?)),
            BackendConfig::SlurmApptainer { .. } => Ok(Arc::new(SlurmApptainerBackend::new(
                self.clone(),
                run_root_dir,
                events,
                cancellation,
            )?)),
        }
    }
}

/// Represents the parallelism for HTTP downloads.
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub enum Parallelism {
    /// Use the available parallelism for the host system.
    #[default]
    Available,
    /// Use the specified parallelism.
    Use(usize),
}

impl From<usize> for Parallelism {
    fn from(value: usize) -> Self {
        Self::Use(value)
    }
}

impl From<Parallelism> for usize {
    fn from(value: Parallelism) -> Self {
        match value {
            Parallelism::Available => available_parallelism().map(Into::into).unwrap_or(1),
            Parallelism::Use(value) => value,
        }
    }
}

impl<'de> FromToml<'de> for Parallelism {
    fn from_toml(ctx: &mut toml_spanner::Context<'de>, item: &Item<'de>) -> Result<Self, Failed> {
        if let Some("available") = item.as_str() {
            return Ok(Self::Available);
        }

        if let Some(n) = item.as_u64().and_then(|n| usize::try_from(n).ok())
            && n > 0
        {
            return Ok(Self::Use(n));
        }

        Err(ctx.report_custom_error(
            "expected a positive integer or `available` for parallelism",
            item,
        ))
    }
}

impl ToToml for Parallelism {
    fn to_toml<'a>(&'a self, _: &'a Arena) -> Result<Item<'a>, ToTomlError> {
        match self {
            Self::Available => Ok(Item::string("available")),
            Self::Use(n) => Ok(i64::try_from(*n)
                .map_err(|e| ToTomlError {
                    message: format!("invalid parallelism: {e}").into(),
                })?
                .into()),
        }
    }
}

/// Represents HTTP configuration.
#[derive(Debug, Clone, Toml, PartialEq, Eq)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct HttpConfig {
    /// The HTTP download cache location.
    ///
    /// Defaults to an operating system specific cache directory for the user.
    #[toml(default = CACHE_DIR_SENTINEL.into())]
    pub cache_dir: String,
    /// The number of retries for transferring files.
    #[toml(default = DEFAULT_HTTP_RETRIES)]
    pub retries: u32,
    /// The maximum parallelism for file transfers.
    ///
    /// Defaults to the host's available parallelism.
    #[toml(default)]
    pub parallelism: Parallelism,
    /// The hash algorithm to use for calculating content digests for file
    /// uploads.
    ///
    /// Defaults to `sha256`.
    #[toml(default, FromToml with = parse_string, ToToml with = display)]
    pub hash_algorithm: cloud_copy::HashAlgorithm,
}

impl Default for HttpConfig {
    fn default() -> Self {
        Self {
            cache_dir: CACHE_DIR_SENTINEL.into(),
            retries: DEFAULT_HTTP_RETRIES,
            parallelism: Default::default(),
            hash_algorithm: Default::default(),
        }
    }
}

impl HttpConfig {
    /// Validates the HTTP configuration.
    pub fn validate(&self) -> Result<()> {
        if let Parallelism::Use(parallelism) = self.parallelism
            && parallelism == 0
        {
            bail!("configuration value `http.parallelism` cannot be zero");
        }
        Ok(())
    }

    /// Get the HTTP cache dir.
    pub fn cache_dir(&self) -> Result<PathBuf> {
        const DOWNLOADS_CACHE_SUBDIR: &str = "downloads";

        if self.using_system_cache_dir() {
            cache_dir().map(|d| d.join(DOWNLOADS_CACHE_SUBDIR))
        } else {
            Ok(PathBuf::from(&self.cache_dir))
        }
    }

    /// Is this configuration using a system cache dir?
    pub fn using_system_cache_dir(&self) -> bool {
        self.cache_dir == CACHE_DIR_SENTINEL
    }
}

/// Represents storage configuration.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct StorageConfig {
    /// Azure Blob Storage configuration.
    #[toml(default, style = Header)]
    pub azure: AzureStorageConfig,
    /// AWS S3 configuration.
    #[toml(default, style = Header)]
    pub s3: S3StorageConfig,
    /// Google Cloud Storage configuration.
    #[toml(default, style = Header)]
    pub google: GoogleStorageConfig,
}

impl StorageConfig {
    /// Validates the HTTP configuration.
    pub fn validate(&self) -> Result<()> {
        self.azure.validate()?;
        self.s3.validate()?;
        self.google.validate()?;
        Ok(())
    }
}

/// Represents authentication information for Azure Blob Storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct AzureStorageAuthConfig {
    /// The Azure Storage account name to use.
    pub account_name: String,
    /// The Azure Storage access key to use.
    pub access_key: SecretString,
}

impl AzureStorageAuthConfig {
    /// Validates the Azure Blob Storage authentication configuration.
    pub fn validate(&self) -> Result<()> {
        if self.account_name.is_empty() {
            bail!("configuration value `storage.azure.auth.account_name` is required");
        }

        if self.access_key.inner.expose_secret().is_empty() {
            bail!("configuration value `storage.azure.auth.access_key` is required");
        }

        Ok(())
    }

    /// Redacts the secrets contained in the Azure Blob Storage storage
    /// authentication configuration.
    pub fn redact(mut self) -> Self {
        self.access_key = self.access_key.redact();
        self
    }
}

/// Represents configuration for Azure Blob Storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct AzureStorageConfig {
    /// The Azure Blob Storage authentication configuration.
    #[toml(style = Header)]
    pub auth: Option<AzureStorageAuthConfig>,
}

impl AzureStorageConfig {
    /// Validates the Azure Blob Storage configuration.
    pub fn validate(&self) -> Result<()> {
        if let Some(auth) = &self.auth {
            auth.validate()?;
        }

        Ok(())
    }
}

/// Represents authentication information for AWS S3 storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct S3StorageAuthConfig {
    /// The AWS Access Key ID to use.
    pub access_key_id: String,
    /// The AWS Secret Access Key to use.
    pub secret_access_key: SecretString,
}

impl S3StorageAuthConfig {
    /// Validates the AWS S3 storage authentication configuration.
    pub fn validate(&self) -> Result<()> {
        if self.access_key_id.is_empty() {
            bail!("configuration value `storage.s3.auth.access_key_id` is required");
        }

        if self.secret_access_key.inner.expose_secret().is_empty() {
            bail!("configuration value `storage.s3.auth.secret_access_key` is required");
        }

        Ok(())
    }

    /// Redacts the secrets contained in the AWS S3 storage authentication
    /// configuration.
    pub fn redact(mut self) -> Self {
        self.secret_access_key = self.secret_access_key.redact();
        self
    }
}

/// Represents configuration for AWS S3 storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct S3StorageConfig {
    /// The default region to use for S3-schemed URLs (e.g.
    /// `s3://<bucket>/<blob>`).
    ///
    /// Defaults to `us-east-1`.
    pub region: Option<String>,

    /// The AWS S3 storage authentication configuration.
    #[toml(style = Header)]
    pub auth: Option<S3StorageAuthConfig>,
}

impl S3StorageConfig {
    /// Validates the AWS S3 storage configuration.
    pub fn validate(&self) -> Result<()> {
        if let Some(auth) = &self.auth {
            auth.validate()?;
        }

        Ok(())
    }
}

/// Represents authentication information for Google Cloud Storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct GoogleStorageAuthConfig {
    /// The HMAC Access Key to use.
    pub access_key: String,
    /// The HMAC Secret to use.
    pub secret: SecretString,
}

impl GoogleStorageAuthConfig {
    /// Validates the Google Cloud Storage authentication configuration.
    pub fn validate(&self) -> Result<()> {
        if self.access_key.is_empty() {
            bail!("configuration value `storage.google.auth.access_key` is required");
        }

        if self.secret.inner.expose_secret().is_empty() {
            bail!("configuration value `storage.google.auth.secret` is required");
        }

        Ok(())
    }

    /// Redacts the secrets contained in the Google Cloud Storage authentication
    /// configuration.
    pub fn redact(mut self) -> Self {
        self.secret = self.secret.redact();
        self
    }
}

/// Represents configuration for Google Cloud Storage.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct GoogleStorageConfig {
    /// The Google Cloud Storage authentication configuration.
    #[toml(style = Header)]
    pub auth: Option<GoogleStorageAuthConfig>,
}

impl GoogleStorageConfig {
    /// Validates the Google Cloud Storage configuration.
    pub fn validate(&self) -> Result<()> {
        if let Some(auth) = &self.auth {
            auth.validate()?;
        }

        Ok(())
    }
}

/// Represents workflow evaluation configuration.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct WorkflowConfig {
    /// Scatter statement evaluation configuration.
    #[toml(default, style = Header)]
    pub scatter: ScatterConfig,
}

impl WorkflowConfig {
    /// Validates the workflow configuration.
    pub fn validate(&self) -> Result<()> {
        self.scatter.validate()?;
        Ok(())
    }
}

/// Represents scatter statement evaluation configuration.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct ScatterConfig {
    /// The number of scatter array elements to process concurrently.
    ///
    /// Defaults to `1000`.
    ///
    /// A value of `0` is invalid.
    ///
    /// Lower values use less memory for evaluation and higher values may better
    /// saturate the task execution backend with tasks to execute for large
    /// scatters.
    ///
    /// This setting does not change how many tasks an execution backend can run
    /// concurrently, but may affect how many tasks are sent to the backend to
    /// run at a time.
    ///
    /// For example, if `concurrency` was set to 10 and we evaluate the
    /// following scatters:
    ///
    /// ```wdl
    /// scatter (i in range(100)) {
    ///     call my_task
    /// }
    ///
    /// scatter (j in range(100)) {
    ///     call my_task as my_task2
    /// }
    /// ```
    ///
    /// Here each scatter is independent and therefore there will be 20 calls
    /// (10 for each scatter) made concurrently. If the task execution
    /// backend can only execute 5 tasks concurrently, 5 tasks will execute
    /// and 15 will be "ready" to execute and waiting for an executing task
    /// to complete.
    ///
    /// If instead we evaluate the following scatters:
    ///
    /// ```wdl
    /// scatter (i in range(100)) {
    ///     scatter (j in range(100)) {
    ///         call my_task
    ///     }
    /// }
    /// ```
    ///
    /// Then there will be 100 calls (10*10 as 10 are made for each outer
    /// element) made concurrently. If the task execution backend can only
    /// execute 5 tasks concurrently, 5 tasks will execute and 95 will be
    /// "ready" to execute and waiting for an executing task to complete.
    ///
    /// <div class="warning">
    /// Warning: nested scatter statements cause exponential memory usage based
    /// on this value, as each scatter statement evaluation requires allocating
    /// new scopes for scatter array elements being processed. </div>
    #[toml(default = DEFAULT_SCATTER_CONCURRENCY)]
    pub concurrency: u64,
}

impl Default for ScatterConfig {
    fn default() -> Self {
        Self {
            concurrency: DEFAULT_SCATTER_CONCURRENCY,
        }
    }
}

impl ScatterConfig {
    /// Validates the scatter configuration.
    pub fn validate(&self) -> Result<()> {
        if self.concurrency == 0 {
            bail!("configuration value `workflow.scatter.concurrency` cannot be zero");
        }

        Ok(())
    }
}

/// Represents the supported call caching modes.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, Toml)]
#[toml(Toml, rename_all = "snake_case")]
pub enum CallCachingMode {
    /// Call caching is disabled.
    ///
    /// The call cache is not checked and new entries are not added to the
    /// cache.
    ///
    /// This is the default value.
    #[default]
    Off,
    /// Call caching is enabled.
    ///
    /// The call cache is checked and new entries are added to the cache.
    ///
    /// Defaults the `cacheable` task hint to `true`.
    On,
    /// Call caching is enabled only for tasks that explicitly have a
    /// `cacheable` hint set to `true`.
    ///
    /// The call cache is checked and new entries are added to the cache *only*
    /// for tasks that have the `cacheable` hint set to `true`.
    ///
    /// Defaults the `cacheable` task hint to `false`.
    Explicit,
}

/// Represents the supported modes for calculating content digests.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, Toml)]
#[toml(Toml, rename_all = "snake_case")]
pub enum ContentDigestMode {
    /// Use a strong digest for file content.
    ///
    /// Strong digests require hashing all of the contents of a file; this may
    /// noticeably impact performance for very large files.
    ///
    /// This setting guarantees that a modified file will be detected.
    Strong,
    /// Use a weak digest for file content.
    ///
    /// A weak digest is based solely off of file metadata, such as size and
    /// last modified time.
    ///
    /// This setting cannot guarantee the detection of modified files and may
    /// result in a modified file not causing a call cache entry to be
    /// invalidated.
    ///
    /// However, it is substantially faster than using a strong digest.
    #[default]
    Weak,
}

/// Represents the maximum number of retries for tasks.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum Retries {
    /// Use the default number of retries for task execution.
    #[default]
    Default,
    /// Use the specified number of retries for task execution.
    Use(u64),
}

impl From<u64> for Retries {
    fn from(value: u64) -> Self {
        Self::Use(value)
    }
}

impl From<Retries> for u64 {
    fn from(value: Retries) -> Self {
        match value {
            Retries::Default => DEFAULT_TASK_REQUIREMENT_MAX_RETRIES,
            Retries::Use(value) => value,
        }
    }
}

impl<'de> FromToml<'de> for Retries {
    fn from_toml(ctx: &mut toml_spanner::Context<'de>, item: &Item<'de>) -> Result<Self, Failed> {
        if let Some("default") = item.as_str() {
            return Ok(Self::Default);
        }

        if let Some(n) = item.as_u64()
            && n < MAX_RETRIES
        {
            return Ok(Self::Use(n));
        }

        Err(ctx.report_custom_error(
            format!("expected an integer less than {MAX_RETRIES} or `default` for retries"),
            item,
        ))
    }
}

impl ToToml for Retries {
    fn to_toml<'a>(&'a self, _: &'a Arena) -> Result<Item<'a>, ToTomlError> {
        match self {
            Self::Default => Ok(Item::string("default")),
            Self::Use(n) => Ok(i64::try_from(*n)
                .map_err(|e| ToTomlError {
                    message: format!("invalid retries: {e}").into(),
                })?
                .into()),
        }
    }
}

/// Represents task evaluation configuration.
#[derive(Debug, Clone, Toml, PartialEq, Eq)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct TaskConfig {
    /// The default maximum number of retries to attempt if a task fails.
    ///
    /// A task's `max_retries` requirement will override this value.
    #[toml(default)]
    pub retries: Retries,
    /// The default container to use if a container is not specified in a task's
    /// requirements.
    #[toml(default = DEFAULT_TASK_CONTAINER.into())]
    pub container: String,
    /// The default shell to use for tasks.
    ///
    /// <div class="warning">
    /// Warning: the use of a shell other than `bash` may lead to tasks that may
    /// not be portable to other execution engines.
    ///
    /// The shell must support a `-c` option to run a specific script file (i.e.
    /// an evaluated task command).
    ///
    /// Note that this option affects all task commands, so every container that
    /// is used must contain the specified shell.
    ///
    /// If using this setting causes your tasks to fail, please do not file an
    /// issue. </div>
    #[toml(default = DEFAULT_TASK_SHELL.into())]
    pub shell: String,
    /// The behavior when a task's `cpu` requirement cannot be met.
    #[toml(default)]
    pub cpu_limit_behavior: TaskResourceLimitBehavior,
    /// The behavior when a task's `memory` requirement cannot be met.
    #[toml(default)]
    pub memory_limit_behavior: TaskResourceLimitBehavior,
    /// The call cache directory to use for caching task execution results.
    ///
    /// Defaults to an operating system specific cache directory for the user.
    #[toml(default = CACHE_DIR_SENTINEL.into())]
    pub cache_dir: String,
    /// The call caching mode to use for tasks.
    #[toml(default)]
    pub cache: CallCachingMode,
    /// The content digest mode to use.
    ///
    /// Used as part of call caching.
    #[toml(default)]
    pub digests: ContentDigestMode,
    /// Keys of task requirements to exclude from call cache checking.
    ///
    /// When specified, these requirement keys will be ignored when
    /// calculating cache keys and validating cache entries.
    ///
    /// This can be useful for requirements that may vary between runs
    /// but should not invalidate the cache (e.g., dynamic resource
    /// allocation).
    #[toml(default)]
    pub excluded_cache_requirements: Vec<String>,
    /// Keys of task hints to exclude from call cache checking.
    ///
    /// When specified, these hint keys will be ignored when
    /// calculating cache keys and validating cache entries.
    ///
    /// This can be useful for hints that may vary between runs
    /// but should not invalidate the cache.
    #[toml(default)]
    pub excluded_cache_hints: Vec<String>,
    /// Keys of task inputs to exclude from call cache checking.
    ///
    /// When specified, these input keys will be ignored when
    /// calculating cache keys and validating cache entries.
    ///
    /// This can be useful for inputs that may vary between runs
    /// but should not affect the task's output.
    #[toml(default)]
    pub excluded_cache_inputs: Vec<String>,
}

impl Default for TaskConfig {
    fn default() -> Self {
        Self {
            retries: Default::default(),
            container: DEFAULT_TASK_CONTAINER.into(),
            shell: DEFAULT_TASK_SHELL.into(),
            cpu_limit_behavior: Default::default(),
            memory_limit_behavior: Default::default(),
            cache_dir: CACHE_DIR_SENTINEL.into(),
            cache: Default::default(),
            digests: Default::default(),
            excluded_cache_requirements: Default::default(),
            excluded_cache_hints: Default::default(),
            excluded_cache_inputs: Default::default(),
        }
    }
}

impl TaskConfig {
    /// Validates the task evaluation configuration.
    pub fn validate(&self) -> Result<()> {
        if let Retries::Use(value) = self.retries
            && value >= MAX_RETRIES
        {
            bail!("configuration value `task.retries` cannot exceed {MAX_RETRIES}");
        }

        Ok(())
    }

    /// Get the configured cache dir if it is set.
    pub fn cache_dir(&self) -> Option<PathBuf> {
        if self.cache_dir == CACHE_DIR_SENTINEL {
            None
        } else {
            Some(PathBuf::from(&self.cache_dir))
        }
    }
}

/// The behavior when a task resource requirement, such as `cpu` or `memory`,
/// cannot be met.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub enum TaskResourceLimitBehavior {
    /// Try executing a task with the maximum amount of the resource available
    /// when the task's corresponding requirement cannot be met.
    TryWithMax,
    /// Do not execute a task if its corresponding requirement cannot be met.
    ///
    /// This is the default behavior.
    #[default]
    Deny,
}

/// Represents supported task execution backends.
#[derive(Debug, Clone, Toml, PartialEq, Eq)]
#[toml(Toml, rename_all = "snake_case", tag = "type")]
pub enum BackendConfig {
    /// Use the local task execution backend.
    Local {
        /// The inner local backend configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: LocalBackendConfig,
    },
    /// Use the Docker task execution backend.
    Docker {
        /// The inner Docker backend configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: DockerBackendConfig,
    },
    /// Use the TES task execution backend.
    Tes {
        /// The inner TES backend configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: TesBackendConfig,
    },
    /// Use the experimental LSF + Apptainer task execution backend.
    ///
    /// Requires enabling experimental features.
    LsfApptainer {
        /// The inner LSF Apptainer backend configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: LsfApptainerBackendConfig,
    },
    /// Use the experimental Slurm + Apptainer task execution backend.
    ///
    /// Requires enabling experimental features.
    SlurmApptainer {
        /// The inner Slurm Apptainer backend configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: SlurmApptainerBackendConfig,
    },
}

impl Default for BackendConfig {
    fn default() -> Self {
        Self::Docker {
            config: Default::default(),
        }
    }
}

impl BackendConfig {
    /// Validates the backend configuration.
    pub async fn validate(&self) -> Result<()> {
        match self {
            Self::Local { config } => config.validate(),
            Self::Docker { config } => config.validate(),
            Self::Tes { config } => config.validate(),
            Self::LsfApptainer { config } => config.validate().await,
            Self::SlurmApptainer { config } => config.validate().await,
        }
    }

    /// Converts the backend configuration into a local backend configuration
    ///
    /// Returns `None` if the backend configuration is not local.
    pub fn as_local(&self) -> Option<&LocalBackendConfig> {
        match self {
            Self::Local { config } => Some(config),
            _ => None,
        }
    }

    /// Converts the backend configuration into a Docker backend configuration
    ///
    /// Returns `None` if the backend configuration is not Docker.
    pub fn as_docker(&self) -> Option<&DockerBackendConfig> {
        match self {
            Self::Docker { config } => Some(config),
            _ => None,
        }
    }

    /// Converts the backend configuration into a TES backend configuration
    ///
    /// Returns `None` if the backend configuration is not TES.
    pub fn as_tes(&self) -> Option<&TesBackendConfig> {
        match self {
            Self::Tes { config } => Some(config),
            _ => None,
        }
    }

    /// Converts the backend configuration into a LSF Apptainer backend
    /// configuration
    ///
    /// Returns `None` if the backend configuration is not LSF Apptainer.
    pub fn as_lsf_apptainer(&self) -> Option<&LsfApptainerBackendConfig> {
        match self {
            Self::LsfApptainer { config } => Some(config),
            _ => None,
        }
    }

    /// Converts the backend configuration into a Slurm Apptainer backend
    /// configuration
    ///
    /// Returns `None` if the backend configuration is not Slurm Apptainer.
    pub fn as_slurm_apptainer(&self) -> Option<&SlurmApptainerBackendConfig> {
        match self {
            Self::SlurmApptainer { config } => Some(config),
            _ => None,
        }
    }

    /// Redacts the secrets contained in the backend configuration.
    pub fn redact(self) -> Self {
        match self {
            Self::Local { .. }
            | Self::Docker { .. }
            | Self::LsfApptainer { .. }
            | Self::SlurmApptainer { .. } => self,
            Self::Tes { config } => Self::Tes {
                config: config.redact(),
            },
        }
    }
}

impl From<LocalBackendConfig> for BackendConfig {
    fn from(config: LocalBackendConfig) -> Self {
        Self::Local { config }
    }
}

impl From<DockerBackendConfig> for BackendConfig {
    fn from(config: DockerBackendConfig) -> Self {
        Self::Docker { config }
    }
}

impl From<TesBackendConfig> for BackendConfig {
    fn from(config: TesBackendConfig) -> Self {
        Self::Tes { config }
    }
}

impl From<LsfApptainerBackendConfig> for BackendConfig {
    fn from(config: LsfApptainerBackendConfig) -> Self {
        Self::LsfApptainer { config }
    }
}

impl From<SlurmApptainerBackendConfig> for BackendConfig {
    fn from(config: SlurmApptainerBackendConfig) -> Self {
        Self::SlurmApptainer { config }
    }
}

/// Represents configuration for the local task execution backend.
///
/// <div class="warning">
/// Warning: the local task execution backend spawns processes on the host
/// directly without the use of a container; only use this backend on trusted
/// WDL. </div>
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct LocalBackendConfig {
    /// Set the number of CPUs available for task execution.
    ///
    /// Defaults to the number of logical CPUs for the host.
    ///
    /// The value cannot be zero or exceed the host's number of CPUs.
    pub cpu: Option<u64>,

    /// Set the total amount of memory for task execution as a unit string (e.g.
    /// `2 GiB`).
    ///
    /// Defaults to the total amount of memory for the host.
    ///
    /// The value cannot be zero or exceed the host's total amount of memory.
    pub memory: Option<String>,
}

impl LocalBackendConfig {
    /// Validates the local task execution backend configuration.
    pub fn validate(&self) -> Result<()> {
        if let Some(cpu) = self.cpu {
            if cpu == 0 {
                bail!("local backend configuration value `cpu` cannot be zero");
            }

            let total = SYSTEM.cpus().len() as u64;
            if cpu > total {
                bail!(
                    "local backend configuration value `cpu` cannot exceed the virtual CPUs \
                     available to the host ({total})"
                );
            }
        }

        if let Some(memory) = &self.memory {
            let memory = convert_unit_string(memory).with_context(|| {
                format!("local backend configuration value `memory` has invalid value `{memory}`")
            })?;

            if memory == 0 {
                bail!("local backend configuration value `memory` cannot be zero");
            }

            let total = SYSTEM.total_memory();
            if memory > total {
                bail!(
                    "local backend configuration value `memory` cannot exceed the total memory of \
                     the host ({total} bytes)"
                );
            }
        }

        Ok(())
    }
}

/// Represents configuration for the Docker backend.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct DockerBackendConfig {
    /// Whether or not to remove a task's container after the task completes.
    ///
    /// Defaults to `true`.
    #[toml(default = true)]
    pub cleanup: bool,
}

impl DockerBackendConfig {
    /// Validates the Docker backend configuration.
    pub fn validate(&self) -> Result<()> {
        Ok(())
    }
}

impl Default for DockerBackendConfig {
    fn default() -> Self {
        Self { cleanup: true }
    }
}

/// Represents HTTP basic authentication configuration.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct BasicAuthConfig {
    /// The HTTP basic authentication username.
    pub username: String,
    /// The HTTP basic authentication password.
    pub password: SecretString,
}

impl BasicAuthConfig {
    /// Validates the HTTP basic auth configuration.
    pub fn validate(&self) -> Result<()> {
        Ok(())
    }

    /// Redacts the secrets contained in the HTTP basic auth configuration.
    pub fn redact(mut self) -> Self {
        self.password = self.password.redact();
        self
    }
}

/// Represents HTTP bearer token authentication configuration.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct BearerAuthConfig {
    /// The HTTP bearer authentication token.
    pub token: SecretString,
}

impl BearerAuthConfig {
    /// Validates the HTTP bearer auth configuration.
    pub fn validate(&self) -> Result<()> {
        Ok(())
    }

    /// Redacts the secrets contained in the HTTP bearer auth configuration.
    pub fn redact(mut self) -> Self {
        self.token = self.token.redact();
        self
    }
}

/// Represents the kind of authentication for a TES backend.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", tag = "type")]
pub enum TesBackendAuthConfig {
    /// Use basic authentication for the TES backend.
    Basic {
        /// The inner basic auth configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: BasicAuthConfig,
    },
    /// Use bearer token authentication for the TES backend.
    Bearer {
        /// The inner bearer auth configuration.
        #[toml(default, style = Header, flatten, with = flatten_any)]
        config: BearerAuthConfig,
    },
}

impl TesBackendAuthConfig {
    /// Validates the TES backend authentication configuration.
    pub fn validate(&self) -> Result<()> {
        match self {
            Self::Basic { config } => config.validate(),
            Self::Bearer { config } => config.validate(),
        }
    }

    /// Redacts the secrets contained in the TES backend authentication
    /// configuration.
    pub fn redact(self) -> Self {
        match self {
            Self::Basic { config } => Self::Basic {
                config: config.redact(),
            },
            Self::Bearer { config } => Self::Bearer {
                config: config.redact(),
            },
        }
    }
}

impl From<BasicAuthConfig> for TesBackendAuthConfig {
    fn from(config: BasicAuthConfig) -> Self {
        Self::Basic { config }
    }
}

impl From<BearerAuthConfig> for TesBackendAuthConfig {
    fn from(config: BearerAuthConfig) -> Self {
        Self::Bearer { config }
    }
}

/// Represents configuration for the Task Execution Service (TES) backend.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct TesBackendConfig {
    /// The URL of the Task Execution Service.
    #[toml(FromToml with = parse_string, ToToml with = display)]
    pub url: Option<Url>,

    /// The authentication configuration for the TES backend.
    #[toml(style = Header)]
    pub auth: Option<TesBackendAuthConfig>,

    /// The root cloud storage URL for storing inputs.
    #[toml(FromToml with = parse_string, ToToml with = display)]
    pub inputs: Option<Url>,

    /// The root cloud storage URL for storing outputs.
    #[toml(FromToml with = parse_string, ToToml with = display)]
    pub outputs: Option<Url>,

    /// The polling interval, in seconds, for checking task status.
    ///
    /// Defaults to 1 second.
    pub interval: Option<u64>,

    /// The number of retries after encountering an error communicating with the
    /// TES server.
    ///
    /// Defaults to no retries.
    pub retries: Option<u32>,

    /// The maximum number of concurrent requests the backend will send to the
    /// TES server.
    ///
    /// Defaults to 10 concurrent requests.
    pub max_concurrency: Option<u32>,

    /// Whether or not the TES server URL may use an insecure protocol like
    /// HTTP.
    #[toml(default)]
    pub insecure: bool,
}

impl TesBackendConfig {
    /// Validates the TES backend configuration.
    pub fn validate(&self) -> Result<()> {
        match &self.url {
            Some(url) => {
                if !self.insecure && url.scheme() != "https" {
                    bail!(
                        "TES backend configuration value `url` has invalid value `{url}`: URL \
                         must use a HTTPS scheme"
                    );
                }
            }
            None => bail!("TES backend configuration value `url` is required"),
        }

        if let Some(auth) = &self.auth {
            auth.validate()?;
        }

        if let Some(max_concurrency) = self.max_concurrency
            && max_concurrency == 0
        {
            bail!("TES backend configuration value `max_concurrency` cannot be zero");
        }

        match &self.inputs {
            Some(url) => {
                if !is_supported_url(url.as_str()) {
                    bail!(
                        "TES backend storage configuration value `inputs` has invalid value \
                         `{url}`: URL scheme is not supported"
                    );
                }

                if !url.path().ends_with('/') {
                    bail!(
                        "TES backend storage configuration value `inputs` has invalid value \
                         `{url}`: URL path must end with a slash"
                    );
                }
            }
            None => bail!("TES backend configuration value `inputs` is required"),
        }

        match &self.outputs {
            Some(url) => {
                if !is_supported_url(url.as_str()) {
                    bail!(
                        "TES backend storage configuration value `outputs` has invalid value \
                         `{url}`: URL scheme is not supported"
                    );
                }

                if !url.path().ends_with('/') {
                    bail!(
                        "TES backend storage configuration value `outputs` has invalid value \
                         `{url}`: URL path must end with a slash"
                    );
                }
            }
            None => bail!("TES backend storage configuration value `outputs` is required"),
        }

        Ok(())
    }

    /// Redacts the secrets contained in the TES backend configuration.
    pub fn redact(mut self) -> Self {
        if let Some(auth) = self.auth.take() {
            self.auth = Some(auth.redact());
        }

        self
    }
}

/// Configuration for the Apptainer container runtime.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct ApptainerConfig {
    /// Path to the Apptainer (or Singularity) executable.
    ///
    /// Defaults to `"apptainer"`. Set to `"singularity"` or a full path
    /// (e.g., `/usr/local/bin/apptainer`) if the executable is not on `PATH`
    /// or if using Singularity instead.
    #[toml(default = DEFAULT_APPTAINER_EXECUTABLE.into())]
    pub executable: String,

    /// Path to a shared directory for caching pulled `.sif` images.
    ///
    /// When set, pulled images are stored in this directory and shared
    /// across runs. When unset, images are stored in a per-run directory
    /// that is not shared.
    pub image_cache_dir: Option<PathBuf>,

    /// Additional command-line arguments to pass to `apptainer exec` when
    /// executing tasks.
    #[toml(default)]
    pub extra_args: Vec<String>,
}

impl Default for ApptainerConfig {
    fn default() -> Self {
        Self {
            executable: DEFAULT_APPTAINER_EXECUTABLE.into(),
            image_cache_dir: None,
            extra_args: Default::default(),
        }
    }
}

impl ApptainerConfig {
    /// Validate that Apptainer is appropriately configured.
    pub async fn validate(&self) -> Result<(), anyhow::Error> {
        Ok(())
    }
}

/// Represents a condition in a conditional argument.
///
/// The expression is evaluated in a context where a task's computed `cpu`,
/// `memory`, `gpu`, `fpga`, and `disks` values and evaluated `hint` object are
/// available as variables.
///
/// The expression is type checked during configuration deserialization to
/// ensure it is a valid WDL expression of type `Boolean`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Condition {
    /// The raw WDL expression string.
    pub raw: String,
    /// The parsed and validated conditional expression.
    expr: GreenNode,
}

impl Condition {
    /// Constructs a new `Condition` given a raw WDL expression string.
    pub fn new(raw: impl Into<String>) -> Result<Self, Vec<Diagnostic>> {
        /// Type evaluation context used for resolving the type of conditional
        /// expressions.
        #[derive(Default)]
        struct Context(Vec<Diagnostic>);

        impl wdl_analysis::types::v1::EvaluationContext for Context {
            fn version(&self) -> SupportedVersion {
                Default::default()
            }

            fn resolve_name(&mut self, name: &str, span: Span) -> Option<Type> {
                match name {
                    "cpu" => Some(PrimitiveType::Float.into()),
                    "memory" => Some(PrimitiveType::Integer.into()),
                    "gpu" | "fpga" => Some(PrimitiveType::Boolean.into()),
                    "disks" => Some(PrimitiveType::Integer.into()),
                    "hint" => Some(Type::Object),
                    _ => {
                        self.add_diagnostic(unknown_name(name, span));
                        None
                    }
                }
            }

            fn resolve_type_name(&mut self, name: &str, span: Span) -> Result<Type, Diagnostic> {
                Err(unknown_type(name, span))
            }

            fn task(&self) -> Option<&Task> {
                None
            }

            fn diagnostics_config(&self) -> DiagnosticsConfig {
                Default::default()
            }

            fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
                self.0.push(diagnostic);
            }
        }

        let raw = raw.into();
        let mut parser = Parser::new(Lexer::new(&raw));
        let marker = parser.start();
        match v1::expr(&mut parser, marker) {
            Ok(()) => {
                if let Some((_, span)) = parser.next() {
                    return Err(vec![
                        Diagnostic::error("expected a single WDL expression")
                            .with_label("extraneous WDL source starts here", span),
                    ]);
                }

                let output = parser.finish();
                if !output.diagnostics.is_empty() {
                    return Err(output.diagnostics);
                }

                let expr = Expr::cast(construct_tree(raw.as_ref(), output.events))
                    .expect("node should cast");

                // Determine the type of the expression
                let mut context = Context::default();
                let ty = ExprTypeEvaluator::new(&mut context)
                    .evaluate_expr(&expr)
                    .unwrap_or(Type::Union);

                if !context.0.is_empty() {
                    return Err(context.0);
                }

                match ty {
                    Type::Primitive(PrimitiveType::Boolean, false) | Type::Union => {}
                    _ => {
                        return Err(vec![
                            Diagnostic::error(format!(
                                "conditional expression is expected to be type `Boolean`, but \
                                 found type `{ty}`",
                            ))
                            .with_highlight(expr.span()),
                        ]);
                    }
                }

                Ok(Self {
                    raw,
                    expr: expr.inner().green().into_owned(),
                })
            }
            Err((marker, diagnostic)) => {
                marker.abandon(&mut parser);
                Err(vec![diagnostic.into()])
            }
        }
    }

    /// Evaluates the condition's expression for the given execution request and
    /// returns the result.
    ///
    /// Returns an error if the evaluation resulted in an error.
    pub(crate) async fn evaluate(
        &self,
        request: &ExecuteTaskRequest<'_>,
        transferer: &dyn Transferer,
    ) -> Result<bool> {
        /// Helper that implements `EvaluationContext`.
        struct Context<'a> {
            /// The task execution request.
            request: &'a ExecuteTaskRequest<'a>,
            /// The file transferer for evaluation.
            transferer: &'a dyn Transferer,
        }

        impl EvaluationContext for Context<'_> {
            fn version(&self) -> SupportedVersion {
                Default::default()
            }

            fn resolve_name(&self, name: &str, span: Span) -> Result<Value, Diagnostic> {
                match name {
                    "cpu" => Ok(self.request.constraints.cpu.into()),
                    "memory" => Ok((self.request.constraints.memory as i64).into()),
                    "gpu" => Ok((!self.request.constraints.gpu.is_empty()).into()),
                    "fpga" => Ok((!self.request.constraints.fpga.is_empty()).into()),
                    "disks" => Ok(self
                        .request
                        .constraints
                        .disks
                        .iter()
                        .map(|(_, s)| *s)
                        .sum::<i64>()
                        .into()),
                    "hint" => Ok(self.request.hints.clone().into()),
                    _ => Err(unknown_name(name, span)),
                }
            }

            fn resolve_type_name(&self, name: &str, span: Span) -> Result<Type, Diagnostic> {
                Err(unknown_type(name, span))
            }

            fn enum_choice_value(
                &self,
                enum_name: &str,
                choice_name: &str,
            ) -> Result<Value, Diagnostic> {
                Err(unknown_enum_choice(enum_name, choice_name))
            }

            fn base_dir(&self) -> &EvaluationPath {
                self.request.base_dir
            }

            fn temp_dir(&self) -> &Path {
                self.request.temp_dir
            }

            fn transferer(&self) -> &dyn Transferer {
                self.transferer
            }

            fn object_access(&self, object: &Object, name: &str) -> Option<Value> {
                // If the object being accessed is not the hint object, let the access proceed
                // normally
                if !Arc::ptr_eq(&object.members, &self.request.hints.members) {
                    return None;
                }

                // Access to the hints object first checks for a hint override in the inputs and
                // then falls back to the task's hints; if the name is not present in either, a
                // `None` value is returned instead of an error
                Some(
                    self.request
                        .inputs
                        .hint(name)
                        .or_else(|| object.get(name))
                        .cloned()
                        .unwrap_or_else(|| NoneValue::untyped().into()),
                )
            }
        }

        /// Helper for evaluating the given expression.
        ///
        /// Returns a diagnostic that will be converted to any `anyhow::Error`
        /// by the caller.
        async fn eval(context: Context<'_>, expr: &Expr<SyntaxNode>) -> Result<bool, Diagnostic> {
            let mut evaluator = ExprEvaluator::new(context);
            let value = evaluator.evaluate_expr(expr).await?;
            match value.as_boolean() {
                Some(res) => Ok(res),
                None => Err(Diagnostic::error(format!(
                    "conditional expression is expected to be type `Boolean`, but found type \
                     `{ty}`",
                    ty = value.ty()
                ))
                .with_highlight(expr.span())),
            }
        }

        let expr = Expr::cast(self.expr.clone().into()).expect("should be an expression node");
        match eval(
            Context {
                request,
                transferer,
            },
            &expr,
        )
        .await
        {
            Ok(res) => Ok(res),
            Err(diagnostic) => {
                let file: SimpleFile<_, _> = SimpleFile::new("<condition>", &self.raw);
                let mut buffer = Buffer::no_color();
                term::emit_to_write_style(
                    &mut buffer,
                    &Default::default(),
                    &file,
                    &diagnostic.to_codespan(()),
                )
                .context("failed to write diagnostic to buffer")?;
                let diagnostic = String::from_utf8(buffer.into_inner())
                    .context("diagnostic buffer contents are not UTF-8")?;
                bail!(
                    "failed to evaluate backend dynamic arguments condition: {diagnostic}",
                    diagnostic = diagnostic
                        .strip_prefix("error: ")
                        .unwrap_or(&diagnostic)
                        .trim()
                );
            }
        }
    }
}

impl ToToml for Condition {
    fn to_toml<'a>(&'a self, arena: &'a Arena) -> Result<Item<'a>, ToTomlError> {
        self.raw.to_toml(arena)
    }
}

impl<'de> FromToml<'de> for Condition {
    fn from_toml(ctx: &mut toml_spanner::Context<'de>, item: &Item<'de>) -> Result<Self, Failed> {
        /// Used to remap an unescaped string index to an index in the
        /// corresponding escaped TOML string.
        fn remap(mapping: &[(usize, usize)], unescaped: usize) -> usize {
            match mapping.binary_search_by_key(&unescaped, |x| x.0) {
                Ok(i) => {
                    // Found in the map, use the escaped index
                    mapping[i].1
                }
                Err(i) => {
                    // Not in the map, need to potentially offset the unescaped position
                    // based on a preceding map entry
                    unescaped
                        + if i == 0 {
                            // No need to offset as this position comes before any
                            // escape sequences
                            0
                        } else {
                            // Offset by the last delta
                            mapping[i - 1].1 - mapping[i - 1].0
                        }
                }
            }
        }

        /// Helper for pushing diagnostics as `toml_spanner::Error` into the
        /// TOML parsing context.
        fn push_errors(
            ctx: &mut toml_spanner::Context<'_>,
            item: &Item<'_>,
            diagnostics: Vec<Diagnostic>,
        ) -> Failed {
            for diagnostic in diagnostics {
                let span = if let Some(label) = diagnostic.labels().next() {
                    let label_span = label.span();
                    let span = item.span();

                    let source = &ctx.source()[span.start as usize..span.end as usize];
                    let offset = if source.starts_with(r#"""""#) | source.starts_with("'''") {
                        3
                    } else {
                        1
                    };

                    let mapping = escape_mapping(
                        source
                            .get(offset..(source.len() - offset))
                            .expect("invalid TOML string"),
                    );

                    // Remap the start and end of the label
                    let label_start = remap(&mapping, label_span.start());
                    let label_end = remap(&mapping, label_span.end());

                    toml_spanner::Span::new(
                        span.start + offset as u32 + label_start as u32,
                        span.start + offset as u32 + label_end as u32,
                    )
                } else {
                    item.span()
                };

                ctx.errors
                    .push(toml_spanner::Error::custom(diagnostic.message(), span));
            }

            Failed
        }

        Self::new(String::from_toml(ctx, item)?).map_err(|diags| push_errors(ctx, item, diags))
    }
}

/// Represents a set of conditional arguments for the LSF and Slurm backends.
///
/// Conditional arguments are passed to the program responsible for queuing a
/// task when the associated conditional expression evaluates to `true`.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct ConditionalArgs {
    /// The condition for including the arguments.
    pub condition: Condition,
    /// The arguments to use when the condition evaluates to `true`.
    #[toml(default)]
    pub args: Vec<String>,
}

impl ConditionalArgs {
    /// Validates the conditional arguments.
    ///
    /// This ensures that the conditional expression is valid WDL and the
    /// specified arguments are not empty.
    pub fn validate(&self) -> Result<()> {
        if self.args.is_empty() {
            bail!("backend conditional arguments must have at least one argument specified");
        }

        Ok(())
    }
}

/// Represents additional arguments to the Slurm and LSF backends.
///
/// These arguments are passed to the executable responsible for queuing a task.
#[derive(Debug, Clone, Default, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct AdditionalArgs {
    /// The additional arguments to pass to the backend program.
    #[toml(default)]
    pub args: Vec<String>,
    /// The conditional arguments to pass to the backend program.
    ///
    /// The first conditional argument with an associated conditional expression
    /// that evaluates to `true` will be passed to the backend program.
    #[toml(default)]
    pub conditional: Vec<ConditionalArgs>,
}

impl AdditionalArgs {
    /// Validates the additional arguments.
    pub fn validate(&self) -> Result<()> {
        for arg in &self.conditional {
            arg.validate()?;
        }

        Ok(())
    }
}

/// Helper functions for `ByteSize` TOML serialization.
mod byte_size {
    use bytesize::ByteSize;
    use toml_spanner::Context;
    use toml_spanner::Failed;
    use toml_spanner::Item;

    /// Helper function for serializing `ByteSize` to TOML.
    pub fn from_toml<'de>(ctx: &mut Context<'de>, item: &Item<'de>) -> Result<ByteSize, Failed> {
        if let Some(s) = item.as_u64() {
            return Ok(ByteSize(s));
        }

        if let Some(s) = item.as_str() {
            return s
                .parse()
                .map_err(|e| ctx.report_custom_error(format!("invalid byte size: {e}"), item));
        }

        Err(ctx.report_expected_but_found(&"integer or string", item))
    }
}

/// Configuration for an LSF queue.
///
/// Each queue can optionally have per-task CPU and memory limits set so that
/// tasks which are too large to be scheduled on that queue will fail
/// immediately instead of pending indefinitely. In the future, these limits may
/// be populated or validated by live information from the cluster, but
/// for now they must be manually based on the user's understanding of the
/// cluster configuration.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct LsfQueueConfig {
    /// The name of the queue; this is the string passed to `bsub -q
    /// <queue_name>`.
    pub name: String,
    /// The maximum number of CPUs this queue can provision for a single task.
    pub max_cpu_per_task: Option<u64>,
    /// The maximum memory this queue can provision for a single task.
    #[toml(FromToml with = byte_size, ToToml with = display)]
    pub max_memory_per_task: Option<ByteSize>,
}

impl LsfQueueConfig {
    /// Validate that this LSF queue exists according to the local `bqueues`.
    pub async fn validate(&self, name: &str) -> Result<(), anyhow::Error> {
        let queue = &self.name;
        ensure!(!queue.is_empty(), "{name}_lsf_queue name cannot be empty");
        if let Some(max_cpu_per_task) = self.max_cpu_per_task {
            ensure!(
                max_cpu_per_task > 0,
                "{name}_lsf_queue `{queue}` must allow at least 1 CPU to be provisioned"
            );
        }
        if let Some(max_memory_per_task) = self.max_memory_per_task {
            ensure!(
                max_memory_per_task.as_u64() > 0,
                "{name}_lsf_queue `{queue}` must allow at least some memory to be provisioned"
            );
        }
        match tokio::time::timeout(
            // 10 seconds is rather arbitrary; `bqueues` ordinarily returns extremely quickly, but
            // we don't want things to run away on a misconfigured system
            std::time::Duration::from_secs(10),
            Command::new("bqueues").arg(queue).output(),
        )
        .await
        {
            Ok(output) => {
                let output = output.context("validating LSF queue")?;
                if !output.status.success() {
                    let stdout = String::from_utf8_lossy(&output.stdout);
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    error!(%stdout, %stderr, %queue, "failed to validate {name}_lsf_queue");
                    Err(anyhow!("failed to validate {name}_lsf_queue `{queue}`"))
                } else {
                    Ok(())
                }
            }
            Err(_) => Err(anyhow!(
                "timed out trying to validate {name}_lsf_queue `{queue}`"
            )),
        }
    }
}

/// Configuration for the LSF + Apptainer backend.
// TODO ACF 2025-09-23: add a Apptainer/Singularity mode config that switches around executable
// name, env var names, etc.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct LsfApptainerBackendConfig {
    /// The task monitor polling interval, in seconds.
    ///
    /// Defaults to 30 seconds.
    pub interval: Option<u64>,
    /// The maximum number of concurrent LSF operations the backend will
    /// perform.
    ///
    /// This controls the maximum concurrent number of `bsub` processes the
    /// backend will spawn to queue tasks.
    ///
    /// Defaults to 10 concurrent operations.
    pub max_concurrency: Option<u32>,
    /// Which queue, if any, to specify when submitting normal jobs to LSF.
    ///
    /// This may be superseded by
    /// [`short_task_lsf_queue`][Self::short_task_lsf_queue],
    /// [`gpu_lsf_queue`][Self::gpu_lsf_queue], or
    /// [`fpga_lsf_queue`][Self::fpga_lsf_queue] for corresponding tasks.
    #[toml(style = Header)]
    pub default_lsf_queue: Option<LsfQueueConfig>,
    /// Which queue, if any, to specify when submitting [short
    /// tasks](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#short_task) to LSF.
    ///
    /// This may be superseded by [`gpu_lsf_queue`][Self::gpu_lsf_queue] or
    /// [`fpga_lsf_queue`][Self::fpga_lsf_queue] for tasks which require
    /// specialized hardware.
    #[toml(style = Header)]
    pub short_task_lsf_queue: Option<LsfQueueConfig>,
    /// Which queue, if any, to specify when submitting [tasks which require a
    /// GPU](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#hardware-accelerators-gpu-and--fpga)
    /// to LSF.
    #[toml(style = Header)]
    pub gpu_lsf_queue: Option<LsfQueueConfig>,
    /// Which queue, if any, to specify when submitting [tasks which require an
    /// FPGA](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#hardware-accelerators-gpu-and--fpga)
    /// to LSF.
    #[toml(style = Header)]
    pub fpga_lsf_queue: Option<LsfQueueConfig>,
    /// Prefix to add to every LSF job name before the task identifier. This is
    /// truncated as needed to satisfy the byte-oriented LSF job name limit.
    pub job_name_prefix: Option<String>,
    /// The additional arguments to `bsub` used to queue a new task.
    #[toml(default)]
    pub bsub: AdditionalArgs,
    /// The configuration of Apptainer, which is used as the container runtime
    /// on the compute nodes where LSF dispatches tasks.
    ///
    /// Note that this will likely be replaced by an abstraction over multiple
    /// container execution runtimes in the future, rather than being
    /// hardcoded to Apptainer.
    #[toml(default)]
    pub apptainer: ApptainerConfig,
}

impl LsfApptainerBackendConfig {
    /// Validate that the backend is appropriately configured.
    pub async fn validate(&self) -> Result<(), anyhow::Error> {
        if cfg!(not(unix)) {
            bail!("LSF + Apptainer backend is not supported on non-unix platforms");
        }

        // Do what we can to validate options that are dependent on the dynamic
        // environment. These are a bit fraught, particularly if the behavior of
        // the external tools changes based on where a job gets dispatched, but
        // querying from the perspective of the current node allows
        // us to get better error messages in circumstances typical to a cluster.
        if let Some(queue) = &self.default_lsf_queue {
            queue.validate("default").await?;
        }

        if let Some(queue) = &self.short_task_lsf_queue {
            queue.validate("short_task").await?;
        }

        if let Some(queue) = &self.gpu_lsf_queue {
            queue.validate("gpu").await?;
        }

        if let Some(queue) = &self.fpga_lsf_queue {
            queue.validate("fpga").await?;
        }

        if let Some(prefix) = &self.job_name_prefix
            && prefix.len() > MAX_LSF_JOB_NAME_PREFIX
        {
            bail!(
                "LSF job name prefix `{prefix}` exceeds the maximum {MAX_LSF_JOB_NAME_PREFIX} \
                 bytes"
            );
        }

        // Validate the additional arguments
        self.bsub.validate()?;

        // Validate the apptainer configuration
        self.apptainer.validate().await?;

        Ok(())
    }

    /// Get the appropriate LSF queue for a task under this configuration.
    ///
    /// Specialized hardware requirements are prioritized over other
    /// characteristics, with FPGA taking precedence over GPU.
    pub(crate) fn lsf_queue_for_task(
        &self,
        requirements: &Object,
        hints: &Object,
    ) -> Option<&LsfQueueConfig> {
        // Specialized hardware gets priority.
        if let Some(queue) = self.fpga_lsf_queue.as_ref()
            && let Some(true) = requirements
                .get(wdl_ast::v1::TASK_REQUIREMENT_FPGA)
                .and_then(Value::as_boolean)
        {
            return Some(queue);
        }

        if let Some(queue) = self.gpu_lsf_queue.as_ref()
            && let Some(true) = requirements
                .get(wdl_ast::v1::TASK_REQUIREMENT_GPU)
                .and_then(Value::as_boolean)
        {
            return Some(queue);
        }

        // Then short tasks.
        if let Some(queue) = self.short_task_lsf_queue.as_ref()
            && let Some(true) = hints
                .get(wdl_ast::v1::TASK_HINT_SHORT_TASK)
                .and_then(Value::as_boolean)
        {
            return Some(queue);
        }

        // Finally the default queue. If this is `None`, `bsub` gets run without a queue
        // argument and the cluster's default is used.
        self.default_lsf_queue.as_ref()
    }
}

/// Configuration for a Slurm partition.
///
/// Each partition can optionally have per-task CPU and memory limits set so
/// that tasks which are too large to be scheduled on that partition will fail
/// immediately instead of pending indefinitely. In the future, these limits may
/// be populated or validated by live information from the cluster, but
/// for now they must be manually based on the user's understanding of the
/// cluster configuration.
#[derive(Debug, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct SlurmPartitionConfig {
    /// The name of the partition; this is the string passed to `sbatch
    /// --partition=<partition_name>`.
    pub name: String,
    /// The maximum number of CPUs this partition can provision for a single
    /// task.
    pub max_cpu_per_task: Option<u64>,
    /// The maximum memory this partition can provision for a single task.
    #[toml(FromToml with = byte_size, ToToml with = display)]
    pub max_memory_per_task: Option<ByteSize>,
}

impl SlurmPartitionConfig {
    /// Validate that this Slurm partition exists according to the local
    /// `sinfo`.
    pub async fn validate(&self, name: &str) -> Result<(), anyhow::Error> {
        let partition = &self.name;
        ensure!(
            !partition.is_empty(),
            "{name}_slurm_partition name cannot be empty"
        );
        if let Some(max_cpu_per_task) = self.max_cpu_per_task {
            ensure!(
                max_cpu_per_task > 0,
                "{name}_slurm_partition `{partition}` must allow at least 1 CPU to be provisioned"
            );
        }
        if let Some(max_memory_per_task) = self.max_memory_per_task {
            ensure!(
                max_memory_per_task.as_u64() > 0,
                "{name}_slurm_partition `{partition}` must allow at least some memory to be \
                 provisioned"
            );
        }
        match tokio::time::timeout(
            // 10 seconds is rather arbitrary; `scontrol` ordinarily returns extremely quickly, but
            // we don't want things to run away on a misconfigured system
            std::time::Duration::from_secs(10),
            Command::new("scontrol")
                .arg("show")
                .arg("partition")
                .arg(partition)
                .output(),
        )
        .await
        {
            Ok(output) => {
                let output = output.context("validating Slurm partition")?;
                if !output.status.success() {
                    let stdout = String::from_utf8_lossy(&output.stdout);
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    error!(%stdout, %stderr, %partition, "failed to validate {name}_slurm_partition");
                    Err(anyhow!(
                        "failed to validate {name}_slurm_partition `{partition}`"
                    ))
                } else {
                    Ok(())
                }
            }
            Err(_) => Err(anyhow!(
                "timed out trying to validate {name}_slurm_partition `{partition}`"
            )),
        }
    }
}

/// Configuration for the Slurm + Apptainer backend.
// TODO ACF 2025-09-23: add a Apptainer/Singularity mode config that switches around executable
// name, env var names, etc.
#[derive(Debug, Default, Clone, PartialEq, Eq, Toml)]
#[toml(Toml, rename_all = "snake_case", deny_unknown_fields)]
pub struct SlurmApptainerBackendConfig {
    /// The task monitor polling interval, in seconds.
    ///
    /// Defaults to 30 seconds.
    pub interval: Option<u64>,
    /// The maximum number of concurrent Slurm operations the backend will
    /// perform.
    ///
    /// This controls the maximum concurrent number of `sbatch` processes the
    /// backend will spawn to queue tasks.
    ///
    /// Defaults to 10 concurrent operations.
    pub max_concurrency: Option<u32>,
    /// Which partition, if any, to specify when submitting normal jobs to
    /// Slurm.
    ///
    /// This may be superseded by
    /// [`short_task_slurm_partition`][Self::short_task_slurm_partition],
    /// [`gpu_slurm_partition`][Self::gpu_slurm_partition], or
    /// [`fpga_slurm_partition`][Self::fpga_slurm_partition] for corresponding
    /// tasks.
    #[toml(style = Header)]
    pub default_slurm_partition: Option<SlurmPartitionConfig>,
    /// Which partition, if any, to specify when submitting [short
    /// tasks](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#short_task) to Slurm.
    ///
    /// This may be superseded by
    /// [`gpu_slurm_partition`][Self::gpu_slurm_partition] or
    /// [`fpga_slurm_partition`][Self::fpga_slurm_partition] for tasks which
    /// require specialized hardware.
    #[toml(style = Header)]
    pub short_task_slurm_partition: Option<SlurmPartitionConfig>,
    /// Which partition, if any, to specify when submitting [tasks which require
    /// a GPU](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#hardware-accelerators-gpu-and--fpga)
    /// to Slurm.
    #[toml(style = Header)]
    pub gpu_slurm_partition: Option<SlurmPartitionConfig>,
    /// Which partition, if any, to specify when submitting [tasks which require
    /// an FPGA](https://github.com/openwdl/wdl/blob/wdl-1.2/SPEC.md#hardware-accelerators-gpu-and--fpga)
    /// to Slurm.
    #[toml(style = Header)]
    pub fpga_slurm_partition: Option<SlurmPartitionConfig>,
    /// The additional arguments to `sbatch` used to queue a new task.
    #[toml(default)]
    pub sbatch: AdditionalArgs,
    /// Prefix to add to every Slurm job name before the task identifier.
    pub job_name_prefix: Option<String>,
    /// The configuration of Apptainer, which is used as the container runtime
    /// on the compute nodes where Slurm dispatches tasks.
    ///
    /// Note that this will likely be replaced by an abstraction over multiple
    /// container execution runtimes in the future, rather than being
    /// hardcoded to Apptainer.
    #[toml(default)]
    pub apptainer: ApptainerConfig,
}

impl SlurmApptainerBackendConfig {
    /// Validate that the backend is appropriately configured.
    pub async fn validate(&self) -> Result<(), anyhow::Error> {
        if cfg!(not(unix)) {
            bail!("Slurm + Apptainer backend is not supported on non-unix platforms");
        }

        // Do what we can to validate options that are dependent on the dynamic
        // environment. These are a bit fraught, particularly if the behavior of
        // the external tools changes based on where a job gets dispatched, but
        // querying from the perspective of the current node allows
        // us to get better error messages in circumstances typical to a cluster.
        if let Some(partition) = &self.default_slurm_partition {
            partition.validate("default").await?;
        }
        if let Some(partition) = &self.short_task_slurm_partition {
            partition.validate("short_task").await?;
        }
        if let Some(partition) = &self.gpu_slurm_partition {
            partition.validate("gpu").await?;
        }
        if let Some(partition) = &self.fpga_slurm_partition {
            partition.validate("fpga").await?;
        }

        // Validate the additional arguments
        self.sbatch.validate()?;

        // Validate the apptainer configuration
        self.apptainer.validate().await?;

        Ok(())
    }

    /// Get the appropriate Slurm partition for a task under this configuration.
    ///
    /// Specialized hardware requirements are prioritized over other
    /// characteristics, with FPGA taking precedence over GPU.
    pub(crate) fn slurm_partition_for_task(
        &self,
        requirements: &Object,
        hints: &Object,
    ) -> Option<&SlurmPartitionConfig> {
        // TODO ACF 2025-09-26: what's the relationship between this code and
        // `TaskExecutionConstraints`? Should this be there instead, or be pulling
        // values from that instead of directly from `requirements` and `hints`?

        // Specialized hardware gets priority.
        if let Some(partition) = self.fpga_slurm_partition.as_ref()
            && let Some(true) = requirements
                .get(wdl_ast::v1::TASK_REQUIREMENT_FPGA)
                .and_then(Value::as_boolean)
        {
            return Some(partition);
        }

        if let Some(partition) = self.gpu_slurm_partition.as_ref()
            && let Some(true) = requirements
                .get(wdl_ast::v1::TASK_REQUIREMENT_GPU)
                .and_then(Value::as_boolean)
        {
            return Some(partition);
        }

        // Then short tasks.
        if let Some(partition) = self.short_task_slurm_partition.as_ref()
            && let Some(true) = hints
                .get(wdl_ast::v1::TASK_HINT_SHORT_TASK)
                .and_then(Value::as_boolean)
        {
            return Some(partition);
        }

        // Finally the default partition. If this is `None`, `sbatch` gets run without a
        // partition argument and the cluster's default is used.
        self.default_slurm_partition.as_ref()
    }
}

/// Represents an error encountered during merging TOML configuration.
#[derive(Debug, thiserror::Error)]
pub enum BuilderMergeError {
    /// An error occurred while serializing merged configuration.
    #[error("failed to serialize after merging configuration")]
    Serialize(#[from] ToTomlError),
    /// An error occurred while attempting to parse merged configuration.
    #[error("failed to parse after merging configuration")]
    Parse {
        /// The merged source.
        source: String,
        /// The error that was encountered.
        #[source]
        error: toml_spanner::Error,
    },
    /// An error occurred while deserializing merged configuration.
    #[error("failed to deserialize after merging configuration")]
    Deserialize {
        /// The merged source.
        source: String,
        /// The error that was encountered.
        #[source]
        error: toml_spanner::FromTomlError,
    },
}

/// Helper for displaying certain builder error messages.
struct BuilderErrorDisplay<'a>(&'static str, &'a Option<PathBuf>);

impl fmt::Display for BuilderErrorDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.1 {
            Some(path) => write!(
                f,
                "failed to {op} configuration file `{path}`",
                op = self.0,
                path = path.display()
            ),
            None => write!(
                f,
                "failed to {op} in-memory configuration string",
                op = self.0
            ),
        }
    }
}

/// Represents an error encountered while building a configuration.
#[derive(Debug, thiserror::Error)]
pub enum BuilderError {
    /// Failed to read the provided configuration file.
    #[error("failed to read configuration file `{path}`")]
    Io {
        /// The path to the file.
        path: PathBuf,
        /// The error that was encountered.
        #[source]
        error: std::io::Error,
    },
    /// Failed to parse the provided configuration file.
    #[error("{}", BuilderErrorDisplay("parse", .path))]
    Parse {
        /// The path to the file.
        ///
        /// This is `None` when the source was a string.
        path: Option<PathBuf>,
        /// The TOML source that was parsed.
        source: String,
        /// The error that was encountered.
        #[source]
        error: toml_spanner::Error,
    },
    /// Failed to deserialize the provided configuration file.
    #[error("{}", BuilderErrorDisplay("deserialize", .path))]
    Deserialize {
        /// The path to the file.
        ///
        /// This is `None` when the source was a string.
        path: Option<PathBuf>,
        /// The TOML source that was parsed.
        source: String,
        /// The error that was encountered.
        #[source]
        error: toml_spanner::FromTomlError,
    },
    /// Failed to merge configuration.
    #[error(transparent)]
    Merge(#[from] BuilderMergeError),
}

impl BuilderError {
    /// Gets the path associated with the error.
    ///
    /// If the error relates to parsing or deserializing an in-memory string,
    /// the path will be `<string>`.
    ///
    /// If the error relates to parsing or deserializing the merged
    /// configuration, the path will be `<merged>`.
    pub fn path(&self) -> impl fmt::Display + Clone {
        #[derive(Clone)]
        struct Helper<'a>(&'a BuilderError);

        impl fmt::Display for Helper<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self.0 {
                    BuilderError::Io { path, .. }
                    | BuilderError::Parse {
                        path: Some(path), ..
                    }
                    | BuilderError::Deserialize {
                        path: Some(path), ..
                    } => path.display().fmt(f),
                    BuilderError::Parse { path: None, .. }
                    | BuilderError::Deserialize { path: None, .. } => write!(f, "<string>"),
                    BuilderError::Merge(_) => write!(f, "<merged>"),
                }
            }
        }

        Helper(self)
    }

    /// Gets the TOML error associated with the error.
    ///
    /// Returns `None` if the error is not associated with parsing or
    /// deserializing TOML.
    pub fn toml_error(&self) -> Option<&toml_spanner::Error> {
        match &self {
            Self::Parse { error, .. } | Self::Merge(BuilderMergeError::Parse { error, .. }) => {
                Some(error)
            }
            Self::Deserialize { error, .. }
            | Self::Merge(BuilderMergeError::Deserialize { error, .. }) => error.errors.first(),
            _ => None,
        }
    }

    /// Gets the TOML source code associated with the error.
    ///
    /// Returns `None` if the error is not associated with parsing or
    /// deserializing TOML.
    pub fn source(&self) -> Option<&str> {
        match &self {
            Self::Parse { source, .. }
            | Self::Deserialize { source, .. }
            | Self::Merge(BuilderMergeError::Parse { source, .. })
            | Self::Merge(BuilderMergeError::Deserialize { source, .. }) => Some(source),
            _ => None,
        }
    }

    /// Converts the error into a [`Diagnostic`].
    pub fn to_diagnostic(&self) -> Diagnostic {
        let mut diagnostic = Diagnostic::error(self.to_string());

        if let Some(e) = self.toml_error() {
            for (span, text) in [e.primary_label(), e.secondary_label()]
                .into_iter()
                .flatten()
            {
                let span = Span::new(span.start as usize, (span.end - span.start) as usize);
                let text: &str = text.trim();

                // For some reason label text isn't returned for certain errors
                let text = if text.is_empty() {
                    match e.kind() {
                        ErrorKind::UnexpectedEof => "unexpected end of file",
                        ErrorKind::RedefineAsArray { .. } => {
                            "a previously defined table was redefined as an array"
                        }
                        ErrorKind::FileTooLarge => "file is too large",
                        ErrorKind::Custom(message) => message,
                        _ => text,
                    }
                } else {
                    text
                };

                if text.is_empty() {
                    diagnostic = diagnostic.with_highlight(span);
                } else {
                    diagnostic = diagnostic.with_label(text, span);
                }
            }
        }

        if matches!(self, Self::Merge(_)) {
            diagnostic = diagnostic.with_help("reported line numbers reflect merged TOML source");
        }

        diagnostic
    }
}

/// Represents a possible source of configuration.
#[derive(Debug)]
enum Source {
    /// A configuration exists as a file on disk.
    Path(PathBuf),
    /// The configuration exists as a TOML string.
    String(String),
}

/// Implements a configuration builder.
///
/// The builder supports merging multiple TOML configuration files together.
///
/// Merging works by the following:
///
/// * Matching table keys that are arrays get appended.
/// * Matching table keys that are tables are recursively merged.
/// * Otherwise, the value associated with the key is replaced by the
///   configuration being merged in.
///
/// The configuration builder is generic so that it can build any type that
/// supports TOML serialization.
#[derive(Default, Debug)]
pub struct ConfigBuilder<T> {
    /// The sources for building the configuration.
    sources: Vec<Source>,
    /// Phantom data to store the type parameter.
    _phantom: PhantomData<T>,
}

impl<T> ConfigBuilder<T> {
    /// Adds a TOML string source to the builder.
    pub fn with_string_source(mut self, toml: impl Into<String>) -> Self {
        self.sources.push(Source::String(toml.into()));
        self
    }

    /// Adds a TOML configuration file source to the builder.
    pub fn with_file_source(mut self, path: impl Into<PathBuf>) -> Self {
        self.sources.push(Source::Path(path.into()));
        self
    }

    /// Attempts to build the configuration.
    ///
    /// Each configuration file is merged with the previous one in the order
    /// they were added to the builder.
    pub fn try_build(self) -> Result<T, BuilderError>
    where
        T: ToToml + for<'de> FromToml<'de>,
    {
        // Read all of the files up front so that the source outlives the arena
        let sources: Vec<(Option<PathBuf>, String)> = self
            .sources
            .into_iter()
            .map(|s| match s {
                Source::Path(path) => {
                    let source = fs::read_to_string(&path).map_err(|e| BuilderError::Io {
                        path: path.clone(),
                        error: e,
                    })?;

                    Ok((Some(path), source))
                }
                Source::String(source) => Ok((None, source)),
            })
            .collect::<Result<_, BuilderError>>()?;

        // Parse all of the documents
        let arena = Arena::new();
        let documents = sources
            .iter()
            .map(|(path, source)| {
                toml_spanner::parse(source, &arena).map_err(|e| BuilderError::Parse {
                    path: path.clone(),
                    source: source.clone(),
                    error: e,
                })
            })
            .collect::<Result<Vec<_>, _>>()?;

        // Merge the documents as TOML tables
        let mut merged_table: Table<'_> = Table::new();
        for (index, mut document) in documents.into_iter().enumerate() {
            // Start by deserializing the document to ensure it is a valid standalone
            // configuration
            document.to::<T>().map_err(|e| {
                let (path, source) = &sources[index];
                BuilderError::Deserialize {
                    path: path.clone(),
                    source: source.clone(),
                    error: e,
                }
            })?;

            // Merge the tables
            Self::merge_tables(document.into_table(), &mut merged_table, &arena);
        }

        // Unfortunately, there's no way to go from `Table` to `T`, so we must
        // round-trip through a string; serialize the merged table back to a
        // string first
        let source =
            toml_spanner::to_string(&merged_table).map_err(BuilderMergeError::Serialize)?;

        // Deserialize the merged contents back to the underlying config type
        Ok(toml_spanner::parse(&source, &arena)
            .map_err(|e| BuilderMergeError::Parse {
                source: source.clone(),
                error: e,
            })?
            .to()
            .map_err(|e| BuilderMergeError::Deserialize {
                source: source.clone(),
                error: e,
            })?)
    }

    /// Merges the `src` table with the `dest` table.
    ///
    /// If a key in `src` exists in `dest` and both items are tables, the tables
    /// are recursively merged together.
    ///
    /// If a key in `src` exists in `dest` and both items are arrays, the array
    /// in `dest` is extended with the elements of the array in `src`.
    ///
    /// Otherwise, the item in the `src` table replaces the item in the `dest`
    /// table.
    fn merge_tables<'de>(src: Table<'de>, dest: &mut Table<'de>, arena: &'de Arena) {
        for (key, src_item) in src {
            let Some(dest_item) = dest.get_mut(key.name) else {
                dest.insert(key, src_item, arena);
                continue;
            };

            // Merge arrays by appending
            if let Some(src_array) = src_item.as_array()
                && let Some(dest_array) = dest_item.as_array_mut()
            {
                for element in src_array {
                    dest_array.push(element.clone_in(arena), arena);
                }
                continue;
            }

            // Merge tables by recursing
            if let Some(_) = src_item.as_table()
                && let Some(dest_table) = dest_item.as_table_mut()
            {
                Self::merge_tables(src_item.into_table().unwrap(), dest_table, arena);
                continue;
            }

            // Overwrite the item
            dest.insert(key, src_item, arena);
        }
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;
    use std::io::Write;

    use codespan_reporting::files::SimpleFile;
    use codespan_reporting::term::DisplayStyle;
    use codespan_reporting::term::emit_into_string;
    use codespan_reporting::term::{self};
    use futures::future::BoxFuture;
    use pretty_assertions::assert_eq;
    use tempfile::TempPath;
    use tempfile::tempdir;

    use super::*;
    use crate::ONE_GIBIBYTE;
    use crate::TaskInputs;
    use crate::backend::TaskExecutionConstraints;
    use crate::http::Location;
    use crate::v1::DEFAULT_TASK_REQUIREMENT_CPU;
    use crate::v1::DEFAULT_TASK_REQUIREMENT_DISKS;
    use crate::v1::DEFAULT_TASK_REQUIREMENT_MEMORY;

    #[test]
    fn redacted_secret() {
        let mut map: HashMap<_, SecretString> = HashMap::new();
        map.insert(
            "foo",
            SecretString {
                inner: "secret".into(),
                redacted: false,
            },
        );

        assert_eq!(
            toml_spanner::to_string(&map).unwrap().trim(),
            format!(r#"foo = "secret""#)
        );

        map.insert(
            "foo",
            SecretString {
                inner: "secret".into(),
                redacted: true,
            },
        );
        assert_eq!(
            toml_spanner::to_string(&map).unwrap().trim(),
            format!(r#"foo = "{REDACTED}""#)
        );
    }

    #[test]
    fn redacted_config() {
        let config = Config {
            backends: [
                (
                    "first".to_string(),
                    TesBackendConfig {
                        auth: Some(TesBackendAuthConfig::Basic {
                            config: BasicAuthConfig {
                                username: "foo".into(),
                                password: "secret".into(),
                            },
                        }),
                        ..Default::default()
                    }
                    .into(),
                ),
                (
                    "second".to_string(),
                    TesBackendConfig {
                        auth: Some(
                            BearerAuthConfig {
                                token: "secret".into(),
                            }
                            .into(),
                        ),
                        ..Default::default()
                    }
                    .into(),
                ),
            ]
            .into(),
            storage: StorageConfig {
                azure: AzureStorageConfig {
                    auth: Some(AzureStorageAuthConfig {
                        account_name: "foo".into(),
                        access_key: "secret".into(),
                    }),
                },
                s3: S3StorageConfig {
                    auth: Some(S3StorageAuthConfig {
                        access_key_id: "foo".into(),
                        secret_access_key: "secret".into(),
                    }),
                    ..Default::default()
                },
                google: GoogleStorageConfig {
                    auth: Some(GoogleStorageAuthConfig {
                        access_key: "foo".into(),
                        secret: "secret".into(),
                    }),
                },
            },
            ..Default::default()
        };

        let toml = toml_spanner::to_string(&config).unwrap();
        assert!(toml.contains("secret"), "`{toml}` contains a secret");
    }

    #[tokio::test]
    async fn test_config_validate() {
        // Test invalid task config
        let mut config = Config::default();
        config.task.retries = 255.into();
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "configuration value `task.retries` cannot exceed 100"
        );

        // Test invalid scatter concurrency config
        let mut config = Config::default();
        config.workflow.scatter.concurrency = 0;
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "configuration value `workflow.scatter.concurrency` cannot be zero"
        );

        // Test invalid backend name
        let config = Config {
            backend: "foo".into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "a backend named `foo` is not present in the configuration"
        );
        let config = Config {
            backend: "bar".into(),
            backends: [("foo".to_string(), BackendConfig::default())].into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "a backend named `bar` is not present in the configuration"
        );

        // Test a singular backend
        let config = Config {
            backend: "foo".to_string(),
            backends: [("foo".to_string(), BackendConfig::default())].into(),
            ..Default::default()
        };
        config.validate().await.expect("config should validate");

        // Test invalid local backend cpu config
        let config = Config {
            backends: [(
                "default".to_string(),
                LocalBackendConfig {
                    cpu: Some(0),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "local backend configuration value `cpu` cannot be zero"
        );
        let config = Config {
            backends: [(
                "default".to_string(),
                LocalBackendConfig {
                    cpu: Some(10000000),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert!(
            config
                .validate()
                .await
                .unwrap_err()
                .to_string()
                .starts_with(
                    "local backend configuration value `cpu` cannot exceed the virtual CPUs \
                     available to the host"
                )
        );

        // Test invalid local backend memory config
        let config = Config {
            backends: [(
                "default".to_string(),
                LocalBackendConfig {
                    memory: Some("0 GiB".to_string()),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "local backend configuration value `memory` cannot be zero"
        );
        let config = Config {
            backends: [(
                "default".to_string(),
                LocalBackendConfig {
                    memory: Some("100 meows".to_string()),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "local backend configuration value `memory` has invalid value `100 meows`"
        );

        let config = Config {
            backends: [(
                "default".to_string(),
                LocalBackendConfig {
                    memory: Some("1000 TiB".to_string()),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert!(
            config
                .validate()
                .await
                .unwrap_err()
                .to_string()
                .starts_with(
                    "local backend configuration value `memory` cannot exceed the total memory of \
                     the host"
                )
        );

        // Test missing TES URL
        let config = Config {
            backends: [("default".to_string(), TesBackendConfig::default().into())].into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "TES backend configuration value `url` is required"
        );

        // Test TES invalid max concurrency
        let config = Config {
            backends: [(
                "default".to_string(),
                TesBackendConfig {
                    url: Some("https://example.com".parse().unwrap()),
                    max_concurrency: Some(0),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "TES backend configuration value `max_concurrency` cannot be zero"
        );

        // Insecure TES URL
        let config = Config {
            backends: [(
                "default".to_string(),
                TesBackendConfig {
                    url: Some("http://example.com".parse().unwrap()),
                    inputs: Some("http://example.com".parse().unwrap()),
                    outputs: Some("http://example.com".parse().unwrap()),
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "TES backend configuration value `url` has invalid value `http://example.com/`: URL \
             must use a HTTPS scheme"
        );

        // Allow insecure URL
        let config = Config {
            backends: [(
                "default".to_string(),
                TesBackendConfig {
                    url: Some("http://example.com".parse().unwrap()),
                    inputs: Some("http://example.com".parse().unwrap()),
                    outputs: Some("http://example.com".parse().unwrap()),
                    insecure: true,
                    ..Default::default()
                }
                .into(),
            )]
            .into(),
            ..Default::default()
        };
        config
            .validate()
            .await
            .expect("configuration should validate");

        // invalid Parallelism
        let mut config = Config::default();
        config.http.parallelism = 0.into();
        assert_eq!(
            config.validate().await.unwrap_err().to_string(),
            "configuration value `http.parallelism` cannot be zero"
        );

        // valid Parallelism
        let mut config = Config::default();
        config.http.parallelism = 5.into();
        assert!(
            config.validate().await.is_ok(),
            "should pass for valid configuration"
        );
        let mut config = Config::default();
        config.http.parallelism = Parallelism::default();
        assert!(config.validate().await.is_ok(), "should pass for default");

        // Test invalid LSF job name prefix
        #[cfg(unix)]
        {
            let job_name_prefix = "A".repeat(MAX_LSF_JOB_NAME_PREFIX * 2);
            let mut config = Config {
                experimental_features_enabled: true,
                ..Default::default()
            };
            config.backends.insert(
                "default".to_string(),
                LsfApptainerBackendConfig {
                    job_name_prefix: Some(job_name_prefix.clone()),
                    ..Default::default()
                }
                .into(),
            );
            assert_eq!(
                config.validate().await.unwrap_err().to_string(),
                format!("LSF job name prefix `{job_name_prefix}` exceeds the maximum 100 bytes")
            );
        }
    }

    fn create_temp_file(contents: &str) -> TempPath {
        let mut file: tempfile::NamedTempFile =
            tempfile::NamedTempFile::new().expect("failed to create temporary file");
        file.write_all(contents.as_bytes())
            .expect("failed to write temporary file");
        file.into_temp_path()
    }

    #[test]
    fn it_builds_with_no_sources() {
        let config = Config::builder().try_build().expect("should build");
        assert_eq!(config, Config::default(), "should be equal");
    }

    #[test]
    fn it_builds_with_one_source() {
        let path = create_temp_file("backend = 'foo'");

        let config = Config::builder()
            .with_file_source(&path)
            .try_build()
            .expect("should build");
        assert_eq!(
            config,
            Config {
                backend: "foo".into(),
                ..Default::default()
            },
            "should be equal"
        );
    }

    #[test]
    fn it_errors_on_invalid_parse() {
        let path = create_temp_file("invalid");

        let e = Config::builder()
            .with_file_source(&path)
            .try_build()
            .expect_err("should fail");

        let source = e.source().expect("should have source");

        let diagnostic = e.to_diagnostic();
        let error = emit_into_string(
            &term::Config {
                display_style: DisplayStyle::Rich,
                ..Default::default()
            },
            &SimpleFile::new(e.path(), source),
            &diagnostic.to_codespan(()),
        )
        .expect("should emit");

        assert!(
            error.contains("expected an equals"),
            "the error `{error}` does not contain the expected message"
        );
    }

    #[test]
    fn it_errors_on_invalid_deserialization() {
        let path = create_temp_file("backend = 42");

        let e = Config::builder()
            .with_file_source(&path)
            .try_build()
            .expect_err("should fail");

        let source = e.source().expect("should have source");

        let diagnostic = e.to_diagnostic();
        let error = emit_into_string(
            &term::Config {
                display_style: DisplayStyle::Rich,
                ..Default::default()
            },
            &SimpleFile::new(e.path(), source),
            &diagnostic.to_codespan(()),
        )
        .expect("should emit");

        assert!(
            error.contains("expected a string"),
            "the error `{error}` does not contain the expected message"
        );
    }

    #[test]
    fn it_merges_sources() {
        let first = create_temp_file(
            r#"
backend = 'foo'

[task]
excluded_cache_inputs = ['1', '2', '3']

[backends.foo]
type = 'local'
"#,
        );

        let second = create_temp_file(
            r#"
[task]
excluded_cache_inputs = ['4', '5']

[backends.bar]
type = 'docker'
"#,
        );

        let third: TempPath = create_temp_file(
            r#"
backend = 'baz'

[task]
excluded_cache_inputs = ['6', '7', '8']

[backends.baz]
type = 'tes'
"#,
        );

        let fourth = r#"
backend = 'qux'

[task]
excluded_cache_inputs = ['9', '10']

[backends.qux]
type = 'lsf_apptainer'
"#;

        let config = Config::builder()
            .with_file_source(&first)
            .with_file_source(&second)
            .with_file_source(&third)
            .with_string_source(fourth)
            .try_build()
            .expect("should build");

        assert_eq!(
            config,
            Config {
                backend: "qux".into(),
                task: TaskConfig {
                    excluded_cache_inputs: vec![
                        "1".to_string(),
                        "2".to_string(),
                        "3".to_string(),
                        "4".to_string(),
                        "5".to_string(),
                        "6".to_string(),
                        "7".to_string(),
                        "8".to_string(),
                        "9".to_string(),
                        "10".to_string(),
                    ],
                    ..Default::default()
                },
                backends: IndexMap::from_iter([
                    ("foo".to_string(), LocalBackendConfig::default().into()),
                    ("bar".to_string(), DockerBackendConfig::default().into()),
                    ("baz".to_string(), TesBackendConfig::default().into()),
                    (
                        "qux".to_string(),
                        LsfApptainerBackendConfig::default().into()
                    ),
                ]),
                ..Default::default()
            },
            "should be equal"
        );
    }

    #[test]
    fn parallelism_serialization() {
        let map: HashMap<&str, Parallelism> =
            HashMap::from_iter([("value", Parallelism::Available)]);
        assert_eq!(
            toml_spanner::to_string(&map).unwrap(),
            format!("value = \"available\"\n")
        );

        let map: HashMap<&str, Parallelism> =
            HashMap::from_iter([("value", Parallelism::Use(123))]);
        assert_eq!(toml_spanner::to_string(&map).unwrap(), "value = 123\n");
    }

    #[test]
    fn parallelism_deserialization() {
        let map: HashMap<String, Parallelism> =
            toml_spanner::from_str("value = 'available'").unwrap();
        assert_eq!(map["value"], Parallelism::Available);

        let map: HashMap<String, Parallelism> = toml_spanner::from_str("value = 123").unwrap();
        assert_eq!(map["value"], Parallelism::Use(123));

        let expected_error =
            "expected a positive integer or `available` for parallelism at `value`";

        let error =
            toml_spanner::from_str::<HashMap<String, Parallelism>>("value = 'wrong'").unwrap_err();
        assert_eq!(error.to_string(), expected_error);

        let error =
            toml_spanner::from_str::<HashMap<String, Parallelism>>("value = 0").unwrap_err();
        assert_eq!(error.to_string(), expected_error);

        let error =
            toml_spanner::from_str::<HashMap<String, Parallelism>>("value = -10").unwrap_err();
        assert_eq!(error.to_string(), expected_error);
    }

    #[test]
    fn retries_serialization() {
        let map: HashMap<&str, Retries> = HashMap::from_iter([("value", Retries::Default)]);
        assert_eq!(
            toml_spanner::to_string(&map).unwrap(),
            format!("value = \"default\"\n")
        );

        let map: HashMap<&str, Retries> = HashMap::from_iter([("value", Retries::Use(123))]);
        assert_eq!(toml_spanner::to_string(&map).unwrap(), "value = 123\n");
    }

    #[test]
    fn retries_deserialization() {
        let map: HashMap<String, Retries> = toml_spanner::from_str("value = 'default'").unwrap();
        assert_eq!(map["value"], Retries::Default);

        let map: HashMap<String, Retries> = toml_spanner::from_str("value = 12").unwrap();
        assert_eq!(map["value"], Retries::Use(12));

        let map: HashMap<String, Retries> = toml_spanner::from_str("value = 0").unwrap();
        assert_eq!(map["value"], Retries::Use(0));

        let expected_error = format!(
            "expected an integer less than {MAX_RETRIES} or `default` for retries at `value`"
        );

        let error =
            toml_spanner::from_str::<HashMap<String, Retries>>("value = 'wrong'").unwrap_err();
        assert_eq!(error.to_string(), expected_error);

        let error = toml_spanner::from_str::<HashMap<String, Retries>>("value = 101").unwrap_err();
        assert_eq!(error.to_string(), expected_error);

        let error = toml_spanner::from_str::<HashMap<String, Retries>>("value = -10").unwrap_err();
        assert_eq!(error.to_string(), expected_error);
    }

    #[test]
    fn mapping_escape_indexes() {
        // Check for empty string
        assert!(escape_mapping("").is_empty());

        // Check a string with no escape sequences
        assert!(escape_mapping("hello world!").is_empty());

        // Check for a string containing only an escape sequences (should contain an
        // exclusive-end mapping)
        assert_eq!(escape_mapping(r#"\"\""#), &[(1, 2), (2, 4)]);
        assert_eq!(escape_mapping(r#"\u0022\u0022"#), &[(1, 6), (2, 12)]);
        assert_eq!(
            escape_mapping(r#"\U00000022\U00000022"#),
            &[(1, 10), (2, 20)]
        );

        // Check a complex string
        assert_eq!(
            escape_mapping(r#"\"foo\u0022 == \U00000022bar\" && \"\" == \"\n\""#),
            &[
                (1, 2),   // f
                (5, 11),  // <space>
                (10, 25), // b
                (14, 30), // <space>
                (19, 36), // \"
                (20, 38), // <space>
                (25, 44), // \n
                (26, 46), // \"
                (27, 48)  // <end of string>
            ]
        );
    }

    #[test]
    fn conditional_args_serialization() {
        // Test for invalid type
        let error = toml_spanner::from_str::<ConditionalArgs>("condition = 1").unwrap_err();
        assert_eq!(
            error.to_string(),
            "expected a string, found integer at `condition`"
        );

        // Test for not a single WDL expression
        let error =
            toml_spanner::from_str::<ConditionalArgs>(r#"condition = "foo bar""#).unwrap_err();
        assert_eq!(error.to_string(), "expected a single WDL expression");

        // Test for parse error
        let error =
            toml_spanner::from_str::<ConditionalArgs>(r#"condition = "{ foo: }""#).unwrap_err();
        assert_eq!(error.to_string(), "expected expression, but found `}`");

        // Test for not a `Boolean` expression
        let error = toml_spanner::from_str::<ConditionalArgs>(r#"condition = "1""#).unwrap_err();
        assert_eq!(
            error.to_string(),
            "conditional expression is expected to be type `Boolean`, but found type `Int`"
        );
        let error = toml_spanner::from_str::<ConditionalArgs>(r#"condition = "hint""#).unwrap_err();
        assert_eq!(
            error.to_string(),
            "conditional expression is expected to be type `Boolean`, but found type `Object`"
        );

        // Test for unknown name
        let error = toml_spanner::from_str::<ConditionalArgs>(r#"condition = "foo""#).unwrap_err();
        assert_eq!(error.to_string(), "unknown name `foo`");

        // Test for unknown type name
        let error =
            toml_spanner::from_str::<ConditionalArgs>(r#"condition = "Foo {}""#).unwrap_err();
        assert_eq!(error.to_string(), "unknown type name `Foo`");

        // Test for valid conditions
        let args: ConditionalArgs = toml_spanner::from_str(r#"condition = "true""#).unwrap();
        assert_eq!(args.condition.raw, "true");
        assert_eq!(
            toml_spanner::to_string(&args).unwrap(),
            "condition = \"true\"\nargs = []\n"
        );

        let args: ConditionalArgs =
            toml_spanner::from_str(r#"condition = "cpu == 1 && hint.bar == \"foo\"""#).unwrap();
        assert_eq!(args.condition.raw, r#"cpu == 1 && hint.bar == "foo""#);
        assert_eq!(
            toml_spanner::to_string(&args).unwrap(),
            "condition = 'cpu == 1 && hint.bar == \"foo\"'\nargs = []\n"
        );
    }

    #[test]
    fn validate_conditional_args() {
        // Check for empty args
        let args: ConditionalArgs = toml_spanner::from_str(r#"condition = "true""#).unwrap();
        assert_eq!(
            args.validate().unwrap_err().to_string(),
            "backend conditional arguments must have at least one argument specified"
        );
    }

    #[tokio::test]
    async fn evaluate_conditions() {
        /// Helper to represent the context for `Condition` evaluation.
        struct Context {
            cpu: f64,
            memory: u64,
            gpu: bool,
            fpga: bool,
            disks: i64,
            inputs: TaskInputs,
            hints: Object,
        }

        impl Default for Context {
            fn default() -> Self {
                Self {
                    cpu: DEFAULT_TASK_REQUIREMENT_CPU,
                    memory: DEFAULT_TASK_REQUIREMENT_MEMORY as u64,
                    gpu: false,
                    fpga: false,
                    disks: (DEFAULT_TASK_REQUIREMENT_DISKS * ONE_GIBIBYTE) as i64,
                    inputs: Default::default(),
                    hints: Default::default(),
                }
            }
        }

        struct Transferer;

        impl crate::http::Transferer for Transferer {
            fn download<'a>(&'a self, _: &'a Url) -> BoxFuture<'a, Result<Location>> {
                unimplemented!()
            }

            fn upload<'a>(&'a self, _: &'a Path, _: &'a Url) -> BoxFuture<'a, Result<()>> {
                unimplemented!()
            }

            fn size<'a>(&'a self, _: &'a Url) -> BoxFuture<'a, anyhow::Result<Option<u64>>> {
                unimplemented!()
            }

            fn walk<'a>(&'a self, _: &'a Url) -> BoxFuture<'a, Result<Arc<[String]>>> {
                unimplemented!()
            }

            fn exists<'a>(&'a self, _: &'a Url) -> BoxFuture<'a, Result<bool>> {
                unimplemented!()
            }

            fn digest<'a>(
                &'a self,
                _: &'a Url,
            ) -> BoxFuture<'a, Result<Option<Arc<cloud_copy::ContentDigest>>>> {
                unimplemented!()
            }
        }

        /// Helper for evaluating `Condition` from a WDL expression string.
        ///
        /// The string is expected to be a valid WDL expression.
        async fn eval(context: Context, expression: &str) -> Result<bool> {
            let dir = tempdir().context("failed to create temporary directory")?;
            let condition = Condition::new(expression).expect("invalid expression");
            condition
                .evaluate(
                    &ExecuteTaskRequest {
                        id: "test",
                        command: "",
                        inputs: &context.inputs,
                        backend_inputs: &[],
                        requirements: &Object::empty(),
                        hints: &context.hints,
                        env: &Default::default(),
                        constraints: &TaskExecutionConstraints {
                            container: None,
                            cpu: context.cpu,
                            memory: context.memory,
                            gpu: if context.gpu {
                                vec![String::new()]
                            } else {
                                Default::default()
                            },
                            fpga: if context.fpga {
                                vec![String::new()]
                            } else {
                                Default::default()
                            },
                            disks: IndexMap::from_iter([("".into(), context.disks)]),
                        },
                        base_dir: &EvaluationPath::from_local_path(dir.path().into()),
                        attempt_dir: &dir.path().join("0"),
                        temp_dir: &dir.path().join("tmp"),
                    },
                    &Transferer,
                )
                .await
        }

        // Check for the simple expressions
        assert_eq!(eval(Context::default(), "true").await.unwrap(), true);
        assert_eq!(eval(Context::default(), "false").await.unwrap(), false);
        assert_eq!(eval(Context::default(), "cpu == 1").await.unwrap(), true);
        assert_eq!(
            eval(Context::default(), "memory == 2147483648")
                .await
                .unwrap(),
            true
        );
        assert_eq!(eval(Context::default(), "gpu").await.unwrap(), false);
        assert_eq!(eval(Context::default(), "fpga").await.unwrap(), false);
        assert_eq!(
            eval(Context::default(), "disks == 1073741824")
                .await
                .unwrap(),
            true
        );
        assert_eq!(
            eval(Context::default(), "defined(hint.foo)").await.unwrap(),
            false
        );

        // Check a comprehensive expression
        assert_eq!(
            eval(
                Context {
                    cpu: 10.,
                    memory: 10 * 1024 * 1024,
                    gpu: true,
                    fpga: true,
                    disks: 1024 * 1024,
                    inputs: Default::default(),
                    hints: Object::new(IndexMap::from_iter([(
                        "foo".into(),
                        "hi".to_string().into()
                    )]))
                },
                r#"cpu == 10 && memory == 10*1024*1024 && gpu && fpga && disks == 1024 * 1024 && hint.foo == "hi""#
            )
            .await
            .unwrap(),
            true
        );

        // Check for input hint override
        let mut context = Context {
            hints: Object::new(IndexMap::from_iter([(
                "foo".into(),
                "hi".to_string().into(),
            )])),
            ..Default::default()
        };
        context
            .inputs
            .override_hint("foo", "overridden!".to_string());
        assert_eq!(
            eval(context, r#"hint.foo == "overridden!""#).await.unwrap(),
            true
        );
    }
}