synx-core 3.7.1

SYNX — The Active Data Format. Core parser + engine with full !active support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
//! SYNX Engine — resolves active markers (:random, :calc, :env, :alias, :secret, etc.)
//! in a parsed SYNX value tree. Only runs in !active mode.

use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant};
use crate::calc::safe_calc;
use crate::parser;
use crate::rng;
use crate::value::*;

static SPAM_BUCKETS: OnceLock<Mutex<HashMap<String, Vec<Instant>>>> = OnceLock::new();

/// Maximum expression length accepted by :calc (prevents ReDoS/stack abuse).
const MAX_CALC_EXPR_LEN: usize = 4096;
/// Maximum resolved expression length produced by :calc substitutions.
/// Prevents pathological inputs from growing the expression until OOM.
const MAX_CALC_RESOLVED_LEN: usize = 64 * 1024;
/// Maximum file size for :include / :watch reads (10 MB).
const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;
/// Default maximum include depth.
const DEFAULT_MAX_INCLUDE_DEPTH: usize = 16;
/// Maximum object nesting depth for active-mode resolution (prevents stack overflow).
const MAX_RESOLVE_DEPTH: usize = 512;

/// Upper bound for single `String` scratch buffers built from hostile `:template` / replace paths.
const MAX_ENGINE_SCRATCH_STRING: usize = 4 * 1024 * 1024;

/// Validate that `full` path stays within the `base` directory (jail).
/// Returns `Ok(canonical)` or an `Err` describing the violation.
fn jail_path(base: &str, file_path: &str) -> Result<std::path::PathBuf, String> {
    // Always check leading "/" or "\" first so the message is portable:
    // POSIX absolute paths and Windows rooted paths both produce the same
    // "rooted paths are not allowed" string.
    if let Some(first) = file_path.chars().next() {
        if first == '/' || first == '\\' {
            return Err(format!("SECURITY: rooted paths are not allowed: '{}'", file_path));
        }
    }
    // Block any other absolute paths (Windows drive letters, UNC, etc.).
    let fp = std::path::Path::new(file_path);
    if fp.is_absolute() {
        return Err(format!("SECURITY: absolute paths are not allowed: '{}'", file_path));
    }

    let base_canonical = match std::fs::canonicalize(base) {
        Ok(p) => p,
        Err(_) => std::path::PathBuf::from(base),
    };
    let full = base_canonical.join(file_path);
    let full_canonical = match std::fs::canonicalize(&full) {
        Ok(p) => p,
        Err(_) => {
            // File may not exist yet — at least verify no ".." escapes.
            let normalized = full.to_string_lossy();
            if normalized.contains("..") {
                return Err(format!("SECURITY: path traversal detected: '{}'", file_path));
            }
            // Without canonicalisation we cannot prove containment; only
            // accept if the un-canonicalised join still starts with the base.
            if !full.starts_with(&base_canonical) {
                return Err(format!("SECURITY: path escapes base directory: '{}'", file_path));
            }
            return Ok(full);
        }
    };
    if !full_canonical.starts_with(&base_canonical) {
        return Err(format!("SECURITY: path escapes base directory: '{}'", file_path));
    }
    Ok(full_canonical)
}

/// Check file size before reading.
fn check_file_size(path: &std::path::Path) -> Result<(), String> {
    match std::fs::metadata(path) {
        Ok(meta) if meta.len() > MAX_FILE_SIZE => {
            Err(format!("SECURITY: file too large ({} bytes, max {})", meta.len(), MAX_FILE_SIZE))
        }
        _ => Ok(()),
    }
}

/// Normalise std::io::Error kinds to a portable, OS-agnostic message so that
/// INCLUDE_ERR / WATCH_ERR strings don't drift between Windows ("The system
/// cannot find the file specified. (os error 2)") and POSIX ("No such file or
/// directory"). Other kinds fall through to the platform-specific message.
fn fmt_io_err(e: &std::io::Error, ctx: &str) -> String {
    use std::io::ErrorKind;
    match e.kind() {
        ErrorKind::NotFound => format!("file not found: {}", ctx),
        ErrorKind::PermissionDenied => format!("permission denied: {}", ctx),
        _ => e.to_string(),
    }
}

/// Resolve all active-mode markers in a ParseResult.
/// Returns the resolved root Value.
pub fn resolve(result: &mut ParseResult, options: &Options) {
    if result.mode != Mode::Active {
        return;
    }
    let metadata = std::mem::take(&mut result.metadata);
    let includes_directives = std::mem::take(&mut result.includes);
    let use_directives = std::mem::take(&mut result.uses);

    // ── Load !use packages (before includes, so packages are available) ──
    #[cfg(feature = "wasm")]
    let mut wasm_runtime = crate::wasm::WasmMarkerRuntime::new();
    let packages_map = load_packages(
        &use_directives,
        options,
        #[cfg(feature = "wasm")]
        &mut wasm_runtime,
    );

    // If wasm feature is enabled and markers were loaded, create options with runtime
    #[cfg(feature = "wasm")]
    let wasm_options;
    #[cfg(feature = "wasm")]
    let options = if !wasm_runtime.marker_names().is_empty() {
        wasm_options = Options {
            wasm_runtime: Some(std::sync::Arc::new(wasm_runtime)),
            ..options.clone()
        };
        &wasm_options
    } else {
        options
    };

    // ── Load !include files ──
    let mut includes_map = load_includes(&includes_directives, options);

    // ── Pre-pass: also register `:include`/`:import` marker keys as aliases ──
    // This makes `{leaf:<key>}` interpolation work for the README pattern:
    //
    //   db:include ./common.synx
    //   greeting Hello, {site_name:db}!
    //
    // Without this, only `!include` directives feed the alias map.
    if let Value::Object(ref root_map) = result.root {
        for (key, _) in root_map.iter() {
            let meta = match metadata.get("").and_then(|mm| mm.get(key)) {
                Some(m) => m,
                None => continue,
            };
            let is_inc = meta.markers.iter().any(|m| m == "include" || m == "import");
            if !is_inc { continue; }
            // The current value is the path string from the parser.
            let path = match root_map.get(key) {
                Some(Value::String(s)) => s.clone(),
                _ => continue,
            };
            let base = options.base_path.as_deref().unwrap_or(".");
            let full = match jail_path(base, &path) {
                Ok(p) => p,
                Err(_) => continue,
            };
            if check_file_size(&full).is_err() { continue; }
            let text = match std::fs::read_to_string(&full) {
                Ok(t) => t,
                Err(_) => continue,
            };
            let mut included = parser::parse(&text);
            if included.mode == Mode::Active {
                let mut child_opts = options.clone();
                child_opts._include_depth += 1;
                if let Some(parent) = full.parent() {
                    child_opts.base_path = Some(parent.to_string_lossy().into_owned());
                }
                resolve(&mut included, &child_opts);
            }
            includes_map.entry(key.clone()).or_insert(included.root);
        }
    }

    // ── Merge packages into root before resolution ──
    if let Value::Object(ref mut root_map) = result.root {
        for (alias, pkg_value) in &packages_map {
            root_map.entry(alias.clone()).or_insert_with(|| pkg_value.clone());
        }
    }

    // ── :inherit pre-pass ──
    apply_inheritance(&mut result.root, &metadata);
    // Remove private blocks (keys starting with _)
    if let Value::Object(ref mut root_map) = result.root {
        root_map.retain(|k, _| !k.starts_with('_'));
    }

    // ── Build type registry ──
    let type_registry = build_type_registry(&metadata);
    // ── Build constraint registry ──
    let constraint_registry = build_constraint_registry(&metadata);

    // SAFETY: `root_ptr` is a raw pointer to `result.root` used exclusively
    // for *immutable* read access inside marker handlers (:calc, :alias,
    // :map, :watch) that need to look up other keys in the root
    // while also holding a mutable reference to a child object.
    // The invariants that keep this sound:
    //   1. We never write through `root_ptr` — only reads via `&*root_ptr`.
    //   2. Mutable writes go through `map` (the current object), which is
    //      always a distinct subtree from what we read via `root_ptr`.
    //   3. The pointer is valid for the entire duration of `resolve_value`.
    let root_ptr = &mut result.root as *mut Value;
    resolve_value(&mut result.root, root_ptr, options, &metadata, "", &includes_map, 0);

    // ── Validate field constraints (global, by field name) ──
    validate_field_constraints(&mut result.root, &constraint_registry);
    
    // ── Validate field types ──
    validate_field_types(&mut result.root, &type_registry, "");
    
    result.metadata = metadata;
    result.includes = includes_directives;
}

