areev-cal 1.5.1

CAL (Context Assembly Language) lexer, parser, and executor for Areev.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
//! CAL Phase 4 — Template engine for formatting grain result sets.
//!
//! Implements a Mustache-subset template engine with:
//! - Single-pass O(n) character scanner parser
//! - Closed variable set with parse-time validation
//! - 10 built-in filters (no external crate dependencies)
//! - Template registry with 6 built-in templates
//! - Per-grain-type rendering blocks
//! - Progressive disclosure tiers (Summary, Headlines, Full)
//! - 1-level template inheritance
//!
//! # Security invariants
//!
//! - **F1**: Render output capped at `MAX_RENDER_OUTPUT_SIZE` (1MB), checked per-grain.
//! - **F2**: `truncate` filter argument validated at parse time to [1, 100_000].
//! - **F3**: `format_epoch` rejects negative epochs (returns `"<invalid date>"`).
//! - **F4**: `date` filter format string capped at 256 chars (parse time + runtime).
//! - **F5**: Runtime `ALLOWED_FIELDS` defense-in-depth on variable resolution.
//! - **F6**: Template names validated with `is_valid_template_name()`.
//! - **F7**: `select_tier` clamps `grain_count` to `u32::MAX` before cast.

use std::collections::HashMap;

use super::errors::{CalError, CalResult, Span};
use super::executor::CalGrainResult;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Maximum template source size in bytes (64KB).
/// OMS CAL §10.8 — max template body size.
pub const MAX_TEMPLATE_SIZE: usize = 4_096;

/// OMS CAL §10.8 — max conditional nesting depth.
pub const MAX_TEMPLATE_NESTING: usize = 5;

/// OMS CAL §10.8 — max `{{#each}}` iterations.
pub const MAX_EACH_ITERATIONS: usize = 200;

/// OMS CAL §10.8 — max custom templates per namespace (built-ins excluded).
pub const MAX_TEMPLATES: usize = 50;

/// Maximum rendered output size in bytes (1MB).
pub const MAX_RENDER_OUTPUT_SIZE: usize = 1_048_576;

/// `FORMAT` arm keywords a built-in template must never be named after —
/// `FORMAT <name>` would hit the arm while `FORMAT TEMPLATE <name>` hit the
/// template, two different outputs under one name. (The §10.1 presets
/// `structured`/`readable`/`compact` are parser aliases, not arm keywords,
/// and their dual role is spec'd.)
const FORMAT_ARM_NAMES: &[&str] = &[
    "json", "yaml", "text", "sml", "toon", "markdown", "csv", "table", "triples",
];

/// Closed set of allowed grain field names for runtime resolution.
/// Defense-in-depth (F5): even if `grain.fields` contains extra keys,
/// only these are accessible through templates.
const ALLOWED_FIELDS: &[&str] = &[
    // Common fields
    "subject",
    "relation",
    "object",
    "namespace",
    "user_id",
    "confidence",
    "importance",
    "created_at",
    "tags",
    "session_id",
    "content",
    "summary",
    "source_hash",
    "source_hashes",
    // Event
    "actor",
    "action",
    "participants",
    // State
    "entity",
    "state_value",
    "validity",
    // Workflow
    "name",
    "nodes",
    "edges",
    "bindings",
    "retries",
    "trigger",
    "node_count",
    "edge_count",
    "status",
    // Tool
    "tool_name",
    "input",
    "is_error",
    "duration_ms",
    "output_schema",
    // Phase 1 (2026-04-19) — Tool definition fields promoted from
    // extra_fields. Available to templates so the Phase 2 renderer can
    // emit tool catalogs in markdown / hermes / openai-tools / etc.
    "kind",
    "tool_description",
    "input_schema",
    "executor_uri",
    "runtime",
    "runtime_limits",
    "locked_params",
    "examples",
    "annotations",
    "spec_hash",
    "tool_call_id",
    "call_batch_id",
    // Observation
    "observer",
    "observed",
    "sensory_data",
    "sensor",
    "value",
    "unit",
    // Goal
    "title",
    "description",
    "goal_state",
    "assigned_to",
    "priority",
    "parent_hash",
    // Reasoning
    "premises",
    "conclusion",
    // Consensus
    "agreement_level",
    "agreement",
    // Consent
    "consenter",
    "scope",
    "granted",
    "expires_at",
    "grantee_did",
    "subject_did",
    "is_withdrawal",
    "basis",
];

/// CAL §10.5 grain-level variables, without the `grain.` prefix. These are
/// the spec's own names — the ones that are computed (content projection,
/// humanized relation, relative time, disclosure sections) rather than read
/// straight off the grain.
const SPEC_GRAIN_VARIABLES: &[&str] = &[
    "content",
    "type",
    "subject",
    "relation",
    "humanized_relation",
    "object",
    "confidence",
    "importance",
    "tags",
    "created_at",
    "relative_time",
    "score",
    "hash",
    "is_full",
    "is_summary",
];

/// CAL §10.5 assembly-level variables (the `assembly.` namespace).
const SPEC_ASSEMBLY_VARIABLES: &[&str] = &["name", "intent", "source_count", "grain_count"];

/// CAL §10.5 budget variables (the `budget.` namespace).
const SPEC_BUDGET_VARIABLES: &[&str] =
    &["total", "used", "remaining", "unit", "utilization"];

/// CAL §10.5 source-level variables (the `source.` namespace).
const SPEC_SOURCE_VARIABLES: &[&str] = &[
    "label",
    "index",
    "priority",
    "grain_count",
    "tokens_used",
    "truncated",
];

/// The full closed set of valid template variable names (fields + metadata + direct).
const ALL_VALID_VARIABLES: &[&str] = &[
    // Direct CalGrainResult fields
    "hash",
    "grain_type",
    "score",
    // Render context metadata
    "_index",
    "_count",
    "_first",
    "_last",
    "_now",
    // Common grain fields
    "subject",
    "relation",
    "object",
    "namespace",
    "user_id",
    "confidence",
    "importance",
    "created_at",
    "tags",
    "session_id",
    "content",
    "summary",
    "source_hash",
    "source_hashes",
    // Event
    "actor",
    "action",
    "participants",
    // State
    "entity",
    "state_value",
    "validity",
    // Workflow
    "name",
    "nodes",
    "edges",
    "bindings",
    "retries",
    "trigger",
    "node_count",
    "edge_count",
    "status",
    // Tool
    "tool_name",
    "input",
    "is_error",
    "duration_ms",
    "output_schema",
    // Phase 1 (2026-04-19) — Tool definition fields promoted from
    // extra_fields. Available to templates so the Phase 2 renderer can
    // emit tool catalogs in markdown / hermes / openai-tools / etc.
    "kind",
    "tool_description",
    "input_schema",
    "executor_uri",
    "runtime",
    "runtime_limits",
    "locked_params",
    "examples",
    "annotations",
    "spec_hash",
    "tool_call_id",
    "call_batch_id",
    // Observation
    "observer",
    "observed",
    "sensory_data",
    "sensor",
    "value",
    "unit",
    // Goal
    "title",
    "description",
    "goal_state",
    "assigned_to",
    "priority",
    "parent_hash",
    // Reasoning
    "premises",
    "conclusion",
    // Consensus
    "agreement_level",
    "agreement",
    // Consent
    "consenter",
    "scope",
    "granted",
    "expires_at",
    "grantee_did",
    "subject_did",
    "is_withdrawal",
    "basis",
];

/// Known filter names.
const KNOWN_FILTERS: &[&str] = &[
    "truncate",
    "date",
    "relative",
    "humanize",
    "percent",
    "uppercase",
    "lowercase",
    "json",
    "default",
    "join",
];

// ---------------------------------------------------------------------------
// Template AST types
// ---------------------------------------------------------------------------

/// A single node in a parsed template tree.
#[derive(Debug, Clone, PartialEq)]
pub enum TemplateNode {
    /// Literal text -- emitted verbatim.
    Text(String),

    /// `{{variable}}` or `{{variable | filter1 | filter2}}` -- resolved
    /// against the current grain's fields + render context metadata.
    Variable {
        /// Field path (e.g. "subject", "confidence", "hash").
        name: String,
        /// Filter pipeline applied left-to-right after resolution.
        filters: Vec<Filter>,
    },

    /// `{{#each grains}}...{{/each}}` -- iterates over the grain result set.
    Each {
        /// Must be the literal string "grains".
        collection: String,
        body: Vec<TemplateNode>,
    },

    /// `{{#if field}}...{{/if}}` or `{{#if field}}...{{else}}...{{/if}}`
    Condition {
        /// Field name to test for truthiness.
        field: String,
        /// Nodes to render when the field is truthy.
        if_body: Vec<TemplateNode>,
        /// Nodes to render when the field is falsy (optional).
        else_body: Vec<TemplateNode>,
    },

    /// `{{^field}}...{{/field}}` -- inverted section (renders when field
    /// is falsy or absent).
    InvertedSection {
        field: String,
        body: Vec<TemplateNode>,
    },

    /// `{{#grain_type "fact"}}...{{/grain_type}}` -- renders body only
    /// when the current grain's type matches.
    GrainTypeBlock {
        /// The grain type name (singular, lowercase).
        grain_type: String,
        body: Vec<TemplateNode>,
    },

    /// `{{! comment text }}` -- stripped during rendering, preserved in AST.
    Comment(String),
}

/// A single filter in a variable's pipeline.
#[derive(Debug, Clone, PartialEq)]
pub struct Filter {
    /// Filter name (e.g. "truncate", "date", "relative").
    pub name: String,
    /// Optional argument (e.g. "80" for truncate, "%Y-%m-%d" for date).
    pub arg: Option<String>,
}

// ---------------------------------------------------------------------------
// Template struct
// ---------------------------------------------------------------------------

/// The six sections of a template body (OMS CAL §10.6).
///
/// A sectioned template inverts who drives iteration. In the whole-result
/// form the author writes `{{#each grains}}` and owns the loop; here the
/// engine owns it, which is what lets it choose a *different* body per grain
/// — `ELEMENT` at full disclosure, `ELEMENT_SUMMARY` when the budget is
/// tight — and interleave `SOURCE_BREAK` between ASSEMBLE sources.
///
/// `None` means the section was not defined and is inherited from the parent
/// (§10.7), which is why these are `Option` rather than empty node lists: an
/// explicitly empty `SOURCE_BREAK { }` must override a parent's, not fall
/// back to it.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct TemplateSections {
    /// Emitted once, before any element.
    pub header: Option<Vec<TemplateNode>>,
    /// Emitted per grain at full disclosure.
    pub element: Option<Vec<TemplateNode>>,
    /// Emitted per grain when the render is squeezed below full disclosure.
    pub element_summary: Option<Vec<TemplateNode>>,
    /// Emitted per grain the budget dropped, so a template can account for
    /// what an `ASSEMBLE` left out rather than presenting a trimmed set as if
    /// it were everything. Fed by `SourceMeta::omitted` on the multi-source
    /// path and by the split-off tail on the single-source one; a bare
    /// `RECALL` has no budget, so nothing is omitted and the section is inert.
    pub element_omit: Option<Vec<TemplateNode>>,
    /// Emitted between sources — not before the first, not after the last.
    pub source_break: Option<Vec<TemplateNode>>,
    /// Emitted once, after every element.
    pub footer: Option<Vec<TemplateNode>>,
}

impl TemplateSections {
    /// True when no section is defined at all.
    pub fn is_empty(&self) -> bool {
        self.header.is_none()
            && self.element.is_none()
            && self.element_summary.is_none()
            && self.element_omit.is_none()
            && self.source_break.is_none()
            && self.footer.is_none()
    }

    /// Fill each undefined section from `parent` (§10.7, depth 1).
    fn inherit_from(&mut self, parent: &TemplateSections) {
        fn fill(child: &mut Option<Vec<TemplateNode>>, parent: &Option<Vec<TemplateNode>>) {
            if child.is_none() {
                child.clone_from(parent);
            }
        }
        fill(&mut self.header, &parent.header);
        fill(&mut self.element, &parent.element);
        fill(&mut self.element_summary, &parent.element_summary);
        fill(&mut self.element_omit, &parent.element_omit);
        fill(&mut self.source_break, &parent.source_break);
        fill(&mut self.footer, &parent.footer);
    }
}

/// One source of grains in a sectioned render.
///
/// A bare `RECALL` has a single unlabelled source; an `ASSEMBLE` has one per
/// `FROM` clause, in priority order.
#[derive(Debug, Clone, Copy)]
pub struct RenderSource<'a> {
    /// The `FROM` label, or `""` for a single-source render.
    pub label: &'a str,
    /// The grains belonging to this source.
    pub grains: &'a [CalGrainResult],
    /// Grains the budget forced out of this source, rendered through
    /// `ELEMENT_OMIT` so a template can account for what was dropped.
    pub omitted: &'a [CalGrainResult],
    /// 1-based `PRIORITY` rank; 0 when the statement set no priority.
    pub priority: usize,
    /// Tokens this source consumed.
    pub tokens_used: usize,
    /// Whether the budget cut grains from this source.
    pub truncated: bool,
}

impl<'a> RenderSource<'a> {
    /// A single unlabelled source — the shape a bare `RECALL` renders with.
    pub fn single(grains: &'a [CalGrainResult]) -> Self {
        Self {
            label: "",
            grains,
            omitted: &[],
            priority: 0,
            tokens_used: 0,
            truncated: false,
        }
    }
}

/// Assembly-level render context — CAL §10.5 `assembly.*` and `budget.*`.
///
/// Only an `ASSEMBLE` has these; a bare `RECALL` renders with `None`, and the
/// variables resolve to empty rather than erroring, per §10.8's rule that an
/// undefined variable renders as the empty string.
#[derive(Debug, Clone, Default)]
pub struct AssemblyContext {
    /// Context name (`ASSEMBLE <name>`).
    pub name: String,
    /// `FOR "..."` text.
    pub intent: String,
    /// Number of sources.
    pub source_count: usize,
    /// Total grains included after budget trimming.
    pub grain_count: usize,
    /// Budget granted.
    pub budget_total: u64,
    /// Budget consumed.
    pub budget_used: u64,
    /// `"tokens"` or `"grains"`.
    pub budget_unit: String,
}

impl AssemblyContext {
    fn budget_remaining(&self) -> u64 {
        self.budget_total.saturating_sub(self.budget_used)
    }

    fn budget_utilization(&self) -> f64 {
        if self.budget_total == 0 {
            0.0
        } else {
            self.budget_used as f64 / self.budget_total as f64
        }
    }
}

/// Everything a FORMAT render needs about the statement that produced the
/// grains.
///
/// A bare `RECALL` renders with the default (no assembly, one implicit
/// source); an `ASSEMBLE` fills both, which is what makes `SOURCE_BREAK`,
/// `ELEMENT_OMIT` and the `assembly.`/`source.`/`budget.` namespaces
/// meaningful.
#[derive(Debug, Default, Clone, Copy)]
pub struct RenderPlan<'a> {
    pub assembly: Option<&'a AssemblyContext>,
    pub sources: Option<&'a [RenderSource<'a>]>,
}

/// What a render knows beyond the grain in hand.
///
/// Kept separate from [`RenderContext`] because it is meaningful only inside
/// a sectioned render: `source.*` changes as the engine walks sources, which
/// a whole-result template has no way to observe.
#[derive(Debug, Clone, Copy, Default)]
pub struct RenderEnv<'a> {
    /// Assembly and budget context, when rendering an `ASSEMBLE`.
    pub assembly: Option<&'a AssemblyContext>,
    /// The source being rendered, with its 0-based index.
    pub source: Option<(&'a RenderSource<'a>, usize)>,
}

/// A parsed, validated template ready for rendering.
#[derive(Debug, Clone, PartialEq)]
pub struct Template {
    /// Original source text (preserved for `get_template` responses).
    source: String,
    /// Parsed AST nodes — the whole-result form. Empty when sectioned.
    nodes: Vec<TemplateNode>,
    /// Parsed sections — the §10.6 form. `None` when whole-result.
    sections: Option<Box<TemplateSections>>,
}

impl Template {
    /// Return the original template source.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Return the parsed node tree (for introspection/debugging).
    ///
    /// Empty for a sectioned template — use [`Template::sections`].
    pub fn nodes(&self) -> &[TemplateNode] {
        &self.nodes
    }

    /// Return the parsed sections, or `None` for a whole-result template.
    pub fn sections(&self) -> Option<&TemplateSections> {
        self.sections.as_deref()
    }

    /// True when this template uses the §10.6 sectioned form.
    pub fn is_sectioned(&self) -> bool {
        self.sections.is_some()
    }

    /// Build a sectioned template from already-parsed sections.
    pub fn from_sections(source: impl Into<String>, sections: TemplateSections) -> Self {
        Self {
            source: source.into(),
            nodes: Vec::new(),
            sections: Some(Box::new(sections)),
        }
    }
}

