skilj-core 0.0.4

SkilJ's domain engine: entities, rules, the plugin API, and persistence. Zero web-framework dependency - see docs/architecture.md §3.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
//! `BoundedContext`, `EventType`, `CommandType`, `Event` (+ its four
//! variants), `Command`, `EncryptionKey`, `Subscription` (+ its two
//! variants), `ReadCursor`; `ProcessCommand`, `RegisterEventType`,
//! `RegisterCommandType`, `CreateExternalEvent`, `CreateDirectEvent`,
//! `FetchEvents`, `ConsumeEvents`, `AcknowledgeEvents`, `FetchCommands`,
//! `QueryEvents`/`CountEvents`/`InspectEvent`, `ForgetSubject`; the
//! in-memory per-bounded-context event cache; and most of this crate's
//! black boxes (`protect_sensitive_fields`, `derive_tags`,
//! `valid_filters`, `valid_tag_mappings`, `valid_sensitive_fields`,
//! `schema_is_backwards_compatible`, `next_sequence`, `render_event`,
//! `render_command`). `Projection`/`ProjectionRebuild` and their four
//! rules live in `crate::projections` instead, not here - see this
//! module's own history note below for why they briefly weren't. See
//! docs/architecture.md §3.2.

use crate::access_control::{
    AccessLevel, CommandToken, DirectCreationToken, EventReadToken, ExternalEventToken,
    PrivateFieldGrant, Role, RoleAccessMapping, RoleStatus, TokenStatus,
};
use crate::encryption::DataKey;
use crate::error::SkiljRejection;
use crate::shared::{
    CommandDecision, Filter, FilterOperator, Metadata, PrivateField, PrivateFieldKind,
    SensitiveField, Tag, TagMapping,
};

// The in-memory per-bounded-context event cache the spec describes
// (drift audit finding #8, closed 2026-08-19) lives in `crate::
// event_cache` - `db::list_events_cached`/
// `list_events_for_bounded_context_cached`/`get_event_by_sequence_cached`
// are what `query_events`/`count_events`/`fetch_events`/`consume_events`/
// `process_command`'s own callers now read through instead of an
// unconditional Postgres load; `query_events`/`count_events` below still
// just take `bounded_context_events: &[Event]` as a caller-supplied
// snapshot, unchanged - which snapshot that is (cache-served or a
// Postgres fallback) is entirely the caller's own concern, the same
// "pure function, I/O resolved before the call" split every other rule
// in this module already follows. `create_all_events_subscription`/
// `create_event_type_subscription` take the identical shape of snapshot
// but are deliberately NOT wired to the cache - see `crate::event_cache`'s
// own module doc comment for which four rule-groups are and aren't.
// `Event.sequence` and every field/argument derived from it are `i64` -
// see docs/architecture.md §2.2.1 - not `i32`, to avoid the overflow
// risk that decision exists to close.
//
// `EventType`, `Event`, `ReadCursor`, `AckMode` and the `FetchEvents`/
// `ConsumeEvents`/`AcknowledgeEvents` rules were a first cut, added while
// propagating tests for the EventFetch surface (docs/architecture.md
// §9's pilot). `EventType`/`Event` grew their remaining fields, and
// `EventOrigin`/`create_external_event`/`create_direct_event` were added
// next, propagating ExternalEventIngestion/DirectEventCreation.
// `BoundedContext`, `CommandType`, `Command`,
// `EventOrigin::CommandTriggered`'s field, `authorise_command_trigger`
// and `process_command` were added propagating CommandTrigger/
// ProcessCommand - `AuthoriseCommandSubmission`/`CommandSubmission` (the
// GraphQL/Role path) stayed out of that pass; see `process_command`'s own
// doc comment for why. `authorise_command_submission` was added next,
// once Role/RoleAccessMapping existed to authorise it against.
// `register_event_type`/`register_command_type`, real implementations of
// `valid_tag_mappings`/`valid_sensitive_fields`/
// `schema_is_backwards_compatible` (flat, bare-field-name schemas - see
// `resolve_field`'s own doc comment for the dotted-path elaboration left
// deferred), and `EventType`'s two `system_triggered_*` fields were added
// next, propagating `RegisterEventType`/`RegisterCommandType`.
// `Projection`/`ProjectionRebuild`, `register_projection`,
// `rebuild_projection` and `discard_projection_rebuild` were added here
// next, propagating `RegisterProjection`/`RebuildProjection`/
// `DiscardProjectionRebuild` - promotion (a rebuild replacing the live
// `Projection` once caught up) is a background-process concern per the
// spec's own text, not something any of these three rules perform, so it
// stayed unmodelled along with the async `project()` consumer that would
// drive it. `query_events`/`count_events`/`inspect_event` and a real
// (empty-`sensitive_fields`-case) `render_event` were added next,
// propagating `QueryEvents`/`CountEvents`/`InspectEvent` -
// `FetchCommands`/`QueryProjection` (the other two admin-facing reads
// sharing this same two-grant redaction contract, via `render_command`/
// `read_projection`) stayed out of that pass, left for their own.
// `fetch_commands` and `render_command` (`render_event`'s own
// `Command`-shaped twin) were added next, propagating `FetchCommands`.
// `query_projection` was added next, propagating `QueryProjection` -
// `read_projection` and `await_projection_caught_up` are both fully
// caller-supplied (see `crate::projections::query_projection`'s own doc
// comment for why), the same treatment `process_command`'s `decision`
// gets, rather than getting the trivial/deferred split
// `render_event`/`render_command` have. `Subscription` (+ its two
// variants), `create_all_events_subscription`/
// `create_event_type_subscription`/`deliver_to_subscriptions` were added
// next, propagating `EventSubscription` - the real delivery loop's own
// asynchronous, best-effort dispatch (outside any request's transaction,
// per the note above rule `DeliverToSubscriptions`) stays unmodelled;
// `deliver_to_subscriptions` is the pure "who matches and what do they
// get" computation underneath it. `EncryptionKey` (full entity -
// `EncryptionKeyRef`'s stand-in role folded into it, since neither is
// populated by anything else yet) and `forget_subject` were added last,
// propagating `ForgetSubject` - deliberately scoped to destruction only;
// see `EncryptionKey`'s own doc comment for why provisioning
// (`protect_sensitive_fields`'s get-or-create) stayed out of this pass.
// `Projection`/`ProjectionRebuild` and their four rules were moved out to
// `crate::projections` last, after the fact: `docs/architecture.md`'s own
// module map had always put them there, in a `projections` module
// already scaffolded with its own placeholder `Error` - missed when they
// were first built directly here instead. See `crate::projections`' own
// doc comment for the rest of that history.

/// See `entity BoundedContext`'s `status` field/transition graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BoundedContextStatus {
    Active,
    Archived,
}

/// See `entity BoundedContext`. `created_at`/`created_by` were added
/// propagating `BoundedContextCreation`/`BoundedContextDirectory` - the
/// `admin` default's own `created_at`/`created_by` are stamped for real
/// at startup by `bootstrap::stamp_admin_bounded_context` (see its own
/// doc comment, including the concurrent-startup race fix). The
/// relationship projections (`event_types`, `commands`,
/// ...) are omitted, the same "caller resolves it, not a stored field"
/// treatment every other relationship projection in this codebase gets.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoundedContext {
    pub name: String,
    pub status: BoundedContextStatus,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub created_by: crate::bootstrap::ContextCreator,
    /// See `entity BoundedContext`'s own `template` field doc comment
    /// (Codeberg issue #13). `None` for every context `AddBoundedContext`
    /// makes and for the admin context - the ordinary case. Set once, at
    /// creation, by `CreateBoundedContextFromTemplate`, and never
    /// afterward - the one write it can still take is `DeleteBoundedContext`
    /// clearing it to `None` when the referenced template is deleted (the
    /// link ends with its target, it never moves to a different one).
    /// Invariant `TemplateIsNeverItselfTemplated`: a `BoundedContext` used
    /// as a template always has `template: None` itself, so this never
    /// recurses past one level.
    pub template: Option<Box<BoundedContext>>,
}

/// See `enum MissedOccurrencePolicy`. No `Default` impl, deliberately -
/// see that type's own doc comment on why this library never picks one
/// on a bounded context's behalf.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MissedOccurrencePolicy {
    Skip,
    FireOnce,
    ReplayBacklog,
}

/// See `entity EventType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventType {
    pub bounded_context: BoundedContext,
    pub name: String,
    pub schema: String,
    pub schema_version: i64,
    pub tag_mappings: Vec<TagMapping>,
    /// See `plugin::EventType::owner_tag_key`'s own doc comment - names
    /// which of `tag_mappings`' own keys is this type's "owner"
    /// dimension, or `None` for a type with no such notion (every type
    /// registered before this field existed). Validated at registration
    /// time by `valid_owner_tag_key` - `null`, or a key present in
    /// `tag_mappings` - never re-validated here. Read by
    /// `event_owner_scope_satisfied`, the cross-tenant read fix's own
    /// per-event predicate (docs/architecture.md's own write-up of these
    /// passes).
    pub owner_tag_key: Option<String>,
    pub sensitive_fields: Vec<SensitiveField>,
    /// See `value PrivateField`'s own doc comment - a plain read-time
    /// redaction rule standing beside `sensitive_fields`, not inside it.
    /// Unlike `sensitive_fields`, this is not a fact about how an event
    /// was stored (a private field is always stored in plaintext), so
    /// changing this declaration changes what every reader sees of this
    /// type's whole history immediately, not merely of events created
    /// next - the same live-declaration treatment `owner_tag_key` already
    /// gets, and for the same reason. Validated at registration by
    /// `valid_private_fields`.
    pub private_fields: Vec<PrivateField>,
    pub external_creation_allowed: bool,
    pub direct_creation_allowed: bool,
    /// See `EventOrigin::SystemTriggered`'s registration opt-in, and
    /// `rule CreateSystemEvent`/`rule SkipMissedOccurrences`, the real
    /// scheduler mechanism these fields drive.
    pub system_triggered_allowed: bool,
    pub system_triggered_schedule: Option<String>,
    /// What this type owes for an occurrence of its own schedule that
    /// passed while nothing was watching for it - `None` for a type that
    /// hasn't opted into scheduling (nothing to choose), mandatory the
    /// moment it has (see `register_event_type`'s own
    /// `MissingScheduleOrPolicy` guard and invariant
    /// `ScheduledTypeIsFullyConfigured`). No default, anywhere - see
    /// `MissedOccurrencePolicy`'s own doc comment.
    pub missed_occurrence_policy: Option<MissedOccurrencePolicy>,
    /// How far this type's schedule has been accounted for: every
    /// occurrence at or before this instant has either produced an event
    /// or been deliberately passed over, and none of them is considered
    /// again. An instant, not an occurrence index - see `create_system_event`'s
    /// own doc comment for why that's what lets a later schedule-string
    /// change not reopen anything. `None` exactly when
    /// `missed_occurrence_policy` is `None`.
    pub schedule_position: Option<chrono::DateTime<chrono::Utc>>,
    /// The occurrence instant of the most recent occurrence of this
    /// type's schedule that actually produced an event - not the wall
    /// clock of the write, which is already on that event's own
    /// `Metadata.created_at` (see `create_system_event`'s own doc
    /// comment). `None` until the first one fires; never reset by
    /// switching scheduling off.
    pub last_fired_at: Option<chrono::DateTime<chrono::Utc>>,
    pub event_read_allowed: bool,
}

/// See `entity CommandType`. `command_tokens` (a relationship
/// projection, not a stored field - the same treatment `EventType`'s
/// `*_tokens` relationships get) is omitted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandType {
    pub bounded_context: BoundedContext,
    pub name: String,
    pub schema: String,
    pub schema_version: i64,
    pub tag_mappings: Vec<TagMapping>,
    /// See `EventType.owner_tag_key`'s own doc comment - identical role,
    /// for `Command.consistency_tags` instead of `Event.tags`. Cross-tenant
    /// read fix (docs/architecture.md's own write-up of these passes).
    pub owner_tag_key: Option<String>,
    pub sensitive_fields: Vec<SensitiveField>,
    /// See `EventType.private_fields`'s own doc comment - identical role,
    /// for `Command.payload` instead of `Event.payload`.
    pub private_fields: Vec<PrivateField>,
    pub rest_trigger_allowed: bool,
}

/// See `entity EncryptionKey`'s `status` field/transition graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncryptionKeyStatus {
    Active,
    Destroyed,
}

/// See `entity EncryptionKey`. An `EncryptionKey` is identified by
/// `bounded_context`/`subject_key`/`subject_value` alone, never by which
/// type declared the field (see the note above rule `CreateExternalEvent`),
/// which is what lets a command and an event naming the same subject
/// encrypt under the very same key. Both provisioning
/// (`protect_sensitive_fields`'s get-or-create) and destruction
/// (`forget_subject`) are real.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncryptionKey {
    pub bounded_context: BoundedContext,
    pub subject_key: String,
    pub subject_value: String,
    pub status: EncryptionKeyStatus,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub destroyed_at: Option<chrono::DateTime<chrono::Utc>>,
}

/// See `value EncryptedPayload`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProtectedPayload {
    pub payload: String,
    pub encryption_keys: Vec<EncryptionKey>,
}

/// See `entity Command`. `triggered_events` (a relationship projection -
/// the events this command's own `ProcessCommand` call created, not a
/// stored field) is deliberately omitted, the same treatment
/// `EventType`'s `*_tokens` get; a `process_command` caller already has
/// them as `ProcessCommandResult::events`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Command {
    /// SkilJ's own internal identifier, generated at creation via
    /// `generate_token_id()` - the same "distinct, internal, not a
    /// credential" register `Role.id`/`AccessToken.id` already occupy
    /// (see their own doc comments). Added to close drift audit finding
    /// #12 (2026-08-20, see project memory `skilj-drift-audit-2026-08-20`):
    /// `fetch_commands`'s own `triggered_event` lookup used to match by
    /// whole-`Command` equality, reconstructing identity from content
    /// rather than having a real one to compare - a distinct concept from
    /// `commands.id`, this struct's own DB row's internal `BIGSERIAL`
    /// primary key (still unexposed here, purely an FK-linking detail -
    /// see `db::insert_command`'s own doc comment).
    pub id: String,
    pub bounded_context: BoundedContext,
    pub command_type: CommandType,
    pub payload: String,
    pub metadata: Metadata,
    pub encryption_keys: Vec<EncryptionKey>,
    pub consistency_tags: Vec<Tag>,
    /// `None` exactly when `highest_sequence` over this command's own
    /// `consistency_tags`-matching events returns `None` - which covers
    /// both "this command type declares no `tag_mappings`, so DCB is not
    /// in play" and "it does, but no event has matched any of these tags
    /// yet" alike. See `consistency_boundary_and_matching_events`'s doc
    /// comment for why those two cases don't need to be told apart.
    pub consistency_boundary: Option<i64>,
}

/// See `Event.origin`'s sum type. `SystemTriggered` is a placeholder with
/// no fields yet - per the spec it has no variant-specific fields at all,
/// so this placeholder is already its final shape (see
/// `SystemTriggeredClientIdIsSystem` for the `metadata.client_id`
/// constraint that comes with it - enforced, not just declared:
/// `create_system_event` stamps `client_id: "system"` unconditionally on
/// every occurrence it raises). `CommandTriggered`
/// boxes its `Command` - `Command` is by far this enum's largest variant
/// payload (it embeds a whole `CommandType`), and every `Event` carries
/// an `EventOrigin` regardless of which variant, so leaving it unboxed
/// would size every `Event` - including the far more common
/// `DirectlyCreated`/`ExternalTriggered` ones - to its worst case.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventOrigin {
    CommandTriggered {
        command: Box<Command>,
    },
    ExternalTriggered {
        source_content: String,
        source_context: Option<String>,
    },
    DirectlyCreated,
    SystemTriggered,
}

/// See `entity Event`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event {
    pub bounded_context: BoundedContext,
    pub event_type: EventType,
    pub payload: String,
    pub metadata: Metadata,
    pub sequence: i64,
    pub tags: Vec<Tag>,
    pub encryption_keys: Vec<EncryptionKey>,
    pub origin: EventOrigin,
}

/// See `variant AllEventsSubscription`. `bounded_context`/`access_mapping`/
/// `created_at`/`from_sequence` are `entity Subscription`'s own base
/// fields, carried on each variant directly - the same "no wrapper
/// struct, the variant carries the base fields too" treatment
/// `AccessToken`'s four variants get.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AllEventsSubscription {
    pub bounded_context: BoundedContext,
    pub access_mapping: RoleAccessMapping,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub from_sequence: i64,
    pub event_types: Vec<EventType>,
}

/// See `variant EventTypeSubscription`. Same base-fields-on-the-variant
/// treatment as `AllEventsSubscription` above.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventTypeSubscription {
    pub bounded_context: BoundedContext,
    pub access_mapping: RoleAccessMapping,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub from_sequence: i64,
    pub event_type: EventType,
    pub filters: Vec<Filter>,
}

/// See `entity Subscription`'s `kind` field - a Rust sum type over its two
/// variants stands in for it directly, the same "the enum variant tag is
/// the field" treatment `Event.origin`/`EventOrigin`, `AccessToken`/
/// `purpose` and `ContextCreator`/`kind` all get. Both variants are boxed:
/// each already embeds a whole `BoundedContext` plus a `RoleAccessMapping`
/// (itself embedding a `Role` and a second `BoundedContext`) before any
/// variant-specific field, so an unboxed enum would size every
/// `Subscription` - and everything that embeds one, like `EventDelivered`
/// below - to whichever variant happens to be larger. Same reasoning
/// `EventOrigin::CommandTriggered`'s boxed `Command` has.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Subscription {
    AllEventsSubscription(Box<AllEventsSubscription>),
    EventTypeSubscription(Box<EventTypeSubscription>),
}

impl Subscription {
    fn bounded_context(&self) -> &BoundedContext {
        match self {
            Subscription::AllEventsSubscription(s) => &s.bounded_context,
            Subscription::EventTypeSubscription(s) => &s.bounded_context,
        }
    }

    fn access_mapping(&self) -> &RoleAccessMapping {
        match self {
            Subscription::AllEventsSubscription(s) => &s.access_mapping,
            Subscription::EventTypeSubscription(s) => &s.access_mapping,
        }
    }

