relon-cli 0.1.0-rc2

Command line interface for the Relon language
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
#![forbid(unsafe_code)]

use clap::{Parser, Subcommand, ValueEnum};
use miette::{IntoDiagnostic, LabeledSpan, NamedSource, Report};
// Public surface lives on the `relon` facade: `Evaluator` (the
// backend-agnostic trait), `Scope` / `Value` (the canonical runtime
// data shapes), plus the `ResolverChainLoader` already routed here.
// Runtime-impl types (`Context`, `TreeWalkEvaluator`,
// `FilesystemModuleResolver`, `Capabilities`) remain direct reach
// into `relon-evaluator` — the CLI's cold-start fast paths (`--lite`,
// trivial-`#main` auto-detect, cache probing) need the lower-level
// surface the facade deliberately doesn't expose, and the CLI
// already declares a direct dep on `relon-evaluator` for them.
use relon::{Evaluator as EvaluatorTrait, ResolverChainLoader, Scope, Value};
use relon_analyzer::{
    analyze_entry_with_options, analyze_with_options, format_type, substitute_generics_in_typenode,
    AnalyzeOptions, AnalyzedTree, MainSignature, SchemaDef, WorkspaceDiagnostic, WorkspaceTree,
};
use relon_codegen_cranelift::AotEvaluator;
use relon_evaluator::module::FilesystemModuleResolver;
use relon_evaluator::{Capabilities, Context, ResourceBudget, TreeWalkEvaluator};
use relon_parser::{parse_document, ParseDocumentError, TypeNode};
use serde_json::{json, Value as JsonValue};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Mirror of [`relon::Backend`] surfaced as a clap-friendly enum so the
/// CLI accepts `--backend=auto` / `--backend=tree-walk` /
/// `--backend=cranelift-aot`. Kept separate from the public type so the
/// rename / display knobs stay CLI-only — the library facade picks
/// ergonomic Rust names while the CLI sticks with kebab-case for
/// muscle-memory.
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
enum BackendArg {
    /// Auto-tier (default). Routes `run_main` through cranelift-AOT
    /// on demand (lazily constructed on first call) and every other
    /// `Evaluator` method through the tree-walker. Inherits the
    /// tree-walker's full surface when the script never asks for
    /// `run_main`; pays the AOT cold-start exactly when needed.
    #[default]
    #[value(name = "auto")]
    Auto,
    /// Tree-walking interpreter only. Supports the full
    /// `Evaluator` surface including arbitrary `eval` on AST nodes
    /// and host-registered native fns.
    #[value(name = "tree-walk")]
    TreeWalk,
    /// Cranelift-native AOT backend. Lowers the entry into native
    /// machine code via cranelift's JIT and dispatches `run_main`
    /// through a panic-shielded trampoline. Library-mode `eval_root`
    /// is rejected — this backend only ships entries.
    #[value(name = "cranelift-aot")]
    CraneliftAot,
}

/// Resource-budget profile for `relon run`. Profiles are CLI policy,
/// not Relon source policy: the operator chooses them at the call site.
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
enum BudgetArg {
    /// Preserve the historical default: no CLI-installed evaluator or
    /// output budget.
    #[default]
    #[value(name = "off")]
    Off,
    /// Local development guardrails: enough room for normal configs,
    /// but catches accidental runaway evaluation and huge output.
    #[value(name = "dev")]
    Dev,
    /// Tighter guardrails for externally supplied configs. This is a
    /// convenience profile, not a VM security boundary.
    #[value(name = "untrusted")]
    Untrusted,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct RunBudget {
    max_source_bytes: Option<usize>,
    max_steps: Option<u64>,
    max_value_elements: Option<usize>,
    max_output_bytes: Option<usize>,
}

struct RunCommand {
    file: PathBuf,
    pretty: bool,
    lite: bool,
    args: Option<String>,
    trust: bool,
    backend: BackendArg,
    budget: BudgetArg,
    max_source_bytes: Option<usize>,
    max_steps: Option<u64>,
    max_value_elements: Option<usize>,
    max_output_bytes: Option<usize>,
    require_hash: bool,
}

impl RunBudget {
    const DEV_MAX_SOURCE_BYTES: usize = 1024 * 1024;
    const DEV_MAX_STEPS: u64 = ResourceBudget::DEV_MAX_STEPS;
    const DEV_MAX_VALUE_ELEMENTS: usize = ResourceBudget::DEV_MAX_VALUE_ELEMENTS;
    const DEV_MAX_OUTPUT_BYTES: usize = 16 * 1024 * 1024;
    const UNTRUSTED_MAX_SOURCE_BYTES: usize = 256 * 1024;
    const UNTRUSTED_MAX_STEPS: u64 = ResourceBudget::UNTRUSTED_MAX_STEPS;
    const UNTRUSTED_MAX_VALUE_ELEMENTS: usize = ResourceBudget::UNTRUSTED_MAX_VALUE_ELEMENTS;
    const UNTRUSTED_MAX_OUTPUT_BYTES: usize = 8 * 1024 * 1024;

    fn from_cli(
        profile: BudgetArg,
        max_source_bytes: Option<usize>,
        max_steps: Option<u64>,
        max_value_elements: Option<usize>,
        max_output_bytes: Option<usize>,
    ) -> Self {
        let mut budget = match profile {
            BudgetArg::Off => Self::default(),
            BudgetArg::Dev => Self {
                max_source_bytes: Some(Self::DEV_MAX_SOURCE_BYTES),
                max_steps: Some(Self::DEV_MAX_STEPS),
                max_value_elements: Some(Self::DEV_MAX_VALUE_ELEMENTS),
                max_output_bytes: Some(Self::DEV_MAX_OUTPUT_BYTES),
            },
            BudgetArg::Untrusted => Self {
                max_source_bytes: Some(Self::UNTRUSTED_MAX_SOURCE_BYTES),
                max_steps: Some(Self::UNTRUSTED_MAX_STEPS),
                max_value_elements: Some(Self::UNTRUSTED_MAX_VALUE_ELEMENTS),
                max_output_bytes: Some(Self::UNTRUSTED_MAX_OUTPUT_BYTES),
            },
        };
        if let Some(max_source_bytes) = max_source_bytes {
            budget.max_source_bytes = Some(max_source_bytes);
        }
        if let Some(max_steps) = max_steps {
            budget.max_steps = Some(max_steps);
        }
        if let Some(max_value_elements) = max_value_elements {
            budget.max_value_elements = Some(max_value_elements);
        }
        if let Some(max_output_bytes) = max_output_bytes {
            budget.max_output_bytes = Some(max_output_bytes);
        }
        budget
    }

    fn has_evaluator_limits(self) -> bool {
        self.max_steps.is_some() || self.max_value_elements.is_some()
    }

    fn apply_to_capabilities(self, caps: &mut Capabilities) {
        if let Some(max_steps) = self.max_steps {
            caps.max_steps = Some(max_steps);
        }
        if let Some(max_value_elements) = self.max_value_elements {
            caps.max_value_elements = Some(max_value_elements);
        }
    }
}

/// Host-runtime policy target for VM-style deployments. The CLI emits
/// policy material; the host remains responsible for applying it to the
/// actual runtime.
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
enum HostPolicyTargetArg {
    /// Wasmtime engine/store limits for Relon wasm execution.
    #[default]
    #[value(name = "wasmtime")]
    Wasmtime,
}

/// Built-in VM host policy profiles. These are deliberately CLI policy
/// profiles, not Relon source-level configuration.
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
enum HostPolicyProfileArg {
    /// Local development guardrails with enough headroom for interactive
    /// debugging.
    #[value(name = "dev")]
    Dev,
    /// Tighter default for externally supplied scripts running inside a VM.
    #[default]
    #[value(name = "untrusted")]
    Untrusted,
}

impl HostPolicyProfileArg {
    fn as_str(self) -> &'static str {
        match self {
            Self::Dev => "dev",
            Self::Untrusted => "untrusted",
        }
    }
}

/// Output format for `relon host-policy`.
#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
enum HostPolicyFormatArg {
    /// Machine-readable policy values for CI/deployment tooling.
    #[default]
    #[value(name = "json")]
    Json,
    /// Rust template showing where the generated limits attach to Wasmtime.
    #[value(name = "rust")]
    Rust,
}

#[derive(Debug, Clone, Copy)]
struct WasmtimeHostPolicy {
    profile: HostPolicyProfileArg,
    fuel: u64,
    epoch_deadline_ticks: u64,
    wall_clock_timeout_ms: u64,
    memory_size_bytes: usize,
    table_elements: usize,
    instances: usize,
    tables: usize,
    memories: usize,
    output_bytes: usize,
}

impl WasmtimeHostPolicy {
    fn from_profile(profile: HostPolicyProfileArg) -> Self {
        match profile {
            HostPolicyProfileArg::Dev => Self {
                profile,
                fuel: RunBudget::DEV_MAX_STEPS,
                epoch_deadline_ticks: 1,
                wall_clock_timeout_ms: 5_000,
                memory_size_bytes: 64 * 1024 * 1024,
                table_elements: 10_000,
                instances: 1,
                tables: 8,
                memories: 1,
                output_bytes: RunBudget::DEV_MAX_OUTPUT_BYTES,
            },
            HostPolicyProfileArg::Untrusted => Self {
                profile,
                fuel: RunBudget::UNTRUSTED_MAX_STEPS,
                epoch_deadline_ticks: 1,
                wall_clock_timeout_ms: 1_000,
                memory_size_bytes: 16 * 1024 * 1024,
                table_elements: 4_096,
                instances: 1,
                tables: 4,
                memories: 1,
                output_bytes: RunBudget::UNTRUSTED_MAX_OUTPUT_BYTES,
            },
        }
    }