fn resolve_value(
    value: &mut Value,
    root_ptr: *mut Value,
    options: &Options,
    metadata: &HashMap<String, MetaMap>,
    path: &str,
    includes: &HashMap<String, Value>,
    depth: usize,
) {
    // Guard: prevent stack overflow from deeply nested objects
    if depth >= MAX_RESOLVE_DEPTH {
        // Safety: recursion only descends into Object variants (see lines below),
        // so value is always an Object here. Non-Object values are safe to skip.
        if let Value::Object(ref mut map) = value {
            for val in map.values_mut() {
                *val = Value::String(
                    "NESTING_ERR: maximum object nesting depth exceeded".to_string()
                );
            }
        }
        return;
    }

    let meta_map = metadata.get(path).cloned();

    if let Value::Object(ref mut map) = value {
        let keys: Vec<String> = map.keys().cloned().collect();

        // First pass: recurse into nested objects/arrays
        for key in &keys {
            let child_path = if path.is_empty() {
                key.clone()
            } else {
                format!("{}.{}", path, key)
            };

            if let Some(child) = map.get_mut(key) {
                match child {
                    Value::Object(_) => {
                        resolve_value(child, root_ptr, options, metadata, &child_path, includes, depth + 1);
                    }
                    Value::Array(arr) => {
                        for item in arr.iter_mut() {
                            if let Value::Object(_) = item {
                                resolve_value(item, root_ptr, options, metadata, &child_path, includes, depth + 1);
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        // Second pass: apply markers
        if let Some(ref mm) = meta_map {
            for key in &keys {
                let meta = match mm.get(key) {
                    Some(m) => m.clone(),
                    None => continue,
                };

                apply_markers(map, key, &meta, root_ptr, options, path, metadata, includes);
            }
        }

        // Third pass: auto-{} interpolation on all string values
        let keys2: Vec<String> = map.keys().cloned().collect();
        for key in &keys2 {
            if let Some(Value::String(s)) = map.get(key) {
                if s.contains('{') {
                    let root_ref = unsafe { &*root_ptr };
                    let result = resolve_interpolation(s, root_ref, map, includes);
                    if result != *s {
                        map.insert(key.to_string(), Value::String(result));
                    }
                }
            }
        }
    }
}

fn apply_markers(
    map: &mut HashMap<String, Value>,
    key: &str,
    meta: &Meta,
    root_ptr: *mut Value,
    options: &Options,
    path: &str,
    metadata: &HashMap<String, MetaMap>,
    _includes: &HashMap<String, Value>,
) {
    let markers = &meta.markers;

    // ── :spam ──
    // Syntax: key:spam:MAX_CALLS:WINDOW_SEC target
    // WINDOW_SEC defaults to 1 when omitted.
    // If target is a key path, resolves its value after passing the limit check.
    if markers.contains(&"spam".to_string()) {
        let spam_idx = markers.iter().position(|m| m == "spam").unwrap();
        let max_calls = markers
            .get(spam_idx + 1)
            .and_then(|s| s.parse::<usize>().ok())
            .unwrap_or(0);
        let window_sec = markers
            .get(spam_idx + 2)
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(1);

        if max_calls == 0 {
            map.insert(
                key.to_string(),
                Value::String("SPAM_ERR: invalid limit, use :spam:MAX[:WINDOW_SEC]".to_string()),
            );
            return;
        }

        let target = map
            .get(key)
            .map(value_to_string)
            .unwrap_or_else(|| key.to_string());
        let bucket_key = format!("{}::{}", key, target);

        if !allow_spam_access(&bucket_key, max_calls, window_sec) {
            map.insert(
                key.to_string(),
                Value::String(format!(
                    "SPAM_ERR: '{}' exceeded {} calls per {}s",
                    target, max_calls, window_sec
                )),
            );
            return;
        }

        if let Some(resolved) = map
            .get(key)
            .and_then(|v| {
                let t = value_to_string(v);
                let root_ref = unsafe { &*root_ptr };
                deep_get(root_ref, &t).or_else(|| map.get(t.as_str()).cloned())
            })
        {
            map.insert(key.to_string(), resolved);
        }
    }

    // ── :include / :import ──
    if markers.contains(&"include".to_string()) || markers.contains(&"import".to_string()) {
        if let Some(Value::String(file_path)) = map.get(key) {
            let max_depth = options.max_include_depth.unwrap_or(DEFAULT_MAX_INCLUDE_DEPTH);
            if options._include_depth >= max_depth {
                map.insert(
                    key.to_string(),
                    Value::String(format!("INCLUDE_ERR: max include depth ({}) exceeded", max_depth)),
                );
                return;
            }
            let base = options
                .base_path
                .as_deref()
                .unwrap_or(".");
            let full = match jail_path(base, file_path) {
                Ok(p) => p,
                Err(e) => {
                    map.insert(key.to_string(), Value::String(format!("INCLUDE_ERR: {}", e)));
                    return;
                }
            };
            if let Err(e) = check_file_size(&full) {
                map.insert(key.to_string(), Value::String(format!("INCLUDE_ERR: {}", e)));
                return;
            }
            match std::fs::read_to_string(&full) {
                Ok(text) => {
                    let mut included = parser::parse(&text);
                    if included.mode == Mode::Active {
                        let mut child_opts = options.clone();
                        child_opts._include_depth += 1;
                        if let Some(parent) = full.parent() {
                            child_opts.base_path = Some(parent.to_string_lossy().into_owned());
                        }
                        resolve(&mut included, &child_opts);
                    }
                    map.insert(key.to_string(), included.root);
                }
                Err(e) => {
                    map.insert(
                        key.to_string(),
                        Value::String(format!("INCLUDE_ERR: {}", fmt_io_err(&e, file_path))),
                    );
                }
            }
        }
        return;
    }

    // ── :env ──
    if markers.contains(&"env".to_string()) {
        if let Some(Value::String(var_name)) = map.get(key) {
            let env_val = if let Some(ref env_map) = options.env {
                env_map.get(var_name.as_str()).cloned()
            } else {
                std::env::var(var_name).ok()
            };

            let force_string = meta.type_hint.as_deref() == Some("string");
            let default_idx = markers.iter().position(|m| m == "default");
            if let Some(val) = env_val.filter(|v| !v.is_empty()) {
                let resolved = if force_string {
                    Value::String(val)
                } else {
                    cast_primitive(&val)
                };
                map.insert(key.to_string(), resolved);
            } else if let Some(di) = default_idx {
                if markers.len() > di + 1 {
                    // Join all parts after 'default' back with ':'
                    // to preserve IPs (0.0.0.0) and compound values
                    let fallback = markers[di + 1..].join(":");
                    let resolved = if force_string {
                        Value::String(fallback)
                    } else {
                        cast_primitive(&fallback)
                    };
                    map.insert(key.to_string(), resolved);
                } else {
                    map.insert(key.to_string(), Value::Null);
                }
            } else {
                map.insert(key.to_string(), Value::Null);
            }
        }
    }

    // ── :random ──
    if markers.contains(&"random".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            if arr.is_empty() {
                map.insert(key.to_string(), Value::Null);
                return;
            }
            let picked = if !meta.args.is_empty() {
                let weights: Vec<f64> = meta.args.iter().filter_map(|s| s.parse().ok()).collect();
                weighted_random(arr, &weights)
            } else {
                arr[rng::random_usize(arr.len())].clone()
            };
            map.insert(key.to_string(), picked);
        }
    }

    // ── :ref ──
    // Like :alias but feeds the resolved value into subsequent markers.
    // Supports :ref:calc shorthand: key:ref:calc:*2 base_rate → resolves base_rate, then applies "VALUE * 2".
    if markers.contains(&"ref".to_string()) {
        if let Some(Value::String(target)) = map.get(key) {
            let root_ref = unsafe { &*root_ptr };
            let resolved = deep_get(root_ref, target)
                .or_else(|| map.get(target.as_str()).cloned())
                .unwrap_or(Value::Null);

            // If :calc follows with a shorthand expression
            if markers.contains(&"calc".to_string()) {
                if let Some(n) = value_as_number(&resolved) {
                    let calc_idx = markers.iter().position(|m| m == "calc").unwrap();
                    if let Some(calc_expr) = markers.get(calc_idx + 1) {
                        let first = calc_expr.chars().next().unwrap_or(' ');
                        if "+-*/%".contains(first) {
                            let expr = format!("{} {}", format_number(n), calc_expr);
                            match safe_calc(&expr) {
                                Ok(result) => {
                                    let v = if result.fract() == 0.0 && result.abs() < i64::MAX as f64 {
                                        Value::Int(result as i64)
                                    } else {
                                        Value::Float(result)
                                    };
                                    map.insert(key.to_string(), v);
                                }
                                Err(e) => {
                                    map.insert(key.to_string(), Value::String(format!("CALC_ERR: {}", e)));
                                }
                            }
                        } else {
                            map.insert(key.to_string(), resolved);
                        }
                    } else {
                        map.insert(key.to_string(), resolved);
                    }
                } else {
                    map.insert(key.to_string(), resolved);
                }
            } else {
                map.insert(key.to_string(), resolved);
            }
        }
    }

    // ── :i18n ──
    // Selects a localized value from a nested object based on options.lang.
    // Supports pluralization: key:i18n:COUNT_FIELD
    //   When count field is specified, the language entry must contain plural forms:
    //   title:i18n:item_count
    //     en
    //       one {count} item
    //       other {count} items
    //     ru
    //       one {count} предмет
    //       few {count} предмета
    //       many {count} предметов
    //       other {count} предметов
    if markers.contains(&"i18n".to_string()) {
        if let Some(Value::Object(translations)) = map.get(key) {
            let lang = options.lang.as_deref().unwrap_or("en");
            let val = translations.get(lang)
                .or_else(|| translations.get("en"))
                .or_else(|| translations.values().next())
                .cloned()
                .unwrap_or(Value::Null);

            // Check for pluralization: i18n:count_field
            let i18n_idx = markers.iter().position(|m| m == "i18n").unwrap();
            let count_field = markers.get(i18n_idx + 1).cloned();

            if let (Some(ref cf), Value::Object(ref plural_forms)) = (&count_field, &val) {
                // Look up count value from current map or root
                let count_val = map.get(cf)
                    .and_then(value_as_number)
                    .or_else(|| {
                        let root_ref = unsafe { &*root_ptr };
                        deep_get(root_ref, cf).and_then(|v| value_as_number(&v))
                    })
                    .unwrap_or(0.0) as i64;

                let category = plural_category(lang, count_val);
                let chosen = plural_forms.get(category)
                    .or_else(|| plural_forms.get("other"))
                    .or_else(|| plural_forms.values().next())
                    .cloned()
                    .unwrap_or(Value::Null);

                // Substitute {count} in the result string
                if let Value::String(ref s) = chosen {
                    let replaced = s.replace("{count}", &count_val.to_string());
                    map.insert(key.to_string(), Value::String(replaced));
                } else {
                    map.insert(key.to_string(), chosen);
                }
            } else {
                map.insert(key.to_string(), val);
            }
        }
    }

    // ── :calc ──
    if markers.contains(&"calc".to_string()) {
        if let Some(Value::String(expr)) = map.get(key) {
            if expr.len() > MAX_CALC_EXPR_LEN {
                map.insert(
                    key.to_string(),
                    Value::String(format!("CALC_ERR: expression too long ({} chars, max {})", expr.len(), MAX_CALC_EXPR_LEN)),
                );
                return;
            }
            let mut resolved = expr.clone();

            // Substitute variables from root (flat keys)
            let root_ref = unsafe { &*root_ptr };
            if let Value::Object(ref root_map) = root_ref {
                for (rk, rv) in root_map {
                    if let Some(n) = value_as_number(rv) {
                        resolved = replace_word(&resolved, rk, &format_number(n));
                        if resolved.len() > MAX_CALC_RESOLVED_LEN {
                            map.insert(
                                key.to_string(),
                                Value::String(format!(
                                    "CALC_ERR: resolved expression too long (max {} bytes)",
                                    MAX_CALC_RESOLVED_LEN
                                )),
                            );
                            return;
                        }
                    }
                }
            }

            // Substitute from current object (flat keys)
            for (rk, rv) in map.iter() {
                if rk != key {
                    if let Some(n) = value_as_number(rv) {
                        resolved = replace_word(&resolved, rk, &format_number(n));
                        if resolved.len() > MAX_CALC_RESOLVED_LEN {
                            map.insert(
                                key.to_string(),
                                Value::String(format!(
                                    "CALC_ERR: resolved expression too long (max {} bytes)",
                                    MAX_CALC_RESOLVED_LEN
                                )),
                            );
                            return;
                        }
                    }
                }
            }

            // Substitute dot-path references (e.g., base.hp, server.port)
            let root_ref2 = unsafe { &*root_ptr };
            let mut dot_resolved = String::new();
            let bytes = resolved.as_bytes();
            let len = bytes.len();
            let mut i = 0;
            while i < len {
                if is_word_char(bytes[i]) {
                    let start = i;
                    let mut has_dot = false;
                    while i < len && (is_word_char(bytes[i]) || bytes[i] == b'.') {
                        if bytes[i] == b'.' { has_dot = true; }
                        i += 1;
                    }
                    let token = &resolved[start..i];
                    if has_dot && token.contains('.') {
                        if let Some(val) = deep_get(root_ref2, token) {
                            if let Some(n) = value_as_number(&val) {
                                dot_resolved.push_str(&format_number(n));
                                if dot_resolved.len() > MAX_CALC_RESOLVED_LEN {
                                    map.insert(
                                        key.to_string(),
                                        Value::String(format!(
                                            "CALC_ERR: resolved expression too long (max {} bytes)",
                                            MAX_CALC_RESOLVED_LEN
                                        )),
                                    );
                                    return;
                                }
                                continue;
                            }
                        }
                    }
                    dot_resolved.push_str(token);
                    if dot_resolved.len() > MAX_CALC_RESOLVED_LEN {
                        map.insert(
                            key.to_string(),
                            Value::String(format!(
                                "CALC_ERR: resolved expression too long (max {} bytes)",
                                MAX_CALC_RESOLVED_LEN
                            )),
                        );
                        return;
                    }
                } else {
                    dot_resolved.push(bytes[i] as char);
                    i += 1;
                    if dot_resolved.len() > MAX_CALC_RESOLVED_LEN {
                        map.insert(
                            key.to_string(),
                            Value::String(format!(
                                "CALC_ERR: resolved expression too long (max {} bytes)",
                                MAX_CALC_RESOLVED_LEN
                            )),
                        );
                        return;
                    }
                }
            }
            resolved = dot_resolved;

            match safe_calc(&resolved) {
                Ok(result) => {
                    let v = if result.fract() == 0.0 && result.abs() < i64::MAX as f64 {
                        Value::Int(result as i64)
                    } else {
                        Value::Float(result)
                    };
                    map.insert(key.to_string(), v);
                }
                Err(e) => {
                    map.insert(
                        key.to_string(),
                        Value::String(format!("CALC_ERR: {}", e)),
                    );
                }
            }
        }
    }

    // ── :alias ──
    if markers.contains(&"alias".to_string()) {
        if let Some(Value::String(target)) = map.get(key) {
            let target = target.clone();
            // Build the full dot-path of the current key
            let current_path = if path.is_empty() {
                key.to_string()
            } else {
                format!("{}.{}", path, key)
            };
            // Detect direct self-reference: key:alias key
            if target == key || target == current_path {
                map.insert(
                    key.to_string(),
                    Value::String(format!("ALIAS_ERR: self-referential alias: {} → {}", current_path, target)),
                );
            } else {
                // Detect one-hop cycle: a → b → a
                // Only flag as cycle if the target key ALSO has an :alias marker.
                // Without this check, plain string values that happen to match the current
                // key name would produce false-positive ALIAS_ERR results.
                let root_ref = unsafe { &*root_ptr };
                let target_val = deep_get(root_ref, &target);
                // Determine the metadata path of the target key
                let (target_parent, target_key_name) = if let Some(dot) = target.rfind('.') {
                    (target[..dot].to_string(), target[dot + 1..].to_string())
                } else {
                    (String::new(), target.clone())
                };
                let target_has_alias = metadata
                    .get(&target_parent)
                    .and_then(|mm| mm.get(&target_key_name))
                    .map(|m| m.markers.contains(&"alias".to_string()))
                    .unwrap_or(false);
                let is_cycle = target_has_alias && match &target_val {
                    Some(Value::String(s)) => s == key || s == &current_path,
                    _ => false,
                };
                if is_cycle {
                    // Stable, order-independent message: sort the participants
                    // lexicographically so both keys produce the same string
                    // regardless of HashMap iteration order.
                    let (a, b) = if current_path <= target {
                        (current_path.as_str(), target.as_str())
                    } else {
                        (target.as_str(), current_path.as_str())
                    };
                    map.insert(
                        key.to_string(),
                        Value::String(format!("ALIAS_ERR: circular alias detected: {} → {}", a, b)),
                    );
                } else {
                    let val = target_val.unwrap_or(Value::Null);
                    map.insert(key.to_string(), val);
                }
            }
        }
    }

    // ── :secret ──
    if markers.contains(&"secret".to_string()) {
        if let Some(val) = map.get(key) {
            let s = value_to_string(val);
            map.insert(key.to_string(), Value::Secret(s));
        }
    }

    // ── :unique ──
    if markers.contains(&"unique".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let mut seen = Vec::new();
            let mut unique = Vec::new();
            for item in arr {
                let s = value_to_string(item);
                if !seen.contains(&s) {
                    seen.push(s);
                    unique.push(item.clone());
                }
            }
            map.insert(key.to_string(), Value::Array(unique));
        }
    }

    // ── :geo ──
    if markers.contains(&"geo".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let region = options.region.as_deref().unwrap_or("US");
            let prefix = format!("{} ", region);
            let found = arr.iter().find(|item| {
                if let Value::String(s) = item {
                    s.starts_with(&prefix)
                } else {
                    false
                }
            });

            let result = if let Some(Value::String(s)) = found {
                Value::String(s[prefix.len()..].trim().to_string())
            } else if let Some(first) = arr.first() {
                if let Value::String(s) = first {
                    if let Some(space) = s.find(' ') {
                        Value::String(s[space + 1..].trim().to_string())
                    } else {
                        first.clone()
                    }
                } else {
                    first.clone()
                }
            } else {
                Value::Null
            };
            map.insert(key.to_string(), result);
        }
    }

    // ── :template (legacy — handled by auto-{} in resolve_value) ──

    // ── :split ──
    if markers.contains(&"split".to_string()) {
        if let Some(Value::String(s)) = map.get(key) {
            let split_idx = markers.iter().position(|m| m == "split").unwrap();
            let sep = if split_idx + 1 < markers.len() {
                delimiter_from_keyword(&markers[split_idx + 1])
            } else {
                ",".to_string()
            };
            let items: Vec<Value> = s
                .split(&sep)
                .map(|p| p.trim())
                .filter(|p| !p.is_empty())
                .map(|p| cast_primitive(p))
                .collect();
            map.insert(key.to_string(), Value::Array(items));
        }
    }

    // ── :join ──
    if markers.contains(&"join".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let join_idx = markers.iter().position(|m| m == "join").unwrap();
            let sep = if join_idx + 1 < markers.len() {
                delimiter_from_keyword(&markers[join_idx + 1])
            } else {
                ",".to_string()
            };
            let joined: String = arr
                .iter()
                .map(|v| value_to_string(v))
                .collect::<Vec<_>>()
                .join(&sep);
            map.insert(key.to_string(), Value::String(joined));
        }
    }

    // ── :default (standalone, without :env) ──
    if markers.contains(&"default".to_string()) && !markers.contains(&"env".to_string()) {
        let is_empty = match map.get(key) {
            Some(Value::Null) | None => true,
            Some(Value::String(s)) if s.is_empty() => true,
            _ => false,
        };
        if is_empty {
            let di = markers.iter().position(|m| m == "default").unwrap();
            if markers.len() > di + 1 {
                let fallback = markers[di + 1..].join(":");
                let resolved = if meta.type_hint.as_deref() == Some("string") {
                    Value::String(fallback)
                } else {
                    cast_primitive(&fallback)
                };
                map.insert(key.to_string(), resolved);
            }
        }
    }

    // ── :clamp ──
    // Syntax: key:clamp:MIN:MAX value
    // Clamps a numeric value to [MIN, MAX].
    if markers.contains(&"clamp".to_string()) {
        let clamp_idx = markers.iter().position(|m| m == "clamp").unwrap();
        let min_s = markers.get(clamp_idx + 1).cloned().unwrap_or_default();
        let max_s = markers.get(clamp_idx + 2).cloned().unwrap_or_default();
        if let (Ok(lo), Ok(hi)) = (min_s.parse::<f64>(), max_s.parse::<f64>()) {
            if lo > hi {
                map.insert(key.to_string(), Value::String(
                    format!("CONSTRAINT_ERR: clamp min ({}) > max ({})", lo, hi),
                ));
            } else if let Some(n) = map.get(key).and_then(value_as_number) {
                let clamped = n.clamp(lo, hi);
                let v = if clamped.fract() == 0.0 && clamped.abs() < i64::MAX as f64 {
                    Value::Int(clamped as i64)
                } else {
                    Value::Float(clamped)
                };
                map.insert(key.to_string(), v);
            }
        }
    }

    // ── :round ──
    // Syntax: key:round:N value  (N = decimal places, default 0)
    // Works standalone or after :calc: key:calc:round:2 expr
    if markers.contains(&"round".to_string()) {
        let round_idx = markers.iter().position(|m| m == "round").unwrap();
        let decimals: u32 = markers.get(round_idx + 1)
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        if let Some(n) = map.get(key).and_then(value_as_number) {
            let factor = 10f64.powi(decimals as i32);
            let rounded = (n * factor).round() / factor;
            let v = if decimals == 0 {
                Value::Int(rounded as i64)
            } else {
                Value::Float(rounded)
            };
            map.insert(key.to_string(), v);
        }
    }

    // ── :map ──
    // Syntax: key:map:source_key\n  - lookup_val result
    // Looks up `source_key` in root, finds matching "lookup_val result" entry in the array.
    if markers.contains(&"map".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let map_idx = markers.iter().position(|m| m == "map").unwrap();
            let source_key = markers.get(map_idx + 1).cloned().unwrap_or_default();
            let lookup_val = if !source_key.is_empty() {
                let root_ref = unsafe { &*root_ptr };
                deep_get(root_ref, &source_key)
                    .or_else(|| map.get(&source_key).cloned())
                    .map(|v| value_to_string(&v))
                    .unwrap_or_default()
            } else {
                // Use the current string value as lookup key
                match map.get(key) {
                    Some(Value::String(s)) => s.clone(),
                    _ => String::new(),
                }
            };

            // Find matching entry: "lookup_val result_text"
            let arr_clone = arr.clone();
            let result = arr_clone.iter().find_map(|item| {
                if let Value::String(s) = item {
                    if let Some(space) = s.find(' ') {
                        if s[..space].trim() == lookup_val {
                            return Some(cast_primitive(s[space + 1..].trim()));
                        }
                    }
                }
                None
            });
            map.insert(key.to_string(), result.unwrap_or(Value::Null));
        }
    }

    // ── :format ──
    // Syntax: key:format:PATTERN value  (printf-style: %.2f, %d, %05d, %e)
    // Converts numeric or string value to a formatted string.
    if markers.contains(&"format".to_string()) {
        let fmt_idx = markers.iter().position(|m| m == "format").unwrap();
        let pattern = markers.get(fmt_idx + 1).cloned().unwrap_or_else(|| "%s".to_string());
        if let Some(current) = map.get(key) {
            let formatted = apply_format_pattern(&pattern, current);
            map.insert(key.to_string(), Value::String(formatted));
        }
    }

    // ── :replace:FROM:TO ──    (since 3.6.2)
    // Literal substring replacement on a string value. `TO` defaults to "" (deletion).
    // `FROM`/`TO` cannot contain ':' because the marker chain is colon-delimited;
    // for those cases use `{interpolation}` instead.
    if markers.contains(&"replace".to_string()) {
        if let Some(Value::String(s)) = map.get(key) {
            let idx = markers.iter().position(|m| m == "replace").unwrap();
            let from = markers.get(idx + 1).cloned().unwrap_or_default();
            let to = markers.get(idx + 2).cloned().unwrap_or_default();
            if !from.is_empty() {
                let replaced = s.replace(&from, &to);
                map.insert(key.to_string(), Value::String(replaced));
            }
        }
    }

    // ── :sort  /  :sort:desc ──    (since 3.6.2)
    // Sort an array. Numeric items compare numerically; otherwise lexicographic.
    if markers.contains(&"sort".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let idx = markers.iter().position(|m| m == "sort").unwrap();
            let desc = matches!(markers.get(idx + 1).map(|s| s.as_str()), Some("desc"));
            let mut sorted = arr.clone();
            sorted.sort_by(|a, b| {
                match (value_as_number(a), value_as_number(b)) {
                    (Some(an), Some(bn)) => an
                        .partial_cmp(&bn)
                        .unwrap_or(std::cmp::Ordering::Equal),
                    _ => value_to_string(a).cmp(&value_to_string(b)),
                }
            });
            if desc { sorted.reverse(); }
            map.insert(key.to_string(), Value::Array(sorted));
        }
    }

    // ── :sum ──    (since 3.6.2)
    // Sum the numeric items of an array. Non-numeric items are ignored.
    // Returns Int when all summands are integers, Float otherwise.
    if markers.contains(&"sum".to_string()) {
        if let Some(Value::Array(arr)) = map.get(key) {
            let mut total: f64 = 0.0;
            let mut all_int = true;
            for v in arr {
                match v {
                    Value::Int(n) => total += *n as f64,
                    Value::Float(f) => {
                        total += *f;
                        if f.fract() != 0.0 { all_int = false; }
                    }
                    Value::String(s) => {
                        if let Ok(f) = s.parse::<f64>() {
                            total += f;
                            if f.fract() != 0.0 { all_int = false; }
                        }
                    }
                    _ => {}
                }
            }
            let result = if all_int && total.fract() == 0.0 && total.abs() < i64::MAX as f64 {
                Value::Int(total as i64)
            } else {
                Value::Float(total)
            };
            map.insert(key.to_string(), result);
        }
    }

    // ── :fallback ──
    // Syntax: key:fallback:DEFAULT_PATH value
    // If the value (treated as a file path) doesn't exist on disk, use the fallback.
    // Falls back to default if value is also null/empty.
    if markers.contains(&"fallback".to_string()) {
        let fb_idx = markers.iter().position(|m| m == "fallback").unwrap();
        let default_val = markers.get(fb_idx + 1).cloned().unwrap_or_default();
        let use_fallback = match map.get(key) {
            None | Some(Value::Null) => true,
            Some(Value::String(s)) if s.is_empty() => true,
            Some(Value::String(s)) => {
                let base = options.base_path.as_deref().unwrap_or(".");
                match jail_path(base, s) {
                    Ok(safe) => !safe.exists(),
                    Err(_) => true, // path escapes jail → treat as missing → use fallback
                }
            }
            _ => false,
        };
        if use_fallback && !default_val.is_empty() {
            map.insert(key.to_string(), Value::String(default_val));
        }
    }

    // ── :once ──
    // Syntax: key:once  or  key:once:uuid  or  key:once:random  or  key:once:timestamp
    // Generates a value once and persists it in a .synx.lock sidecar file.
    if markers.contains(&"once".to_string()) {
        let once_idx = markers.iter().position(|m| m == "once").unwrap();
        let gen_type = markers.get(once_idx + 1).map(|s| s.as_str()).unwrap_or("uuid");
        let lock_path = options.base_path.as_deref()
            .map(|b| std::path::Path::new(b).join(".synx.lock"))
            .unwrap_or_else(|| std::path::Path::new(".synx.lock").to_path_buf());

        // Try to read existing value from lock file
        let existing = read_lock_value(&lock_path, key);
        if let Some(locked) = existing {
            map.insert(key.to_string(), Value::String(locked));
        } else {
            let generated = match gen_type {
                "uuid" => rng::generate_uuid(),
                "timestamp" => std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs()
                    .to_string(),
                "random" => rng::random_usize(u32::MAX as usize).to_string(),
                _ => rng::generate_uuid(),
            };
            write_lock_value(&lock_path, key, &generated);
            map.insert(key.to_string(), Value::String(generated));
        }
    }

    // ── :version ──
    // Syntax: key:version:OP:REQUIRED value
    // Compares the value (current version) against REQUIRED using OP (>=, <=, >, <, ==, !=).
    // Returns a bool.
    if markers.contains(&"version".to_string()) {
        if let Some(Value::String(current_ver)) = map.get(key) {
            let ver_idx = markers.iter().position(|m| m == "version").unwrap();
            let op = markers.get(ver_idx + 1).map(|s| s.as_str()).unwrap_or(">=");
            let required = markers.get(ver_idx + 2).cloned().unwrap_or_default();
            let result = compare_versions(current_ver, op, &required);
            map.insert(key.to_string(), Value::Bool(result));
        }
    }

    // ── :watch ──
    // Syntax: key:watch:KEY_PATH ./file.json  (or ./file.synx)
    // Reads the referenced file at parse time. Optionally extracts a key path (JSON/SYNX).
    if markers.contains(&"watch".to_string()) {
        if let Some(Value::String(file_path)) = map.get(key) {
            let max_depth = options.max_include_depth.unwrap_or(DEFAULT_MAX_INCLUDE_DEPTH);
            if options._include_depth >= max_depth {
                map.insert(
                    key.to_string(),
                    Value::String(format!("WATCH_ERR: max include depth ({}) exceeded", max_depth)),
                );
                return;
            }
            let base = options.base_path.as_deref().unwrap_or(".");
            let full = match jail_path(base, file_path) {
                Ok(p) => p,
                Err(e) => {
                    map.insert(key.to_string(), Value::String(format!("WATCH_ERR: {}", e)));
                    return;
                }
            };
            if let Err(e) = check_file_size(&full) {
                map.insert(key.to_string(), Value::String(format!("WATCH_ERR: {}", e)));
                return;
            }
            let watch_idx = markers.iter().position(|m| m == "watch").unwrap();
            let key_path = markers.get(watch_idx + 1).cloned();

            match std::fs::read_to_string(&full) {
                Ok(content) => {
                    let value = if let Some(ref kp) = key_path {
                        extract_from_file_content(&content, kp, full.extension().and_then(|e| e.to_str()).unwrap_or("")).unwrap_or(Value::Null)
                    } else {
                        Value::String(content.trim().to_string())
                    };
                    map.insert(key.to_string(), value);
                }
                Err(e) => {
                    map.insert(key.to_string(), Value::String(format!("WATCH_ERR: {}", fmt_io_err(&e, file_path))));
                }
            }
        }
    }

    // ── :prompt ──
    // Syntax: key:prompt:LABEL subtree
    // Converts the resolved subtree (object) into a SYNX-formatted string
    // wrapped in a labeled code fence, ready for LLM prompt embedding.
    if markers.contains(&"prompt".to_string()) {
        let prompt_idx = markers.iter().position(|m| m == "prompt").unwrap();
        let label = markers.get(prompt_idx + 1).cloned().unwrap_or_else(|| key.to_string());
        if let Some(val) = map.get(key) {
            let synx_text = stringify_value(val, 0);
            let block = format!("{} (SYNX):\n```synx\n{}```", label, synx_text);
            map.insert(key.to_string(), Value::String(block));
        }
    }

    // ── :vision ──
    // Metadata-only marker. Recognized by the engine (no error), value passes through.
    // Applications detect this marker via metadata to dispatch image generation.

    // ── :audio ──
    // Metadata-only marker. Recognized by the engine (no error), value passes through.
    // Applications detect this marker via metadata to dispatch audio generation.

    // ── WASM custom markers ──
    // If a marker is not built-in and a WASM runtime is loaded, dispatch to it.
    #[cfg(feature = "wasm")]
    if let Some(ref wasm_rt) = options.wasm_runtime {
        for marker in markers {
            if crate::wasm::BUILTIN_MARKERS.contains(&marker.as_str()) {
                continue;
            }
            if wasm_rt.has_marker(marker) {
                // Collect args: all marker parts after this marker name
                let marker_idx = markers.iter().position(|m| m == marker).unwrap();
                let args: Vec<String> = markers[marker_idx + 1..].to_vec();
                let current_value = map.get(key).cloned().unwrap_or(Value::Null);
                match wasm_rt.apply_marker(marker, &current_value, &args) {
                    Ok(result) => {
                        map.insert(key.to_string(), result);
                    }
                    Err(e) => {
                        map.insert(key.to_string(), Value::String(format!("WASM_ERR: {}", e)));
                    }
                }
                break; // Only apply one WASM marker per key
            }
        }
    }

    // ── Constraint validation (always last, after all markers resolved) ──
    if let Some(ref c) = meta.constraints {
        validate_constraints(map, key, c);
    }
}