    fn starting_sequence(&self) -> i64 {
        match self {
            Subscription::AllEventsSubscription(s) => s.from_sequence,
            Subscription::EventTypeSubscription(s) => s.from_sequence,
        }
    }
}

/// See `enum AckMode`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AckMode {
    AutoAdvance,
    ManualAck,
}

/// See `entity ReadCursor`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadCursor {
    pub token: EventReadToken,
    pub ack_mode: AckMode,
    pub sequence: i64,
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

/// Library-level errors this module's own rules reject for.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    #[error("this bounded context is archived and accepts no new writes")]
    BoundedContextArchived,

    #[error("this schema change is not backwards compatible")]
    SchemaIncompatible,

    /// See `rule RegisterEventType`'s own note on scheduling being
    /// "opted into whole": `requires: system_triggered_allowed = true
    /// implies (system_triggered_schedule != null and
    /// missed_occurrence_policy != null)`. A real new rejection this
    /// pass introduced - a registration with `system_triggered_allowed:
    /// true` and no schedule used to be silently accepted (and simply
    /// never fired); it no longer is, since the same "no default" reasoning
    /// that now requires a policy would be incoherent if it left the
    /// schedule itself still optional.
    #[error(
        "a scheduled EventType must declare both system_triggered_schedule and \
         missed_occurrence_policy - neither has a default"
    )]
    MissingScheduleOrPolicy,

    #[error("this filter is invalid for its field's declared type")]
    InvalidFilter,

    /// Drift audit finding #16 (2026-08-20, see project memory
    /// `skilj-drift-audit-2026-08-20`): a registered `schema` used to go
    /// unvalidated at registration time whenever a type declared no
    /// `tag_mappings` and no `sensitive_fields` - both `valid_tag_mappings`/
    /// `valid_sensitive_fields` short-circuit `true` on empty input,
    /// never calling `schema_properties`/`jsonschema::validator_for` at
    /// all in that case, so a genuinely malformed schema (unparseable
    /// JSON, or JSON that isn't a valid JSON Schema document) silently
    /// "succeeded" at registration and only ever surfaced later, on the
    /// first real write, via `PayloadDoesNotMatchSchema` - confusing at
    /// the point a caller would actually see it, and far from where the
    /// real mistake was made. `valid_schema` closes this unconditionally.
    #[error("this schema is not well-formed JSON Schema")]
    InvalidSchema,

    #[error("this tag mapping names a field the schema doesn't declare")]
    InvalidTagMapping,

    /// Cross-tenant read fix (docs/architecture.md's own write-up of
    /// these passes): `owner_tag_key` names something other than an
    /// existing `tag_mappings` key - see `valid_owner_tag_key`'s own doc
    /// comment.
    #[error("this owner_tag_key does not name a key present in tag_mappings")]
    InvalidOwnerTagKey,

    #[error("a TagMapping and a SensitiveField may not name the same field")]
    SensitiveFieldTagOverlap,

    #[error("this sensitive field names a field the schema doesn't declare")]
    InvalidSensitiveField,

    /// `valid_private_fields`'s own rejection - either `field`/
    /// `addressee_field` names something the schema doesn't declare, or
    /// the `team`/`addressee_field` presence doesn't match `kind` (see
    /// that function's own doc comment for the full contract).
    #[error(
        "this private field names a field the schema doesn't declare, or its team/addressee_field \
         presence doesn't match its kind"
    )]
    InvalidPrivateField,

    /// The identical leak this variant's `tag_mappings` sibling
    /// (`SensitiveFieldTagOverlap`) already guards against, one register
    /// over: a tag is visible to every reader with no per-field
    /// redaction at all, so a field that is also `tag_mappings`-derived
    /// or `sensitive_fields`-encrypted cannot also be `private_fields` -
    /// its plaintext value would leak through the tag/ciphertext
    /// unredacted regardless of what `private_fields` says.
    #[error("a private field may not also be a tag mapping or a sensitive field")]
    PrivateFieldOverlap,

    #[error("a re-registration may not drop a tag mapping key already in use")]
    TagMappingKeyDropped,

    #[error("this EncryptionKey is not active")]
    EncryptionKeyNotActive,

    #[error("this named EventType belongs to a different bounded context than the grant's")]
    EventTypeNotInBoundedContext,

    #[error("this named CommandType belongs to a different bounded context than the grant's")]
    CommandTypeNotInBoundedContext,

    #[error("this triggered_event belongs to a different bounded context than the grant's")]
    TriggeredEventNotInBoundedContext,

    #[error("this EventReadToken's cursor mode doesn't match what was requested")]
    CursorAckModeMismatch,

    #[error("acknowledging requires a manual_ack cursor")]
    NotManualAckCursor,

    #[error("this EventType is not opted into event reads")]
    EventReadNotAllowed,

    #[error("this EventReadToken has no read cursor yet - consume before acknowledging")]
    NoReadCursor,

    #[error("an acknowledgement may not move a cursor backwards")]
    AcknowledgementRegresses,

    #[error("this EventType is not opted into external event ingestion")]
    ExternalCreationNotAllowed,

    #[error("this EventType is not opted into direct event creation")]
    DirectCreationNotAllowed,

    #[error("this CommandType is not opted into REST triggering")]
    RestTriggerNotAllowed,

    /// `valid_payload(schema, payload)` rejected a caller-supplied
    /// payload at the one of four boundary crossings that checks it -
    /// see the note above `entity CommandType`'s "Payload schema shape"
    /// for why `decide()`'s `EventSpec`s and `scheduled_payload`'s own
    /// output are deliberately not covered by this variant; those are an
    /// obligation on the implementer, never runtime-checked.
    #[error("this payload does not validate against its type's registered schema")]
    PayloadDoesNotMatchSchema,

    /// Not spec-modeled: a security-review finding on `CrossContextRoute`
    /// (docs/architecture.md §36) - the `idempotency_keys` table's own
    /// primary key is `(command_type_name, idempotency_key)` with no
    /// caller/client_id column at all (Codeberg issue #12's original
    /// design never needed one - every prior caller only ever collided
    /// with its own past submissions). `CrossContextRoute`'s background
    /// task is the first caller to place an *unauthenticated internal*
    /// idempotency key into that same shared namespace
    /// (`"{RESERVED_IDEMPOTENCY_KEY_PREFIX}{route_name}:{sequence}"`),
    /// which an ordinary Write-level caller could otherwise pre-plant
    /// via `submitCommand`/`Idempotency-Key` to silently swallow a real
    /// route delivery as a `Deduplicated` no-op - see
    /// `reject_reserved_idempotency_key`'s own doc comment for the fix.
    #[error(
        "this idempotency key uses a reserved prefix - {RESERVED_IDEMPOTENCY_KEY_PREFIX:?} \
         is reserved for skilj's own internal use"
    )]
    ReservedIdempotencyKeyPrefix,

    /// Not spec-modeled: the spec's own `ProcessCommand` assumes `decide()`
    /// only ever names an `EventType` its bounded context actually
    /// registered - a plugin-author responsibility, not a case the spec
    /// enumerates a rejection for. Kept here anyway as a defensive check
    /// rather than an `unwrap()`/panic, since `decide()` is exactly the
    /// kind of black-box plugin code a misconfigured bounded context could
    /// get wrong.
    #[error("decide() named an event type this bounded context has not registered: {0}")]
    UnregisteredEventType(String),

    /// Not spec-modeled either, same register as `UnregisteredEventType`
    /// right above: a command payload, or a stored `Event`'s own payload,
    /// didn't deserialize into the compiled binary's typed
    /// `CommandType::Payload`/`BoundedContextEvent`-implementing enum
    /// variant. `valid_payload`/`PayloadDoesNotMatchSchema` closes this
    /// for a caller-supplied payload that's outright schema-invalid, but
    /// this stays reachable for the narrower gap `valid_payload` was
    /// never meant to cover: the compiled Rust type can still be
    /// *stricter* than the registered JSON Schema (e.g. an
    /// `#[serde(deny_unknown_fields)]`, a numeric type narrower than
    /// `"number"`, an enum whose variants the schema's own `"string"`
    /// doesn't enumerate), and a `decide()`/`scheduled_payload`-produced
    /// payload is never schema-checked at all (see the note above
    /// `entity CommandType`'s "Payload schema shape") - so this remains a
    /// real, unaddressed decode failure mode for a plugin-authored
    /// payload specifically. See docs/architecture.md §1.6/§1.7 for where
    /// this is raised (the `skilj` facade's decide()-dispatch bridge).
    #[error("stored payload did not decode into its expected type: {0}")]
    PayloadDecodeFailed(String),
}

impl SkiljRejection for Error {
    fn code(&self) -> &str {
        match self {
            Error::BoundedContextArchived => "bounded_context_archived",
            Error::SchemaIncompatible => "schema_incompatible",
            Error::MissingScheduleOrPolicy => "missing_schedule_or_policy",
            Error::InvalidFilter => "invalid_filter",
            Error::InvalidSchema => "invalid_schema",
            Error::InvalidTagMapping => "invalid_tag_mapping",
            Error::InvalidOwnerTagKey => "invalid_owner_tag_key",
            Error::SensitiveFieldTagOverlap => "sensitive_field_tag_overlap",
            Error::InvalidSensitiveField => "invalid_sensitive_field",
            Error::InvalidPrivateField => "invalid_private_field",
            Error::PrivateFieldOverlap => "private_field_overlap",
            Error::TagMappingKeyDropped => "tag_mapping_key_dropped",
            Error::EventTypeNotInBoundedContext => "event_type_not_in_bounded_context",
            Error::CommandTypeNotInBoundedContext => "command_type_not_in_bounded_context",
            Error::TriggeredEventNotInBoundedContext => "triggered_event_not_in_bounded_context",
            Error::EncryptionKeyNotActive => "encryption_key_not_active",
            Error::CursorAckModeMismatch => "cursor_ack_mode_mismatch",
            Error::NotManualAckCursor => "not_manual_ack_cursor",
            Error::EventReadNotAllowed => "event_read_not_allowed",
            Error::NoReadCursor => "no_read_cursor",
            Error::AcknowledgementRegresses => "acknowledgement_regresses",
            Error::ExternalCreationNotAllowed => "external_creation_not_allowed",
            Error::DirectCreationNotAllowed => "direct_creation_not_allowed",
            Error::RestTriggerNotAllowed => "rest_trigger_not_allowed",
            Error::PayloadDoesNotMatchSchema => "payload_does_not_match_schema",
            Error::ReservedIdempotencyKeyPrefix => "reserved_idempotency_key_prefix",
            Error::UnregisteredEventType(_) => "unregistered_event_type",
            Error::PayloadDecodeFailed(_) => "payload_decode_failed",
        }
    }

    fn message(&self) -> String {
        self.to_string()
    }
}

/// A resolved leaf schema's own shape, classified for the "reject
/// anything that does not land on a scalar or list-of-scalar leaf" check
/// `valid_tag_mappings`/`valid_sensitive_fields`/`valid_filters` all share
/// (see the payload schema shape note above `entity CommandType`) -
/// `valid_filters` additionally uses the `Scalar` case's own `json_type`/
/// `format` to pick which `FilterOperator`s are valid for that field.
/// Follows a bare `$ref` itself: `schemars` renders a unit enum via
/// `$ref` even for a *bare* top-level field (e.g.
/// `{"$ref": "#/definitions/OrderStatus"}` where `OrderStatus` is itself
/// `{"type": "string", "enum": [...]}`) - a plain scalar leaf, just
/// declared indirectly, unlike a `$ref` resolving to a real nested-object
/// shape (has its own `"properties"` rather than a scalar `"type"`),
/// which stays `Other` for a bare reference (needs one more dot to reach
/// a leaf inside it). `resolve_field`'s own dotted branch only follows a
/// `$ref` when a caller supplies a dot, so a bare reference still needs
/// resolving here.
#[derive(Debug, Clone, PartialEq, Eq)]
enum FieldKind {
    Scalar {
        json_type: &'static str,
        format: Option<String>,
    },
    ListOfScalar,
    Other,
}

/// A JSON Schema `"type"` value as one string - handles both the plain
/// `"string"` shape and the `["string","null"]` shape an `Option<T>`
/// field's `type` carries (nullability itself is decided from `required`
/// elsewhere - this only recovers the *scalar* kind either shape names).
/// Mirrors `skilj-graphql::projection_types::json_type_str` exactly - the
/// identical extraction, needed again here since `skilj-core` can't
/// depend on `skilj-graphql`.
fn json_type_str(type_value: &serde_json::Value) -> Option<&str> {
    match type_value {
        serde_json::Value::String(s) => Some(s.as_str()),
        serde_json::Value::Array(arr) => {
            arr.iter().find_map(|v| v.as_str().filter(|s| *s != "null"))
        }
        _ => None,
    }
}

fn classify(
    schema: &serde_json::Value,
    definitions: Option<&serde_json::Map<String, serde_json::Value>>,
) -> FieldKind {
    if let Some(def_name) = schema
        .get("$ref")
        .and_then(|v| v.as_str())
        .and_then(|r| r.rsplit('/').next())
    {
        return definitions
            .and_then(|defs| defs.get(def_name))
            .map(|resolved| classify(resolved, definitions))
            .unwrap_or(FieldKind::Other);
    }
    match schema.get("type").and_then(json_type_str) {
        Some(t @ ("string" | "integer" | "number" | "boolean")) => FieldKind::Scalar {
            // Interned via a small match, not the borrowed &str - FieldKind::Scalar
            // needs 'static so it can be constructed without carrying schema's own lifetime.
            json_type: match t {
                "string" => "string",
                "integer" => "integer",
                "number" => "number",
                _ => "boolean",
            },
            format: schema
                .get("format")
                .and_then(|f| f.as_str())
                .map(String::from),
        },
        Some("array") => match schema
            .get("items")
            .and_then(|i| i.get("type"))
            .and_then(json_type_str)
        {
            Some("string" | "integer" | "number" | "boolean") => FieldKind::ListOfScalar,
            _ => FieldKind::Other, // an array of non-scalar items - not a target either
        },
        _ => FieldKind::Other, // "object" (incl. a $ref resolved to one), unknown, or absent
    }
}

/// `resolve_field` plus the shape check every caller of it here actually
/// needs: `None` when the field doesn't resolve at all, or resolves to
/// something that isn't a scalar or list-of-scalar leaf.
fn resolve_field_kind(
    properties: &serde_json::Map<String, serde_json::Value>,
    definitions: Option<&serde_json::Map<String, serde_json::Value>>,
    field: &str,
) -> Option<FieldKind> {
    match classify(resolve_field(properties, definitions, field)?, definitions) {
        FieldKind::Other => None,
        kind => Some(kind),
    }
}

/// The type-to-operator matrix (see the note above rule `FetchEvents`:
/// "the exact type-to-operator matrix... is deliberately not enumerated
/// here... exactly the kind of mechanism this spec consistently hands to
/// a black box"). `format` values `"date-time"`/`"date"`/
/// `"partial-date-time"` (`chrono::DateTime<Utc>`/`NaiveDate`/`NaiveTime`'s
/// own three renderings) additionally get ordering operators - see
/// `matches_filters`' own `string_ordering` for why the comparison itself
/// needs real parsing, not plain string `Ord`, despite the check here
/// being schema-driven. Three more `format` values gate their own single
/// operator the same way: `"geo-point"` → `Near`, `"color"` →
/// `SimilarColor`, `"ip"` → `InSubnet` - each format-gated pairing has
/// its own real-parsing helper in `matches_one_filter` below, same
/// reasoning as the date/time case. `In` needs no `format` at all - valid
/// against any scalar leaf, same as `Equals`.
fn filter_operator_is_valid(kind: &FieldKind, operator: FilterOperator) -> bool {
    match kind {
        FieldKind::Scalar {
            json_type: "string",
            format,
        } => {
            matches!(
                operator,
                FilterOperator::Equals
                    | FilterOperator::Contains
                    | FilterOperator::IsLike
                    | FilterOperator::In
            ) || (matches!(
                format.as_deref(),
                Some("date-time" | "date" | "partial-date-time")
            ) && matches!(
                operator,
                FilterOperator::GreaterThan | FilterOperator::LessThan
            )) || (format.as_deref() == Some("geo-point") && operator == FilterOperator::Near)
                || (format.as_deref() == Some("color") && operator == FilterOperator::SimilarColor)
                || (format.as_deref() == Some("ip") && operator == FilterOperator::InSubnet)
        }
        FieldKind::Scalar {
            json_type: "integer" | "number",
            ..
        } => matches!(
            operator,
            FilterOperator::Equals
                | FilterOperator::GreaterThan
                | FilterOperator::LessThan
                | FilterOperator::In
        ),
        FieldKind::Scalar {
            json_type: "boolean",
            ..
        } => matches!(operator, FilterOperator::Equals | FilterOperator::In),
        FieldKind::Scalar { .. } => false, // unreachable - classify only ever sets one of the four above
        FieldKind::ListOfScalar => operator == FilterOperator::Contains,
        FieldKind::Other => false,
    }
}

/// Black box shared with `QueryEvents`/`CountEvents` (see the note above
/// rule `FetchEvents` and docs/architecture.md's "black boxes" list) -
/// not owned by the EventFetch pilot. Checks two things per filter: that
/// `field` names a field `event_type.schema` actually declares (bare or
/// a two-segment dotted path - see `resolve_field`), and that `operator`
/// is one that field's declared type supports (see `filter_operator_is_valid`).
pub fn valid_filters(event_type: &EventType, filters: &[Filter]) -> bool {
    if filters.is_empty() {
        return true;
    }
    let Some(properties) = schema_properties(&event_type.schema) else {
        return false;
    };
    let definitions = schema_definitions(&event_type.schema);
    filters.iter().all(|f| {
        resolve_field_kind(&properties, definitions.as_ref(), &f.field)
            .is_some_and(|kind| filter_operator_is_valid(&kind, f.operator))
    })
}

/// Parses `schema` as a JSON Schema object and returns its top-level
/// `properties` map - the one piece `valid_tag_mappings`/
/// `valid_sensitive_fields`/`schema_is_backwards_compatible` below all
/// need, per the payload schema shape note above `entity CommandType`.
/// `None` for anything that doesn't parse as `{"properties": {...}, ...}`,
/// which callers treat as "no fields exist", the same closed-world answer
/// an empty `properties` map gives.
fn schema_properties(schema: &str) -> Option<serde_json::Map<String, serde_json::Value>> {
    let parsed: serde_json::Value = serde_json::from_str(schema).ok()?;
    parsed.get("properties")?.as_object().cloned()
}

