oxdock-core 0.28.0-alpha

Core engine for OxDock's Dockerfile-inspired compile-time DSL, orchestrating workspace snapshots and asset embedding.
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
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

use anyhow::Result;
use oxdock_func_macro::oxdock_func;
use oxdock_parser::{
    Field, KEYWORD_INSPECT, SCRIPT_MODULE_NAME, STD_MODULE_NAME, SemaphoreState, Step, TypeTag,
    Value, base_name, qualify, render_structural, split_qualified,
};
use oxdock_pipe::PipeHandle;
use oxdock_process::{CommandStdin, DefaultProcessManager, ProcessManager};

use super::io::StreamHandle;
use super::state::ExecState;
use super::steps::StepCtx;
use super::typing::TypeDescriptor;

/// Origin of a callable in the unified function registry. `Script` is an
/// interpreted `FUNC` body; `HostCtx` and `HostPure` are compiled Rust
/// functions (the `#[oxdock_func]` host export macro). Builtins and
/// runtime-registered hosts share the host kinds; only `DESCRIBE` output
/// shows the label, and both host kinds keep rendering as `host`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FuncKind {
    Script,
    HostCtx,
    HostPure,
}

impl FuncKind {
    pub fn label(&self) -> &'static str {
        match self {
            FuncKind::Script => "script",
            FuncKind::HostCtx | FuncKind::HostPure => "host",
        }
    }
}

/// One declared parameter of a registered function. `param_type` is `None`
/// for unconstrained `Value` parameters (the macro accepts any value).
/// `allowed` names the closed value set for constrained `STRING`
/// parameters, declared once via `#[values(...)]`: the generated
/// extractor enforces membership and the reference renders the set,
/// so the list cannot rot apart from the check. `docs` carries the
/// parameter's description (parsed from the `# Arguments` doc section
/// for macro functions, written inline for hand-built entries) and
/// renders into every reference surface. `options` declares the known
/// keys of a `MAP` options parameter: same single-source rule as
/// `allowed`, with per-key types, required flags, defaults, and docs.
/// `optional` marks a trailing `MAP` parameter callers may omit: the
/// runtime fills an empty MAP, and signatures render `= {}`. Only
/// trailing `MAP` params may be optional (run-start validation
/// rejects anything else), so omission always means "no options".
/// The macro never emits optional params: only hand-built entries
/// use them, and only for options MAPs.
#[derive(Debug, Clone)]
pub struct FuncParam {
    pub name: String,
    pub param_type: Option<TypeTag>,
    pub allowed: Option<&'static [&'static str]>,
    pub docs: &'static str,
    pub options: Option<&'static [ParamOption]>,
    pub optional: bool,
}

/// Required argument count: total params minus trailing optionals.
/// Callers may pass any count in `required..=total`; omitted trailing
/// MAPs fill with an empty MAP. Shared by the static arity gate and
/// the runtime gate so the two can never disagree.
pub fn required_arity(params: &[FuncParam]) -> usize {
    params
        .iter()
        .rev()
        .take_while(|param| param.optional)
        .fold(params.len(), |count, _| count - 1)
}

/// Fill omitted trailing optional MAP params with empty MAPs. The
/// caller guarantees `values.len()` is within
/// `required_arity(params)..=params.len()`; anything else is a bug,
/// not a user error, so over- and under-counts panic loudly.
pub fn fill_optional_args(params: &[FuncParam], mut values: Vec<Value>) -> Vec<Value> {
    assert!(
        values.len() <= params.len(),
        "arity gate must run before fill_optional_args"
    );
    assert!(
        values.len() >= required_arity(params),
        "arity gate must run before fill_optional_args"
    );
    while values.len() < params.len() {
        values.push(Value::map(BTreeMap::new()));
    }
    values
}

/// One known key of a `MAP` options parameter: name, value type,
/// whether callers must pass it, the rendered default for optional
/// keys, and what it controls. Declared once per options parameter
/// (macro `#[options(...)]` plus the `# Options` doc section, or
/// inline for hand-built entries) and rendered into signatures,
/// `DESCRIBE` output, and generated references alike.
#[derive(Debug, Clone)]
pub struct ParamOption {
    pub name: &'static str,
    pub value: TypeTag,
    pub required: bool,
    pub default: Option<&'static str>,
}

/// Introspectable metadata for one function. Single source for
/// `DESCRIBE(name)` output and the static function reference. `rpn` names
/// whether the function also runs on the compiled math path (`true` for
/// pure functions and opted-in stateful ones); everything runs on the AST
/// path.
#[derive(Debug, Clone)]
pub struct FuncMeta {
    pub name: String,
    /// Owning module (`STD` for builtins, `SCRIPT` for DSL definitions,
    /// the host module name otherwise). `name` is always the qualified
    /// `MODULE::BASE` form, so listings and `DESCRIBE` never lose origin.
    pub module: String,
    pub kind: FuncKind,
    pub params: Option<Vec<FuncParam>>,
    pub returns: Option<TypeTag>,
    pub rpn: bool,
    pub summary: &'static str,
    pub docs: &'static str,
    /// Executable examples, rendered as ```oxdock fences into the
    /// generated reference and executed by docs_conformance like
    /// command examples. The single precondition mechanism: the
    /// returns gate executes these blocks to cover functions blind
    /// sampling cannot reach.
    pub examples: &'static [crate::Example],
}

/// Pure scalar function: no filesystem, no scope, no process access.
/// Usable from both AST evaluation and compiled RPN math.
pub type PureFn = Arc<dyn Fn(Vec<Value>) -> Result<Value> + Send + Sync>;

/// Stateful/IO function with full step context (fs, cwd, envs, vars, pipes).
pub type NativeFn<P> = Arc<dyn Fn(&mut StepCtx<P>, Vec<Value>) -> Result<Value> + Send + Sync>;

/// Export hook for a DSL function, implemented by `#[oxdock_func]` on a
/// registration marker. The engine calls `registration()` and never names
/// a generated symbol.
pub trait OxDockFn<P: ProcessManager> {
    /// The registry entry deriving from the Rust signature plus doc
    /// comments: name, metadata, and entry point, composed.
    fn registration() -> HostRegistration<P>;
}

/// One host-registered function, grouped into a [`HostModule`] and passed
/// to `Engine::register_module`. Build the entry with the `#[oxdock_func]`-
/// generated registration marker, or by hand. `Pure` entries run
/// on both the AST and the compiled RPN math paths; `Stateful` entries run
/// on the AST path with full step context. `Both` entries carry a stateful
/// body plus a pure string projection: every context with step IO runs
/// the body, while `{{ }}` placeholder expansion (which has no step
/// context) runs the projection.
pub enum HostRegistration<P: ProcessManager> {
    Stateful {
        name: String,
        meta: FuncMeta,
        func: NativeFn<P>,
    },
    Pure {
        name: String,
        meta: FuncMeta,
        func: PureFn,
    },
    Both {
        name: String,
        meta: FuncMeta,
        func: NativeFn<P>,
        template: PureFn,
    },
}

// Manual `Clone` (a derive would demand `P: Clone`): entries share their
// function pointers through the `Arc`s and deep-copy the metadata.
impl<P: ProcessManager> Clone for HostRegistration<P> {
    fn clone(&self) -> Self {
        match self {
            HostRegistration::Stateful { name, meta, func } => HostRegistration::Stateful {
                name: name.clone(),
                meta: meta.clone(),
                func: Arc::clone(func),
            },
            HostRegistration::Pure { name, meta, func } => HostRegistration::Pure {
                name: name.clone(),
                meta: meta.clone(),
                func: Arc::clone(func),
            },
            HostRegistration::Both {
                name,
                meta,
                func,
                template,
            } => HostRegistration::Both {
                name: name.clone(),
                meta: meta.clone(),
                func: Arc::clone(func),
                template: Arc::clone(template),
            },
        }
    }
}

impl<P: ProcessManager> HostRegistration<P> {
    /// Introspectable metadata for this entry, for documentation tooling
    /// that reads modules without registering them.
    pub fn meta(&self) -> &FuncMeta {
        match self {
            HostRegistration::Stateful { meta, .. } => meta,
            HostRegistration::Pure { meta, .. } => meta,
            HostRegistration::Both { meta, .. } => meta,
        }
    }
}

/// One user-defined function body (`FUNC NAME($p: TYPE, ...) { ... }`).
/// Param types resolve to tags at definition time, where the type
/// directory is in scope; the registry never holds raw type strings.
#[derive(Debug, Clone)]
pub(super) struct FuncDefData {
    pub(super) params: Vec<(String, TypeTag)>,
    pub(super) body: Vec<Step>,
}

/// Executable body behind one registry entry: an interpreted script, a
/// pure scalar function, a stateful function with step context, or a
/// dual entry whose pure arm serves `{{ }}` placeholders while the ctx
/// arm serves every context that carries step IO.
pub(super) enum FuncBody<P: ProcessManager> {
    Script(FuncDefData),
    Pure(PureFn),
    Ctx(NativeFn<P>),
    Both { pure: PureFn, ctx: NativeFn<P> },
}

// Manual `Clone` (a derive would demand `P: Clone`).
impl<P: ProcessManager> Clone for FuncBody<P> {
    fn clone(&self) -> Self {
        match self {
            FuncBody::Script(def) => FuncBody::Script(def.clone()),
            FuncBody::Pure(func) => FuncBody::Pure(Arc::clone(func)),
            FuncBody::Ctx(func) => FuncBody::Ctx(Arc::clone(func)),
            FuncBody::Both { pure, ctx } => FuncBody::Both {
                pure: Arc::clone(pure),
                ctx: Arc::clone(ctx),
            },
        }
    }
}

/// One entry in the unified function registry: introspectable metadata
/// (mandatory for every entry, backing the pre-evaluation arity gate)
/// plus the executable body.
pub(super) struct FuncEntry<P: ProcessManager> {
    pub(super) meta: FuncMeta,
    pub(super) body: FuncBody<P>,
}

// Manual `Clone` (a derive would demand `P: Clone`).
impl<P: ProcessManager> Clone for FuncEntry<P> {
    fn clone(&self) -> Self {
        Self {
            meta: self.meta.clone(),
            body: self.body.clone(),
        }
    }
}

/// Names defined in one lexical scope frame: `defined` rejects duplicates
/// in the same scope, `shadowed` restores outer definitions on exit.
struct ScopeFrame<P: ProcessManager> {
    defined: HashSet<String>,
    shadowed: Vec<(String, FuncEntry<P>)>,
}

// Manual `Clone` (a derive would demand `P: Clone`).
impl<P: ProcessManager> Clone for ScopeFrame<P> {
    fn clone(&self) -> Self {
        Self {
            defined: self.defined.clone(),
            shadowed: self.shadowed.clone(),
        }
    }
}

/// The single function registry: DSL `FUNC` definitions, builtins, and
/// host extensions share one lookup table, one metadata path, and one
/// dispatch order. Script entries scope lexically (defined names revert on
/// scope exit, shadowing an outer definition restores it); native entries
/// persist for the run. Shared across `fork()` via clone; the entry maps
/// clone while scope frames stay per state.
/// Module to function-name table of pure entries, shared by reference.
/// Nested so placeholder dispatch looks up `table[module][func]` with
/// zero allocation; rebuilt only when native entries register.
pub type PureTable = HashMap<String, HashMap<String, PureFn>>;

pub struct FunctionRegistry<P: ProcessManager> {
    entries: HashMap<String, FuncEntry<P>>,
    scopes: Vec<ScopeFrame<P>>,
    pure_shared: Arc<PureTable>,
}

impl<P: ProcessManager> FunctionRegistry<P> {
    pub(super) fn with_builtins() -> Self {
        let mut reg = Self {
            entries: HashMap::new(),
            pure_shared: Arc::new(HashMap::new()),
            scopes: vec![ScopeFrame {
                defined: HashSet::new(),
                shadowed: Vec::new(),
            }],
        };
        // Every builtin registers through the same `HostRegistration`
        // entries hosts use: no separate authoring path for engine natives.
        // Builtins land in the `STD` module, exactly like a host module.
        for host in Self::builtin_registrations() {
            match host {
                HostRegistration::Stateful { name, meta, func } => {
                    reg.insert_qualified(STD_MODULE_NAME, name, meta, FuncBody::Ctx(func));
                }
                HostRegistration::Pure { name, meta, func } => {
                    reg.insert_qualified(STD_MODULE_NAME, name, meta, FuncBody::Pure(func));
                }
                HostRegistration::Both {
                    name,
                    meta,
                    func,
                    template,
                } => {
                    reg.insert_qualified(
                        STD_MODULE_NAME,
                        name,
                        meta,
                        FuncBody::Both {
                            pure: template,
                            ctx: func,
                        },
                    );
                }
            }
        }
        reg
    }