    fn to_json_value(self) -> JsonValue {
        json!({
            "target": "wasmtime",
            "profile": self.profile.as_str(),
            "engine": {
                "consume_fuel": true,
                "epoch_interruption": true
            },
            "store": {
                "fuel": self.fuel,
                "epoch_deadline_ticks": self.epoch_deadline_ticks,
                "limits": {
                    "memory_size_bytes": self.memory_size_bytes,
                    "table_elements": self.table_elements,
                    "instances": self.instances,
                    "tables": self.tables,
                    "memories": self.memories,
                    "trap_on_grow_failure": true
                }
            },
            "host": {
                "wall_clock_timeout_ms": self.wall_clock_timeout_ms,
                "output_bytes": self.output_bytes,
                "wasi": "deny-by-default",
                "imports": "allowlist"
            },
            "notes": [
                "Fuel is a Wasmtime instruction-cost budget, not a Relon evaluator step count.",
                "Epoch interruption requires a host timer/task that calls Engine::increment_epoch().",
                "Output bytes must be checked by the host after serializing the returned value."
            ]
        })
    }

    fn to_rust_snippet(self) -> String {
        format!(
            r#"use wasmtime::{{Config, Engine, Store, StoreLimits, StoreLimitsBuilder}};

pub struct RelonVmState {{
    limits: StoreLimits,
    // Add host-owned state for Relon imports/functions here.
}}

pub fn build_relon_store() -> Result<(Engine, Store<RelonVmState>), wasmtime::Error> {{
    let mut config = Config::new();
    config.consume_fuel(true);
    config.epoch_interruption(true);

    let engine = Engine::new(&config)?;
    let state = RelonVmState {{
        limits: StoreLimitsBuilder::new()
            .memory_size({memory_size_bytes})
            .table_elements({table_elements})
            .instances({instances})
            .tables({tables})
            .memories({memories})
            .trap_on_grow_failure(true)
            .build(),
    }};

    let mut store = Store::new(&engine, state);
    store.limiter(|state| &mut state.limits);
    store.set_fuel({fuel})?;

    #[cfg(target_has_atomic = "64")]
    store.set_epoch_deadline({epoch_deadline_ticks});

    Ok((engine, store))
}}

// Profile: {profile}
// Host wall-clock timeout: {wall_clock_timeout_ms} ms.
// Host JSON/output byte limit: {output_bytes} bytes.
// Keep WASI denied by default and expose only audited Relon host imports.
// If epoch interruption is enabled, run a host timer/task that calls
// `engine.increment_epoch()` after the wall-clock deadline."#,
            profile = self.profile.as_str(),
            fuel = self.fuel,
            epoch_deadline_ticks = self.epoch_deadline_ticks,
            wall_clock_timeout_ms = self.wall_clock_timeout_ms,
            memory_size_bytes = self.memory_size_bytes,
            table_elements = self.table_elements,
            instances = self.instances,
            tables = self.tables,
            memories = self.memories,
            output_bytes = self.output_bytes,
        )
    }
}

#[derive(Parser)]
#[command(name = "relon")]
#[command(about = "Relon: A programmable configuration language", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Evaluate a .relon file and output JSON
    Run {
        /// The path to the .relon file
        file: PathBuf,
        /// Pretty-print the output JSON
        #[arg(short, long, default_value_t = true)]
        pretty: bool,
        /// v6-fix-D2 cold-start lite mode. Short-circuits every
        /// startup-side lazy init the default `relon run` path
        /// would normally pay: forces `Backend::TreeWalk` (no
        /// cranelift-AOT lower / JIT), skips the trust-only
        /// remote-HTTP resolver mount, skips the optional warm-up
        /// tick, and avoids constructing or probing the on-disk
        /// AOT cache directory.
        ///
        /// Differs from `--backend tree-walk`: that flag only
        /// switches the evaluator selection but still walks the
        /// same default-mode startup sequence; `--lite` *also*
        /// gates every other lazy init that doesn't pay for a
        /// short-lived `relon run foo.relon` invocation. Pairs
        /// best with the W11 bench shape (single-shot evaluator,
        /// no JIT tier worth amortising).
        #[arg(long, default_value_t = false)]
        lite: bool,
        /// JSON object whose keys are `#main(...)` parameter names and
        /// whose values are the host-pushed args. Required if the
        /// target file declares a `#main(...)` signature; ignored
        /// otherwise. Either pass JSON inline (`--args '{"u": ...}'`)
        /// or via stdin redirect (`--args -` reads from stdin).
        #[arg(long)]
        args: Option<String>,
        /// Grant full filesystem and capability-gated native-fn
        /// access. By default the runtime runs sandboxed: only
        /// `std/*` imports resolve, local `#import "./foo.relon"`
        /// paths surface as `ModuleNotFound`, and native fns
        /// registered with a non-empty `NativeFnGate` are denied
        /// (pure fns registered via `register_pure_fn` keep working).
        /// Pass `--trust` when the script expects the legacy
        /// fully-granted environment (e.g. local imports, registered
        /// HTTP / FS helpers).
        #[arg(long)]
        trust: bool,
        /// Evaluation backend. `auto` (default) routes `run_main`
        /// through cranelift-AOT lazily (built on first invocation)
        /// and keeps every other operation on the tree-walker.
        /// `tree-walk` forces the original interpreter on every
        /// path; `cranelift-aot` pre-compiles the entry into native
        /// machine code via `relon-codegen-cranelift`'s cranelift JIT
        /// and dispatches `run_main` through a panic-shielded
        /// trampoline. The cranelift-aot-only backend supports
        /// `#main(...)` entries only and rejects library-mode
        /// (no-#main) sources; `auto` falls back to the tree-walker
        /// for library-mode files.
        #[arg(long, value_enum, default_value_t = BackendArg::Auto)]
        backend: BackendArg,
        /// Resource-budget profile installed by the CLI. `off` preserves
        /// the historical default. `dev` and `untrusted` currently apply
        /// a source-byte preflight, tree-walk step/value limits, and an
        /// output-byte limit; they are guardrails, not a replacement for a
        /// wasm VM boundary.
        #[arg(long, value_enum, default_value_t = BudgetArg::Off)]
        budget: BudgetArg,
        /// Maximum number of bytes the input `.relon` source file may
        /// contain. Checked from file metadata before reading/parsing so
        /// pathological untrusted inputs can be rejected before the parser
        /// stack is involved.
        #[arg(long)]
        max_source_bytes: Option<usize>,
        /// Override the evaluator step budget. Forces the tree-walk
        /// evaluator under `--backend auto`; rejected with explicit
        /// `--backend cranelift-aot` because that backend does not honor
        /// this CLI budget.
        #[arg(long)]
        max_steps: Option<u64>,
        /// Override the maximum element count for a single list/tuple/dict.
        /// Forces the tree-walk evaluator under `--backend auto`; rejected
        /// with explicit `--backend cranelift-aot`.
        #[arg(long)]
        max_value_elements: Option<usize>,
        /// Maximum number of bytes the final JSON output may contain after
        /// the selected pretty/compact serialization.
        #[arg(long)]
        max_output_bytes: Option<usize>,
        /// v3++ b-2: when set, every `#import` whose path looks remote
        /// (`https://`, `http://`) MUST carry an inline integrity pin
        /// (e.g. `sha256:"<hex>"`). Missing pins surface as
        /// `ImportHashRequired` *before* the loader fetches anything,
        /// so a CI gate can refuse unpinned dependencies without
        /// touching the network. Local-path imports are exempt — the
        /// supply-chain risk targeted here is wire-time, not source-
        /// tree-time. Default off preserves the v3+ a-3 behavior.
        #[arg(long, default_value_t = false)]
        require_hash: bool,
    },

    /// Parse, analyze, and report whether a file is compatible with a
    /// selected execution backend without running it.
    Check {
        /// The path to the .relon file
        file: PathBuf,
        /// Backend compatibility target to check.
        #[arg(long, value_enum, default_value_t = BackendArg::Auto)]
        backend: BackendArg,
        /// Use the trusted resolver posture while checking imports.
        #[arg(long)]
        trust: bool,
        /// Maximum number of bytes the input `.relon` source file may
        /// contain. Checked before reading/parsing.
        #[arg(long)]
        max_source_bytes: Option<usize>,
        /// Require remote imports to carry inline hash pins.
        #[arg(long, default_value_t = false)]
        require_hash: bool,
    },

    /// Emit host-runtime policy for VM deployments. This is intentionally
    /// outside `.relon` source: operators choose limits at the runtime/host
    /// boundary, where they can actually be enforced.
    #[command(name = "host-policy")]
    HostPolicy {
        /// Runtime target to generate for.
        #[arg(long, value_enum, default_value_t = HostPolicyTargetArg::Wasmtime)]
        target: HostPolicyTargetArg,
        /// Built-in policy profile.
        #[arg(long, value_enum, default_value_t = HostPolicyProfileArg::Untrusted)]
        profile: HostPolicyProfileArg,
        /// Output format.
        #[arg(long, value_enum, default_value_t = HostPolicyFormatArg::Json)]
        format: HostPolicyFormatArg,
    },

    /// Format or check Relon files. Equivalent to the standalone
    /// `relon-fmt` binary, exposed here so a single `relon` install
    /// covers the toolchain.
    Fmt {
        /// Check whether files are formatted without writing changes.
        #[arg(long)]
        check: bool,
        /// Print formatted output to stdout instead of writing files.
        #[arg(long)]
        stdout: bool,
        /// Relon files to format.
        files: Vec<PathBuf>,
    },

    /// Run the Language Server (stdio transport). Equivalent to the
    /// standalone `relon-lsp` binary; editors that want a single
    /// `relon lsp` command can wire to this.
    ///
    /// Phase G.W11 Phase 2 cold-start: the subcommand only ships
    /// when the `cli-lsp` cargo feature is enabled. The standalone
    /// `relon-lsp` binary stays the canonical LSP entry; embedding
    /// it here cost ~160 KB text on every CLI invocation, which the
    /// W11 trivial-`#main` shape pays without ever entering. Editors
    /// that want one-binary ergonomics rebuild with
    /// `cargo build --features cli-lsp` (or `--features full`).
    #[cfg(feature = "cli-lsp")]
    Lsp,
}