/// `schema`'s own top-level `"definitions"` map - named reusable nested
/// shapes a `"$ref"` elsewhere in the same schema points at (see
/// `skilj-graphql::projection_types`'s own identical extraction for
/// GraphQL type generation). `None` for anything that doesn't parse or
/// declares none, the same "absence reads as empty" treatment
/// `schema_properties` gives `properties`.
fn schema_definitions(schema: &str) -> Option<serde_json::Map<String, serde_json::Value>> {
    let parsed: serde_json::Value = serde_json::from_str(schema).ok()?;
    parsed.get("definitions")?.as_object().cloned()
}

/// `schema`'s top-level `required` array, as field names - `[]` for
/// anything that doesn't parse or declares none, the same "absence reads
/// as empty" treatment `schema_properties` gives `properties`.
fn schema_required(schema: &str) -> Vec<String> {
    serde_json::from_str::<serde_json::Value>(schema)
        .ok()
        .and_then(|v| v.get("required").cloned())
        .and_then(|v| v.as_array().cloned())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default()
}

/// Resolves a `FieldPath` - a bare top-level field name, or a two-segment
/// dotted path reaching one leaf inside a named nested shape - to the
/// property schema object it names, per the payload schema shape note
/// above `entity CommandType`. The bare-name case is a direct
/// `properties` lookup; a dotted path resolves its outer segment in
/// `properties`, follows that property's own `"$ref"` into `definitions`
/// (the same pattern `skilj-graphql::projection_types::build_field`
/// already uses for GraphQL type generation, reused for consistency
/// rather than reinvented), then looks the inner segment up in the
/// resolved nested shape's own `properties`. `?`-chained throughout, so
/// anything not shaped exactly right (no `$ref`, unknown definition
/// name, no nested `properties`) falls through to `None` - "reject,
/// don't guess", same as the bare-name case already does.
fn resolve_field<'a>(
    properties: &'a serde_json::Map<String, serde_json::Value>,
    definitions: Option<&'a serde_json::Map<String, serde_json::Value>>,
    field: &str,
) -> Option<&'a serde_json::Value> {
    match field.split_once('.') {
        None => properties.get(field),
        Some((outer, inner)) => {
            let def_name = properties
                .get(outer)?
                .get("$ref")?
                .as_str()?
                .rsplit('/')
                .next()?;
            definitions?.get(def_name)?.get("properties")?.get(inner)
        }
    }
}

/// `resolve_field`'s own counterpart against a *payload* (real JSON data)
/// rather than a *schema* (property definitions) - `protect_sensitive_fields`'s
/// own field/subject_field lookup. No `definitions` needed here: real
/// data has no `$ref`s, so a dotted path is simply nested `get` calls -
/// absence at either segment falls through to `None`, the same as the
/// bare-name case already does.
fn payload_field_value<'a>(
    payload: &'a serde_json::Value,
    field: &str,
) -> Option<&'a serde_json::Value> {
    match field.split_once('.') {
        None => payload.get(field),
        Some((outer, inner)) => payload.get(outer)?.get(inner),
    }
}

/// The mutable counterpart to `payload_field_value` - `protect_sensitive_fields`'s
/// own substitution step needs a place to write ciphertext back into.
fn payload_field_value_mut<'a>(
    payload: &'a mut serde_json::Value,
    field: &str,
) -> Option<&'a mut serde_json::Value> {
    match field.split_once('.') {
        None => payload.get_mut(field),
        Some((outer, inner)) => payload.get_mut(outer)?.get_mut(inner),
    }
}

/// A payload field's own JSON value, as the plain string
/// `EncryptionKey.subject_value`/`protect_sensitive_fields`'s own
/// plaintext-to-encrypt both need - a bare JSON string is used as-is; any
/// other scalar (number, boolean) is rendered via its own JSON literal
/// text. `None` for anything structured (object/array/null), which has no
/// sound single-string reading.
fn json_scalar_to_string(value: &serde_json::Value) -> Option<String> {
    match value {
        serde_json::Value::String(s) => Some(s.clone()),
        serde_json::Value::Number(_) | serde_json::Value::Bool(_) => Some(value.to_string()),
        _ => None,
    }
}

/// Black box (see the note above rule `RegisterEventType`): a registered
/// `schema` is well-formed JSON Schema - drift audit finding #16
/// (2026-08-20, see project memory `skilj-drift-audit-2026-08-20`).
/// Unconditional, unlike `valid_tag_mappings`/`valid_sensitive_fields`:
/// those only ever inspect `schema` as a side effect of checking a
/// non-empty `tag_mappings`/`sensitive_fields` list, so a type declaring
/// neither previously registered a garbage `schema` unrejected - this
/// closes that gap regardless of what else the registration declares.
/// The identical check `valid_payload` already runs against `schema`
/// itself (`serde_json::from_str` for well-formed JSON,
/// `jsonschema::validator_for` for well-formed JSON Schema specifically -
/// a schema that parses as JSON but isn't valid JSON Schema, e.g. a
/// `"type"` value that isn't a recognised keyword, is rejected here too).
pub fn valid_schema(schema: &str) -> bool {
    let Ok(schema_value) = serde_json::from_str::<serde_json::Value>(schema) else {
        return false;
    };
    jsonschema::validator_for(&schema_value).is_ok()
}

/// Black box (see the note above rule `RegisterEventType`): "every
/// `TagMapping.field` names a field the schema declares" - covers both a
/// bare field name and a two-segment dotted path into a named nested
/// shape, and rejects a field that doesn't land on a scalar or
/// list-of-scalar leaf (see `resolve_field_kind` - the payload schema
/// shape note above `entity CommandType` calls for this on all three of
/// `valid_tag_mappings`/`valid_sensitive_fields`/`valid_filters`, not
/// just the last). Takes `schema` alone, not a whole `EventType`/
/// `CommandType` - the identical `String` on both, same treatment
/// `protect_sensitive_fields`/`derive_tags` get for `sensitive_fields`/
/// `tag_mappings`.
pub fn valid_tag_mappings(schema: &str, tag_mappings: &[TagMapping]) -> bool {
    if tag_mappings.is_empty() {
        return true;
    }
    let Some(properties) = schema_properties(schema) else {
        return false;
    };
    let definitions = schema_definitions(schema);
    tag_mappings
        .iter()
        .all(|m| resolve_field_kind(&properties, definitions.as_ref(), &m.field).is_some())
}

/// Black box (see the note above rule `RegisterEventType`) - cross-tenant
/// read fix (docs/architecture.md's own write-up of these passes):
/// `owner_tag_key` is `null` (no owner dimension declared - every type
/// registered before this field existed), or names a key present in
/// `tag_mappings`. Unlike `valid_tag_mappings`'/`valid_sensitive_fields`'
/// additive-only re-registration treatment (`RegisterEventType`'s own
/// `existing.tag_mappings.all(...)` check), `owner_tag_key` is re-validated
/// fresh on every registration rather than accumulated: it is a pointer
/// *into* `tag_mappings`, not itself additive state, so a later
/// registration is free to change or clear it as long as it still points
/// at a real key (or is null).
pub fn valid_owner_tag_key(tag_mappings: &[TagMapping], owner_tag_key: Option<&str>) -> bool {
    match owner_tag_key {
        None => true,
        Some(key) => tag_mappings.iter().any(|m| m.key == key),
    }
}

/// Black box (see the note above rule `RegisterEventType`): the same
/// existence-and-shape check as `valid_tag_mappings` above for
/// `SensitiveField.subject_field` (any scalar or list-of-scalar leaf,
/// identically - `subject_field` is only ever read, never encrypted, so
/// nothing about its own declared type constrains what it can be).
/// `SensitiveField.field` - the leaf `protect_sensitive_fields` actually
/// replaces with ciphertext - is narrower: `FieldKind::Scalar { json_type:
/// "string", .. }` only, not any scalar and not `ListOfScalar`. This is
/// not an arbitrary restriction: ciphertext is fundamentally string-shaped
/// (`crate::encryption::encrypt_leaf`'s own output), so a leaf whose
/// declared type isn't already `"string"` cannot hold it and still
/// conform to its own schema - "a scalar keeps the same declared type
/// whether it holds plaintext or ciphertext" (the payload schema shape
/// note above `entity CommandType`) is only ever true for a leaf that was
/// a string to begin with. Encrypting a non-string leaf in place, or a
/// whole list at once (both accepted here before this fix), silently
/// broke every downstream typed decode instead.
pub fn valid_sensitive_fields(schema: &str, sensitive_fields: &[SensitiveField]) -> bool {
    if sensitive_fields.is_empty() {
        return true;
    }
    let Some(properties) = schema_properties(schema) else {
        return false;
    };
    let definitions = schema_definitions(schema);
    sensitive_fields.iter().all(|s| {
        matches!(
            resolve_field_kind(&properties, definitions.as_ref(), &s.field),
            Some(FieldKind::Scalar {
                json_type: "string",
                ..
            })
        ) && resolve_field_kind(&properties, definitions.as_ref(), &s.subject_field).is_some()
    })
}

/// Black box (see the note above rule `RegisterEventType`) - the
/// private-field mechanism's own registration-time check, in the same
/// register as `valid_tag_mappings`/`valid_sensitive_fields`: `field` (and,
/// for an `addressed`-kind entry, `addressee_field`) must be a real schema
/// leaf. Unlike `valid_sensitive_fields`, no `json_type: "string"`
/// restriction on `field` itself - a private field is redacted to `null`
/// at read time, not encrypted, so any scalar leaf works. The
/// kind-specific presence rule is `value PrivateField`'s own contract,
/// checked here rather than by the type system alone: `team` set and
/// `addressee_field` absent iff `kind = Team`; `addressee_field` set (and
/// itself a real schema leaf) and `team` absent iff `kind = Addressed`;
/// both absent iff `kind = Own`.
pub fn valid_private_fields(schema: &str, private_fields: &[PrivateField]) -> bool {
    if private_fields.is_empty() {
        return true;
    }
    let Some(properties) = schema_properties(schema) else {
        return false;
    };
    let definitions = schema_definitions(schema);
    private_fields.iter().all(|p| {
        if resolve_field_kind(&properties, definitions.as_ref(), &p.field).is_none() {
            return false;
        }
        match p.kind {
            PrivateFieldKind::Own => p.team.is_none() && p.addressee_field.is_none(),
            PrivateFieldKind::Team => p.team.is_some() && p.addressee_field.is_none(),
            PrivateFieldKind::Addressed => {
                p.team.is_none()
                    && p.addressee_field.as_deref().is_some_and(|af| {
                        resolve_field_kind(&properties, definitions.as_ref(), af).is_some()
                    })
            }
        }
    })
}

/// Black box (see the note above `entity CommandType`'s "Payload schema
/// shape"): does `payload`, read as JSON, validate against the JSON
/// Schema string `schema`. Checked wherever a caller-supplied payload
/// crosses into the system - `create_external_event`/`create_direct_event`
/// for an event's, `authorise_command_trigger`/`authorise_command_submission`
/// for a command's - and nowhere else: a payload produced by a bounded
/// context's own compiled-in code (`decide()`'s `EventSpec`s,
/// `scheduled_payload`) carries the same obligation but is never
/// re-checked at runtime (see that note's own explanation of why). `false`
/// for a `schema`/`payload` that doesn't even parse as JSON, or a
/// `schema` that isn't itself a valid JSON Schema document - the same
/// "malformed input never validates" register `schema_properties`/
/// `schema_definitions` already use elsewhere in this module, just
/// surfaced here as an outright rejection rather than an absent
/// `properties`/`definitions` map.
pub fn valid_payload(schema: &str, payload: &str) -> bool {
    let Ok(schema_value) = serde_json::from_str::<serde_json::Value>(schema) else {
        return false;
    };
    let Ok(payload_value) = serde_json::from_str::<serde_json::Value>(payload) else {
        return false;
    };
    let Ok(validator) = jsonschema::validator_for(&schema_value) else {
        return false;
    };
    validator.is_valid(&payload_value)
}

/// The idempotency-key namespace `CrossContextRoute`'s own background
/// task (`db::catch_up_cross_context_route`, docs/architecture.md §36)
/// reserves for its own internally-derived keys
/// (`"{RESERVED_IDEMPOTENCY_KEY_PREFIX}{route_name}:{source_event_sequence}"`) -
/// see `Error::ReservedIdempotencyKeyPrefix`'s own doc comment for why
/// this needs to be reserved at all. Chosen to be something no ordinary
/// caller-meaningful idempotency key (a Temporal `run_id:activity_id`,
/// an app's own UUID, anything human-chosen) would plausibly start
/// with by accident - not a guarantee against a caller who deliberately
/// (or coincidentally) picks a key starting with this exact literal
/// prefix, who gets rejected same as an attacker would; low enough
/// odds in practice to accept as this mechanism's tradeoff.
///
/// **No longer load-bearing** since docs/architecture.md §37's
/// `client_id`-scoped `idempotency_keys` (`db::ensure_idempotency_keys_table`'s
/// own doc comment) - no external caller's `client_id` is ever
/// caller-suppliable, so no external submission could land under
/// `CrossContextRoute`'s own partition regardless of what
/// `idempotency_key` string it used, prefix or not. Kept anyway as
/// harmless defense-in-depth (the same register the `Deduplicated`
/// warning in `catch_up_cross_context_route` already keeps for a
/// scenario the reservation itself is meant to make unreachable).
pub const RESERVED_IDEMPOTENCY_KEY_PREFIX: &str = "skilj-cross-context-route:";

/// Checked by `authorise_command_trigger`/`authorise_command_submission`'s
/// own two REST/GraphQL callers, immediately after either reads a
/// caller-supplied `idempotencyKey`/`Idempotency-Key` value - never
/// folded into those two functions themselves, since neither is on
/// `decide_and_submit_command`'s internal `CrossContextRoute` caller's
/// own path (that caller reaches `decide_and_submit_command` directly,
/// bypassing both), so centralising there would have been safe; kept
/// at the call site instead purely to match the "wire-boundary concern"
/// register `submitCommand`'s own `required_role` gate already uses
/// (see that resolver's own doc comment) rather than out of necessity.
/// `Ok(())` for `None` - omitting an idempotency key entirely is always
/// fine, the same "unchanged behaviour when absent" every other
/// idempotency-key caller already gets. See `RESERVED_IDEMPOTENCY_KEY_PREFIX`'s
/// own doc comment for why this check is no longer this mechanism's
/// real defense, just a harmless second layer on top of it.
pub fn reject_reserved_idempotency_key(idempotency_key: Option<&str>) -> crate::error::Result<()> {
    match idempotency_key {
        Some(key) if key.starts_with(RESERVED_IDEMPOTENCY_KEY_PREFIX) => {
            Err(Error::ReservedIdempotencyKeyPrefix.into())
        }
        _ => Ok(()),
    }
}

/// Black box (see the note above rule `RegisterEventType`): the four-
/// bullet contract stated there, in full - a declared field is
/// permanent, requiredness may only loosen, a newly added field must be
/// optional, and a field's declared `type` never changes. Real, not
/// deferred: unlike `valid_tag_mappings`/`valid_sensitive_fields`, this
/// contract governs the top-level schema only by its own text ("a nested
/// shape's own internal evolution... is not tracked, validated or
/// enforced here at all" - see the note above `entity CommandType`), so
/// there is no dotted-path elaboration left to defer.
pub fn schema_is_backwards_compatible(existing_schema: &str, schema: &str) -> bool {
    if existing_schema == schema {
        return true;
    }
    let (Some(existing_properties), Some(new_properties)) = (
        schema_properties(existing_schema),
        schema_properties(schema),
    ) else {
        return false;
    };
    let existing_required = schema_required(existing_schema);
    let new_required = schema_required(schema);

    for (field, existing_def) in &existing_properties {
        let Some(new_def) = new_properties.get(field) else {
            return false; // bullet 1: a declared field is permanent
        };
        if existing_def.get("type") != new_def.get("type") {
            return false; // bullet 4: a field's declared type never changes
        }
        let was_required = existing_required.iter().any(|f| f == field);
        let now_required = new_required.iter().any(|f| f == field);
        if !was_required && now_required {
            return false; // bullet 2: optional -> required is tightening, forbidden
        }
    }
    for field in new_properties.keys() {
        if !existing_properties.contains_key(field) && new_required.iter().any(|f| f == field) {
            return false; // bullet 3: a newly added field must be optional
        }
    }
    true
}

/// Classic SQL-LIKE matching for `FilterOperator::IsLike` - `%` matches
/// any run of characters (including none), `_` matches exactly one
/// character, everything else matches itself literally. Case-sensitive,
/// whole-string anchored (no implicit substring search - that's what
/// `Contains` is for). Standard DP wildcard-matching, operating on
/// `Vec<char>` for UTF-8 safety rather than byte indexing; no `regex`
/// dependency needed for this.
fn like_matches(text: &str, pattern: &str) -> bool {
    let text: Vec<char> = text.chars().collect();
    let pattern: Vec<char> = pattern.chars().collect();
    let mut dp = vec![vec![false; pattern.len() + 1]; text.len() + 1];
    dp[0][0] = true;
    for j in 1..=pattern.len() {
        if pattern[j - 1] == '%' {
            dp[0][j] = dp[0][j - 1];
        }
    }
    for i in 1..=text.len() {
        for j in 1..=pattern.len() {
            dp[i][j] = match pattern[j - 1] {
                '%' => dp[i - 1][j] || dp[i][j - 1],
                '_' => dp[i - 1][j - 1],
                c => dp[i - 1][j - 1] && text[i - 1] == c,
            };
        }
    }
    dp[text.len()][pattern.len()]
}