// ─── Constraint enforcement ───────────────────────────────

fn validate_constraints(map: &mut HashMap<String, Value>, key: &str, c: &Constraints) {
    let val = match map.get(key) {
        Some(v) => v.clone(),
        None => {
            if c.required {
                map.insert(key.to_string(), Value::String(
                    format!("CONSTRAINT_ERR: '{}' is required", key),
                ));
            }
            return;
        }
    };

    // required
    if c.required {
        let empty = matches!(val, Value::Null)
            || matches!(&val, Value::String(s) if s.is_empty());
        if empty {
            map.insert(key.to_string(), Value::String(
                format!("CONSTRAINT_ERR: '{}' is required", key),
            ));
            return;
        }
    }

    // type check
    if let Some(ref type_name) = c.type_name {
        let ok = match type_name.as_str() {
            "int"    => matches!(val, Value::Int(_)),
            "float"  => matches!(val, Value::Float(_) | Value::Int(_)),
            "bool"   => matches!(val, Value::Bool(_)),
            "string" => matches!(val, Value::String(_)),
            _        => true,
        };
        if !ok {
            map.insert(key.to_string(), Value::String(
                format!("CONSTRAINT_ERR: '{}' expected type '{}'", key, type_name),
            ));
            return;
        }
    }

    // enum check
    if let Some(ref enum_vals) = c.enum_values {
        let val_str = match &val {
            Value::String(s) => s.clone(),
            Value::Int(n)    => n.to_string(),
            Value::Float(f)  => f.to_string(),
            Value::Bool(b)   => b.to_string(),
            _                => String::new(),
        };
        if !enum_vals.contains(&val_str) {
            map.insert(key.to_string(), Value::String(
                format!("CONSTRAINT_ERR: '{}' must be one of [{}]", key, enum_vals.join("|")),
            ));
            return;
        }
    }

    // min / max  (numbers: value range; strings: length range)
    let num = match &val {
        Value::Int(n)    => Some(*n as f64),
        Value::Float(f)  => Some(*f),
        Value::String(s) if c.min.is_some() || c.max.is_some() => Some(s.len() as f64),
        _                => None,
    };
    if let Some(n) = num {
        if let Some(min) = c.min {
            if n < min {
                map.insert(key.to_string(), Value::String(
                    format!("CONSTRAINT_ERR: '{}' value {} is below min {}", key, n, min),
                ));
                return;
            }
        }
        if let Some(max) = c.max {
            if n > max {
                map.insert(key.to_string(), Value::String(
                    format!("CONSTRAINT_ERR: '{}' value {} exceeds max {}", key, n, max),
                ));
                return;
            }
        }
    }

    // pattern (regex match — bounded length to avoid pathological compilation costs)
    if let Some(ref pat) = c.pattern {
        if pat.len() <= 256 {
            if let Value::String(ref s) = val {
                match regex::Regex::new(pat) {
                    Ok(re) if !re.is_match(s) => {
                        map.insert(key.to_string(), Value::String(
                            format!("CONSTRAINT_ERR: '{}' does not match pattern /{}/", key, pat),
                        ));
                        return;
                    }
                    Ok(_) => {}
                    // Invalid regex — skip silently, matching the JS engine.
                    Err(_) => {}
                }
            }
        }
    }
}