/// v6-fix-D2-J cold-start: hand-rolled argv fast-path for the
/// trivial `relon run <file> [--lite] [--args <json>]` invocation
/// shape the W11 bench exercises. Constructing `Cli` through
/// `clap::Cli::parse()` is ~270 µs on a fresh process — clap rebuilds
/// the whole command tree (subcommands + value enums + help strings)
/// before matching a 3 - 5 element argv. The match here is conservative:
/// any unrecognised arg, prefix abbreviation, value-form (`--args=foo`),
/// short flag, or extra positional falls back to `clap::Cli::parse()`
/// so flag semantics stay defined by the `#[derive(Parser)]` block
/// (single source of truth). The shape we accept by design:
///
/// * `relon run <file>` (positional only)
/// * `relon run <file> --args <json>` (or `--args -` for stdin)
/// * `relon run <file> --lite [--args <json>]`
///
/// Anything else — `fmt`, `lsp`, `--trust`, `--backend`, budget flags,
/// `--require-hash`, `--pretty`, `--help`, `--version` — defers to clap
/// so the surface stays compatible.
fn try_parse_run_fast(argv: &[std::ffi::OsString]) -> Option<Commands> {
    // argv[0] is the binary. The first command-line arg must be `run`;
    // everything else (`fmt`, `lsp`, `--help`, …) goes through clap.
    let mut iter = argv.iter().skip(1);
    let sub = iter.next()?;
    if sub != "run" {
        return None;
    }

    let mut file: Option<PathBuf> = None;
    let mut lite = false;
    let mut args: Option<String> = None;

    while let Some(tok) = iter.next() {
        let s = tok.to_str()?;
        if s == "--lite" {
            if lite {
                return None;
            }
            lite = true;
        } else if s == "--args" {
            if args.is_some() {
                return None;
            }
            let v = iter.next()?.to_str()?.to_string();
            args = Some(v);
        } else if s.starts_with("--") || s.starts_with('-') {
            // Any other flag (`--args=...`, `--trust`, `--pretty`,
            // `--help`, short flags) bails so clap owns the parse.
            return None;
        } else {
            if file.is_some() {
                return None;
            }
            file = Some(PathBuf::from(tok));
        }
    }

    let file = file?;
    Some(Commands::Run {
        file,
        pretty: true,
        lite,
        args,
        trust: false,
        backend: BackendArg::Auto,
        budget: BudgetArg::Off,
        max_steps: None,
        max_value_elements: None,
        max_output_bytes: None,
        max_source_bytes: None,
        require_hash: false,
    })
}

fn main() -> miette::Result<()> {
    // v6-fix-D2-J: phase timing begins *before* `clap::Cli::parse`
    // so the `RELON_CLI_PROFILE=1` trace includes the parser cost
    // (previously hidden in the run-prologue total). The probe stays
    // gated on the env var so the production hot path pays nothing.
    let profile_main_entry = std::time::Instant::now();
    let profile_enabled = std::env::var_os("RELON_CLI_PROFILE").is_some();
    if profile_enabled {
        let entry_us = profile_main_entry.elapsed().as_micros();
        eprintln!(
            "[relon-cli profile] main_entry              +{entry_us:>6}us  (total {entry_us}us)"
        );
    }
    // v6-fix-D2-J: try the hand-rolled argv matcher first. On the W11
    // shape (`run <file> [--lite] [--args …]`) this skips clap's
    // ~270 µs cold-start tax entirely; any other argv shape falls
    // through to `Cli::parse()` so flag semantics stay clap-defined.
    let argv: Vec<std::ffi::OsString> = std::env::args_os().collect();
    let command = match try_parse_run_fast(&argv) {
        Some(cmd) => {
            if profile_enabled {
                let total_us = profile_main_entry.elapsed().as_micros();
                eprintln!(
                    "[relon-cli profile] argv_fast_run          +{total_us:>6}us  (total {total_us}us)"
                );
            }
            cmd
        }
        None => {
            let cli = Cli::parse();
            if profile_enabled {
                let total_us = profile_main_entry.elapsed().as_micros();
                eprintln!(
                    "[relon-cli profile] clap::parse             +{total_us:>6}us  (total {total_us}us)"
                );
            }
            cli.command
        }
    };

    match command {
        Commands::Run {
            file,
            pretty,
            lite,
            args,
            trust,
            backend,
            budget,
            max_steps,
            max_value_elements,
            max_output_bytes,
            max_source_bytes,
            require_hash,
        } => cmd_run(RunCommand {
            file,
            pretty,
            lite,
            args,
            trust,
            backend,
            budget,
            max_source_bytes,
            max_steps,
            max_value_elements,
            max_output_bytes,
            require_hash,
        })?,
        Commands::Check {
            file,
            backend,
            trust,
            max_source_bytes,
            require_hash,
        } => cmd_check(file, backend, trust, max_source_bytes, require_hash)?,
        Commands::HostPolicy {
            target,
            profile,
            format,
        } => cmd_host_policy(target, profile, format)?,
        Commands::Fmt {
            check,
            stdout,
            files,
        } => {
            run_fmt(check, stdout, files)?;
        }
        #[cfg(feature = "cli-lsp")]
        Commands::Lsp => {
            relon_lsp::server::run_stdio()
                .map_err(|e| miette::miette!("language server exited with error: {e}"))?;
        }
    }

    Ok(())
}

/// Handle the `relon run` subcommand. Extracted from `main` so the
/// 670-line body lives in its own fn (the dispatch + every backend
/// arm stay together as before; future cleanups can split this
/// further by phase). Returns `Err` for any operator-facing failure
/// (parse / analyze / runtime / IO) — `main` propagates straight
/// through.
/// Warning emitted when `--trust` is passed but the resolved execution
/// path is the cranelift-AOT backend, which the CLI cannot honour it on
/// (no host-fn registry to grant capabilities from). Surfacing this
/// keeps the flag from being a silent no-op: tree-walk is the backend
/// that acts on `--trust`.
const TRUST_UNSUPPORTED_ON_AOT: &str =
    "[relon-cli] warning: --trust has no effect on the cranelift-AOT backend \
     (single file, no host-fn registry): it grants no runtime native-fn \
     capability, and the `#import` paths --trust would open are not stitched \
     by this backend. Use --backend tree-walk if your \
     program relies on --trust.";

fn warn_trust_unsupported_on_aot() {
    eprintln!("{TRUST_UNSUPPORTED_ON_AOT}");
}

fn cmd_host_policy(
    target: HostPolicyTargetArg,
    profile: HostPolicyProfileArg,
    format: HostPolicyFormatArg,
) -> miette::Result<()> {
    match target {
        HostPolicyTargetArg::Wasmtime => {
            let policy = WasmtimeHostPolicy::from_profile(profile);
            match format {
                HostPolicyFormatArg::Json => {
                    let output =
                        serde_json::to_string_pretty(&policy.to_json_value()).into_diagnostic()?;
                    println!("{output}");
                }
                HostPolicyFormatArg::Rust => {
                    println!("{}", policy.to_rust_snippet());
                }
            }
        }
    }
    Ok(())
}

fn parse_main_args_json(
    args_json: &str,
    signature: &MainSignature,
    tree: &AnalyzedTree,
) -> Result<HashMap<String, Value>, String> {
    let raw_args: serde_json::Map<String, JsonValue> =
        serde_json::from_str(args_json).map_err(|e| {
            format!("--args must be a JSON object keyed by `#main(...)` parameter names: {e}")
        })?;
    let param_types: HashMap<&str, &TypeNode> = signature
        .params
        .iter()
        .map(|param| (param.name.as_str(), &param.type_node))
        .collect();
    let mut args = HashMap::with_capacity(raw_args.len());
    for (name, json) in raw_args {
        let value = if let Some(type_hint) = param_types.get(name.as_str()) {
            let type_hint = *type_hint;
            decode_json_for_type(json, type_hint, tree).map_err(|e| {
                format!(
                    "--args value for `{name}` cannot be decoded as {}: {e}",
                    format_type(type_hint)
                )
            })?
        } else {
            targetless_json_to_value(json)?
        };
        args.insert(name, value);
    }
    Ok(args)
}

fn decode_json_for_type(
    json: JsonValue,
    type_hint: &TypeNode,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    if type_hint.is_optional {
        let mut inner_type = type_hint.clone();
        inner_type.is_optional = false;
        return decode_json_for_option(json, Some(&inner_type), tree);
    }

    if type_key(type_hint) == "Option" {
        return decode_json_for_option(json, type_hint.generics.first(), tree);
    }

    if type_key(type_hint) == "Result" {
        return decode_json_for_result(
            json,
            type_hint.generics.first(),
            type_hint.generics.get(1),
            tree,
        );
    }

    if let Some(value) = decode_json_for_builtin_scalar(json.clone(), type_hint) {
        return value;
    }

    match type_key(type_hint).as_str() {
        "Tuple" => decode_json_array_as_tuple_or_targetless(json, &type_hint.generics, tree),
        "List" => decode_json_array_as_list_or_targetless(json, type_hint.generics.first(), tree),
        "Dict" => decode_json_object_as_dict_or_targetless(json, type_hint.generics.get(1), tree),
        "Enum" => decode_json_for_enum(json, type_hint, tree),
        _ => {
            if let Some(def) = schema_def_for_type(type_hint, tree) {
                if !def.variants.is_empty() {
                    return decode_json_for_tagged_enum_schema(json, type_hint, def, tree);
                }
            }
            if let Some(elements) = schema_tuple_elements_for_type(type_hint, tree) {
                decode_json_array_as_tuple_or_targetless(json, &elements, tree)
            } else if let Some(fields) = schema_fields_for_type(type_hint, tree) {
                decode_json_object_as_schema_or_targetless(json, &fields, tree)
            } else {
                targetless_json_to_value(json)
            }
        }
    }
}