/// `GreaterThan`/`LessThan` for a string leaf. `matches_filters` has no
/// schema, so unlike `valid_filters` it can't know in advance which of
/// the three ordered formats (`date-time`/`date`/`partial-date-time`)
/// it's looking at - tries each of `chrono::DateTime`'s/`NaiveDate`'s/
/// `NaiveTime`'s own parsers in turn on *both* sides, since each format
/// only parses its own shape. Real typed comparison (`PartialOrd`), not
/// raw string `Ord` - plain string comparison is provably wrong here:
/// chrono's own serde serialization trims trailing-zero fractional
/// digits, even down to no fractional part at all when it's exactly
/// zero, so e.g. `"...:00Z"` sorts *after* `"...:00.500Z"` as plain
/// strings despite being chronologically earlier (`'Z'` > `'.'`).
/// `valid_filters` already gated the field to one of these three formats
/// before a caller could reach this at all; a value that doesn't parse
/// under any of them here just doesn't match - the same "reject
/// gracefully rather than panic" register as everywhere else in this
/// module.
fn string_ordering(payload: &str, filter_value: &str, operator: FilterOperator) -> bool {
    fn cmp<T: PartialOrd>(a: T, b: T, operator: FilterOperator) -> bool {
        match operator {
            FilterOperator::GreaterThan => a > b,
            FilterOperator::LessThan => a < b,
            _ => false,
        }
    }
    if let (Ok(a), Ok(b)) = (
        chrono::DateTime::parse_from_rfc3339(payload),
        chrono::DateTime::parse_from_rfc3339(filter_value),
    ) {
        return cmp(a, b, operator);
    }
    if let (Ok(a), Ok(b)) = (
        chrono::NaiveDate::parse_from_str(payload, "%Y-%m-%d"),
        chrono::NaiveDate::parse_from_str(filter_value, "%Y-%m-%d"),
    ) {
        return cmp(a, b, operator);
    }
    if let (Ok(a), Ok(b)) = (
        chrono::NaiveTime::parse_from_str(payload, "%H:%M:%S%.f"),
        chrono::NaiveTime::parse_from_str(filter_value, "%H:%M:%S%.f"),
    ) {
        return cmp(a, b, operator);
    }
    false
}

/// `FilterOperator::Near` - `payload` is a stored `"lat,lng"` string,
/// `filter_value` is `"lat,lng,radius_meters"` (the query point plus how
/// close counts as "near"). Haversine great-circle distance, `f64`
/// throughout - plenty precise for anything this library's own filtering
/// needs (not a geodesy library). Any parse failure on either side (not
/// exactly two/three comma-separated numbers) just doesn't match, same
/// "reject gracefully" register as `string_ordering`.
fn geo_distance_within(payload: &str, filter_value: &str) -> bool {
    fn parse_point(s: &str) -> Option<(f64, f64)> {
        let mut parts = s.splitn(2, ',');
        let lat = parts.next()?.trim().parse::<f64>().ok()?;
        let lng = parts.next()?.trim().parse::<f64>().ok()?;
        Some((lat, lng))
    }
    let Some((lat1, lng1)) = parse_point(payload) else {
        return false;
    };
    let mut filter_parts = filter_value.splitn(3, ',');
    let (Some(lat2_str), Some(lng2_str), Some(radius_str)) = (
        filter_parts.next(),
        filter_parts.next(),
        filter_parts.next(),
    ) else {
        return false;
    };
    let (Ok(lat2), Ok(lng2), Ok(radius_meters)) = (
        lat2_str.trim().parse::<f64>(),
        lng2_str.trim().parse::<f64>(),
        radius_str.trim().parse::<f64>(),
    ) else {
        return false;
    };
    const EARTH_RADIUS_METERS: f64 = 6_371_000.0;
    let (phi1, phi2) = (lat1.to_radians(), lat2.to_radians());
    let d_phi = (lat2 - lat1).to_radians();
    let d_lambda = (lng2 - lng1).to_radians();
    let a = (d_phi / 2.0).sin().powi(2) + phi1.cos() * phi2.cos() * (d_lambda / 2.0).sin().powi(2);
    let distance_meters = EARTH_RADIUS_METERS * 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
    distance_meters <= radius_meters
}

/// `FilterOperator::SimilarColor` - `payload` is a stored `"#RRGGBB"`
/// string, `filter_value` is `"#RRGGBB,max_distance"`. Plain Euclidean
/// distance over the three RGB channels (range `0.0..=441.67`, i.e.
/// `sqrt(255^2 * 3)`) - deliberately not a perceptual metric (CIE ΔE
/// would need a color-science dependency this doesn't warrant); the doc
/// comment on `FilterOperator::SimilarColor` says so too, so a caller
/// isn't misled about what "similar" means here.
fn color_similarity_within(payload: &str, filter_value: &str) -> bool {
    fn parse_hex(s: &str) -> Option<(f64, f64, f64)> {
        let s = s.trim().strip_prefix('#')?;
        if s.len() != 6 {
            return None;
        }
        let r = u8::from_str_radix(&s[0..2], 16).ok()? as f64;
        let g = u8::from_str_radix(&s[2..4], 16).ok()? as f64;
        let b = u8::from_str_radix(&s[4..6], 16).ok()? as f64;
        Some((r, g, b))
    }
    let Some((r1, g1, b1)) = parse_hex(payload) else {
        return false;
    };
    let mut filter_parts = filter_value.splitn(2, ',');
    let (Some(color_str), Some(max_distance_str)) = (filter_parts.next(), filter_parts.next())
    else {
        return false;
    };
    let Some((r2, g2, b2)) = parse_hex(color_str) else {
        return false;
    };
    let Ok(max_distance) = max_distance_str.trim().parse::<f64>() else {
        return false;
    };
    let distance = ((r1 - r2).powi(2) + (g1 - g2).powi(2) + (b1 - b2).powi(2)).sqrt();
    distance <= max_distance
}

/// `FilterOperator::InSubnet` - `payload` is a stored IPv4/IPv6 address
/// string, `filter_value` is a CIDR (e.g. `"192.168.1.0/24"`,
/// `"2001:db8::/32"`). Delegates the actual containment check to
/// `ipnet` rather than hand-rolled bitwise subnet math - IPv6 in
/// particular is easy to get subtly wrong by hand.
fn ip_in_subnet(payload: &str, filter_value: &str) -> bool {
    let Ok(addr) = payload.trim().parse::<std::net::IpAddr>() else {
        return false;
    };
    let Ok(subnet) = filter_value.trim().parse::<ipnet::IpNet>() else {
        return false;
    };
    subnet.contains(&addr)
}

/// `FilterOperator::In` - `filter_value` is a comma-separated list of
/// candidates, no escaping (same as `IsLike`'s `%`/`_` wildcards already
/// being unescaped). Valid against any scalar leaf; reuses
/// `json_scalar_to_string` (the same string representation the `Array`/
/// `Contains` arm below already uses) so `In` is exactly "equals one of",
/// not its own comparison logic.
fn matches_any_of(value: &serde_json::Value, filter_value: &str) -> bool {
    let Some(value_as_string) = json_scalar_to_string(value) else {
        return false;
    };
    filter_value
        .split(',')
        .any(|candidate| candidate == value_as_string)
}

fn matches_one_filter(payload: &serde_json::Value, filter: &Filter) -> bool {
    // Absent field never matches a comparison filter - `valid_filters`
    // guarantees the field exists and is scalar/list-shaped in the
    // *schema*; this only fires for a legitimately-optional field absent
    // on this one specific event instance.
    let Some(value) = payload_field_value(payload, &filter.field) else {
        return false;
    };
    match value {
        serde_json::Value::Array(items) => {
            filter.operator == FilterOperator::Contains
                && items.iter().any(|item| {
                    json_scalar_to_string(item).as_deref() == Some(filter.value.as_str())
                })
        }
        serde_json::Value::String(s) => match filter.operator {
            FilterOperator::Equals => s == &filter.value,
            FilterOperator::Contains => s.contains(filter.value.as_str()),
            FilterOperator::IsLike => like_matches(s, &filter.value),
            FilterOperator::GreaterThan | FilterOperator::LessThan => {
                string_ordering(s, &filter.value, filter.operator)
            }
            FilterOperator::Near => geo_distance_within(s, &filter.value),
            FilterOperator::SimilarColor => color_similarity_within(s, &filter.value),
            FilterOperator::InSubnet => ip_in_subnet(s, &filter.value),
            FilterOperator::In => matches_any_of(value, &filter.value),
        },
        serde_json::Value::Number(n) => {
            if filter.operator == FilterOperator::In {
                return matches_any_of(value, &filter.value);
            }
            let (Some(a), Ok(b)) = (n.as_f64(), filter.value.parse::<f64>()) else {
                return false;
            };
            match filter.operator {
                FilterOperator::Equals => a == b,
                FilterOperator::GreaterThan => a > b,
                FilterOperator::LessThan => a < b,
                FilterOperator::Contains
                | FilterOperator::IsLike
                | FilterOperator::Near
                | FilterOperator::SimilarColor
                | FilterOperator::InSubnet
                | FilterOperator::In => false,
            }
        }
        serde_json::Value::Bool(b) => match filter.operator {
            FilterOperator::Equals => b.to_string() == filter.value,
            FilterOperator::In => matches_any_of(value, &filter.value),
            _ => false,
        },
        serde_json::Value::Null | serde_json::Value::Object(_) => false,
    }
}

/// Black box (see the note above rule `FetchEvents`, also used by
/// `ConsumeEvents`/`DeliverToSubscriptions`). Real now: parses
/// `event.payload` once, then per filter resolves `field` via
/// `payload_field_value` (bare or dotted, real since `derive_tags`) and
/// matches on the actual runtime `serde_json::Value` - no schema access
/// needed here, unlike `valid_filters`, which is what already gates
/// which operator a given field can ever receive. A payload that somehow
/// fails to parse as JSON (shouldn't happen - always valid JSON by the
/// time it's stored) falls through to no match rather than panicking.
pub fn matches_filters(event: &Event, filters: &[Filter]) -> bool {
    if filters.is_empty() {
        return true;
    }
    let Ok(payload) = serde_json::from_str::<serde_json::Value>(&event.payload) else {
        return false;
    };
    filters.iter().all(|f| matches_one_filter(&payload, f))
}

/// Cross-tenant read/write fix (docs/architecture.md's own write-up of
/// these passes) - the shared core `event_owner_scope_satisfied`/
/// `command_owner_scope_satisfied` below each delegate to, and the one
/// the write-side `authorise_command_submission`/`authorise_command_trigger`/
/// `create_external_event`/`create_direct_event` call directly, since
/// none of those have an `Event`/`Command` yet to read `.tags`/
/// `.event_type.owner_tag_key` off of at the point they need this check -
/// the record does not exist until after it passes. Takes the derived
/// `tags` and the type's own `owner_tag_key` as plain values instead, so
/// every caller - already-materialized or not-yet-created alike - feeds
/// it identically.
///
/// Holds - the record is visible/included, or in the write-side case,
/// permitted to be created - when `scope` is `None` (unrestricted, every
/// caller's behaviour before `scope` existed), or `owner_tag_key` is
/// `None` (this type declares no owner dimension, so no `scope` value
/// ever restricts it), or `tags` carries a tag whose key equals
/// `owner_tag_key` and whose value equals `scope`. Does not hold - fails
/// closed, the "affirmatively provable, not merely un-contradicted"
/// stance every caller of this shares - when `scope` is `Some`, the type
/// does declare `owner_tag_key`, and no tag carries that key with a
/// matching value (including no such tag at all, or one with a null
/// value - the "mapped field was absent" case, see `Tag.value` in the
/// spec).
pub fn tag_owner_scope_satisfied(
    tags: &[Tag],
    owner_tag_key: Option<&str>,
    scope: Option<&str>,
) -> bool {
    let Some(scope) = scope else {
        return true;
    };
    let Some(owner_tag_key) = owner_tag_key else {
        return true;
    };
    tags.iter()
        .any(|tag| tag.key == owner_tag_key && tag.value.as_deref() == Some(scope))
}

/// Cross-tenant read fix (docs/architecture.md's own write-up of these
/// passes) - the per-event sibling of `projections::query_projection`'s
/// own `tag_owner_scope_satisfied`, same idea applied to a raw `Event`
/// instead of a projection instance; delegates to `tag_owner_scope_satisfied`
/// above with `event.tags`/`event.event_type.owner_tag_key`. Takes a raw
/// `scope: Option<&str>` rather than a whole `RoleAccessMapping`, so both
/// the GraphQL track (`RoleAccessMapping.scope`) and the REST track
/// (`EventReadToken.scope`) feed it identically - `query_events`/
/// `count_events`/`deliver_to_subscriptions`/`inspect_event` call it with
/// the former, `fetch_events`/`consume_events` with the latter.
///
/// A multi-record surface (`query_events`/`count_events`/
/// `deliver_to_subscriptions`/`fetch_events`/`consume_events`) uses this
/// as a `.filter()`: a non-owned event is silently excluded, the call
/// still succeeds. A single-record surface (`inspect_event`) rejects
/// outright when it returns `false`, the same shape
/// `query_projection`'s own check has.
pub fn event_owner_scope_satisfied(event: &Event, scope: Option<&str>) -> bool {
    tag_owner_scope_satisfied(
        &event.tags,
        event.event_type.owner_tag_key.as_deref(),
        scope,
    )
}

/// `event_owner_scope_satisfied`'s own sibling for `Command`, read by
/// `fetch_commands` (`FetchCommands`' `command_owner_scope_satisfied`).
/// Identical contract - see that function's own doc comment for the full
/// three-holds/fails-closed reasoning, not restated here - delegating to
/// `tag_owner_scope_satisfied` above with
/// `command.consistency_tags`/`command.command_type.owner_tag_key` where
/// the event version delegates with `Event.tags`/`EventType.owner_tag_key`.
/// `consistency_tags` is the right field for this: it is always
/// `derive_tags(command_type, payload)`, unconditionally - a plain
/// `Vec<Tag>`, never absent - regardless of whether this particular
/// command actually used a consistency boundary; only `consistency_boundary`
/// itself goes missing for that case (see `Command.consistency_boundary`'s
/// own doc comment).
pub fn command_owner_scope_satisfied(command: &Command, scope: Option<&str>) -> bool {
    tag_owner_scope_satisfied(
        &command.consistency_tags,
        command.command_type.owner_tag_key.as_deref(),
        scope,
    )
}

/// The greatest `Event.sequence` among a set of events, or `None` when
/// empty - see the note above rule `ConsumeEvents`.
fn highest_sequence(events: &[Event]) -> Option<i64> {
    events.iter().map(|e| e.sequence).max()
}

/// Black box (see the note above rule `CreateExternalEvent`), now real for
/// both branches. Still pure/I/O-free (§1.1) - `resolve_key` is a plain
/// `Fn` closure the caller builds from *already*-provisioned
/// `EncryptionKey`s (`db::get_or_create_encryption_key`, called once per
/// distinct subject `sensitive_field_subjects` names, before this is ever
/// invoked) - the identical "pure core needs a value only I/O can
/// produce, so the caller pre-resolves it and hands over a closure"
/// pattern `process_command`'s own `resolve_event_type`/`next_sequence`
/// parameters already use, not a new one (see docs/architecture.md's
/// write-up of this pass for the full reasoning). An unresolved lookup is
/// a caller bug - `resolve_key` is trusted to always have what's needed,
/// the same trust convention `next_sequence`'s own closure already gets.
///
/// Takes `sensitive_fields` alone, not a whole `EventType`/`CommandType`,
/// since "it takes an EventType and a CommandType interchangeably" per
/// the spec's own note - `sensitive_fields` is the identical
/// `Set<SensitiveField>` on both. A field or its own `subject_field`
/// missing from `payload` (an optional field the caller omitted) is
/// skipped rather than encrypted with nothing to key it by - the same
/// "absence reads as empty/no-op" register `schema_properties`/
/// `schema_required` already use elsewhere in this module.
pub fn protect_sensitive_fields(
    sensitive_fields: &[SensitiveField],
    payload: &str,
    resolve_key: impl Fn(&str, &str) -> (EncryptionKey, DataKey),
) -> ProtectedPayload {
    if sensitive_fields.is_empty() {
        return ProtectedPayload {
            payload: payload.to_string(),
            encryption_keys: Vec::new(),
        };
    }

    let mut parsed: serde_json::Value = serde_json::from_str(payload).expect(
        "protect_sensitive_fields: payload is always valid JSON by the time this is called",
    );
    let mut used_keys: Vec<EncryptionKey> = Vec::new();

    for sf in sensitive_fields {
        let Some(subject_value) =
            payload_field_value(&parsed, &sf.subject_field).and_then(json_scalar_to_string)
        else {
            continue;
        };
        let Some(plaintext) = payload_field_value(&parsed, &sf.field).map(|v| {
            v.as_str()
                .map(str::to_string)
                .unwrap_or_else(|| v.to_string())
        }) else {
            continue;
        };

        let (key, data_key) = resolve_key(&sf.subject_key, &subject_value);
        let ciphertext = crate::encryption::encrypt_leaf(&data_key, &plaintext);

        if let Some(slot) = payload_field_value_mut(&mut parsed, &sf.field) {
            *slot = serde_json::Value::String(ciphertext);
        }
        if !used_keys.contains(&key) {
            used_keys.push(key);
        }
    }

    ProtectedPayload {
        payload: serde_json::to_string(&parsed)
            .expect("re-serialising a parsed JSON Value is infallible"),
        encryption_keys: used_keys,
    }
}

/// Every distinct `(subject_key, subject_value)` pair `payload` will need
/// an `EncryptionKey` for, per `sensitive_fields` - `protect_sensitive_fields`'s
/// own caller uses this to know what to pre-provision (via
/// `db::get_or_create_encryption_key`) before ever calling it, the same
/// pre-resolution step `resolve_event_type`/`next_sequence` already need.
/// Pure, no DB, no encryption - just a payload walk. A field whose
/// `subject_field` is absent or non-scalar contributes nothing, matching
/// `protect_sensitive_fields`'s own skip for that case.
pub fn sensitive_field_subjects(
    sensitive_fields: &[SensitiveField],
    payload: &str,
) -> Vec<(String, String)> {
    if sensitive_fields.is_empty() {
        return Vec::new();
    }
    let parsed: serde_json::Value = serde_json::from_str(payload).expect(
        "sensitive_field_subjects: payload is always valid JSON by the time this is called",
    );

    let mut subjects = Vec::new();
    for sf in sensitive_fields {
        if let Some(subject_value) =
            payload_field_value(&parsed, &sf.subject_field).and_then(json_scalar_to_string)
        {
            let pair = (sf.subject_key.clone(), subject_value);
            if !subjects.contains(&pair) {
                subjects.push(pair);
            }
        }
    }
    subjects
}