// ─── New-marker helpers ───────────────────────────────────

/// Apply a printf-style format pattern to a value.
fn apply_format_pattern(pattern: &str, value: &Value) -> String {
    match value {
        Value::Int(n) => {
            if pattern.contains('d') || pattern.contains('i') {
                format_int_pattern(pattern, *n)
            } else if pattern.contains('f') || pattern.contains('e') {
                format_float_pattern(pattern, *n as f64)
            } else {
                n.to_string()
            }
        }
        Value::Float(f) => {
            if pattern.contains('f') || pattern.contains('e') {
                format_float_pattern(pattern, *f)
            } else {
                format_number(*f)
            }
        }
        Value::String(s) => s.clone(),
        other => value_to_string(other),
    }
}

fn format_int_pattern(pattern: &str, n: i64) -> String {
    // Guardrail: user-controlled width can be enormous (esp. under fuzzing).
    // Large widths can cause pathological allocations or panics in formatting internals.
    const MAX_FMT_WIDTH: usize = 4096;
    if let Some(s) = pattern.strip_prefix('%') {
        if let Some(inner) = s.strip_suffix('d').or_else(|| s.strip_suffix('i')) {
            if let Some(w) = inner.strip_prefix('0') {
                if let Ok(width) = w.parse::<usize>() {
                    let width = width.min(MAX_FMT_WIDTH);
                    return format!("{:0>width$}", n, width = width);
                }
            }
            if let Ok(width) = inner.parse::<usize>() {
                let width = width.min(MAX_FMT_WIDTH);
                return format!("{:>width$}", n, width = width);
            }
        }
    }
    n.to_string()
}

fn format_float_pattern(pattern: &str, f: f64) -> String {
    // Same rationale as MAX_FMT_WIDTH: avoid pathological precision values.
    const MAX_FMT_PREC: usize = 1024;
    if let Some(s) = pattern.strip_prefix('%') {
        // %e — exponential form, matches the JS `Number.toExponential()` shape:
        //   e.g. 123456.789 → "1.23456789e+5"
        if s == "e" {
            if f == 0.0 {
                return "0e+0".to_string();
            }
            // Use Rust's exponential formatter (which uses ryu-like shortest
            // round-trip digits) and then reshape the exponent token to match
            // JS `toExponential()` (always signed, no leading zeros).
            let raw = format!("{:e}", f); // e.g. "1.23456789e5" or "1.23456789e-5"
            if let Some(epos) = raw.rfind('e') {
                let mantissa = &raw[..epos];
                let exp_part = &raw[epos + 1..];
                let (sign, digits) = match exp_part.chars().next() {
                    Some('+') => ('+', &exp_part[1..]),
                    Some('-') => ('-', &exp_part[1..]),
                    _ => ('+', exp_part),
                };
                // Strip the decimal point if the mantissa is an integer (1.0 → 1)
                let mantissa_clean = if mantissa.ends_with(".0") {
                    &mantissa[..mantissa.len() - 2]
                } else {
                    mantissa
                };
                return format!("{}e{}{}", mantissa_clean, sign, digits);
            }
            return raw;
        }
        if let Some(inner) = s.strip_suffix('f').or_else(|| s.strip_suffix('e')) {
            if let Some(prec_s) = inner.strip_prefix('.') {
                if let Ok(prec) = prec_s.parse::<usize>() {
                    let prec = prec.min(MAX_FMT_PREC);
                    if s.ends_with('e') {
                        return format!("{:.prec$e}", f, prec = prec);
                    }
                    return format!("{:.prec$}", f, prec = prec);
                }
            }
        }
    }
    f.to_string()
}