    /// All builtins as host-style registrations, built from the same
    /// `#[oxdock_func]`-generated markers hosts use. `with_builtins`
    /// consumes this list, so builtins and hosts share one registration
    /// pathway instead of two authoring models.
    pub(super) fn builtin_registrations() -> Vec<HostRegistration<P>> {
        vec![
            Int::registration(),
            Float::registration(),
            FormatByteCount::registration(),
            ParseByteCount::registration(),
            FormatDuration::registration(),
            ParseDuration::registration(),
            Types::registration(),
            TypeDescribe::registration(),
            Glob::registration(),
            LoadToml::registration(),
            LoadJson::registration(),
            ParseToml::registration(),
            ParseJson::registration(),
            PathType::registration(),
            PathWritable::registration(),
            IsExec::registration(),
            HasKey::registration(),
            MapSet::registration(),
            MergeMaps::registration(),
            Base64Encode::registration(),
            Base64Decode::registration(),
            Rand::registration(),
            ToJson::registration(),
            TypeOf::registration(),
            Functions::registration(),
            Describe::registration(),
            IsTerminal::registration(),
            Eof::registration(),
            TextTrim::registration(),
            TextTrimPrefix::registration(),
            TextTrimSuffix::registration(),
            TextToUpper::registration(),
            TextToLower::registration(),
            TextReplace::registration(),
            TextReplaceRegex::registration(),
            TextSplit::registration(),
            TextJoin::registration(),
            TextLines::registration(),
            TextFields::registration(),
            TextMatch::registration(),
            TextExtract::registration(),
            TextStartsWith::registration(),
            TextEndsWith::registration(),
            TextContains::registration(),
            TextLength::registration(),
            TextCount::registration(),
            TextCountMatches::registration(),
            TextGrep::registration(),
            TextLinesRange::registration(),
            SemaphoreNew::registration(),
            SemaphoreTryAcquire::registration(),
            SemaphoreAvailable::registration(),
        ]
    }

    /// Every name the registry answers to. Backs parse-time shadow
    /// validation through `builtin_function_names`.
    pub(super) fn keys(&self) -> HashSet<String> {
        self.entries.keys().cloned().collect()
    }

    /// Single lookup for every callable: script, pure, or stateful.
    pub(super) fn get(&self, name: &str) -> Option<FuncEntry<P>> {
        self.entries.get(name).cloned()
    }

    fn insert_native(&mut self, name: String, meta: FuncMeta, body: FuncBody<P>) {
        self.entries.insert(name, FuncEntry { meta, body });
        self.rebuild_pure_shared();
    }

    /// Rebuild the shared pure table. Runs only on native insertion:
    /// script definitions insert `Script` bodies directly and never
    /// affect the pure set, so this stays correct between rebuilds.
    /// Dual entries contribute their template arm, which is what
    /// `{{ }}` placeholder expansion resolves.
    fn rebuild_pure_shared(&mut self) {
        let mut table: PureTable = HashMap::new();
        for (name, entry) in &self.entries {
            let func = match &entry.body {
                FuncBody::Pure(func) => func,
                FuncBody::Both { pure, .. } => pure,
                FuncBody::Script(_) | FuncBody::Ctx(_) => continue,
            };
            if let Some((module, base)) = split_qualified(name) {
                table
                    .entry(module.to_string())
                    .or_default()
                    .insert(base.to_string(), Arc::clone(func));
            }
        }
        self.pure_shared = Arc::new(table);
    }

    /// Insert under `MODULE::BASE`, stamping provenance on the metadata.
    /// The single choke point for every registry entry: builtins pass
    /// `STD`, hosts pass their module, scripts pass `SCRIPT`.
    fn insert_qualified(
        &mut self,
        module: &str,
        base: String,
        mut meta: FuncMeta,
        body: FuncBody<P>,
    ) {
        meta.name = qualify(module, &base);
        meta.module = module.to_string();
        // Last-write-wins would silently reroute calls, so a repeated
        // qualified name is a programmer error, never a shadow: panic like
        // conflicting type registrations do. `SCRIPT` definitions bypass
        // this path (`define_script` owns their scoped shadowing).
        if self.entries.contains_key(&meta.name) {
            panic!("duplicate function registration `{}`", meta.name);
        }
        self.insert_native(meta.name.clone(), meta, body);
    }

    pub(super) fn register_host(
        &mut self,
        module: &str,
        name: String,
        mut meta: FuncMeta,
        func: NativeFn<P>,
    ) {
        meta.kind = FuncKind::HostCtx;
        self.insert_qualified(module, name, meta, FuncBody::Ctx(func));
    }

    pub(super) fn register_pure_host(
        &mut self,
        module: &str,
        name: String,
        mut meta: FuncMeta,
        func: PureFn,
    ) {
        meta.kind = FuncKind::HostPure;
        self.insert_qualified(module, name, meta, FuncBody::Pure(func));
    }

    pub(super) fn register_both_host(
        &mut self,
        module: &str,
        name: String,
        mut meta: FuncMeta,
        func: NativeFn<P>,
        template: PureFn,
    ) {
        meta.kind = FuncKind::HostCtx;
        self.insert_qualified(
            module,
            name,
            meta,
            FuncBody::Both {
                pure: template,
                ctx: func,
            },
        );
    }

    /// Define a DSL `FUNC`: names colliding with any known base name cannot
    /// shadow, same-scope duplicates cannot redefine, and nested shadowing
    /// of an outer script definition restores on scope exit. Stored as
    /// `SCRIPT::NAME`, exactly like every other qualified entry.
    pub(super) fn define_script(
        &mut self,
        name: &str,
        params: &[(String, TypeTag)],
        body: &[Step],
    ) -> Result<()> {
        let qualified = qualify(SCRIPT_MODULE_NAME, name);
        let shadowable = matches!(
            self.entries.get(&qualified).map(|entry| &entry.body),
            Some(FuncBody::Script(_))
        );
        // Reserved spans every module: compare base names so the message
        // keeps naming the bare script identifier.
        let reserved = self
            .entries
            .keys()
            .any(|key| split_qualified(key).is_some_and(|(_, base)| base == name));
        if reserved && !shadowable {
            anyhow::bail!("cannot shadow reserved function `{name}`");
        }
        if self
            .scopes
            .last()
            .is_some_and(|frame| frame.defined.contains(&qualified))
        {
            anyhow::bail!("duplicate function `{name}` in same scope");
        }
        let old = self.entries.insert(
            qualified.clone(),
            FuncEntry {
                meta: FuncMeta {
                    name: qualified.clone(),
                    module: SCRIPT_MODULE_NAME.to_string(),
                    kind: FuncKind::Script,
                    params: Some(
                        params
                            .iter()
                            .map(|(name, param_type)| FuncParam {
                                name: name.clone(),
                                param_type: Some(param_type.clone()),
                                allowed: None,
                                // Script `FUNC` params carry no prose:
                                // declarations have no doc syntax.
                                docs: "",
                                options: None,
                                optional: false,
                            })
                            .collect(),
                    ),
                    returns: None,
                    rpn: false,
                    summary: "DSL-defined function.",
                    docs: "Defined via FUNC in script.",
                    examples: &[],
                },
                body: FuncBody::Script(FuncDefData {
                    params: params.to_vec(),
                    body: body.to_vec(),
                }),
            },
        );
        if let Some(frame) = self.scopes.last_mut() {
            frame.defined.insert(qualified.clone());
            if let Some(old) = old {
                frame.shadowed.push((qualified, old));
            }
        }
        Ok(())
    }

    /// Open a lexical scope frame for script definitions. Native entries
    /// persist; only script names track here.
    pub(super) fn push_scope(&mut self) {
        self.scopes.push(ScopeFrame {
            defined: HashSet::new(),
            shadowed: Vec::new(),
        });
    }

    /// Close a lexical scope frame: names defined inside revert, and any
    /// outer definition they shadowed is restored.
    pub(super) fn pop_scope(&mut self) {
        let Some(frame) = self.scopes.pop() else {
            return;
        };
        for name in frame.defined {
            self.entries.remove(&name);
        }
        for (name, old) in frame.shadowed {
            self.entries.insert(name, old);
        }
    }

    /// True when `name` is an interpreted script definition (needing call
    /// scoping and depth budgeting through `call_func_value` rather than
    /// inline evaluation).
    pub(super) fn contains_script(&self, name: &str) -> bool {
        matches!(
            self.entries.get(name).map(|entry| &entry.body),
            Some(FuncBody::Script(_))
        )
    }

    /// Clone the pure fn for `name` (ending the registry borrow) so callers
    /// can invoke it without holding `&self` across a `&mut StepCtx` use.
    /// Dual entries answer with their template arm.
    fn clone_pure_fn(&self, name: &str) -> Option<PureFn> {
        match self.entries.get(name)?.body {
            FuncBody::Pure(ref func) => Some(Arc::clone(func)),
            FuncBody::Both { ref pure, .. } => Some(Arc::clone(pure)),
            _ => None,
        }
    }

    /// Shared snapshot of every pure entry, nested by module and base
    /// name. Cloning the `Arc` is refcount-only: placeholder dispatch
    /// borrows this per expansion with no map copies.
    pub(super) fn pure_shared(&self) -> Arc<PureTable> {
        Arc::clone(&self.pure_shared)
    }

    /// Every entry's metadata, scripts included, sorted by name. Backs
    /// run-start type tag validation.
    pub(super) fn all_metas(&self) -> Vec<FuncMeta> {
        self.entries_metas()
    }

    /// Clone the ctx fn for `name` (ending the registry borrow) so callers
    /// can invoke it with `&mut StepCtx` without double-borrowing state.
    /// Dual entries answer with their stateful arm, which is the full
    /// behavior wherever step IO exists.
    fn clone_ctx_fn(&self, name: &str) -> Option<NativeFn<P>> {
        match self.entries.get(name)?.body {
            FuncBody::Ctx(ref func) => Some(Arc::clone(func)),
            FuncBody::Both { ref ctx, .. } => Some(Arc::clone(ctx)),
            _ => None,
        }
    }

    fn meta(&self, name: &str) -> Option<FuncMeta> {
        self.entries.get(name).map(|entry| entry.meta.clone())
    }

    fn native_metas(&self) -> Vec<FuncMeta> {
        let mut out: Vec<FuncMeta> = Vec::new();
        for entry in self.entries.values() {
            if !matches!(entry.body, FuncBody::Script(_)) {
                out.push(entry.meta.clone());
            }
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    /// Every entry's metadata, scripts included, sorted by name. Backs
    /// runtime `FUNCTIONS()` listings.
    fn entries_metas(&self) -> Vec<FuncMeta> {
        let mut out: Vec<FuncMeta> = self
            .entries
            .values()
            .map(|entry| entry.meta.clone())
            .collect();
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }
}

impl<P: ProcessManager> Clone for FunctionRegistry<P> {
    fn clone(&self) -> Self {
        Self {
            entries: self.entries.clone(),
            scopes: self.scopes.clone(),
            pure_shared: Arc::clone(&self.pure_shared),
        }
    }
}

/// Names of all compiled-in builtins plus `INSPECT` (a dedicated AST/RPN
/// node, not a registry entry). Read straight off a stock registry, so the
/// `#[oxdock_func]` annotations stay the single source of truth: adding a
/// builtin extends this set with no parallel list to update. Seeds
/// parse-time shadow validation, so the parser crate keeps zero
/// compile-time knowledge of builtin names.
pub fn builtin_function_names() -> HashSet<String> {
    let mut names = FunctionRegistry::<DefaultProcessManager>::with_builtins().keys();
    names.insert(KEYWORD_INSPECT.to_string());
    names
}

/// Metadata of every builtin function, sorted by name, for static
/// rendering (docs-gen). Same single source as `builtin_function_names`:
/// the `#[oxdock_func]` annotations, never a parallel list.
pub fn builtin_function_metas() -> Vec<FuncMeta> {
    FunctionRegistry::<DefaultProcessManager>::with_builtins().native_metas()
}

/// Stock `STD` module table derived from the `#[oxdock_func]` builtins:
/// the single source of truth for builtin membership and RPN eligibility.
/// Seeds parse-time module resolution, so the parser crate keeps zero
/// compile-time knowledge of builtin names.
pub fn std_module_table() -> oxdock_parser::ModuleTable {
    // Registry names are qualified (`STD::GLOB`); the table holds bases.
    let functions: HashSet<String> = builtin_function_metas()
        .into_iter()
        .map(|meta| base_name(&meta.name).to_string())
        .collect();
    oxdock_parser::ModuleTable {
        modules: HashMap::from([(
            STD_MODULE_NAME.to_string(),
            Some(oxdock_parser::ModuleFuncs { functions }),
        )]),
    }
}

// Builtins below use the `#[oxdock_func]` host export macro, the exact same authoring
// model as host-registered functions: metadata, arity checks, and argument
// unpacking derive from the signature plus doc comments, so adding a native
// means writing one small typed function plus one line each in
// `builtin_registrations` (consumed by `with_builtins`) and the
// `FUNCTIONS`/`DESCRIBE`/`TYPES` surface, which all read the same symbols.

/// Convert a value to INT.
///
/// Trims ASCII whitespace and parses i64. Passes Int through; Float only
/// when integral and finite; Bool as 1/0.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "convert",
    code = r###"
    IMPORT [STD]
    LET $n: INT = INT("42")
    ASSERT_EQ $n 42
    LET $flag: INT = INT(true)
    ASSERT_EQ $flag 1
    "###
)]
fn int(
    /// Value to convert to `INT`.
    #[union(Int, Float, String, Bool)]
    val: Value,
) -> Result<Value> {
    super::args::int_from_value(val)
}