/// Pushes `Tag { key, value }` onto `tags` unless an identical tag is
/// already present - `derive_tags`'s own "Set<Tag>" dedup, kept as a
/// small helper rather than inlined since it's needed at two call sites
/// below (the per-mapping "absent" fallback, and each distinct list
/// element).
fn push_unique_tag(tags: &mut Vec<Tag>, key: &str, value: Option<String>) {
    let tag = Tag {
        key: key.to_string(),
        value,
    };
    if !tags.contains(&tag) {
        tags.push(tag);
    }
}

/// Black box (see the note above `derive_tags` in the spec). Real now:
/// per `TagMapping`, `payload_field_value` resolves `field` (bare or
/// dotted, see its own doc comment) against the parsed payload. A
/// non-empty JSON array pushes one `Tag(key, value: e)` per **distinct**
/// scalar element (`json_scalar_to_string`) - a non-scalar element is
/// silently skipped, undefined input rather than a spurious "absent"
/// tag. Everything else - a present scalar, an explicit `null`, an empty
/// array, or the field missing entirely - falls through to
/// `json_scalar_to_string`'s own `None`-for-non-scalar behaviour, which
/// already collapses every one of those onto the single correct
/// `Tag(key, value: null)` "absent" state with no extra branching.
/// `tag_mappings` empty needs no payload parse at all.
pub fn derive_tags(tag_mappings: &[TagMapping], payload: &str) -> Vec<Tag> {
    if tag_mappings.is_empty() {
        return Vec::new();
    }
    let parsed: serde_json::Value =
        serde_json::from_str(payload).unwrap_or(serde_json::Value::Null);
    let mut tags = Vec::new();
    for mapping in tag_mappings {
        let value = payload_field_value(&parsed, &mapping.field);
        match value.and_then(|v| v.as_array()) {
            Some(elements) if !elements.is_empty() => {
                for element in elements {
                    if let Some(scalar) = json_scalar_to_string(element) {
                        push_unique_tag(&mut tags, &mapping.key, Some(scalar));
                    }
                }
            }
            _ => {
                let scalar = value.and_then(json_scalar_to_string);
                push_unique_tag(&mut tags, &mapping.key, scalar);
            }
        }
    }
    tags
}

/// The two-grant test itself (see the note above rule
/// `DeliverToSubscriptions`, restated identically above `QueryEvents`/
/// `FetchCommands`/`QueryProjection`): a sensitive field decrypts under
/// either `access_mapping.can_read_sensitive`, or the caller's own
/// verified identity matching the specific subject that field is about.
/// Factored out as its own function so `render_event`/`render_command`
/// (whether to actually substitute a decrypted value) and their own
/// caller's pre-resolution step (which subjects are even worth fetching
/// a key for) share the identical check, never two independently-drifting
/// copies of the same boolean.
pub fn sensitive_field_is_granted(access_mapping: &RoleAccessMapping, subject_value: &str) -> bool {
    access_mapping.can_read_sensitive || access_mapping.role.external_subject == subject_value
}

/// The private-field mechanism's own accessor trait: `is_default_private_reader`
/// and the render-time redaction pass below read the same three things
/// off an `Event` or a `Command` alike rather than being duplicated per
/// type - the identical role `protect_sensitive_fields`'s own shared walk
/// already plays for `sensitive_fields`, just needing one more field read
/// (`private_fields`) than that walk does.
pub trait PrivateFieldRecord {
    fn private_fields(&self) -> &[PrivateField];
    fn client_id(&self) -> &str;
    fn payload(&self) -> &str;
}

impl PrivateFieldRecord for Event {
    fn private_fields(&self) -> &[PrivateField] {
        &self.event_type.private_fields
    }
    fn client_id(&self) -> &str {
        &self.metadata.client_id
    }
    fn payload(&self) -> &str {
        &self.payload
    }
}

impl PrivateFieldRecord for Command {
    fn private_fields(&self) -> &[PrivateField] {
        &self.command_type.private_fields
    }
    fn client_id(&self) -> &str {
        &self.metadata.client_id
    }
    fn payload(&self) -> &str {
        &self.payload
    }
}

/// `is_default_private_reader(record, access_mapping)` in the spec -
/// takes `role: &Role` rather than a whole `&RoleAccessMapping` here,
/// since nothing else off it is ever read: `RoleAccessMapping.status`/
/// `.level`/`.bounded_context` play no part in this specific question,
/// only `.role.id`/`.role.external_subject` do. That narrower signature
/// is what lets `PrivateFieldGrant.grantor` - a bare `Role`, no
/// `RoleAccessMapping` alongside it - be re-checked here directly at
/// render time (see the redaction pass below) without fabricating one.
///
/// Holds when either of two things is true, read off `record`'s own
/// type's `private_fields`:
///   - some `Own`-kind field is declared and `record`'s own `client_id`
///     equals `role.id` - the caller is the record's creator (`client_id`
///     is the Role's internal id for anything a GraphQL caller produced -
///     see `authorise_command_submission`), or
///   - some `Addressed`-kind field is declared and `role.external_subject`
///     equals the value that field's own `addressee_field` names in
///     `record`'s payload - the caller is the party the record itself
///     addressed, the identical match a sensitive field's subject already
///     gets in `render_event`/`render_command`, resolved against a
///     payload field rather than an `EncryptionKey`'s subject value.
///
/// Does not hold for a record whose type declares only `Team`-kind
/// private fields, or none at all - there is no default reader to be.
/// Generic over `Event`/`Command` alike via `PrivateFieldRecord` above -
/// used both here and at grant time (`grant_private_field_access_for_event`/
/// `grant_private_field_access_for_command`), one predicate, several call
/// sites, never two implementations of the same question.
pub fn is_default_private_reader<R: PrivateFieldRecord>(record: &R, role: &Role) -> bool {
    let private_fields = record.private_fields();
    if private_fields
        .iter()
        .any(|p| p.kind == PrivateFieldKind::Own)
        && record.client_id() == role.id
    {
        return true;
    }
    if !private_fields
        .iter()
        .any(|p| p.kind == PrivateFieldKind::Addressed)
    {
        return false;
    }
    let Ok(parsed) = serde_json::from_str::<serde_json::Value>(record.payload()) else {
        return false;
    };
    private_fields.iter().any(|p| {
        p.kind == PrivateFieldKind::Addressed
            && p.addressee_field
                .as_deref()
                .and_then(|af| payload_field_value(&parsed, af))
                .and_then(json_scalar_to_string)
                .is_some_and(|addressee| addressee == role.external_subject)
    })
}

/// Whether `access_mapping.role` may read one `PrivateField` entry on
/// `record` - the one decision the redaction pass below makes per field.
/// `Team`-kind needs no `record`/grant lookup at all: membership is the
/// grant. `Own`/`Addressed` hold when `is_default_private_reader` holds
/// directly, or an active `PrivateFieldGrant` reaches this caller -
/// either a per-record one `matches_this_record` identifies, or a
/// blanket one (naming neither an event nor a command) whose own
/// `grantor` independently satisfies `is_default_private_reader` for
/// `record` - re-derived here, against the record actually being read,
/// which is what keeps a blanket grant a convenience rather than a wider
/// trust boundary (see `entity PrivateFieldGrant`'s own doc comment).
fn entitled_to_read_private_field<R: PrivateFieldRecord>(
    record: &R,
    kind: PrivateFieldKind,
    team: Option<&str>,
    access_mapping: &RoleAccessMapping,
    grants: &[PrivateFieldGrant],
    matches_this_record: impl Fn(&PrivateFieldGrant) -> bool,
) -> bool {
    match kind {
        PrivateFieldKind::Team => {
            crate::access_control::role_matches_required_team(&access_mapping.role, team)
        }
        PrivateFieldKind::Own | PrivateFieldKind::Addressed => {
            is_default_private_reader(record, &access_mapping.role)
                || grants.iter().any(|g| {
                    g.status == TokenStatus::Active
                        && g.grantee == access_mapping.role
                        && (matches_this_record(g)
                            || (g.event_sequence.is_none()
                                && g.command_id.is_none()
                                && is_default_private_reader(record, &g.grantor)))
                })
        }
    }
}

/// Black box (see the note above rule `DeliverToSubscriptions`, reused by
/// `QueryEvents`/`CountEvents`/`InspectEvent` and by `crate::projections::
/// read_projection` too - see its own doc comment for its real, since
/// implemented, once-per-query treatment). Real now, both branches: per
/// `sensitive_fields` entry, `sensitive_field_is_granted`
/// decides whether to even attempt a decrypt; `resolve_data_key` (the
/// caller's own pre-resolved closure, the identical "impure resolution,
/// pure decision" split `resolve_key` already has on the write side) is
/// only ever called for a field this caller is actually granted. `None`
/// back from it - no active key, whether never provisioned or destroyed
/// by `ForgetSubject` - leaves the leaf untouched, correct crypto-
/// shredding behaviour with no special-casing needed. A field that *is*
/// granted a key but fails to decrypt (`encryption::decrypt_leaf`
/// returning `Err`) is left untouched too, not a panic - the spec's own
/// acknowledged case of a historical row that predates `sensitive_fields`
/// being declared on this type, so the leaf was never actually ciphertext
/// to begin with (see the note above `AddBoundedContext`/`ForgetSubject`:
/// "declaring it later protects the future only, never the past"). A
/// field with neither grant is never even attempted - "left as stored
/// ciphertext, never decrypted and then redacted afterwards" per the
/// spec's own repeated wording. The decrypted leaf is always rendered as
/// a JSON string regardless of the field's original scalar type - an
/// already-shipped consequence of `protect_sensitive_fields` itself
/// always storing ciphertext as a string leaf, discarding the original
/// type at encrypt time; restoring it would need schema-aware re-parsing,
/// a separate, out-of-scope piece of work.
/// `grants` is the caller's own already-loaded snapshot of active
/// `PrivateFieldGrant`s naming `access_mapping.role` as grantee, within
/// this bounded context - the identical "pure function, I/O resolved
/// before the call" split every other rule in this module already
/// follows for `resolve_data_key`. Empty for a caller with no such
/// grants, which is every caller before this mechanism existed - the
/// third pass below is then just `is_default_private_reader`/`Team`
/// membership, unchanged from a `private_fields`-free type's own
/// behaviour.
pub fn render_event(
    event: &Event,
    access_mapping: &RoleAccessMapping,
    resolve_data_key: &impl Fn(&str, &str) -> Option<DataKey>,
    grants: &[PrivateFieldGrant],
) -> String {
    let decrypted = decrypt_sensitive_fields(
        &event.event_type.sensitive_fields,
        &event.payload,
        access_mapping,
        resolve_data_key,
    );
    redact_unentitled_private_fields(event, &decrypted, access_mapping, grants, |g| {
        g.bounded_context == event.bounded_context && g.event_sequence == Some(event.sequence)
    })
}

/// See `render_event` above - same black box, same real decrypt/redact
/// logic, applied to `Command.payload`/`CommandType.sensitive_fields`/
/// `CommandType.private_fields` instead. See `rule FetchCommands`' own
/// `@guidance` for why this is a distinct function rather than
/// `render_event` reused: the tests are identical, but the value being
/// rendered is a `Command`, not an `Event`.
pub fn render_command(
    command: &Command,
    access_mapping: &RoleAccessMapping,
    resolve_data_key: &impl Fn(&str, &str) -> Option<DataKey>,
    grants: &[PrivateFieldGrant],
) -> String {
    let decrypted = decrypt_sensitive_fields(
        &command.command_type.sensitive_fields,
        &command.payload,
        access_mapping,
        resolve_data_key,
    );
    redact_unentitled_private_fields(command, &decrypted, access_mapping, grants, |g| {
        g.bounded_context == command.bounded_context
            && g.command_id.as_deref() == Some(command.id.as_str())
    })
}

/// The redaction pass `render_event`/`render_command` both run after
/// decrypting sensitive fields - a plain visibility rule, not a
/// cryptographic one, with no `EncryptionKey`/`resolve_data_key`/
/// `ForgetSubject` involvement anywhere: a private field is stored in
/// plaintext exactly as written, and that absence is the whole point of
/// the mechanism (see `value PrivateField`). Per `PrivateField` entry,
/// `entitled_to_read_private_field` decides whether this caller may see
/// the leaf; a caller who isn't gets it set to `null` - not the enclosing
/// object, not the whole record withheld, and not ciphertext either,
/// since none was ever produced, exactly the shape and exactly the place
/// an unentitled caller already finds a sensitive field's leaf left as
/// stored ciphertext, one pass up.
fn redact_unentitled_private_fields<R: PrivateFieldRecord>(
    record: &R,
    payload: &str,
    access_mapping: &RoleAccessMapping,
    grants: &[PrivateFieldGrant],
    matches_this_record: impl Fn(&PrivateFieldGrant) -> bool,
) -> String {
    let private_fields = record.private_fields();
    if private_fields.is_empty() {
        return payload.to_string();
    }

    let mut parsed: serde_json::Value = serde_json::from_str(payload).expect(
        "redact_unentitled_private_fields: payload is always valid JSON by the time this is called",
    );
    for p in private_fields {
        if entitled_to_read_private_field(
            record,
            p.kind,
            p.team.as_deref(),
            access_mapping,
            grants,
            &matches_this_record,
        ) {
            continue;
        }
        if let Some(slot) = payload_field_value_mut(&mut parsed, &p.field) {
            *slot = serde_json::Value::Null;
        }
    }
    serde_json::to_string(&parsed).expect("re-serialising a parsed JSON Value is infallible")
}

/// `redact_private_fields(record)` in the spec - the REST track's
/// unconditional counterpart to the render-time pass above. No
/// `access_mapping`/`Role` argument at all, deliberately: an
/// `EventReadToken` is tied to no `Role`, so every `private_fields` leaf
/// is nulled for every caller alike, the same fail-closed-by-construction
/// shape `EventFetch`'s own `PrivateFieldsStayRedacted` guarantee states.
/// All three kinds fail closed structurally here, not by omission - a
/// token is nobody's `client_id`, carries no `Role.name`, carries no
/// `external_subject`, and no `PrivateFieldGrant` can name it as a
/// grantee (a grant's grantee is always a `Role`) - so no amount of
/// sharing on the GraphQL side ever opens a private field on this one.
/// Called by `fetch_events`/`consume_events` alone: `CommandQuery` is
/// GraphQL/`AdminAccess`-only and already routes through `render_command`,
/// and `CommandTrigger` reads nothing back, so there is no command-side
/// counterpart to this function.
pub fn redact_private_fields(event: &Event) -> Event {
    let private_fields = &event.event_type.private_fields;
    if private_fields.is_empty() {
        return event.clone();
    }
    let mut parsed: serde_json::Value = serde_json::from_str(&event.payload)
        .expect("redact_private_fields: payload is always valid JSON by the time this is called");
    for p in private_fields {
        if let Some(slot) = payload_field_value_mut(&mut parsed, &p.field) {
            *slot = serde_json::Value::Null;
        }
    }
    Event {
        payload: serde_json::to_string(&parsed)
            .expect("re-serialising a parsed JSON Value is infallible"),
        ..event.clone()
    }
}

/// The shared walk `render_event`/`render_command` both need - same
/// per-field shape `protect_sensitive_fields` itself already walks
/// (`payload_field_value`/`json_scalar_to_string` reused unchanged), just
/// decrypting in place instead of encrypting. Returns `payload` unchanged
/// (not even re-serialised) when `sensitive_fields` is empty - the
/// identical "empty is a no-op" register every other black box in this
/// module already uses.
fn decrypt_sensitive_fields(
    sensitive_fields: &[SensitiveField],
    payload: &str,
    access_mapping: &RoleAccessMapping,
    resolve_data_key: &impl Fn(&str, &str) -> Option<DataKey>,
) -> String {
    if sensitive_fields.is_empty() {
        return payload.to_string();
    }

    let mut parsed: serde_json::Value = serde_json::from_str(payload).expect(
        "decrypt_sensitive_fields: payload is always valid JSON by the time this is called",
    );

    for sf in sensitive_fields {
        let Some(subject_value) =
            payload_field_value(&parsed, &sf.subject_field).and_then(json_scalar_to_string)
        else {
            continue;
        };
        if !sensitive_field_is_granted(access_mapping, &subject_value) {
            continue;
        }
        let Some(data_key) = resolve_data_key(&sf.subject_key, &subject_value) else {
            continue;
        };
        let Some(ciphertext) = payload_field_value(&parsed, &sf.field).and_then(|v| v.as_str())
        else {
            continue;
        };
        let Ok(plaintext) = crate::encryption::decrypt_leaf(&data_key, ciphertext) else {
            continue;
        };
        if let Some(slot) = payload_field_value_mut(&mut parsed, &sf.field) {
            *slot = serde_json::Value::String(plaintext);
        }
    }

    serde_json::to_string(&parsed).expect("re-serialising a parsed JSON Value is infallible")
}

/// The outcome of `register_event_type` below - the spec's own `if exists
/// existing: ... else: EventType.created(...)` branch, made explicit
/// rather than folded into a single return value, so a caller (and a
/// test) can tell "this registration created a new row" from "this
/// registration updated one in place" without comparing timestamps.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventTypeRegistration {
    Created(EventType),
    Updated(EventType),
}

impl EventTypeRegistration {
    pub fn event_type(&self) -> &EventType {
        match self {
            EventTypeRegistration::Created(et) | EventTypeRegistration::Updated(et) => et,
        }
    }
}