fn decode_json_for_builtin_scalar(
    json: JsonValue,
    type_hint: &TypeNode,
) -> Option<Result<Value, String>> {
    match type_key(type_hint).as_str() {
        "Any" => Some(targetless_json_to_value(json)),
        "Bool" => Some(match json {
            JsonValue::Bool(value) => Ok(Value::Bool(value)),
            other => Err(format!("expected JSON bool, got {}", json_kind(&other))),
        }),
        "Int" => Some(match json {
            JsonValue::Number(value) => value
                .as_i64()
                .map(Value::Int)
                .ok_or_else(|| "expected JSON integer in i64 range".to_string()),
            other => Err(format!("expected JSON integer, got {}", json_kind(&other))),
        }),
        "Float" => Some(match json {
            JsonValue::Number(value) => value
                .as_f64()
                .map(|value| Value::Float(value.into()))
                .ok_or_else(|| "expected finite JSON number".to_string()),
            other => Err(format!("expected JSON number, got {}", json_kind(&other))),
        }),
        "Number" => Some(match json {
            JsonValue::Number(value) => {
                if let Some(int) = value.as_i64() {
                    Ok(Value::Int(int))
                } else {
                    value
                        .as_f64()
                        .map(|value| Value::Float(value.into()))
                        .ok_or_else(|| "expected finite JSON number".to_string())
                }
            }
            other => Err(format!("expected JSON number, got {}", json_kind(&other))),
        }),
        "String" => Some(match json {
            JsonValue::String(value) => Ok(Value::String(value.into())),
            other => Err(format!("expected JSON string, got {}", json_kind(&other))),
        }),
        _ => None,
    }
}

fn json_kind(value: &JsonValue) -> &'static str {
    match value {
        JsonValue::Null => "null",
        JsonValue::Bool(_) => "bool",
        JsonValue::Number(_) => "number",
        JsonValue::String(_) => "string",
        JsonValue::Array(_) => "array",
        JsonValue::Object(_) => "object",
    }
}

fn decode_json_for_option(
    json: JsonValue,
    inner_type: Option<&TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    match json {
        JsonValue::Null => Ok(Value::option_none()),
        JsonValue::String(name) if name == "None" => Ok(Value::option_none()),
        JsonValue::Object(mut map) if map.len() == 1 && map.contains_key("None") => {
            let payload = map.remove("None").expect("checked None payload");
            match payload {
                JsonValue::Null => Ok(Value::option_none()),
                JsonValue::Object(obj) if obj.is_empty() => Ok(Value::option_none()),
                _ => Err("Option.None expects null or an empty object payload".to_string()),
            }
        }
        JsonValue::Object(mut map) if map.len() == 1 && map.contains_key("Some") => {
            let payload = map.remove("Some").expect("checked Some payload");
            decode_json_single_payload(payload, "value", inner_type, tree).map(Value::option_some)
        }
        other => {
            decode_json_single_payload(other, "value", inner_type, tree).map(Value::option_some)
        }
    }
}

fn decode_json_for_result(
    json: JsonValue,
    ok_type: Option<&TypeNode>,
    err_type: Option<&TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    let JsonValue::Object(mut map) = json else {
        return Err(
            r#"Result<T, E> expects an externally tagged object: {"Ok": ...} or {"Err": ...}"#
                .to_string(),
        );
    };
    if map.len() != 1 {
        return Err("Result<T, E> expects exactly one variant key: Ok or Err".to_string());
    }
    if let Some(payload) = map.remove("Ok") {
        return decode_json_single_payload(payload, "value", ok_type, tree).map(Value::result_ok);
    }
    if let Some(payload) = map.remove("Err") {
        return decode_json_single_payload(payload, "error", err_type, tree).map(Value::result_err);
    }
    let name = map.keys().next().cloned().unwrap_or_default();
    Err(format!(
        "object key `{name}` is not a Result variant; expected Ok or Err"
    ))
}

fn decode_json_single_payload(
    payload: JsonValue,
    field_name: &str,
    field_type: Option<&TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    let Some(field_type) = field_type else {
        return targetless_json_to_value(payload);
    };
    match decode_json_for_type(payload.clone(), field_type, tree) {
        Ok(value) => Ok(value),
        Err(direct_err) => match payload {
            JsonValue::Object(mut map) if map.len() == 1 && map.contains_key(field_name) => {
                let inner = map.remove(field_name).expect("checked payload field");
                decode_json_for_type(inner, field_type, tree)
            }
            _ => Err(direct_err),
        },
    }
}

fn decode_json_array_as_tuple_or_targetless(
    json: JsonValue,
    element_types: &[TypeNode],
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    match json {
        JsonValue::Array(items) => {
            let mut values = Vec::with_capacity(items.len());
            for (idx, item) in items.into_iter().enumerate() {
                let value = match element_types.get(idx) {
                    Some(slot_type) => decode_json_for_type(item, slot_type, tree)?,
                    None => targetless_json_to_value(item)?,
                };
                values.push(value);
            }
            Ok(Value::tuple(values))
        }
        other => targetless_json_to_value(other),
    }
}

fn decode_json_array_as_list_or_targetless(
    json: JsonValue,
    item_type: Option<&TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    match json {
        JsonValue::Array(items) => {
            let mut values = Vec::with_capacity(items.len());
            for item in items {
                let value = match item_type {
                    Some(item_type) => decode_json_for_type(item, item_type, tree)?,
                    None => targetless_json_to_value(item)?,
                };
                values.push(value);
            }
            Ok(Value::list(values))
        }
        other => targetless_json_to_value(other),
    }
}

fn decode_json_object_as_dict_or_targetless(
    json: JsonValue,
    value_type: Option<&TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    match json {
        JsonValue::Object(map) => {
            let mut values = Vec::with_capacity(map.len());
            for (key, item) in map {
                let value = match value_type {
                    Some(value_type) => decode_json_for_type(item, value_type, tree)?,
                    None => targetless_json_to_value(item)?,
                };
                values.push((key, value));
            }
            Ok(Value::dict(values))
        }
        other => targetless_json_to_value(other),
    }
}

fn decode_json_object_as_schema_or_targetless(
    json: JsonValue,
    field_types: &HashMap<String, TypeNode>,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    match json {
        JsonValue::Object(map) => {
            let mut values = Vec::with_capacity(map.len());
            for (key, item) in map {
                let value = match field_types.get(&key) {
                    Some(field_type) => decode_json_for_type(item, field_type, tree)?,
                    None => targetless_json_to_value(item)?,
                };
                values.push((key, value));
            }
            Ok(Value::dict(values))
        }
        other => targetless_json_to_value(other),
    }
}

fn decode_json_for_tagged_enum_schema(
    json: JsonValue,
    type_hint: &TypeNode,
    def: &SchemaDef,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    let enum_name = def.name.clone().unwrap_or_else(|| type_key(type_hint));
    match json {
        JsonValue::String(name) => decode_json_string_as_enum_unit(name, &enum_name, def),
        JsonValue::Object(map) => {
            if map.len() != 1 {
                return Err(format!(
                    "enum `{enum_name}` expects an externally tagged object with exactly one variant key"
                ));
            }
            let (variant_name, payload) = map
                .into_iter()
                .next()
                .expect("checked exactly one externally tagged enum entry");
            decode_json_object_as_enum_variant(variant_name, payload, type_hint, def, tree)
        }
        other => targetless_json_to_value(other),
    }
}

fn decode_json_string_as_enum_unit(
    name: String,
    enum_name: &str,
    def: &SchemaDef,
) -> Result<Value, String> {
    let matching: Vec<_> = def
        .variants
        .iter()
        .filter(|variant| variant.name == name)
        .collect();
    match matching.as_slice() {
        [variant] if variant.fields.is_empty() => Ok(Value::variant_dict(
            Vec::<(String, Value)>::new(),
            variant.name.clone(),
            enum_name.to_string(),
        )),
        [variant] => Err(format!(
            "string `{}` names enum variant `{}` but that variant requires payload fields",
            name, variant.name
        )),
        [] => Err(format!(
            "string `{name}` does not name a unit variant of enum `{enum_name}`"
        )),
        _ => Err(format!(
            "string `{name}` is ambiguous for enum `{enum_name}`"
        )),
    }
}

fn decode_json_object_as_enum_variant(
    variant_name: String,
    payload: JsonValue,
    type_hint: &TypeNode,
    def: &SchemaDef,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    let enum_name = def.name.clone().unwrap_or_else(|| type_key(type_hint));
    let variant = def
        .variants
        .iter()
        .find(|variant| variant.name == variant_name)
        .ok_or_else(|| {
            format!("object key `{variant_name}` is not a variant of enum `{enum_name}`")
        })?;
    let fields = substituted_variant_fields(variant, def, &type_hint.generics)?;
    if fields.is_empty() {
        return match payload {
            JsonValue::Object(map) if map.is_empty() => Ok(Value::variant_dict(
                Vec::<(String, Value)>::new(),
                variant.name.clone(),
                enum_name,
            )),
            _ => Err(format!(
                "unit enum variant `{}` expects an empty object payload",
                variant.name
            )),
        };
    }

    let values = if variant_fields_are_tuple_payload(&fields) {
        decode_json_payload_as_tuple_variant(payload, &fields, tree, &variant.name)?
    } else {
        decode_json_payload_as_struct_variant(payload, &fields, tree, &variant.name)?
    };
    Ok(Value::variant_dict(values, variant.name.clone(), enum_name))
}

fn substituted_variant_fields(
    variant: &relon_analyzer::schema::EnumVariant,
    def: &SchemaDef,
    concrete_args: &[TypeNode],
) -> Result<Vec<(String, TypeNode)>, String> {
    variant
        .fields
        .iter()
        .map(|field| {
            let type_hint = field.type_hint.as_ref().ok_or_else(|| {
                format!(
                    "enum variant `{}` field `{}` has no static type",
                    variant.name, field.name
                )
            })?;
            Ok((
                field.name.clone(),
                substitute_schema_type(type_hint, &def.generics, concrete_args),
            ))
        })
        .collect()
}