/// Convert a value to FLOAT.
///
/// Parses f64 (accepts int strings), bails on non-finite or non-numeric.
///
#[oxdock_func(pure, returns = TypeTag::Float)]
#[example(
    name = "convert",
    code = r###"
    IMPORT [STD]
    LET $f: FLOAT = FLOAT("1.5")
    ASSERT_EQ $f 1.5
    LET $whole: FLOAT = FLOAT(2)
    ASSERT_EQ $whole 2.0
    "###
)]
fn float(
    /// Value to convert to `FLOAT`.
    #[union(Float, Int, String)]
    val: Value,
) -> Result<Value> {
    super::args::float_from_value(val)
}

/// Format a byte count canonically (`500B`, `10KB`, `2MB`, `1GB`,
/// binary `10KIB` and friends): the inverse of `PARSE_BYTE_COUNT`, so
/// formatted values reparse exactly. Negative inputs fail.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "format and round-trip",
    code = r###"
    # Byte counts render in the largest exact unit.
    IMPORT [STD]
    LET $pretty: STRING = FORMAT_BYTE_COUNT(1_048_576)
    ASSERT_EQ $pretty "1MiB"

    # The pair round-trips through the parser.
    LET $back: INT = PARSE_BYTE_COUNT($pretty)
    ASSERT_EQ $back 1_048_576
    "###
)]
fn format_byte_count(
    /// Byte count to format; negatives fail.
    bytes: i64,
) -> Result<Value> {
    if bytes < 0 {
        return Err(anyhow::anyhow!(
            "FORMAT_BYTE_COUNT needs a non-negative byte count, got {bytes}"
        ));
    }
    Ok(Value::string(oxdock_parser::command::format_byte_count(
        bytes as u64,
    )))
}

/// Parse a byte-count token to `INT` bytes.
///
/// Bare digits are bytes; suffixes `B`/`KB`/`MB`/`GB`/`TB` scale
/// decimal and `KIB`/`MIB`/`GIB`/`TIB` binary, in any case. Plain
/// `INT` values pass through (already bytes); integral finite `FLOAT`
/// values convert. Pairs with `FORMAT_BYTE_COUNT` for the round trip.
///
/// ```oxdock
/// # Units parse case-insensitively; bare digits are bytes.
/// IMPORT [STD]
/// LET $bytes: INT = PARSE_BYTE_COUNT("1Gb")
/// ASSERT_EQ $bytes 1_000_000_000
///
/// # Binary suffixes scale by powers of two.
/// LET $mebi: INT = PARSE_BYTE_COUNT("2mib")
/// ASSERT_EQ $mebi 2_097_152
///
/// # Digit runs group Rust-literal style.
/// LET $grouped: INT = PARSE_BYTE_COUNT("1_000_000")
/// ASSERT_EQ $grouped 1_000_000
///
/// # Plain numbers pass through as bytes already.
/// LET $plain: INT = PARSE_BYTE_COUNT(1_048_576)
/// ASSERT_EQ $plain 1_048_576
///
/// # Bare literals group too.
/// LET $bare: INT = PARSE_BYTE_COUNT(1_000_000)
/// ASSERT_EQ $bare 1_000_000
///
/// # One optional space separates number from unit.
/// LET $spaced: INT = PARSE_BYTE_COUNT("1 GB")
/// ASSERT_EQ $spaced 1_000_000_000
/// ```
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "parse",
    code = r###"
    IMPORT [STD]
    LET $n: INT = PARSE_BYTE_COUNT("10KB")
    ASSERT_EQ $n 10_000
    LET $identity: INT = PARSE_BYTE_COUNT(512)
    ASSERT_EQ $identity 512
    "###
)]
fn parse_byte_count(
    /// Byte-count token to parse, or bytes already.
    #[union(Int, Float, String)]
    text: Value,
) -> Result<Value> {
    if let Some(n) = text.as_i64() {
        if n < 0 {
            return Err(anyhow::anyhow!(
                "PARSE_BYTE_COUNT needs a non-negative byte count, got {n}"
            ));
        }
        return Ok(Value::int(n));
    }
    if let Some(f) = text.as_f64() {
        if f.is_finite() && f.fract() == 0.0 && f >= 0.0 && f <= i64::MAX as f64 {
            return Ok(Value::int(f as i64));
        }
        return Err(anyhow::anyhow!(
            "PARSE_BYTE_COUNT needs an integral byte count, got {f:?}"
        ));
    }
    let Some(s) = text.as_str() else {
        return Err(anyhow::anyhow!(
            "PARSE_BYTE_COUNT needs a token or number, found {text:?}"
        ));
    };
    let n = oxdock_parser::command::parse_byte_count(s)?;
    let n = i64::try_from(n)
        .map_err(|_| anyhow::anyhow!("PARSE_BYTE_COUNT out of INT range: {s:?}"))?;
    Ok(Value::int(n))
}

/// Format millisecond durations canonically (`500ms`, `10s`, `2m`,
/// `1h`): the inverse of `PARSE_DURATION`, so formatted values
/// reparse exactly. Negative inputs fail.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "format and round-trip",
    code = r###"
    # Durations render in the largest exact unit.
    IMPORT [STD]
    LET $pretty: STRING = FORMAT_DURATION(90_000)
    ASSERT_EQ $pretty "90s"

    # The pair round-trips through the parser.
    LET $back: INT = PARSE_DURATION($pretty)
    ASSERT_EQ $back 90_000
    "###
)]
fn format_duration(
    /// Milliseconds to format; negatives fail.
    millis: i64,
) -> Result<Value> {
    if millis < 0 {
        return Err(anyhow::anyhow!(
            "FORMAT_DURATION needs non-negative milliseconds, got {millis}"
        ));
    }
    Ok(Value::string(oxdock_parser::command::format_duration(
        &std::time::Duration::from_millis(millis as u64),
    )))
}

/// Parse a duration token to `INT` milliseconds.
///
/// Suffixes `ms`/`s`/`m`/`h`; a bare number means seconds. Pairs with
/// `FORMAT_DURATION` for the round trip.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "parse",
    code = r###"
    # A bare number means seconds, like TIMEOUT.
    IMPORT [STD]
    LET $ms: INT = PARSE_DURATION("2m")
    ASSERT_EQ $ms 120_000

    # Milliseconds stay exact.
    LET $exact: INT = PARSE_DURATION("1500ms")
    ASSERT_EQ $exact 1_500

    # One optional space separates number from unit.
    LET $spaced: INT = PARSE_DURATION("1 h")
    ASSERT_EQ $spaced 3_600_000
    "###
)]
fn parse_duration(
    /// Duration token to parse.
    text: String,
) -> Result<Value> {
    let duration = oxdock_parser::command::parse_duration(&text)?;
    let millis = i64::try_from(duration.as_millis())
        .map_err(|_| anyhow::anyhow!("PARSE_DURATION out of INT range: {text:?}"))?;
    Ok(Value::int(millis))
}

/// List workspace paths matching a glob pattern.
///
/// Sorted, root-relative LIST; empty on no match or `..` escape.
///
#[oxdock_func(rpn, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "match",
    code = r###"
    IMPORT [STD]
    WRITE "notes-a.md" "a"
    WRITE "notes-b.md" "b"
    WRITE "other.txt" "c"
    LET $found: LIST<STRING> = GLOB("notes-*.md")
    ASSERT_EQ $found.0 "notes-a.md"
    ASSERT_EQ $found.1 "notes-b.md"
    "###
)]
fn glob<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Glob pattern matched against workspace paths.
    #[union(String, Path)]
    pattern: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&pattern)?;
    super::args::glob_from_value(&[Value::string(text)], cx)
}

/// Load and parse a TOML file.
///
/// Reads a workspace file and parses TOML into a DSL value.
///
#[oxdock_func(rpn, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
    name = "load",
    code = r###"
    IMPORT [STD]
    WRITE "cfg.toml" "title = \"demo\""
    LET $cfg: MAP<ANY> = LOAD_TOML("cfg.toml")
    ASSERT_EQ $cfg.title "demo"
    "###
)]
fn load_toml<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Workspace file path to load and parse as TOML.
    #[union(String, Path)]
    path: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&path)?;
    super::args::load_toml_from_value(&[Value::string(text)], cx)
}

/// Load and parse a JSON file.
///
/// Reads a workspace file and parses JSON into a DSL value. Returns
/// `ANY` by design: a top-level array or scalar parses to `LIST` or a
/// scalar word. `LET` coercion still checks the actual value at
/// assignment.
///
#[oxdock_func(rpn, returns = TypeTag::Any)]
#[example(
    name = "load",
    code = r###"
    IMPORT [STD]
    WRITE "data.json" "{\"a\": 1}"
    LET $doc: MAP<ANY> = LOAD_JSON("data.json")
    ASSERT_EQ $doc.a 1
    "###
)]
fn load_json<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Workspace file path to load and parse as JSON.
    #[union(String, Path)]
    path: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&path)?;
    super::args::load_json_from_value(&[Value::string(text)], cx)
}

/// Parse TOML text already held in memory.
///
/// Uses the same conversion as file loading, so fetch bodies, file
/// contents, and captured text share one JSON/TOML shape.
///
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
    name = "parse",
    code = r###"
    IMPORT [STD]
    LET $doc: MAP<ANY> = PARSE_TOML("title = \"demo\"")
    ASSERT_EQ $doc.title "demo"
    "###
)]
fn parse_toml(
    /// TOML text already held in memory.
    text: String,
) -> Result<Value> {
    super::args::parse_toml_from_value(Value::string(text))
}

/// Parse JSON text already held in memory.
///
/// Uses the same conversion as file loading, so fetch bodies, file
/// contents, and captured text share one JSON value shape. Returns
/// `ANY` by design, matching `LOAD_JSON`: a top-level array or scalar
/// parses to `LIST` or a scalar word. `LET` coercion still checks the
/// actual value at assignment.
///
#[oxdock_func(pure, returns = TypeTag::Any)]
#[example(
    name = "parse",
    code = r###"
    # Parse in-memory JSON text with the same conversion as file loading.
    IMPORT [STD]
    LET $body: STRING = "{\"a\": 1}"
    LET $doc: ANY = PARSE_JSON($body)
    ASSERT_EQ $doc.a 1

    # Top-level arrays parse to LIST, which is why the return is ANY.
    LET $items: LIST<ANY> = PARSE_JSON("[1, 2]")
    ASSERT_EQ $items.0 1
    ASSERT_EQ $items.1 2
    "###
)]
fn parse_json(
    /// JSON text already held in memory.
    text: String,
) -> Result<Value> {
    super::args::parse_json_from_value(Value::string(text))
}

/// Describe a filesystem entry.
///
/// Reports file, dir, symlink (no-follow), or absent. AST-only by design;
/// there is no RPN arm for filesystem IO.
///
#[oxdock_func(returns = TypeTag::String)]
#[example(
    name = "describe",
    code = r###"
    IMPORT [STD]
    WRITE "entry.txt" "words"
    LET $kind: STRING = PATH_TYPE("entry.txt")
    ASSERT_EQ $kind "file"
    LET $missing: STRING = PATH_TYPE("no-such-entry.txt")
    ASSERT_EQ $missing "absent"
    "###
)]
fn path_type<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Workspace path of the entry to describe.
    #[union(String, Path)]
    path: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&path)?;
    super::args::path_type_from_value(&[Value::string(text)], cx)
}

/// Report whether an ordinary file can be created at a path.
///
/// Walks up to the nearest existing ancestor, requires it be a
/// directory, and probes writability by creating and deleting a probe
/// file, so retry loops never litter. The empty string is false.
/// AST-only by design, like `PATH_TYPE`: there is no RPN arm for
/// filesystem IO.
///
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
    name = "probe",
    code = r###"
    # An absent nested path is writable when its ancestor is.
    IMPORT [STD]
    LET $fresh: BOOL = PATH_WRITABLE("probe-check/nested/file.txt")
    ASSERT_EQ $fresh true

    # The empty string never aliases the working directory.
    LET $empty: BOOL = PATH_WRITABLE("")
    ASSERT_EQ $empty false
    "###
)]
fn path_writable<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Workspace path to probe for file creation.
    #[union(String, Path)]
    path: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&path)?;
    super::args::path_writable_from_value(&[Value::string(text)], cx)
}

/// Report whether a name resolves to an executable file (#100).
///
/// Bare command names search `env:PATH` (`IS_EXEC("tar")` finds the
/// first executable `tar`, plus `tar.exe`, on `PATH`); spellings
/// containing a separator check that file directly. True means a real
/// file with an executable bit on Unix, or a `PATHEXT` extension on
/// Windows: each `env:PATHEXT` entry is probed in order (default
/// `.COM;.EXE;.BAT;.CMD`, an explicitly empty value probes nothing). Absolute `PATH` entries resolve through the workspace as
/// usual, so host probing wants `WORKSPACE SYSTEM` and an inherited
/// `PATH`; anything unresolvable reads false, never errors. AST-only
/// by design, like `PATH_TYPE`: there is no RPN arm for filesystem IO.
///
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
    name = "probe",
    code = r###"
    # Missing tools read false instead of failing the run.
    IMPORT [STD]
    LET $missing: BOOL = IS_EXEC("no-such-tool-xyz")
    ASSERT_EQ $missing false

    # Plain files are not executable on any platform.
    WRITE tool.txt payload
    LET $plain: BOOL = IS_EXEC("tool.txt")
    ASSERT_EQ $plain false
    "###
)]
fn is_exec<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Command name or file path to probe.
    #[union(String, Path)]
    name: Value,
) -> Result<Value> {
    let text = super::args::string_or_path_text(&name)?;
    super::args::is_exec_from_value(&[Value::string(text)], cx)
}