/// See `rule RegisterEventType`. `existing` is `EventType{bounded_context,
/// name}` as already looked up by the caller - the same get-or-create
/// lookup treatment `consume_events`' `existing_cursor` gets. Checks run
/// in the spec's own `requires` order; the two `not exists existing or
/// ...` guards below only fire on the update path, exactly as written.
///
/// `now` is new - needed to anchor `schedule_position` the moment
/// scheduling is newly opted into (see the note above the rule: "opting
/// in also anchors the schedule position, and only opting in does"). Every
/// other field this function sets stays timestamp-free, so this is the
/// one place `EventType` itself needs a clock at all.
#[allow(clippy::too_many_arguments)]
pub fn register_event_type(
    access_mapping: &RoleAccessMapping,
    bounded_context: &BoundedContext,
    name: String,
    schema: String,
    tag_mappings: Vec<TagMapping>,
    owner_tag_key: Option<String>,
    sensitive_fields: Vec<SensitiveField>,
    private_fields: Vec<PrivateField>,
    external_creation_allowed: bool,
    direct_creation_allowed: bool,
    system_triggered_allowed: bool,
    system_triggered_schedule: Option<String>,
    missed_occurrence_policy: Option<MissedOccurrencePolicy>,
    event_read_allowed: bool,
    existing: Option<&EventType>,
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<EventTypeRegistration> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if &access_mapping.bounded_context != bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_schema(&schema) {
        return Err(Error::InvalidSchema.into());
    }
    if !valid_tag_mappings(&schema, &tag_mappings) {
        return Err(Error::InvalidTagMapping.into());
    }
    if !valid_owner_tag_key(&tag_mappings, owner_tag_key.as_deref()) {
        return Err(Error::InvalidOwnerTagKey.into());
    }
    if !valid_sensitive_fields(&schema, &sensitive_fields) {
        return Err(Error::InvalidSensitiveField.into());
    }
    if !valid_private_fields(&schema, &private_fields) {
        return Err(Error::InvalidPrivateField.into());
    }
    if tag_mappings
        .iter()
        .any(|m| sensitive_fields.iter().any(|s| s.field == m.field))
    {
        return Err(Error::SensitiveFieldTagOverlap.into());
    }
    // A private field may not also be a tag mapping or a sensitive
    // field - the identical leak `SensitiveFieldTagOverlap` above already
    // guards against, one register over (see `Error::PrivateFieldOverlap`'s
    // own doc comment).
    if tag_mappings
        .iter()
        .any(|m| private_fields.iter().any(|p| p.field == m.field))
        || sensitive_fields
            .iter()
            .any(|s| private_fields.iter().any(|p| p.field == s.field))
    {
        return Err(Error::PrivateFieldOverlap.into());
    }
    // Scheduling is opted into whole - a type saying it fires on a
    // schedule has to say when *and* what a missed occurrence means, no
    // default for either. A real new rejection: `system_triggered_allowed
    // = true` with no schedule used to be silently accepted here and
    // simply never fire.
    if system_triggered_allowed
        && (system_triggered_schedule.is_none() || missed_occurrence_policy.is_none())
    {
        return Err(Error::MissingScheduleOrPolicy.into());
    }

    // The one act that anchors schedule_position, whether for a brand
    // new type or one that had scheduling switched off - see the note
    // above the rule. Computed from the caller-supplied `existing`
    // before it's shadowed below.
    let scheduling_newly_enabled =
        system_triggered_allowed && existing.is_none_or(|e| !e.system_triggered_allowed);

    let Some(existing) = existing else {
        return Ok(EventTypeRegistration::Created(EventType {
            bounded_context: bounded_context.clone(),
            name,
            schema,
            schema_version: 1,
            tag_mappings,
            owner_tag_key,
            sensitive_fields,
            private_fields,
            external_creation_allowed,
            direct_creation_allowed,
            system_triggered_allowed,
            system_triggered_schedule,
            missed_occurrence_policy,
            schedule_position: scheduling_newly_enabled.then_some(now),
            last_fired_at: None,
            event_read_allowed,
        }));
    };

    if !schema_is_backwards_compatible(&existing.schema, &schema) {
        return Err(Error::SchemaIncompatible.into());
    }
    if !existing
        .tag_mappings
        .iter()
        .all(|m| tag_mappings.iter().any(|n| n.key == m.key))
    {
        return Err(Error::TagMappingKeyDropped.into());
    }

    let schema_changed = existing.schema != schema;
    Ok(EventTypeRegistration::Updated(EventType {
        bounded_context: bounded_context.clone(),
        name,
        schema,
        schema_version: if schema_changed {
            existing.schema_version + 1
        } else {
            existing.schema_version
        },
        tag_mappings,
        owner_tag_key,
        sensitive_fields,
        private_fields,
        external_creation_allowed,
        direct_creation_allowed,
        system_triggered_allowed,
        system_triggered_schedule,
        missed_occurrence_policy,
        // Re-registration leaves the position exactly where the
        // scheduler left it, unless this *is* the opt-in moment - a
        // deploy must not be a way to re-fire (resetting back) or skip
        // (resetting forward) a backlog. last_fired_at is never touched
        // here at all: what once fired stays a fact regardless of
        // re-registration.
        schedule_position: if scheduling_newly_enabled {
            Some(now)
        } else {
            existing.schedule_position
        },
        last_fired_at: existing.last_fired_at,
        event_read_allowed,
    }))
}

/// See `EventTypeRegistration` above - same shape, for `CommandType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandTypeRegistration {
    Created(CommandType),
    Updated(CommandType),
}

impl CommandTypeRegistration {
    pub fn command_type(&self) -> &CommandType {
        match self {
            CommandTypeRegistration::Created(ct) | CommandTypeRegistration::Updated(ct) => ct,
        }
    }
}

/// See `rule RegisterCommandType`. Same shape and reasoning as
/// `register_event_type` above - the only differences are the absence of
/// the three `EventType`-only permission fields and the presence of
/// `rest_trigger_allowed` in their place.
#[allow(clippy::too_many_arguments)]
pub fn register_command_type(
    access_mapping: &RoleAccessMapping,
    bounded_context: &BoundedContext,
    name: String,
    schema: String,
    tag_mappings: Vec<TagMapping>,
    owner_tag_key: Option<String>,
    sensitive_fields: Vec<SensitiveField>,
    private_fields: Vec<PrivateField>,
    rest_trigger_allowed: bool,
    existing: Option<&CommandType>,
) -> crate::error::Result<CommandTypeRegistration> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if &access_mapping.bounded_context != bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_schema(&schema) {
        return Err(Error::InvalidSchema.into());
    }
    if !valid_tag_mappings(&schema, &tag_mappings) {
        return Err(Error::InvalidTagMapping.into());
    }
    if !valid_owner_tag_key(&tag_mappings, owner_tag_key.as_deref()) {
        return Err(Error::InvalidOwnerTagKey.into());
    }
    if !valid_sensitive_fields(&schema, &sensitive_fields) {
        return Err(Error::InvalidSensitiveField.into());
    }
    if !valid_private_fields(&schema, &private_fields) {
        return Err(Error::InvalidPrivateField.into());
    }
    if tag_mappings
        .iter()
        .any(|m| sensitive_fields.iter().any(|s| s.field == m.field))
    {
        return Err(Error::SensitiveFieldTagOverlap.into());
    }
    // See the identical check in `register_event_type` - same reasoning.
    if tag_mappings
        .iter()
        .any(|m| private_fields.iter().any(|p| p.field == m.field))
        || sensitive_fields
            .iter()
            .any(|s| private_fields.iter().any(|p| p.field == s.field))
    {
        return Err(Error::PrivateFieldOverlap.into());
    }

    let Some(existing) = existing else {
        return Ok(CommandTypeRegistration::Created(CommandType {
            bounded_context: bounded_context.clone(),
            name,
            schema,
            schema_version: 1,
            tag_mappings,
            owner_tag_key,
            sensitive_fields,
            private_fields,
            rest_trigger_allowed,
        }));
    };

    if !schema_is_backwards_compatible(&existing.schema, &schema) {
        return Err(Error::SchemaIncompatible.into());
    }
    if !existing
        .tag_mappings
        .iter()
        .all(|m| tag_mappings.iter().any(|n| n.key == m.key))
    {
        return Err(Error::TagMappingKeyDropped.into());
    }

    let schema_changed = existing.schema != schema;
    Ok(CommandTypeRegistration::Updated(CommandType {
        bounded_context: bounded_context.clone(),
        name,
        schema,
        schema_version: if schema_changed {
            existing.schema_version + 1
        } else {
            existing.schema_version
        },
        tag_mappings,
        owner_tag_key,
        sensitive_fields,
        private_fields,
        rest_trigger_allowed,
    }))
}

/// See `rule ForgetSubject`. Same grant-shape checks
/// `archive_bounded_context`/`revoke_token` make (`access_mapping.status`/
/// `.level`/`.bounded_context`), reusing the same
/// `access_control::Error` variants, plus this module's own
/// `EncryptionKeyNotActive` for "already destroyed" - the same rejection
/// register `BoundedContextArchived`/`TokenNotActive` are in. Crypto-
/// shredding itself needs nothing beyond this status flip: every payload
/// ciphertext written under this key becomes unreadable the instant no
/// plaintext key material exists to reverse it, which this function's own
/// caller enacts by destroying the key row - there is no second action
/// this pure function performs or defers.
pub fn forget_subject(
    access_mapping: &RoleAccessMapping,
    key: &EncryptionKey,
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<EncryptionKey> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if access_mapping.bounded_context != key.bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if key.status != EncryptionKeyStatus::Active {
        return Err(Error::EncryptionKeyNotActive.into());
    }

    Ok(EncryptionKey {
        status: EncryptionKeyStatus::Destroyed,
        destroyed_at: Some(now),
        ..key.clone()
    })
}

/// See `rule QueryEvents`. `bounded_context_events` is every `Event`
/// matching what this call needs, the same full-snapshot shape
/// `consistency_boundary_and_matching_events` takes `Event`s in - real
/// callers source it via `db::list_events_for_bounded_context_cached`
/// (`crate::event_cache`'s own module doc comment has the full design),
/// not an unconditional Postgres load. An empty `event_types`/`tags` means
/// "no restriction", per the spec's own convention - not a vacuous case,
/// so both are checked with `is_empty()`/`is_none()` rather than treated
/// as "matches nothing".
///
/// Returns each candidate's own `sequence` alongside its rendered
/// payload, not the rendered payload alone - the wire contract around
/// what else a caller sees per event stays deferred, same register as
/// everywhere else this spec is "deliberately coarse on the wire
/// contract" (see the surface's own guidance), but `sequence` isn't
/// optional scenery: it's the one thing a paging caller needs back to
/// supply as the next call's own `after_sequence`, propagating
/// `skilj-graphql`'s `EventQuery` resolver (§8 item 5, Phase 3) - a
/// caller with only the rendered strings back could never page past the
/// first call.
#[allow(clippy::too_many_arguments)]
pub fn query_events(
    access_mapping: &RoleAccessMapping,
    event_types: &[EventType],
    tags: Option<&[Tag]>,
    after_sequence: Option<i64>,
    bounded_context_events: &[Event],
    resolve_data_key: impl Fn(&str, &str) -> Option<DataKey>,
    private_field_grants: &[PrivateFieldGrant],
) -> crate::error::Result<Vec<(i64, String)>> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if !event_types
        .iter()
        .all(|et| et.bounded_context == access_mapping.bounded_context)
    {
        return Err(Error::EventTypeNotInBoundedContext.into());
    }

    let after = after_sequence.unwrap_or(-1);
    Ok(bounded_context_events
        .iter()
        .filter(|e| e.bounded_context == access_mapping.bounded_context)
        .filter(|e| event_types.is_empty() || event_types.contains(&e.event_type))
        .filter(|e| tags.is_none_or(|wanted| wanted.iter().any(|t| e.tags.contains(t))))
        .filter(|e| e.sequence > after)
        .filter(|e| event_owner_scope_satisfied(e, access_mapping.scope.as_deref()))
        .map(|e| {
            (
                e.sequence,
                render_event(e, access_mapping, &resolve_data_key, private_field_grants),
            )
        })
        .collect())
}

/// See `rule CountEvents`. Same shape and narrowing as `query_events`
/// above, minus the `after_sequence` cursor - a count is a live aggregate
/// over everything matching, not a page of it (see the note above the
/// rule).
pub fn count_events(
    access_mapping: &RoleAccessMapping,
    event_types: &[EventType],
    tags: Option<&[Tag]>,
    bounded_context_events: &[Event],
) -> crate::error::Result<i64> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if !event_types
        .iter()
        .all(|et| et.bounded_context == access_mapping.bounded_context)
    {
        return Err(Error::EventTypeNotInBoundedContext.into());
    }

    Ok(bounded_context_events
        .iter()
        .filter(|e| e.bounded_context == access_mapping.bounded_context)
        .filter(|e| event_types.is_empty() || event_types.contains(&e.event_type))
        .filter(|e| tags.is_none_or(|wanted| wanted.iter().any(|t| e.tags.contains(t))))
        .filter(|e| event_owner_scope_satisfied(e, access_mapping.scope.as_deref()))
        .count() as i64)
}

/// See `rule InspectEvent`'s own `ensures` - `event` and `rendered_payload`
/// delivered alongside each other, not one folded into the other, since
/// `event` carries provenance (`metadata`, `origin`) `render_event`'s
/// output alone doesn't.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventInspected {
    pub event: Event,
    pub rendered_payload: String,
}

/// See `rule InspectEvent`. Unlike `EventTypeAdminOperations`'s `event_type`
/// context binding, `event` here is a caller-supplied argument the surface's
/// own `context event: Event where bounded_context = access_mapping.bounded_context`
/// scopes structurally on the GraphQL side, but `RoleAccessMapping` and
/// `Event` are independent entities with no field tying them together -
/// the same "explicit check, not derived" treatment
/// `authorise_command_submission`'s bounded-context match gets.
pub fn inspect_event(
    access_mapping: &RoleAccessMapping,
    event: &Event,
    resolve_data_key: impl Fn(&str, &str) -> Option<DataKey>,
    private_field_grants: &[PrivateFieldGrant],
) -> crate::error::Result<EventInspected> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if access_mapping.bounded_context != event.bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if !event_owner_scope_satisfied(event, access_mapping.scope.as_deref()) {
        return Err(crate::access_control::Error::GrantScopeMismatch.into());
    }

    Ok(EventInspected {
        event: event.clone(),
        rendered_payload: render_event(
            event,
            access_mapping,
            &resolve_data_key,
            private_field_grants,
        ),
    })
}

/// See `rule FetchCommands`. `bounded_context_commands` is every `Command`
/// this engine currently knows of for `access_mapping.bounded_context` -
/// the same full-snapshot treatment `query_events`'/`count_events`'
/// `bounded_context_events` get. `triggered_event in triggered_events` -
/// the reverse "which command produced this event" lookup - has no
/// separate relationship to resolve: `Command.triggered_events` is itself
/// a caller-resolved relationship projection everywhere else in this
/// codebase, but here it doesn't need resolving at all, since a
/// candidate's own membership in it is exactly "does `triggered_event`'s
/// `origin` name this candidate" - already answerable from
/// `EventOrigin::CommandTriggered`'s own boxed `Command` field, with no
/// second collection required. Matched by `Command.id` specifically
/// (drift audit finding #12, 2026-08-20, see project memory
/// `skilj-drift-audit-2026-08-20`) - not whole-struct equality, which
/// used to stand in for identity here before `Command` had a real one.
/// Returns rendered payloads only, the same "deliberately coarse on the
/// wire contract" choice `query_events` makes for `EventsQueried.events`
/// - see its own doc comment.
#[allow(clippy::too_many_arguments)]
pub fn fetch_commands(
    access_mapping: &RoleAccessMapping,
    command_types: &[CommandType],
    after: Option<chrono::DateTime<chrono::Utc>>,
    before: Option<chrono::DateTime<chrono::Utc>>,
    triggered_event: Option<&Event>,
    bounded_context_commands: &[Command],
    resolve_data_key: impl Fn(&str, &str) -> Option<DataKey>,
    private_field_grants: &[PrivateFieldGrant],
) -> crate::error::Result<Vec<String>> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.level != AccessLevel::Admin {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if !command_types
        .iter()
        .all(|ct| ct.bounded_context == access_mapping.bounded_context)
    {
        return Err(Error::CommandTypeNotInBoundedContext.into());
    }
    if triggered_event.is_some_and(|te| te.bounded_context != access_mapping.bounded_context) {
        return Err(Error::TriggeredEventNotInBoundedContext.into());
    }

    Ok(bounded_context_commands
        .iter()
        .filter(|c| c.bounded_context == access_mapping.bounded_context)
        .filter(|c| command_types.is_empty() || command_types.contains(&c.command_type))
        .filter(|c| after.is_none_or(|a| c.metadata.created_at >= a))
        .filter(|c| before.is_none_or(|b| c.metadata.created_at <= b))
        .filter(|c| {
            triggered_event.is_none_or(|te| match &te.origin {
                EventOrigin::CommandTriggered { command } => command.id == c.id,
                _ => false,
            })
        })
        .filter(|c| command_owner_scope_satisfied(c, access_mapping.scope.as_deref()))
        .map(|c| render_command(c, access_mapping, &resolve_data_key, private_field_grants))
        .collect())
}

/// See `rule CreateAllEventsSubscription`. `bounded_context_events` is
/// every `Event` this engine currently knows of for
/// `access_mapping.bounded_context`, for `starting_point`'s default - the
/// same full-snapshot treatment `query_events`' `bounded_context_events`
/// gets. The spec's own `(Events where ...).count - 1` and computing the
/// highest sequence directly are equivalent given
/// `SequenceIsGaplessPerBoundedContext` (both read as "the bounded
/// context's current latest committed sequence, or -1 when empty"); this
/// takes the latter, more direct route.
pub fn create_all_events_subscription(
    access_mapping: &RoleAccessMapping,
    event_types: Vec<EventType>,
    from_sequence: Option<i64>,
    bounded_context_events: &[Event],
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<AllEventsSubscription> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !event_types
        .iter()
        .all(|et| et.bounded_context == access_mapping.bounded_context)
    {
        return Err(Error::EventTypeNotInBoundedContext.into());
    }

    let starting_point = from_sequence.unwrap_or_else(|| {
        bounded_context_events
            .iter()
            .filter(|e| e.bounded_context == access_mapping.bounded_context)
            .map(|e| e.sequence)
            .max()
            .unwrap_or(-1)
    });

    Ok(AllEventsSubscription {
        bounded_context: access_mapping.bounded_context.clone(),
        access_mapping: access_mapping.clone(),
        created_at: now,
        from_sequence: starting_point,
        event_types,
    })
}

