allium-cli 3.5.2

CLI for checking Allium specification files
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
//! Walks the Allium AST and emits a test plan.
//!
//! Each obligation represents a test that should exist, derived mechanically
//! from the spec's constructs. The output is language-agnostic structured data.
//!
//! Invoked by `allium plan`.

use allium_parser::ast::*;
use allium_parser::{Module, Span};
use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct SourceSpan {
    pub start: usize,
    pub end: usize,
}

#[derive(Debug, Serialize)]
pub struct TestPlan {
    pub version: Option<u32>,
    pub obligations: Vec<Obligation>,
}

#[derive(Debug, Serialize)]
pub struct Obligation {
    pub id: String,
    pub category: ObligationCategory,
    pub description: String,
    /// Name of the entity, rule, surface, etc. that sourced this obligation.
    pub source_construct: String,
    /// Invariant expression text, when this obligation is an invariant property.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expression: Option<String>,
    /// Byte offset range in the source file.
    pub source_span: SourceSpan,
    /// Category-specific data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<ObligationDetail>,
    /// Rule dependency analysis, present on rule obligations only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<RuleDependencies>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ObligationCategory {
    EntityFields,
    EntityOptional,
    EntityRelationship,
    ValueEquality,
    EnumComparable,
    SumTypeVariant,
    Derived,
    Projection,
    ConfigDefault,
    Invariant,
    RuleSuccess,
    RuleFailure,
    RuleEntityCreation,
    TransitionEdge,
    TransitionRejected,
    TransitionTerminal,
    WhenPresence,
    WhenSet,
    WhenClear,
    Temporal,
    SurfaceExposure,
    SurfaceProvides,
    SurfaceActor,
    ContractSignature,
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum ObligationDetail {
    Fields { fields: Vec<String> },
    Transition { entity: String, field: String, from: String, to: String },
    Terminal { entity: String, field: String, states: Vec<String> },
    WhenFieldPresence {
        entity: String,
        field: String,
        status_field: String,
        qualifying_states: Vec<String>,
    },
    WhenPresence {
        rule: String,
        entity: String,
        field: String,
        source_state: String,
        target_state: String,
        qualifying_states: Vec<String>,
    },
    WhenAbsence {
        rule: String,
        entity: String,
        field: String,
        source_state: String,
        target_state: String,
        qualifying_states: Vec<String>,
    },
    Surface { surface: String, items: Vec<String> },
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TriggerSource {
    External,
    StateTransition,
    Temporal,
    Creation,
    Chained,
}

#[derive(Debug, Clone, Serialize)]
pub struct RuleDependencies {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub entities_read: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub entities_written: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub entities_created: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub entities_removed: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub deferred_specs: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub trigger_emissions: Vec<String>,
    pub trigger_source: TriggerSource,
}

/// Module-level context for dependency analysis.
struct ModuleContext {
    entity_names: std::collections::BTreeSet<String>,
    deferred_names: std::collections::BTreeSet<String>,
    /// Trigger names emitted in ensures clauses across all rules.
    emitted_triggers: std::collections::BTreeSet<String>,
}

pub fn generate_test_plan(module: &Module, source: &str) -> TestPlan {
    let mut plan = TestPlan {
        version: module.version,
        obligations: Vec::new(),
    };

    let ctx = build_module_context(module);

    for decl in &module.declarations {
        match decl {
            Decl::Block(block) => match block.kind {
                BlockKind::Entity | BlockKind::ExternalEntity => {
                    emit_entity_obligations(&mut plan, block, source);
                }
                BlockKind::Value => {
                    emit_value_obligations(&mut plan, block);
                }
                BlockKind::Enum => {
                    emit_enum_obligations(&mut plan, block);
                }
                BlockKind::Rule => {
                    emit_rule_obligations(&mut plan, block, &ctx);
                }
                BlockKind::Surface => {
                    emit_surface_obligations(&mut plan, block);
                }
                BlockKind::Config => {
                    emit_config_obligations(&mut plan, block);
                }
                BlockKind::Contract => {
                    emit_contract_obligations(&mut plan, block);
                }
                _ => {}
            },
            Decl::Variant(v) => {
                emit_variant_obligations(&mut plan, v);
            }
            Decl::Invariant(inv) => {
                emit_invariant_obligation(&mut plan, inv, source);
            }
            _ => {}
        }
    }

    // Cross-reference rules with entity when-qualified fields for presence/absence obligations
    emit_when_crossing_obligations(&mut plan, &module.declarations);

    plan
}

/// Collect when-qualified fields from all entities, then check each rule for
/// status transitions that cross the boundary of a field's when set.
fn emit_when_crossing_obligations(plan: &mut TestPlan, declarations: &[Decl]) {
    struct WhenField {
        entity: String,
        field: String,
        status_field: String,
        qualifying_states: Vec<String>,
    }

    // Per-entity: when-qualified fields and transition graph edges
    struct EntityInfo {
        when_fields: Vec<WhenField>,
        /// Transition edges as (from, to) for the status field referenced by when-fields
        edges: Vec<(String, String)>,
        /// All states in the transition graph
        states: Vec<String>,
    }

    let mut entities: std::collections::HashMap<String, EntityInfo> = std::collections::HashMap::new();

    for decl in declarations {
        if let Decl::Block(block) = decl {
            if block.kind == BlockKind::Entity || block.kind == BlockKind::ExternalEntity {
                let entity_name = block.name.as_ref().map(|n| n.name.clone()).unwrap_or_default();
                let info = entities.entry(entity_name.clone()).or_insert_with(|| EntityInfo {
                    when_fields: Vec::new(),
                    edges: Vec::new(),
                    states: Vec::new(),
                });

                for item in &block.items {
                    match &item.kind {
                        BlockItemKind::FieldWithWhen { name, when_clause, .. } => {
                            info.when_fields.push(WhenField {
                                entity: entity_name.clone(),
                                field: name.name.clone(),
                                status_field: when_clause.status_field.name.clone(),
                                qualifying_states: when_clause.qualifying_states.iter().map(|s| s.name.clone()).collect(),
                            });
                        }
                        BlockItemKind::TransitionsBlock(graph) => {
                            for edge in &graph.edges {
                                info.edges.push((edge.from.name.clone(), edge.to.name.clone()));
                                if !info.states.contains(&edge.from.name) {
                                    info.states.push(edge.from.name.clone());
                                }
                                if !info.states.contains(&edge.to.name) {
                                    info.states.push(edge.to.name.clone());
                                }
                            }
                            for t in &graph.terminal {
                                if !info.states.contains(&t.name) {
                                    info.states.push(t.name.clone());
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    // Filter to entities that have when-fields
    let entities_with_when: Vec<(&String, &EntityInfo)> = entities.iter()
        .filter(|(_, info)| !info.when_fields.is_empty())
        .collect();

    if entities_with_when.is_empty() {
        return;
    }

    // For each rule, extract source/target state and match against entities
    for decl in declarations {
        if let Decl::Block(block) = decl {
            if block.kind != BlockKind::Rule {
                continue;
            }
            let rule_name = block.name.as_ref().map(|n| n.name.clone()).unwrap_or_default();
            let rule_span = block.span;

            let source_states = extract_requires_states(&block.items);
            let target_state = extract_ensures_target_state(&block.items);

            if let Some((target_obj, target_field, target_value)) = &target_state {
                // Use object variable to scope to the correct entity.
                // Convention: `order.status` → entity `Order` (capitalised variable name).
                let target_entity_hint = if !target_obj.is_empty() {
                    let mut hint = target_obj.clone();
                    if let Some(first) = hint.get_mut(0..1) {
                        first.make_ascii_uppercase();
                    }
                    Some(hint)
                } else {
                    None
                };

                for (entity_name, info) in &entities_with_when {
                    // If we have an entity hint, use it to filter
                    if let Some(hint) = &target_entity_hint {
                        if hint != *entity_name {
                            continue;
                        }
                    } else if !info.states.contains(target_value) {
                        // No hint — fall back to checking graph membership
                        continue;
                    }

                    // Find source states relevant to this entity from the requires clause
                    let entity_source_states: Vec<&StateInfo> = source_states.iter()
                        .filter(|(obj, field, _)| {
                            field == target_field && (obj.is_empty() || {
                                let mut h = obj.clone();
                                if let Some(f) = h.get_mut(0..1) { f.make_ascii_uppercase(); }
                                &h == *entity_name
                            })
                        })
                        .collect();

                    for wf in &info.when_fields {
                        if wf.status_field != *target_field {
                            continue;
                        }

                        let target_in_set = wf.qualifying_states.contains(target_value);

                        if !entity_source_states.is_empty() {
                            // Collect valid (source_value, direction) pairs from graph edges
                            let mut emitted_presence = false;
                            let mut emitted_absence = false;
                            for (_, _, src_value) in &entity_source_states {
                                if !info.edges.iter().any(|(f, t)| f == src_value && t == target_value) {
                                    continue;
                                }
                                let source_in_set = wf.qualifying_states.contains(src_value);

                                if !source_in_set && target_in_set && !emitted_presence {
                                    emitted_presence = true;
                                    plan.obligations.push(Obligation {
                                        id: format!("when-set.{}.{}.{}", rule_name, wf.entity, wf.field),
                                        category: ObligationCategory::WhenSet,
                                        description: format!(
                                            "Verify rule {} sets {}.{} when transitioning {} from {} to {} (entering when set {{{}}})",
                                            rule_name, wf.entity, wf.field, wf.status_field,
                                            src_value, target_value,
                                            wf.qualifying_states.join(", ")
                                        ),
                                        source_construct: rule_name.clone(),
                                        expression: None,
                                        source_span: SourceSpan { start: rule_span.start, end: rule_span.end },
                                        detail: Some(ObligationDetail::WhenPresence {
                                            rule: rule_name.clone(),
                                            entity: wf.entity.clone(),
                                            field: wf.field.clone(),
                                            source_state: src_value.clone(),
                                            target_state: target_value.clone(),
                                            qualifying_states: wf.qualifying_states.clone(),
                                        }),
                                        dependencies: None,
                                    });
                                } else if source_in_set && !target_in_set && !emitted_absence {
                                    emitted_absence = true;
                                    plan.obligations.push(Obligation {
                                        id: format!("when-clear.{}.{}.{}", rule_name, wf.entity, wf.field),
                                        category: ObligationCategory::WhenClear,
                                        description: format!(
                                            "Verify rule {} clears {}.{} when transitioning {} from {} to {} (leaving when set {{{}}})",
                                            rule_name, wf.entity, wf.field, wf.status_field,
                                            src_value, target_value,
                                            wf.qualifying_states.join(", ")
                                        ),
                                        source_construct: rule_name.clone(),
                                        expression: None,
                                        source_span: SourceSpan { start: rule_span.start, end: rule_span.end },
                                        detail: Some(ObligationDetail::WhenAbsence {
                                            rule: rule_name.clone(),
                                            entity: wf.entity.clone(),
                                            field: wf.field.clone(),
                                            source_state: src_value.clone(),
                                            target_state: target_value.clone(),
                                            qualifying_states: wf.qualifying_states.clone(),
                                        }),
                                        dependencies: None,
                                    });
                                }
                            }
                        } else if target_in_set {
                            // No source state info — emit conservatively if entity has edges to target
                            if !info.edges.iter().any(|(_, t)| t == target_value) {
                                continue;
                            }
                            plan.obligations.push(Obligation {
                                id: format!("when-set.{}.{}.{}", rule_name, wf.entity, wf.field),
                                category: ObligationCategory::WhenSet,
                                description: format!(
                                    "Verify rule {} sets {}.{} when transitioning {} to {} (entering when set {{{}}})",
                                    rule_name, wf.entity, wf.field, wf.status_field,
                                    target_value,
                                    wf.qualifying_states.join(", ")
                                ),
                                source_construct: rule_name.clone(),
                                expression: None,
                                source_span: SourceSpan { start: rule_span.start, end: rule_span.end },
                                detail: Some(ObligationDetail::WhenPresence {
                                    rule: rule_name.clone(),
                                    entity: wf.entity.clone(),
                                    field: wf.field.clone(),
                                    source_state: "unknown".to_string(),
                                    target_state: target_value.clone(),
                                    qualifying_states: wf.qualifying_states.clone(),
                                }),
                                dependencies: None,
                            });
                        }
                    }
                }
            }
        }
    }
}

/// State extraction result: (object_variable, field_name, state_value)
/// e.g. `order.status = pending` → ("order", "status", "pending")
type StateInfo = (String, String, String);

/// Extract source states from requires clause.
/// Returns a list because `in {a, b}` produces multiple source states.
fn extract_requires_states(items: &[BlockItem]) -> Vec<StateInfo> {
    for item in items {
        if let BlockItemKind::Clause { keyword, value } = &item.kind {
            if keyword == "requires" {
                return extract_states_from_expr(value);
            }
        }
    }
    Vec::new()
}

/// Extract target state from ensures: `entity.status = state`
fn extract_ensures_target_state(items: &[BlockItem]) -> Option<StateInfo> {
    for item in items {
        if let BlockItemKind::Clause { keyword, value } = &item.kind {
            if keyword == "ensures" {
                let result = extract_eq_state(value);
                if result.is_some() {
                    return result;
                }
                if let Expr::Block { items: exprs, .. } = value {
                    for expr in exprs {
                        let result = extract_eq_state(expr);
                        if result.is_some() {
                            return result;
                        }
                    }
                }
            }
        }
    }
    None
}

/// Extract states from an expression. Handles `obj.field = state` and `obj.field in {a, b}`.
fn extract_states_from_expr(expr: &Expr) -> Vec<StateInfo> {
    // Single equality
    if let Some(info) = extract_eq_state(expr) {
        return vec![info];
    }
    // `obj.field in {a, b, c}`
    if let Expr::In { element, collection, .. } = expr {
        let (obj, field) = match element.as_ref() {
            Expr::MemberAccess { object, field, .. } => {
                if let Expr::Ident(id) = object.as_ref() {
                    (id.name.clone(), field.name.clone())
                } else {
                    return Vec::new();
                }
            }
            _ => return Vec::new(),
        };
        if let Expr::SetLiteral { elements, .. } = collection.as_ref() {
            return elements.iter().filter_map(|e| {
                if let Expr::Ident(id) = e {
                    Some((obj.clone(), field.clone(), id.name.clone()))
                } else {
                    None
                }
            }).collect();
        }
    }
    // LogicalOp (and/or) — check both sides
    if let Expr::LogicalOp { left, right, .. } = expr {
        let mut results = extract_states_from_expr(left);
        results.extend(extract_states_from_expr(right));
        return results;
    }
    Vec::new()
}

/// Extract `obj.field = value` → (obj, field, value)
fn extract_eq_state(expr: &Expr) -> Option<StateInfo> {
    if let Expr::Comparison { left, op: ComparisonOp::Eq, right, .. } = expr {
        let (obj, field) = match left.as_ref() {
            Expr::MemberAccess { object, field, .. } => {
                let obj_name = match object.as_ref() {
                    Expr::Ident(id) => id.name.clone(),
                    _ => String::new(),
                };
                (obj_name, field.name.clone())
            }
            Expr::Ident(id) => (String::new(), id.name.clone()),
            _ => return None,
        };
        let value_name = match right.as_ref() {
            Expr::Ident(id) => Some(id.name.clone()),
            _ => None,
        };
        if let Some(v) = value_name {
            return Some((obj, field, v));
        }
    }
    None
}

fn block_name(block: &BlockDecl) -> String {
    block.name.as_ref().map(|n| n.name.clone()).unwrap_or_default()
}

fn emit_entity_obligations(plan: &mut TestPlan, block: &BlockDecl, source: &str) {
    let name = block_name(block);

    // Collect fields
    let mut fields = Vec::new();
    let mut optional_fields = Vec::new();
    let mut relationships = Vec::new();
    let mut derived_values = Vec::new();
    let mut projections = Vec::new();

    for item in &block.items {
        match &item.kind {
            BlockItemKind::Assignment { name: field_name, value } => {
                fields.push(field_name.name.clone());

                // Check for optional type
                if matches!(value, Expr::TypeOptional { .. }) {
                    optional_fields.push(field_name.name.clone());
                }

                // Check for relationship (has `with`)
                if matches!(value, Expr::With { .. }) {
                    relationships.push(field_name.name.clone());
                }

                // Check for projection (has `where` on a field ref)
                if matches!(value, Expr::Where { .. }) {
                    projections.push(field_name.name.clone());
                }

                // Derived values: anything that's a comparison, logical op, member access chain, etc.
                if is_derived_expression(value) {
                    derived_values.push(field_name.name.clone());
                }
            }
            BlockItemKind::FieldWithWhen { name: field_name, value, when_clause } => {
                fields.push(field_name.name.clone());

                if matches!(value, Expr::TypeOptional { .. }) {
                    optional_fields.push(field_name.name.clone());
                }

                // Emit when-field-presence obligation
                let states: Vec<String> = when_clause.qualifying_states.iter().map(|s| s.name.clone()).collect();
                plan.obligations.push(Obligation {
                    id: format!("when-presence.{}.{}", name, field_name.name),
                    category: ObligationCategory::WhenPresence,
                    description: format!(
                        "Verify {}.{} is present when {} in {{{}}} and absent otherwise",
                        name, field_name.name, when_clause.status_field.name,
                        states.join(", ")
                    ),
                    source_construct: format!("{}.{}", name, field_name.name),
                    expression: None,
                    source_span: SourceSpan { start: item.span.start, end: item.span.end },
                    detail: Some(ObligationDetail::WhenFieldPresence {
                        entity: name.clone(),
                        field: field_name.name.clone(),
                        status_field: when_clause.status_field.name.clone(),
                        qualifying_states: states,
                    }),
                    dependencies: None,
                });
            }
            BlockItemKind::TransitionsBlock(graph) => {
                emit_transition_obligations(plan, &name, graph);
            }
            BlockItemKind::InvariantBlock { name: inv_name, body } => {
                plan.obligations.push(Obligation {
                    id: format!("invariant.{}.{}", name, inv_name.name),
                    category: ObligationCategory::Invariant,
                    description: format!(
                        "Verify invariant {} holds after any field mutation on {}",
                        inv_name.name, name
                    ),
                    source_construct: format!("{}.{}", name, inv_name.name),
                    expression: Some(span_text(source, body.span())),
                    source_span: SourceSpan { start: item.span.start, end: item.span.end },
                    detail: None,
                    dependencies: None,
                });
            }
            _ => {}
        }
    }

    if !fields.is_empty() {
        plan.obligations.push(Obligation {
            id: format!("entity-fields.{}", name),
            category: ObligationCategory::EntityFields,
            description: format!("Verify all declared fields on {} are present with correct types", name),
            source_construct: name.clone(),
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: Some(ObligationDetail::Fields { fields: fields.clone() }),
            dependencies: None,
        });
    }

    for f in &optional_fields {
        plan.obligations.push(Obligation {
            id: format!("entity-optional.{}.{}", name, f),
            category: ObligationCategory::EntityOptional,
            description: format!("Verify optional field {}.{} accepts null and non-null values", name, f),
            source_construct: format!("{}.{}", name, f),
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: None,
            dependencies: None,
        });
    }

    for r in &relationships {
        plan.obligations.push(Obligation {
            id: format!("entity-relationship.{}.{}", name, r),
            category: ObligationCategory::EntityRelationship,
            description: format!("Verify relationship {}.{} navigates to the correct related entities", name, r),
            source_construct: format!("{}.{}", name, r),
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: None,
            dependencies: None,
        });
    }

    for p in &projections {
        plan.obligations.push(Obligation {
            id: format!("projection.{}.{}", name, p),
            category: ObligationCategory::Projection,
            description: format!("Verify projection {}.{} filters correctly", name, p),
            source_construct: format!("{}.{}", name, p),
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: None,
            dependencies: None,
        });
    }

    for d in &derived_values {
        plan.obligations.push(Obligation {
            id: format!("derived.{}.{}", name, d),
            category: ObligationCategory::Derived,
            description: format!("Verify derived value {}.{} computes correctly", name, d),
            source_construct: format!("{}.{}", name, d),
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: None,
            dependencies: None,
        });
    }
}

fn emit_transition_obligations(plan: &mut TestPlan, entity: &str, graph: &TransitionGraph) {
    let field = &graph.field.name;

    // One obligation per declared edge
    for edge in &graph.edges {
        plan.obligations.push(Obligation {
            id: format!("transition-edge.{}.{}.{}", entity, edge.from.name, edge.to.name),
            category: ObligationCategory::TransitionEdge,
            description: format!(
                "Verify transition {} -> {} on {}.{} is reachable via a witnessing rule",
                edge.from.name, edge.to.name, entity, field
            ),
            source_construct: format!("{}.{}", entity, field),
            expression: None,
            source_span: SourceSpan { start: edge.span.start, end: edge.span.end },
            detail: Some(ObligationDetail::Transition {
                entity: entity.to_string(),
                field: field.clone(),
                from: edge.from.name.clone(),
                to: edge.to.name.clone(),
            }),
            dependencies: None,
        });
    }

    // Obligation: undeclared transitions are rejected
    plan.obligations.push(Obligation {
        id: format!("transition-rejected.{}.{}", entity, field),
        category: ObligationCategory::TransitionRejected,
        description: format!(
            "Verify undeclared transitions on {}.{} are rejected",
            entity, field
        ),
        source_construct: format!("{}.{}", entity, field),
        expression: None,
        source_span: SourceSpan { start: graph.span.start, end: graph.span.end },
        detail: None,
        dependencies: None,
    });

    // Obligation per terminal state: no outbound transitions
    if !graph.terminal.is_empty() {
        plan.obligations.push(Obligation {
            id: format!("transition-terminal.{}.{}", entity, field),
            category: ObligationCategory::TransitionTerminal,
            description: format!(
                "Verify terminal states on {}.{} have no outbound transitions",
                entity, field
            ),
            source_construct: format!("{}.{}", entity, field),
            expression: None,
            source_span: SourceSpan { start: graph.span.start, end: graph.span.end },
            detail: Some(ObligationDetail::Terminal {
                entity: entity.to_string(),
                field: field.clone(),
                states: graph.terminal.iter().map(|t| t.name.clone()).collect(),
            }),
            dependencies: None,
        });
    }
}

fn emit_value_obligations(plan: &mut TestPlan, block: &BlockDecl) {
    let name = block_name(block);
    plan.obligations.push(Obligation {
        id: format!("value-equality.{}", name),
        category: ObligationCategory::ValueEquality,
        description: format!("Verify value type {} has structural equality", name),
        source_construct: name.clone(),
        expression: None,
        source_span: SourceSpan { start: block.span.start, end: block.span.end },
        detail: None,
        dependencies: None,
    });

    let fields: Vec<String> = block.items.iter().filter_map(|item| {
        if let BlockItemKind::Assignment { name, .. } = &item.kind {
            Some(name.name.clone())
        } else {
            None
        }
    }).collect();

    if !fields.is_empty() {
        plan.obligations.push(Obligation {
            id: format!("entity-fields.{}", name),
            category: ObligationCategory::EntityFields,
            description: format!("Verify all declared fields on {} are present with correct types", name),
            source_construct: name,
            expression: None,
            source_span: SourceSpan { start: block.span.start, end: block.span.end },
            detail: Some(ObligationDetail::Fields { fields }),
            dependencies: None,
        });
    }
}

fn emit_enum_obligations(plan: &mut TestPlan, block: &BlockDecl) {
    let name = block_name(block);
    plan.obligations.push(Obligation {
        id: format!("enum-comparable.{}", name),
        category: ObligationCategory::EnumComparable,
        description: format!("Verify fields typed with enum {} are comparable", name),
        source_construct: name,
        expression: None,
        source_span: SourceSpan { start: block.span.start, end: block.span.end },
        detail: None,
        dependencies: None,
    });
}

fn emit_rule_obligations(plan: &mut TestPlan, block: &BlockDecl, ctx: &ModuleContext) {
    let name = block_name(block);
    let deps = extract_rule_dependencies(block, ctx);

    // Success case
    plan.obligations.push(Obligation {
        id: format!("rule-success.{}", name),
        category: ObligationCategory::RuleSuccess,
        description: format!("Verify rule {} succeeds when all preconditions are met", name),
        source_construct: name.clone(),
        expression: None,
        source_span: SourceSpan { start: block.span.start, end: block.span.end },
        detail: None,
        dependencies: Some(deps.clone()),
    });

    // Walk items for specific obligations
    walk_rule_items(plan, &name, &block.items, &deps);
}

fn walk_rule_items(plan: &mut TestPlan, rule_name: &str, items: &[BlockItem], deps: &RuleDependencies) {
    walk_rule_items_inner(plan, rule_name, items, deps, &mut 0, &mut 0);
}

fn walk_rule_items_inner(plan: &mut TestPlan, rule_name: &str, items: &[BlockItem], deps: &RuleDependencies, failure_ordinal: &mut u32, creation_ordinal: &mut u32) {
    for item in items {
        match &item.kind {
            BlockItemKind::Clause { keyword, .. } if keyword == "requires" => {
                *failure_ordinal += 1;
                plan.obligations.push(Obligation {
                    id: format!("rule-failure.{}.{}", rule_name, failure_ordinal),
                    category: ObligationCategory::RuleFailure,
                    description: format!(
                        "Verify rule {} is rejected when requires clause fails",
                        rule_name
                    ),
                    source_construct: rule_name.to_string(),
                    expression: None,
                    source_span: SourceSpan { start: item.span.start, end: item.span.end },
                    detail: None,
                    dependencies: Some(deps.clone()),
                });
            }
            BlockItemKind::Clause { keyword, value } if keyword == "when" => {
                // Check for temporal triggers
                if contains_temporal(value) {
                    plan.obligations.push(Obligation {
                        id: format!("temporal.{}", rule_name),
                        category: ObligationCategory::Temporal,
                        description: format!(
                            "Verify temporal trigger in {} fires at deadline, not before, and does not re-fire",
                            rule_name
                        ),
                        source_construct: rule_name.to_string(),
                        expression: None,
                        source_span: SourceSpan { start: item.span.start, end: item.span.end },
                        detail: None,
                        dependencies: Some(deps.clone()),
                    });
                }
                // Check for entity creation triggers
                if contains_entity_creation(value) {
                    *creation_ordinal += 1;
                    plan.obligations.push(Obligation {
                        id: format!("rule-entity-creation.{}.{}", rule_name, creation_ordinal),
                        category: ObligationCategory::RuleEntityCreation,
                        description: format!(
                            "Verify entity creation in rule {} produces the specified fields",
                            rule_name
                        ),
                        source_construct: rule_name.to_string(),
                        expression: None,
                        source_span: SourceSpan { start: item.span.start, end: item.span.end },
                        detail: None,
                        dependencies: Some(deps.clone()),
                    });
                }
            }
            BlockItemKind::Clause { keyword, value } if keyword == "ensures" => {
                if contains_entity_creation(value) {
                    *creation_ordinal += 1;
                    plan.obligations.push(Obligation {
                        id: format!("rule-entity-creation.{}.{}", rule_name, creation_ordinal),
                        category: ObligationCategory::RuleEntityCreation,
                        description: format!(
                            "Verify entity creation in rule {} ensures clause produces the specified fields",
                            rule_name
                        ),
                        source_construct: rule_name.to_string(),
                        expression: None,
                        source_span: SourceSpan { start: item.span.start, end: item.span.end },
                        detail: None,
                        dependencies: Some(deps.clone()),
                    });
                }
            }
            BlockItemKind::ForBlock { items, .. } => {
                walk_rule_items_inner(plan, rule_name, items, deps, failure_ordinal, creation_ordinal);
            }
            BlockItemKind::IfBlock { branches, else_items } => {
                for branch in branches {
                    walk_rule_items_inner(plan, rule_name, &branch.items, deps, failure_ordinal, creation_ordinal);
                }
                if let Some(else_items) = else_items {
                    walk_rule_items_inner(plan, rule_name, else_items, deps, failure_ordinal, creation_ordinal);
                }
            }
            _ => {}
        }
    }
}

fn emit_surface_obligations(plan: &mut TestPlan, block: &BlockDecl) {
    let name = block_name(block);

    // Actor restriction
    plan.obligations.push(Obligation {
        id: format!("surface-actor.{}", name),
        category: ObligationCategory::SurfaceActor,
        description: format!("Verify surface {} is accessible only to the specified actor", name),
        source_construct: name.clone(),
        expression: None,
        source_span: SourceSpan { start: block.span.start, end: block.span.end },
        detail: None,
        dependencies: None,
    });

    for item in &block.items {
        match &item.kind {
            BlockItemKind::Clause { keyword, value } if keyword == "exposes" => {
                let items = collect_exposed_names(value);
                plan.obligations.push(Obligation {
                    id: format!("surface-exposure.{}", name),
                    category: ObligationCategory::SurfaceExposure,
                    description: format!("Verify each exposed item on {} is accessible", name),
                    source_construct: name.clone(),
                    expression: None,
                    source_span: SourceSpan { start: item.span.start, end: item.span.end },
                    detail: if items.is_empty() { None } else {
                        Some(ObligationDetail::Surface { surface: name.clone(), items })
                    },
                    dependencies: None,
                });
            }
            BlockItemKind::Clause { keyword, .. } if keyword == "provides" => {
                plan.obligations.push(Obligation {
                    id: format!("surface-provides.{}", name),
                    category: ObligationCategory::SurfaceProvides,
                    description: format!(
                        "Verify provided operations on {} appear/hide based on when conditions",
                        name
                    ),
                    source_construct: name.clone(),
                    expression: None,
                    source_span: SourceSpan { start: item.span.start, end: item.span.end },
                    detail: None,
                    dependencies: None,
                });
            }
            _ => {}
        }
    }
}

fn emit_config_obligations(plan: &mut TestPlan, block: &BlockDecl) {
    for item in &block.items {
        if let BlockItemKind::Assignment { name, .. } = &item.kind {
            plan.obligations.push(Obligation {
                id: format!("config-default.{}", name.name),
                category: ObligationCategory::ConfigDefault,
                description: format!("Verify config parameter {} has its declared default", name.name),
                source_construct: format!("config.{}", name.name),
                expression: None,
                source_span: SourceSpan { start: item.span.start, end: item.span.end },
                detail: None,
                dependencies: None,
            });
        }
    }
}

fn emit_contract_obligations(plan: &mut TestPlan, block: &BlockDecl) {
    let name = block_name(block);
    for item in &block.items {
        if let BlockItemKind::Assignment { name: sig_name, .. } = &item.kind {
            plan.obligations.push(Obligation {
                id: format!("contract-signature.{}.{}", name, sig_name.name),
                category: ObligationCategory::ContractSignature,
                description: format!(
                    "Verify implementation satisfies contract {}.{}",
                    name, sig_name.name
                ),
                source_construct: format!("{}.{}", name, sig_name.name),
                expression: None,
                source_span: SourceSpan { start: item.span.start, end: item.span.end },
                detail: None,
                dependencies: None,
            });
        }
    }
}

fn emit_variant_obligations(plan: &mut TestPlan, v: &VariantDecl) {
    plan.obligations.push(Obligation {
        id: format!("sum-type-variant.{}", v.name.name),
        category: ObligationCategory::SumTypeVariant,
        description: format!(
            "Verify variant {} has its specific fields accessible within a type guard",
            v.name.name
        ),
        source_construct: v.name.name.clone(),
        expression: None,
        source_span: SourceSpan { start: v.span.start, end: v.span.end },
        detail: None,
        dependencies: None,
    });
}

fn emit_invariant_obligation(plan: &mut TestPlan, inv: &InvariantDecl, source: &str) {
    plan.obligations.push(Obligation {
        id: format!("invariant.{}", inv.name.name),
        category: ObligationCategory::Invariant,
        description: format!(
            "Verify invariant {} holds after every state-changing rule that touches constrained entities",
            inv.name.name
        ),
        source_construct: inv.name.name.clone(),
        expression: Some(span_text(source, inv.body.span())),
        source_span: SourceSpan { start: inv.span.start, end: inv.span.end },
        detail: None,
        dependencies: None,
    });
}

// --- Helpers ---

fn span_text(source: &str, span: Span) -> String {
    source.get(span.start..span.end).unwrap_or("").to_string()
}

fn is_derived_expression(expr: &Expr) -> bool {
    matches!(
        expr,
        Expr::Comparison { .. }
            | Expr::LogicalOp { .. }
            | Expr::BinaryOp { .. }
            | Expr::Not { .. }
    )
}

fn contains_temporal(expr: &Expr) -> bool {
    match expr {
        Expr::Comparison { right, .. } => matches!(right.as_ref(), Expr::Now { .. }),
        Expr::Binding { value, .. } => contains_temporal(value),
        Expr::Block { items, .. } => items.iter().any(contains_temporal),
        _ => false,
    }
}

fn contains_entity_creation(expr: &Expr) -> bool {
    match expr {
        Expr::Call { function, .. } => {
            if let Expr::MemberAccess { field, .. } = function.as_ref() {
                field.name == "created"
            } else {
                false
            }
        }
        Expr::Block { items, .. } => items.iter().any(contains_entity_creation),
        _ => false,
    }
}

fn collect_exposed_names(expr: &Expr) -> Vec<String> {
    match expr {
        Expr::MemberAccess { field, .. } => vec![field.name.clone()],
        Expr::Block { items, .. } => items.iter().flat_map(collect_exposed_names).collect(),
        Expr::WhenGuard { action, .. } => collect_exposed_names(action),
        _ => Vec::new(),
    }
}

// --- Module context and dependency extraction ---

fn build_module_context(module: &Module) -> ModuleContext {
    let mut entity_names = std::collections::BTreeSet::new();
    let mut deferred_names = std::collections::BTreeSet::new();
    let mut emitted_triggers = std::collections::BTreeSet::new();

    for decl in &module.declarations {
        match decl {
            Decl::Block(block) => {
                match block.kind {
                    BlockKind::Entity | BlockKind::ExternalEntity => {
                        if let Some(name) = &block.name {
                            entity_names.insert(name.name.clone());
                        }
                    }
                    BlockKind::Rule => {
                        // Scan ensures clauses for trigger emissions
                        for item in &block.items {
                            if let BlockItemKind::Clause { keyword, value } = &item.kind {
                                if keyword == "ensures" {
                                    collect_trigger_emissions(value, &mut emitted_triggers);
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
            Decl::Deferred(d) => {
                if let Some(name) = deferred_leaf_name(&d.path) {
                    deferred_names.insert(name);
                }
            }
            _ => {}
        }
    }

    ModuleContext { entity_names, deferred_names, emitted_triggers }
}

/// Extract the leaf name from a deferred spec path expression.
fn deferred_leaf_name(expr: &Expr) -> Option<String> {
    match expr {
        Expr::Ident(id) => Some(id.name.clone()),
        Expr::MemberAccess { field, .. } => Some(field.name.clone()),
        Expr::QualifiedName(q) => Some(q.name.clone()),
        _ => None,
    }
}

/// Collect trigger emission names from an ensures expression.
/// A trigger emission is `TriggerName(args)` — a call with an uppercase bare ident function.
fn collect_trigger_emissions(expr: &Expr, out: &mut std::collections::BTreeSet<String>) {
    match expr {
        Expr::Call { function, args, .. } => {
            if let Expr::Ident(id) = function.as_ref() {
                if id.name.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
                    // Check it's not Entity.created() — bare Ident means it's a trigger, not a method
                    out.insert(id.name.clone());
                }
            }
            // Recurse into args
            for arg in args {
                match arg {
                    CallArg::Positional(e) => collect_trigger_emissions(e, out),
                    CallArg::Named(na) => collect_trigger_emissions(&na.value, out),
                }
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_trigger_emissions(item, out);
            }
        }
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_trigger_emissions(value, out);
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_trigger_emissions(&b.body, out);
            }
            if let Some(eb) = else_body {
                collect_trigger_emissions(eb, out);
            }
        }
        Expr::For { body, .. } => collect_trigger_emissions(body, out),
        _ => {}
    }
}

fn extract_rule_dependencies(block: &BlockDecl, ctx: &ModuleContext) -> RuleDependencies {
    let mut entities_read = std::collections::BTreeSet::new();
    let mut entities_written = std::collections::BTreeSet::new();
    let mut entities_created = std::collections::BTreeSet::new();
    let mut entities_removed = std::collections::BTreeSet::new();
    let mut deferred_specs = std::collections::BTreeSet::new();
    let mut trigger_emissions = std::collections::BTreeSet::new();
    let mut trigger_source = TriggerSource::External;

    for item in &block.items {
        if let BlockItemKind::Clause { keyword, value } = &item.kind {
            match keyword.as_str() {
                "when" => {
                    trigger_source = classify_trigger_source(value, &ctx.emitted_triggers);
                    collect_entity_refs(value, &ctx.entity_names, &mut entities_read);
                    collect_deferred_refs(value, &ctx.deferred_names, &mut deferred_specs);
                }
                "requires" => {
                    collect_entity_refs(value, &ctx.entity_names, &mut entities_read);
                    collect_deferred_refs(value, &ctx.deferred_names, &mut deferred_specs);
                }
                "ensures" => {
                    collect_written_entities(value, &ctx.entity_names, &mut entities_written);
                    collect_created_entities(value, &mut entities_created);
                    collect_removed_entities(value, &mut entities_removed);
                    collect_ensures_trigger_emissions(value, &ctx.entity_names, &mut trigger_emissions);
                    collect_deferred_refs(value, &ctx.deferred_names, &mut deferred_specs);
                }
                _ => {}
            }
        }
        // Also walk ForBlock/IfBlock items for nested clauses
        if let BlockItemKind::ForBlock { items, .. } = &item.kind {
            extract_deps_from_items(items, ctx, &mut entities_read, &mut entities_written,
                &mut entities_created, &mut entities_removed, &mut deferred_specs, &mut trigger_emissions);
        }
        if let BlockItemKind::IfBlock { branches, else_items } = &item.kind {
            for branch in branches {
                extract_deps_from_items(&branch.items, ctx, &mut entities_read, &mut entities_written,
                    &mut entities_created, &mut entities_removed, &mut deferred_specs, &mut trigger_emissions);
            }
            if let Some(items) = else_items {
                extract_deps_from_items(items, ctx, &mut entities_read, &mut entities_written,
                    &mut entities_created, &mut entities_removed, &mut deferred_specs, &mut trigger_emissions);
            }
        }
    }

    RuleDependencies {
        entities_read: entities_read.into_iter().collect(),
        entities_written: entities_written.into_iter().collect(),
        entities_created: entities_created.into_iter().collect(),
        entities_removed: entities_removed.into_iter().collect(),
        deferred_specs: deferred_specs.into_iter().collect(),
        trigger_emissions: trigger_emissions.into_iter().collect(),
        trigger_source,
    }
}

fn extract_deps_from_items(
    items: &[BlockItem],
    ctx: &ModuleContext,
    entities_read: &mut std::collections::BTreeSet<String>,
    entities_written: &mut std::collections::BTreeSet<String>,
    entities_created: &mut std::collections::BTreeSet<String>,
    entities_removed: &mut std::collections::BTreeSet<String>,
    deferred_specs: &mut std::collections::BTreeSet<String>,
    trigger_emissions: &mut std::collections::BTreeSet<String>,
) {
    for item in items {
        if let BlockItemKind::Clause { keyword, value } = &item.kind {
            match keyword.as_str() {
                "requires" => {
                    collect_entity_refs(value, &ctx.entity_names, entities_read);
                    collect_deferred_refs(value, &ctx.deferred_names, deferred_specs);
                }
                "ensures" => {
                    collect_written_entities(value, &ctx.entity_names, entities_written);
                    collect_created_entities(value, entities_created);
                    collect_removed_entities(value, entities_removed);
                    collect_ensures_trigger_emissions(value, &ctx.entity_names, trigger_emissions);
                    collect_deferred_refs(value, &ctx.deferred_names, deferred_specs);
                }
                _ => {}
            }
        }
        if let BlockItemKind::ForBlock { items: nested, .. } = &item.kind {
            extract_deps_from_items(nested, ctx, entities_read, entities_written,
                entities_created, entities_removed, deferred_specs, trigger_emissions);
        }
        if let BlockItemKind::IfBlock { branches, else_items } = &item.kind {
            for branch in branches {
                extract_deps_from_items(&branch.items, ctx, entities_read, entities_written,
                    entities_created, entities_removed, deferred_specs, trigger_emissions);
            }
            if let Some(nested) = else_items {
                extract_deps_from_items(nested, ctx, entities_read, entities_written,
                    entities_created, entities_removed, deferred_specs, trigger_emissions);
            }
        }
    }
}

/// Classify how a rule is triggered from its when clause.
fn classify_trigger_source(
    expr: &Expr,
    emitted_triggers: &std::collections::BTreeSet<String>,
) -> TriggerSource {
    match expr {
        Expr::TransitionsTo { .. } | Expr::Becomes { .. } => TriggerSource::StateTransition,
        Expr::Binding { value, .. } => classify_trigger_source(value, emitted_triggers),
        Expr::Block { items, .. } => {
            // Use the first classifiable item
            for item in items {
                let ts = classify_trigger_source(item, emitted_triggers);
                if !matches!(ts, TriggerSource::External) {
                    return ts;
                }
            }
            TriggerSource::External
        }
        Expr::Comparison { right, .. } => {
            if matches!(right.as_ref(), Expr::Now { .. }) {
                TriggerSource::Temporal
            } else {
                TriggerSource::External
            }
        }
        Expr::Call { function, .. } => {
            match function.as_ref() {
                Expr::MemberAccess { field, .. } if field.name == "created" => {
                    TriggerSource::Creation
                }
                Expr::Ident(id) => {
                    if emitted_triggers.contains(&id.name) {
                        TriggerSource::Chained
                    } else {
                        TriggerSource::External
                    }
                }
                _ => TriggerSource::External,
            }
        }
        _ => TriggerSource::External,
    }
}

/// Collect entity references from an expression, resolving lowercase variables
/// by capitalising and checking against known entity names.
fn collect_entity_refs(
    expr: &Expr,
    entity_names: &std::collections::BTreeSet<String>,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        Expr::Ident(id) => {
            if entity_names.contains(&id.name) {
                out.insert(id.name.clone());
            }
        }
        Expr::MemberAccess { object, .. } | Expr::OptionalAccess { object, .. } => {
            if let Expr::Ident(id) = object.as_ref() {
                if entity_names.contains(&id.name) {
                    out.insert(id.name.clone());
                } else {
                    let cap = capitalize_first(&id.name);
                    if entity_names.contains(&cap) {
                        out.insert(cap);
                    }
                }
            } else {
                collect_entity_refs(object, entity_names, out);
            }
        }
        Expr::QualifiedName(q) => {
            if entity_names.contains(&q.name) {
                out.insert(q.name.clone());
            }
        }
        Expr::Call { function, args, .. } => {
            // Don't treat bare function names as entity refs (they're trigger names).
            // But do recurse into MemberAccess functions (Entity.method) and args.
            if let Expr::MemberAccess { object, .. } = function.as_ref() {
                collect_entity_refs(object, entity_names, out);
            }
            for arg in args {
                match arg {
                    CallArg::Positional(e) => collect_entity_refs(e, entity_names, out),
                    CallArg::Named(na) => collect_entity_refs(&na.value, entity_names, out),
                }
            }
        }
        Expr::JoinLookup { entity, fields, .. } => {
            collect_entity_refs(entity, entity_names, out);
            for f in fields {
                if let Some(v) = &f.value {
                    collect_entity_refs(v, entity_names, out);
                }
            }
        }
        // Two-child nodes
        Expr::BinaryOp { left, right, .. }
        | Expr::Comparison { left, right, .. }
        | Expr::LogicalOp { left, right, .. }
        | Expr::In { element: left, collection: right, .. }
        | Expr::NotIn { element: left, collection: right, .. }
        | Expr::NullCoalesce { left, right, .. }
        | Expr::Where { source: left, condition: right, .. }
        | Expr::With { source: left, predicate: right, .. }
        | Expr::Pipe { left, right, .. }
        | Expr::TransitionsTo { subject: left, new_state: right, .. }
        | Expr::Becomes { subject: left, new_state: right, .. }
        | Expr::WhenGuard { action: left, condition: right, .. } => {
            collect_entity_refs(left, entity_names, out);
            collect_entity_refs(right, entity_names, out);
        }
        // One-child nodes
        Expr::Not { operand, .. }
        | Expr::Exists { operand, .. }
        | Expr::NotExists { operand, .. }
        | Expr::TypeOptional { inner: operand, .. } => {
            collect_entity_refs(operand, entity_names, out);
        }
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_entity_refs(value, entity_names, out);
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_entity_refs(item, entity_names, out);
            }
        }
        Expr::SetLiteral { elements, .. } => {
            for e in elements {
                collect_entity_refs(e, entity_names, out);
            }
        }
        Expr::For { collection, filter, body, .. } => {
            collect_entity_refs(collection, entity_names, out);
            if let Some(f) = filter {
                collect_entity_refs(f, entity_names, out);
            }
            collect_entity_refs(body, entity_names, out);
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_entity_refs(&b.condition, entity_names, out);
                collect_entity_refs(&b.body, entity_names, out);
            }
            if let Some(eb) = else_body {
                collect_entity_refs(eb, entity_names, out);
            }
        }
        Expr::Lambda { body, .. } | Expr::ProjectionMap { source: body, .. } => {
            collect_entity_refs(body, entity_names, out);
        }
        Expr::GenericType { name, args, .. } => {
            collect_entity_refs(name, entity_names, out);
            for a in args {
                collect_entity_refs(a, entity_names, out);
            }
        }
        Expr::ObjectLiteral { fields, .. } => {
            for f in fields {
                collect_entity_refs(&f.value, entity_names, out);
            }
        }
        _ => {}
    }
}

/// Collect entities that are written (field assignments, state transitions) in ensures.
fn collect_written_entities(
    expr: &Expr,
    entity_names: &std::collections::BTreeSet<String>,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        // `entity.field = value` or `entity.field transitions_to state`
        Expr::Comparison { left, op: ComparisonOp::Eq, .. } => {
            if let Some(name) = resolve_entity_from_member(left, entity_names) {
                out.insert(name);
            }
        }
        Expr::TransitionsTo { subject, .. } | Expr::Becomes { subject, .. } => {
            if let Some(name) = resolve_entity_from_member(subject, entity_names) {
                out.insert(name);
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_written_entities(item, entity_names, out);
            }
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_written_entities(&b.body, entity_names, out);
            }
            if let Some(eb) = else_body {
                collect_written_entities(eb, entity_names, out);
            }
        }
        Expr::For { body, .. } => collect_written_entities(body, entity_names, out),
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_written_entities(value, entity_names, out);
        }
        _ => {}
    }
}

/// Collect entities created via `.created()` calls.
/// Reports any uppercase identifier before `.created()`, whether or not it is
/// a declared entity — undeclared entities being created are still dependencies.
fn collect_created_entities(
    expr: &Expr,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        Expr::Call { function, args, .. } => {
            if let Expr::MemberAccess { object, field, .. } = function.as_ref() {
                if field.name == "created" {
                    if let Expr::Ident(id) = object.as_ref() {
                        if id.name.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
                            out.insert(id.name.clone());
                        }
                    }
                }
            }
            for arg in args {
                match arg {
                    CallArg::Positional(e) => collect_created_entities(e, out),
                    CallArg::Named(na) => collect_created_entities(&na.value, out),
                }
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_created_entities(item, out);
            }
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_created_entities(&b.body, out);
            }
            if let Some(eb) = else_body {
                collect_created_entities(eb, out);
            }
        }
        Expr::For { body, .. } => collect_created_entities(body, out),
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_created_entities(value, out);
        }
        _ => {}
    }
}

/// Collect entities removed via `.remove()` or `remove` calls.
fn collect_removed_entities(
    expr: &Expr,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        Expr::Call { function, .. } => {
            if let Expr::MemberAccess { object, field, .. } = function.as_ref() {
                if field.name == "remove" || field.name == "removed" {
                    if let Expr::Ident(id) = object.as_ref() {
                        if id.name.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
                            out.insert(id.name.clone());
                        }
                    }
                }
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_removed_entities(item, out);
            }
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_removed_entities(&b.body, out);
            }
            if let Some(eb) = else_body {
                collect_removed_entities(eb, out);
            }
        }
        Expr::For { body, .. } => collect_removed_entities(body, out),
        _ => {}
    }
}