fn variant_fields_are_tuple_payload(fields: &[(String, TypeNode)]) -> bool {
    !fields.is_empty()
        && fields
            .iter()
            .enumerate()
            .all(|(idx, (name, _))| *name == idx.to_string())
}

fn decode_json_payload_as_tuple_variant(
    payload: JsonValue,
    fields: &[(String, TypeNode)],
    tree: &AnalyzedTree,
    variant_name: &str,
) -> Result<Vec<(String, Value)>, String> {
    let JsonValue::Array(items) = payload else {
        return Err(format!(
            "tuple enum variant `{variant_name}` expects a JSON array payload"
        ));
    };
    if items.len() != fields.len() {
        return Err(format!(
            "tuple enum variant `{variant_name}` expects {} elements, got {}",
            fields.len(),
            items.len()
        ));
    }
    fields
        .iter()
        .zip(items)
        .map(|((name, field_type), item)| {
            decode_json_for_type(item, field_type, tree).map(|value| (name.clone(), value))
        })
        .collect()
}

fn decode_json_payload_as_struct_variant(
    payload: JsonValue,
    fields: &[(String, TypeNode)],
    tree: &AnalyzedTree,
    variant_name: &str,
) -> Result<Vec<(String, Value)>, String> {
    let JsonValue::Object(mut map) = payload else {
        return Err(format!(
            "struct enum variant `{variant_name}` expects a JSON object payload"
        ));
    };
    let mut values = Vec::with_capacity(fields.len());
    for (name, field_type) in fields {
        let item = map
            .remove(name)
            .ok_or_else(|| format!("enum variant `{variant_name}` is missing field `{name}`"))?;
        values.push((name.clone(), decode_json_for_type(item, field_type, tree)?));
    }
    if !map.is_empty() {
        let mut names: Vec<_> = map.keys().cloned().collect();
        names.sort();
        return Err(format!(
            "enum variant `{variant_name}` received unknown field(s): {}",
            names.join(", ")
        ));
    }
    Ok(values)
}

fn decode_json_for_enum(
    json: JsonValue,
    type_hint: &TypeNode,
    tree: &AnalyzedTree,
) -> Result<Value, String> {
    let arity = match &json {
        JsonValue::Array(items) => items.len(),
        _ => return targetless_json_to_value(json),
    };
    let matching_tuple_alts: Vec<&TypeNode> = type_hint
        .generics
        .iter()
        .filter(|alt| type_accepts_tuple_array(alt, arity, tree))
        .collect();
    if matching_tuple_alts.len() == 1 {
        decode_json_for_type(json, matching_tuple_alts[0], tree)
    } else {
        targetless_json_to_value(json)
    }
}

fn type_accepts_tuple_array(type_hint: &TypeNode, arity: usize, tree: &AnalyzedTree) -> bool {
    if type_key(type_hint) == "Tuple" {
        return type_hint.generics.len() == arity;
    }
    matches!(
        schema_tuple_elements_for_type(type_hint, tree),
        Some(elements) if elements.len() == arity
    )
}

fn targetless_json_to_value(json: JsonValue) -> Result<Value, String> {
    if json.is_null() {
        return Err("JSON null needs an Option<T> target type".to_string());
    }
    serde_json::from_value::<Value>(json).map_err(|e| format!("invalid JSON value: {e}"))
}

fn schema_tuple_elements_for_type(
    type_hint: &TypeNode,
    tree: &AnalyzedTree,
) -> Option<Vec<TypeNode>> {
    if let Some(def) = local_schema_def_for_type(type_hint, tree) {
        if let Some(elements) = &def.tuple_elements {
            return Some(substitute_schema_types(
                elements,
                &def.generics,
                &type_hint.generics,
            ));
        }
    }
    let key = type_key(type_hint);
    let imports = tree.workspace_import_index.as_ref()?;
    let elements = imports.imported_tuple_schemas.get(&key)?;
    let generics = imports
        .imported_schema_generics
        .get(&key)
        .cloned()
        .unwrap_or_default();
    Some(substitute_schema_types(
        elements,
        &generics,
        &type_hint.generics,
    ))
}

fn schema_fields_for_type(
    type_hint: &TypeNode,
    tree: &AnalyzedTree,
) -> Option<HashMap<String, TypeNode>> {
    if let Some(def) = local_schema_def_for_type(type_hint, tree) {
        return Some(substitute_schema_fields_from_def(def, &type_hint.generics));
    }
    let key = type_key(type_hint);
    let imports = tree.workspace_import_index.as_ref()?;
    let fields = imports.imported_schemas.get(&key)?;
    let generics = imports
        .imported_schema_generics
        .get(&key)
        .cloned()
        .unwrap_or_default();
    Some(substitute_schema_fields(
        fields,
        &generics,
        &type_hint.generics,
    ))
}

fn local_schema_def_for_type<'a>(
    type_hint: &TypeNode,
    tree: &'a AnalyzedTree,
) -> Option<&'a SchemaDef> {
    if type_hint.path.len() != 1 {
        return None;
    }
    let name = &type_hint.path[0];
    let decl = tree.root_schemas.iter().find(|decl| &decl.name == name)?;
    tree.schemas.get(&decl.schema_node.id)
}

fn schema_def_for_type<'a>(type_hint: &TypeNode, tree: &'a AnalyzedTree) -> Option<&'a SchemaDef> {
    if let Some(def) = local_schema_def_for_type(type_hint, tree) {
        return Some(def);
    }
    let key = type_key(type_hint);
    tree.workspace_import_index
        .as_ref()?
        .imported_schema_defs
        .get(&key)
}

fn substitute_schema_fields_from_def(
    def: &SchemaDef,
    concrete_args: &[TypeNode],
) -> HashMap<String, TypeNode> {
    let fields = def.fields.iter().filter_map(|field| {
        let type_hint = field.type_hint.as_ref()?;
        Some((field.name.clone(), type_hint.clone()))
    });
    substitute_schema_fields_from_iter(fields, &def.generics, concrete_args)
}

fn substitute_schema_fields(
    fields: &HashMap<String, TypeNode>,
    generic_names: &[String],
    concrete_args: &[TypeNode],
) -> HashMap<String, TypeNode> {
    substitute_schema_fields_from_iter(
        fields
            .iter()
            .map(|(name, type_hint)| (name.clone(), type_hint.clone())),
        generic_names,
        concrete_args,
    )
}

fn substitute_schema_fields_from_iter(
    fields: impl Iterator<Item = (String, TypeNode)>,
    generic_names: &[String],
    concrete_args: &[TypeNode],
) -> HashMap<String, TypeNode> {
    fields
        .map(|(name, type_hint)| {
            (
                name,
                substitute_schema_type(&type_hint, generic_names, concrete_args),
            )
        })
        .collect()
}

fn substitute_schema_types(
    types: &[TypeNode],
    generic_names: &[String],
    concrete_args: &[TypeNode],
) -> Vec<TypeNode> {
    types
        .iter()
        .map(|type_hint| substitute_schema_type(type_hint, generic_names, concrete_args))
        .collect()
}

fn substitute_schema_type(
    type_hint: &TypeNode,
    generic_names: &[String],
    concrete_args: &[TypeNode],
) -> TypeNode {
    let subst: HashMap<String, TypeNode> = generic_names
        .iter()
        .cloned()
        .zip(concrete_args.iter().cloned())
        .collect();
    if subst.is_empty() {
        type_hint.clone()
    } else {
        substitute_generics_in_typenode(type_hint, &subst)
    }
}

fn type_key(type_hint: &TypeNode) -> String {
    type_hint.path.join(".")
}

#[allow(clippy::too_many_arguments)]
fn enforce_source_size(path: &Path, limit: Option<usize>) -> miette::Result<()> {
    let Some(limit) = limit else {
        return Ok(());
    };
    let actual = std::fs::metadata(path)
        .into_diagnostic()
        .map_err(|e| e.wrap_err(format!("Failed to stat file {:?}", path)))?
        .len() as usize;
    if actual > limit {
        return Err(miette::miette!(
            "Relon source exceeded --max-source-bytes: actual {actual} bytes, limit {limit} bytes"
        ));
    }
    Ok(())
}