/// Report whether a map holds a key.
///
/// Pure MAP probe so scripts can branch on optional fields without
/// tripping the strict missing-key error.
///
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
    name = "probe",
    code = r###"
    IMPORT [STD]
    LET $m: MAP<ANY> = PARSE_JSON("{\"a\": 1}")
    LET $hit: BOOL = HAS_KEY($m, "a")
    ASSERT_EQ $hit true
    LET $miss: BOOL = HAS_KEY($m, "b")
    ASSERT_EQ $miss false
    "###
)]
fn has_key(
    /// Map to probe.
    map: BTreeMap<String, Value>,
    /// Key to look up.
    key: String,
) -> Result<Value> {
    super::args::has_key_from_value(Value::map(map), &key)
}

/// Insert one key into a map.
///
/// Fails on duplicates so two entries sharing a key fail the run
/// instead of silently shadowing each other.
///
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
    name = "insert",
    code = r###"
    IMPORT [STD]
    LET $m: MAP<ANY> = MAP_SET({seed: 0}, "a", 1)
    ASSERT_EQ $m.a 1
    "###
)]
fn map_set(
    /// Map to insert into.
    map: BTreeMap<String, Value>,
    /// Key to insert; duplicates fail.
    key: String,
    /// Value to store.
    value: Value,
) -> Result<Value> {
    super::args::map_set_from_value(Value::map(map), key, value)
}

/// Draw a random FLOAT uniformly from [0, 1), optionally seeded.
///
/// Impure by design: unseeded calls return different values, so this
/// runs on the AST script path only, never as a `{{ }}` placeholder
/// call. A seed draws the same value on every run, for deterministic
/// fixtures. Scale and shift the draw for wider ranges.
///
#[oxdock_func(returns = TypeTag::Float, summary = "Draw a random FLOAT uniformly from [0, 1), optionally seeded.")]
#[example(
    name = "seeded draw",
    code = r###"
    # A seed draws the same value on every run.
    IMPORT [STD]
    LET $one: FLOAT = RAND({seed: 7})
    LET $two: FLOAT = RAND({seed: 7})
    ASSERT_EQ $one $two
    "###
)]
#[example(
    name = "unseeded draw",
    code = r###"
    # Scale a unit draw into a backoff delay between 1 and 11 seconds.
    IMPORT [STD]
    LET $draw: FLOAT = RAND()
    LET $delay: FLOAT = 1.0 + $draw * 10.0
    IF $delay < 1.0 {
        EXIT 1
    }
    IF $delay >= 11.0 {
        EXIT 1
    }
    ECHO "retrying in {{ $delay }}s"
    "###
)]
fn rand<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Draw settings: `seed`. Omittable: a missing options MAP fills `{}`.
    #[options("seed?: INT")]
    #[optional]
    options: BTreeMap<String, Value>,
) -> Result<Value> {
    let _ = cx;
    // `check_options` in the wrapper verified the shape above; the
    // read below narrows infallibly after.
    let seed = options
        .get("seed")
        .map(|value| value.as_i64().expect("check_options verified INT"));
    super::args::rand_float_from_value(seed)
}

/// Encode a STRING as standard base64.
///
/// Padded RFC 4648 alphabet, so encoded output pastes into any
/// standard decoder. Pairs with `BASE64_DECODE` for the round trip.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "encode",
    code = r###"
    # Encode text to standard base64.
    IMPORT [STD]
    LET $encoded: STRING = BASE64_ENCODE("hello")
    ASSERT_EQ $encoded "aGVsbG8="
    "###
)]
fn base64_encode(
    /// Text to encode.
    text: String,
) -> Result<Value> {
    super::args::base64_encode_from_value(Value::string(text))
}

/// Decode standard base64 to a STRING.
///
/// Fails naming the input on invalid alphabet characters or
/// non-UTF-8 payloads instead of rendering a silent empty. Pairs
/// with `BASE64_ENCODE` for the round trip.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "decode",
    code = r###"
    # Decode standard base64 back to text.
    IMPORT [STD]
    LET $decoded: STRING = BASE64_DECODE("aGVsbG8=")
    ASSERT_EQ $decoded "hello"
    "###
)]
fn base64_decode(
    /// Base64 text to decode.
    text: String,
) -> Result<Value> {
    super::args::base64_decode_from_value(Value::string(text))
}

/// Encode a script value as JSON with one trailing newline.
///
/// Maps stay sorted; only template-safe shapes (STRING, INT, FLOAT,
/// BOOL, LIST, MAP) survive, anything else fails here instead of
/// rendering as a silent empty.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "encode",
    code = r###"
    IMPORT [STD]
    LET $json: STRING = TO_JSON({a: 1})
    ASSERT_EQ $json "{\"a\":1}\n"
    "###
)]
fn to_json(
    /// Value to encode as JSON.
    #[union(String, Int, Float, Bool, List, Map)]
    value: Value,
) -> Result<Value> {
    super::args::to_json_from_value(value)
}

/// Name the word a value holds, for data-driven branching.
///
/// Returns the registered type word (`STRING`, `INT`, `FLOAT`, `BOOL`,
/// `LIST`, `MAP`, plus handle words like `PIPE`): the same name the
/// value prints in arity and coercion errors, so scripts can branch
/// on config shapes (a path string or a path list) without failing.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "name the word",
    code = r###"
    IMPORT [STD]
    LET $word: STRING = TYPE_OF(42)
    ASSERT_EQ $word "INT"
    "###
)]
fn type_of(
    /// Value whose word to name.
    value: Value,
) -> Result<Value> {
    super::args::type_of_from_value(value)
}

/// Merge a LIST of MAPs in order under one duplicate policy.
///
/// `fail_on_duplicate` fails naming the repeated key, so two files
/// claiming one placeholder fail the run instead of shadowing each
/// other. `overwrite` lets later files win, for environment overlays.
/// Non MAP elements fail naming their position.
///
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
    name = "merge",
    code = r###"
    IMPORT [STD]
    LET $m: MAP<ANY> = MERGE_MAPS([{a: 1}, {b: 2}], "overwrite")
    ASSERT_EQ $m.a 1
    ASSERT_EQ $m.b 2
    "###
)]
fn merge_maps(
    /// LIST of MAPs to merge in order.
    maps: Vec<BTreeMap<String, Value>>,
    /// Duplicate policy.
    #[values("fail_on_duplicate", "overwrite")]
    policy: String,
) -> Result<Value> {
    super::args::merge_maps_from_value(
        Value::list(maps.into_iter().map(Value::map).collect()),
        policy,
    )
}

/// List all visible function names.
///
/// Sorted LIST of qualified `MODULE::NAME` entries: DSL-defined plus native
/// plus host-registered names.
#[oxdock_func(returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "list",
    code = r###"
    IMPORT [STD]
    LET $names: LIST<STRING> = FUNCTIONS()
    ASSERT_CONTAINS $names "STD::INT"
    "###
)]
fn functions<P: ProcessManager>(cx: &mut StepCtx<P>) -> Result<Value> {
    let mut names: Vec<String> = cx
        .state
        .list_functions()
        .into_iter()
        .map(|meta| meta.name)
        .collect();
    names.sort();
    names.dedup();
    Ok(Value::list(names.into_iter().map(Value::string).collect()))
}

/// Describe one function by qualified name.
///
/// Returns a MAP with name, module, kind, params, returns, rpn, and
/// summary. `params` is always a LIST (empty for parameterless
/// functions) of entries carrying name, param_type, allowed, docs,
/// options, and optional flags, so the shape holds for every entry. Bare names
/// fail closed: `DESCRIBE` requires the qualified form (except
/// `INSPECT`, which is syntax rather than a registry entry). Errors
/// on unknown function.
///
/// Field table for the `DESCRIBE` result bundle: one source for the
/// shape the host mints, so introspection, enforcement, and
/// documentation cannot drift apart.

#[oxdock_func(
    returns = TypeTag::Record(std::sync::Arc::from([Field {
        name: "name".to_string(),
        ty: TypeTag::String,
        docs: "Qualified function name.",
        optional: false,
    }, Field {
        name: "module".to_string(),
        ty: TypeTag::String,
        docs: "Owning module.",
        optional: false,
    }, Field {
        name: "kind".to_string(),
        ty: TypeTag::String,
        docs: "Origin label (`host` or `script`).",
        optional: false,
    }, Field {
        name: "params".to_string(),
        ty: TypeTag::ListOf(std::sync::Arc::new(TypeTag::MapOf(std::sync::Arc::new(
            TypeTag::Any,
        )))),
        docs: "Parameter entries (empty for parameterless functions).",
        optional: false,
    }, Field {
        name: "returns".to_string(),
        ty: TypeTag::String,
        docs: "Structural return shape (empty when untagged).",
        optional: false,
    }, Field {
        name: "rpn".to_string(),
        ty: TypeTag::Bool,
        docs: "True when callable in RPN position.",
        optional: false,
    }, Field {
        name: "summary".to_string(),
        ty: TypeTag::String,
        docs: "One-line description.",
        optional: false,
    }]))
    summary = "Describe one function by qualified name."
)]
#[example(
    name = "describe",
    code = r###"
    IMPORT [STD]
    LET $info: MAP<ANY> = DESCRIBE("STD::INT")
    ASSERT_EQ $info.name "STD::INT"
    ASSERT_EQ $info.returns "INT"
    "###
)]
fn describe<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Qualified function name (`MODULE::NAME`).
    name: String,
) -> Result<Value> {
    if split_qualified(&name).is_none() && name != KEYWORD_INSPECT {
        anyhow::bail!(
            "unknown function `{name}`: DESCRIBE requires a qualified name (e.g. `STD::{name}`)"
        );
    }
    cx.state
        .describe_function(&name)
        .ok_or_else(|| anyhow::anyhow!("unknown function {name}"))
}

/// List all known type names.
///
/// Sorted LIST of startup plus host-registered type descriptors. Reads the
/// run's name directory, so it runs on the AST path like the other
/// introspection functions.
#[oxdock_func(returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "list",
    code = r###"
    IMPORT [STD]
    LET $names: LIST<STRING> = TYPES()
    ASSERT_CONTAINS $names "STRING"
    "###
)]
fn types<P: ProcessManager>(cx: &mut StepCtx<P>) -> Result<Value> {
    Ok(Value::list(
        cx.state
            .type_names()
            .into_iter()
            .map(Value::string)
            .collect(),
    ))
}

/// Describe one type by name.
///
/// Returns a MAP with name, summary, and docs. Errors on unknown type.
/// Reads the run's name directory, so it runs on the AST path.
///
#[oxdock_func(returns = TypeTag::Record(std::sync::Arc::from([Field {
    name: "name".to_string(),
    ty: TypeTag::String,
    docs: "Queried type name.",
    optional: false,
}, Field {
    name: "summary".to_string(),
    ty: TypeTag::String,
    docs: "One-line description.",
    optional: false,
}, Field {
    name: "docs".to_string(),
    ty: TypeTag::String,
    docs: "Full documentation text.",
    optional: false,
}])))]
#[example(
    name = "describe",
    code = r###"
    IMPORT [STD]
    LET $info: MAP<ANY> = TYPE_DESCRIBE("STRING")
    ASSERT_EQ $info.name "STRING"
    "###
)]
fn type_describe<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Type name to describe.
    name: String,
) -> Result<Value> {
    if let Some(descriptor) = cx.state.describe_type(&name) {
        let mut map = BTreeMap::new();
        map.insert(
            "name".to_string(),
            Value::string(descriptor.name.to_string()),
        );
        map.insert(
            "summary".to_string(),
            Value::string(descriptor.summary.to_string()),
        );
        map.insert(
            "docs".to_string(),
            Value::string(descriptor.docs.to_string()),
        );
        return Ok(Value::map(map));
    }
    // Script aliases have no descriptor but introspect the same way:
    // the entry names the alias and its canonical target spelling.
    if let Some(target) = cx.state.alias_target(&name) {
        let mut map = BTreeMap::new();
        map.insert("name".to_string(), Value::string(name.clone()));
        map.insert(
            "summary".to_string(),
            Value::string(format!("Script type alias for {target}.")),
        );
        map.insert(
            "docs".to_string(),
            Value::string(format!(
                "Declared with TYPE {name} = {target}. The alias expands structurally at every declaration."
            )),
        );
        return Ok(Value::map(map));
    }
    Err(anyhow::anyhow!("unknown type {name}"))
}