/// Collect trigger emission names from an ensures clause.
/// These are `Call { function: Ident(UppercaseName) }` that are NOT known entities.
fn collect_ensures_trigger_emissions(
    expr: &Expr,
    entity_names: &std::collections::BTreeSet<String>,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        Expr::Call { function, args, .. } => {
            if let Expr::Ident(id) = function.as_ref() {
                if id.name.chars().next().map(|c| c.is_uppercase()).unwrap_or(false)
                    && !entity_names.contains(&id.name)
                {
                    out.insert(id.name.clone());
                }
            }
            for arg in args {
                match arg {
                    CallArg::Positional(e) => collect_ensures_trigger_emissions(e, entity_names, out),
                    CallArg::Named(na) => collect_ensures_trigger_emissions(&na.value, entity_names, out),
                }
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_ensures_trigger_emissions(item, entity_names, out);
            }
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_ensures_trigger_emissions(&b.body, entity_names, out);
            }
            if let Some(eb) = else_body {
                collect_ensures_trigger_emissions(eb, entity_names, out);
            }
        }
        Expr::For { body, .. } => collect_ensures_trigger_emissions(body, entity_names, out),
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_ensures_trigger_emissions(value, entity_names, out);
        }
        _ => {}
    }
}

/// Collect deferred spec references from an expression.
/// These are `Call { function: Ident(name) }` where name matches a known deferred spec.
fn collect_deferred_refs(
    expr: &Expr,
    deferred_names: &std::collections::BTreeSet<String>,
    out: &mut std::collections::BTreeSet<String>,
) {
    match expr {
        Expr::Call { function, args, .. } => {
            if let Expr::Ident(id) = function.as_ref() {
                if deferred_names.contains(&id.name) {
                    out.insert(id.name.clone());
                }
            }
            for arg in args {
                match arg {
                    CallArg::Positional(e) => collect_deferred_refs(e, deferred_names, out),
                    CallArg::Named(na) => collect_deferred_refs(&na.value, deferred_names, out),
                }
            }
        }
        Expr::Ident(id) => {
            if deferred_names.contains(&id.name) {
                out.insert(id.name.clone());
            }
        }
        Expr::Block { items, .. } => {
            for item in items {
                collect_deferred_refs(item, deferred_names, out);
            }
        }
        Expr::Binding { value, .. } | Expr::LetExpr { value, .. } => {
            collect_deferred_refs(value, deferred_names, out);
        }
        Expr::Conditional { branches, else_body, .. } => {
            for b in branches {
                collect_deferred_refs(&b.condition, deferred_names, out);
                collect_deferred_refs(&b.body, deferred_names, out);
            }
            if let Some(eb) = else_body {
                collect_deferred_refs(eb, deferred_names, out);
            }
        }
        Expr::For { collection, filter, body, .. } => {
            collect_deferred_refs(collection, deferred_names, out);
            if let Some(f) = filter {
                collect_deferred_refs(f, deferred_names, out);
            }
            collect_deferred_refs(body, deferred_names, out);
        }
        Expr::BinaryOp { left, right, .. }
        | Expr::Comparison { left, right, .. }
        | Expr::LogicalOp { left, right, .. } => {
            collect_deferred_refs(left, deferred_names, out);
            collect_deferred_refs(right, deferred_names, out);
        }
        Expr::Not { operand, .. } => collect_deferred_refs(operand, deferred_names, out),
        Expr::MemberAccess { object, .. } => collect_deferred_refs(object, deferred_names, out),
        _ => {}
    }
}