fn cmd_check(
    file: PathBuf,
    backend: BackendArg,
    trust: bool,
    max_source_bytes: Option<usize>,
    require_hash: bool,
) -> miette::Result<()> {
    let canonical_file = std::fs::canonicalize(&file)
        .into_diagnostic()
        .map_err(|e| e.wrap_err(format!("Failed to resolve file {:?}", file)))?;
    enforce_source_size(&canonical_file, max_source_bytes)?;
    let content = std::fs::read_to_string(&canonical_file)
        .into_diagnostic()
        .map_err(|e| e.wrap_err(format!("Failed to read file {:?}", canonical_file)))?;

    let cache_namespace = canonical_file.to_string_lossy().to_string();
    let entry_dir = canonical_file
        .parent()
        .unwrap_or(Path::new("."))
        .to_path_buf();
    let mut loader = if trust {
        ResolverChainLoader::trusted()
    } else {
        ResolverChainLoader::sandboxed()
    };
    let analyze_options = AnalyzeOptions {
        require_hash,
        ..AnalyzeOptions::default()
    };
    let workspace = analyze_entry_with_options(
        cache_namespace.clone(),
        &content,
        entry_dir,
        &mut loader,
        &analyze_options,
    );
    if workspace.has_errors() {
        let mut messages: Vec<String> = workspace
            .workspace_diagnostics
            .iter()
            .filter(|d| d.severity() == relon_analyzer::Severity::Error)
            .map(|d| d.to_string())
            .collect();
        for (id, tree) in &workspace.modules {
            for d in &tree.diagnostics {
                if d.severity() == relon_analyzer::Severity::Error {
                    messages.push(format!("[{id}] {d}"));
                }
            }
        }
        let joined = messages.join("\n  - ");
        return Err(miette::miette!("Analyzer reported errors:\n  - {joined}")
            .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
    }

    let has_main = workspace
        .modules
        .get(&cache_namespace)
        .and_then(|tree| tree.main_signature.as_ref())
        .is_some();
    println!("ok: analyzer");
    match backend {
        BackendArg::TreeWalk => {
            println!("backend tree-walk: compatible (reference evaluator)");
            Ok(())
        }
        BackendArg::CraneliftAot => {
            if trust {
                warn_trust_unsupported_on_aot();
            }
            check_cranelift_compat(&content, has_main, "cranelift-aot", true)
        }
        BackendArg::Auto => {
            if !has_main {
                println!("backend auto: compatible (library-mode routes to tree-walk)");
                return Ok(());
            }
            if relon::is_trivial_scalar_main(&content) {
                println!("backend auto: compatible (trivial #main routes to tree-walk)");
                return Ok(());
            }
            check_cranelift_compat(&content, has_main, "auto/cranelift-aot", false)
        }
    }
}

fn check_cranelift_compat(
    content: &str,
    has_main: bool,
    label: &str,
    strict: bool,
) -> miette::Result<()> {
    if !has_main {
        let message = format!("{label}: incompatible (cranelift-aot requires `#main(...)`)");
        return if strict {
            Err(miette::miette!("{message}"))
        } else {
            println!("backend {message}; auto will route to tree-walk");
            Ok(())
        };
    }
    match AotEvaluator::from_source(content) {
        Ok(_) => {
            println!("backend {label}: compatible");
            Ok(())
        }
        Err(e) if e.is_unsupported_shape() && !strict => {
            println!("backend {label}: not compiled ({e}); auto will fall back to tree-walk");
            Ok(())
        }
        Err(e) => Err(miette::miette!("{label}: incompatible: {e}")),
    }
}

fn cmd_run(command: RunCommand) -> miette::Result<()> {
    let RunCommand {
        file,
        pretty,
        lite,
        args,
        trust,
        backend,
        budget,
        max_source_bytes,
        max_steps,
        max_value_elements,
        max_output_bytes,
        require_hash,
    } = command;
    // v6-fix-D2: phase timing. Emitted only when the
    // operator opts in via `RELON_CLI_PROFILE=1`; default
    // path stays silent so `--lite` doesn't pollute
    // stderr.
    let profile_phases = std::env::var_os("RELON_CLI_PROFILE").is_some();
    let phase_start = std::time::Instant::now();
    let mut last_phase_at = phase_start;
    let mut phase = |label: &str| {
        if !profile_phases {
            return;
        }
        let now = std::time::Instant::now();
        let total_us = now.duration_since(phase_start).as_micros();
        let delta_us = now.duration_since(last_phase_at).as_micros();
        eprintln!("[relon-cli profile] {label:<24} +{delta_us:>6}us  (total {total_us}us)");
        last_phase_at = now;
    };
    let run_budget = RunBudget::from_cli(
        budget,
        max_source_bytes,
        max_steps,
        max_value_elements,
        max_output_bytes,
    );

    // v6-fix-D2: `--lite` forces the tree-walker, which is
    // the only backend whose cold-start fits inside a 2x
    // LuaJIT envelope on the W11 shape. Resolve the effective
    // backend once here so the dispatch site below has a
    // single source of truth:
    //   * --lite + cranelift-aot            -> hard reject
    //     (operator should not see a silent swap).
    //   * --lite + auto / tree-walk         -> tree-walk
    //   * no --lite                          -> requested backend
    //
    // CLI evaluator-side budgets are currently tree-walk-enforced
    // (`Capabilities::max_steps` / `max_value_elements`). `--backend auto`
    // therefore resolves to tree-walk when such a budget is present; an
    // explicit `--backend cranelift-aot` rejects instead of silently
    // ignoring the operator's limit.
    let effective_backend = match (lite, run_budget.has_evaluator_limits(), backend) {
        (true, _, BackendArg::Auto | BackendArg::TreeWalk)
        | (false, true, BackendArg::Auto)
        | (false, _, BackendArg::TreeWalk) => BackendArg::TreeWalk,
        (true, _, other) => {
            return Err(miette::miette!(
                "--lite forces `--backend tree-walk`; remove the explicit `--backend {:?}` or drop `--lite`",
                other
            ));
        }
        (false, true, BackendArg::CraneliftAot) => {
            return Err(miette::miette!(
                "evaluator resource budgets (--budget dev|untrusted, --max-steps, --max-value-elements) \
                 currently require `--backend tree-walk`; cranelift-aot does not honor these CLI limits"
            ));
        }
        (false, false, other) => other,
    };
    let canonical_file = std::fs::canonicalize(&file)
        .into_diagnostic()
        .map_err(|e| e.wrap_err(format!("Failed to resolve file {:?}", file)))?;
    phase("canonicalize");
    enforce_source_size(&canonical_file, run_budget.max_source_bytes)?;
    let content = std::fs::read_to_string(&canonical_file)
        .into_diagnostic()
        .map_err(|e| e.wrap_err(format!("Failed to read file {:?}", canonical_file)))?;
    phase("read_to_string");

    // The CLI runs sandboxed by default: the operator passes
    // `--trust` when the script needs filesystem `#import`
    // paths or capability-gated native fns. The grant is
    // written out so a code reviewer can see what's being
    // trusted.
    //
    // Run the workspace analyzer over the entry + every
    // transitive `#import` so cycles, missing modules,
    // collisions, and per-file structural errors all surface
    // here — before the evaluator gets a chance to start.
    let cache_namespace = canonical_file.to_string_lossy().to_string();
    let entry_dir = canonical_file
        .parent()
        .unwrap_or(Path::new("."))
        .to_path_buf();
    let mut loader = if trust {
        ResolverChainLoader::trusted()
    } else {
        ResolverChainLoader::sandboxed()
    };
    // v3++ b-2: forward the `--require-hash` policy so the
    // workspace pass can flag unpinned remote imports up front
    // (before the loader runs). All other analyzer knobs stay
    // at their defaults so this branch keeps its prior behavior.
    // v6-fix-D2 default-path-2: detect trivial scalar
    // `#main(...)` shapes (single Int/Float/Bool/String
    // param + literal-or-arith body, no `#import`) so the
    // default branch can take the same fast-analyze path
    // `--lite` does. The classifier reads the raw source
    // text — cheap, no analyzer state required — and the
    // tree-walker's per-method-dispatch surface never enters
    // trivial-`#main` bodies, so skipping carrier injection
    // is observationally invisible.
    //
    // v6-fix-D2-I (this stage): replace the previous
    // double-parse — first inside `is_trivial_scalar_main`,
    // then again inside the lite branch's
    // `relon_parser::parse_document` — with a single attempt
    // at the cold-start fast path. `parse_document_fast` only
    // accepts the trivial-scalar `#main` envelope; success
    // both confirms triviality *and* yields the parsed Node
    // the lite branch needs. Cache the result here so neither
    // the classifier nor the lite-parse pays a second parse.
    let prelite_node = if !lite {
        relon_parser::parse_document_fast(&content)
    } else {
        // `--lite` already opts the operator into the
        // tree-walker; still try the fast path so the
        // explicit `--lite` invocation also avoids the
        // rowan CST construction on trivial sources.
        relon_parser::parse_document_fast(&content)
    };
    let trivial_default = !lite
        && match &prelite_node {
            Some(node) => relon::is_trivial_scalar_main_node(node),
            None => relon::is_trivial_scalar_main(&content),
        };
    phase("trivial_classify");
    let lite_analyze = lite || trivial_default;
    let analyze_options = AnalyzeOptions {
        require_hash,
        // v6-fix-D2 cold-start: `--lite` strips the
        // built-in `core/*.relon` carrier pass from the
        // analyzer. The carrier only feeds the
        // `s.upper()` / `lst.map(f)`-style method
        // dispatch table — it's safe to skip on entries
        // that don't invoke built-in methods. The
        // operator opts in by passing `--lite`; any
        // method call against a built-in carrier in a
        // `--lite` run will surface as
        // `UnknownMethod`. (See `--lite`'s flag doc for
        // the contract.)
        //
        // v6-fix-D2 default-path: trivial scalar `#main`
        // bodies (path b) provably do not dispatch any
        // built-in method. Skip the carrier in that
        // branch too — same fast-analyze the operator
        // would get from `--lite`, without making them
        // pass the flag.
        skip_core_schemas: lite_analyze,
        // v6-fix-D2-H cold-start: when the lite/trivial
        // path is selected, also permit the analyzer's
        // trivial-`#main` fast-path so passes that are
        // provable no-ops on this shape (schema-collect,
        // extend, constraints, resolve, full typecheck)
        // drop out. The analyzer re-validates the shape
        // internally and falls through to the full
        // pipeline on any non-trivial source, so flipping
        // this on for every lite-mode entry is safe.
        trivial_main_fast_path: lite_analyze,
        ..AnalyzeOptions::default()
    };
    // v6-fix-D2 cold-start fast path. The default
    // workspace pass (`analyze_entry_with_options`) is the
    // single biggest in-process cost for short-lived
    // invocations (~1.8 ms on a `#main(Int x) -> Int\nx + 1`
    // shape). Most of that goes into BFS bookkeeping, cycle
    // detection, cross-module schema collision, and the
    // unknown-types re-check — every one of which is a
    // no-op when the entry source has no `#import`
    // directive. `--lite` short-circuits by parsing +
    // running the per-module `analyze_with_options` pass
    // directly, then hand-rolling a `WorkspaceTree` that
    // looks identical to the multi-pass output for an
    // import-free entry. Import-bearing entries fall back
    // to the full workspace path so cross-module diagnostics
    // keep their teeth.
    let workspace = if lite_analyze {
        // v6-fix-D2-I: reuse the fast-path Node if the
        // pre-classify attempt accepted; otherwise fall back
        // to the rowan/CST `parse_document`. Either way the
        // lite branch sees one parsed `Node` — no double-parse.
        let parsed = match prelite_node {
            Some(node) => Ok(node),
            None => relon_parser::parse_document(&content),
        };
        match parsed {
            Ok(node) => {
                phase("[lite] parse_only");
                let arc_node = Arc::new(node);
                // Mirror `workspace_build::build`'s entry
                // strict-mode decision: workspace flag AND
                // no `#relaxed` / `#unstrict` directive on
                // the root. The directive constants come
                // from `relon_parser::directive` so the
                // CLI stays in lockstep with the analyzer
                // crate's view.
                let entry_relaxed = arc_node.directives.iter().any(|d| {
                    d.name == relon_parser::directive::RELAXED
                        || d.name == relon_parser::directive::UNSTRICT
                });
                let entry_strict = analyze_options.strict_mode && !entry_relaxed;
                let mut eff = analyze_options.clone();
                eff.strict_mode = entry_strict;
                let tree = analyze_with_options(&arc_node, &eff);
                phase("[lite] analyze");
                // Only short-circuit when there really are
                // no imports. Otherwise the cross-module
                // analysis the workspace pass owns must
                // run; fall back to the standard path.
                if tree.imports.is_empty() {
                    let mut ws = WorkspaceTree::new();
                    ws.entry_id = cache_namespace.clone();
                    ws.strict_mode = entry_strict;
                    ws.import_graph.insert(cache_namespace.clone(), Vec::new());
                    ws.modules.insert(cache_namespace.clone(), Arc::new(tree));
                    ws.nodes.insert(cache_namespace.clone(), arc_node);
                    phase("[lite] synth_ws");
                    ws
                } else {
                    phase("[lite] has_imports_fallback");
                    analyze_entry_with_options(
                        cache_namespace.clone(),
                        &content,
                        entry_dir,
                        &mut loader,
                        &analyze_options,
                    )
                }
            }
            Err(parse_err) => {
                // Synthesize a workspace-shaped parse-error
                // record so the downstream error renderer
                // (which expects a `WorkspaceTree`) still
                // gets a coherent input. Mirrors the
                // workspace_build path verbatim.
                phase("[lite] parse_failed");
                let mut ws = WorkspaceTree::new();
                ws.entry_id = cache_namespace.clone();
                ws.workspace_diagnostics
                    .push(WorkspaceDiagnostic::ModuleParseError {
                        path: cache_namespace.clone(),
                        message: parse_err.to_string(),
                        range: miette::SourceSpan::from((0usize, 0usize)),
                    });
                ws
            }
        }
    } else {
        if profile_phases {
            let _ = relon_parser::parse_document(&content);
            phase("[probe] parse_only");
        }
        let ws = analyze_entry_with_options(
            cache_namespace.clone(),
            &content,
            entry_dir,
            &mut loader,
            &analyze_options,
        );
        phase("analyze_entry");
        ws
    };
    if workspace.has_errors() {
        // Surface entry parse errors with the same labelled
        // span treatment the CLI used to give pre-workspace,
        // so the operator still sees the offending position
        // rather than just a generic "Parse error".
        if let Some(parse_msg) =
            workspace
                .workspace_diagnostics
                .iter()
                .find_map(|d| match d {
                    relon_analyzer::WorkspaceDiagnostic::ModuleParseError {
                        path, message, ..
                    } if path == &cache_namespace => Some(message.clone()),
                    _ => None,
                })
        {
            if let Err(e) = parse_document(&content) {
                let report = if let Some(span) = e.source_span() {
                    let label = match &e {
                        ParseDocumentError::Parse { .. } => "parse error",
                        ParseDocumentError::TrailingInput { .. } => "invalid trailing input",
                    };
                    miette::miette!(
                        labels = vec![LabeledSpan::new_with_span(Some(label.to_string()), span)],
                        "Parse error: {}",
                        e
                    )
                } else {
                    miette::miette!("Parse error: {}", e)
                };
                return Err(
                    report.with_source_code(NamedSource::new(file.to_string_lossy(), content))
                );
            }
            // Defensive: workspace flagged a parse error but
            // re-parsing succeeded — fall through to the
            // generic message so we surface *something*.
            return Err(miette::miette!("Parse error in entry module: {parse_msg}")
                .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
        }

        let mut messages: Vec<String> = workspace
            .workspace_diagnostics
            .iter()
            .filter(|d| d.severity() == relon_analyzer::Severity::Error)
            .map(|d| d.to_string())
            .collect();
        for (id, tree) in &workspace.modules {
            for d in &tree.diagnostics {
                if d.severity() == relon_analyzer::Severity::Error {
                    messages.push(format!("[{id}] {d}"));
                }
            }
        }
        let joined = messages.join("\n  - ");
        return Err(miette::miette!("Analyzer reported errors:\n  - {joined}")
            .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
    }

    // Pull the entry's parsed root out of the workspace so
    // we don't double-parse. `analyze_entry` populated
    // `nodes` for every successfully-resolved module, and on
    // a clean workspace the entry is always present. Using
    // this node — instead of a separate `parse_document` —
    // keeps `with_root` and `with_workspace` aligned on the
    // same `NodeId`s; the analyzer's resolved-reference
    // table indexes by id, so they have to match.
    let entry_node = workspace
        .nodes
        .get(&cache_namespace)
        .map(|arc| (**arc).clone())
        .expect("workspace passed has_errors check but entry node missing");
    let workspace = Arc::new(workspace);
    let ctx = {
        let mut ctx = Context::sandboxed()
            .with_root(entry_node)
            .with_workspace(Arc::clone(&workspace));
        let mut caps = if trust {
            Capabilities::all_granted()
        } else {
            Capabilities::default()
        };
        run_budget.apply_to_capabilities(&mut caps);
        if trust || run_budget.has_evaluator_limits() {
            ctx = ctx.with_capabilities(caps);
        }
        if trust {
            ctx.prepend_module_resolver(Arc::new(FilesystemModuleResolver::trusted()));
            // v3+ a-3: --trust opens remote `#import "https://..."`
            // resolution. Resolver lives on native targets only;
            // the CLI never runs on wasm32 so an unconditional
            // mount is safe.
            //
            // Phase G.W11 Phase 2: the resolver is gated behind the
            // `remote-http` cargo feature. Default CLI builds drop
            // ureq + rustls + ring (~700 KB text) so the W11
            // trivial-`#main` cold start stays lean. Operators that
            // need remote `#import` rebuild with
            // `cargo build --features cli-remote-http`. With the
            // feature off, the `ResolverChainLoader` still emits
            // `RemoteImportDenied` for `https://` paths so the
            // failure mode is unchanged from a sandboxed run.
            #[cfg(feature = "cli-remote-http")]
            ctx.prepend_module_resolver(Arc::new(
                relon_evaluator::module::RemoteHttpResolver::new(),
            ));
        }
        Arc::new({
            let mut ctx = ctx;
            // v6-fix-D2-I: the trivial-scalar `#main` envelope
            // never dispatches into stdlib / decorators /
            // prelude — `is_trivial_scalar_main` rejects any
            // fn call, dict literal, schema, `#import`. Skip
            // the ~86 HashMap inserts + Arc allocations the
            // full `prepare_in_place` performs on the trivial
            // path. Both `lite` (operator opt-in) and
            // `trivial_default` (auto-detected) go through
            // the same classifier guard, so they share the
            // lite-prep shortcut.
            if lite_analyze {
                relon_evaluator::TreeWalkEvaluator::prepare_in_place_lite(&mut ctx);
            } else {
                relon_evaluator::TreeWalkEvaluator::prepare_in_place(&mut ctx);
            }
            ctx
        })
    };
    let _root_loading_guard = ctx.enter_loading_module(cache_namespace.clone());
    let evaluator = TreeWalkEvaluator::new(Arc::clone(&ctx));
    phase("context+tw_evaluator");

    let scope = std::sync::Arc::new(Scope {
        current_dir: canonical_file
            .parent()
            .unwrap_or(std::path::Path::new("."))
            .to_string_lossy()
            .into_owned()
            .into(),
        cache_namespace: cache_namespace.clone().into(),
        ..Scope::default()
    });

    // Branch on whether the file declares `#main(...)`. With a
    // signature, hosts push args; without one, we walk the body
    // as plain data. Both shapes share the same root scope.
    let has_main = workspace
        .modules
        .get(&cache_namespace)
        .and_then(|tree| tree.main_signature.as_ref())
        .is_some();
    let result = if has_main {
        let args_json = match args.as_deref() {
            Some("-") => {
                use std::io::Read;
                let mut buf = String::new();
                std::io::stdin()
                    .read_to_string(&mut buf)
                    .into_diagnostic()
                    .map_err(|e| e.wrap_err("Failed to read --args from stdin"))?;
                buf
            }
            Some(other) => other.to_string(),
            None => {
                return Err(miette::miette!(
                            "File declares `#main(...)`; pass --args '<json>' (or --args -) to provide host arguments"
                        )
                        .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
            }
        };
        let entry_tree = workspace
            .modules
            .get(&cache_namespace)
            .expect("workspace passed has_main check but entry tree missing");
        let signature = entry_tree
            .main_signature
            .as_ref()
            .expect("workspace passed has_main check but entry signature missing");
        let args_map = parse_main_args_json(&args_json, signature, entry_tree).map_err(|e| {
            miette::miette!("{e}")
                .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
        })?;
        // `--backend auto` (default) flows through
        // [`relon::AutoEvaluator`], which (a) probes the
        // on-disk cache (`from_cache_dir` → dlopen-execute)
        // before falling back to `from_source_with_cache`
        // (writes a fresh cache pair as a side-effect), and
        // (b) short-circuits straight to the tree-walker
        // when the source is a trivial scalar `#main` (no
        // cranelift cold-start at all). The second `relon
        // run` of the same source therefore lands on the
        // dlopen path — closing the lap on
        // `--lite`'s tree-walker time. Note: `--trust` does not
        // affect the cranelift-AOT leg of this path (see the
        // `BackendArg::CraneliftAot` arm for why); the only
        // trivial-scalar sources routed here carry no guarded
        // `#native` op, so trust is a no-op on those shapes.
        //
        // v6-fix-D2: `--lite` forces tree-walk for the
        // `#main(...)` path too. The lower / JIT path
        // (cranelift-AOT) dominates the W11 cold-start
        // budget; the tree-walker reaches the same
        // scalar answer with parse + analyze + walk
        // only.
        phase("backend_select");
        match effective_backend {
            BackendArg::Auto => {
                // v6-fix-D2 default-path. Two short-circuits
                // apply here, in priority order:
                //
                // (b) Trivial scalar `#main` (single scalar
                // param + literal / arith body): route
                // straight through the workspace-built
                // tree-walker. The cranelift cold-start
                // (~4-5 ms) is pure overhead on these
                // shapes; the tree-walker reaches the same
                // scalar answer in ~60 µs. We share the
                // already-prepared `evaluator` + workspace
                // so the duplicate parse + analyze that
                // `AutoEvaluator::new` would have done stays
                // out of the hot path.
                //
                // (a) Otherwise drive the cranelift-AOT
                // pipeline via `from_source_with_cache` so
                // the second cold start of the same source
                // can dlopen-execute the cached binary
                // (skips parse + analyze + lower entirely).
                // The cache directory is the conventional
                // `$XDG_CACHE_HOME/relon` (override via env
                // for test isolation).
                if trivial_default {
                    phase("default_trivial_tree_walk");
                    evaluator.run_main(&scope, args_map).map_err(|e| {
                        Report::new(e).with_source_code(NamedSource::new(
                            file.to_string_lossy(),
                            content.clone(),
                        ))
                    })?
                } else {
                    // Non-trivial `auto` routes through cranelift-AOT,
                    // which does not honour `--trust` (see the
                    // `CraneliftAot` arm) — warn rather than silently drop.
                    if trust {
                        warn_trust_unsupported_on_aot();
                    }
                    let cache_dir = relon_codegen_cranelift::default_cache_dir();
                    // Cache-hit fast path: pull a matching
                    // pair off disk if present. Any soft
                    // miss (file absent, integrity failure,
                    // metadata mismatch) returns `Ok(None)`;
                    // a hard I/O failure surfaces as a
                    // logged warning and we fall back to
                    // the source path.
                    let aot_opt = match AotEvaluator::from_cache_dir(&content, &cache_dir) {
                        Ok(opt) => opt,
                        Err(e) => {
                            // Cache infrastructure problem
                            // (not a miss). Log and fall
                            // back to source; we don't
                            // gate the live invocation on
                            // a transient cache issue. The
                            // line goes to stderr so a
                            // tracing subscriber installed
                            // by `RELON_LOG=...` upstream
                            // would already capture it via
                            // the codegen-cranelift
                            // `tracing::warn!` mirrored
                            // there.
                            eprintln!(
                                        "[relon-cli] AOT cache load failed: {e}; falling back to from_source"
                                    );
                            None
                        }
                    };
                    phase("default_cache_probe");
                    // Build the compiled backend (cache hit, or fresh
                    // from source). On a cache miss the source build can
                    // fail because the compiled backend can't *express*
                    // this `#main` shape (e.g. `-> List<P>`) — that is a
                    // capability boundary, not a program error, so auto
                    // adapts by falling back to the tree-walk oracle
                    // instead of surfacing the error. Genuine source
                    // errors (parse / analyze) and host / infra faults
                    // (JIT setup, module define, cache I/O) are *not*
                    // swallowed: they re-surface so the user sees the
                    // real problem. See `CraneliftError::is_unsupported_shape`.
                    let aot = match aot_opt {
                        Some(a) => Some(a),
                        None => match AotEvaluator::from_source_with_cache(&content, &cache_dir) {
                            Ok(a) => Some(a),
                            Err(e) if e.is_unsupported_shape() => {
                                // Observable: the user just lost AOT
                                // acceleration for this run. Mirror the
                                // existing `[relon-cli] ...` stderr style
                                // so a plain run still shows it.
                                eprintln!(
                                    "[relon-cli] auto backend: compiled (cranelift-AOT) path can't \
                                     express this #main shape ({e}); falling back to the tree-walk \
                                     interpreter — this run forgoes AOT acceleration"
                                );
                                phase("default_unsupported_tree_walk");
                                None
                            }
                            Err(e) => {
                                return Err(miette::miette!("auto backend setup: {e}")
                                    .with_source_code(NamedSource::new(
                                        file.to_string_lossy(),
                                        content.clone(),
                                    )));
                            }
                        },
                    };
                    match aot {
                        Some(aot) => EvaluatorTrait::run_main(&aot, args_map).map_err(|e| {
                            Report::new(e).with_source_code(NamedSource::new(
                                file.to_string_lossy(),
                                content.clone(),
                            ))
                        })?,
                        // Fallback: tree-walk is the golden oracle. It
                        // produces the same result the compiled path
                        // would have, or surfaces the genuine runtime /
                        // source error if the program is actually wrong.
                        None => evaluator.run_main(&scope, args_map).map_err(|e| {
                            Report::new(e).with_source_code(NamedSource::new(
                                file.to_string_lossy(),
                                content.clone(),
                            ))
                        })?,
                    }
                }
            }
            BackendArg::TreeWalk => evaluator.run_main(&scope, args_map).map_err(|e| {
                Report::new(e)
                    .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
            })?,
            BackendArg::CraneliftAot => {
                // Cranelift-AOT lowers the entry file through
                // the cranelift JIT and dispatches the same
                // host-args HashMap through `run_main`. We
                // build from source here (single-file entry);
                // workspace-aware multi-file imports remain
                // a v5-γ target for the cranelift backend.
                //
                // `--trust` cannot be honoured on this backend
                // yet: cranelift gates a guarded `#native` call by
                // looking the host fn up in the `CapabilityVtable`
                // (a `cap_bit → HostFnPtr` map) and trapping
                // `CapabilityDenied` on a null slot. Granting a
                // capability therefore means *registering a host
                // fn* via `install_capabilities_mut` /
                // `register_via_gate`, and the CLI ships no host-fn
                // registry — so there is nothing to grant. The
                // scalar `#main` envelope the CLI routes here also
                // lowers to no `CallNative` / `CheckCap` op, so the
                // gate never fires regardless of the trust flag.
                // Wiring `--trust` through requires a host-fn
                // registry plus a gate-driven vtable builder (a
                // cross-crate API addition tracked separately); we
                // deliberately do not fabricate an all-granted but
                // empty vtable here because installing it would
                // change no behaviour.
                // Surface the no-op rather than silently dropping the flag.
                if trust {
                    warn_trust_unsupported_on_aot();
                }
                let aot = AotEvaluator::from_source(&content).map_err(|e| {
                    miette::miette!("cranelift-aot backend setup: {e}")
                        .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
                })?;
                EvaluatorTrait::run_main(&aot, args_map).map_err(|e| {
                    Report::new(e)
                        .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
                })?
            }
        }
    } else {
        if args.is_some() {
            return Err(miette::miette!(
                "--args was provided but the file has no `#main(...)` signature"
            )
            .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
        }
        if matches!(backend, BackendArg::CraneliftAot) {
            return Err(miette::miette!(
                "cranelift-aot backend only supports `#main(...)` entries; the file declares no signature"
            )
            .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
        }
        // `Auto` for library-mode falls through to the tree-walker
        // — cranelift-AOT can't run `eval_root`, so the auto-tier
        // rule ("AOT only on run_main") makes the right choice for free.
        evaluator.eval_root(&scope).map_err(|e| {
            Report::new(e)
                .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
        })?
    };

    phase("evaluate");
    let final_val = relon::to_json_value(result).map_err(|e| {
        miette::miette!("{}", e)
            .with_source_code(NamedSource::new(file.to_string_lossy(), content.clone()))
    })?;
    phase("to_json_value");

    let output = if pretty {
        serde_json::to_string_pretty(&final_val).into_diagnostic()?
    } else {
        serde_json::to_string(&final_val).into_diagnostic()?
    };
    phase("serialise_json");

    if let Some(limit) = run_budget.max_output_bytes {
        let actual = output.len();
        if actual > limit {
            return Err(miette::miette!(
                "JSON output exceeded --max-output-bytes: actual {actual} bytes, limit {limit} bytes"
            )
            .with_source_code(NamedSource::new(file.to_string_lossy(), content)));
        }
    }

    println!("{}", output);
    phase("println");
    Ok(())
}

/// Same logic as the standalone `relon-fmt` binary. Kept here so a
/// single `relon` install covers fmt without an extra crate
/// dependency on the operator's side; the standalone binary stays
/// available for scripts that already wire to it.
fn run_fmt(check: bool, stdout: bool, files: Vec<PathBuf>) -> miette::Result<()> {
    if files.is_empty() {
        return Err(miette::miette!("expected at least one file"));
    }

    let mut failed_check = false;
    for file in &files {
        let source = std::fs::read_to_string(file)
            .into_diagnostic()
            .map_err(|e| e.wrap_err(format!("failed to read {}", file.display())))?;
        let formatted = relon_fmt::format_source(&source)
            .map_err(|e| miette::miette!("{}: {e}", file.display()))?;

        if check {
            if formatted != source {
                eprintln!("{} is not formatted", file.display());
                failed_check = true;
            }
            continue;
        }

        if stdout {
            print!("{formatted}");
        } else if formatted != source {
            std::fs::write(file, formatted)
                .into_diagnostic()
                .map_err(|e| e.wrap_err(format!("failed to write {}", file.display())))?;
        }
    }

    if failed_check {
        Err(miette::miette!("one or more files were not formatted"))
    } else {
        Ok(())
    }
}