/// Read a persisted value from the .synx.lock file.
fn read_lock_value(lock_path: &std::path::Path, key: &str) -> Option<String> {
    let content = std::fs::read_to_string(lock_path).ok()?;
    for line in content.lines() {
        if let Some(rest) = line.strip_prefix(key) {
            if rest.starts_with(' ') {
                return Some(rest.trim_start().to_string());
            }
        }
    }
    None
}

/// Write/update a key value pair in the .synx.lock file.
fn write_lock_value(lock_path: &std::path::Path, key: &str, value: &str) {
    let mut lines: Vec<String> = std::fs::read_to_string(lock_path)
        .unwrap_or_default()
        .lines()
        .map(|l| l.to_string())
        .collect();

    let new_line = format!("{} {}", key, value);
    let mut found = false;
    for line in lines.iter_mut() {
        if line.starts_with(key) && line[key.len()..].starts_with(' ') {
            *line = new_line.clone();
            found = true;
            break;
        }
    }
    if !found {
        lines.push(new_line);
    }
    let _ = std::fs::write(lock_path, lines.join("\n") + "\n");
}

/// Compare two version strings using a comparison operator.
fn compare_versions(current: &str, op: &str, required: &str) -> bool {
    let parse_ver = |s: &str| -> Vec<u64> {
        s.split('.').filter_map(|p| p.parse().ok()).collect()
    };
    let cv = parse_ver(current);
    let rv = parse_ver(required);
    let len = cv.len().max(rv.len());
    let mut ord = std::cmp::Ordering::Equal;
    for i in 0..len {
        let a = cv.get(i).copied().unwrap_or(0);
        let b = rv.get(i).copied().unwrap_or(0);
        if a != b {
            ord = a.cmp(&b);
            break;
        }
    }
    match op {
        ">=" => ord != std::cmp::Ordering::Less,
        "<=" => ord != std::cmp::Ordering::Greater,
        ">"  => ord == std::cmp::Ordering::Greater,
        "<"  => ord == std::cmp::Ordering::Less,
        "==" | "=" => ord == std::cmp::Ordering::Equal,
        "!=" => ord != std::cmp::Ordering::Equal,
        _ => false,
    }
}

fn allow_spam_access(bucket_key: &str, max_calls: usize, window_sec: u64) -> bool {
    let now = Instant::now();
    let window = Duration::from_secs(window_sec.max(1));

    let buckets = SPAM_BUCKETS.get_or_init(|| Mutex::new(HashMap::new()));
    let mut guard = match buckets.lock() {
        Ok(g) => g,
        Err(poisoned) => poisoned.into_inner(),
    };

    let calls = guard.entry(bucket_key.to_string()).or_default();
    calls.retain(|ts| now.duration_since(*ts) <= window);

    if calls.len() >= max_calls {
        return false;
    }

    calls.push(now);
    true
}

#[cfg(test)]
fn clear_spam_buckets() {
    let buckets = SPAM_BUCKETS.get_or_init(|| Mutex::new(HashMap::new()));
    if let Ok(mut guard) = buckets.lock() {
        guard.clear();
    }
}

/// Extract a value from file content by key path (JSON dot-path or SYNX key).
fn extract_from_file_content(content: &str, key_path: &str, ext: &str) -> Option<Value> {
    if ext == "json" {
        // Real JSON parse via serde_json — supports dot-path traversal.
        let parsed: serde_json::Value = serde_json::from_str(content).ok()?;
        let mut current = &parsed;
        for part in key_path.split('.') {
            current = current.get(part)?;
        }
        return Some(json_value_to_synx(current));
    }

    // SYNX file: parse it and follow the dot-path through the resulting tree.
    let parsed = crate::parser::parse(content);
    let mut current = &parsed.root;
    for part in key_path.split('.') {
        match current {
            Value::Object(map) => {
                current = map.get(part)?;
            }
            _ => return None,
        }
    }
    Some(current.clone())
}

/// Convert a `serde_json::Value` to `Value` (best-effort).
fn json_value_to_synx(v: &serde_json::Value) -> Value {
    match v {
        serde_json::Value::Null => Value::Null,
        serde_json::Value::Bool(b) => Value::Bool(*b),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Value::Int(i)
            } else if let Some(f) = n.as_f64() {
                Value::Float(f)
            } else {
                Value::Null
            }
        }
        serde_json::Value::String(s) => Value::String(s.clone()),
        serde_json::Value::Array(arr) => {
            Value::Array(arr.iter().map(json_value_to_synx).collect())
        }
        serde_json::Value::Object(map) => {
            let mut out = HashMap::new();
            for (k, v) in map {
                out.insert(k.clone(), json_value_to_synx(v));
            }
            Value::Object(out)
        }
    }
}

// ─── Helpers ─────────────────────────────────────────────

/// Serialize a Value to SYNX format string (for :prompt marker).
fn stringify_value(value: &Value, indent: usize) -> String {
    let spaces = " ".repeat(indent);
    match value {
        Value::Object(map) => {
            let mut out = String::new();
            let mut keys: Vec<&str> = map.keys().map(|k| k.as_str()).collect();
            keys.sort_unstable();
            for key in keys {
                let val = &map[key];
                match val {
                    Value::Object(_) => {
                        out.push_str(&format!("{}{}\n", spaces, key));
                        out.push_str(&stringify_value(val, indent + 2));
                    }
                    Value::Array(arr) => {
                        out.push_str(&format!("{}{}\n", spaces, key));
                        for item in arr {
                            out.push_str(&format!("{}  - {}\n", spaces, value_to_string(item)));
                        }
                    }
                    _ => {
                        out.push_str(&format!("{}{} {}\n", spaces, key, value_to_string(val)));
                    }
                }
            }
            out
        }
        _ => format!("{}{}\n", spaces, value_to_string(value)),
    }
}

pub(crate) fn cast_primitive(val: &str) -> Value {
    // Quoted strings preserve literal value
    if val.len() >= 2 {
        let bytes = val.as_bytes();
        if (bytes[0] == b'"' && bytes[bytes.len() - 1] == b'"')
            || (bytes[0] == b'\'' && bytes[bytes.len() - 1] == b'\'')
        {
            return Value::String(val[1..val.len() - 1].to_string());
        }
    }
    match val {
        "true" => Value::Bool(true),
        "false" => Value::Bool(false),
        "null" => Value::Null,
        _ => {
            if let Ok(i) = val.parse::<i64>() {
                Value::Int(i)
            } else if let Ok(f) = val.parse::<f64>() {
                Value::Float(f)
            } else {
                Value::String(val.to_string())
            }
        }
    }
}

fn delimiter_from_keyword(keyword: &str) -> String {
    match keyword {
        "space" => " ".to_string(),
        "pipe" => "|".to_string(),
        "dash" => "-".to_string(),
        "dot" => ".".to_string(),
        "semi" => ";".to_string(),
        "tab" => "\t".to_string(),
        "slash" => "/".to_string(),
        other => other.to_string(),
    }
}

fn value_as_number(v: &Value) -> Option<f64> {
    match v {
        Value::Int(n) => Some(*n as f64),
        Value::Float(f) => Some(*f),
        _ => None,
    }
}

fn value_to_string(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Int(n) => n.to_string(),
        Value::Float(f) => format_number(*f as f64),
        Value::Bool(b) => b.to_string(),
        Value::Null => "null".to_string(),
        Value::Secret(s) => s.clone(),
        Value::Array(_) | Value::Object(_) => String::new(),
    }
}

fn format_number(n: f64) -> String {
    if n.fract() == 0.0 && n.abs() < i64::MAX as f64 {
        (n as i64).to_string()
    } else {
        n.to_string()
    }
}

/// Replace whole-word occurrences of `word` with `replacement`.
fn replace_word(haystack: &str, word: &str, replacement: &str) -> String {
    let word_bytes = word.as_bytes();
    let word_len = word_bytes.len();
    let hay_bytes = haystack.as_bytes();
    let hay_len = hay_bytes.len();

    if word_len > hay_len {
        return haystack.to_string();
    }

    let mut result = String::with_capacity(hay_len.min(MAX_ENGINE_SCRATCH_STRING));
    let mut i = 0;

    while i <= hay_len - word_len {
        if result.len() >= MAX_ENGINE_SCRATCH_STRING {
            break;
        }
        if &hay_bytes[i..i + word_len] == word_bytes {
            let before_ok = i == 0 || !is_word_char(hay_bytes[i - 1]);
            let after_ok = i + word_len >= hay_len || !is_word_char(hay_bytes[i + word_len]);
            if before_ok && after_ok {
                let room = MAX_ENGINE_SCRATCH_STRING.saturating_sub(result.len());
                if room > 0 {
                    let take = replacement.len().min(room);
                    let end = replacement.floor_char_boundary(take);
                    result.push_str(&replacement[..end]);
                }
                i += word_len;
                continue;
            }
        }
        if result.len() < MAX_ENGINE_SCRATCH_STRING {
            result.push(hay_bytes[i] as char);
        }
        i += 1;
    }
    while i < hay_len && result.len() < MAX_ENGINE_SCRATCH_STRING {
        result.push(hay_bytes[i] as char);
        i += 1;
    }
    result
}

fn is_word_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

fn weighted_random(items: &[Value], weights: &[f64]) -> Value {
    let mut w: Vec<f64> = weights.to_vec();
    if w.len() < items.len() {
        let assigned: f64 = w.iter().sum();
        // If the explicit weights already exceed 100, give unassigned items
        // the same average weight as the assigned ones so they remain visible.
        // If there is room left under 100, distribute the remainder equally.
        let per_item = if assigned < 100.0 {
            (100.0 - assigned) / (items.len() - w.len()) as f64
        } else {
            assigned / w.len() as f64
        };
        while w.len() < items.len() {
            w.push(per_item);
        }
    }
    let total: f64 = w.iter().sum();
    if total <= 0.0 {
        return items[rng::random_usize(items.len())].clone();
    }

    let rand_val = rng::random_f64_01();
    let mut cumulative = 0.0;
    for (i, item) in items.iter().enumerate() {
        cumulative += w[i] / total;
        if rand_val <= cumulative {
            return item.clone();
        }
    }
    items.last().cloned().unwrap_or(Value::Null)
}

// ─── Inheritance pre-pass ─────────────────────────────────