// ---------------------------------------------------------------------------
// ResolvedValue
// ---------------------------------------------------------------------------

/// A resolved template variable value.
#[derive(Debug, Clone)]
pub enum ResolvedValue {
    Str(String),
    Number(f64),
    Integer(i64),
    Bool(bool),
    Array(Vec<String>),
    Null,
}

impl ResolvedValue {
    /// Truthiness test for conditional sections.
    pub fn is_truthy(&self) -> bool {
        match self {
            Self::Str(s) => !s.is_empty(),
            Self::Number(n) => *n != 0.0,
            Self::Integer(n) => *n != 0,
            Self::Bool(b) => *b,
            Self::Array(a) => !a.is_empty(),
            Self::Null => false,
        }
    }

    /// Render to string for output.
    pub fn to_display(&self) -> String {
        match self {
            Self::Str(s) => s.clone(),
            Self::Number(n) => {
                // Avoid trailing .0 for whole numbers
                if *n == (*n as i64) as f64 {
                    format!("{}", *n as i64)
                } else {
                    format!("{:.2}", n)
                }
            }
            Self::Integer(n) => format!("{}", n),
            Self::Bool(b) => if *b { "true" } else { "false" }.to_string(),
            Self::Array(a) => a.join(", "),
            Self::Null => String::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// Disclosure tiers
// ---------------------------------------------------------------------------

/// Disclosure tier for progressive rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisclosureTier {
    /// ~50 tokens/grain -- minimal detail.
    Summary,
    /// ~150 tokens/grain -- moderate detail.
    Headlines,
    /// No limit -- full detail.
    Full,
}

// ---------------------------------------------------------------------------
// Render context
// ---------------------------------------------------------------------------

/// Render context passed to the template renderer.
#[derive(Debug, Clone)]
pub struct RenderContext {
    /// Current Unix timestamp for relative time calculations.
    pub now_secs: i64,
    /// Progressive disclosure tier.
    pub tier: DisclosureTier,
    /// Total grains being rendered (for `_count`).
    pub total_count: usize,
    /// User-injected display variables from `WITH VARS { ... }`.
    ///
    /// Accessible in templates via `{{$key}}` syntax. String-only.
    pub user_vars: HashMap<String, String>,
}

// ---------------------------------------------------------------------------
// Template name validation
// ---------------------------------------------------------------------------

/// Validate a template name: `^[a-zA-Z][a-zA-Z0-9 _-]{0,63}$`
/// Allows mixed case, spaces, hyphens, underscores, and digits.
/// Rejects leading/trailing whitespace and consecutive spaces.
/// Hand-validated without regex crate.
pub fn is_valid_template_name(name: &str) -> bool {
    if name.is_empty() || name.len() > 64 {
        return false;
    }
    // Reject leading/trailing whitespace and consecutive spaces.
    if name != name.trim() || name.contains("  ") {
        return false;
    }
    let bytes = name.as_bytes();
    // First character must be a letter (upper or lower).
    if !bytes[0].is_ascii_alphabetic() {
        return false;
    }
    // Remaining characters: letter, digit, space, hyphen, or underscore.
    for &b in &bytes[1..] {
        if !(b.is_ascii_alphanumeric() || b == b' ' || b == b'-' || b == b'_') {
            return false;
        }
    }
    true
}

// ---------------------------------------------------------------------------
// Template parsing
// ---------------------------------------------------------------------------

/// Parse and validate a template string.
///
/// Returns `CalError` on invalid syntax, unknown variables, or unknown filters.
/// Single-pass O(n) character scanner.
pub fn parse_template(source: &str) -> CalResult<Template> {
    // 1. Check size limit (64KB).
    if source.len() > MAX_TEMPLATE_SIZE {
        return Err(CalError::TemplateTooLarge {
            size: source.len(),
            max: MAX_TEMPLATE_SIZE,
            span: Some(Span::zero()),
        });
    }

    // 2. Scan characters, building a Vec<TemplateNode>.
    let chars: Vec<char> = source.chars().collect();
    let nodes = parse_nodes(&chars, 0, &mut 0, 0, false, None)?;

    // 3. Validate: known variables in proper context.
    validate_variables(&nodes, None)?;

    Ok(Template {
        source: source.to_string(),
        nodes,
        sections: None,
    })
}

/// Recognise a sectioned template source and parse it (§10.6).
///
/// Returns `None` when `source` is an ordinary whole-result template, which
/// is what lets one `source: &str` column carry both forms: persistence, the
/// facade signature and `get_template` all stay unchanged, and the form is
/// recovered on load.
///
/// Detection reuses the CAL lexer, so what counts as sectioned here is
/// exactly what the parser would accept — the text must tokenize to nothing
/// but section keywords carrying bodies.
pub fn parse_sectioned_source(source: &str) -> Option<crate::ast::TemplateSectionSources> {
    use crate::lexer::{Lexer, SectionBody, Token};

    let tokens = Lexer::tokenize(source).ok()?;
    if tokens.is_empty() {
        return None;
    }

    let mut out = crate::ast::TemplateSectionSources::default();
    for st in tokens {
        let (slot, body) = match &st.token {
            Token::Header(b) => (&mut out.header, b),
            Token::Element(b) => (&mut out.element, b),
            Token::ElementSummary(b) => (&mut out.element_summary, b),
            Token::ElementOmit(b) => (&mut out.element_omit, b),
            Token::SourceBreak(b) => (&mut out.source_break, b),
            Token::Footer(b) => (&mut out.footer, b),
            // Anything else means this is not a sectioned source.
            _ => return None,
        };
        match body {
            SectionBody::Body(text) => *slot = Some(text.clone()),
            SectionBody::Bare => return None,
        }
    }
    Some(out)
}

/// Parse every section body of a sectioned source into render-ready nodes.
pub fn parse_sections(src: &crate::ast::TemplateSectionSources) -> CalResult<TemplateSections> {
    let parse = |s: &Option<String>| -> CalResult<Option<Vec<TemplateNode>>> {
        match s {
            Some(text) => Ok(Some(parse_section(text)?)),
            None => Ok(None),
        }
    };
    Ok(TemplateSections {
        header: parse(&src.header)?,
        element: parse(&src.element)?,
        element_summary: parse(&src.element_summary)?,
        element_omit: parse(&src.element_omit)?,
        source_break: parse(&src.source_break)?,
        footer: parse(&src.footer)?,
    })
}

/// Parse a template source in either form.
///
/// Sectioned when the text is a run of §10.6 sections, whole-result
/// otherwise. This is the entry point the registry uses so a template
/// reloaded from a file comes back in the form it was defined in.
pub fn parse_template_any(source: &str) -> CalResult<Template> {
    match parse_sectioned_source(source) {
        Some(src) => Ok(Template::from_sections(source, parse_sections(&src)?)),
        None => parse_template(source),
    }
}

/// Parse and validate one section body (§10.6).
///
/// Same grammar, variable set and limits as a whole-result template; the only
/// difference is that the engine, not the author, supplies the grain context.
pub fn parse_section(source: &str) -> CalResult<Vec<TemplateNode>> {
    if source.len() > MAX_TEMPLATE_SIZE {
        return Err(CalError::TemplateTooLarge {
            size: source.len(),
            max: MAX_TEMPLATE_SIZE,
            span: Some(Span::zero()),
        });
    }
    let chars: Vec<char> = source.chars().collect();
    let nodes = parse_nodes(&chars, 0, &mut 0, 0, false, None)?;
    validate_variables(&nodes, None)?;
    Ok(nodes)
}

/// Parse template nodes from a character slice. `in_each` tracks whether we
/// are inside an `{{#each}}` block (to prohibit nesting). `grain_type_ctx`
/// tracks the enclosing grain_type block for variable validation.
///
/// Returns the parsed nodes and advances `pos` past the consumed characters.
fn parse_nodes(
    chars: &[char],
    start: usize,
    pos: &mut usize,
    depth: usize,
    in_each: bool,
    stop_tag: Option<&str>,
) -> CalResult<Vec<TemplateNode>> {
    // §10.8: bound conditional nesting. Checked before any work, so it also
    // caps parser recursion on hostile input.
    if depth > MAX_TEMPLATE_NESTING {
        return Err(CalError::TemplateNestingTooDeep {
            max: MAX_TEMPLATE_NESTING,
            span: Some(make_span(start, start)),
        });
    }

    let mut nodes = Vec::new();
    *pos = start;

    while *pos < chars.len() {
        if *pos + 1 < chars.len() && chars[*pos] == '{' && chars[*pos + 1] == '{' {
            // Check for closing tags first
            let tag_start = *pos;
            let tag_content = read_tag(chars, pos)?;

            let trimmed = tag_content.trim();

            // Comment: {{! ... }}
            if let Some(stripped) = trimmed.strip_prefix('!') {
                let comment_text = stripped.trim();
                nodes.push(TemplateNode::Comment(comment_text.to_string()));
                continue;
            }

            // Closing tag: {{/...}}
            if let Some(stripped) = trimmed.strip_prefix('/') {
                let close_name = stripped.trim();
                if let Some(expected) = stop_tag {
                    if close_name == expected {
                        return Ok(nodes);
                    }
                }
                return Err(CalError::TemplateSyntaxError {
                    detail: format!("unexpected closing tag \"{}\"", close_name),
                    span: Some(make_span(tag_start, *pos)),
                });
            }

            // Else tag: {{else}}
            if trimmed == "else" {
                if let Some(expected) = stop_tag {
                    if expected == "if" || expected.starts_with("if:") {
                        // Push a sentinel and return; caller handles else
                        nodes.push(TemplateNode::Comment("__else__".to_string()));
                        // Continue parsing for else body (handled by caller)
                        return Ok(nodes);
                    }
                }
                return Err(CalError::TemplateSyntaxError {
                    detail: "{{else}} without matching {{#if}}".to_string(),
                    span: Some(make_span(tag_start, *pos)),
                });
            }

            // Section open: {{#...}}
            if let Some(stripped) = trimmed.strip_prefix('#') {
                let section = stripped.trim();
                if section.starts_with("each ") || section == "each" {
                    // {{#each grains}}
                    if in_each {
                        return Err(CalError::TemplateNestedEach {
                            span: Some(make_span(tag_start, *pos)),
                        });
                    }
                    let collection = section.strip_prefix("each").unwrap_or("").trim();
                    let collection = if collection.is_empty() {
                        "grains"
                    } else {
                        collection
                    };
                    if collection != "grains" {
                        return Err(CalError::TemplateSyntaxError {
                            detail: format!(
                                "{{{{#each}}}} only supports \"grains\", got \"{}\"",
                                collection
                            ),
                            span: Some(make_span(tag_start, *pos)),
                        });
                    }
                    let body = parse_nodes(chars, *pos, pos, depth + 1, true, Some("each"))?;
                    nodes.push(TemplateNode::Each {
                        collection: collection.to_string(),
                        body,
                    });
                } else if section.starts_with("if ") || section == "if" {
                    // {{#if field}}
                    let field = section.strip_prefix("if").unwrap_or("").trim().to_string();
                    if field.is_empty() {
                        return Err(CalError::TemplateSyntaxError {
                            detail: "{{#if}} requires a field name".to_string(),
                            span: Some(make_span(tag_start, *pos)),
                        });
                    }
                    let if_body = parse_nodes(chars, *pos, pos, depth + 1, in_each, Some("if"))?;

                    // Check if we got an {{else}} sentinel
                    let has_else = if_body
                        .last()
                        .is_some_and(|n| matches!(n, TemplateNode::Comment(c) if c == "__else__"));

                    let (if_body, else_body) = if has_else {
                        let if_nodes: Vec<TemplateNode> = if_body
                            .into_iter()
                            .filter(|n| !matches!(n, TemplateNode::Comment(c) if c == "__else__"))
                            .collect();
                        let else_body = parse_nodes(chars, *pos, pos, depth + 1, in_each, Some("if"))?;
                        (if_nodes, else_body)
                    } else {
                        (if_body, Vec::new())
                    };

                    nodes.push(TemplateNode::Condition {
                        field,
                        if_body,
                        else_body,
                    });
                } else if section.starts_with("grain_type ") {
                    // {{#grain_type "fact"}}
                    let rest = section.strip_prefix("grain_type").unwrap_or("").trim();
                    let grain_type =
                        unquote(rest).ok_or_else(|| CalError::TemplateSyntaxError {
                            detail: format!(
                                "{{{{#grain_type}}}} requires a quoted type name, got \"{}\"",
                                rest
                            ),
                            span: Some(make_span(tag_start, *pos)),
                        })?;
                    let body = parse_nodes(chars, *pos, pos, depth + 1, in_each, Some("grain_type"))?;
                    nodes.push(TemplateNode::GrainTypeBlock { grain_type, body });
                } else {
                    // §10.2 conditional block: `{{#var}}...{{/var}}` renders
                    // the body when `var` is truthy. This is what makes the
                    // §10.5 `{{#grain.is_full}}` / `{{#grain.is_summary}}`
                    // sections usable, and what the §10.6 example's
                    // `{{#grain.confidence}}` needs.
                    let field = section.to_string();
                    if field.contains(char::is_whitespace) {
                        return Err(CalError::TemplateSyntaxError {
                            detail: format!("unknown section type \"{}\"", section),
                            span: Some(make_span(tag_start, *pos)),
                        });
                    }
                    validate_variable_name(&field, None)?;
                    let if_body = parse_nodes(chars, *pos, pos, depth + 1, in_each, Some(&field))?;
                    nodes.push(TemplateNode::Condition {
                        field,
                        if_body,
                        else_body: Vec::new(),
                    });
                }
                continue;
            }

            // Inverted section: {{^field}}
            if let Some(stripped) = trimmed.strip_prefix('^') {
                let field = stripped.trim().to_string();
                if field.is_empty() {
                    return Err(CalError::TemplateSyntaxError {
                        detail: "{{^}} requires a field name".to_string(),
                        span: Some(make_span(tag_start, *pos)),
                    });
                }
                let body = parse_nodes(chars, *pos, pos, depth + 1, in_each, Some(&field))?;
                nodes.push(TemplateNode::InvertedSection { field, body });
                continue;
            }

            // Variable (possibly with filters): {{name | filter1 | filter2}}
            let (var_name, filters) = parse_variable_and_filters(trimmed, tag_start, *pos)?;
            nodes.push(TemplateNode::Variable {
                name: var_name,
                filters,
            });
        } else {
            // Literal text -- accumulate until the next `{{`.
            let text_start = *pos;
            while *pos < chars.len() {
                if *pos + 1 < chars.len() && chars[*pos] == '{' && chars[*pos + 1] == '{' {
                    break;
                }
                *pos += 1;
            }
            let text: String = chars[text_start..*pos].iter().collect();
            if !text.is_empty() {
                nodes.push(TemplateNode::Text(text));
            }
        }
    }

    // If we expected a closing tag but hit EOF, that's an error.
    if let Some(expected) = stop_tag {
        return Err(CalError::TemplateSyntaxError {
            detail: format!("unclosed {{{{#{}}}}} block", expected),
            span: Some(make_span(start, chars.len())),
        });
    }

    Ok(nodes)
}

/// Read a `{{ ... }}` tag, advancing `pos` past the closing `}}`.
/// Returns the content between the delimiters (trimmed of delimiters).
fn read_tag(chars: &[char], pos: &mut usize) -> CalResult<String> {
    let tag_start = *pos;
    // Skip opening `{{`
    *pos += 2;

    let content_start = *pos;
    let mut depth = 1;

    while *pos < chars.len() {
        if *pos + 1 < chars.len() && chars[*pos] == '}' && chars[*pos + 1] == '}' {
            depth -= 1;
            if depth == 0 {
                let content: String = chars[content_start..*pos].iter().collect();
                *pos += 2; // Skip closing `}}`
                return Ok(content);
            }
        }
        if *pos + 1 < chars.len() && chars[*pos] == '{' && chars[*pos + 1] == '{' {
            depth += 1;
            *pos += 2;
            continue;
        }
        *pos += 1;
    }

    Err(CalError::TemplateSyntaxError {
        detail: "unclosed template tag (missing `}}`)".to_string(),
        span: Some(make_span(tag_start, *pos)),
    })
}

/// Parse a variable expression with optional filter pipeline.
/// Input: `"name | filter1 arg | filter2"` (already trimmed of `{{ }}`).
fn parse_variable_and_filters(
    expr: &str,
    tag_start: usize,
    tag_end: usize,
) -> CalResult<(String, Vec<Filter>)> {
    let parts = split_pipe(expr);
    if parts.is_empty() {
        return Err(CalError::TemplateSyntaxError {
            detail: "empty variable expression".to_string(),
            span: Some(make_span(tag_start, tag_end)),
        });
    }

    let var_name = parts[0].trim().to_string();
    if var_name.is_empty() {
        return Err(CalError::TemplateSyntaxError {
            detail: "empty variable name".to_string(),
            span: Some(make_span(tag_start, tag_end)),
        });
    }

    let mut filters = Vec::new();
    for part in &parts[1..] {
        let filter = parse_single_filter(part.trim(), tag_start, tag_end)?;
        filters.push(filter);
    }

    Ok((var_name, filters))
}

/// Parse a single filter segment like `"truncate 80"` or `"relative"` or
/// `"date \"%Y-%m-%d\""`.
fn parse_single_filter(segment: &str, tag_start: usize, tag_end: usize) -> CalResult<Filter> {
    let segment = segment.trim();
    if segment.is_empty() {
        return Err(CalError::TemplateSyntaxError {
            detail: "empty filter name".to_string(),
            span: Some(make_span(tag_start, tag_end)),
        });
    }

    // Split on first whitespace or opening paren to get name and optional argument.
    // Supports both `truncate 5` (space-separated) and `truncate(5)` (parenthesized).
    let (name, arg) = if let Some(paren_idx) = segment.find('(') {
        // Parenthesized syntax: name(arg)
        let name = segment[..paren_idx].trim();
        let rest = &segment[paren_idx + 1..];
        let close = rest.rfind(')').unwrap_or(rest.len());
        let raw_arg = rest[..close].trim();
        let arg = unquote(raw_arg).unwrap_or_else(|| raw_arg.to_string());
        (name, Some(arg))
    } else {
        match segment.find(|c: char| c.is_whitespace()) {
            Some(idx) => {
                let name = &segment[..idx];
                let raw_arg = segment[idx..].trim();
                let arg = unquote(raw_arg).unwrap_or_else(|| raw_arg.to_string());
                (name, Some(arg))
            }
            None => (segment, None),
        }
    };

    // Validate filter name is known.
    if !KNOWN_FILTERS.contains(&name) {
        return Err(CalError::TemplateUnknownFilter {
            name: name.to_string(),
            span: Some(make_span(tag_start, tag_end)),
        });
    }

    // Parse-time validation of filter arguments.
    match name {
        "truncate" => {
            if let Some(ref a) = arg {
                match a.parse::<usize>() {
                    Ok(n) if (1..=100_000).contains(&n) => {}
                    Ok(n) => {
                        return Err(CalError::TemplateSyntaxError {
                            detail: format!("truncate argument {} out of range [1, 100000]", n),
                            span: Some(make_span(tag_start, tag_end)),
                        });
                    }
                    Err(_) => {
                        return Err(CalError::TemplateSyntaxError {
                            detail: format!("truncate argument \"{}\" is not a valid number", a),
                            span: Some(make_span(tag_start, tag_end)),
                        });
                    }
                }
            }
        }
        "date" => {
            if let Some(ref a) = arg {
                if a.len() > 256 {
                    return Err(CalError::TemplateSyntaxError {
                        detail: format!("date format string too long ({} chars, max 256)", a.len()),
                        span: Some(make_span(tag_start, tag_end)),
                    });
                }
            }
        }
        _ => {}
    }

    Ok(Filter {
        name: name.to_string(),
        arg,
    })
}

/// Split an expression on `|` characters, respecting quoted strings.
fn split_pipe(expr: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut in_quote = false;
    let mut quote_char = '"';

    for ch in expr.chars() {
        if in_quote {
            current.push(ch);
            if ch == quote_char {
                in_quote = false;
            }
        } else if ch == '"' || ch == '\'' {
            in_quote = true;
            quote_char = ch;
            current.push(ch);
        } else if ch == '|' {
            parts.push(current.clone());
            current.clear();
        } else {
            current.push(ch);
        }
    }
    if !current.is_empty() || parts.is_empty() {
        parts.push(current);
    }
    parts
}

/// Remove surrounding quotes from a string if present.
fn unquote(s: &str) -> Option<String> {
    let s = s.trim();
    if s.len() >= 2
        && ((s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')))
    {
        return Some(s[1..s.len() - 1].to_string());
    }
    // Return the string as-is if not quoted (for unquoted arguments).
    if !s.is_empty() {
        Some(s.to_string())
    } else {
        None
    }
}

/// Create a Span from byte offsets (approximated from char positions).
fn make_span(start: usize, end: usize) -> Span {
    Span::new(start, end, 1, start + 1)
}

// ---------------------------------------------------------------------------
// Variable validation (parse-time)
// ---------------------------------------------------------------------------

/// Validate all variables in the node tree against the closed variable set.
fn validate_variables(nodes: &[TemplateNode], grain_type_ctx: Option<&str>) -> CalResult<()> {
    for node in nodes {
        match node {
            TemplateNode::Variable { name, .. } => {
                validate_variable_name(name, grain_type_ctx)?;
            }
            TemplateNode::Each { body, .. } => {
                validate_variables(body, grain_type_ctx)?;
            }
            TemplateNode::Condition {
                field,
                if_body,
                else_body,
            } => {
                validate_variable_name(field, grain_type_ctx)?;
                validate_variables(if_body, grain_type_ctx)?;
                validate_variables(else_body, grain_type_ctx)?;
            }
            TemplateNode::InvertedSection { field, body } => {
                validate_variable_name(field, grain_type_ctx)?;
                validate_variables(body, grain_type_ctx)?;
            }
            TemplateNode::GrainTypeBlock { grain_type, body } => {
                validate_variables(body, Some(grain_type))?;
            }
            TemplateNode::Text(_) | TemplateNode::Comment(_) => {}
        }
    }
    Ok(())
}

/// Validate a single variable name against the closed set.
///
/// `$`-prefixed names are user-injected display variables (from `WITH VARS`)
/// and bypass the closed set check — they are validated at query time, not
/// template definition time.
fn validate_variable_name(name: &str, _grain_type_ctx: Option<&str>) -> CalResult<()> {
    // User-injected display variables: $-prefixed, bypass closed set.
    if let Some(stripped) = name.strip_prefix('$') {
        // Basic validation: must be a valid identifier after stripping $.
        if !stripped.is_empty()
            && (stripped.as_bytes()[0].is_ascii_alphabetic() || stripped.as_bytes()[0] == b'_')
            && stripped
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b == b'_')
        {
            return Ok(());
        }
        return Err(CalError::TemplateUnknownVariable {
            name: name.to_string(),
            span: None,
            suggestion: Some("user variable names must match $[a-zA-Z_][a-zA-Z0-9_]*".into()),
        });
    }

    if ALL_VALID_VARIABLES.contains(&name) {
        return Ok(());
    }

    // CAL §10.5 namespaced variables. The set stays closed: a `grain.` name
    // must still resolve to a spec grain variable or an allowed grain field.
    if matches!(name, "disclosure.level" | "timestamp") {
        return Ok(());
    }
    if let Some(field) = name.strip_prefix("grain.") {
        if SPEC_GRAIN_VARIABLES.contains(&field) || ALLOWED_FIELDS.contains(&field) {
            return Ok(());
        }
    }
    if let Some(field) = name.strip_prefix("assembly.") {
        if SPEC_ASSEMBLY_VARIABLES.contains(&field) {
            return Ok(());
        }
    }
    if let Some(field) = name.strip_prefix("budget.") {
        if SPEC_BUDGET_VARIABLES.contains(&field) {
            return Ok(());
        }
    }
    if let Some(field) = name.strip_prefix("source.") {
        if SPEC_SOURCE_VARIABLES.contains(&field) {
            return Ok(());
        }
    }

    // Try to suggest a close match.
    let suggestion = find_closest_variable(name);
    Err(CalError::TemplateUnknownVariable {
        name: name.to_string(),
        span: None,
        suggestion,
    })
}

/// Simple Levenshtein-like closest match for unknown variable suggestions.
fn find_closest_variable(name: &str) -> Option<String> {
    let mut best: Option<(&str, usize)> = None;
    for &var in ALL_VALID_VARIABLES {
        let dist = simple_edit_distance(name, var);
        if dist <= 3 && best.is_none_or(|(_, d)| dist < d) {
            best = Some((var, dist));
        }
    }
    best.map(|(v, _)| format!("did you mean \"{}\"?", v))
}

/// Simple edit distance (substitution-only for performance).
fn simple_edit_distance(a: &str, b: &str) -> usize {
    let a_bytes = a.as_bytes();
    let b_bytes = b.as_bytes();
    let len = a_bytes.len().max(b_bytes.len());
    let min_len = a_bytes.len().min(b_bytes.len());
    let mut dist = len - min_len;
    for i in 0..min_len {
        if a_bytes[i] != b_bytes[i] {
            dist += 1;
        }
    }
    dist
}

// ---------------------------------------------------------------------------
// Variable resolution (runtime)
// ---------------------------------------------------------------------------

/// Resolve a template variable against grain fields and render context.
/// CAL §10.3.3 `humanize()` — strip a namespace prefix and turn underscores
/// into spaces, so `"mg:works_at"` reads as `"works at"`. The raw relation
/// stays available in the machine envelope.
pub fn humanize_relation(relation: &str) -> String {
    let stripped = relation.rsplit(':').next().unwrap_or(relation);
    stripped.replace('_', " ")
}

/// CAL §10.3.4 time humanization. Full ISO stays in the machine envelope.
///
/// Granularity only ever coarsens as the age grows — minutes, hours, days,
/// weeks, months, years. Reporting 29 days as "4w ago" and 31 days as
/// "31d ago" read as two different scales for two neighbouring instants.
///
/// Accepts either epoch milliseconds or epoch seconds: anything past
/// 100_000_000_000 cannot be a plausible timestamp in seconds (year 5138) but
/// is an ordinary one in milliseconds (1973 onward).
pub fn humanize_time(created_ms: i64, now_secs: i64) -> String {
    let created_secs = if created_ms > 100_000_000_000 { created_ms / 1000 } else { created_ms };
    let age = now_secs.saturating_sub(created_secs);
    const DAY: i64 = 86_400;
    if age < 3600 {
        format!("{}m ago", (age / 60).max(0))
    } else if age < DAY {
        format!("{}h ago", age / 3600)
    } else if age < 7 * DAY {
        let d = age / DAY;
        if d <= 1 { "yesterday".to_string() } else { format!("{d}d ago") }
    } else if age < 30 * DAY {
        format!("{}w ago", age / (7 * DAY))
    } else if age < 365 * DAY {
        format!("{}mo ago", age / (30 * DAY))
    } else {
        format!("{}y ago", age / (365 * DAY))
    }
}

/// CAL §10.3.2 Content Projection Model — the text content of a rendered
/// element, per grain type. Everything else stays a metadata attribute or
/// remains in the machine envelope.
fn project_content(grain: &CalGrainResult) -> ResolvedValue {
    let f = &grain.fields;
    let get = |k: &str| f.get(k).and_then(|v| v.as_str()).map(str::to_string);
    let text = match grain.grain_type.as_str() {
        "fact" => {
            let rel = get("relation").unwrap_or_default();
            let obj = get("object").unwrap_or_default();
            let humanized = humanize_relation(&rel);
            match (humanized.is_empty(), obj.is_empty()) {
                (true, _) => obj,
                (_, true) => humanized,
                _ => format!("{humanized} {obj}"),
            }
        }
        "event" => get("content").unwrap_or_default(),
        "goal" | "tool" | "observation" | "consensus" => get("object").unwrap_or_default(),
        "reasoning" => get("conclusion").unwrap_or_default(),
        "state" => get("plan").or_else(|| get("state_value")).unwrap_or_default(),
        "workflow" => match f.get("nodes").and_then(|v| v.as_array()) {
            Some(nodes) => nodes
                .iter()
                .filter_map(|n| n.as_str())
                .collect::<Vec<_>>()
                .join(" -> "),
            None => String::new(),
        },
        "consent" => get("purpose").unwrap_or_default(),
        "skill" => get("description").unwrap_or_default(),
        // OMS §8.12: `summary` is `{template_id, args}`, not prose — reading it
        // as a string yielded nothing. Project the argument values, which is
        // what a reviewer needs; the template id alone means nothing.
        "recommendation" => f
            .get("summary")
            .and_then(|s| s.get("args"))
            .and_then(|a| a.as_object())
            .map(|o| {
                let mut vs: Vec<String> = o
                    .iter()
                    .map(|(k, v)| match v {
                        serde_json::Value::String(t) => format!("{k}={t}"),
                        other => format!("{k}={other}"),
                    })
                    .collect();
                vs.sort();
                vs.join(", ")
            })
            .or_else(|| get("target_ref"))
            .unwrap_or_default(),
        _ => get("content").or_else(|| get("object")).unwrap_or_default(),
    };
    if text.is_empty() { ResolvedValue::Null } else { ResolvedValue::Str(text) }
}

/// CAL §10.5 grain-level variables, resolved under the `grain.` namespace.
/// `name` arrives with the prefix already stripped.
fn resolve_grain_var(field: &str, grain: &CalGrainResult, ctx: &RenderContext) -> ResolvedValue {
    match field {
        "content" => project_content(grain),
        "type" => ResolvedValue::Str(grain.grain_type.clone()),
        "hash" => ResolvedValue::Str(grain.hash.clone()),
        "score" => ResolvedValue::Number(grain.score),
        "humanized_relation" => match grain.fields.get("relation").and_then(|v| v.as_str()) {
            Some(r) => ResolvedValue::Str(humanize_relation(r)),
            None => ResolvedValue::Null,
        },
        "relative_time" => match grain.fields.get("created_at").and_then(|v| v.as_i64()) {
            Some(ms) => ResolvedValue::Str(humanize_time(ms, ctx.now_secs)),
            None => ResolvedValue::Null,
        },
        "is_full" => ResolvedValue::Bool(ctx.tier == DisclosureTier::Full),
        "is_summary" => ResolvedValue::Bool(ctx.tier == DisclosureTier::Summary),
        // Everything else is a plain grain field, behind the same closed set.
        _ => {
            if !ALLOWED_FIELDS.contains(&field) {
                return ResolvedValue::Null;
            }
            resolve_from_fields(&grain.fields, field)
        }
    }
}

fn resolve_variable(
    name: &str,
    grain: Option<&CalGrainResult>,
    index: Option<usize>,
    ctx: &RenderContext,
    env: &RenderEnv<'_>,
) -> ResolvedValue {
    // 0. User-injected display variables ($prefix).
    if let Some(var_name) = name.strip_prefix('$') {
        return match ctx.user_vars.get(var_name) {
            Some(val) => ResolvedValue::Str(val.clone()),
            None => ResolvedValue::Null,
        };
    }

    // 0b. CAL §10.5 namespaced variables. `grain.*` needs a grain; the
    // assembly-level ones are answered from the render context.
    match name {
        "disclosure.level" => {
            return ResolvedValue::Str(
                match ctx.tier {
                    DisclosureTier::Summary => "summary",
                    DisclosureTier::Headlines => "headlines",
                    DisclosureTier::Full => "full",
                }
                .to_string(),
            )
        }
        "timestamp" => return ResolvedValue::Integer(ctx.now_secs),
        _ => {}
    }

    // CAL §10.5 assembly-, budget- and source-level variables. Absent
    // context resolves to Null, which §10.8 renders as the empty string —
    // the same template works for RECALL and ASSEMBLE.
    if let Some(field) = name.strip_prefix("assembly.") {
        let Some(a) = env.assembly else {
            return ResolvedValue::Null;
        };
        return match field {
            "name" => ResolvedValue::Str(a.name.clone()),
            "intent" => ResolvedValue::Str(a.intent.clone()),
            "source_count" => ResolvedValue::Integer(a.source_count as i64),
            "grain_count" => ResolvedValue::Integer(a.grain_count as i64),
            _ => ResolvedValue::Null,
        };
    }
    if let Some(field) = name.strip_prefix("budget.") {
        let Some(a) = env.assembly else {
            return ResolvedValue::Null;
        };
        return match field {
            "total" => ResolvedValue::Integer(a.budget_total as i64),
            "used" => ResolvedValue::Integer(a.budget_used as i64),
            "remaining" => ResolvedValue::Integer(a.budget_remaining() as i64),
            "unit" => ResolvedValue::Str(a.budget_unit.clone()),
            "utilization" => ResolvedValue::Number(a.budget_utilization()),
            _ => ResolvedValue::Null,
        };
    }
    if let Some(field) = name.strip_prefix("source.") {
        let Some((src, idx)) = env.source else {
            return ResolvedValue::Null;
        };
        return match field {
            "label" => ResolvedValue::Str(src.label.to_string()),
            "index" => ResolvedValue::Integer(idx as i64),
            "priority" => ResolvedValue::Integer(src.priority as i64),
            "grain_count" => ResolvedValue::Integer(src.grains.len() as i64),
            "tokens_used" => ResolvedValue::Integer(src.tokens_used as i64),
            "truncated" => ResolvedValue::Bool(src.truncated),
            _ => ResolvedValue::Null,
        };
    }

    if let Some(field) = name.strip_prefix("grain.") {
        return match grain {
            Some(g) => resolve_grain_var(field, g, ctx),
            None => ResolvedValue::Null,
        };
    }

    // 1. Metadata variables (_prefix).
    match name {
        "_index" => return ResolvedValue::Integer(index.unwrap_or(0) as i64),
        "_count" => return ResolvedValue::Integer(ctx.total_count as i64),
        "_first" => return ResolvedValue::Bool(index == Some(0)),
        "_last" => return ResolvedValue::Bool(index.is_some_and(|i| i + 1 == ctx.total_count)),
        "_now" => return ResolvedValue::Integer(ctx.now_secs),
        _ => {}
    }

    let Some(grain) = grain else {
        return ResolvedValue::Null;
    };

    // 2. Direct CalGrainResult fields.
    match name {
        "hash" => return ResolvedValue::Str(grain.hash.clone()),
        "grain_type" => return ResolvedValue::Str(grain.grain_type.clone()),
        "score" => return ResolvedValue::Number(grain.score),
        _ => {}
    }

    // 3. Defense-in-depth: check ALLOWED_FIELDS (F5).
    if !ALLOWED_FIELDS.contains(&name) {
        return ResolvedValue::Null;
    }

    // 4. Lookup in grain.fields.
    resolve_from_fields(&grain.fields, name)
}

/// Extract a value from a serde_json::Value object by field name.
fn resolve_from_fields(fields: &serde_json::Value, name: &str) -> ResolvedValue {
    let Some(val) = fields.get(name) else {
        return ResolvedValue::Null;
    };

    match val {
        serde_json::Value::String(s) => ResolvedValue::Str(s.clone()),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                ResolvedValue::Integer(i)
            } else if let Some(f) = n.as_f64() {
                ResolvedValue::Number(f)
            } else {
                ResolvedValue::Str(n.to_string())
            }
        }
        serde_json::Value::Bool(b) => ResolvedValue::Bool(*b),
        serde_json::Value::Array(arr) => {
            let strings: Vec<String> = arr
                .iter()
                .map(|v| match v {
                    serde_json::Value::String(s) => s.clone(),
                    other => other.to_string(),
                })
                .collect();
            ResolvedValue::Array(strings)
        }
        serde_json::Value::Null => ResolvedValue::Null,
        serde_json::Value::Object(_) => {
            // Render objects as JSON.
            ResolvedValue::Str(val.to_string())
        }
    }
}

// ---------------------------------------------------------------------------
// Filter pipeline
// ---------------------------------------------------------------------------

/// Apply a filter to a resolved value.
pub fn apply_filter(
    value: &ResolvedValue,
    filter: &Filter,
    ctx: &RenderContext,
) -> CalResult<ResolvedValue> {
    match filter.name.as_str() {
        "truncate" => {
            // F2 safety: argument validated at parse time to be in [1, 100_000].
            let n: usize = filter
                .arg
                .as_ref()
                .and_then(|a| a.parse().ok())
                .unwrap_or(80);
            // Apply tier-based effective truncation.
            let effective = effective_truncate(Some(n), ctx.tier);
            let s = value.to_display();
            if char_len(&s) > effective {
                let truncated = truncate_at_char_boundary(&s, effective);
                Ok(ResolvedValue::Str(format!("{}...", truncated)))
            } else {
                Ok(ResolvedValue::Str(s))
            }
        }
        "relative" => match value {
            ResolvedValue::Integer(epoch) => Ok(ResolvedValue::Str(
                super::humanize::humanize_time(*epoch, ctx.now_secs),
            )),
            ResolvedValue::Number(n) => Ok(ResolvedValue::Str(super::humanize::humanize_time(
                *n as i64,
                ctx.now_secs,
            ))),
            _ => Ok(ResolvedValue::Str(value.to_display())),
        },
        "humanize" => {
            let s = value.to_display();
            Ok(ResolvedValue::Str(super::humanize::humanize_relation(&s)))
        }
        "percent" => match value {
            ResolvedValue::Number(n) => Ok(ResolvedValue::Str(format!("{}%", (*n * 100.0) as u32))),
            ResolvedValue::Integer(n) => Ok(ResolvedValue::Str(format!("{}%", *n * 100))),
            _ => Ok(ResolvedValue::Str(value.to_display())),
        },
        "uppercase" => Ok(ResolvedValue::Str(value.to_display().to_uppercase())),
        "lowercase" => Ok(ResolvedValue::Str(value.to_display().to_lowercase())),
        "json" => {
            // Render the value as a JSON-compatible string.
            let s = match value {
                ResolvedValue::Str(s) => {
                    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
                }
                ResolvedValue::Number(n) => format!("{}", n),
                ResolvedValue::Integer(n) => format!("{}", n),
                ResolvedValue::Bool(b) => format!("{}", b),
                ResolvedValue::Array(a) => {
                    let items: Vec<String> = a
                        .iter()
                        .map(|s| format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")))
                        .collect();
                    format!("[{}]", items.join(","))
                }
                ResolvedValue::Null => "null".to_string(),
            };
            Ok(ResolvedValue::Str(s))
        }
        "default" => {
            if value.is_truthy() {
                Ok(value.clone())
            } else {
                Ok(ResolvedValue::Str(filter.arg.clone().unwrap_or_default()))
            }
        }
        "join" => {
            let sep = filter.arg.as_deref().unwrap_or(", ");
            match value {
                ResolvedValue::Array(items) => Ok(ResolvedValue::Str(items.join(sep))),
                _ => Ok(ResolvedValue::Str(value.to_display())),
            }
        }
        "date" => match value {
            ResolvedValue::Integer(epoch) => {
                let fmt = filter.arg.as_deref().unwrap_or("%Y-%m-%d");
                Ok(ResolvedValue::Str(format_epoch(*epoch, fmt)))
            }
            ResolvedValue::Number(n) => {
                let fmt = filter.arg.as_deref().unwrap_or("%Y-%m-%d");
                Ok(ResolvedValue::Str(format_epoch(*n as i64, fmt)))
            }
            _ => Ok(ResolvedValue::Str(value.to_display())),
        },
        unknown => Err(CalError::TemplateUnknownFilter {
            name: unknown.to_string(),
            span: None,
        }),
    }
}

// ---------------------------------------------------------------------------
// Date formatting (zero-dependency)
// ---------------------------------------------------------------------------

/// Format a Unix epoch timestamp using a simple format string.
///
/// Supported placeholders: `%Y`, `%m`, `%d`, `%H`, `%M`, `%S`.
/// UTC only, post-epoch only. Negative epochs return `"<invalid date>"` (F3).
pub fn format_epoch(epoch_secs: i64, fmt: &str) -> String {
    // F3 safety: reject negative epochs.
    if epoch_secs < 0 {
        return "<invalid date>".to_string();
    }
    // F4 safety: format string length re-checked at runtime as defense-in-depth.
    if fmt.len() > 256 {
        return "<format too long>".to_string();
    }

    // Extract H/M/S from time-of-day seconds (safe: epoch_secs >= 0).
    let day_secs = (epoch_secs % 86400) as u32;
    let h = day_secs / 3600;
    let m = (day_secs % 3600) / 60;
    let s = day_secs % 60;

    // Date from civil_from_days (Howard Hinnant algorithm).
    let total_days = (epoch_secs / 86400) as i32;
    let z = total_days + 719468;
    let era = if z >= 0 { z } else { z - 146096 } / 146097;
    let doe = (z - era * 146097) as u32;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe as i32 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let mo = if mp < 10 { mp + 3 } else { mp - 9 };
    let year = if mo <= 2 { y + 1 } else { y };

    // Replace format placeholders.
    let mut result = String::with_capacity(fmt.len() + 16);
    let fchars: Vec<char> = fmt.chars().collect();
    let mut i = 0;
    while i < fchars.len() {
        if fchars[i] == '%' && i + 1 < fchars.len() {
            match fchars[i + 1] {
                'Y' => {
                    result.push_str(&format!("{:04}", year));
                    i += 2;
                }
                'm' => {
                    result.push_str(&format!("{:02}", mo));
                    i += 2;
                }
                'd' => {
                    result.push_str(&format!("{:02}", d));
                    i += 2;
                }
                'H' => {
                    result.push_str(&format!("{:02}", h));
                    i += 2;
                }
                'M' => {
                    result.push_str(&format!("{:02}", m));
                    i += 2;
                }
                'S' => {
                    result.push_str(&format!("{:02}", s));
                    i += 2;
                }
                _ => {
                    result.push('%');
                    result.push(fchars[i + 1]);
                    i += 2;
                }
            }
        } else {
            result.push(fchars[i]);
            i += 1;
        }
    }

    result
}

// ---------------------------------------------------------------------------
// String helpers
// ---------------------------------------------------------------------------

/// Count the number of characters in a string.
fn char_len(s: &str) -> usize {
    s.chars().count()
}

/// Truncate a string at a character boundary (not byte boundary).
fn truncate_at_char_boundary(s: &str, max_chars: usize) -> &str {
    if max_chars == 0 {
        return "";
    }
    match s.char_indices().nth(max_chars) {
        Some((idx, _)) => &s[..idx],
        None => s,
    }
}

/// Truncate a string, appending "..." if truncated. Used by default_grain_render.
fn truncate_str(s: &str, max_chars: usize) -> String {
    if char_len(s) > max_chars {
        format!("{}...", truncate_at_char_boundary(s, max_chars))
    } else {
        s.to_string()
    }
}

// ---------------------------------------------------------------------------
// Progressive disclosure
// ---------------------------------------------------------------------------

/// Select disclosure tier based on available tokens per grain.
pub fn select_tier(budget_tokens: u32, grain_count: usize) -> DisclosureTier {
    if grain_count == 0 {
        return DisclosureTier::Full;
    }
    // F7 safety: clamp grain_count to u32::MAX before cast to prevent
    // silent truncation to zero on 64-bit platforms.
    let clamped_count = grain_count.min(u32::MAX as usize) as u32;
    if clamped_count == 0 {
        return DisclosureTier::Summary;
    }
    let tokens_per_grain = budget_tokens / clamped_count;
    if tokens_per_grain >= 200 {
        DisclosureTier::Full
    } else if tokens_per_grain >= 80 {
        DisclosureTier::Headlines
    } else {
        DisclosureTier::Summary
    }
}

/// Compute effective truncation limit based on explicit limit and tier.
fn effective_truncate(explicit_limit: Option<usize>, tier: DisclosureTier) -> usize {
    match explicit_limit {
        Some(n) => match tier {
            DisclosureTier::Summary => n.min(40),
            DisclosureTier::Headlines => n.min(80),
            DisclosureTier::Full => n,
        },
        None => match tier {
            DisclosureTier::Summary => 40,
            DisclosureTier::Headlines => 80,
            DisclosureTier::Full => usize::MAX,
        },
    }
}

// ---------------------------------------------------------------------------
// Template renderer
// ---------------------------------------------------------------------------

/// Render a grain result set using a parsed template.
pub fn render(
    template: &Template,
    grains: &[CalGrainResult],
    ctx: &RenderContext,
) -> CalResult<String> {
    render_with_limit(template, grains, ctx, MAX_RENDER_OUTPUT_SIZE)
}

/// Render with a configurable output size limit.
pub fn render_with_limit(
    template: &Template,
    grains: &[CalGrainResult],
    ctx: &RenderContext,
    max_output_size: usize,
) -> CalResult<String> {
    if let Some(sections) = template.sections() {
        return render_sectioned(
            sections,
            &[RenderSource::single(grains)],
            ctx,
            None,
            max_output_size,
        );
    }
    let mut output = String::with_capacity(grains.len().min(1024) * 128);
    render_nodes(
        &template.nodes,
        grains,
        None,
        ctx,
        &RenderEnv::default(),
        &mut output,
        max_output_size,
    )?;
    Ok(output)
}

/// Report whether rendering `template` over `grain_count` grains will hit the
/// §10.8 `{{#each}}` iteration cap.
///
/// Truncating the output silently would read as "these are all the grains",
/// so the caller turns this into a `CAL-W011`. Only the whole-result form can
/// trip it — a sectioned template has the engine drive iteration.
pub fn each_iteration_cap(template: &Template, grain_count: usize) -> Option<(usize, usize)> {
    (grain_count > MAX_EACH_ITERATIONS && nodes_contain_each(template.nodes()))
        .then_some((MAX_EACH_ITERATIONS, grain_count))
}

/// Whether a node tree drives its own iteration with `{{#each}}`.
///
/// Two callers need this: the §10.8 cap warning, and `render_sectioned`, which
/// uses it to decide whether the whole-result grain slice is worth materializing
/// at all.
fn nodes_contain_each(nodes: &[TemplateNode]) -> bool {
    nodes.iter().any(|n| match n {
        TemplateNode::Each { .. } => true,
        TemplateNode::Condition {
            if_body, else_body, ..
        } => nodes_contain_each(if_body) || nodes_contain_each(else_body),
        TemplateNode::InvertedSection { body, .. }
        | TemplateNode::GrainTypeBlock { body, .. } => nodes_contain_each(body),
        _ => false,
    })
}

/// Render sources through a sectioned template (OMS CAL §10.6).
///
/// HEADER once, then each source's grains through `ELEMENT` (or
/// `ELEMENT_SUMMARY` when disclosure is below full), with `SOURCE_BREAK`
/// *between* sources, then FOOTER. Pieces are joined with newlines: the flat
/// semantic model is line-oriented, and section bodies have their brace
/// layout trimmed, so the separator has to come from the engine.
pub fn render_sectioned(
    sections: &TemplateSections,
    sources: &[RenderSource<'_>],
    ctx: &RenderContext,
    assembly: Option<&AssemblyContext>,
    max_output_size: usize,
) -> CalResult<String> {
    // A grain's body: ELEMENT at full disclosure, ELEMENT_SUMMARY when
    // squeezed. Either falls back to the other so a template that defines
    // only one still renders at every tier.
    let (primary, fallback) = match ctx.tier {
        DisclosureTier::Full => (&sections.element, &sections.element_summary),
        _ => (&sections.element_summary, &sections.element),
    };
    let element = primary.as_ref().or(fallback.as_ref());

    // `render_nodes` only reads the whole-result slice to drive `{{#each}}`,
    // which a sectioned template has no reason to use — the engine already
    // iterates. So flatten the sources only when some section actually asks
    // for it, rather than deep-cloning every grain (JSON payload and all) on
    // every render that will never look at the result.
    let needs_all_grains = [
        &sections.header,
        &sections.element,
        &sections.element_summary,
        &sections.element_omit,
        &sections.source_break,
        &sections.footer,
    ]
    .into_iter()
    .flatten()
    .any(|nodes| nodes_contain_each(nodes));
    let all_grains: Vec<CalGrainResult> = if needs_all_grains {
        sources.iter().flat_map(|s| s.grains.iter().cloned()).collect()
    } else {
        Vec::new()
    };

    let mut pieces: Vec<String> = Vec::new();
    let mut emitted = 0usize;
    // Running total, not a re-sum per grain: summing every piece on every
    // iteration makes the guard quadratic in the grain count, and the guard
    // exists for exactly the large renders that would suffer most.
    let mut rendered_bytes = 0usize;

    let render_one = |nodes: &[TemplateNode],
                      current: Option<(&CalGrainResult, usize)>,
                      env: &RenderEnv<'_>,
                      pieces: &mut Vec<String>,
                      rendered_bytes: &mut usize|
     -> CalResult<()> {
        let mut buf = String::new();
        render_nodes(nodes, &all_grains, current, ctx, env, &mut buf, max_output_size)?;
        *rendered_bytes += buf.len();
        pieces.push(buf);
        Ok(())
    };

    // HEADER and FOOTER sit outside any source, so `source.*` is unbound
    // there — §10.5 scopes those variables to an element run.
    let outer = RenderEnv {
        assembly,
        source: None,
    };

    if let Some(header) = &sections.header {
        render_one(header, None, &outer, &mut pieces, &mut rendered_bytes)?;
    }

    for (s_idx, source) in sources.iter().enumerate() {
        if s_idx > 0 {
            if let Some(brk) = &sections.source_break {
                render_one(brk, None, &outer, &mut pieces, &mut rendered_bytes)?;
            }
        }
        let env = RenderEnv {
            assembly,
            source: Some((source, s_idx)),
        };
        if let Some(element) = element {
            for grain in source.grains {
                render_one(element, Some((grain, emitted)), &env, &mut pieces, &mut rendered_bytes)?;
                emitted += 1;
                // F1 safety: bound the output as we go, not only at the end.
                if rendered_bytes > max_output_size {
                    return Err(CalError::RenderOutputTooLarge {
                        size: rendered_bytes,
                        max: max_output_size,
                        span: None,
                    });
                }
            }
        }
        // Grains the budget dropped, after the ones that survived.
        if let Some(omit) = &sections.element_omit {
            for grain in source.omitted {
                render_one(omit, Some((grain, emitted)), &env, &mut pieces, &mut rendered_bytes)?;
                emitted += 1;
                if rendered_bytes > max_output_size {
                    return Err(CalError::RenderOutputTooLarge {
                        size: rendered_bytes,
                        max: max_output_size,
                        span: None,
                    });
                }
            }
        }
    }

    if let Some(footer) = &sections.footer {
        render_one(footer, None, &outer, &mut pieces, &mut rendered_bytes)?;
    }

    let output = pieces.join("\n");
    if output.len() > max_output_size {
        return Err(CalError::RenderOutputTooLarge {
            size: output.len(),
            max: max_output_size,
            span: None,
        });
    }
    Ok(output)
}

/// Recursively render template nodes into the output string.
fn render_nodes(
    nodes: &[TemplateNode],
    grains: &[CalGrainResult],
    current_grain: Option<(&CalGrainResult, usize)>,
    ctx: &RenderContext,
    env: &RenderEnv<'_>,
    output: &mut String,
    max_output_size: usize,
) -> CalResult<()> {
    for node in nodes {
        match node {
            TemplateNode::Text(s) => output.push_str(s),

            TemplateNode::Variable { name, filters } => {
                let grain = current_grain.map(|(g, _)| g);
                let index = current_grain.map(|(_, i)| i);
                let mut value = resolve_variable(name, grain, index, ctx, env);
                for filter in filters {
                    value = apply_filter(&value, filter, ctx)?;
                }
                output.push_str(&value.to_display());
            }

            TemplateNode::Each { body, .. } => {
                // §10.8 caps iteration at 200. `{{#each grains}}` is the
                // pre-§10.6 whole-result form; a sectioned template has the
                // engine drive iteration and is not bounded by this.
                for (i, grain) in grains.iter().take(MAX_EACH_ITERATIONS).enumerate() {
                    render_nodes(body, grains, Some((grain, i)), ctx, env, output, max_output_size)?;
                    // F1 safety: check output size after each grain iteration.
                    if output.len() > max_output_size {
                        return Err(CalError::RenderOutputTooLarge {
                            size: output.len(),
                            max: max_output_size,
                            span: None,
                        });
                    }
                }
            }

            TemplateNode::Condition {
                field,
                if_body,
                else_body,
            } => {
                let grain = current_grain.map(|(g, _)| g);
                let index = current_grain.map(|(_, i)| i);
                let value = resolve_variable(field, grain, index, ctx, env);
                if value.is_truthy() {
                    render_nodes(if_body, grains, current_grain, ctx, env, output, max_output_size)?;
                } else if !else_body.is_empty() {
                    render_nodes(
                        else_body,
                        grains,
                        current_grain,
                        ctx,
                        env,
                        output,
                        max_output_size,
                    )?;
                }
            }

            TemplateNode::InvertedSection { field, body } => {
                let grain = current_grain.map(|(g, _)| g);
                let index = current_grain.map(|(_, i)| i);
                let value = resolve_variable(field, grain, index, ctx, env);
                if !value.is_truthy() {
                    render_nodes(body, grains, current_grain, ctx, env, output, max_output_size)?;
                }
            }

            TemplateNode::GrainTypeBlock { grain_type, body } => {
                if let Some((grain, _)) = current_grain {
                    if grain.grain_type == *grain_type {
                        render_nodes(body, grains, current_grain, ctx, env, output, max_output_size)?;
                    }
                }
            }

            TemplateNode::Comment(_) => { /* skip */ }
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Default grain rendering (fallback)
// ---------------------------------------------------------------------------

/// Helpers for extracting typed fields from grain.fields JSON.
fn field_str(grain: &CalGrainResult, name: &str) -> String {
    match grain.fields.get(name) {
        Some(serde_json::Value::String(s)) => s.clone(),
        Some(v) => v.to_string(),
        None => String::new(),
    }
}

fn field_str_or(grain: &CalGrainResult, name: &str, default: &str) -> String {
    let s = field_str(grain, name);
    if s.is_empty() {
        default.to_string()
    } else {
        s
    }
}

fn field_f64(grain: &CalGrainResult, name: &str) -> f64 {
    match grain.fields.get(name) {
        Some(serde_json::Value::Number(n)) => n.as_f64().unwrap_or(0.0),
        _ => 0.0,
    }
}

fn field_i64(grain: &CalGrainResult, name: &str) -> i64 {
    match grain.fields.get(name) {
        Some(serde_json::Value::Number(n)) => n.as_i64().unwrap_or(0),
        _ => 0,
    }
}

fn field_bool(grain: &CalGrainResult, name: &str) -> bool {
    match grain.fields.get(name) {
        Some(serde_json::Value::Bool(b)) => *b,
        _ => false,
    }
}

fn format_confidence(conf: f64) -> String {
    format!("{}%", (conf * 100.0) as u32)
}

/// Default one-line rendering per grain type (used when no grain_type block
/// matches or when the template has no grain_type blocks).
pub fn default_grain_render(grain: &CalGrainResult, ctx: &RenderContext) -> String {
    match grain.grain_type.as_str() {
        "fact" => format!(
            "{} {} {} ({})",
            field_str(grain, "subject"),
            super::humanize::humanize_relation(&field_str(grain, "relation")),
            field_str(grain, "object"),
            format_confidence(field_f64(grain, "confidence")),
        ),
        "event" => format!(
            "{} {} [{}]",
            field_str(grain, "actor"),
            field_str(grain, "action"),
            super::humanize::humanize_time(field_i64(grain, "created_at"), ctx.now_secs),
        ),
        "state" => format!(
            "{}: {} ({})",
            field_str(grain, "entity"),
            field_str(grain, "state_value"),
            field_str_or(grain, "validity", "current"),
        ),
        "workflow" => {
            let node_count = grain
                .fields
                .get("nodes")
                .and_then(|v: &serde_json::Value| v.as_array())
                .map(|a: &Vec<serde_json::Value>| a.len())
                .unwrap_or(0);
            let edge_count = grain
                .fields
                .get("edges")
                .and_then(|v: &serde_json::Value| v.as_array())
                .map(|a: &Vec<serde_json::Value>| a.len())
                .unwrap_or(0);
            format!(
                "{} ({} nodes, {} edges, {})",
                field_str(grain, "name"),
                node_count,
                edge_count,
                field_str(grain, "status"),
            )
        }
        "tool" => format!(
            "{}({}) -> {}{}",
            field_str(grain, "tool_name"),
            truncate_str(&field_str(grain, "input"), 40),
            truncate_str(&field_str(grain, "content"), 60),
            if field_bool(grain, "is_error") {
                " [ERROR]"
            } else {
                ""
            },
        ),
        "observation" => format!(
            "{} observed {}: {}",
            field_str(grain, "observer"),
            field_str(grain, "observed"),
            truncate_str(&field_str(grain, "sensory_data"), 60),
        ),
        "goal" => format!(
            "{} - {} ({})",
            field_str(grain, "goal_state"),
            field_str_or(grain, "assigned_to", "unassigned"),
            field_str_or(grain, "priority", "normal"),
        ),
        "reasoning" => format!(
            "{} -> {} ({})",
            truncate_str(&field_str(grain, "premises"), 60),
            field_str(grain, "conclusion"),
            format_confidence(field_f64(grain, "confidence")),
        ),
        "consensus" => format!(
            "{}: {}",
            field_str(grain, "participants"),
            field_str(grain, "agreement_level"),
        ),
        "consent" => format!(
            "{}: {} [{}]",
            field_str(grain, "consenter"),
            field_str(grain, "scope"),
            field_str_or(grain, "expires_at", "no expiry"),
        ),
        // Skill — data projection only (name + description + domain +
        // proficiency). `instructions`/`when_to_use` are deliberately NOT
        // interpolated raw into the template (design §13 non-blocking note).
        "skill" => format!(
            "{}: {} [{}] ({})",
            field_str(grain, "name"),
            truncate_str(&field_str(grain, "description"), 60),
            field_str_or(grain, "domain", "general"),
            format_confidence(field_f64(grain, "proficiency")),
        ),
        // Recommendation — the reviewable line: severity, target, and the
        // change kind, so a queue reads at a glance.
        "recommendation" => format!(
            "[{}] {} -> {}",
            field_str_or(grain, "severity", "info"),
            truncate_str(&field_str(grain, "dedup_key"), 12),
            field_str(grain, "target_ref"),
        ),
        _ => format!("{}: {}", grain.grain_type, grain.hash),
    }
}

// ---------------------------------------------------------------------------
// Template registry
// ---------------------------------------------------------------------------

/// A registered template entry.
#[derive(Debug, Clone)]
pub struct TemplateEntry {
    /// The parsed template AST.
    pub template: Template,
    /// Whether this is a built-in (non-deletable) template.
    pub builtin: bool,
    /// Optional parent template name (for inheritance, 1-level only).
    pub parent: Option<String>,
    /// Human-readable description.
    pub description: String,
    /// Supported grain types (empty = all).
    pub grain_types: Vec<String>,
    /// Last time this template was used in a FORMAT preset render (epoch secs).
    pub last_run_at: Option<u64>,
    /// Last time this template was created or updated (epoch secs).
    pub updated_at: Option<u64>,
}

/// Summary of a registered template (for listing).
#[derive(Debug, Clone, serde::Serialize)]
pub struct TemplateListEntry {
    pub name: String,
    pub description: String,
    pub builtin: bool,
    pub parent: Option<String>,
    pub grain_types: Vec<String>,
    /// Original template source text (for preset rendering).
    pub source: String,
    pub last_run_at: Option<u64>,
    pub updated_at: Option<u64>,
}

/// Detailed template info (for `get_template` responses).
#[derive(Debug, Clone, serde::Serialize)]
pub struct TemplateInfo {
    pub name: String,
    pub description: String,
    pub builtin: bool,
    pub parent: Option<String>,
    pub source: String,
}

/// Serialization format for persisting custom templates to Fjall meta partition (FR-003).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PersistedTemplate {
    pub source: String,
    pub description: String,
    pub parent: Option<String>,
    pub grain_types: Vec<String>,
    #[serde(default)]
    pub last_run_at: Option<u64>,
    #[serde(default)]
    pub updated_at: Option<u64>,
}

/// In-memory template registry.
///
/// Thread-safe when wrapped in `Arc<RwLock<...>>` externally. The
/// `CalExecutor` holds this and passes it by reference to the renderer.
#[derive(Debug, Clone)]
pub struct TemplateRegistry {
    templates: HashMap<String, TemplateEntry>,
}

impl Default for TemplateRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl TemplateRegistry {
    /// Create a new registry pre-loaded with the 6 built-in templates.
    pub fn new() -> Self {
        let mut reg = Self {
            templates: HashMap::new(),
        };
        reg.load_builtins();
        reg
    }

    /// Register a custom template. Returns error if name is invalid or
    /// a built-in template would be overwritten.
    pub fn register(
        &mut self,
        name: &str,
        source: &str,
        description: &str,
        parent: Option<&str>,
    ) -> CalResult<()> {
        // 1. Validate name: ^[a-zA-Z][a-zA-Z0-9 _-]{0,63}$
        if !is_valid_template_name(name) {
            return Err(CalError::TemplateInvalidName {
                name: name.to_string(),
                span: None,
            });
        }
        // 2. Cannot overwrite built-in.
        if self.templates.get(name).is_some_and(|e| e.builtin) {
            return Err(CalError::TemplateBuiltinImmutable {
                name: name.to_string(),
                span: None,
            });
        }
        // 3. Validate parent exists (if specified).
        if let Some(p) = parent {
            // §10.7: `data` emits structural JSON, not template text.
            if p.eq_ignore_ascii_case("data") {
                return Err(CalError::CannotExtendData {
                    name: name.to_string(),
                    span: None,
                });
            }
            if !self.templates.contains_key(p) {
                return Err(CalError::TemplateParentNotFound {
                    name: name.to_string(),
                    parent: p.to_string(),
                    span: None,
                });
            }
            // Prevent 2-level inheritance.
            if self.templates[p].parent.is_some() {
                return Err(CalError::TemplateInheritanceDepth {
                    name: name.to_string(),
                    span: None,
                });
            }
        }
        // 4. §10.8 caps custom templates per namespace. Redefining an
        //    existing one is always allowed — it does not grow the set.
        if !self.templates.contains_key(name) {
            let custom = self.templates.values().filter(|e| !e.builtin).count();
            if custom >= MAX_TEMPLATES {
                return Err(CalError::TooManyTemplates {
                    count: custom,
                    max: MAX_TEMPLATES,
                    span: None,
                });
            }
        }

        // 5. Parse and validate. `parse_template_any` recovers the §10.6
        //    sectioned form, so a template reloaded from a file comes back in
        //    the form it was defined in.
        let template = parse_template_any(source)?;
        // 6. Store.
        self.templates.insert(
            name.to_string(),
            TemplateEntry {
                template,
                builtin: false,
                parent: parent.map(|s| s.to_string()),
                description: description.to_string(),
                grain_types: Vec::new(),
                last_run_at: None,
                updated_at: Some(
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_secs(),
                ),
            },
        );
        Ok(())
    }

    /// Get a template by name.
    pub fn get(&self, name: &str) -> Option<&TemplateEntry> {
        self.templates.get(name)
    }

    /// Record the grain types a template's `FOR` clause narrowed it to.
    ///
    /// Separate from [`TemplateRegistry::register`] because the `FOR` clause is
    /// carried by the statement, not by the template body — but it still has to
    /// survive a round-trip through the file, or a reloaded template quietly
    /// loses what the user wrote.
    pub fn set_grain_types(&mut self, name: &str, grain_types: &[String]) {
        if let Some(entry) = self.templates.get_mut(name) {
            entry.grain_types = grain_types.to_vec();
        }
    }

    /// List all templates (name + description + builtin flag).
    pub fn list(&self) -> Vec<TemplateListEntry> {
        let mut entries: Vec<TemplateListEntry> = self
            .templates
            .iter()
            .map(|(name, entry)| TemplateListEntry {
                name: name.clone(),
                description: entry.description.clone(),
                builtin: entry.builtin,
                parent: entry.parent.clone(),
                grain_types: entry.grain_types.clone(),
                source: entry.template.source().to_string(),
                last_run_at: entry.last_run_at,
                updated_at: entry.updated_at,
            })
            .collect();
        entries.sort_by_key(|b| std::cmp::Reverse(b.updated_at.unwrap_or(0)));
        entries
    }

    /// Restore timestamps from persisted data (used during rehydration).
    pub fn restore_timestamps(
        &mut self,
        name: &str,
        last_run_at: Option<u64>,
        updated_at: Option<u64>,
    ) {
        if let Some(entry) = self.templates.get_mut(name) {
            if last_run_at.is_some() {
                entry.last_run_at = last_run_at;
            }
            if updated_at.is_some() {
                entry.updated_at = updated_at;
            }
        }
    }

    /// Record that a template was used (updates last_run_at).
    pub fn record_run(&mut self, name: &str) {
        if let Some(entry) = self.templates.get_mut(name) {
            entry.last_run_at = Some(
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs(),
            );
        }
    }

    /// Delete a custom template. Returns error for built-in templates.
    pub fn delete(&mut self, name: &str) -> CalResult<()> {
        match self.templates.get(name) {
            None => Err(CalError::TemplateNotFound {
                name: name.to_string(),
                span: None,
            }),
            Some(entry) if entry.builtin => Err(CalError::TemplateBuiltinImmutable {
                name: name.to_string(),
                span: None,
            }),
            _ => {
                self.templates.remove(name);
                Ok(())
            }
        }
    }

    /// Load the built-in templates: the §10.1 semantic presets (`structured`,
    /// `readable`, `compact`). They are sectioned, exist so `EXTENDS` has
    /// something to inherit from, and deliberately define only element-level
    /// sections — a template extending them must supply its own HEADER/FOOTER,
    /// and `readable` being the default parent must not silently wrap every
    /// template in one. `data` is intentionally absent: §10.7 forbids
    /// extending it. A builtin must never take the name of a `FORMAT` arm
    /// (`sml`, `toon`, …): `FORMAT <name>` would hit the arm while
    /// `FORMAT TEMPLATE <name>` hit the template, two different outputs under
    /// one name — `insert_builtin_sectioned` enforces this.
    fn load_builtins(&mut self) {
        self.insert_builtin_sectioned(
            "structured",
            "ELEMENT {\n\
             <{{grain.type}} subject=\"{{grain.subject}}\"{{#grain.confidence}} \
             confidence=\"{{grain.confidence}}\"{{/grain.confidence}}>\
             {{grain.content}}</{{grain.type}}>\n\
             }\n\
             ELEMENT_SUMMARY {\n\
             <{{grain.type}} subject=\"{{grain.subject}}\">{{grain.content}}</{{grain.type}}>\n\
             }\n",
            "OMS §10.1 structured/sml preset — one SML element per grain",
        );
        self.insert_builtin_sectioned(
            "readable",
            "ELEMENT {\n\
             - **{{grain.subject}}** {{grain.content}}{{#grain.confidence}} \
             _({{grain.confidence | percent}})_{{/grain.confidence}}\n\
             }\n\
             ELEMENT_SUMMARY {\n\
             - {{grain.subject}} {{grain.content}}\n\
             }\n",
            "OMS §10.1 readable/markdown preset — the default parent",
        );
        self.insert_builtin_sectioned(
            "compact",
            "ELEMENT {\n\
             {{grain.subject}} {{grain.content}}\n\
             }\n\
             ELEMENT_SUMMARY {\n\
             {{grain.content}}\n\
             }\n",
            "OMS §10.1 compact/text preset — minimal, token-efficient",
        );
    }

    /// Register a §10.6 sectioned built-in from its canonical section text.
    /// Panics only in debug if the template is malformed (they are
    /// compile-time constants and always valid).
    fn insert_builtin_sectioned(&mut self, name: &str, source: &str, description: &str) {
        debug_assert!(
            !FORMAT_ARM_NAMES.contains(&name),
            "built-in template \"{name}\" would shadow the FORMAT {name} arm"
        );
        let template = match parse_template_any(source) {
            Ok(t) => t,
            Err(e) => {
                #[cfg(debug_assertions)]
                panic!("built-in template \"{}\" failed to parse: {}", name, e);
                #[cfg(not(debug_assertions))]
                {
                    let _ = e;
                    return;
                }
            }
        };
        debug_assert!(
            template.is_sectioned(),
            "built-in preset \"{name}\" must parse as sectioned"
        );
        self.templates.insert(
            name.to_string(),
            TemplateEntry {
                template,
                builtin: true,
                parent: None,
                description: description.to_string(),
                grain_types: Vec::new(),
                last_run_at: None,
                updated_at: None,
            },
        );
    }

}

// ---------------------------------------------------------------------------
// Template inheritance
// ---------------------------------------------------------------------------

/// Resolve template inheritance (1-level max). Returns the effective template
/// after merging parent and child GrainTypeBlocks.
pub fn resolve_inheritance(entry: &TemplateEntry, registry: &TemplateRegistry) -> Template {
    // §10.7: the default parent is `readable`. It applies only to sectioned
    // templates — a whole-result body has no sections to inherit into — and
    // `readable` deliberately defines no HEADER/FOOTER, so the effect is to
    // supply the ELEMENT_SUMMARY the engine needs under budget pressure, not
    // to wrap every template in a header.
    let default_parent = entry.parent.is_none()
        && entry.template.is_sectioned()
        && !entry.builtin;
    if default_parent {
        if let Some(readable) = registry.get("readable") {
            return merge_templates(&readable.template, &entry.template);
        }
    }
    let Some(ref parent_name) = entry.parent else {
        return entry.template.clone();
    };
    let Some(parent_entry) = registry.get(parent_name) else {
        // Should not happen (validated at registration time).
        return entry.template.clone();
    };

    merge_templates(&parent_entry.template, &entry.template)
}

/// Merge a parent template with a child template.
///
/// Strategy:
/// 1. Start with parent's nodes.
/// 2. For each GrainTypeBlock in child, replace matching block in parent.
/// 3. New GrainTypeBlocks from child are appended.
pub fn merge_templates(parent: &Template, child: &Template) -> Template {
    // Sectioned inheritance is per section (§10.7): the child keeps every
    // section it defines and takes the rest from the parent. Only meaningful
    // when both are sectioned — a sectioned child of a whole-result parent
    // has no sections to inherit, so it stands alone.
    if let Some(child_sections) = child.sections() {
        let mut merged = child_sections.clone();
        if let Some(parent_sections) = parent.sections() {
            merged.inherit_from(parent_sections);
        }
        return Template::from_sections(child.source.clone(), merged);
    }

    let mut merged_nodes = parent.nodes.clone();

    for child_node in &child.nodes {
        if let TemplateNode::GrainTypeBlock { grain_type, body } = child_node {
            let mut replaced = false;
            for node in merged_nodes.iter_mut() {
                match node {
                    TemplateNode::GrainTypeBlock {
                        grain_type: gt,
                        body: parent_body,
                    } if gt == grain_type => {
                        *parent_body = body.clone();
                        replaced = true;
                        break;
                    }
                    // Also check inside Each blocks.
                    TemplateNode::Each {
                        body: each_body, ..
                    } => {
                        for inner in each_body.iter_mut() {
                            if let TemplateNode::GrainTypeBlock {
                                grain_type: gt,
                                body: parent_body,
                            } = inner
                            {
                                if gt == grain_type {
                                    *parent_body = body.clone();
                                    replaced = true;
                                    break;
                                }
                            }
                        }
                        if replaced {
                            break;
                        }
                    }
                    _ => {}
                }
            }
            if !replaced {
                merged_nodes.push(child_node.clone());
            }
        }
    }

    Template {
        source: child.source.clone(),
        nodes: merged_nodes,
        sections: None,
    }
}

// ---------------------------------------------------------------------------
// apply_format (for executor integration)
// ---------------------------------------------------------------------------

/// Apply FORMAT spec to a grain result payload. This function is designed
/// to be called by the executor after `execute_statement()` and
/// `apply_pipeline()`. It transforms grain payloads into formatted strings.
///
/// Note: This returns the rendered content as a String. The executor is
/// responsible for wrapping it into the appropriate CalResultPayload variant.
pub fn apply_format(
    grains: &[CalGrainResult],
    template: &Template,
    ctx: &RenderContext,
) -> CalResult<String> {
    render(template, grains, ctx)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Helper: create a test CalGrainResult.
    fn make_fact(subject: &str, relation: &str, object: &str, confidence: f64) -> CalGrainResult {
        CalGrainResult {
            hash: "abc123".to_string(),
            grain_type: "fact".to_string(),
            score: 0.95,
            fields: json!({
                "subject": subject,
                "relation": relation,
                "object": object,
                "confidence": confidence,
                "created_at": 1700000000_i64,
                "namespace": "default",
                "user_id": "user1",
            }),
            score_breakdown: None,
            explanation: None,
            relative_time: None,
            is_deterministic: false,
            contested_by: None,
        }
    }

    fn make_tool(tool: &str, input: &str, content: &str, is_error: bool) -> CalGrainResult {
        CalGrainResult {
            hash: "def456".to_string(),
            grain_type: "tool".to_string(),
            score: 0.90,
            fields: json!({
                "tool_name": tool,
                "input": input,
                "content": content,
                "is_error": is_error,
                "created_at": 1700000000_i64,
                "duration_ms": 150,
            }),
            score_breakdown: None,
            explanation: None,
            relative_time: None,
            is_deterministic: false,
            contested_by: None,
        }
    }

    fn make_event(actor: &str, action: &str) -> CalGrainResult {
        CalGrainResult {
            hash: "ghi789".to_string(),
            grain_type: "event".to_string(),
            score: 0.85,
            fields: json!({
                "actor": actor,
                "action": action,
                "created_at": 1700000000_i64,
            }),
            score_breakdown: None,
            explanation: None,
            relative_time: None,
            is_deterministic: false,
            contested_by: None,
        }
    }

    fn make_goal(title: &str, goal_state: &str) -> CalGrainResult {
        CalGrainResult {
            hash: "jkl012".to_string(),
            grain_type: "goal".to_string(),
            score: 0.80,
            fields: json!({
                "title": title,
                "goal_state": goal_state,
                "priority": "high",
                "assigned_to": "john",
            }),
            score_breakdown: None,
            explanation: None,
            relative_time: None,
            is_deterministic: false,
            contested_by: None,
        }
    }

    // ── CAL §10.3 / §10.5 spec conformance ──────────────────────────

    #[test]
    fn test_humanize_relation_per_spec() {
        // §10.3.3: strip namespace, underscores to spaces, custom kept as-is.
        assert_eq!(humanize_relation("mg:prefers"), "prefers");
        assert_eq!(humanize_relation("works_at"), "works at");
        assert_eq!(humanize_relation("acme:similar_to"), "similar to");
        assert_eq!(humanize_relation("knows"), "knows");
    }

    #[test]
    fn test_humanize_time_buckets_per_spec() {
        // §10.3.4 buckets, measured back from a fixed "now".
        let now = 1_000_000_000i64;
        let ago = |secs: i64| humanize_time((now - secs) * 1000, now);
        assert_eq!(ago(23 * 60), "23m ago");
        assert_eq!(ago(3 * 3600), "3h ago");
        assert_eq!(ago(86_400), "yesterday");
        assert_eq!(ago(2 * 86_400), "2d ago");
        assert_eq!(ago(14 * 86_400), "2w ago");
        assert_eq!(ago(45 * 86_400), "1mo ago");
        assert_eq!(ago(400 * 86_400), "1y ago");
    }

    /// Granularity may only coarsen with age. 29 days reading as "4w ago" and
    /// 31 days as "31d ago" is two scales for two neighbouring instants.
    #[test]
    fn test_humanize_time_granularity_only_coarsens() {
        let now = 1_000_000_000i64;
        let unit = |secs: i64| {
            let s = humanize_time((now - secs) * 1000, now);
            s.trim_end_matches(" ago")
                .trim_start_matches(|c: char| c.is_ascii_digit())
                .to_string()
        };
        // "yesterday" carries no unit suffix; every other bucket does.
        let order = ["m", "h", "d", "w", "mo", "y"];
        let rank = |u: &str| order.iter().position(|o| *o == u);
        let mut last = 0usize;
        for days in [0, 1, 3, 10, 29, 31, 200, 400, 1000] {
            let u = unit(days * 86_400 + 1);
            let Some(r) = rank(&u) else { continue };
            assert!(r >= last, "granularity went backwards at {days}d: {u:?}");
            last = r;
        }
    }

    #[test]
    fn test_content_projection_per_grain_type() {
        // §10.3.2: each grain type has its own text-content rule.
        let mk = |ty: &str, fields: serde_json::Value| CalGrainResult {
            hash: "h".into(),
            grain_type: ty.into(),
            fields,
            score: 0.0,
            is_deterministic: true,
            contested_by: None,
            explanation: None,
            score_breakdown: None,
            relative_time: None,
        };
        let content = |g: &CalGrainResult| match project_content(g) {
            ResolvedValue::Str(s) => s,
            _ => String::new(),
        };
        // Fact: humanize(relation) + " " + object
        assert_eq!(
            content(&mk("fact", serde_json::json!({"relation": "works_at", "object": "acme"}))),
            "works at acme"
        );
        // Event: content
        assert_eq!(
            content(&mk("event", serde_json::json!({"content": "asked about pricing"}))),
            "asked about pricing"
        );
        // Reasoning: conclusion, not content
        assert_eq!(
            content(&mk("reasoning", serde_json::json!({"conclusion": "prioritise reliability"}))),
            "prioritise reliability"
        );
        // Consent: purpose
        assert_eq!(
            content(&mk("consent", serde_json::json!({"purpose": "access dashboards"}))),
            "access dashboards"
        );
        // Workflow: nodes joined readably
        assert_eq!(
            content(&mk("workflow", serde_json::json!({"nodes": ["a", "b", "c"]}))),
            "a -> b -> c"
        );
    }

    /// The variable set stays closed — a `grain.` prefix is not a wildcard.
    #[test]
    fn test_grain_namespace_is_still_a_closed_set() {
        assert!(validate_variable_name("grain.subject", None).is_ok());
        assert!(validate_variable_name("grain.content", None).is_ok());
        assert!(validate_variable_name("grain.relative_time", None).is_ok());
        assert!(validate_variable_name("disclosure.level", None).is_ok());
        assert!(validate_variable_name("grain.not_a_field", None).is_err());
        assert!(validate_variable_name("nonsense.thing", None).is_err());
    }

    fn test_ctx() -> RenderContext {
        RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        }
    }

    // -----------------------------------------------------------------------
    // Template name validation
    // -----------------------------------------------------------------------

    #[test]
    fn test_valid_template_names() {
        assert!(is_valid_template_name("triples"));
        assert!(is_valid_template_name("a"));
        assert!(is_valid_template_name("my_template"));
        assert!(is_valid_template_name("template123"));
        assert!(is_valid_template_name("a_b_c_d"));
        assert!(is_valid_template_name("HasUpperCase"));
        assert!(is_valid_template_name("has space"));
        assert!(is_valid_template_name("has-hyphen"));
        assert!(is_valid_template_name("Customer Support Context"));
        assert!(is_valid_template_name("My Template-1"));
        // 64 characters (ok)
        assert!(is_valid_template_name(&"a".repeat(64)));
    }

    #[test]
    fn test_invalid_template_names() {
        assert!(!is_valid_template_name(""));
        assert!(!is_valid_template_name("1starts_with_digit"));
        assert!(!is_valid_template_name("_starts_with_underscore"));
        assert!(!is_valid_template_name("-starts-with-hyphen"));
        assert!(!is_valid_template_name("has.dot"));
        assert!(!is_valid_template_name("trailing space "));
        assert!(!is_valid_template_name(" leading space"));
        assert!(!is_valid_template_name("double  space"));
        // 65 characters (too long)
        assert!(!is_valid_template_name(&"a".repeat(65)));
    }

    // -----------------------------------------------------------------------
    // Template parsing
    // -----------------------------------------------------------------------

    #[test]
    fn test_parse_simple_text() {
        let t = parse_template("Hello, world!").unwrap();
        assert_eq!(t.nodes().len(), 1);
        assert_eq!(t.nodes()[0], TemplateNode::Text("Hello, world!".into()));
    }

    #[test]
    fn test_parse_variable() {
        let t = parse_template("{{subject}}").unwrap();
        assert_eq!(t.nodes().len(), 1);
        match &t.nodes()[0] {
            TemplateNode::Variable { name, filters } => {
                assert_eq!(name, "subject");
                assert!(filters.is_empty());
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_parse_variable_with_filter() {
        let t = parse_template("{{subject | uppercase}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Variable { name, filters } => {
                assert_eq!(name, "subject");
                assert_eq!(filters.len(), 1);
                assert_eq!(filters[0].name, "uppercase");
                assert!(filters[0].arg.is_none());
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_parse_variable_with_filter_arg() {
        let t = parse_template("{{content | truncate 80}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Variable { name, filters } => {
                assert_eq!(name, "content");
                assert_eq!(filters.len(), 1);
                assert_eq!(filters[0].name, "truncate");
                assert_eq!(filters[0].arg, Some("80".to_string()));
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_parse_multiple_filters() {
        let t = parse_template("{{relation | humanize | uppercase}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Variable { name, filters } => {
                assert_eq!(name, "relation");
                assert_eq!(filters.len(), 2);
                assert_eq!(filters[0].name, "humanize");
                assert_eq!(filters[1].name, "uppercase");
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_parse_each_block() {
        let t = parse_template("{{#each grains}}{{subject}}{{/each}}").unwrap();
        assert_eq!(t.nodes().len(), 1);
        match &t.nodes()[0] {
            TemplateNode::Each { collection, body } => {
                assert_eq!(collection, "grains");
                assert_eq!(body.len(), 1);
            }
            _ => panic!("expected Each"),
        }
    }

    #[test]
    fn test_parse_condition() {
        let t = parse_template("{{#if confidence}}yes{{/if}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Condition {
                field,
                if_body,
                else_body,
            } => {
                assert_eq!(field, "confidence");
                assert_eq!(if_body.len(), 1);
                assert!(else_body.is_empty());
            }
            _ => panic!("expected Condition"),
        }
    }

    #[test]
    fn test_parse_condition_with_else() {
        let t = parse_template("{{#if confidence}}yes{{else}}no{{/if}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Condition {
                field,
                if_body,
                else_body,
            } => {
                assert_eq!(field, "confidence");
                assert_eq!(if_body.len(), 1);
                assert_eq!(else_body.len(), 1);
            }
            _ => panic!("expected Condition"),
        }
    }

    #[test]
    fn test_parse_inverted_section() {
        let t = parse_template("{{^summary}}no summary{{/summary}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::InvertedSection { field, body } => {
                assert_eq!(field, "summary");
                assert_eq!(body.len(), 1);
            }
            _ => panic!("expected InvertedSection"),
        }
    }

    #[test]
    fn test_parse_grain_type_block() {
        let t = parse_template("{{#grain_type \"fact\"}}{{subject}}{{/grain_type}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::GrainTypeBlock { grain_type, body } => {
                assert_eq!(grain_type, "fact");
                assert_eq!(body.len(), 1);
            }
            _ => panic!("expected GrainTypeBlock"),
        }
    }

    #[test]
    fn test_parse_comment() {
        let t = parse_template("{{! this is a comment }}hello").unwrap();
        assert_eq!(t.nodes().len(), 2);
        match &t.nodes()[0] {
            TemplateNode::Comment(text) => assert_eq!(text, "this is a comment"),
            _ => panic!("expected Comment"),
        }
    }

    #[test]
    fn test_parse_nested_each_rejected() {
        let result = parse_template("{{#each grains}}{{#each grains}}{{/each}}{{/each}}");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E041");
    }

    #[test]
    fn test_parse_unknown_variable_rejected() {
        let result = parse_template("{{unknown_variable}}");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E042");
    }

    #[test]
    fn test_parse_unknown_filter_rejected() {
        let result = parse_template("{{subject | bogus_filter}}");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E043");
    }

    #[test]
    fn test_parse_template_too_large() {
        let huge = "x".repeat(MAX_TEMPLATE_SIZE + 1);
        let result = parse_template(&huge);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E040");
    }

    #[test]
    fn test_parse_truncate_out_of_range() {
        let result = parse_template("{{subject | truncate 0}}");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E049");

        let result2 = parse_template("{{subject | truncate 200000}}");
        assert!(result2.is_err());
    }

    #[test]
    fn test_parse_unclosed_tag() {
        let result = parse_template("{{subject");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E049");
    }

    #[test]
    fn test_parse_unclosed_section() {
        let result = parse_template("{{#if confidence}}yes");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E049");
    }

    // -----------------------------------------------------------------------
    // ResolvedValue
    // -----------------------------------------------------------------------

    #[test]
    fn test_resolved_value_truthiness() {
        assert!(ResolvedValue::Str("hello".into()).is_truthy());
        assert!(!ResolvedValue::Str("".into()).is_truthy());
        assert!(ResolvedValue::Number(1.0).is_truthy());
        assert!(!ResolvedValue::Number(0.0).is_truthy());
        assert!(ResolvedValue::Integer(1).is_truthy());
        assert!(!ResolvedValue::Integer(0).is_truthy());
        assert!(ResolvedValue::Bool(true).is_truthy());
        assert!(!ResolvedValue::Bool(false).is_truthy());
        assert!(ResolvedValue::Array(vec!["a".into()]).is_truthy());
        assert!(!ResolvedValue::Array(vec![]).is_truthy());
        assert!(!ResolvedValue::Null.is_truthy());
    }

    #[test]
    #[allow(clippy::approx_constant)]
    fn test_resolved_value_display() {
        assert_eq!(ResolvedValue::Str("hello".into()).to_display(), "hello");
        assert_eq!(ResolvedValue::Number(3.14).to_display(), "3.14");
        assert_eq!(ResolvedValue::Number(42.0).to_display(), "42");
        assert_eq!(ResolvedValue::Integer(42).to_display(), "42");
        assert_eq!(ResolvedValue::Bool(true).to_display(), "true");
        assert_eq!(
            ResolvedValue::Array(vec!["a".into(), "b".into()]).to_display(),
            "a, b"
        );
        assert_eq!(ResolvedValue::Null.to_display(), "");
    }

    // -----------------------------------------------------------------------
    // Variable resolution
    // -----------------------------------------------------------------------

    #[test]
    fn test_resolve_metadata_variables() {
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 5,
            user_vars: HashMap::new(),
        };
        let grain = make_fact("john", "mg:likes", "coffee", 0.94);

        assert_eq!(
            resolve_variable("_index", Some(&grain), Some(2), &ctx, &RenderEnv::default()).to_display(),
            "2"
        );
        assert_eq!(
            resolve_variable("_count", Some(&grain), Some(0), &ctx, &RenderEnv::default()).to_display(),
            "5"
        );
        assert!(resolve_variable("_first", Some(&grain), Some(0), &ctx, &RenderEnv::default()).is_truthy());
        assert!(!resolve_variable("_first", Some(&grain), Some(1), &ctx, &RenderEnv::default()).is_truthy());
        assert!(resolve_variable("_last", Some(&grain), Some(4), &ctx, &RenderEnv::default()).is_truthy());
        assert!(!resolve_variable("_last", Some(&grain), Some(3), &ctx, &RenderEnv::default()).is_truthy());
    }

    #[test]
    fn test_resolve_direct_fields() {
        let grain = make_fact("john", "mg:likes", "coffee", 0.94);
        let ctx = test_ctx();

        assert_eq!(
            resolve_variable("hash", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "abc123"
        );
        assert_eq!(
            resolve_variable("grain_type", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "fact"
        );
        assert_eq!(
            resolve_variable("score", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "0.95"
        );
    }

    #[test]
    fn test_resolve_grain_fields() {
        let grain = make_fact("john", "mg:likes", "coffee", 0.94);
        let ctx = test_ctx();

        assert_eq!(
            resolve_variable("subject", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "john"
        );
        assert_eq!(
            resolve_variable("relation", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "mg:likes"
        );
        assert_eq!(
            resolve_variable("object", Some(&grain), None, &ctx, &RenderEnv::default()).to_display(),
            "coffee"
        );
    }

    #[test]
    fn test_resolve_disallowed_field_returns_null() {
        let grain = make_fact("john", "mg:likes", "coffee", 0.94);
        let ctx = test_ctx();

        // A field not in ALLOWED_FIELDS should return Null.
        assert!(!resolve_variable("secret_internal_field", Some(&grain), None, &ctx, &RenderEnv::default()).is_truthy());
    }

    // -----------------------------------------------------------------------
    // Filter pipeline
    // -----------------------------------------------------------------------

    #[test]
    fn test_filter_truncate() {
        let ctx = test_ctx();
        let val = ResolvedValue::Str("Hello, world! This is a long string.".into());
        let filter = Filter {
            name: "truncate".into(),
            arg: Some("13".into()),
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "Hello, world!...");
    }

    #[test]
    fn test_filter_truncate_no_truncation_needed() {
        let ctx = test_ctx();
        let val = ResolvedValue::Str("short".into());
        let filter = Filter {
            name: "truncate".into(),
            arg: Some("80".into()),
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "short");
    }

    #[test]
    fn test_filter_relative() {
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let val = ResolvedValue::Integer(1700000000);
        let filter = Filter {
            name: "relative".into(),
            arg: None,
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "1m ago");
    }

    #[test]
    fn test_filter_humanize() {
        let ctx = test_ctx();
        let val = ResolvedValue::Str("mg:likes".into());
        let filter = Filter {
            name: "humanize".into(),
            arg: None,
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "likes");
    }

    #[test]
    fn test_filter_percent() {
        let ctx = test_ctx();
        let val = ResolvedValue::Number(0.94);
        let filter = Filter {
            name: "percent".into(),
            arg: None,
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "94%");
    }

    #[test]
    fn test_filter_uppercase_lowercase() {
        let ctx = test_ctx();
        let val = ResolvedValue::Str("Hello".into());

        let upper = apply_filter(
            &val,
            &Filter {
                name: "uppercase".into(),
                arg: None,
            },
            &ctx,
        )
        .unwrap();
        assert_eq!(upper.to_display(), "HELLO");

        let lower = apply_filter(
            &val,
            &Filter {
                name: "lowercase".into(),
                arg: None,
            },
            &ctx,
        )
        .unwrap();
        assert_eq!(lower.to_display(), "hello");
    }

    #[test]
    fn test_filter_default() {
        let ctx = test_ctx();

        let truthy = ResolvedValue::Str("value".into());
        let result = apply_filter(
            &truthy,
            &Filter {
                name: "default".into(),
                arg: Some("fallback".into()),
            },
            &ctx,
        )
        .unwrap();
        assert_eq!(result.to_display(), "value");

        let null = ResolvedValue::Null;
        let result = apply_filter(
            &null,
            &Filter {
                name: "default".into(),
                arg: Some("fallback".into()),
            },
            &ctx,
        )
        .unwrap();
        assert_eq!(result.to_display(), "fallback");
    }

    #[test]
    fn test_filter_join() {
        let ctx = test_ctx();
        let val = ResolvedValue::Array(vec!["a".into(), "b".into(), "c".into()]);
        let filter = Filter {
            name: "join".into(),
            arg: Some(", ".into()),
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "a, b, c");
    }

    #[test]
    fn test_filter_date() {
        let ctx = test_ctx();
        let val = ResolvedValue::Integer(1700000000);
        let filter = Filter {
            name: "date".into(),
            arg: Some("%Y-%m-%d".into()),
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "2023-11-14");
    }

    #[test]
    fn test_filter_date_negative_epoch() {
        let ctx = test_ctx();
        let val = ResolvedValue::Integer(-100);
        let filter = Filter {
            name: "date".into(),
            arg: None,
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "<invalid date>");
    }

    #[test]
    fn test_filter_json() {
        let ctx = test_ctx();
        let val = ResolvedValue::Str("hello".into());
        let filter = Filter {
            name: "json".into(),
            arg: None,
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "\"hello\"");

        let bool_val = ResolvedValue::Bool(true);
        let result = apply_filter(&bool_val, &filter, &ctx).unwrap();
        assert_eq!(result.to_display(), "true");
    }

    // -----------------------------------------------------------------------
    // format_epoch
    // -----------------------------------------------------------------------

    #[test]
    fn test_format_epoch_basic() {
        assert_eq!(format_epoch(0, "%Y-%m-%d"), "1970-01-01");
        assert_eq!(format_epoch(1700000000, "%Y-%m-%d"), "2023-11-14");
        assert_eq!(
            format_epoch(1700000000, "%Y-%m-%d %H:%M:%S"),
            "2023-11-14 22:13:20"
        );
    }

    #[test]
    fn test_format_epoch_negative() {
        assert_eq!(format_epoch(-1, "%Y-%m-%d"), "<invalid date>");
    }

    #[test]
    fn test_format_epoch_long_format() {
        let long_fmt = "%Y".repeat(200);
        assert_eq!(format_epoch(0, &long_fmt), "<format too long>");
    }

    // -----------------------------------------------------------------------
    // Template rendering
    // -----------------------------------------------------------------------

    #[test]
    fn test_render_simple_text() {
        let t = parse_template("Hello!").unwrap();
        let ctx = test_ctx();
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "Hello!");
    }

    #[test]
    fn test_render_variable() {
        let t = parse_template("{{#each grains}}{{subject}}{{/each}}").unwrap();
        let grains = vec![make_fact("john", "mg:likes", "coffee", 0.94)];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "john");
    }

    #[test]
    fn test_render_each_with_filter() {
        let t = parse_template(
            "{{#each grains}}{{subject}} {{relation | humanize}} {{object}}\n{{/each}}",
        )
        .unwrap();
        let grains = vec![
            make_fact("john", "mg:likes", "coffee", 0.94),
            make_fact("bob", "mg:knows", "john", 0.87),
        ];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "john likes coffee\nbob knows john\n");
    }

    #[test]
    fn test_render_condition_true() {
        let t = parse_template("{{#each grains}}{{#if confidence}}yes{{/if}}{{/each}}").unwrap();
        let grains = vec![make_fact("john", "mg:likes", "coffee", 0.94)];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "yes");
    }

    #[test]
    fn test_render_condition_false_with_else() {
        let t = parse_template(
            "{{#each grains}}{{#if summary}}has summary{{else}}no summary{{/if}}{{/each}}",
        )
        .unwrap();
        let grains = vec![make_fact("john", "mg:likes", "coffee", 0.94)];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "no summary");
    }

    #[test]
    fn test_render_inverted_section() {
        let t =
            parse_template("{{#each grains}}{{^summary}}no summary{{/summary}}{{/each}}").unwrap();
        let grains = vec![make_fact("john", "mg:likes", "coffee", 0.94)];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "no summary");
    }

    #[test]
    fn test_render_grain_type_block() {
        let t = parse_template(concat!(
            "{{#each grains}}",
            "{{#grain_type \"fact\"}}FACT: {{subject}}{{/grain_type}}",
            "{{#grain_type \"tool\"}}TOOL: {{tool_name}}{{/grain_type}}",
            "\n{{/each}}"
        ))
        .unwrap();
        let grains = vec![
            make_fact("john", "mg:likes", "coffee", 0.94),
            make_tool("search", "weather", "sunny", false),
        ];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "FACT: john\nTOOL: search\n");
    }

    #[test]
    fn test_render_metadata_variables() {
        let t = parse_template(
            "Count: {{_count}}, {{#each grains}}[{{_index}}]{{subject}}{{#if _last}} END{{/if}} {{/each}}"
        ).unwrap();
        let grains = vec![
            make_fact("john", "mg:likes", "coffee", 0.94),
            make_fact("bob", "mg:knows", "john", 0.87),
        ];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = render(&t, &grains, &ctx).unwrap();
        assert_eq!(result, "Count: 2, [0]john [1]bob END ");
    }

    #[test]
    fn test_render_output_size_limit() {
        let t = parse_template("{{#each grains}}{{subject}}{{/each}}").unwrap();
        let grains: Vec<CalGrainResult> = (0..1000)
            .map(|i| make_fact(&format!("subject_{}", i), "mg:knows", "object", 0.5))
            .collect();
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        // Set a tiny limit to trigger the safety check.
        let result = render_with_limit(&t, &grains, &ctx, 100);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E050");
    }

    // -----------------------------------------------------------------------
    // Default grain rendering
    // -----------------------------------------------------------------------

    #[test]
    fn test_default_grain_render_fact() {
        let grain = make_fact("john", "mg:likes", "coffee", 0.94);
        let ctx = test_ctx();
        let result = default_grain_render(&grain, &ctx);
        assert_eq!(result, "john likes coffee (94%)");
    }

    #[test]
    fn test_default_grain_render_tool() {
        let grain = make_tool("search", "weather", "sunny", false);
        let ctx = test_ctx();
        let result = default_grain_render(&grain, &ctx);
        assert_eq!(result, "search(weather) -> sunny");
    }

    #[test]
    fn test_default_grain_render_tool_error() {
        let grain = make_tool("search", "weather", "timeout", true);
        let ctx = test_ctx();
        let result = default_grain_render(&grain, &ctx);
        assert_eq!(result, "search(weather) -> timeout [ERROR]");
    }

    #[test]
    fn test_default_grain_render_event() {
        let grain = make_event("john", "logged in");
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result = default_grain_render(&grain, &ctx);
        assert_eq!(result, "john logged in [1m ago]");
    }

    #[test]
    fn test_default_grain_render_goal() {
        let grain = make_goal("Complete onboarding", "active");
        let ctx = test_ctx();
        let result = default_grain_render(&grain, &ctx);
        assert_eq!(result, "active - john (high)");
    }

    // -----------------------------------------------------------------------
    // Progressive disclosure tiers
    // -----------------------------------------------------------------------

    #[test]
    fn test_select_tier() {
        assert_eq!(select_tier(10000, 10), DisclosureTier::Full); // 1000 tokens/grain
        assert_eq!(select_tier(1000, 10), DisclosureTier::Headlines); // 100 tokens/grain
        assert_eq!(select_tier(100, 10), DisclosureTier::Summary); // 10 tokens/grain
        assert_eq!(select_tier(1000, 0), DisclosureTier::Full); // no grains
    }

    #[test]
    fn test_effective_truncate() {
        assert_eq!(effective_truncate(Some(100), DisclosureTier::Summary), 40);
        assert_eq!(effective_truncate(Some(100), DisclosureTier::Headlines), 80);
        assert_eq!(effective_truncate(Some(100), DisclosureTier::Full), 100);
        assert_eq!(effective_truncate(None, DisclosureTier::Summary), 40);
        assert_eq!(effective_truncate(None, DisclosureTier::Full), usize::MAX);
    }

    #[test]
    fn test_tier_affects_truncation() {
        let t = parse_template("{{#each grains}}{{content | truncate 100}}{{/each}}").unwrap();
        let grains = vec![{
            let mut g = make_fact("john", "mg:likes", "coffee", 0.94);
            g.fields
                .as_object_mut()
                .unwrap()
                .insert("content".into(), json!("x".repeat(200)));
            g
        }];

        // Full tier: truncate at 100
        let ctx_full = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result_full = render(&t, &grains, &ctx_full).unwrap();
        assert!(result_full.len() <= 103 + 3); // 100 chars + "..."

        // Summary tier: truncate at 40
        let ctx_summary = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Summary,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result_summary = render(&t, &grains, &ctx_summary).unwrap();
        assert!(result_summary.len() <= 43 + 3); // 40 chars + "..."
        assert!(result_summary.len() < result_full.len());
    }

    // -----------------------------------------------------------------------
    // Template registry
    // -----------------------------------------------------------------------

    #[test]
    fn test_registry_has_builtins() {
        let reg = TemplateRegistry::new();
        // Exactly the §10.1 semantic presets — no other builtins, and never
        // one named after a FORMAT arm (`toon`, `triples`, … once shadowed
        // the arms with different output).
        assert!(reg.get("structured").is_some());
        assert!(reg.get("readable").is_some());
        assert!(reg.get("compact").is_some());
        assert!(reg.get("readable").unwrap().builtin);
        assert_eq!(reg.list().len(), 3);
        for name in super::FORMAT_ARM_NAMES {
            assert!(reg.get(name).is_none(), "builtin shadows FORMAT {name}");
        }
    }

    // ── OMS CAL §10.8 limits ──────────────────────────────────────────

    #[test]
    fn test_limit_template_body_size() {
        assert_eq!(MAX_TEMPLATE_SIZE, 4_096, "§10.8 max template body size");
        let ok = "x".repeat(MAX_TEMPLATE_SIZE);
        assert!(parse_template(&ok).is_ok());
        let too_big = "x".repeat(MAX_TEMPLATE_SIZE + 1);
        assert!(matches!(
            parse_template(&too_big),
            Err(CalError::TemplateTooLarge { .. })
        ));
    }

    #[test]
    fn test_limit_conditional_nesting_depth() {
        // Nesting is bounded at 5; this also caps parser recursion.
        let nest = |n: usize| {
            let mut t = String::new();
            for _ in 0..n {
                t.push_str("{{#grain.confidence}}");
            }
            t.push('x');
            for _ in 0..n {
                t.push_str("{{/grain.confidence}}");
            }
            t
        };
        assert!(parse_template(&nest(MAX_TEMPLATE_NESTING)).is_ok());
        assert!(matches!(
            parse_template(&nest(MAX_TEMPLATE_NESTING + 2)),
            Err(CalError::TemplateNestingTooDeep { .. })
        ));
    }

    #[test]
    fn test_limit_each_iterations() {
        // §10.8 caps {{#each}} at 200. This is the pre-§10.6 whole-result
        // form; a sectioned template has the engine drive iteration and is
        // not bounded by it.
        let grains: Vec<CalGrainResult> = (0..MAX_EACH_ITERATIONS + 50)
            .map(|i| CalGrainResult {
                hash: format!("{i:064}"),
                grain_type: "fact".into(),
                score: 1.0,
                fields: serde_json::json!({ "subject": "s" }),
                is_deterministic: true,
                contested_by: None,
                explanation: None,
                score_breakdown: None,
                relative_time: None,
            })
            .collect();
        let t = parse_template("{{#each grains}}x{{/each}}").unwrap();
        let ctx = RenderContext {
            now_secs: 0,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        assert_eq!(render(&t, &grains, &ctx).unwrap().len(), MAX_EACH_ITERATIONS);
    }

    #[test]
    fn test_limit_templates_per_namespace() {
        let mut reg = TemplateRegistry::new();
        for i in 0..MAX_TEMPLATES {
            reg.register(&format!("t{i}"), "{{grain.content}}", "", None)
                .unwrap_or_else(|e| panic!("template {i} should register: {e}"));
        }
        assert!(matches!(
            reg.register("one_too_many", "{{grain.content}}", "", None),
            Err(CalError::TooManyTemplates { .. })
        ));
        // Redefining an existing one does not grow the set, so it is allowed.
        assert!(reg.register("t0", "{{grain.subject}}", "", None).is_ok());
    }

    #[test]
    fn test_data_preset_cannot_be_extended() {
        let mut reg = TemplateRegistry::new();
        assert!(matches!(
            reg.register("x", "{{grain.content}}", "", Some("data")),
            Err(CalError::CannotExtendData { .. })
        ));
    }

    #[test]
    fn test_registry_builtin_count() {
        let reg = TemplateRegistry::new();
        let list = reg.list();
        // Exactly the 3 OMS §10.1 presets (structured/readable/compact).
        // `data` is deliberately absent — §10.7 forbids extending it, and it
        // is a renderer, not a template.
        assert_eq!(list.len(), 3);
        assert!(list.iter().all(|e| e.builtin));
        for preset in ["structured", "readable", "compact"] {
            assert!(
                reg.get(preset).is_some_and(|e| e.template.is_sectioned()),
                "preset {preset} must exist and be sectioned"
            );
        }
        assert!(reg.get("data").is_none(), "`data` is not an extendable template");
    }

    #[test]
    fn test_registry_register_custom() {
        let mut reg = TemplateRegistry::new();
        reg.register(
            "my_template",
            "{{#each grains}}{{subject}}{{/each}}",
            "A custom template",
            None,
        )
        .unwrap();
        assert!(reg.get("my_template").is_some());
        assert!(!reg.get("my_template").unwrap().builtin);
        assert_eq!(reg.list().len(), 4);
    }

    #[test]
    fn test_registry_cannot_overwrite_builtin() {
        let mut reg = TemplateRegistry::new();
        let result = reg.register("readable", "{{subject}}", "override", None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E046");
    }

    #[test]
    fn test_registry_invalid_name_rejected() {
        let mut reg = TemplateRegistry::new();
        let result = reg.register("123-starts-with-digit", "{{subject}}", "bad name", None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        // CAL-E115 = TemplateInvalidName. (CAL-E044 = Tier1NotEnabled.)
        assert_eq!(err.code(), "CAL-E115");
    }

    #[test]
    fn test_registry_delete_custom() {
        let mut reg = TemplateRegistry::new();
        reg.register(
            "my_template",
            "{{#each grains}}{{subject}}{{/each}}",
            "test",
            None,
        )
        .unwrap();
        assert!(reg.get("my_template").is_some());
        reg.delete("my_template").unwrap();
        assert!(reg.get("my_template").is_none());
    }

    #[test]
    fn test_registry_cannot_delete_builtin() {
        let mut reg = TemplateRegistry::new();
        let result = reg.delete("readable");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E046");
    }

    #[test]
    fn test_registry_delete_nonexistent() {
        let mut reg = TemplateRegistry::new();
        let result = reg.delete("nonexistent");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E045");
    }

    #[test]
    fn test_registry_inheritance() {
        let mut reg = TemplateRegistry::new();

        // Register a parent template.
        reg.register(
            "parent_tpl",
            concat!(
                "{{#each grains}}",
                "{{#grain_type \"fact\"}}B: {{subject}}{{/grain_type}}",
                "{{#grain_type \"tool\"}}A: {{tool_name}}{{/grain_type}}",
                "{{/each}}"
            ),
            "Parent",
            None,
        )
        .unwrap();

        // Register a child that overrides fact block.
        reg.register(
            "child_tpl",
            "{{#grain_type \"fact\"}}CUSTOM: {{subject}} -> {{object}}{{/grain_type}}",
            "Child",
            Some("parent_tpl"),
        )
        .unwrap();

        let child = reg.get("child_tpl").unwrap();
        let resolved = resolve_inheritance(child, &reg);

        // The resolved template should have the child's fact block
        // but keep the parent's tool block.
        let grains = vec![
            make_fact("john", "mg:likes", "coffee", 0.94),
            make_tool("search", "weather", "sunny", false),
        ];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = render(&resolved, &grains, &ctx).unwrap();
        assert!(result.contains("CUSTOM: john -> coffee"));
        assert!(result.contains("A: search"));
    }

    #[test]
    fn test_registry_2_level_inheritance_rejected() {
        let mut reg = TemplateRegistry::new();

        reg.register(
            "grandparent",
            "{{#each grains}}{{subject}}{{/each}}",
            "GP",
            None,
        )
        .unwrap();

        reg.register(
            "parent_tpl",
            "{{#each grains}}{{subject}}{{/each}}",
            "P",
            Some("grandparent"),
        )
        .unwrap();

        let result = reg.register(
            "child_tpl",
            "{{#each grains}}{{subject}}{{/each}}",
            "C",
            Some("parent_tpl"),
        );
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E048");
    }

    #[test]
    fn test_registry_parent_not_found() {
        let mut reg = TemplateRegistry::new();
        let result = reg.register(
            "child_tpl",
            "{{#each grains}}{{subject}}{{/each}}",
            "C",
            Some("nonexistent"),
        );
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E047");
    }

    // -----------------------------------------------------------------------
    // apply_format integration
    // -----------------------------------------------------------------------

    #[test]
    fn test_apply_format() {
        let mut reg = TemplateRegistry::new();
        reg.register(
            "one_line_triples",
            "{{#each grains}}{{subject}} {{relation | humanize}} {{object}}\n{{/each}}",
            "test",
            None,
        )
        .unwrap();
        let entry = reg.get("one_line_triples").unwrap();
        let grains = vec![make_fact("john", "mg:likes", "coffee", 0.94)];
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: grains.len(),
            user_vars: HashMap::new(),
        };
        let result = apply_format(&grains, &entry.template, &ctx).unwrap();
        assert!(result.contains("john likes coffee"));
    }

    // -----------------------------------------------------------------------
    // Edge cases
    // -----------------------------------------------------------------------

    #[test]
    fn test_empty_template() {
        let t = parse_template("").unwrap();
        let ctx = test_ctx();
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "");
    }

    #[test]
    fn test_empty_grains() {
        let t = parse_template("Before{{#each grains}}{{subject}}{{/each}}After").unwrap();
        let ctx = test_ctx();
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "BeforeAfter");
    }

    #[test]
    fn test_unicode_truncation() {
        let ctx = test_ctx();
        // Japanese characters (3 bytes each in UTF-8)
        let val =
            ResolvedValue::Str("\u{3053}\u{3093}\u{306b}\u{3061}\u{306f}\u{4e16}\u{754c}".into()); // "konnichiwa sekai"
        let filter = Filter {
            name: "truncate".into(),
            arg: Some("3".into()),
        };
        let result = apply_filter(&val, &filter, &ctx).unwrap();
        let display = result.to_display();
        // Should truncate at 3 characters, not 3 bytes.
        assert!(display.ends_with("..."));
        assert_eq!(display, "\u{3053}\u{3093}\u{306b}...");
    }

    #[test]
    fn test_render_with_null_fields() {
        // A grain with no subject/relation/object.
        let grain = CalGrainResult {
            hash: "abc123".to_string(),
            grain_type: "fact".to_string(),
            score: 0.5,
            fields: json!({}),
            score_breakdown: None,
            explanation: None,
            relative_time: None,
            is_deterministic: false,
            contested_by: None,
        };
        let t =
            parse_template("{{#each grains}}{{subject}} {{relation}} {{object}}{{/each}}").unwrap();
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &[grain], &ctx).unwrap();
        // Null fields render as empty strings.
        assert_eq!(result, "  ");
    }

    #[test]
    fn test_parse_date_filter_with_quoted_arg() {
        let t = parse_template("{{created_at | date \"%Y-%m-%d %H:%M\"}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Variable { filters, .. } => {
                assert_eq!(filters[0].name, "date");
                assert_eq!(filters[0].arg, Some("%Y-%m-%d %H:%M".to_string()));
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_parse_default_filter_with_quoted_arg() {
        let t = parse_template("{{summary | default \"No summary\"}}").unwrap();
        match &t.nodes()[0] {
            TemplateNode::Variable { filters, .. } => {
                assert_eq!(filters[0].name, "default");
                assert_eq!(filters[0].arg, Some("No summary".to_string()));
            }
            _ => panic!("expected Variable"),
        }
    }

    #[test]
    fn test_template_source_preserved() {
        let source = "{{#each grains}}{{subject}}{{/each}}";
        let t = parse_template(source).unwrap();
        assert_eq!(t.source(), source);
    }

    #[test]
    fn test_registry_list_sorted() {
        // `TemplateRegistry::list` sorts entries by `updated_at` descending
        // (most-recent first). A fresh registry has all builtins with
        // identical timestamps, so the relative order between them is not
        // guaranteed — but the list must still be a non-increasing sequence
        // of timestamps.
        let reg = TemplateRegistry::new();
        let list = reg.list();
        let ts: Vec<u64> = list.iter().map(|e| e.updated_at.unwrap_or(0)).collect();
        assert!(
            ts.windows(2).all(|w| w[0] >= w[1]),
            "list is not sorted by updated_at DESC: {:?}",
            ts
        );
    }

    #[test]
    fn test_select_tier_boundary_values() {
        // 200 tokens/grain = Full
        assert_eq!(select_tier(2000, 10), DisclosureTier::Full);
        // 199 tokens/grain = Headlines
        assert_eq!(select_tier(1990, 10), DisclosureTier::Headlines);
        // 80 tokens/grain = Headlines
        assert_eq!(select_tier(800, 10), DisclosureTier::Headlines);
        // 79 tokens/grain = Summary
        assert_eq!(select_tier(790, 10), DisclosureTier::Summary);
    }

    #[test]
    fn test_suggestion_for_unknown_variable() {
        let result = parse_template("{{subjet}}"); // typo of "subject"
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), "CAL-E042");
        // Should suggest "subject".
        let suggestion = err.suggestion();
        assert!(suggestion.is_some());
        assert!(suggestion.unwrap().contains("subject"));
    }

    // -----------------------------------------------------------------------
    // User-injected display variables ($-prefixed)
    // -----------------------------------------------------------------------

    #[test]
    fn test_user_var_parse_valid() {
        // $-prefixed variables should pass parse-time validation.
        let t = parse_template("{{$user_query}}");
        assert!(t.is_ok(), "should accept $-prefixed variable");
    }

    #[test]
    fn test_user_var_parse_with_filter() {
        let t = parse_template("{{$name | uppercase}}");
        assert!(t.is_ok(), "should accept $-prefixed variable with filter");
    }

    #[test]
    fn test_user_var_parse_in_condition() {
        let t = parse_template("{{#if $show_header}}Header{{/if}}");
        assert!(t.is_ok(), "should accept $-prefixed variable in condition");
    }

    #[test]
    fn test_user_var_parse_invalid_name() {
        let result = parse_template("{{$123bad}}");
        assert!(
            result.is_err(),
            "should reject $-prefixed var starting with digit"
        );
    }

    #[test]
    fn test_user_var_resolve_found() {
        let t = parse_template("Query: {{$user_query}}").unwrap();
        let mut vars = HashMap::new();
        vars.insert("user_query".into(), "what does john like?".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: vars,
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "Query: what does john like?");
    }

    #[test]
    fn test_user_var_resolve_missing() {
        let t = parse_template("{{$missing_var}}").unwrap();
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(
            result, "",
            "missing user var should resolve to empty string"
        );
    }

    #[test]
    fn test_user_var_with_filter() {
        let t = parse_template("{{$name | uppercase}}").unwrap();
        let mut vars = HashMap::new();
        vars.insert("name".into(), "john".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: vars,
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "JOHN");
    }

    #[test]
    fn test_user_var_with_grain_fields() {
        // User vars + grain fields coexist.
        let t = parse_template("{{$label}}: {{#each}}{{subject}} {{relation}} {{object}}{{/each}}")
            .unwrap();
        let grain = make_fact("john", "likes", "coffee", 0.9);
        let mut vars = HashMap::new();
        vars.insert("label".into(), "Results".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: vars,
        };
        let result = render(&t, &[grain], &ctx).unwrap();
        assert_eq!(result, "Results: john likes coffee");
    }

    #[test]
    fn test_user_var_condition_truthy() {
        let t = parse_template("{{#if $show}}visible{{/if}}").unwrap();
        let mut vars = HashMap::new();
        vars.insert("show".into(), "yes".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: vars,
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "visible");
    }

    #[test]
    fn test_user_var_condition_falsy() {
        // Missing user var → Null → falsy.
        let t = parse_template("{{#if $show}}visible{{/if}}").unwrap();
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: HashMap::new(),
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "");
    }

    #[test]
    fn test_user_var_condition_empty_string_is_falsy() {
        // Empty string user var → should be falsy (empty = no content).
        let t = parse_template("{{#if $show}}visible{{/if}}").unwrap();
        let mut vars = HashMap::new();
        vars.insert("show".into(), "".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 0,
            user_vars: vars,
        };
        let result = render(&t, &[], &ctx).unwrap();
        assert_eq!(result, "", "empty-string user var should be falsy");
    }

    #[test]
    fn test_user_var_no_collision_with_grain_fields() {
        // A user var named $subject should NOT override the grain field "subject".
        let t = parse_template("{{#each}}grain:{{subject}} user:{{$subject}}{{/each}}").unwrap();
        let grain = make_fact("john", "likes", "coffee", 0.9);
        let mut vars = HashMap::new();
        vars.insert("subject".into(), "override_attempt".into());
        let ctx = RenderContext {
            now_secs: 1700000100,
            tier: DisclosureTier::Full,
            total_count: 1,
            user_vars: vars,
        };
        let result = render(&t, &[grain], &ctx).unwrap();
        assert_eq!(result, "grain:john user:override_attempt");
    }
}