/// See `rule CreateEventTypeSubscription`. Same `starting_point` treatment
/// as `create_all_events_subscription` above.
pub fn create_event_type_subscription(
    access_mapping: &RoleAccessMapping,
    event_type: &EventType,
    filters: Vec<Filter>,
    from_sequence: Option<i64>,
    bounded_context_events: &[Event],
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<EventTypeSubscription> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if access_mapping.bounded_context != event_type.bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if event_type.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_filters(event_type, &filters) {
        return Err(Error::InvalidFilter.into());
    }

    let starting_point = from_sequence.unwrap_or_else(|| {
        bounded_context_events
            .iter()
            .filter(|e| e.bounded_context == event_type.bounded_context)
            .map(|e| e.sequence)
            .max()
            .unwrap_or(-1)
    });

    Ok(EventTypeSubscription {
        bounded_context: event_type.bounded_context.clone(),
        access_mapping: access_mapping.clone(),
        created_at: now,
        from_sequence: starting_point,
        event_type: event_type.clone(),
        filters,
    })
}

/// See `rule DeliverToSubscriptions`'s own `ensures` - `event` is
/// delivered alongside `rendered_payload`, the same "not folded into one
/// value" treatment `EventInspected` gives the two.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventDelivered {
    pub subscription: Subscription,
    pub event: Event,
    pub rendered_payload: String,
}

/// See `rule DeliverToSubscriptions`. No `requires` clause in the spec -
/// this rule is an unconditional `for subscription in Subscriptions
/// where ...` loop triggered by `Event.created`, not gated by anything a
/// caller can fail, so unlike every other rule in this module it returns
/// a plain `Vec`, not a `Result`. The `where` clause's three conditions
/// (`bounded_context = event.bounded_context`, `from_sequence <
/// event.sequence`, `access_mapping.status = active`) are this
/// function's own filter, not the caller's to pre-apply - `subscriptions`
/// is every `Subscription` this engine currently knows of, the usual
/// full-snapshot treatment. Real delivery is asynchronous and best-effort
/// per the note above the rule (outside any request's transaction); this
/// function is the pure "who matches and what do they get" computation
/// underneath that, the same split `project()`'s own async/sync halves
/// have.
/// `private_field_grants` is every active `PrivateFieldGrant` in
/// `event.bounded_context`, regardless of grantee - a single shared
/// snapshot passed to every subscriber's own `render_event` call below
/// rather than looked up per subscriber, since `render_event` already
/// filters a grant list down to `g.grantee == access_mapping.role`
/// internally. Different subscribers see different rendered payloads
/// from the identical input for exactly that reason: each is only ever
/// entitled by its own grants.
pub fn deliver_to_subscriptions(
    event: &Event,
    subscriptions: &[Subscription],
    resolve_data_key: impl Fn(&str, &str) -> Option<DataKey>,
    private_field_grants: &[PrivateFieldGrant],
) -> Vec<EventDelivered> {
    subscriptions
        .iter()
        .filter(|s| {
            s.bounded_context() == &event.bounded_context
                && s.starting_sequence() < event.sequence
                && s.access_mapping().status == RoleStatus::Active
        })
        .filter(|s| match s {
            Subscription::AllEventsSubscription(a) => {
                a.event_types.is_empty() || a.event_types.contains(&event.event_type)
            }
            Subscription::EventTypeSubscription(e) => {
                e.event_type == event.event_type && matches_filters(event, &e.filters)
            }
        })
        .filter(|s| event_owner_scope_satisfied(event, s.access_mapping().scope.as_deref()))
        .map(|s| EventDelivered {
            subscription: s.clone(),
            event: event.clone(),
            rendered_payload: render_event(
                event,
                s.access_mapping(),
                &resolve_data_key,
                private_field_grants,
            ),
        })
        .collect()
}

/// The real-time delivery mechanism `DeliverToSubscriptions`/
/// `EventSubscription` need, deliberately as small as possible - a
/// single-process, in-memory broadcast rather than anything itself
/// Postgres-`LISTEN`/`NOTIFY`- or broker-backed, since every local
/// `EventSubscription` subscriber already lives in this one process and
/// needs nothing more elaborate to be reached. Multi-instance delivery
/// (Codeberg issue #2; `crate::cross_instance`) builds on top of this
/// rather than replacing it: another instance's committed event still
/// arrives here, republished by this instance's own cross-instance
/// listener - see `instance_id`'s own doc comment for how a duplicate
/// republish of *this* instance's own writes is avoided.
///
/// `Clone`-cheap - `tokio::sync::broadcast::Sender` is already an `Arc`
/// around its own internal state, so every clone shares the same
/// channel, the same "hand out cheap clones from one shared thing"
/// treatment `db::Pool`/the two dispatchers already get. `instance_id` is
/// a plain `String`, but `Clone` still stays effectively free for what
/// this type is used for - it's cloned to hand out shared handles, not
/// in any per-event hot path.
#[derive(Clone)]
pub struct EventBroadcaster {
    sender: tokio::sync::broadcast::Sender<Event>,
    instance_id: String,
}

impl EventBroadcaster {
    /// `capacity` is how many not-yet-delivered events a single slow
    /// subscriber may lag behind by before its next `subscribe()`d
    /// receiver starts reporting `Lagged` - see
    /// `SkiljBuilder::event_broadcast_capacity`'s own doc comment.
    pub fn new(capacity: usize) -> Self {
        let (sender, _receiver) = tokio::sync::broadcast::channel(capacity);
        Self {
            sender,
            instance_id: crate::shared::generate_token_id(),
        }
    }

    /// A random id generated once per broadcaster - in practice once per
    /// running `Skilj` instance, since `SkiljBuilder::build` is this
    /// type's only real construction site. `db::notify_event_appended`
    /// stamps every `NOTIFY` this instance sends with it; `skilj`'s own
    /// cross-instance dispatch loop compares an incoming notification's
    /// `origin_instance_id` against this to recognise "this is my own
    /// write echoing back" and skip it, rather than calling `publish`
    /// here a second time for an event this exact broadcaster already
    /// delivered once directly. Without that check, every locally
    /// submitted event reached every local subscriber twice - Postgres
    /// `NOTIFY` is delivered to every listening backend including ones
    /// opened by the same process that sent it, and this broadcaster is
    /// the identical shared destination both the direct `publish` call
    /// and the cross-instance republish write into.
    pub fn instance_id(&self) -> &str {
        &self.instance_id
    }

    /// A fresh, independent receiver - `resolvers::event_subscription`'s
    /// own entry point, one call per live GraphQL subscription. Multiple
    /// receivers each see every event published after they were created,
    /// with no coordination needed between them - `broadcast`'s own
    /// native fan-out, not something this type manages by hand.
    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<Event> {
        self.sender.subscribe()
    }

    /// `db::insert_event_and_update_sync_projections`'s own post-commit
    /// step - called once per committed event, regardless of which
    /// call site produced it (REST or GraphQL alike), since this is the
    /// one choke point every event write already funnels through. A
    /// `SendError` (zero receivers currently subscribed) is the expected
    /// steady state, not a failure - silently ignored, the same
    /// "nobody's listening right now" non-error `tokio::sync::broadcast`
    /// itself already models this way.
    pub fn publish(&self, event: &Event) {
        let _ = self.sender.send(event.clone());
    }
}

/// See `rule FetchEvents`. `read_type` isn't a separate parameter - it's
/// always `token.event_type` (the surface's own `context` binding), so
/// it's derived here rather than accepted from the caller, which makes
/// the spec's `requires: token.event_type = read_type` true by
/// construction instead of a runtime check with its own failure mode.
pub fn fetch_events(
    token: &EventReadToken,
    events: &[Event],
    filters: &[Filter],
    after_sequence: Option<i64>,
) -> crate::error::Result<Vec<Event>> {
    if token.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let read_type = &token.event_type;
    if !read_type.event_read_allowed {
        return Err(Error::EventReadNotAllowed.into());
    }
    if !valid_filters(read_type, filters) {
        return Err(Error::InvalidFilter.into());
    }

    let after = after_sequence.unwrap_or(-1);
    Ok(events
        .iter()
        .filter(|e| e.bounded_context == read_type.bounded_context)
        .filter(|e| &e.event_type == read_type)
        .filter(|e| e.sequence > after)
        .filter(|e| matches_filters(e, filters))
        .filter(|e| event_owner_scope_satisfied(e, token.scope.as_deref()))
        .cloned()
        .collect())
}

/// What `consume_events` did to the token's `ReadCursor` - the caller
/// (the eventual sqlx-backed persistence layer) is responsible for
/// actually storing this; the same pure/no-I/O split `decide()`/
/// `project()` use (§1.1), extended here since this rule's own logic has
/// no I/O of its own either.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CursorUpdate {
    /// First `ConsumeEvents` call for this token - `ReadCursor.created`.
    /// Boxed: `ReadCursor` embeds a whole `EventReadToken` (itself grown to
    /// the full `AccessToken` base shape - `secret`/`created_at`/
    /// `revoked_at` - propagating `TokenRevocation`), which would otherwise
    /// size every `CursorUpdate` - including the far more common
    /// `Advanced`/`Unchanged` ones - to this variant's worst case. Same
    /// reasoning as `EventOrigin::CommandTriggered`'s boxed `Command`.
    Created(Box<ReadCursor>),
    /// `auto_advance` - the spec's `ensures` block writes
    /// `cursor.sequence = next_position` unconditionally on this branch,
    /// even when nothing new matched and `next_position` falls back to
    /// the unchanged `position` (`highest_sequence(served) ?? position`).
    /// So this fires on every `auto_advance` call after the first, not
    /// only when something new was served.
    Advanced {
        sequence: i64,
        updated_at: chrono::DateTime<chrono::Utc>,
    },
    /// `manual_ack` - only `AcknowledgeEvents` moves this cursor, so
    /// serving never does, matched or not.
    Unchanged,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumeEventsResult {
    pub served: Vec<Event>,
    pub cursor_update: CursorUpdate,
}

/// See `rule ConsumeEvents`. `existing_cursor` is this token's current
/// `ReadCursor` as already looked up by the caller (`None` when this
/// token has never consumed before) - the get-or-create lookup itself is
/// the caller's persistence concern, not this rule's.
pub fn consume_events(
    token: &EventReadToken,
    existing_cursor: Option<&ReadCursor>,
    ack_mode: Option<AckMode>,
    events: &[Event],
    filters: &[Filter],
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<ConsumeEventsResult> {
    if token.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let read_type = &token.event_type;
    if !read_type.event_read_allowed {
        return Err(Error::EventReadNotAllowed.into());
    }
    if !valid_filters(read_type, filters) {
        return Err(Error::InvalidFilter.into());
    }

    let is_new = existing_cursor.is_none();
    if is_new && ack_mode.is_none() {
        return Err(Error::CursorAckModeMismatch.into());
    }
    if let (Some(cursor), Some(requested)) = (existing_cursor, ack_mode) {
        if requested != cursor.ack_mode {
            return Err(Error::CursorAckModeMismatch.into());
        }
    }

    let mode = ack_mode
        .or_else(|| existing_cursor.map(|c| c.ack_mode))
        .expect("is_new implies ack_mode.is_some(), checked above");
    let position = existing_cursor.map(|c| c.sequence).unwrap_or(-1);

    let served: Vec<Event> = events
        .iter()
        .filter(|e| e.bounded_context == read_type.bounded_context)
        .filter(|e| &e.event_type == read_type)
        .filter(|e| e.sequence > position)
        .filter(|e| matches_filters(e, filters))
        .filter(|e| event_owner_scope_satisfied(e, token.scope.as_deref()))
        .cloned()
        .collect();

    let next_position = match mode {
        AckMode::AutoAdvance => highest_sequence(&served).unwrap_or(position),
        AckMode::ManualAck => position,
    };

    let cursor_update = if is_new {
        CursorUpdate::Created(Box::new(ReadCursor {
            token: token.clone(),
            ack_mode: mode,
            sequence: next_position,
            updated_at: now,
        }))
    } else if mode == AckMode::AutoAdvance {
        CursorUpdate::Advanced {
            sequence: next_position,
            updated_at: now,
        }
    } else {
        CursorUpdate::Unchanged
    };

    Ok(ConsumeEventsResult {
        served,
        cursor_update,
    })
}

/// See `rule AcknowledgeEvents`. `cursor` is this token's current
/// `ReadCursor` as already looked up by the caller - same split as
/// `consume_events` above. Returns the cursor's new `(sequence,
/// updated_at)` on success.
pub fn acknowledge_events(
    token: &EventReadToken,
    cursor: Option<&ReadCursor>,
    sequence: i64,
    now: chrono::DateTime<chrono::Utc>,
) -> crate::error::Result<(i64, chrono::DateTime<chrono::Utc>)> {
    if token.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let cursor = cursor.ok_or(Error::NoReadCursor)?;
    if cursor.ack_mode != AckMode::ManualAck {
        return Err(Error::NotManualAckCursor.into());
    }
    if sequence < cursor.sequence {
        return Err(Error::AcknowledgementRegresses.into());
    }

    Ok((sequence, now))
}

/// See `rule CreateExternalEvent`. `next_sequence` is the value the
/// caller's own `next_sequence(bounded_context)` already allocated under
/// its Postgres row lock (see the note above the rules) - not this
/// function's to compute, the same pure/no-I/O split every other rule in
/// this module uses. Same "derived, not a separate parameter" treatment
/// as `fetch_events`' `read_type` for `adapter.event_type = event_type`:
/// there's no `event_type` argument to disagree with `adapter.event_type`.
pub fn create_external_event(
    adapter: &ExternalEventToken,
    payload: String,
    source_content: String,
    source_context: Option<String>,
    next_sequence: i64,
    now: chrono::DateTime<chrono::Utc>,
    resolve_key: impl Fn(&str, &str) -> (EncryptionKey, DataKey),
) -> crate::error::Result<Event> {
    if adapter.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let event_type = &adapter.event_type;
    if !event_type.external_creation_allowed {
        return Err(Error::ExternalCreationNotAllowed.into());
    }
    if event_type.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_payload(&event_type.schema, &payload) {
        return Err(Error::PayloadDoesNotMatchSchema.into());
    }
    // Cross-tenant write fix (docs/architecture.md's own write-up of
    // these passes) - the write-side counterpart to EventFetch's own
    // EventsScopedToOwnerWhenDeclared read guarantee: a token that names
    // a scope may create events only for the owner it names. Single-
    // record, so this rejects outright rather than filtering, the same
    // reasoning authorise_command_submission's own note gives. Vacuously
    // true for a token naming no scope or an event type declaring no
    // owner dimension.
    let tags = derive_tags(&event_type.tag_mappings, &payload);
    if !tag_owner_scope_satisfied(
        &tags,
        event_type.owner_tag_key.as_deref(),
        adapter.scope.as_deref(),
    ) {
        return Err(crate::access_control::Error::GrantScopeMismatch.into());
    }

    let protected = protect_sensitive_fields(&event_type.sensitive_fields, &payload, resolve_key);
    Ok(Event {
        bounded_context: event_type.bounded_context.clone(),
        event_type: event_type.clone(),
        payload: protected.payload,
        metadata: Metadata {
            r#type: event_type.name.clone(),
            version: event_type.schema_version,
            client_id: adapter.id.clone(),
            created_at: now,
        },
        sequence: next_sequence,
        tags,
        encryption_keys: protected.encryption_keys,
        origin: EventOrigin::ExternalTriggered {
            source_content,
            source_context,
        },
    })
}

/// See `rule CreateDirectEvent`. Same shape and reasoning as
/// `create_external_event` above - the only differences are the opt-in
/// checked (`direct_creation_allowed`), the absence of the two opaque
/// `source_content`/`source_context` fields, and the resulting
/// `EventOrigin` variant.
pub fn create_direct_event(
    adapter: &DirectCreationToken,
    payload: String,
    next_sequence: i64,
    now: chrono::DateTime<chrono::Utc>,
    resolve_key: impl Fn(&str, &str) -> (EncryptionKey, DataKey),
) -> crate::error::Result<Event> {
    if adapter.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let event_type = &adapter.event_type;
    if !event_type.direct_creation_allowed {
        return Err(Error::DirectCreationNotAllowed.into());
    }
    if event_type.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_payload(&event_type.schema, &payload) {
        return Err(Error::PayloadDoesNotMatchSchema.into());
    }
    // See create_external_event's own identical note above.
    let tags = derive_tags(&event_type.tag_mappings, &payload);
    if !tag_owner_scope_satisfied(
        &tags,
        event_type.owner_tag_key.as_deref(),
        adapter.scope.as_deref(),
    ) {
        return Err(crate::access_control::Error::GrantScopeMismatch.into());
    }

    let protected = protect_sensitive_fields(&event_type.sensitive_fields, &payload, resolve_key);
    Ok(Event {
        bounded_context: event_type.bounded_context.clone(),
        event_type: event_type.clone(),
        payload: protected.payload,
        metadata: Metadata {
            r#type: event_type.name.clone(),
            version: event_type.schema_version,
            client_id: adapter.id.clone(),
            created_at: now,
        },
        sequence: next_sequence,
        tags,
        encryption_keys: protected.encryption_keys,
        origin: EventOrigin::DirectlyCreated,
    })
}

/// `next_occurrence_after(schedule, instant)` - the single cron primitive
/// `create_system_event`'s own guards need: the first occurrence of
/// `schedule` strictly after `instant`. Unlike `scheduled_payload`, a real
/// black box (implemented here, not caller-supplied) in the same register
/// as `derive_tags`/`schema_is_backwards_compatible` - the expression
/// grammar is mechanism, the guard built on top of it is behaviour - and
/// deterministic in the ordinary way: the same schedule and instant always
/// resolve to the same occurrence, which is exactly why two instances
/// evaluating `create_system_event`'s guards independently reach the same
/// verdict.
///
/// `schedule` is a 7-field Quartz-style cron expression (`sec min hour
/// day-of-month month day-of-week year`, e.g. `"0 0 3 * * * *"` for daily
/// at 03:00) - the `cron` crate's own dialect, not the more familiar
/// 5-field POSIX one; `RegisterEventType`'s own `requires` doesn't
/// validate this string's own syntax (the spec never added a
/// `valid_schedule` black box the way it did for filters), so a
/// malformed expression here just means this function returns `None`
/// forever for it, the same as a schedule with no occurrence left at all
/// - `create_system_event` never gets to reach a due occurrence, and the
///   type is never raised.
pub fn next_occurrence_after(
    schedule: &str,
    instant: chrono::DateTime<chrono::Utc>,
) -> Option<chrono::DateTime<chrono::Utc>> {
    use std::str::FromStr;
    cron::Schedule::from_str(schedule)
        .ok()?
        .after(&instant)
        .next()
}

/// See `rule CreateSystemEvent`. `occurrence_at` is the instant
/// `SystemTriggerDue` itself carries - what this event stands for, not
/// inferred from `now` (which only ever appears in its `Metadata.created_at`,
/// the wall clock of the write). `payload` is `scheduled_payload(event_type)`'s
/// own already-resolved output - a `FnOnce` so the caller (the scheduler's
/// own orchestration) only ever invokes that black box once eligibility is
/// confirmed, never for an occurrence this function is about to reject
/// anyway.
///
/// `None` when this occurrence must not produce an event - already
/// accounted for, a policy still waiting on a later occurrence, or the
/// type/context isn't eligible at all. Unlike `create_external_event`/
/// `create_direct_event`, this never returns `Err`: nothing external ever
/// calls `CreateSystemEvent` (see the note above the rule - the scheduler
/// presents no credential and faces no actor), so there is no caller for a
/// typed rejection to reach. `Some((event, occurrence_at))` pairs the
/// built event with the instant both `EventType.schedule_position` and
/// `EventType.last_fired_at` advance to - the caller's own job to persist,
/// atomically with the event itself (see `db::fire_system_event`).
#[allow(clippy::too_many_arguments)]
pub fn create_system_event(
    event_type: &EventType,
    occurrence_at: chrono::DateTime<chrono::Utc>,
    now: chrono::DateTime<chrono::Utc>,
    next_sequence: i64,
    resolve_payload: impl FnOnce() -> String,
    resolve_key: impl Fn(&str, &str) -> (EncryptionKey, DataKey),
) -> Option<(Event, chrono::DateTime<chrono::Utc>)> {
    if !event_type.system_triggered_allowed {
        return None;
    }
    if event_type.bounded_context.status != BoundedContextStatus::Active {
        return None;
    }
    let schedule = event_type.system_triggered_schedule.as_deref()?;
    let policy = event_type.missed_occurrence_policy?;
    let position = event_type.schedule_position?;

    // An occurrence is never fired before its own instant, and never
    // twice: `position` is the durable, shared record of how far this
    // type's schedule has been accounted for, so an occurrence at or
    // behind it has already been settled - by an earlier firing, by a
    // policy that passed it over, or by another instance a moment ago.
    // This is what makes two instances raising the same occurrence
    // harmless rather than a duplicate event.
    if occurrence_at > now || occurrence_at <= position {
        return None;
    }

    // no_gap: this occurrence is the very next one the position hasn't
    // accounted for yet. nothing_later_is_due: no occurrence after this
    // one has come due, so it is the whole of a backlog's tail. See the
    // three bullets in the note above `rule CreateSystemEvent`.
    let no_gap = next_occurrence_after(schedule, position) == Some(occurrence_at);
    let nothing_later_is_due =
        next_occurrence_after(schedule, occurrence_at).is_none_or(|next| next > now);

    let eligible = match policy {
        MissedOccurrencePolicy::ReplayBacklog => true,
        MissedOccurrencePolicy::FireOnce => nothing_later_is_due,
        MissedOccurrencePolicy::Skip => no_gap && nothing_later_is_due,
    };
    if !eligible {
        return None;
    }

    let payload = resolve_payload();
    let protected = protect_sensitive_fields(&event_type.sensitive_fields, &payload, resolve_key);
    let event = Event {
        bounded_context: event_type.bounded_context.clone(),
        event_type: event_type.clone(),
        payload: protected.payload,
        metadata: Metadata {
            r#type: event_type.name.clone(),
            version: event_type.schema_version,
            client_id: "system".to_string(),
            created_at: now,
        },
        sequence: next_sequence,
        tags: derive_tags(&event_type.tag_mappings, &payload),
        encryption_keys: protected.encryption_keys,
        origin: EventOrigin::SystemTriggered,
    };
    Some((event, occurrence_at))
}

/// See `rule SkipMissedOccurrences`. `None` when the resume is a no-op
/// (the position is already at or past `now`) - see that rule's own note
/// on why this makes a second instance resuming alongside the first
/// harmless: a live cluster's position is already at or past its last
/// occurrence, so nothing here moves it further. Like `create_system_event`,
/// no typed rejection - `SystemScheduleResumed` reaches no surface either.
pub fn skip_missed_occurrences(
    event_type: &EventType,
    now: chrono::DateTime<chrono::Utc>,
) -> Option<chrono::DateTime<chrono::Utc>> {
    // Drift audit finding #10 (2026-08-20, see project memory
    // `skilj-drift-audit-2026-08-20`): `rule SkipMissedOccurrences`'s own
    // first `requires` clause, missing here until this fix. Not reachable
    // from the real scheduler today - `list_scheduled_event_types` already
    // filters to this flag before this function is ever called - but a
    // pure function should faithfully encode every one of its rule's
    // `requires` clauses regardless of what its current callers happen to
    // pre-filter, the same standard every other pure function in this
    // module is held to.
    if !event_type.system_triggered_allowed {
        return None;
    }
    if event_type.missed_occurrence_policy != Some(MissedOccurrencePolicy::Skip) {
        return None;
    }
    if event_type.bounded_context.status != BoundedContextStatus::Active {
        return None;
    }
    let position = event_type.schedule_position?;
    if position >= now {
        return None;
    }
    Some(now)
}

/// The `CommandAuthorised` fact `AuthoriseCommandSubmission`/
/// `AuthoriseCommandTrigger` each produce, and `process_command` below
/// consumes - the join point between REST/GraphQL authorisation and the
/// one command-processing pipeline both funnel into. `consistency_tags`
/// is a Rust-only addition beyond the spec's own `CommandAuthorised`
/// fact: both authorising functions already have to compute
/// `derive_tags(command_type, payload)` themselves to check
/// `tag_owner_scope_satisfied` before returning, and every caller needs the
/// identical value again immediately afterward (to fetch
/// `matching_events`) - carrying it here is one computation instead of
/// two, not a behaviour change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandAuthorised {
    pub command_type: CommandType,
    pub payload: String,
    pub client_id: String,
    pub consistency_tags: Vec<Tag>,
}