fn apply_inheritance(root: &mut Value, metadata: &HashMap<String, MetaMap>) {
    let root_meta = match metadata.get("") {
        Some(m) => m.clone(),
        None => return,
    };

    let root_map = match root.as_object_mut() {
        Some(m) => m as *mut HashMap<String, Value>,
        None => return,
    };

    // Collect inherit targets: child_key → [parent1, parent2, ...]
    //
    // Two surface syntaxes feed this list:
    //   production:inherit:base   → parents come from markers[idx+1..]
    //   production:inherit base   → parser promotes "base" into meta.args
    let mut inherits: Vec<(String, Vec<String>)> = Vec::new();
    for (key, meta) in &root_meta {
        if meta.markers.contains(&"inherit".to_string()) {
            let idx = meta.markers.iter().position(|m| m == "inherit").unwrap();
            let mut parents: Vec<String> = meta.markers[idx + 1..].to_vec();
            // Fall back / extend with marker args promoted from a positional value.
            if parents.is_empty() && !meta.args.is_empty() {
                parents = meta.args.clone();
            }
            if !parents.is_empty() {
                inherits.push((key.clone(), parents));
            }
        }
    }

    let map = unsafe { &mut *root_map };
    for (child_key, parents) in &inherits {
        // Merge parents left-to-right: first parent is base, each subsequent overrides
        let mut merged: HashMap<String, Value> = HashMap::new();
        for parent_name in parents {
            if let Some(Value::Object(p)) = map.get(parent_name) {
                for (k, v) in p {
                    merged.insert(k.clone(), v.clone());
                }
            }
        }
        // Child fields override all parents
        if let Some(Value::Object(c)) = map.get(child_key) {
            for (k, v) in c {
                merged.insert(k.clone(), v.clone());
            }
        }
        map.insert(child_key.clone(), Value::Object(merged));
    }
}

fn deep_get(root: &Value, path: &str) -> Option<Value> {
    // Try direct key
    if let Value::Object(map) = root {
        if let Some(val) = map.get(path) {
            return Some(val.clone());
        }
    }
    // Dot-path traversal
    let parts: Vec<&str> = path.split('.').collect();
    let mut current = root;
    for part in parts {
        match current {
            Value::Object(map) => match map.get(part) {
                Some(v) => current = v,
                None => return None,
            },
            _ => return None,
        }
    }
    Some(current.clone())
}

/// Resolve {placeholder} references in a template string.
/// Supports: {key}, {key.nested}, {key:alias}, {key:include}
fn resolve_interpolation(
    tpl: &str,
    root: &Value,
    local_map: &HashMap<String, Value>,
    includes: &HashMap<String, Value>,
) -> String {
    let bytes = tpl.as_bytes();
    let len = bytes.len();
    let mut result = String::with_capacity(len.min(MAX_ENGINE_SCRATCH_STRING));
    let mut i = 0;

    while i < len {
        if result.len() >= MAX_ENGINE_SCRATCH_STRING {
            break;
        }
        if bytes[i] == b'{' {
            if let Some(close) = tpl[i + 1..].find('}') {
                let inner = &tpl[i + 1..i + 1 + close];
                // Check for scope separator ':'
                if let Some(colon) = inner.find(':') {
                    let ref_name = &inner[..colon];
                    let scope = &inner[colon + 1..];
                    // Valid ref name?
                    if ref_name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '.') {
                        let resolved = if scope == "include" {
                            // {key:include} — first/only include
                            if includes.len() == 1 {
                                let first = includes.values().next().unwrap();
                                deep_get(first, ref_name)
                            } else {
                                None
                            }
                        } else {
                            // {key:alias} — look up by alias
                            includes.get(scope).and_then(|inc| deep_get(inc, ref_name))
                        };
                        if let Some(val) = resolved {
                            let s = value_to_string(&val);
                            let room = MAX_ENGINE_SCRATCH_STRING.saturating_sub(result.len());
                            if room > 0 {
                                let take = s.len().min(room);
                                let end = s.floor_char_boundary(take);
                                result.push_str(&s[..end]);
                            }
                        } else {
                            result.push('{');
                            let rem = MAX_ENGINE_SCRATCH_STRING.saturating_sub(result.len() + 1);
                            if rem > 0 {
                                let end = inner.floor_char_boundary(inner.len().min(rem));
                                result.push_str(&inner[..end]);
                            }
                            if result.len() < MAX_ENGINE_SCRATCH_STRING {
                                result.push('}');
                            }
                        }
                        i += 2 + close;
                        continue;
                    }
                } else {
                    // {key} — local
                    let ref_name = inner;
                    if ref_name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '.') {
                        let resolved = deep_get(root, ref_name).or_else(|| {
                            local_map.get(ref_name).cloned()
                        });
                        if let Some(val) = resolved {
                            let s = value_to_string(&val);
                            let room = MAX_ENGINE_SCRATCH_STRING.saturating_sub(result.len());
                            if room > 0 {
                                let take = s.len().min(room);
                                let end = s.floor_char_boundary(take);
                                result.push_str(&s[..end]);
                            }
                        } else {
                            result.push('{');
                            let rem = MAX_ENGINE_SCRATCH_STRING.saturating_sub(result.len() + 1);
                            if rem > 0 {
                                let end = ref_name.floor_char_boundary(ref_name.len().min(rem));
                                result.push_str(&ref_name[..end]);
                            }
                            if result.len() < MAX_ENGINE_SCRATCH_STRING {
                                result.push('}');
                            }
                        }
                        i += 2 + close;
                        continue;
                    }
                }
            }
        }
        if result.len() < MAX_ENGINE_SCRATCH_STRING {
            result.push(bytes[i] as char);
        }
        i += 1;
    }
    result
}

/// Load !include files into a map<alias, Value>.
fn load_includes(
    directives: &[IncludeDirective],
    options: &Options,
) -> HashMap<String, Value> {
    let mut map = HashMap::new();
    let base = options.base_path.as_deref().unwrap_or(".");
    let max_depth = options.max_include_depth.unwrap_or(DEFAULT_MAX_INCLUDE_DEPTH);
    if options._include_depth >= max_depth {
        return map;
    }
    for inc in directives {
        let full = match jail_path(base, &inc.path) {
            Ok(p) => p,
            Err(_) => continue,
        };
        if check_file_size(&full).is_err() {
            continue;
        }
        if let Ok(text) = std::fs::read_to_string(&full) {
            let mut included = parser::parse(&text);
            if included.mode == Mode::Active {
                let mut child_opts = options.clone();
                child_opts._include_depth += 1;
                if let Some(parent) = full.parent() {
                    child_opts.base_path = Some(parent.to_string_lossy().into_owned());
                }
                resolve(&mut included, &child_opts);
            }
            map.insert(inc.alias.clone(), included.root);
        }
    }
    map
}

/// Load !use packages into a map<alias, Value>.
///
/// Looks for `<packages_path>/@scope/name/src/main.synx` on disk.
/// Falls back to `./synx_packages/` when `options.packages_path` is unset.
fn load_packages(
    directives: &[UseDirective],
    options: &Options,
    #[cfg(feature = "wasm")] wasm_runtime: &mut crate::wasm::WasmMarkerRuntime,
) -> HashMap<String, Value> {
    let mut map = HashMap::new();
    let pkg_base = options
        .packages_path
        .as_deref()
        .unwrap_or("./synx_packages");
    let base = options.base_path.as_deref().unwrap_or(".");
    let pkg_root = std::path::Path::new(base).join(pkg_base);

    for ud in directives {
        // @scope/name  → package dir is <pkg_root>/@scope/name
        let pkg_dir = pkg_root.join(&ud.package);

        // Check if this is a WASM marker package (type markers in manifest)
        #[cfg(feature = "wasm")]
        {
            if is_marker_package(&pkg_dir) {
                // Load the .wasm file from the package
                let wasm_entry = read_manifest_wasm(&pkg_dir)
                    .unwrap_or_else(|| pkg_dir.join("src").join("main.wasm"));
                let caps = read_manifest_capabilities(&pkg_dir);
                if wasm_entry.is_file() {
                    if let Ok(wasm_bytes) = std::fs::read(&wasm_entry) {
                        match wasm_runtime.load_module(&wasm_bytes, caps) {
                            Ok(_markers) => {}
                            Err(e) => {
                                map.insert(
                                    ud.alias.clone(),
                                    Value::String(format!("WASM_ERR: {}", e)),
                                );
                            }
                        }
                    }
                }
                continue;
            }
        }

        // Read entry point from manifest (synx-pkg.synx), fall back to src/main.synx
        let entry = read_manifest_main(&pkg_dir)
            .unwrap_or_else(|| pkg_dir.join("src").join("main.synx"));

        if !entry.is_file() {
            continue;
        }
        if check_file_size(&entry).is_err() {
            continue;
        }
        if let Ok(text) = std::fs::read_to_string(&entry) {
            let mut parsed = parser::parse(&text);
            if parsed.mode == Mode::Active {
                let mut child_opts = options.clone();
                child_opts._include_depth += 1;
                child_opts.base_path = Some(
                    entry.parent().unwrap_or(&pkg_dir).to_string_lossy().into_owned()
                );
                resolve(&mut parsed, &child_opts);
            }
            map.insert(ud.alias.clone(), parsed.root);
        }
    }
    map
}

/// Read synx-pkg.synx manifest and extract the `main` entry point path.
/// Returns the absolute path to the entry file, or None if manifest is missing/invalid.
fn read_manifest_main(pkg_dir: &std::path::Path) -> Option<std::path::PathBuf> {
    let manifest = pkg_dir.join("synx-pkg.synx");
    let text = std::fs::read_to_string(&manifest).ok()?;
    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("main ") {
            let entry_path = rest.trim();
            if !entry_path.is_empty() {
                return Some(pkg_dir.join(entry_path));
            }
        }
        // Legacy field name support
        if let Some(rest) = trimmed.strip_prefix("entry ") {
            let entry_path = rest.trim();
            if !entry_path.is_empty() {
                return Some(pkg_dir.join(entry_path));
            }
        }
    }
    None
}

/// Check if a package is a WASM marker package.
/// Matches `type markers` in manifest, or `main` pointing to a `.wasm` file.
#[cfg(feature = "wasm")]
fn is_marker_package(pkg_dir: &std::path::Path) -> bool {
    let manifest = pkg_dir.join("synx-pkg.synx");
    if let Ok(text) = std::fs::read_to_string(&manifest) {
        for line in text.lines() {
            let trimmed = line.trim();
            if trimmed == "type markers" {
                return true;
            }
            if let Some(rest) = trimmed.strip_prefix("main ") {
                if rest.trim().ends_with(".wasm") {
                    return true;
                }
            }
        }
    }
    false
}

/// Read the WASM entry point from a marker package manifest.
#[cfg(feature = "wasm")]
fn read_manifest_wasm(pkg_dir: &std::path::Path) -> Option<std::path::PathBuf> {
    let manifest = pkg_dir.join("synx-pkg.synx");
    let text = std::fs::read_to_string(&manifest).ok()?;
    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("wasm ") {
            let wasm_path = rest.trim();
            if !wasm_path.is_empty() {
                return Some(pkg_dir.join(wasm_path));
            }
        }
        // Also check `main` if it points to a .wasm file
        if let Some(rest) = trimmed.strip_prefix("main ") {
            let path = rest.trim();
            if path.ends_with(".wasm") {
                return Some(pkg_dir.join(path));
            }
        }
    }
    None
}

/// Read capability permissions from a marker package manifest.
#[cfg(feature = "wasm")]
fn read_manifest_capabilities(pkg_dir: &std::path::Path) -> crate::wasm::WasmCapabilities {
    let manifest = pkg_dir.join("synx-pkg.synx");
    let text = match std::fs::read_to_string(&manifest) {
        Ok(t) => t,
        Err(_) => return crate::wasm::WasmCapabilities::default(),
    };
    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("permissions ") {
            return crate::wasm::WasmCapabilities::from_manifest_line(rest);
        }
    }
    crate::wasm::WasmCapabilities::default()
}

// ─── Type validation ──────────────────────────────────────

/// Build a global type registry from all metadata.
/// Maps field name → expected type (e.g., "hp" → "int").
fn build_type_registry(metadata: &HashMap<String, MetaMap>) -> HashMap<String, String> {
    let mut registry: HashMap<String, String> = HashMap::new();

    for meta_map in metadata.values() {
        for (key, meta) in meta_map {
            if let Some(ref type_hint) = meta.type_hint {
                // If type already registered, check for conflict
                if let Some(existing) = registry.get(key) {
                    if existing != type_hint {
                        // Type conflict: same field defined with different types
                        // For now, first definition wins; could also log error
                    }
                } else {
                    registry.insert(key.clone(), type_hint.clone());
                }
            }
        }
    }

    registry
}