/// Resolve a MemberAccess expression to an entity name.
/// Handles both `Entity.field` (direct) and `var.field` (capitalise variable name).
fn resolve_entity_from_member(
    expr: &Expr,
    entity_names: &std::collections::BTreeSet<String>,
) -> Option<String> {
    if let Expr::MemberAccess { object, .. } = expr {
        if let Expr::Ident(id) = object.as_ref() {
            if entity_names.contains(&id.name) {
                return Some(id.name.clone());
            }
            let cap = capitalize_first(&id.name);
            if entity_names.contains(&cap) {
                return Some(cap);
            }
        }
    }
    None
}

fn capitalize_first(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

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

    fn parse_plan(source: &str) -> TestPlan {
        let result = allium_parser::parse(source);
        generate_test_plan(&result.module, source)
    }

    fn to_json(plan: &TestPlan) -> serde_json::Value {
        serde_json::to_value(plan).unwrap()
    }

    fn find_obligation<'a>(plan: &'a TestPlan, id_substring: &str) -> &'a Obligation {
        plan.obligations.iter().find(|o| o.id.contains(id_substring))
            .unwrap_or_else(|| panic!("no obligation matching '{}'; have: {:?}",
                id_substring,
                plan.obligations.iter().map(|o| &o.id).collect::<Vec<_>>()))
    }

    fn obligations_matching<'a>(plan: &'a TestPlan, id_substring: &str) -> Vec<&'a Obligation> {
        plan.obligations.iter().filter(|o| o.id.contains(id_substring)).collect()
    }

    // --- Version passthrough ---

    #[test]
    fn version_passed_through() {
        let plan = parse_plan("-- allium: 3\nentity Foo { x: Integer }");
        assert_eq!(plan.version, Some(3));
    }

    #[test]
    fn version_none_when_absent() {
        let plan = parse_plan("entity Foo { x: Integer }");
        assert_eq!(plan.version, None);
    }

    // --- Entity field obligations ---

    #[test]
    fn entity_fields_obligation_lists_fields() {
        let source = "-- allium: 3\nentity Order {\n  name: String\n  total: Integer\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Order");
        assert!(matches!(ob.category, ObligationCategory::EntityFields));
        if let Some(ObligationDetail::Fields { fields }) = &ob.detail {
            assert_eq!(fields, &["name", "total"]);
        } else {
            panic!("expected Fields detail");
        }
    }

    #[test]
    fn entity_fields_source_construct() {
        let source = "-- allium: 3\nentity Widget { size: Integer }";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Widget");
        assert_eq!(ob.source_construct, "Widget");
    }

    #[test]
    fn empty_entity_no_fields_obligation() {
        let plan = parse_plan("-- allium: 3\nentity Empty {}");
        let matching = obligations_matching(&plan, "entity-fields.Empty");
        assert!(matching.is_empty());
    }

    // --- Optional field obligations ---

    #[test]
    fn optional_field_obligation() {
        let source = "-- allium: 3\nentity User {\n  bio: String?\n  name: String\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-optional.User.bio");
        assert!(matches!(ob.category, ObligationCategory::EntityOptional));
        assert!(ob.description.contains("bio"));
    }

    #[test]
    fn non_optional_field_no_optional_obligation() {
        let source = "-- allium: 3\nentity User { name: String }";
        let plan = parse_plan(source);
        let matching = obligations_matching(&plan, "entity-optional");
        assert!(matching.is_empty());
    }

    // --- Relationship obligations ---

    #[test]
    fn relationship_obligation() {
        let source = "-- allium: 3\nentity Order { items: OrderItem with order }";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-relationship.Order.items");
        assert!(matches!(ob.category, ObligationCategory::EntityRelationship));
    }

    // --- Projection obligations ---

    #[test]
    fn projection_obligation() {
        let source = "-- allium: 3\nentity Order {\n  items: OrderItem with order\n  active_items: items where active = true\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "projection.Order.active_items");
        assert!(matches!(ob.category, ObligationCategory::Projection));
    }

    // --- Derived value obligations ---

    #[test]
    fn derived_value_obligation() {
        let source = "-- allium: 3\nentity Order {\n  a: Integer\n  b: Integer\n  total: a + b\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "derived.Order.total");
        assert!(matches!(ob.category, ObligationCategory::Derived));
        assert!(ob.expression.is_none());
    }

    // --- Transition obligations ---

    #[test]
    fn transition_edge_obligations() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | shipped | done\n  transitions status {\n    pending -> shipped\n    shipped -> done\n    terminal: done\n  }\n}";
        let plan = parse_plan(source);
        let edge1 = find_obligation(&plan, "transition-edge.Order.pending.shipped");
        assert!(matches!(edge1.category, ObligationCategory::TransitionEdge));
        if let Some(ObligationDetail::Transition { from, to, .. }) = &edge1.detail {
            assert_eq!(from, "pending");
            assert_eq!(to, "shipped");
        } else {
            panic!("expected Transition detail");
        }
        find_obligation(&plan, "transition-edge.Order.shipped.done");
    }

    #[test]
    fn transition_rejected_obligation() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | done\n  transitions status {\n    pending -> done\n    terminal: done\n  }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "transition-rejected.Order.status");
        assert!(matches!(ob.category, ObligationCategory::TransitionRejected));
    }

    #[test]
    fn transition_terminal_obligation() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | done\n  transitions status {\n    pending -> done\n    terminal: done\n  }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "transition-terminal.Order.status");
        assert!(matches!(ob.category, ObligationCategory::TransitionTerminal));
        if let Some(ObligationDetail::Terminal { states, .. }) = &ob.detail {
            assert_eq!(states, &["done"]);
        } else {
            panic!("expected Terminal detail");
        }
    }

    #[test]
    fn no_terminal_obligation_when_no_terminal_states() {
        let source = "-- allium: 3\nentity Order {\n  status: a | b\n  transitions status {\n    a -> b\n    b -> a\n  }\n}";
        let plan = parse_plan(source);
        let matching = obligations_matching(&plan, "transition-terminal");
        assert!(matching.is_empty());
    }

    // --- When field presence ---

    #[test]
    fn when_field_presence_obligation() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | shipped\n  tracking: String when status = shipped\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-presence.Order.tracking");
        assert!(matches!(ob.category, ObligationCategory::WhenPresence));
        if let Some(ObligationDetail::WhenFieldPresence { entity, field, status_field, qualifying_states }) = &ob.detail {
            assert_eq!(entity, "Order");
            assert_eq!(field, "tracking");
            assert_eq!(status_field, "status");
            assert_eq!(qualifying_states, &["shipped"]);
        } else {
            panic!("expected WhenFieldPresence detail");
        }
    }

    // --- Value type obligations ---

    #[test]
    fn value_type_equality_obligation() {
        let source = "-- allium: 3\nvalue Address {\n  street: String\n  city: String\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "value-equality.Address");
        assert!(matches!(ob.category, ObligationCategory::ValueEquality));
    }

    #[test]
    fn value_type_fields_obligation() {
        let source = "-- allium: 3\nvalue Address {\n  street: String\n  city: String\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Address");
        if let Some(ObligationDetail::Fields { fields }) = &ob.detail {
            assert_eq!(fields, &["street", "city"]);
        } else {
            panic!("expected Fields detail");
        }
    }

    // --- Enum obligations ---

    #[test]
    fn enum_comparable_obligation() {
        let source = "-- allium: 3\nenum Colour {\n  red\n  green\n  blue\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "enum-comparable.Colour");
        assert!(matches!(ob.category, ObligationCategory::EnumComparable));
    }

    // --- Rule obligations ---

    #[test]
    fn rule_success_obligation() {
        let source = "-- allium: 3\nrule DoThing {\n  requires: x = 1\n  ensures: x = 2\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-success.DoThing");
        assert!(matches!(ob.category, ObligationCategory::RuleSuccess));
        assert!(ob.expression.is_none());
    }

    #[test]
    fn rule_failure_obligation_from_requires() {
        let source = "-- allium: 3\nrule DoThing {\n  requires: x = 1\n  ensures: x = 2\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-failure.DoThing.1");
        assert!(matches!(ob.category, ObligationCategory::RuleFailure));
    }

    #[test]
    fn rule_without_requires_no_failure_obligation() {
        let source = "-- allium: 3\nrule DoThing {\n  ensures: x = 2\n}";
        let plan = parse_plan(source);
        let matching = obligations_matching(&plan, "rule-failure");
        assert!(matching.is_empty());
    }

    // --- Surface obligations ---

    #[test]
    fn surface_actor_obligation() {
        let source = "-- allium: 3\nsurface Dashboard {\n  facing: admin\n  exposes: Order.status\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "surface-actor.Dashboard");
        assert!(matches!(ob.category, ObligationCategory::SurfaceActor));
    }

    #[test]
    fn surface_exposure_obligation() {
        let source = "-- allium: 3\nsurface Dashboard {\n  facing: admin\n  exposes: Order.status\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "surface-exposure.Dashboard");
        assert!(matches!(ob.category, ObligationCategory::SurfaceExposure));
    }

    #[test]
    fn surface_provides_obligation() {
        let source = "-- allium: 3\nsurface Dashboard {\n  facing: admin\n  provides: Order.cancel\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "surface-provides.Dashboard");
        assert!(matches!(ob.category, ObligationCategory::SurfaceProvides));
    }

    // --- Config obligations ---

    #[test]
    fn config_default_obligation() {
        let source = "-- allium: 3\nconfig {\n  max_retries: Integer = 3\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "config-default.max_retries");
        assert!(matches!(ob.category, ObligationCategory::ConfigDefault));
        assert_eq!(ob.source_construct, "config.max_retries");
    }

    #[test]
    fn config_multiple_params() {
        let source = "-- allium: 3\nconfig {\n  max_retries: Integer = 3\n  batch_size: Integer = 10\n}";
        let plan = parse_plan(source);
        find_obligation(&plan, "config-default.max_retries");
        find_obligation(&plan, "config-default.batch_size");
    }

    // --- Contract obligations ---

    #[test]
    fn contract_signature_obligation() {
        let source = "-- allium: 3\ncontract PaymentGateway {\n  charge: Amount -> Result\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "contract-signature.PaymentGateway.charge");
        assert!(matches!(ob.category, ObligationCategory::ContractSignature));
        assert_eq!(ob.source_construct, "PaymentGateway.charge");
    }

    // --- Variant obligations ---

    #[test]
    fn variant_obligation() {
        let source = "-- allium: 3\nvariant NetworkError : Error {\n  code: Integer\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "sum-type-variant.NetworkError");
        assert!(matches!(ob.category, ObligationCategory::SumTypeVariant));
    }

    // --- Entity-level invariant obligations ---

    #[test]
    fn entity_invariant_obligation_includes_expression() {
        let source = "-- allium: 3\nentity Order {\n  total: Integer\n  invariant NonNeg { this.total >= 0 }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.Order.NonNeg");
        assert_eq!(ob.expression.as_deref(), Some("this.total >= 0"));
    }

    #[test]
    fn entity_invariant_obligation_category_and_description() {
        let source = "-- allium: 3\nentity Foo {\n  x: Integer\n  invariant XPos { this.x >= 0 }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.Foo.XPos");
        assert!(matches!(ob.category, ObligationCategory::Invariant));
        assert!(ob.description.contains("XPos"));
        assert!(ob.description.contains("Foo"));
    }

    #[test]
    fn entity_invariant_source_construct() {
        let source = "-- allium: 3\nentity Foo {\n  x: Integer\n  invariant XPos { this.x >= 0 }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.Foo.XPos");
        assert_eq!(ob.source_construct, "Foo.XPos");
    }

    // --- Top-level invariant obligations ---

    #[test]
    fn top_level_invariant_obligation_includes_expression() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | done\n}\ninvariant AllDone {\n  for o in Orders: o.status = done\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.AllDone");
        assert!(ob.expression.is_some());
        let expr = ob.expression.as_deref().unwrap();
        assert!(expr.contains("for o in Orders"), "expression was: {}", expr);
    }

    #[test]
    fn top_level_invariant_complex_expression_preserved() {
        let source = "-- allium: 3\nentity Account {\n  balance: Decimal\n}\ninvariant AllSolvent {\n  for a in Accounts: a.balance >= 0\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.AllSolvent");
        let expr = ob.expression.as_deref().unwrap();
        assert!(expr.contains("a.balance >= 0"), "expression was: {}", expr);
    }

    #[test]
    fn top_level_invariant_source_construct() {
        let source = "-- allium: 3\ninvariant GlobalCheck {\n  true\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "invariant.GlobalCheck");
        assert_eq!(ob.source_construct, "GlobalCheck");
    }

    // --- Expression field absent on non-invariant obligations ---

    #[test]
    fn entity_fields_obligation_omits_expression() {
        let source = "-- allium: 3\nentity Order {\n  total: Integer\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Order");
        assert!(ob.expression.is_none());
    }

    #[test]
    fn expression_omitted_from_json_when_none() {
        let source = "-- allium: 3\nentity Order {\n  total: Integer\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap().contains("entity-fields"))
            .unwrap();
        assert!(ob.get("expression").is_none(), "expression key should be absent from JSON");
    }

    #[test]
    fn expression_present_in_json_for_invariant() {
        let source = "-- allium: 3\nentity Foo {\n  x: Integer\n  invariant XPos { this.x >= 0 }\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap().contains("invariant."))
            .unwrap();
        assert_eq!(ob["expression"].as_str().unwrap(), "this.x >= 0");
    }

    #[test]
    fn rule_obligation_omits_expression() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | done\n}\nrule Confirm {\n  requires: status = pending\n  ensures: status = done\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-success.Confirm");
        assert!(ob.expression.is_none());
    }

    // --- Multiple invariants on one entity ---

    #[test]
    fn multiple_entity_invariants_each_have_expression() {
        let source = "-- allium: 3\nentity Gauge {\n  level: Integer\n  invariant MinLevel { this.level >= 0 }\n  invariant MaxLevel { this.level <= 100 }\n}";
        let plan = parse_plan(source);
        let min = find_obligation(&plan, "MinLevel");
        let max = find_obligation(&plan, "MaxLevel");
        assert_eq!(min.expression.as_deref(), Some("this.level >= 0"));
        assert_eq!(max.expression.as_deref(), Some("this.level <= 100"));
    }

    // --- Source spans are non-zero ---

    #[test]
    fn obligation_source_spans_are_nonzero() {
        let source = "-- allium: 3\nentity Order {\n  total: Integer\n  invariant NonNeg { this.total >= 0 }\n}";
        let plan = parse_plan(source);
        for ob in &plan.obligations {
            assert!(ob.source_span.end > ob.source_span.start, "obligation {} has zero-width span", ob.id);
        }
    }

    // --- Temporal trigger obligations ---

    #[test]
    fn temporal_trigger_obligation() {
        let source = "-- allium: 3\nrule Timeout {\n  when: o: Order.created_at + 48.hours <= now\n  ensures: o.status = cancelled\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "temporal.Timeout");
        assert!(matches!(ob.category, ObligationCategory::Temporal));
    }

    // --- Entity creation obligations ---

    #[test]
    fn entity_creation_obligation_from_ensures() {
        let source = "-- allium: 3\nrule Notify {\n  when: order: Order.status transitions_to shipped\n  ensures: Email.created(to: order.customer.email)\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-entity-creation.Notify.1");
        assert!(matches!(ob.category, ObligationCategory::RuleEntityCreation));
    }

    // --- When crossing obligations ---

    #[test]
    fn when_crossing_presence_obligation() {
        let source = "-- allium: 3\n\
            entity Order {\n  status: pending | shipped\n  tracking: String when status = shipped\n  \
            transitions status {\n    pending -> shipped\n    terminal: shipped\n  }\n}\n\
            rule Ship {\n  when: Ship(order)\n  requires: order.status = pending\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-set.Ship.Order.tracking");
        assert!(matches!(ob.category, ObligationCategory::WhenSet));
        if let Some(ObligationDetail::WhenPresence { rule, entity, field, source_state, target_state, qualifying_states }) = &ob.detail {
            assert_eq!(rule, "Ship");
            assert_eq!(entity, "Order");
            assert_eq!(field, "tracking");
            assert_eq!(source_state, "pending");
            assert_eq!(target_state, "shipped");
            assert_eq!(qualifying_states, &["shipped"]);
        } else {
            panic!("expected WhenPresence detail");
        }
    }

    #[test]
    fn when_crossing_absence_obligation() {
        let source = "-- allium: 3\n\
            entity Order {\n  status: active | shipped | cancelled\n  tracking: String when status = shipped\n  \
            transitions status {\n    active -> shipped\n    shipped -> cancelled\n    terminal: cancelled\n  }\n}\n\
            rule Cancel {\n  when: Cancel(order)\n  requires: order.status = shipped\n  ensures: order.status = cancelled\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-clear.Cancel.Order.tracking");
        assert!(matches!(ob.category, ObligationCategory::WhenClear));
        if let Some(ObligationDetail::WhenAbsence { rule, entity, field, source_state, target_state, .. }) = &ob.detail {
            assert_eq!(rule, "Cancel");
            assert_eq!(entity, "Order");
            assert_eq!(field, "tracking");
            assert_eq!(source_state, "shipped");
            assert_eq!(target_state, "cancelled");
        } else {
            panic!("expected WhenAbsence detail");
        }
    }

    // --- External entity ---

    #[test]
    fn external_entity_emits_field_obligations() {
        let source = "-- allium: 3\nexternal entity Customer {\n  email: String\n  name: String\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Customer");
        if let Some(ObligationDetail::Fields { fields }) = &ob.detail {
            assert_eq!(fields, &["email", "name"]);
        } else {
            panic!("expected Fields detail");
        }
    }

    // --- Surface exposes detail ---

    #[test]
    fn surface_exposure_detail_lists_items() {
        let source = "-- allium: 3\nsurface Dashboard {\n  facing: admin\n  exposes:\n    Order.status\n    Order.tracking_number\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "surface-exposure.Dashboard");
        if let Some(ObligationDetail::Surface { surface, items }) = &ob.detail {
            assert_eq!(surface, "Dashboard");
            assert_eq!(items, &["status", "tracking_number"]);
        } else {
            panic!("expected Surface detail, got {:?}", ob.detail);
        }
    }

    // --- Multiple when-qualifying states ---

    #[test]
    fn when_field_multiple_qualifying_states() {
        let source = "-- allium: 3\nentity Order {\n  status: pending | shipped | delivered\n  tracking: String when status = shipped | delivered\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-presence.Order.tracking");
        if let Some(ObligationDetail::WhenFieldPresence { qualifying_states, .. }) = &ob.detail {
            assert_eq!(qualifying_states, &["shipped", "delivered"]);
        } else {
            panic!("expected WhenFieldPresence detail");
        }
    }

    // --- For-block and if-block recursion in rules ---

    #[test]
    fn for_block_entity_creation_in_rule() {
        let source = "-- allium: 3\nrule NotifyAll {\n  when: NotifyAll(orders)\n  for order in orders:\n    ensures: Email.created(to: order.email)\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-entity-creation.NotifyAll.1");
        assert!(matches!(ob.category, ObligationCategory::RuleEntityCreation));
    }

    #[test]
    fn if_block_entity_creation_in_rule() {
        let source = "-- allium: 3\nrule Process {\n  when: Process(order, express)\n  if express:\n    ensures: Express.created(order: order)\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-entity-creation.Process.1");
        assert!(matches!(ob.category, ObligationCategory::RuleEntityCreation));
    }

    // --- When-crossing: no requires (conservative fallback) ---

    #[test]
    fn when_crossing_presence_without_requires() {
        let source = "-- allium: 3\n\
            entity Order {\n  status: pending | shipped\n  tracking: String when status = shipped\n  \
            transitions status {\n    pending -> shipped\n    terminal: shipped\n  }\n}\n\
            rule AutoShip {\n  when: AutoShip(order)\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-set.AutoShip.Order.tracking");
        assert!(matches!(ob.category, ObligationCategory::WhenSet));
        if let Some(ObligationDetail::WhenPresence { source_state, .. }) = &ob.detail {
            assert_eq!(source_state, "unknown");
        } else {
            panic!("expected WhenPresence detail");
        }
    }

    // --- When-crossing: requires with in-set syntax ---

    #[test]
    fn when_crossing_with_in_set_requires() {
        let source = "-- allium: 3\n\
            entity Order {\n  status: pending | confirmed | shipped\n  tracking: String when status = shipped\n  \
            transitions status {\n    pending -> shipped\n    confirmed -> shipped\n    terminal: shipped\n  }\n}\n\
            rule Ship {\n  when: Ship(order)\n  requires: order.status in {pending, confirmed}\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "when-set.Ship.Order.tracking");
        assert!(matches!(ob.category, ObligationCategory::WhenSet));
    }

    // --- Transition rejected: verify source_construct ---

    #[test]
    fn transition_rejected_describes_field() {
        let source = "-- allium: 3\nentity Order {\n  status: a | b\n  transitions status {\n    a -> b\n  }\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "transition-rejected.Order.status");
        assert_eq!(ob.source_construct, "Order.status");
        assert!(ob.description.contains("Order") && ob.description.contains("status"));
    }

    // --- Multiple requires clauses ---

    #[test]
    fn multiple_requires_produce_multiple_failure_obligations() {
        let source = "-- allium: 3\nrule DoThing {\n  requires: x = 1\n  requires: y = 2\n  ensures: z = 3\n}";
        let plan = parse_plan(source);
        let failures = obligations_matching(&plan, "rule-failure.DoThing");
        assert_eq!(failures.len(), 2);
    }

    // --- Entity creation inside multi-line ensures block ---

    #[test]
    fn entity_creation_in_block_ensures() {
        let source = "-- allium: 3\nrule Notify {\n  when: Notify(order)\n  ensures:\n    order.status = shipped\n    Email.created(to: order.email)\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-entity-creation.Notify.1");
        assert!(matches!(ob.category, ObligationCategory::RuleEntityCreation));
    }

    // --- Empty config ---

    #[test]
    fn empty_config_no_obligations() {
        let plan = parse_plan("-- allium: 3\nconfig {}");
        let matching = obligations_matching(&plan, "config-default");
        assert!(matching.is_empty());
    }

    // --- ParamAssignment excluded from fields and derived ---

    #[test]
    fn param_assignment_excluded_from_fields_and_derived() {
        let source = "-- allium: 3\nentity Order {\n  subtotal: Decimal\n  total(tax_rate): subtotal * tax_rate\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Order");
        if let Some(ObligationDetail::Fields { fields }) = &ob.detail {
            assert!(fields.contains(&"subtotal".to_string()));
            assert!(!fields.contains(&"total".to_string()));
        } else {
            panic!("expected Fields detail");
        }
        let derived = obligations_matching(&plan, "derived.Order.total");
        assert!(derived.is_empty());
    }

    // --- Full round-trip: mixed spec produces correct obligation set ---

    #[test]
    fn mixed_spec_obligation_categories() {
        let source = "\
-- allium: 3
entity Order {
  total: Integer
  status: pending | done
  transitions status {
    pending -> done
    terminal: done
  }
  invariant NonNeg { this.total >= 0 }
}
enum Priority { low high }
value Money { amount: Decimal }
config { max_retries: Integer = 3 }
invariant GlobalCheck { for o in Orders: o.total >= 0 }";
        let plan = parse_plan(source);

        // Spot-check that each category is present
        find_obligation(&plan, "entity-fields.Order");
        find_obligation(&plan, "transition-edge.Order.pending.done");
        find_obligation(&plan, "transition-rejected.Order.status");
        find_obligation(&plan, "transition-terminal.Order.status");
        find_obligation(&plan, "invariant.Order.NonNeg");
        find_obligation(&plan, "enum-comparable.Priority");
        find_obligation(&plan, "value-equality.Money");
        find_obligation(&plan, "config-default.max_retries");
        find_obligation(&plan, "invariant.GlobalCheck");

        // Invariants have expressions, others don't
        assert!(find_obligation(&plan, "invariant.Order.NonNeg").expression.is_some());
        assert!(find_obligation(&plan, "invariant.GlobalCheck").expression.is_some());
        assert!(find_obligation(&plan, "entity-fields.Order").expression.is_none());
        assert!(find_obligation(&plan, "config-default.max_retries").expression.is_none());
    }

    // --- Rule dependency analysis ---

    #[test]
    fn rule_dependencies_external_trigger() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  when: Confirm(order)\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-success.Confirm");
        let deps = ob.dependencies.as_ref().expect("rule should have dependencies");
        assert!(matches!(deps.trigger_source, TriggerSource::External));
    }

    #[test]
    fn rule_dependencies_entities_read_from_requires() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  when: Confirm(order)\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Confirm").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_entities_written_from_ensures() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  when: Confirm(order)\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Confirm").dependencies.as_ref().unwrap();
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_state_transition_trigger() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            rule Ship {\n  when: order: Order.status transitions_to shipped\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Ship").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::StateTransition));
        assert!(deps.entities_read.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_temporal_trigger() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | cancelled, created_at: Timestamp }\n\
            rule Timeout {\n  when: o: Order.created_at + 48.hours <= now\n  ensures: o.status = cancelled\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Timeout").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::Temporal));
    }

    #[test]
    fn rule_dependencies_creation_trigger() {
        let source = "-- allium: 3\n\
            entity Order { total: Integer }\n\
            entity Email { to: String }\n\
            rule Notify {\n  when: order: Order.created()\n  ensures: Email.created(to: order.to)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Notify").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::Creation));
    }

    #[test]
    fn rule_dependencies_chained_trigger() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule First {\n  when: Start(order)\n  ensures:\n    order.status = done\n    Notify(order)\n}\n\
            rule Second {\n  when: Notify(order)\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Second").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::Chained));
    }

    #[test]
    fn rule_dependencies_entity_creation_in_ensures() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            entity Email { to: String }\n\
            rule Ship {\n  when: Ship(order)\n  ensures:\n    order.status = shipped\n    Email.created(to: order.to)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Ship").dependencies.as_ref().unwrap();
        assert!(deps.entities_created.contains(&"Email".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_trigger_emissions() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Process {\n  when: Process(order)\n  ensures:\n    order.status = done\n    Notify(order)\n    Alert(order)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Process").dependencies.as_ref().unwrap();
        assert!(deps.trigger_emissions.contains(&"Notify".to_string()));
        assert!(deps.trigger_emissions.contains(&"Alert".to_string()));
    }

    #[test]
    fn rule_dependencies_deferred_specs() {
        let source = "-- allium: 3\n\
            entity Order { total: Integer }\n\
            deferred Evaluate\n\
            rule Process {\n  when: Process(order)\n  ensures: order.total = Evaluate(order)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Process").dependencies.as_ref().unwrap();
        assert!(deps.deferred_specs.contains(&"Evaluate".to_string()));
    }

    #[test]
    fn rule_dependencies_present_on_failure_obligation() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "rule-failure.Confirm.1");
        assert!(ob.dependencies.is_some());
    }

    #[test]
    fn rule_dependencies_absent_on_non_rule_obligations() {
        let source = "-- allium: 3\nentity Order { total: Integer }";
        let plan = parse_plan(source);
        let ob = find_obligation(&plan, "entity-fields.Order");
        assert!(ob.dependencies.is_none());
    }

    #[test]
    fn rule_dependencies_empty_arrays_when_no_deps() {
        let source = "-- allium: 3\n\
            rule Simple {\n  ensures: true\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Simple").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.is_empty());
        assert!(deps.entities_written.is_empty());
        assert!(deps.entities_created.is_empty());
        assert!(deps.entities_removed.is_empty());
        assert!(deps.deferred_specs.is_empty());
        assert!(deps.trigger_emissions.is_empty());
    }

    #[test]
    fn rule_dependencies_json_serialisation() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  when: Confirm(order)\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap() == "rule-success.Confirm")
            .unwrap();
        let deps = &ob["dependencies"];
        assert!(deps.is_object());
        assert!(deps["entities_read"].is_array());
        assert!(deps["entities_written"].is_array());
        assert!(deps["trigger_source"].is_string());
    }

    #[test]
    fn rule_dependencies_omitted_from_json_for_non_rules() {
        let source = "-- allium: 3\nentity Order { total: Integer }";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap().contains("entity-fields"))
            .unwrap();
        assert!(ob.get("dependencies").is_none());
    }

    #[test]
    fn rule_dependencies_direct_entity_ref_in_when() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            rule Ship {\n  when: order: Order.status transitions_to shipped\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Ship").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_multiple_entities() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            entity Customer { email: String }\n\
            rule Process {\n  when: Process(order)\n  requires: order.status = pending\n  ensures:\n    order.status = done\n    customer.email = order.email\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Process").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Customer".to_string()));
    }

    // --- Trigger source: becomes ---

    #[test]
    fn rule_dependencies_becomes_trigger() {
        let source = "-- allium: 3\n\
            entity Order { status: shipped | delivered }\n\
            rule Archive {\n  when: order: Order.status becomes delivered\n  ensures: AuditLog.created(action: delivered, order: order)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Archive").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::StateTransition));
        assert!(deps.entities_read.contains(&"Order".to_string()));
    }

    // --- For-block and if-block dependency extraction ---

    #[test]
    fn rule_dependencies_for_block_writes() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | confirmed }\n\
            rule BulkConfirm {\n  when: BulkConfirm(batch)\n  for order in batch.orders where order.status = pending:\n    ensures: order.status = confirmed\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.BulkConfirm").dependencies.as_ref().unwrap();
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn rule_dependencies_if_block_writes() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | cancelled, cancelled_by: String? }\n\
            entity Customer { name: String }\n\
            rule Cancel {\n  when: Cancel(order, reason)\n  ensures:\n    order.status = cancelled\n    if reason = customer_request:\n      order.cancelled_by = order.customer.name\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Cancel").dependencies.as_ref().unwrap();
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    // --- Multi-line ensures with multiple field writes ---

    #[test]
    fn rule_dependencies_multi_field_ensures() {
        let source = "-- allium: 3\n\
            entity Order { status: picking | shipped, tracking_number: String, shipped_at: Timestamp }\n\
            rule ShipOrder {\n  when: ShipOrder(order, tracking)\n  requires: order.status = picking\n  ensures:\n    order.status = shipped\n    order.tracking_number = tracking\n    order.shipped_at = now\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.ShipOrder").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    // --- Entity creation with named args ---

    #[test]
    fn rule_dependencies_entity_creation_named_args() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            entity Email { to: String }\n\
            rule Notify {\n  when: order: Order.status transitions_to shipped\n  ensures: Email.created(to: order.customer.email, template: order_shipped)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Notify").dependencies.as_ref().unwrap();
        assert!(deps.entities_created.contains(&"Email".to_string()));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        // .created() is entity creation, not a write
        assert!(!deps.entities_written.contains(&"Email".to_string()));
    }

    // --- Entity in requires via `in` set membership ---

    #[test]
    fn rule_dependencies_in_set_requires() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | confirmed | cancelled }\n\
            rule Cancel {\n  when: Cancel(order)\n  requires: order.status in {pending, confirmed}\n  ensures: order.status = cancelled\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Cancel").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
    }

    // --- External entity included in entity_names ---

    #[test]
    fn rule_dependencies_external_entity_resolved() {
        let source = "-- allium: 3\n\
            external entity Customer { email: String }\n\
            rule Notify {\n  when: Notify(customer)\n  ensures: customer.email = null\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Notify").dependencies.as_ref().unwrap();
        assert!(deps.entities_written.contains(&"Customer".to_string()));
    }

    // --- Deferred spec with qualified path ---

    #[test]
    fn rule_dependencies_deferred_qualified_path() {
        let source = "-- allium: 3\n\
            entity Order { total: Integer }\n\
            deferred Order.fraud_check\n\
            rule Check {\n  when: Check(order)\n  ensures: order.total = fraud_check(order)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Check").dependencies.as_ref().unwrap();
        assert!(deps.deferred_specs.contains(&"fraud_check".to_string()));
    }

    // --- Same entity appears in both read and written ---

    #[test]
    fn rule_dependencies_entity_both_read_and_written() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Confirm {\n  when: Confirm(order)\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Confirm").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    // --- Trigger emission not confused with entity name ---

    #[test]
    fn rule_dependencies_entity_name_not_in_trigger_emissions() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            entity Email { to: String }\n\
            rule Process {\n  when: Process(order)\n  ensures:\n    order.status = done\n    Email.created(to: order.email)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Process").dependencies.as_ref().unwrap();
        // Email.created() is entity creation, not a trigger emission
        assert!(!deps.trigger_emissions.contains(&"Email".to_string()));
        assert!(deps.entities_created.contains(&"Email".to_string()));
    }

    // --- Rule with no when clause ---

    #[test]
    fn rule_dependencies_no_when_clause() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule Update {\n  requires: order.status = pending\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Update").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::External));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    // --- Dependencies shared across obligation types from same rule ---

    #[test]
    fn rule_dependencies_shared_across_obligation_types() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | cancelled, created_at: Timestamp }\n\
            rule Timeout {\n  when: o: Order.created_at + 48.hours <= now\n  requires: o.status = pending\n  ensures: o.status = cancelled\n}";
        let plan = parse_plan(source);
        let success_deps = find_obligation(&plan, "rule-success.Timeout").dependencies.as_ref().unwrap();
        let failure_deps = find_obligation(&plan, "rule-failure.Timeout.1").dependencies.as_ref().unwrap();
        let temporal_deps = find_obligation(&plan, "temporal.Timeout").dependencies.as_ref().unwrap();
        // All three should carry the same dependency info
        assert_eq!(success_deps.entities_read, failure_deps.entities_read);
        assert_eq!(success_deps.entities_read, temporal_deps.entities_read);
        assert!(matches!(failure_deps.trigger_source, TriggerSource::Temporal));
        assert!(matches!(temporal_deps.trigger_source, TriggerSource::Temporal));
    }

    // --- trigger_source serialises to snake_case ---

    #[test]
    fn rule_dependencies_trigger_source_snake_case_json() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            rule Ship {\n  when: order: Order.status transitions_to shipped\n  ensures: order.status = shipped\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap() == "rule-success.Ship")
            .unwrap();
        assert_eq!(ob["dependencies"]["trigger_source"].as_str().unwrap(), "state_transition");
    }

    #[test]
    fn rule_dependencies_trigger_source_chained_json() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule A {\n  when: Start(x)\n  ensures: Next(x)\n}\n\
            rule B {\n  when: Next(x)\n  ensures: x.status = done\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap() == "rule-success.B")
            .unwrap();
        assert_eq!(ob["dependencies"]["trigger_source"].as_str().unwrap(), "chained");
    }

    // --- BTreeSet ordering: entities are alphabetically sorted ---

    #[test]
    fn rule_dependencies_entities_sorted() {
        let source = "-- allium: 3\n\
            entity Zebra { x: Integer }\n\
            entity Alpha { x: Integer }\n\
            entity Middle { x: Integer }\n\
            rule Foo {\n  when: Foo(zebra, alpha, middle)\n  requires: zebra.x = 1 and alpha.x = 2 and middle.x = 3\n  ensures: zebra.x = 4\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Foo").dependencies.as_ref().unwrap();
        assert_eq!(deps.entities_read, vec!["Alpha", "Middle", "Zebra"]);
    }

    // --- Entity creation not reported as entity written ---

    #[test]
    fn rule_dependencies_created_entity_not_in_written() {
        let source = "-- allium: 3\n\
            entity AuditLog { action: String }\n\
            rule Log {\n  when: Log(x)\n  ensures: AuditLog.created(action: x)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Log").dependencies.as_ref().unwrap();
        assert!(deps.entities_created.contains(&"AuditLog".to_string()));
        assert!(deps.entities_written.is_empty());
    }

    // --- Multiple entity creations ---

    #[test]
    fn rule_dependencies_multiple_entity_creations() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | shipped }\n\
            entity Email { to: String }\n\
            entity AuditLog { action: String }\n\
            rule Ship {\n  when: Ship(order)\n  ensures:\n    order.status = shipped\n    Email.created(to: order.email)\n    AuditLog.created(action: shipped)\n}";
        let plan = parse_plan(source);
        let deps = find_obligation(&plan, "rule-success.Ship").dependencies.as_ref().unwrap();
        assert_eq!(deps.entities_created, vec!["AuditLog", "Email"]);
    }

    // --- Chained: emitter rule is external, not chained to itself ---

    #[test]
    fn rule_dependencies_emitter_is_external_not_self_chained() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            rule First {\n  when: Start(order)\n  ensures:\n    order.status = done\n    Notify(order)\n}\n\
            rule Second {\n  when: Notify(order)\n  ensures: order.status = done\n}";
        let plan = parse_plan(source);
        let first_deps = find_obligation(&plan, "rule-success.First").dependencies.as_ref().unwrap();
        let second_deps = find_obligation(&plan, "rule-success.Second").dependencies.as_ref().unwrap();
        // First emits Notify — its trigger_source is external (Start is not emitted by any rule)
        assert!(matches!(first_deps.trigger_source, TriggerSource::External));
        // Second listens on Notify — chained
        assert!(matches!(second_deps.trigger_source, TriggerSource::Chained));
    }

    // --- JSON: all dependency fields present with correct types ---

    #[test]
    fn rule_dependencies_json_shape_complete() {
        let source = "-- allium: 3\n\
            entity Order { status: pending | done }\n\
            entity Email { to: String }\n\
            deferred Evaluate\n\
            rule Process {\n  when: Process(order)\n  requires: order.status = pending\n  ensures:\n    order.status = done\n    Email.created(to: Evaluate(order))\n    Notify(order)\n}";
        let plan = parse_plan(source);
        let json = to_json(&plan);
        let ob = json["obligations"].as_array().unwrap()
            .iter().find(|o| o["id"].as_str().unwrap() == "rule-success.Process")
            .unwrap();
        let deps = &ob["dependencies"];
        // Populated fields present, empty fields omitted
        assert!(deps["entities_read"].is_array());
        assert!(deps["entities_written"].is_array());
        assert!(deps["entities_created"].is_array());
        assert!(deps["entities_removed"].is_null(), "empty entities_removed should be omitted");
        assert!(deps["deferred_specs"].is_array());
        assert!(deps["trigger_emissions"].is_array());
        assert!(deps["trigger_source"].is_string());
        // Check specific values
        assert!(deps["entities_read"].as_array().unwrap().iter().any(|v| v == "Order"));
        assert!(deps["entities_written"].as_array().unwrap().iter().any(|v| v == "Order"));
        assert!(deps["entities_created"].as_array().unwrap().iter().any(|v| v == "Email"));
        assert!(deps["deferred_specs"].as_array().unwrap().iter().any(|v| v == "Evaluate"));
        assert!(deps["trigger_emissions"].as_array().unwrap().iter().any(|v| v == "Notify"));
        assert_eq!(deps["trigger_source"], "external");
    }

    // --- Integration: v3-lifecycle fixture ---

    #[test]
    fn fixture_confirm_order_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.ConfirmOrder").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::External));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
        assert!(deps.entities_created.is_empty());
        assert!(deps.trigger_emissions.is_empty());
    }

    #[test]
    fn fixture_cancel_by_timeout_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.CancelByTimeout").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::Temporal));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn fixture_notify_on_shipment_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.NotifyOnShipment").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::StateTransition));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_created.contains(&"Email".to_string()));
        assert!(deps.entities_written.is_empty());
    }

    #[test]
    fn fixture_archive_delivered_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.ArchiveDelivered").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::StateTransition));
        assert!(deps.entities_created.contains(&"AuditLog".to_string()));
    }

    #[test]
    fn fixture_bulk_confirm_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.BulkConfirm").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::External));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn fixture_process_cancellation_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.ProcessCancellation").dependencies.as_ref().unwrap();
        assert!(matches!(deps.trigger_source, TriggerSource::External));
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
    }

    #[test]
    fn fixture_ship_order_dependencies() {
        let source = std::fs::read_to_string("../allium-parser/tests/fixtures/v3-lifecycle.allium").unwrap();
        let plan = parse_plan(&source);
        let deps = find_obligation(&plan, "rule-success.ShipOrder").dependencies.as_ref().unwrap();
        assert!(deps.entities_read.contains(&"Order".to_string()));
        assert!(deps.entities_written.contains(&"Order".to_string()));
        assert!(deps.entities_created.is_empty());
    }
}