/// See `rule AuthoriseCommandTrigger`. Same "derived, not a separate
/// parameter" treatment as `fetch_events`/`create_external_event` for
/// `token.command_type = command_type`. Reuses `Error::BoundedContextArchived`
/// for `command_type.bounded_context.status = active` - same rejection,
/// same meaning, whichever rule hits it.
pub fn authorise_command_trigger(
    token: &CommandToken,
    payload: String,
) -> crate::error::Result<CommandAuthorised> {
    if token.status != TokenStatus::Active {
        return Err(crate::access_control::Error::TokenNotActive.into());
    }
    let command_type = &token.command_type;
    if !command_type.rest_trigger_allowed {
        return Err(Error::RestTriggerNotAllowed.into());
    }
    if command_type.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_payload(&command_type.schema, &payload) {
        return Err(Error::PayloadDoesNotMatchSchema.into());
    }
    // Cross-tenant write fix (docs/architecture.md's own write-up of
    // these passes) - the write-side counterpart to CommandQuery's own
    // command_owner_scope_satisfied read check: a token that names a
    // scope may trigger commands only for the owner it names, checked
    // against its own scope rather than any minting admin's, exactly as
    // EventFetch's tokens are (see create_event_read_token's own doc
    // comment). Single-record, so this rejects outright rather than
    // filtering - there is exactly one command being authorised here.
    let consistency_tags = derive_tags(&command_type.tag_mappings, &payload);
    if !tag_owner_scope_satisfied(
        &consistency_tags,
        command_type.owner_tag_key.as_deref(),
        token.scope.as_deref(),
    ) {
        return Err(crate::access_control::Error::GrantScopeMismatch.into());
    }

    Ok(CommandAuthorised {
        command_type: command_type.clone(),
        payload,
        client_id: token.id.clone(),
        consistency_tags,
    })
}

/// See `rule AuthoriseCommandSubmission`. The GraphQL/Role counterpart to
/// `authorise_command_trigger` above - same `CommandAuthorised` hand-off,
/// same reused `Error::BoundedContextArchived` for
/// `command_type.bounded_context.status = active`, but authorised against
/// a `RoleAccessMapping` rather than a `CommandToken`: `access_mapping.role.id`
/// becomes `client_id` (a `CommandToken` has no `Role` to attribute to, so
/// it uses its own `id` instead), and `access_mapping.bounded_context =
/// command_type.bounded_context` is an explicit check here rather than
/// structurally derived, since - unlike `CommandToken.command_type` - a
/// `RoleAccessMapping` and a `CommandType` are independent entities with no
/// field tying them together short of comparing this pair.
pub fn authorise_command_submission(
    access_mapping: &RoleAccessMapping,
    command_type: &CommandType,
    payload: String,
) -> crate::error::Result<CommandAuthorised> {
    if access_mapping.status != RoleStatus::Active {
        return Err(crate::access_control::Error::GrantNotActive.into());
    }
    if !matches!(
        access_mapping.level,
        AccessLevel::Write | AccessLevel::Admin
    ) {
        return Err(crate::access_control::Error::InsufficientAccessLevel.into());
    }
    if access_mapping.bounded_context != command_type.bounded_context {
        return Err(crate::access_control::Error::GrantBoundedContextMismatch.into());
    }
    if command_type.bounded_context.status != BoundedContextStatus::Active {
        return Err(Error::BoundedContextArchived.into());
    }
    if !valid_payload(&command_type.schema, &payload) {
        return Err(Error::PayloadDoesNotMatchSchema.into());
    }
    // Cross-tenant write fix (docs/architecture.md's own write-up of
    // these passes) - the write-side counterpart to CommandQuery's own
    // CommandsScopedToOwnerWhenDeclared read guarantee: a grant that
    // names a scope may submit commands only for the owner it names,
    // checked against the same command_type.owner_tag_key/derived tags
    // the read side already reads back with command_owner_scope_satisfied,
    // just before the record exists rather than after. Single-record, so
    // this rejects outright rather than filtering, the same shape
    // InspectEvent/InspectSnapshot already have - there is exactly one
    // command being authorised here, so there is nothing to filter, only
    // one thing to refuse. Vacuously true, same as always, for a grant
    // naming no scope or a command type declaring no owner dimension.
    let consistency_tags = derive_tags(&command_type.tag_mappings, &payload);
    if !tag_owner_scope_satisfied(
        &consistency_tags,
        command_type.owner_tag_key.as_deref(),
        access_mapping.scope.as_deref(),
    ) {
        return Err(crate::access_control::Error::GrantScopeMismatch.into());
    }

    Ok(CommandAuthorised {
        command_type: command_type.clone(),
        payload,
        client_id: access_mapping.role.id.clone(),
        consistency_tags,
    })
}

/// See the `boundary`/`matching_events` `let`s above rule `ProcessCommand`.
/// Reuses `highest_sequence` unmodified - the spec calls it with
/// different arguments here (`command_type.bounded_context,
/// consistency_tags`, rather than a `Vec<Event>`) but it's "a black box in
/// the same trivial register" either way, and once reduced to "the
/// highest sequence among this bounded context's events matching any of
/// these tags", it's the identical computation, so no second black box is
/// declared for it. The spec's own `matching_events` binding adds a
/// `sequence <= boundary` filter on top - definitionally satisfied by
/// every member of the tag-matching set computed here, since `boundary`
/// *is* that set's own maximum - so it drops out entirely in a
/// single-snapshot pure function. It only bites under concurrent appends
/// between reading and committing, which is `next_sequence`'s caller's
/// own re-check to make under its Postgres lock (see the @guidance note
/// above `ProcessCommand`), not this function's.
///
/// The `None` case - no matching event found, so no boundary - covers
/// "this command type declares no `tag_mappings`" and "it does, but
/// nothing has matched any of them yet" alike; `Command.consistency_boundary`
/// (`Option<i64>`) carries that same `None` through unchanged. The spec's
/// own doc for that field reads "null when the command does not use a
/// consistency boundary", which literally describes only the first case,
/// but `highest_sequence`'s contract ("the greatest sequence... or null
/// when empty") doesn't distinguish an empty-because-no-tags-were-given
/// set from an empty-because-nothing-matched-yet one - and
/// `DynamicConsistencyBoundaryHonoured` doesn't need to either: a `null`
/// boundary makes its `e.sequence > c.consistency_boundary` comparison
/// vacuous either way, and a command whose tags have zero prior matches
/// has nothing a concurrent writer could have raced it on regardless.
pub fn consistency_boundary_and_matching_events(
    bounded_context_events: &[Event],
    consistency_tags: &[Tag],
) -> (Option<i64>, Vec<Event>) {
    let matching: Vec<Event> = bounded_context_events
        .iter()
        .filter(|e| consistency_tags.iter().any(|t| e.tags.contains(t)))
        .cloned()
        .collect();
    let boundary = highest_sequence(&matching);
    (boundary, matching)
}

/// What `process_command` produced - the caller (the eventual sqlx-backed
/// persistence layer) stores `command` and every one of `events` in one
/// transaction, alongside updating whichever sync projections consume
/// them (see the @guidance note above rule `ProcessCommand`). Not named
/// `CommandTriggered` because that's `Event.origin`'s variant name for
/// each individual event, not this pairing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessCommandResult {
    pub command: Command,
    pub events: Vec<Event>,
}

/// See `rule ProcessCommand`. Split from `AuthoriseCommandSubmission`/
/// `AuthoriseCommandTrigger` above at exactly the spec's own boundary -
/// `when: CommandAuthorised(...)` - so this one function serves both
/// authorisation paths: `AuthoriseCommandTrigger` (REST/`CommandToken`)
/// and `AuthoriseCommandSubmission` (GraphQL/`RoleAccessMapping`) are both
/// fully implemented, each in `resolvers::command_submission`/
/// `skilj-rest`'s own `post_commands_trigger` respectively - this function
/// itself doesn't care which one produced its `command_type`/`payload`/
/// `client_id` input, and never has to know.
///
/// `decision` is already-computed, not called from inside this function:
/// invoking the right bounded context's own typed `CommandType::decide()`
/// crosses a type-erasure boundary this internal engine sits on the far
/// side of (raw JSON/`EventType`-by-name) - the spec itself says that
/// wiring "stays a prose contract here, not a formally specified
/// interface" (see the note above the rule), so resolving it isn't this
/// pure function's job. Same reasoning for `resolve_event_type`: turning
/// `decision`'s `EventSpec.event_type: String` names back into full
/// `EventType`s is a bounded-context-scoped registry lookup - real now,
/// `db::get_event_type` - supplied by the caller since resolving it is
/// I/O, not this pure function's own job. `next_sequence` is called once
/// per accepted event, in order - the same Postgres-lock-backed,
/// caller-supplied value as `create_external_event`/`create_direct_event`'s.
///
/// Ten parameters because this function's own scope is genuinely that
/// wide - it's `ProcessCommand`'s entire `let`/`ensures` body, not
/// something a smaller grouping would simplify without inventing a
/// struct that exists only to satisfy the lint. `resolve_key` is
/// `protect_sensitive_fields`'s own pre-resolution parameter, threaded
/// through unchanged to both of this function's own call sites below (the
/// command's payload, and each accepted event spec's) - a command and an
/// event naming the same subject resolve to the very same `EncryptionKey`
/// this way, exactly as the spec requires. `id` is `Command.id`'s own
/// already-resolved `generate_token_id()` output (drift audit finding
/// #12), caller-supplied the same way `next_sequence`'s own values are -
/// this function has no way to generate one itself, the same "black box
/// resolved outside, handed in" treatment every id-bearing entity's own
/// pure constructor gets (see `access_control::create_role`).
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(
    skip_all,
    fields(
        bounded_context = %command_type.bounded_context.name,
        command_type = %command_type.name,
    )
)]
pub fn process_command(
    id: String,
    command_type: &CommandType,
    payload: &str,
    client_id: &str,
    bounded_context_events: &[Event],
    decision: CommandDecision,
    resolve_event_type: impl Fn(&str) -> Option<EventType>,
    mut next_sequence: impl FnMut() -> i64,
    now: chrono::DateTime<chrono::Utc>,
    resolve_key: impl Fn(&str, &str) -> (EncryptionKey, DataKey),
) -> crate::error::Result<ProcessCommandResult> {
    let event_specs = match decision {
        CommandDecision::Accepted { events } => events,
        CommandDecision::Rejected { reason, kind } => {
            return Err(crate::error::Error::CommandRejected { reason, kind });
        }
    };

    let consistency_tags = derive_tags(&command_type.tag_mappings, payload);
    let (consistency_boundary, _matching_events) =
        consistency_boundary_and_matching_events(bounded_context_events, &consistency_tags);

    let protected = protect_sensitive_fields(&command_type.sensitive_fields, payload, &resolve_key);
    let command = Command {
        id,
        bounded_context: command_type.bounded_context.clone(),
        command_type: command_type.clone(),
        payload: protected.payload,
        metadata: Metadata {
            r#type: command_type.name.clone(),
            version: command_type.schema_version,
            client_id: client_id.to_string(),
            created_at: now,
        },
        encryption_keys: protected.encryption_keys,
        consistency_tags,
        consistency_boundary,
    };

    let mut events = Vec::with_capacity(event_specs.len());
    for spec in event_specs {
        let event_type = resolve_event_type(&spec.event_type)
            .ok_or_else(|| Error::UnregisteredEventType(spec.event_type.clone()))?;
        let spec_payload = spec.payload.to_string();
        let event_protected =
            protect_sensitive_fields(&event_type.sensitive_fields, &spec_payload, &resolve_key);
        events.push(Event {
            bounded_context: command_type.bounded_context.clone(),
            event_type: event_type.clone(),
            payload: event_protected.payload,
            metadata: Metadata {
                r#type: event_type.name.clone(),
                version: event_type.schema_version,
                client_id: client_id.to_string(),
                created_at: now,
            },
            sequence: next_sequence(),
            tags: derive_tags(&event_type.tag_mappings, &spec_payload),
            encryption_keys: event_protected.encryption_keys,
            origin: EventOrigin::CommandTriggered {
                command: Box::new(command.clone()),
            },
        });
    }

    Ok(ProcessCommandResult { command, events })
}