/// Report whether a standard stream is a terminal.
///
/// `IS_TERMINAL("stdin")`, `IS_TERMINAL("stdout")`, or `IS_TERMINAL("stderr")`
/// answers for the step's stream as currently bound, so scripts can adapt
/// prompts, colors, and progress output. Anything diverted from the
/// terminal reports false without touching host handles: `WITH_IO` pipe
/// bindings (script backends and OS pairs), `LET`-capture sinks, staged
/// runner sinks, and any materialized stdin stream (only a directly
/// inherited fd falls back to the process check). A transparent root tee
/// still answers the session question via the process check. The name
/// matches exactly (no case folding): anything else bails. AST-only:
/// reads the step context like the other introspection functions.
///
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
    name = "null stdin",
    code = r###"
    # A null stdin is never a terminal, in every harness.
    IMPORT [STD]
    LET $term: BOOL = IS_TERMINAL("stdin")
    ASSERT_EQ $term false
    "###
)]
fn is_terminal<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Stream name: `stdin`, `stdout`, or `stderr` (exact match).
    #[values("stdin", "stdout", "stderr")]
    stream_name: String,
) -> Result<Value> {
    use std::io::IsTerminal;
    let terminal = match stream_name.as_str() {
        "stdin" => {
            // A script-pipe backend is definitive; Null is /dev/null.
            // Only a directly inherited fd answers the process check: a
            // materialized Stream is always a binding (staged input,
            // WITH_IO pipe, or OS half), never the raw fd.
            if cx.stdin_pipe.is_some() {
                false
            } else {
                match &cx.stdin {
                    CommandStdin::Null => false,
                    #[cfg(not(miri))]
                    CommandStdin::OsPipe(_) => false,
                    CommandStdin::Stream(_) => false,
                    CommandStdin::Inherit => std::io::stdin().is_terminal(),
                }
            }
        }
        "stdout" => {
            if cx.out_pipe.is_some() {
                // WITH_IO script-pipe binding: never a terminal.
                false
            } else if cx.state.io.stdout().is_some() {
                // Staged runner sink: the root tee diverts bytes to the
                // sink only, never to real stdout.
                false
            } else {
                match &cx.out {
                    // Unbound: inherited straight through.
                    None => std::io::stdout().is_terminal(),
                    // OS kernel pipe: never a terminal.
                    #[cfg(not(miri))]
                    Some(StreamHandle::Os(_)) => false,
                    // Root tee (transparent: forwards to real stdout when
                    // unstaged, so terminal-ness survives) versus a genuine
                    // diversion. WITH_IO bindings never reach here (script
                    // pipes trip out_pipe, OS pipes trip Os, staged sinks
                    // trip above). LET-capture cannot reach here either:
                    // assign_capture installs no sink for a bare NAME(...)
                    // call, so a direct query always observes the ambient
                    // routing. The process check answers the session
                    // question.
                    Some(StreamHandle::Stream(_)) => std::io::stdout().is_terminal(),
                }
            }
        }
        "stderr" => {
            if cx.state.io.stderr().is_some() {
                // Staged runner sink: diverted, never a terminal.
                false
            } else {
                match &cx.err {
                    // Unbound: inherited straight through.
                    None => std::io::stderr().is_terminal(),
                    // OS kernel pipe: never a terminal.
                    #[cfg(not(miri))]
                    Some(StreamHandle::Os(_)) => false,
                    // Root never tees stderr and LET never captures it,
                    // so a bound handle here is always a WITH_IO binding.
                    Some(StreamHandle::Stream(_)) => false,
                }
            }
        }
        _ => anyhow::bail!(
            // Unreachable through the macro path: the generated
            // extractor rejects non-members before the body runs, and
            // the static pre-pass rejects bad literals earlier still.
            // Kept as a bail (never a panic) for hand-built callers.
            "IS_TERMINAL expects \"stdin\", \"stdout\", or \"stderr\", got {stream_name:?}"
        ),
    };
    Ok(Value::bool(terminal))
}

/// Create a counting semaphore admitting at most `max` concurrent holders.
///
/// Non-positive maxima bail. The word names a shared backend: every clone
/// observes the same count, and admission runs through
/// `SEMAPHORE_TRY_ACQUIRE`, never through the `SEMAPHORE_AVAILABLE`
/// readout.
///
#[oxdock_func(returns = TypeTag::Semaphore)]
#[example(
    name = "mint",
    code = r###"
    # Mint a semaphore admitting at most two holders.
    IMPORT [STD]
    LET $sem: SEMAPHORE = SEMAPHORE_NEW(2)

    # A fresh semaphore reports its full cap as available.
    LET $free: INT = SEMAPHORE_AVAILABLE($sem)
    ASSERT_EQ $free 2
    "###
)]
fn semaphore_new<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Maximum concurrent holders; must be positive.
    max: i64,
) -> Result<Value> {
    let _ = cx;
    if max <= 0 {
        return Err(anyhow::anyhow!(
            "SEMAPHORE_NEW() requires a positive max, got {max}"
        ));
    }
    Ok(Value::semaphore(max as usize))
}

/// Attempt one non-blocking acquire, always answering a MAP.
///
/// `held` is `true` with the permit under the `permit` key, or `false`
/// with no `permit` key: branch on `$m.held` directly. The DSL has no
/// null, so the absent key is the miss shape. Do not read `$m.permit`
/// unless `held`: missing-key access bails strictly.
/// Never waits, so no wait can wedge.
///
#[oxdock_func(returns = TypeTag::Record(std::sync::Arc::from([Field {
    name: "held".to_string(),
    ty: TypeTag::Bool,
    docs: "`true` with the permit under `permit`, `false` with no `permit` key.",
    optional: false,
}, Field {
    name: "permit".to_string(),
    ty: TypeTag::Permit,
    docs: "Permit handle; read only when `held`.",
    optional: true,
}])))]
#[example(
    name = "acquire",
    code = r###"
    # First acquire holds with a permit to bind.
    IMPORT [STD]
    LET $sem: SEMAPHORE = SEMAPHORE_NEW(1)
    LET $first: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
    ASSERT_EQ $first.held true
    LET $permit: PERMIT = $first.permit

    # A capped semaphore answers held false with no permit key.
    LET $second: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
    ASSERT_EQ $second.held false
    LET $has_permit: BOOL = HAS_KEY($second, "permit")
    ASSERT_EQ $has_permit false
    "###
)]
fn semaphore_try_acquire<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Semaphore handle from `SEMAPHORE_NEW`.
    sem: Arc<SemaphoreState>,
) -> Result<Value> {
    let _ = cx;
    let mut map = BTreeMap::new();
    if sem.try_acquire() {
        map.insert("held".to_string(), Value::bool(true));
        map.insert("permit".to_string(), Value::permit(&sem));
    } else {
        map.insert("held".to_string(), Value::bool(false));
    }
    Ok(Value::map(map))
}

/// Read free permits under the lock, with no mutation.
///
/// Observability only (audit lines, healthchecks: `active = max - free`).
/// Exact at read time and stale the instant the caller acts on it, so it
/// must never drive admission: that is `SEMAPHORE_TRY_ACQUIRE`'s job.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "readout",
    code = r###"
    # Hold one permit on a cap of two.
    IMPORT [STD]
    LET $sem: SEMAPHORE = SEMAPHORE_NEW(2)
    LET $acq: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
    ASSERT_EQ $acq.held true

    # One held permit leaves exactly one free.
    LET $free: INT = SEMAPHORE_AVAILABLE($sem)
    ASSERT_EQ $free 1
    "###
)]
fn semaphore_available(
    /// Semaphore handle from `SEMAPHORE_NEW`.
    sem: Arc<SemaphoreState>,
) -> Result<Value> {
    Ok(Value::int(sem.available() as i64))
}

/// Trim Unicode whitespace from both ends of text.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes stream on a pump thread with O(chunk) memory: leading
/// whitespace falls at the stream head while trailing whitespace
/// holds in the spill substrate until the next segment proves it
/// interior, so unbounded streams never buffer unbounded memory.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_trim_string
)]
#[example(
    name = "trim",
    code = r###"
    # Leading and trailing whitespace falls away.
    IMPORT [STD]
    LET $clean: STRING = TEXT_TRIM("  hello  ")
    ASSERT_EQ $clean "hello"
    "###
)]
#[example(
    name = "trim pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same transform.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "  hello  "
    }
    LET $out: PIPE = TEXT_TRIM($cap)
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "hello"
    "###
)]
#[example(
    name = "trim template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'clean:{{ STD::TEXT_TRIM("  hello  ") }}'
    ASSERT_EQ $msg "clean:hello"
    "###
)]
#[example(
    name = "trim file template",
    code = r###"
    # EXPAND templates resolve the same registry.
    IMPORT [STD]
    WRITE "tpl.txt" "clean:\{{ STD::TEXT_TRIM(\"  hello  \") }}"
    LET $out: STRING = EXPAND "tpl.txt"
    ASSERT_EQ $out "clean:hello"
    "###
)]
fn text_trim<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to trim.
    #[union(String, Pipe)]
    text: Value,
) -> Result<Value> {
    super::args::mirror_stream(cx, "TEXT_TRIM", &text, super::args::TextPump::Trim)
}

/// Template projection of `TEXT_TRIM`: the same trim for `{{ }}`
/// placeholders, which carry no step IO for pipes.
fn text_trim_string(text: Value) -> Result<Value> {
    super::args::mirror_string("TEXT_TRIM", &text, super::args::TextPump::Trim)
}

/// Strip one leading prefix, returning the input unchanged when absent.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes decide on the first `prefix.len()` bytes and stream the rest
/// untouched. Matching is case-sensitive; there is no
/// case-insensitive mode.
/// Used for tag prefix strips such as `v1.2.3` to `1.2.3`.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_trim_prefix_string
)]
#[example(
    name = "strip prefix",
    code = r###"
    # A present prefix falls away; absence returns the input unchanged.
    IMPORT [STD]
    LET $bare: STRING = TEXT_TRIM_PREFIX("v1.2.3", "v")
    ASSERT_EQ $bare "1.2.3"

    # Missing prefixes pass through untouched.
    LET $same: STRING = TEXT_TRIM_PREFIX("1.2.3", "v")
    ASSERT_EQ $same "1.2.3"
    "###
)]
#[example(
    name = "strip prefix pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same strip.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "v1.2.3"
    }
    LET $trimmed: PIPE = TEXT_TRIM($cap)
    LET $out: PIPE = TEXT_TRIM_PREFIX($trimmed, "v")
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "1.2.3"
    "###
)]
#[example(
    name = "strip prefix template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'bare:{{ STD::TEXT_TRIM_PREFIX("v1.2.3", "v") }}'
    ASSERT_EQ $msg "bare:1.2.3"
    "###
)]
fn text_trim_prefix<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to strip.
    #[union(String, Pipe)]
    text: Value,
    /// Leading prefix to remove once.
    prefix: String,
) -> Result<Value> {
    super::args::mirror_stream(
        cx,
        "TEXT_TRIM_PREFIX",
        &text,
        super::args::TextPump::TrimPrefix(prefix),
    )
}

/// Template projection of `TEXT_TRIM_PREFIX`: the same strip for
/// `{{ }}` placeholders, which carry no step IO for pipes.
fn text_trim_prefix_string(text: Value, prefix: String) -> Result<Value> {
    super::args::mirror_string(
        "TEXT_TRIM_PREFIX",
        &text,
        super::args::TextPump::TrimPrefix(prefix),
    )
}

/// Strip one trailing suffix, returning the input unchanged when absent.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes hold a `suffix.len()` trailing window and stream the rest.
/// Matching is case-sensitive; there is no case-insensitive mode.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_trim_suffix_string
)]
#[example(
    name = "strip suffix",
    code = r###"
    # A present suffix falls away; absence returns the input unchanged.
    IMPORT [STD]
    LET $bare: STRING = TEXT_TRIM_SUFFIX("archive.tar.gz", ".gz")
    ASSERT_EQ $bare "archive.tar"

    # Missing suffixes pass through untouched.
    LET $same: STRING = TEXT_TRIM_SUFFIX("archive.tar", ".gz")
    ASSERT_EQ $same "archive.tar"
    "###
)]
#[example(
    name = "strip suffix pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same strip.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "archive.tar.gz"
    }
    LET $trimmed: PIPE = TEXT_TRIM($cap)
    LET $out: PIPE = TEXT_TRIM_SUFFIX($trimmed, ".gz")
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "archive.tar"
    "###
)]
#[example(
    name = "strip suffix template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'bare:{{ STD::TEXT_TRIM_SUFFIX("archive.tar.gz", ".gz") }}'
    ASSERT_EQ $msg "bare:archive.tar"
    "###
)]
fn text_trim_suffix<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to strip.
    #[union(String, Pipe)]
    text: Value,
    /// Trailing suffix to remove once.
    suffix: String,
) -> Result<Value> {
    super::args::mirror_stream(
        cx,
        "TEXT_TRIM_SUFFIX",
        &text,
        super::args::TextPump::TrimSuffix(suffix),
    )
}

/// Template projection of `TEXT_TRIM_SUFFIX`: the same strip for
/// `{{ }}` placeholders, which carry no step IO for pipes.
fn text_trim_suffix_string(text: Value, suffix: String) -> Result<Value> {
    super::args::mirror_string(
        "TEXT_TRIM_SUFFIX",
        &text,
        super::args::TextPump::TrimSuffix(suffix),
    )
}