/// Build a global constraint registry from all metadata.
/// Maps field name → merged constraints from [] declarations.
fn build_constraint_registry(metadata: &HashMap<String, MetaMap>) -> HashMap<String, Constraints> {
    let mut registry: HashMap<String, Constraints> = HashMap::new();

    for meta_map in metadata.values() {
        for (key, meta) in meta_map {
            if let Some(ref constraints) = meta.constraints {
                registry
                    .entry(key.clone())
                    .and_modify(|existing| merge_constraints(existing, constraints))
                    .or_insert_with(|| constraints.clone());
            }
        }
    }

    registry
}

/// Merge constraints when the same field is declared multiple times.
/// Strategy is intentionally strict to keep schemas consistent across templates.
fn merge_constraints(base: &mut Constraints, incoming: &Constraints) {
    if incoming.required {
        base.required = true;
    }
    if incoming.readonly {
        base.readonly = true;
    }

    // Stricter numeric bounds win.
    base.min = match (base.min, incoming.min) {
        (Some(a), Some(b)) => Some(a.max(b)),
        (None, Some(b)) => Some(b),
        (a, None) => a,
    };
    base.max = match (base.max, incoming.max) {
        (Some(a), Some(b)) => Some(a.min(b)),
        (None, Some(b)) => Some(b),
        (a, None) => a,
    };

    // Keep first non-empty type/pattern/enum declaration.
    if base.type_name.is_none() {
        base.type_name = incoming.type_name.clone();
    }
    if base.pattern.is_none() {
        base.pattern = incoming.pattern.clone();
    }
    if base.enum_values.is_none() {
        base.enum_values = incoming.enum_values.clone();
    }
}