/// Uppercase text with full Unicode mapping.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes map chunk by chunk across a char-boundary carry, so
/// multibyte characters split across reads still fold correctly.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_to_upper_string
)]
#[example(
    name = "uppercase",
    code = r###"
    # Text folds to uppercase.
    IMPORT [STD]
    LET $loud: STRING = TEXT_TO_UPPER("hello")
    ASSERT_EQ $loud "HELLO"
    "###
)]
#[example(
    name = "uppercase pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same fold.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "hello"
    }
    LET $out: PIPE = TEXT_TO_UPPER($cap)
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "HELLO"
    "###
)]
#[example(
    name = "uppercase template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'loud:{{ STD::TEXT_TO_UPPER("hello") }}'
    ASSERT_EQ $msg "loud:HELLO"
    "###
)]
fn text_to_upper<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to uppercase.
    #[union(String, Pipe)]
    text: Value,
) -> Result<Value> {
    super::args::mirror_stream(cx, "TEXT_TO_UPPER", &text, super::args::TextPump::ToUpper)
}

/// Template projection of `TEXT_TO_UPPER`: the same fold for `{{ }}`
/// placeholders, which carry no step IO for pipes.
fn text_to_upper_string(text: Value) -> Result<Value> {
    super::args::mirror_string("TEXT_TO_UPPER", &text, super::args::TextPump::ToUpper)
}

/// Lowercase text with full Unicode mapping.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes map chunk by chunk across a char-boundary carry, like
/// `TEXT_TO_UPPER`.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_to_lower_string
)]
#[example(
    name = "lowercase",
    code = r###"
    # Text folds to lowercase.
    IMPORT [STD]
    LET $quiet: STRING = TEXT_TO_LOWER("HELLO")
    ASSERT_EQ $quiet "hello"
    "###
)]
#[example(
    name = "lowercase pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same fold.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "HELLO"
    }
    LET $out: PIPE = TEXT_TO_LOWER($cap)
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "hello"
    "###
)]
#[example(
    name = "lowercase template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'quiet:{{ STD::TEXT_TO_LOWER("HELLO") }}'
    ASSERT_EQ $msg "quiet:hello"
    "###
)]
fn text_to_lower<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to lowercase.
    #[union(String, Pipe)]
    text: Value,
) -> Result<Value> {
    super::args::mirror_stream(cx, "TEXT_TO_LOWER", &text, super::args::TextPump::ToLower)
}

/// Template projection of `TEXT_TO_LOWER`: the same fold for `{{ }}`
/// placeholders, which carry no step IO for pipes.
fn text_to_lower_string(text: Value) -> Result<Value> {
    super::args::mirror_string("TEXT_TO_LOWER", &text, super::args::TextPump::ToLower)
}

/// Replace every literal occurrence of `from` with `to`.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Pipes scan through a `from.len()` sliding window, so needles split
/// across chunk reads still rewrite.
/// Literal sliding search, not a pattern: an empty `from` fails
/// closed, and regex work with captures belongs to
/// `TEXT_REPLACE_REGEX`. Matching is case-sensitive; there is no
/// case-insensitive mode.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_replace_string
)]
#[example(
    name = "replace",
    code = r###"
    # Every literal occurrence rewrites in place.
    IMPORT [STD]
    LET $fixed: STRING = TEXT_REPLACE("hello world", "world", "there")
    ASSERT_EQ $fixed "hello there"
    "###
)]
#[example(
    name = "replace pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same rewrite.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "hello world"
    }
    LET $trimmed: PIPE = TEXT_TRIM($cap)
    LET $out: PIPE = TEXT_REPLACE($trimmed, "world", "there")
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "hello there"
    "###
)]
#[example(
    name = "replace template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'fixed:{{ STD::TEXT_REPLACE("hello world", "world", "there") }}'
    ASSERT_EQ $msg "fixed:hello there"
    "###
)]
fn text_replace<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to rewrite.
    #[union(String, Pipe)]
    text: Value,
    /// Literal text to search for; empty fails closed.
    from: String,
    /// Literal replacement text.
    to: String,
) -> Result<Value> {
    super::args::mirror_stream(
        cx,
        "TEXT_REPLACE",
        &text,
        super::args::TextPump::ReplaceLiteral { from, to },
    )
}

/// Template projection of `TEXT_REPLACE`: the same rewrite for `{{ }}`
/// placeholders, which carry no step IO for pipes.
fn text_replace_string(text: Value, from: String, to: String) -> Result<Value> {
    super::args::mirror_string(
        "TEXT_REPLACE",
        &text,
        super::args::TextPump::ReplaceLiteral { from, to },
    )
}

/// Replace every regex match, expanding `$1` and `$name` captures.
///
/// Accepts `STRING` or `PIPE`; the return mirrors the input kind.
/// Eager-only framing: matches span chunks, so pipe inputs stage
/// through the spill substrate instead of streaming.
/// Bad patterns fail closed naming the pattern and its position.
/// Matching is case-sensitive unless the pattern carries an inline
/// `(?i)` flag, which makes uppercase and lowercase letters match
/// each other.
/// The literal counterpart is `TEXT_REPLACE`.
///
#[oxdock_func(
    rpn,
    returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
    template = text_replace_regex_string
)]
#[example(
    name = "replace with captures",
    code = r###"
    # Capture groups expand in the replacement.
    IMPORT [STD]
    LET $swapped: STRING = TEXT_REPLACE_REGEX("2026-10-13", "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1")
    ASSERT_EQ $swapped "13/10/2026"
    "###
)]
#[example(
    name = "replace regex pipe mirrors",
    code = r###"
    # Pipe input mints pipe output with the same rewrite.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "2026-10-13"
    }
    LET $trimmed: PIPE = TEXT_TRIM($cap)
    LET $out: PIPE = TEXT_REPLACE_REGEX($trimmed, "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1")
    WITH_IO [stdin=$out] READ_LINE $line
    ASSERT_EQ $line "13/10/2026"
    "###
)]
#[example(
    name = "replace regex template",
    code = r###"
    # String inputs run inside placeholders too.
    IMPORT [STD]
    LET $msg: STRING = 'swapped:{{ STD::TEXT_REPLACE_REGEX("2026-10-13", "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1") }}'
    ASSERT_EQ $msg "swapped:13/10/2026"
    "###
)]
fn text_replace_regex<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Text to rewrite.
    #[union(String, Pipe)]
    text: Value,
    /// Regex pattern to match.
    pattern: String,
    /// Replacement with `$1` and `$name` capture expansion.
    replacement: String,
) -> Result<Value> {
    if let Some(s) = text.as_str() {
        let out =
            super::args::text_replace_regex_str(s, &pattern, &replacement, "TEXT_REPLACE_REGEX")?;
        return Ok(Value::string(out));
    }
    let content = super::args::read_pipe_eager(cx, "TEXT_REPLACE_REGEX", &text)?;
    let out = super::args::text_replace_regex_str(
        &content,
        &pattern,
        &replacement,
        "TEXT_REPLACE_REGEX",
    )?;
    super::args::write_completed_pipe(cx, "TEXT_REPLACE_REGEX", out)
}

/// Template projection of `TEXT_REPLACE_REGEX`: the same rewrite for
/// `{{ }}` placeholders, which carry no step IO for pipes.
fn text_replace_regex_string(text: Value, pattern: String, replacement: String) -> Result<Value> {
    let Some(s) = text.as_str() else {
        return Err(super::args::pipe_needs_step_io("TEXT_REPLACE_REGEX"));
    };
    let out = super::args::text_replace_regex_str(s, &pattern, &replacement, "TEXT_REPLACE_REGEX")?;
    Ok(Value::string(out))
}

/// Split text on a literal delimiter into a `LIST<STRING>`.
///
/// An empty delimiter splits into single characters. Eager-only by
/// design: splitting into a list has no line framing to stream.
/// Delimiters match case-sensitively; there is no
/// case-insensitive mode.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "split",
    code = r###"
    # Delimiters split into list items.
    IMPORT [STD]
    LET $parts: LIST<STRING> = TEXT_SPLIT("a,b,c", ",")
    ASSERT_EQ $parts.0 "a"
    ASSERT_EQ $parts.1 "b"
    ASSERT_EQ $parts.2 "c"
    "###
)]
fn text_split(
    /// Text to split.
    text: String,
    /// Literal delimiter; empty splits into characters.
    delimiter: String,
) -> Result<Value> {
    Ok(super::args::text_list_value(super::args::text_split_str(
        &text, &delimiter,
    )))
}

/// Join a `LIST<STRING>` with a literal separator into a `STRING`.
///
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
    name = "join",
    code = r###"
    # List items join with the separator between them.
    IMPORT [STD]
    LET $joined: STRING = TEXT_JOIN(["a", "b", "c"], ",")
    ASSERT_EQ $joined "a,b,c"
    "###
)]
fn text_join(
    /// List elements to join.
    elements: Vec<String>,
    /// Literal delimiter placed between elements.
    delimiter: String,
) -> Result<Value> {
    Ok(Value::string(elements.join(delimiter.as_str())))
}

/// Split text into lines with universal newline handling.
///
/// Normalizes through the line-ending crate first so LF, CRLF, CR,
/// and mixed endings all split, then delegates the split itself to
/// `LineEnding::split` instead of hand-rolled scanning.
/// Returns a `LIST<STRING>`; eager-only like `TEXT_SPLIT`.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "lines",
    code = r###"
    # Newlines split into line items.
    IMPORT [STD]
    LET $lines: LIST<STRING> = TEXT_LINES("one\ntwo\nthree")
    ASSERT_EQ $lines.0 "one"
    ASSERT_EQ $lines.1 "two"
    ASSERT_EQ $lines.2 "three"

    # Windows endings split the same way after normalization.
    LET $crlf: LIST<STRING> = TEXT_LINES("one\r\ntwo\r\nthree")
    ASSERT_EQ $crlf.0 "one"
    ASSERT_EQ $crlf.1 "two"
    ASSERT_EQ $crlf.2 "three"

    # Old Mac endings split too, and mixed endings split together because the input normalizes to LF before splitting.
    LET $mixed: LIST<STRING> = TEXT_LINES("a\r\nb\nc\rd")
    ASSERT_EQ $mixed.0 "a"
    ASSERT_EQ $mixed.1 "b"
    ASSERT_EQ $mixed.2 "c"
    ASSERT_EQ $mixed.3 "d"
    "###
)]
fn text_lines(
    /// Text to split into lines.
    text: String,
) -> Result<Value> {
    Ok(super::args::text_list_value(super::args::text_lines_str(
        &text,
    )))
}

/// Split text into whitespace-separated fields, ignoring empties.
///
/// Returns a `LIST<STRING>`; the checksum-line parser that replaces
/// `cut -d' ' -f1` in CI legs.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "fields",
    code = r###"
    # Runs of whitespace separate fields; empties never appear.
    IMPORT [STD]
    LET $fields: LIST<STRING> = TEXT_FIELDS("  abc  def ")
    ASSERT_EQ $fields.0 "abc"
    ASSERT_EQ $fields.1 "def"
    "###
)]
fn text_fields(
    /// Text to split into fields.
    text: String,
) -> Result<Value> {
    Ok(super::args::text_list_value(super::args::text_fields_str(
        &text,
    )))
}

/// Read lines `from` through `to` as a `LIST<STRING>`: the `sed -n
/// 'A,Bp'` window over universal newlines, for reading one file
/// section without holding the surrounding text.
///
/// Lines are 1-based and inclusive on both ends. Ranges past the end
/// clamp like sed instead of failing; a start before line 1 or past
/// `to` fails closed, since no line reading satisfies it.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "line range",
    code = r###"
    # Lines 2 through 3 come back as a list, 1-based inclusive.
    IMPORT [STD]
    LET $window: LIST<STRING> = TEXT_LINES_RANGE("one\ntwo\nthree\nfour", 2, 3)
    ASSERT_EQ $window.0 "two"
    ASSERT_EQ $window.1 "three"

    # Ranges past the end clamp instead of failing.
    LET $tail: LIST<STRING> = TEXT_LINES_RANGE("one\ntwo", 2, 9)
    ASSERT_EQ $tail.0 "two"
    "###
)]
fn text_lines_range(
    /// Text whose lines to window.
    text: String,
    /// First line, 1-based inclusive.
    from: i64,
    /// Last line, inclusive; past the end clamps.
    to: i64,
) -> Result<Value> {
    Ok(super::args::text_list_value(
        super::args::text_lines_range_str(&text, from, to, "TEXT_LINES_RANGE")?,
    ))
}

/// Report whether text matches a regex pattern.
///
/// Returns `BOOL`. Bad patterns fail closed naming the pattern and
/// its position. Matching is case-sensitive unless the pattern
/// carries an inline `(?i)` flag, which makes uppercase and lowercase
/// letters match each other.
///
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
    name = "match",
    code = r###"
    # Regex matches report true; misses report false.
    IMPORT [STD]
    LET $hit: BOOL = TEXT_MATCH("abc123", "\\d+")
    ASSERT_EQ $hit true

    # A miss is false, not a failure.
    LET $miss: BOOL = TEXT_MATCH("abc", "^\\d+$")
    ASSERT_EQ $miss false
    "###
)]
fn text_match(
    /// Text to test.
    text: String,
    /// Regex pattern to match.
    pattern: String,
) -> Result<Value> {
    let re = super::args::text_compile_pattern("TEXT_MATCH", &pattern)?;
    Ok(Value::bool(re.is_match(&text)))
}

/// Extract capture groups for the first regex match.
///
/// Returns a `LIST<STRING>` where index 0 is the full match and the
/// rest are groups in order; non-participating groups bind empty so
/// indexes stay stable. No match binds an empty list. Matching is
/// case-sensitive unless the pattern carries an inline `(?i)` flag,
/// which makes uppercase and lowercase letters match each other.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "extract",
    code = r###"
    # Index zero is the full match; groups follow in order.
    IMPORT [STD]
    LET $caps: LIST<STRING> = TEXT_EXTRACT("abc123", "([a-z]+)(\\d+)")
    ASSERT_EQ $caps.0 "abc123"
    ASSERT_EQ $caps.1 "abc"
    ASSERT_EQ $caps.2 "123"
    "###
)]
fn text_extract(
    /// Text to search.
    text: String,
    /// Regex pattern with capture groups.
    pattern: String,
) -> Result<Value> {
    let groups = super::args::text_extract_str(&text, &pattern, "TEXT_EXTRACT")?;
    Ok(super::args::text_list_value(groups))
}

/// Report whether text starts with a literal prefix.
///
/// Returns `BOOL`; the literal check behind tag and target branching
/// such as `matrix.target` suffix and prefix tests. Matching is
/// case-sensitive; there is no case-insensitive mode.
///
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
    name = "starts with",
    code = r###"
    # Present prefixes report true; misses report false.
    IMPORT [STD]
    LET $hit: BOOL = TEXT_STARTS_WITH("linux-musl-x64", "linux")
    ASSERT_EQ $hit true

    # A miss is false, not a failure.
    LET $miss: BOOL = TEXT_STARTS_WITH("macos-arm", "linux")
    ASSERT_EQ $miss false
    "###
)]
fn text_starts_with(
    /// Text to test.
    text: String,
    /// Literal prefix to look for.
    prefix: String,
) -> Result<Value> {
    Ok(Value::bool(text.starts_with(prefix.as_str())))
}

/// Report whether text ends with a literal suffix.
///
/// Returns `BOOL`; the `endsWith(matrix.target, 'linux-musl')`
/// check in CI legs. Matching is case-sensitive; there is no
/// case-insensitive mode.
///
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
    name = "ends with",
    code = r###"
    # Present suffixes report true; misses report false.
    IMPORT [STD]
    LET $hit: BOOL = TEXT_ENDS_WITH("x86_64-linux-musl", "linux-musl")
    ASSERT_EQ $hit true

    # A miss is false, not a failure.
    LET $miss: BOOL = TEXT_ENDS_WITH("x86_64-linux-gnu", "linux-musl")
    ASSERT_EQ $miss false
    "###
)]
fn text_ends_with(
    /// Text to test.
    text: String,
    /// Literal suffix to look for.
    suffix: String,
) -> Result<Value> {
    Ok(Value::bool(text.ends_with(suffix.as_str())))
}

/// Report whether text holds a literal substring.
///
/// Returns `BOOL`; the substring search behind target filtering
/// alongside the prefix and suffix tests. Matching is
/// case-sensitive; there is no case-insensitive mode.
///
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
    name = "contains",
    code = r###"
    # Present needles report true; misses report false.
    IMPORT [STD]
    LET $hit: BOOL = TEXT_CONTAINS("x86_64-linux-musl", "musl")
    ASSERT_EQ $hit true

    # A miss is false, not a failure.
    LET $miss: BOOL = TEXT_CONTAINS("x86_64-linux-gnu", "musl")
    ASSERT_EQ $miss false
    "###
)]
fn text_contains(
    /// Text to search.
    text: String,
    /// Literal substring to look for.
    needle: String,
) -> Result<Value> {
    Ok(Value::bool(text.contains(needle.as_str())))
}

/// Count the characters in text as Unicode scalars.
///
/// Multibyte characters count once each, so this is not the byte
/// length: `"héllo"` binds 5, not 6. The `${#var}` and `wc -m`
/// counterpart for scripts that size tags and digests.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "length",
    code = r###"
    # Plain text counts one per character.
    IMPORT [STD]
    LET $n: INT = TEXT_LENGTH("hello")
    ASSERT_EQ $n 5

    # Multibyte characters count once each, not per byte.
    LET $wide: INT = TEXT_LENGTH("héllo")
    ASSERT_EQ $wide 5
    "###
)]
fn text_length(
    /// Text to measure.
    text: String,
) -> Result<Value> {
    Ok(Value::int(text.chars().count() as i64))
}

/// Count non-overlapping literal occurrences of a needle.
///
/// Matches never share bytes, so `"aaa"` holds one `"aa"`, matching
/// `grep -o 'aa' | wc -l`. An empty needle fails closed instead of
/// binding every character boundary. Matching is case-sensitive;
/// there is no case-insensitive mode.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "count",
    code = r###"
    # Every non-overlapping occurrence counts once.
    IMPORT [STD]
    LET $n: INT = TEXT_COUNT("hello world", "o")
    ASSERT_EQ $n 2

    # Overlapping candidates never double count.
    LET $once: INT = TEXT_COUNT("aaa", "aa")
    ASSERT_EQ $once 1
    "###
)]
fn text_count(
    /// Text to search.
    text: String,
    /// Literal needle to count; empty fails closed.
    needle: String,
) -> Result<Value> {
    let n = super::args::text_count_str(&text, &needle, "TEXT_COUNT")?;
    Ok(Value::int(n))
}

/// Count lines matching a regex pattern.
///
/// Lines split with universal newline handling, like `TEXT_LINES`,
/// and each line reports independently: this is `grep -c`, so a line
/// with two matches still counts once. Bad patterns fail closed
/// naming the pattern and its position. Matching is case-sensitive
/// unless the pattern carries an inline `(?i)` flag, which makes
/// uppercase and lowercase letters match each other.
///
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
    name = "count matches",
    code = r###"
    # One line matches once no matter how many times it hits.
    IMPORT [STD]
    LET $n: INT = TEXT_COUNT_MATCHES("test a: test\ntest b: nope\ntest c: test", ": test")
    ASSERT_EQ $n 2

    # A miss binds zero, never an error.
    LET $none: INT = TEXT_COUNT_MATCHES("test b: nope", ": test$")
    ASSERT_EQ $none 0
    "###
)]
fn text_count_matches(
    /// Text whose lines to test.
    text: String,
    /// Regex pattern tested per line.
    pattern: String,
) -> Result<Value> {
    let n = super::args::text_count_matches_str(&text, &pattern, "TEXT_COUNT_MATCHES")?;
    Ok(Value::int(n))
}

/// Keep the lines matching a regex pattern.
///
/// Splits with universal newline handling, like `TEXT_LINES`, and
/// returns every matching line in order: plain `grep`, so a miss
/// binds an empty list instead of failing. A trailing options MAP
/// carries `invert`: `TEXT_GREP($text, $pattern, {invert: true})`
/// keeps every non-matching line instead, the `grep -v` shape. The
/// MAP omits freely; a missing MAP keeps matches. Matching is
/// case-sensitive unless the pattern carries an inline `(?i)` flag,
/// which makes uppercase and lowercase letters match each other. Bad
/// patterns fail closed naming the pattern and its position.
///
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
    name = "grep",
    code = r###"
    # Matching lines come back as a list, in order.
    IMPORT [STD]
    LET $hits: LIST<STRING> = TEXT_GREP("one\ntwo\none more", "^one")
    ASSERT_EQ $hits.0 "one"
    ASSERT_EQ $hits.1 "one more"

    # A miss binds an empty list, never an error.
    LET $miss: LIST<STRING> = TEXT_GREP("one\ntwo", "^z")
    LET $joined: STRING = TEXT_JOIN($miss, ",")
    ASSERT_EQ $joined ""

    # The (?i) flag ignores case, so uppercase lines match too.
    LET $folded: LIST<STRING> = TEXT_GREP("One\ntwo\nONE more", "(?i)^one")
    ASSERT_EQ $folded.0 "One"
    ASSERT_EQ $folded.1 "ONE more"
    "###
)]
#[example(
    name = "grep invert",
    code = r###"
    # The invert option keeps every non-matching line instead.
    IMPORT [STD]
    LET $kept: LIST<STRING> = TEXT_GREP("one\ntwo\none more", "^one", {invert: true})
    ASSERT_EQ $kept.0 "two"

    # A pattern matching every line binds an empty list, never an error.
    LET $none: LIST<STRING> = TEXT_GREP("one\ntwo", "^", {invert: true})
    LET $joined: STRING = TEXT_JOIN($none, ",")
    ASSERT_EQ $joined ""

    # Options ride into placeholders as MAP literals. Placeholder
    # strings unescape only backslash and quote, so multiline text
    # arrives through a variable.
    LET $two_lines: STRING = "one\ntwo"
    LET $msg: STRING = 'kept:{{ STD::TEXT_GREP($two_lines, "^one", {invert: true}) }}'
    ASSERT_EQ $msg "kept:two"
    "###
)]
fn text_grep(
    /// Text whose lines to filter.
    text: String,
    /// Regex pattern keeping a line.
    pattern: String,
    /// Filter settings: `invert`. Omittable: a missing options MAP fills `{}`.
    #[options("invert?: BOOL")]
    #[optional]
    options: BTreeMap<String, Value>,
) -> Result<Value> {
    // `check_options` in the wrapper verified the shape above (keys
    // and value types); the read below narrows infallibly after.
    let invert = super::args::text_invert_flag(&options);
    Ok(super::args::text_list_value(super::args::text_grep_str(
        &text,
        &pattern,
        invert,
        "TEXT_GREP",
    )?))
}

/// True when a pipe sits at end of stream: closed with nothing buffered,
/// so the next `READ_LINE` would bind `""` via EOF rather than a line.
/// Live writers, pinned keepers, and buffered bytes all answer false.
/// Never blocks: a reader already blocked stays blocked, so branch on
/// `EOF` before reading, not after. OS pairs and unbound handles answer
/// best-effort (kernel bytes are invisible there; see `INSPECT`).
/// Non-pipe arguments bail at the boundary: the parameter declares
/// `PIPE`, so the extractor and the static pass reject them before
/// the body runs.
///
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
    name = "drain to end",
    code = r###"
    # Capture two lines, then drain to end of stream with no sentinel line.
    IMPORT [STD]
    LET $cap: PIPE
    WITH_IO [stdout=$cap] {
        ECHO "one"
        ECHO "two"
    }

    # The loop exits on close, and the count proves both lines drained.
    LET $n: INT = 0
    WHILE !EOF($cap) {
        WITH_IO [stdin=$cap] READ_LINE $line
        $n = $n + 1
    }
    ASSERT_EQ $n 2
    "###
)]
fn eof<P: ProcessManager>(
    cx: &mut StepCtx<P>,
    /// Pipe handle to query for end of stream.
    pipe: PipeHandle,
) -> Result<Value> {
    let info = cx.state.io.inspect_pipe(&pipe);
    Ok(Value::bool(info.closed && info.buffered == 0))
}

fn meta_to_value(meta: &FuncMeta) -> Value {
    let mut map = BTreeMap::new();
    map.insert("name".to_string(), Value::string(meta.name.clone()));
    map.insert("module".to_string(), Value::string(meta.module.clone()));
    map.insert(
        "kind".to_string(),
        Value::string(meta.kind.label().to_string()),
    );
    let params = match &meta.params {
        Some(params) => Value::list(
            params
                .iter()
                .map(|p| {
                    let mut entry = BTreeMap::new();
                    entry.insert("name".to_string(), Value::string(p.name.clone()));
                    entry.insert(
                        "param_type".to_string(),
                        Value::string(
                            p.param_type
                                .as_ref()
                                .map(render_structural)
                                .unwrap_or_default(),
                        ),
                    );
                    entry.insert(
                        "allowed".to_string(),
                        Value::string(
                            p.allowed
                                .map(|values| values.join(", "))
                                .unwrap_or_default(),
                        ),
                    );
                    entry.insert("docs".to_string(), Value::string(String::new()));
                    entry.insert("optional".to_string(), Value::bool(p.optional));
                    entry.insert(
                        "options".to_string(),
                        match p.options {
                            Some(keys) => Value::list(
                                keys.iter()
                                    .map(|key| {
                                        let mut option = BTreeMap::new();
                                        option.insert(
                                            "name".to_string(),
                                            Value::string(key.name.to_string()),
                                        );
                                        option.insert(
                                            "value_type".to_string(),
                                            Value::string(render_structural(&key.value)),
                                        );
                                        option.insert(
                                            "required".to_string(),
                                            Value::bool(key.required),
                                        );
                                        option.insert(
                                            "default".to_string(),
                                            Value::string(
                                                key.default.unwrap_or_default().to_string(),
                                            ),
                                        );
                                        Value::map(option)
                                    })
                                    .collect(),
                            ),
                            None => Value::string(String::new()),
                        },
                    );
                    Value::map(entry)
                })
                .collect(),
        ),
        // Parameterless functions mint an empty LIST, never an empty
        // STRING: the `DESCRIBE` return tag promises `LIST<MAP<ANY>>`
        // for every entry, and the boundary enforces it.
        None => Value::list(Vec::new()),
    };
    map.insert("params".to_string(), params);
    map.insert(
        "returns".to_string(),
        Value::string(
            meta.returns
                .as_ref()
                .map(render_structural)
                .unwrap_or_default(),
        ),
    );
    map.insert("rpn".to_string(), Value::bool(meta.rpn));
    map.insert(
        "summary".to_string(),
        Value::string(meta.summary.to_string()),
    );
    Value::map(map)
}