/// Validate [] constraints recursively for all object fields that have
/// a registered constraint rule.
///
/// Skips values that already hold a CONSTRAINT_ERR string from the per-key
/// validation pass in `apply_markers` — otherwise we'd re-validate the error
/// message itself and report bogus numbers (the length of the error string)
/// instead of the original value.
fn validate_field_constraints(value: &mut Value, registry: &HashMap<String, Constraints>) {
    if let Value::Object(ref mut map) = value {
        let keys: Vec<String> = map.keys().cloned().collect();
        for key in &keys {
            if let Some(constraints) = registry.get(key) {
                let already_errored = matches!(
                    map.get(key),
                    Some(Value::String(s)) if s.starts_with("CONSTRAINT_ERR:")
                        || s.starts_with("TYPE_ERR:")
                );
                if !already_errored {
                    validate_constraints(map, key, constraints);
                }
            }

            if let Some(child) = map.get_mut(key) {
                match child {
                    Value::Object(_) => validate_field_constraints(child, registry),
                    Value::Array(arr) => {
                        for item in arr.iter_mut() {
                            if let Value::Object(_) = item {
                                validate_field_constraints(item, registry);
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }
}

/// Validate that all values in the tree match their registered types.
fn validate_field_types(value: &mut Value, registry: &HashMap<String, String>, path: &str) {
    match value {
        Value::Object(ref mut map) => {
            let keys: Vec<String> = map.keys().cloned().collect();
            for key in &keys {
                if let Some(expected_type) = registry.get(key) {
                    if let Some(val) = map.get(key) {
                        if !value_matches_type(val, expected_type) {
                            // Type mismatch: replace with error string
                            let current_type = value_type_name(val);
                            map.insert(key.clone(), Value::String(
                                format!("TYPE_ERR: '{}' expected {} but got {}", key, expected_type, current_type)
                            ));
                        }
                    }
                }
                
                // Recurse into nested objects and arrays
                if let Some(child) = map.get_mut(key) {
                    match child {
                        Value::Object(_) => {
                            let child_path = if path.is_empty() {
                                key.clone()
                            } else {
                                format!("{}.{}", path, key)
                            };
                            validate_field_types(child, registry, &child_path);
                        }
                        Value::Array(ref mut arr) => {
                            for item in arr.iter_mut() {
                                if let Value::Object(_) = item {
                                    validate_field_types(item, registry, path);
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
        _ => {}
    }
}

/// Check if a value matches an expected type.
fn value_matches_type(value: &Value, expected_type: &str) -> bool {
    match expected_type {
        "int" => matches!(value, Value::Int(_)),
        "float" => matches!(value, Value::Float(_) | Value::Int(_)),
        "bool" => matches!(value, Value::Bool(_)),
        "string" => matches!(value, Value::String(_) | Value::Secret(_)),
        "array" => matches!(value, Value::Array(_)),
        "object" => matches!(value, Value::Object(_)),
        _ => true, // Unknown types are accepted
    }
}

/// Get the human-readable name of a value's type.
fn value_type_name(value: &Value) -> String {
    match value {
        Value::Int(_) => "int".to_string(),
        Value::Float(_) => "float".to_string(),
        Value::Bool(_) => "bool".to_string(),
        Value::String(_) => "string".to_string(),
        Value::Secret(_) => "secret".to_string(),
        Value::Array(_) => "array".to_string(),
        Value::Object(_) => "object".to_string(),
        Value::Null => "null".to_string(),
    }
}

// ─── CLDR plural rules ───────────────────────────────────

/// Return the CLDR plural category for a given language and integer count.
/// Categories: "zero", "one", "two", "few", "many", "other".
fn plural_category(lang: &str, n: i64) -> &'static str {
    let abs_n = n.unsigned_abs();
    let n10 = abs_n % 10;
    let n100 = abs_n % 100;

    match lang {
        // East Slavic: Russian, Ukrainian, Belarusian
        "ru" | "uk" | "be" => {
            if n10 == 1 && n100 != 11 {
                "one"
            } else if (2..=4).contains(&n10) && !(12..=14).contains(&n100) {
                "few"
            } else {
                "many"
            }
        }
        // West/South Slavic: Polish
        "pl" => {
            if n10 == 1 && n100 != 11 {
                "one"
            } else if (2..=4).contains(&n10) && !(12..=14).contains(&n100) {
                "few"
            } else {
                "many"
            }
        }
        // Czech, Slovak
        "cs" | "sk" => {
            if abs_n == 1 { "one" }
            else if (2..=4).contains(&abs_n) { "few" }
            else { "other" }
        }
        // Arabic
        "ar" => {
            if abs_n == 0 { "zero" }
            else if abs_n == 1 { "one" }
            else if abs_n == 2 { "two" }
            else if (3..=10).contains(&n100) { "few" }
            else if (11..=99).contains(&n100) { "many" }
            else { "other" }
        }
        // French, Portuguese (Brazilian) — 0 and 1 are "one"
        "fr" | "pt" => {
            if abs_n <= 1 { "one" } else { "other" }
        }
        // Japanese, Chinese, Korean, Vietnamese, Thai — no plural forms
        "ja" | "zh" | "ko" | "vi" | "th" => "other",
        // English, German, Spanish, Italian, Dutch, Swedish, Norwegian, Danish, etc.
        _ => {
            if abs_n == 1 { "one" } else { "other" }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{parse, Options, Value};
    use super::{resolve, read_manifest_main};

    #[test]
    fn test_ref_simple() {
        let mut r = parse("!active\nbase_rate 50\nquick_rate:ref base_rate");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["quick_rate"], Value::Int(50));
    }

    #[test]
    fn test_ref_calc_shorthand() {
        let mut r = parse("!active\nbase_rate 50\ndouble_rate:ref:calc:*2 base_rate");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["double_rate"], Value::Int(100));
    }

    #[test]
    fn test_inherit() {
        let mut r = parse("!active\n_base\n  weight 10\n  stackable true\nsteel:inherit:_base\n  weight 25\n  material metal");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert!(!map.contains_key("_base"));
        let steel = map["steel"].as_object().unwrap();
        assert_eq!(steel["weight"], Value::Int(25));
        assert_eq!(steel["stackable"], Value::Bool(true));
        assert_eq!(steel["material"], Value::String("metal".into()));
    }

    #[test]
    fn test_i18n_select_lang() {
        let mut r = parse("!active\ntitle:i18n\n  en Hello\n  ru Привет\n  de Hallo");
        let opts = Options { lang: Some("ru".into()), ..Default::default() };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert_eq!(map["title"], Value::String("Привет".into()));
    }

    #[test]
    fn test_i18n_fallback_en() {
        let mut r = parse("!active\ntitle:i18n\n  en Hello\n  ru Привет");
        let opts = Options { lang: Some("fr".into()), ..Default::default() };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert_eq!(map["title"], Value::String("Hello".into()));
    }

    #[test]
    fn test_auto_interpolation_simple() {
        let mut r = parse("!active\nname Wario\ngreeting Hello, {name}!");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["greeting"], Value::String("Hello, Wario!".into()));
    }

    #[test]
    fn test_auto_interpolation_nested() {
        let mut r = parse("!active\nserver\n  host localhost\n  port 8080\nurl http://{server.host}:{server.port}/api");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["url"], Value::String("http://localhost:8080/api".into()));
    }

    #[test]
    fn test_template_legacy_still_works() {
        let mut r = parse("!active\nname Wario\ngreeting:template Hello, {name}!");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["greeting"], Value::String("Hello, Wario!".into()));
    }

    #[test]
    fn test_type_validation() {
        // Test that type validation works: hp(int) defined in _base_unit,
        // then used in other places with correct type
        let mut r = parse(
            "!active\n\
            _base_unit\n  \
              hp(int) 100\n  \
              speed(float) 1.5\n\
            infantry:inherit:_base_unit\n  \
              name Infantry\n  \
              hp 80"
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        
        // _base_unit should be removed (private)
        assert!(!map.contains_key("_base_unit"));
        
        // infantry should exist with correct types
        let infantry = map["infantry"].as_object().unwrap();
        assert_eq!(infantry["hp"], Value::Int(80));  // Correct: int
        assert_eq!(infantry["speed"], Value::Float(1.5));  // Correct: float
    }

    #[test]
    fn test_type_validation_error() {
        // Test that type mismatch is detected and replaced with error
        let mut r = parse(
            "!active\n\
            _base_unit\n  \
              hp(int) 100\n\
            infantry:inherit:_base_unit\n  \
              hp hello"  // Type mismatch: string instead of int
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        
        let infantry = map["infantry"].as_object().unwrap();
        // Should be replaced with error message
        if let Value::String(s) = &infantry["hp"] {
            assert!(s.contains("TYPE_ERR"));
        } else {
            panic!("Expected error string for type mismatch");
        }
    }

    #[test]
    fn test_constraint_validation_inherited_range() {
        let mut r = parse(
            "!active\n\
            _base_unit\n  \
              hp[min:1, max:50000] 1000\n\
            infantry:inherit:_base_unit\n  \
              hp 60000"
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        let infantry = map["infantry"].as_object().unwrap();

        if let Value::String(s) = &infantry["hp"] {
            assert!(s.contains("CONSTRAINT_ERR"));
            assert!(s.contains("exceeds max"));
        } else {
            panic!("Expected constraint error string");
        }
    }

    #[test]
    fn test_constraint_validation_required() {
        let mut r = parse(
            "!active\n\
            _base_unit\n  \
                            description[type:string, required] hello\n\
            scout:inherit:_base_unit\n  \
                            description null"
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        let scout = map["scout"].as_object().unwrap();

        if let Value::String(s) = &scout["description"] {
            assert!(s.contains("CONSTRAINT_ERR"));
            assert!(s.contains("required"));
        } else {
            panic!("Expected required-constraint error string");
        }
    }

    #[test]
    fn test_multi_parent_inherit() {
        let mut r = parse(
            "!active\n\
            _movable\n  \
              speed 10\n  \
              can_move true\n\
            _damageable\n  \
              hp 100\n  \
              armor 5\n\
            tank:inherit:_movable:_damageable\n  \
              name Tank\n  \
              armor 20"
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();

        assert!(!map.contains_key("_movable"));
        assert!(!map.contains_key("_damageable"));

        let tank = map["tank"].as_object().unwrap();
        assert_eq!(tank["speed"], Value::Int(10));        // from _movable
        assert_eq!(tank["can_move"], Value::Bool(true));   // from _movable
        assert_eq!(tank["hp"], Value::Int(100));           // from _damageable
        assert_eq!(tank["armor"], Value::Int(20));         // child overrides _damageable's 5
        assert_eq!(tank["name"], Value::String("Tank".into()));
    }

    #[test]
    fn test_calc_dot_path() {
        let mut r = parse(
            "!active\n\
            stats\n  \
              base_hp 100\n  \
              multiplier 3\n\
            total_hp:calc stats.base_hp * stats.multiplier"
        );
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["total_hp"], Value::Int(300));
    }

    #[test]
    fn test_i18n_plural_en() {
        let mut r = parse(
            "!active\n\
            count 5\n\
            items:i18n:count\n  \
              en\n    \
                one item\n    \
                other items"
        );
        let opts = Options { lang: Some("en".into()), ..Default::default() };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert_eq!(map["items"], Value::String("items".into()));
    }

    #[test]
    fn test_i18n_plural_en_one() {
        let mut r = parse(
            "!active\n\
            count 1\n\
            items:i18n:count\n  \
              en\n    \
                one {count} item\n    \
                other {count} items"
        );
        let opts = Options { lang: Some("en".into()), ..Default::default() };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert_eq!(map["items"], Value::String("1 item".into()));
    }

    #[test]
    fn test_i18n_plural_ru() {
        let mut r = parse(
            "!active\n\
            count 3\n\
            items:i18n:count\n  \
              ru\n    \
                one предмет\n    \
                few предмета\n    \
                many предметов\n    \
                other предметов"
        );
        let opts = Options { lang: Some("ru".into()), ..Default::default() };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert_eq!(map["items"], Value::String("предмета".into()));
    }

    #[test]
    fn test_quoted_null_preserved() {
        let r = parse("status \"null\"\nenabled \"true\"\ncount \"42\"");
        let map = r.root.as_object().unwrap();
        assert_eq!(map["status"], Value::String("null".into()));
        assert_eq!(map["enabled"], Value::String("true".into()));
        assert_eq!(map["count"], Value::String("42".into()));
    }

    #[test]
    fn test_unquoted_null_is_null() {
        let r = parse("status null\nenabled true\ncount 42");
        let map = r.root.as_object().unwrap();
        assert_eq!(map["status"], Value::Null);
        assert_eq!(map["enabled"], Value::Bool(true));
        assert_eq!(map["count"], Value::Int(42));
    }

    #[test]
    fn test_spam_rate_limit_exceeded() {
        super::clear_spam_buckets();

        let mut r1 = parse("!active\nsecret_token abc\naccess:spam:1:5 secret_token");
        resolve(&mut r1, &Options::default());
        let map1 = r1.root.as_object().unwrap();
        assert_eq!(map1["access"], Value::String("abc".into()));

        let mut r2 = parse("!active\nsecret_token abc\naccess:spam:1:5 secret_token");
        resolve(&mut r2, &Options::default());
        let map2 = r2.root.as_object().unwrap();
        match &map2["access"] {
            Value::String(s) => assert!(s.starts_with("SPAM_ERR:")),
            _ => panic!("Expected SPAM_ERR string"),
        }
    }

    #[test]
    fn test_spam_default_window_sec_is_one() {
        super::clear_spam_buckets();

        let mut r = parse("!active\na 1\nx:spam:2 a");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["x"], Value::Int(1));
    }

    #[test]
    fn test_deep_nesting_does_not_overflow() {
        // Deep indentation chain: parser caps nesting (see `MAX_PARSE_NESTING_DEPTH` in parser);
        // this test only checks resolve + navigation do not panic and yield a bounded tree.
        let mut synx = String::from("!active\n");
        let mut indent = String::new();
        for i in 0..200 {
            synx.push_str(&format!("{}level_{}\n", indent, i));
            indent.push_str("  ");
        }
        synx.push_str(&format!("{}value deep\n", indent));

        let mut result = parse(&synx);
        resolve(&mut result, &Default::default());
        assert!(matches!(result.root, Value::Object(_)));

        let mut cur = &result.root;
        let mut depth = 0usize;
        loop {
            let Value::Object(map) = cur else { break };
            let key = format!("level_{}", depth);
            match map.get(&key) {
                Some(next) => {
                    cur = next;
                    depth += 1;
                }
                None => break,
            }
        }
        assert!(
            depth >= 100,
            "expected at least 100 chained levels from parse, got {}",
            depth
        );
        assert!(
            depth <= 130,
            "parser nesting cap should keep chain shallow, got {}",
            depth
        );
    }

    #[test]
    fn test_circular_alias_returns_error() {
        let mut r = parse("!active\na:alias b\nb:alias a");
        resolve(&mut r, &Default::default());
        let root = r.root.as_object().unwrap();
        let a_val = root.get("a").unwrap();
        let b_val = root.get("b").unwrap();
        assert!(
            matches!(a_val, Value::String(s) if s.starts_with("ALIAS_ERR:")),
            "expected ALIAS_ERR for 'a', got: {:?}", a_val
        );
        assert!(
            matches!(b_val, Value::String(s) if s.starts_with("ALIAS_ERR:")),
            "expected ALIAS_ERR for 'b', got: {:?}", b_val
        );
    }

    #[test]
    fn test_self_alias_returns_error() {
        let mut r = parse("!active\na:alias a");
        resolve(&mut r, &Default::default());
        let root = r.root.as_object().unwrap();
        let a_val = root.get("a").unwrap();
        assert!(
            matches!(a_val, Value::String(s) if s.starts_with("ALIAS_ERR:")),
            "expected ALIAS_ERR for self-alias, got: {:?}", a_val
        );
    }

    #[test]
    fn test_valid_alias_still_works() {
        let mut r = parse("!active\nbase 42\ncopy:alias base");
        resolve(&mut r, &Default::default());
        let root = r.root.as_object().unwrap();
        assert_eq!(root.get("copy"), Some(&Value::Int(42)));
    }

    #[test]
    fn test_alias_to_string_valued_key_no_false_positive() {
        // 'a' holds a literal string "b". 'b' aliases 'a'.
        // b should resolve to "b" — NOT trigger ALIAS_ERR.
        let mut r = parse("!active\na b\nb:alias a");
        resolve(&mut r, &Default::default());
        let root = r.root.as_object().unwrap();
        assert_eq!(
            root.get("b"),
            Some(&Value::String("b".to_string())),
            "alias to a string-valued key should not produce ALIAS_ERR"
        );
    }

    #[test]
    fn test_prompt_marker() {
        let mut r = parse("!active\nmemory:prompt:Core\n  identity ASAI\n  creator APERTURESyndicate");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        if let Value::String(s) = &map["memory"] {
            assert!(s.starts_with("Core (SYNX):"));
            assert!(s.contains("```synx"));
            assert!(s.contains("identity ASAI"));
        } else {
            panic!("Expected :prompt to produce a string");
        }
    }

    #[test]
    fn test_vision_marker_passthrough() {
        let mut r = parse("!active\nimage:vision Generate a sunset");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["image"], Value::String("Generate a sunset".into()));
    }

    #[test]
    fn test_audio_marker_passthrough() {
        let mut r = parse("!active\nnarration:audio Read this summary aloud");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        assert_eq!(map["narration"], Value::String("Read this summary aloud".into()));
    }

    #[test]
    fn test_use_directive_loads_package() {
        // Set up a temp package for `!use`
        let tmp = std::env::temp_dir().join("synx-use-test-load");
        let _ = std::fs::remove_dir_all(&tmp);
        let pkg_dir = tmp.join("synx_packages/@test/config");
        std::fs::create_dir_all(pkg_dir.join("src")).unwrap();
        std::fs::write(pkg_dir.join("synx-pkg.synx"), "name @test/config\nversion 1.0.0\nmain src/main.synx\n").unwrap();
        std::fs::write(pkg_dir.join("src/main.synx"), "identity APERTURESyndicate\ndefault_port 8080\n").unwrap();

        let mut r = parse("!active\n!use @test/config\napp MyApp");
        let opts = Options {
            base_path: Some(tmp.to_string_lossy().into_owned()),
            ..Default::default()
        };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert!(map.contains_key("config"), "package not loaded");
        let pkg = map["config"].as_object().unwrap();
        assert_eq!(pkg["identity"], Value::String("APERTURESyndicate".into()));
        assert_eq!(pkg["default_port"], Value::Int(8080));
        assert_eq!(map["app"], Value::String("MyApp".into()));
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_use_directive_with_alias() {
        let tmp = std::env::temp_dir().join("synx-use-test-alias");
        let _ = std::fs::remove_dir_all(&tmp);
        let pkg_dir = tmp.join("synx_packages/@test/config");
        std::fs::create_dir_all(pkg_dir.join("src")).unwrap();
        std::fs::write(pkg_dir.join("synx-pkg.synx"), "name @test/config\nversion 1.0.0\nmain src/main.synx\n").unwrap();
        std::fs::write(pkg_dir.join("src/main.synx"), "identity APERTURESyndicate\ndefault_port 8080\n").unwrap();

        let mut r = parse("!active\n!use @test/config as defaults\napp MyApp");
        let opts = Options {
            base_path: Some(tmp.to_string_lossy().into_owned()),
            ..Default::default()
        };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert!(map.contains_key("defaults"), "aliased package not loaded");
        assert!(!map.contains_key("config"), "should use alias, not auto name");
        let pkg = map["defaults"].as_object().unwrap();
        assert_eq!(pkg["identity"], Value::String("APERTURESyndicate".into()));
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_use_directive_missing_package_ignored() {
        let mut r = parse("!active\n!use @nonexistent/pkg\napp MyApp");
        resolve(&mut r, &Options::default());
        let map = r.root.as_object().unwrap();
        // Missing package should be silently skipped
        assert!(!map.contains_key("pkg"));
        assert_eq!(map["app"], Value::String("MyApp".into()));
    }

    #[test]
    fn test_use_reads_manifest_main_field() {
        let tmp = std::env::temp_dir().join("synx-use-test-main");
        let _ = std::fs::remove_dir_all(&tmp);
        let pkg_dir = tmp.join("synx_packages/@test/myapp");
        std::fs::create_dir_all(pkg_dir.join("src")).unwrap();
        std::fs::write(pkg_dir.join("synx-pkg.synx"), "name @test/myapp\nversion 1.0.0\nmain src/main.synx\n").unwrap();
        std::fs::write(pkg_dir.join("src/main.synx"), "app_name MyApp\nversion 2.0.0\n").unwrap();

        let mut r = parse("!active\n!use @test/myapp as myapp\napp Test");
        let opts = Options {
            base_path: Some(tmp.to_string_lossy().into_owned()),
            ..Default::default()
        };
        resolve(&mut r, &opts);
        let map = r.root.as_object().unwrap();
        assert!(map.contains_key("myapp"), "package not loaded via manifest");
        let pkg = map["myapp"].as_object().unwrap();
        assert_eq!(pkg["app_name"], Value::String("MyApp".into()));
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_read_manifest_main_function() {
        let tmp = std::env::temp_dir().join("synx-manifest-main-test");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(tmp.join("src")).unwrap();
        std::fs::write(tmp.join("synx-pkg.synx"), "name @test/pkg\nversion 1.0.0\nmain src/main.synx\n").unwrap();
        std::fs::write(tmp.join("src/main.synx"), "key value\n").unwrap();

        let result = read_manifest_main(&tmp);
        assert!(result.is_some(), "should read main from synx-pkg.synx");
        let path = result.unwrap();
        assert!(path.ends_with("src/main.synx") || path.ends_with("src\\main.synx"));
        let _ = std::fs::remove_dir_all(&tmp);
    }
}