/// One named record schema: a shape host functions produce and scripts
/// bind by name (`LET $s: SSH_SESSION_INFO`). Travels with its module
/// so schemas register exactly when their producer does.
#[derive(Debug, Clone)]
pub struct RecordSchema {
    pub name: &'static str,
    pub fields: std::sync::Arc<[Field]>,
}

/// One host library: functions and types registered under a single module
/// name. `Engine::register_module` stages these; runs expose them as
/// `MODULE::NAME` calls with `MODULE` provenance on every entry.
#[derive(Clone)]
pub struct HostModule<P: ProcessManager> {
    pub name: String,
    pub funcs: Vec<HostRegistration<P>>,
    pub types: Vec<&'static TypeDescriptor>,
    pub record_schemas: Vec<RecordSchema>,
    /// Shared module state (endpoint registries and the like), stashed
    /// at registration and reachable from bodies through
    /// `StepCtx::module_state`. Lets every entry stay `#[oxdock_func]`:
    /// the macro only generates closers-over-nothing, so state that
    /// cannot ride in a value travels here instead of a hand-built
    /// closure. Prefer `.with_state(...)` over spelling this field.
    pub states: Vec<ModuleState>,
}

/// One shared state object traveling with its module. Keyed by the
/// owning module name plus the value type at registration, so two
/// modules sharing one state type never collide.
#[derive(Clone)]
pub struct ModuleState {
    id: std::any::TypeId,
    state: std::sync::Arc<dyn std::any::Any + Send + Sync>,
}

/// A pre-run steps inspection: pure function of the parsed steps,
///
/// shared so the struct field stays under the complexity lint.
pub type PreRunInspector = dyn Fn(&[Step]) -> Result<()> + Send + Sync;

/// A pre-run check riding in a module's `states`: the runner executes
/// every staged check against the parsed steps before the first step
/// runs, so unmapped services fail fast with zero side effects. Modules
/// attach one with `.with_state(Arc::new(check))`; the runner discovers
/// them by type, so no struct field or plugin knowledge is needed here.
#[derive(Clone)]
pub struct PreRunCheck {
    check: Arc<PreRunInspector>,
}

impl PreRunCheck {
    /// Wrap a steps inspector as a pre-run check.
    pub fn new(check: impl Fn(&[Step]) -> Result<()> + Send + Sync + 'static) -> Self {
        Self {
            check: Arc::new(check),
        }
    }

    /// Run the inspection: `Ok` lets execution start.
    pub fn run(&self, steps: &[Step]) -> Result<()> {
        (self.check)(steps)
    }
}

impl<P: ProcessManager> HostModule<P> {
    /// Attach shared state for bodies to fetch with
    /// `cx.module_state::<T>("MODULE")`. Chainable, like registration.
    pub fn with_state<T: Send + Sync + 'static>(mut self, state: std::sync::Arc<T>) -> Self {
        self.states.push(ModuleState {
            id: std::any::TypeId::of::<T>(),
            state,
        });
        self
    }

    /// Run every staged [`PreRunCheck`] against already-parsed steps.
    /// The runner calls this before the first step executes; the first
    /// failing check aborts the run with no side effects.
    pub fn check_pre_run(&self, steps: &[Step]) -> Result<()> {
        for state in &self.states {
            if let Some(check) = state.state.downcast_ref::<PreRunCheck>() {
                check.run(steps)?;
            }
        }
        Ok(())
    }
}

impl<P: ProcessManager> ExecState<P> {
    /// Register one [`HostModule`]: every function becomes callable as
    /// `MODULE::NAME`, every type joins the run's name directory.
    pub fn register_module(&mut self, module: HostModule<P>) {
        for registration in module.funcs {
            match registration {
                HostRegistration::Stateful { name, meta, func } => {
                    self.functions.register_host(&module.name, name, meta, func);
                }
                HostRegistration::Pure { name, meta, func } => {
                    self.functions
                        .register_pure_host(&module.name, name, meta, func);
                }
                HostRegistration::Both {
                    name,
                    meta,
                    func,
                    template,
                } => {
                    self.functions
                        .register_both_host(&module.name, name, meta, func, template);
                }
            }
        }
        for descriptor in module.types {
            self.register_type(descriptor);
        }
        for schema in module.record_schemas {
            self.register_record_schema(schema.name, schema.fields);
        }
        for state in module.states {
            self.module_states
                .insert((module.name.clone(), state.id), state.state);
        }
    }

    /// All visible functions: natives plus hosts plus current DSL definitions.
    pub fn list_functions(&self) -> Vec<FuncMeta> {
        let mut out: Vec<FuncMeta> = self.functions.entries_metas().into_iter().collect();
        // TODO: Make a "virtual function" and don't hardcode
        if !out.iter().any(|m| m.name == KEYWORD_INSPECT) {
            out.push(FuncMeta {
                name: KEYWORD_INSPECT.to_string(),
                module: STD_MODULE_NAME.to_string(),
                kind: FuncKind::HostCtx,
                params: None,
                returns: Some(TypeTag::Map),
                rpn: false,
                summary: "Inspect a variable binding.",
                docs: "INSPECT($var): dedicated AST node taking a variable, not a value.",
                examples: &[],
            });
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    /// Describe one function by name, or `None` when unknown.
    pub fn describe_function(&self, name: &str) -> Option<Value> {
        if let Some(meta) = self.functions.meta(name) {
            return Some(meta_to_value(&meta));
        }
        // TODO: Make a "virtual function" and don't hardcode
        if name == KEYWORD_INSPECT {
            return Some(meta_to_value(&FuncMeta {
                name: KEYWORD_INSPECT.to_string(),
                module: STD_MODULE_NAME.to_string(),
                kind: FuncKind::HostCtx,
                params: None,
                returns: Some(TypeTag::Map),
                rpn: false,
                summary: "Inspect a variable binding.",
                docs: "INSPECT($var): dedicated AST node taking a variable, not a value.",
                examples: &[],
            }));
        }
        None
    }

    pub(super) fn clone_native_pure(&self, name: &str) -> Option<PureFn> {
        self.functions.clone_pure_fn(name)
    }

    /// Shared snapshot of every registered pure function for
    /// placeholder and host driven expansion. Public so host crates
    /// resolve calls through the live registry instead of hardcoding
    /// module or function names. Cloning the `Arc` never copies the map.
    pub fn pure_function_table(&self) -> Arc<PureTable> {
        self.functions.pure_shared()
    }

    pub(super) fn clone_native_ctx(&self, name: &str) -> Option<NativeFn<P>> {
        self.functions.clone_ctx_fn(name)
    }

    pub(super) fn native_meta(&self, name: &str) -> Option<FuncMeta> {
        self.functions.meta(name)
    }
}

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

    #[test]
    fn describe_entry_renders_union_spellings() {
        // Union params and returns render through the same structural
        // renderer every other tag uses: `DESCRIBE` (and the generated
        // reference built on it) names the admission set, never a
        // bare word or an empty string. Built by macro, so the
        // spelling cannot drift from the members.
        let union = oxdock_parser::union_tag!(String, Pipe);
        let meta = FuncMeta {
            name: "T::F".to_string(),
            module: "T".to_string(),
            kind: FuncKind::HostPure,
            params: Some(vec![FuncParam {
                name: "text".to_string(),
                param_type: Some(union.clone()),
                allowed: None,
                docs: "",
                options: None,
                optional: false,
            }]),
            returns: Some(union),
            rpn: false,
            summary: "test-only",
            docs: "test-only",
            examples: &[],
        };
        let value = meta_to_value(&meta);
        let map = value.as_map().expect("describe value is a map");
        let params = map
            .get("params")
            .expect("params key")
            .as_list()
            .expect("params is a list");
        let first = params[0].as_map().expect("entry is a map");
        assert_eq!(
            first.get("param_type").and_then(Value::as_str),
            Some("STRING|PIPE"),
            "param renders the set"
        );
        assert_eq!(
            map.get("returns").and_then(Value::as_str),
            Some("STRING|PIPE"),
            "returns render the set"
        );
    }

    #[test]
    fn describe_entry_carries_optional_flag() {
        // The `optional` key on params entries is the machine-readable
        // half of the `?` rendering: pin both values through the same
        // builder the runtime serves, not through prose.
        let meta = FuncMeta {
            name: "T::F".to_string(),
            module: "T".to_string(),
            kind: FuncKind::HostPure,
            params: Some(vec![
                FuncParam {
                    name: "url".to_string(),
                    param_type: Some(TypeTag::String),
                    allowed: None,
                    docs: "",
                    options: None,
                    optional: false,
                },
                FuncParam {
                    name: "options".to_string(),
                    param_type: Some(TypeTag::Map),
                    allowed: None,
                    docs: "",
                    options: None,
                    optional: true,
                },
            ]),
            returns: Some(TypeTag::String),
            rpn: false,
            summary: "test-only",
            docs: "test-only",
            examples: &[],
        };
        let value = meta_to_value(&meta);
        let map = value.as_map().expect("describe value is a map");
        let params = map
            .get("params")
            .expect("params key")
            .as_list()
            .expect("params is a list");
        assert_eq!(params.len(), 2);
        let first = params[0].as_map().expect("entry is a map");
        let second = params[1].as_map().expect("entry is a map");
        assert_eq!(
            first.get("optional").and_then(Value::as_bool),
            Some(false),
            "required param must read false"
        );
        assert_eq!(
            second.get("optional").and_then(Value::as_bool),
            Some(true),
            "omittable param must read true"
        );
    }

    #[test]
    fn template_twins_mirror_string_paths_and_refuse_pipes() {
        // Every template twin answers strings through the shared pump
        // semantics and refuses pipes naming the step IO they need:
        // placeholder evaluation carries no step context to pump
        // through. Table-driven so a twinless mirror op fails here
        // naming it.
        let pipe = Value::pipe_fresh();
        type TwinCase = (&'static str, Box<dyn Fn() -> Result<Value>>);
        let cases: Vec<TwinCase> = vec![
            (
                "TEXT_TRIM",
                Box::new(|| text_trim_string(Value::string("  hello  ".to_string()))),
            ),
            (
                "TEXT_TRIM_PREFIX",
                Box::new(|| {
                    text_trim_prefix_string(Value::string("v1".to_string()), "v".to_string())
                }),
            ),
            (
                "TEXT_TRIM_SUFFIX",
                Box::new(|| {
                    text_trim_suffix_string(Value::string("a.gz".to_string()), "x".to_string())
                }),
            ),
            (
                "TEXT_TO_UPPER",
                Box::new(|| text_to_upper_string(Value::string("hi".to_string()))),
            ),
            (
                "TEXT_TO_LOWER",
                Box::new(|| text_to_lower_string(Value::string("HI".to_string()))),
            ),
            (
                "TEXT_REPLACE",
                Box::new(|| {
                    text_replace_string(
                        Value::string("ab".to_string()),
                        "b".to_string(),
                        "c".to_string(),
                    )
                }),
            ),
            (
                "TEXT_REPLACE_REGEX",
                Box::new(|| {
                    text_replace_regex_string(
                        Value::string("ab".to_string()),
                        "b".to_string(),
                        "c".to_string(),
                    )
                }),
            ),
        ];
        assert_eq!(cases.len(), 7, "every mirror op needs a twin row");
        for (name, run) in &cases {
            let value = run().unwrap_or_else(|e| panic!("{name} twin must answer strings: {e:#}"));
            assert!(
                value.as_str().is_some(),
                "{name} twin must answer STRING, got {}",
                value.type_name()
            );
        }
        assert_eq!(
            text_trim_string(Value::string("  hello  ".to_string()))
                .expect("trim twin")
                .as_str(),
            Some("hello")
        );
        // Pipes refuse through the shared message on every twin.
        let pipe_cases: Vec<(&str, Result<Value>)> = vec![
            ("TEXT_TRIM", text_trim_string(pipe.clone())),
            (
                "TEXT_TRIM_PREFIX",
                text_trim_prefix_string(pipe.clone(), "v".to_string()),
            ),
            (
                "TEXT_TRIM_SUFFIX",
                text_trim_suffix_string(pipe.clone(), ".gz".to_string()),
            ),
            ("TEXT_TO_UPPER", text_to_upper_string(pipe.clone())),
            ("TEXT_TO_LOWER", text_to_lower_string(pipe.clone())),
            (
                "TEXT_REPLACE",
                text_replace_string(pipe.clone(), "a".to_string(), "b".to_string()),
            ),
            (
                "TEXT_REPLACE_REGEX",
                text_replace_regex_string(pipe.clone(), "a".to_string(), "b".to_string()),
            ),
        ];
        for (name, result) in pipe_cases {
            let err = result.expect_err(&format!("{name} twin must refuse PIPE"));
            assert!(
                format!("{err:#}").contains("step IO"),
                "{name} refusal names the missing context: {err:#}"
            );
        }
    }
}