ai-memory 0.7.1

AI-agnostic persistent memory system — MCP server, HTTP API, and CLI for any AI platform
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
// Copyright 2026 AlphaOne LLC
// SPDX-License-Identifier: Apache-2.0

//! `ai-memory rules` subcommand — operator-facing CRUD for the
//! substrate-level agent-action rules engine (issue #691).
//!
//! Six verbs:
//!
//! * `add`     — insert a new rule (mutation: requires operator key).
//! * `list`    — print every rule, including disabled ones (read).
//! * `check`   — evaluate a proposed action against the live rule set
//!               and print the [`Decision`] (read).
//! * `enable`  — flip `enabled = 1` on an existing rule (mutation).
//! * `disable` — flip `enabled = 0` on an existing rule (mutation).
//! * `remove`  — delete a rule (mutation).
//!
//! # Operator identity (mutation gate)
//!
//! Per issue #691 design revision 2026-05-13, the four mutation
//! verbs require the operator's Ed25519 keypair on disk at
//! `${AI_MEMORY_KEY_DIR:-~/.config/ai-memory/keys}/operator.priv`
//! (mode 0600). The CLI:
//!
//! 1. Resolves the key directory (env override → default).
//! 2. Loads `operator.priv` and verifies mode bits (0600 on Unix).
//! 3. Signs the canonical rule encoding via Ed25519.
//! 4. Persists the signature alongside the rule (
//!    [`crate::governance::rules_store::update_signature`]).
//!
//! If the key file is absent / wrong-mode, the CLI refuses with
//! `governance.no_operator_key` error. No mutation lands.
//!
//! The HTTP / MCP surfaces enforce the same gate: HTTP verifies an
//! Ed25519 signature header against `operator.pub`; MCP stdio
//! mutation tools are explicitly disabled (return
//! `governance.not_available_over_mcp`).

use crate::models::field_names;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use clap::{Args, Subcommand};
use ed25519_dalek::{Signer, SigningKey};
use serde::Serialize;

use crate::cli::CliOutput;
use crate::governance::agent_action::{AgentAction, action_kinds as ak, check_agent_action};
use crate::governance::rules_store::{self, Rule};
use crate::identity::keypair as kp;

/// Operator Ed25519 private-key file name under the key dir (#1558 batch 6).
const OPERATOR_KEY_FILENAME: &str = "operator.key";

/// Wire id reserved for the operator's keypair file on disk. Stored
/// under the same directory as per-agent keys but treated specially
/// — the agent_id resolution stack never returns this id; only the
/// rules subcommand looks for it.
pub const OPERATOR_KEY_ID: &str = "operator";

/// `attest_level` stamped on rules after the operator signs them.
/// Re-exported from the governance layer so the rules table and the
/// `signed_events` audit chain share one source of truth for the
/// literal (see [`crate::governance::rules_store::OPERATOR_SIGNED_ATTEST_LEVEL`]).
pub const OPERATOR_SIGNED_LEVEL: &str =
    crate::governance::rules_store::OPERATOR_SIGNED_ATTEST_LEVEL;

/// Length of a raw Ed25519 signing-key seed on disk.
const ED25519_SEED_LEN: usize = ed25519_dalek::SECRET_KEY_LENGTH;
/// Length of a raw Ed25519 verifying-key on disk (decoded base64).
const ED25519_PUBLIC_LEN: usize = ed25519_dalek::PUBLIC_KEY_LENGTH;

#[derive(Args)]
pub struct RulesArgs {
    /// Override the default key storage directory.
    /// Honors `AI_MEMORY_KEY_DIR` env var when this flag is omitted.
    #[arg(long, value_name = "PATH", global = true)]
    pub key_dir: Option<PathBuf>,
    #[command(subcommand)]
    pub action: RulesAction,
}

#[derive(Subcommand)]
pub enum RulesAction {
    /// Add a new agent-action rule. Requires operator keypair on
    /// disk; signs the canonical row encoding before persisting.
    Add {
        /// Rule id (e.g. R005, `tmp-noisy-build`). Must be unique.
        #[arg(long)]
        id: String,
        /// Action kind: `bash` / `filesystem_write` / `network_request`
        /// / `process_spawn` / `custom`.
        #[arg(long)]
        kind: String,
        /// Matcher JSON. Shape depends on `--kind`. See
        /// `docs/governance/agent-action-rules.md`.
        #[arg(long)]
        matcher: String,
        /// Severity: `refuse` / `warn` / `log`.
        #[arg(long, default_value = "refuse")]
        severity: String,
        /// Human-readable reason surfaced to the agent on a match.
        #[arg(long)]
        reason: String,
        /// Optional namespace scope. Defaults to `_global`.
        #[arg(long, default_value = crate::quotas::GLOBAL_NAMESPACE)]
        namespace: String,
        /// Land the rule with `enabled = 0` (operator activates
        /// later via `ai-memory rules enable <id> --sign`).
        #[arg(long)]
        disabled: bool,
        /// Sign the rule with the operator keypair on disk. Required
        /// for non-dry-run inserts; without `--sign` the CLI refuses.
        #[arg(long)]
        sign: bool,
    },
    /// List every rule (enabled + disabled). Read-only, no key
    /// required.
    List,
    /// Evaluate a proposed action against the live rule set without
    /// committing it. Read-only. The output is the same JSON
    /// [`Decision`] shape the MCP / HTTP path returns.
    Check {
        /// Action kind: same vocabulary as `add --kind`.
        #[arg(long)]
        kind: String,
        /// Action payload JSON. For Bash: `{"command":"ls"}`.
        /// For `FilesystemWrite`: `{"path":"/tmp/x"}`. Etc.
        #[arg(long)]
        payload: String,
        /// Optional agent id; defaults to the resolved NHI id for
        /// audit-row provenance.
        #[arg(long)]
        agent_id: Option<String>,
    },
    /// Activate a rule (flip `enabled = 1`). Requires `--sign`.
    Enable {
        /// Rule id.
        #[arg(long)]
        id: String,
        /// Sign the activation with the operator key.
        #[arg(long)]
        sign: bool,
    },
    /// Deactivate a rule (flip `enabled = 0`). Requires `--sign`.
    Disable {
        /// Rule id.
        #[arg(long)]
        id: String,
        /// Sign the deactivation with the operator key.
        #[arg(long)]
        sign: bool,
    },
    /// Remove a rule from the table. Requires `--sign`.
    Remove {
        /// Rule id.
        #[arg(long)]
        id: String,
        /// Sign the removal with the operator key.
        #[arg(long)]
        sign: bool,
    },
    /// v0.7.0 L1-6 — generate a fresh Ed25519 operator keypair and
    /// write the private 32-byte seed to `--out` (mode 0600 on Unix)
    /// plus a base64-encoded public key sibling at `<out>.pub`
    /// (mode 0644). Default `--out` is `~/.config/ai-memory/operator.key`.
    ///
    /// Refuses to overwrite an existing file unless `--force` is passed;
    /// even with `--force` a stderr warning is emitted (an existing
    /// operator key is the keystone of the signature verify chain — a
    /// silent overwrite would invalidate every prior signed rule).
    ///
    /// The 32-byte seed never appears in stdout, stderr, or any
    /// memory the agent emits. Only the fingerprint
    /// `sha256(public_key)[:16]` is logged.
    Keygen {
        /// Output path for the 32-byte private seed. The base64
        /// public key sibling is written to `<out>.pub`.
        #[arg(long, value_name = "PATH")]
        out: Option<PathBuf>,
        /// Overwrite an existing private/public key pair. Emits a
        /// stderr warning even when set. Default: refuse to overwrite.
        #[arg(long)]
        force: bool,
    },
    /// v0.7.0 L1-6 — sign every seeded rule (R001..R004 today) with
    /// the operator key. Sets `signature = ed25519(canonical_payload)`
    /// and `attest_level = 'operator_signed'`. `enabled` stays at 0
    /// — the operator audits and activates manually after this runs.
    ///
    /// The canonical payload includes `enabled`, so a direct
    /// `UPDATE governance_rules SET enabled = 1` after signing would
    /// fail signature verification at load time — that is the
    /// bypass-prevention property.
    SignSeed {
        /// Path to the operator private seed (32 bytes) — same shape
        /// `rules keygen --out` writes. Defaults to
        /// `~/.config/ai-memory/operator.key`.
        #[arg(long, value_name = "PATH")]
        key: Option<PathBuf>,
        /// Override the DB path (useful for smoke tests against a
        /// scratch sqlite file). Defaults to the same `--db` the
        /// rest of the `rules` verbs use (the top-level `ai-memory
        /// --db` flag).
        #[arg(long, value_name = "PATH")]
        db: Option<PathBuf>,
    },
}

/// JSON envelope used by `--json` callers — keeps a stable wire shape
/// across the six verbs.
#[derive(Serialize)]
struct CliEnvelope<'a> {
    verb: &'a str,
    result: serde_json::Value,
}

/// Dispatch entry point called by `daemon_runtime::run`.
///
/// # Errors
///
/// Returns an error on a SQLite / key / signature failure; the
/// caller surfaces the error to the operator via the standard
/// `anyhow` chain.
pub fn run(
    db_path: &std::path::Path,
    args: RulesArgs,
    json: bool,
    out: &mut CliOutput<'_>,
) -> Result<()> {
    // Open via the migrating path (`crate::db::open` runs apply_migrations)
    // rather than a raw `rusqlite::Connection::open`, so the `rules` verbs
    // work against a FRESH db — the `governance_rules` table is created on
    // open like every other db-opening CLI command. Pre-fix, a standalone
    // `ai-memory rules <verb>` against a never-migrated db failed with
    // `rules_store::list: prepare — no such table: governance_rules`; this
    // broke Form-7 governance bootstrap on fresh (esp. postgres-backed)
    // fleet peers where the daemon — which would otherwise have migrated the
    // local sqlite — has not yet started. Surfaced by the do-1461 A2A run.
    let conn = crate::db::open(db_path)
        .with_context(|| format!("rules: open db at {}", db_path.display()))?;
    let key_dir = resolve_key_dir(args.key_dir.as_deref())?;

    match args.action {
        RulesAction::Add {
            id,
            kind,
            matcher,
            severity,
            reason,
            namespace,
            disabled,
            sign,
        } => {
            if !sign {
                bail!("governance.no_operator_key: `rules add` requires --sign");
            }
            let signing_key = load_operator_signing_key_from_dir(&key_dir)?;
            // Validate matcher JSON shape now — better to refuse at
            // input time than on the next check call.
            let matcher_json: serde_json::Value = serde_json::from_str(&matcher)
                .with_context(|| format!("rules add: matcher is not valid JSON: {matcher}"))?;
            // SEC-12 / COR-10 (Cluster D, issue #767) — the bash
            // matcher field is a LITERAL substring (despite the
            // legacy `command_regex` field name). Reject regex
            // metacharacters at CLI input time so an operator who
            // pastes `rm\s+-rf` does not silently install a
            // never-matching rule.
            if let Some(val) = matcher_json
                .get(crate::governance::agent_action::MATCHER_COMMAND_SUBSTRING)
                .or_else(|| {
                    matcher_json.get(crate::governance::agent_action::MATCHER_COMMAND_REGEX)
                })
                .and_then(|v| v.as_str())
            {
                crate::governance::agent_action::validate_command_substring(val)
                    .map_err(|e| anyhow::anyhow!("rules add: {e}"))?;
                if matcher_json
                    .get(crate::governance::agent_action::MATCHER_COMMAND_REGEX)
                    .is_some()
                    && matcher_json
                        .get(crate::governance::agent_action::MATCHER_COMMAND_SUBSTRING)
                        .is_none()
                {
                    tracing::warn!(
                        "rules add: matcher field `command_regex` is DEPRECATED — rename to \
                         `command_substring` (the engine has always done literal substring \
                         matching, not regex). See SEC-12 in the v0.7.0 cluster-D fix."
                    );
                }
            }
            let created_at = chrono::Utc::now().timestamp();
            let agent_id = resolve_agent_id();
            let mut rule = Rule {
                id: id.clone(),
                kind,
                matcher,
                severity,
                reason,
                namespace,
                created_by: agent_id,
                created_at,
                enabled: !disabled,
                signature: None,
                attest_level: crate::models::AttestLevel::Unsigned.as_str().to_string(),
            };
            // v0.7.0 issue #800 / Form 7 critical fix: sign the
            // canonical bytes that `verify_rule_signature` will read
            // back. `canonical_bytes` (without `enabled`) and
            // `canonical_bytes_for_signing` (with `enabled`) were
            // out-of-sync between the signer and verifier — the
            // signatures produced here never validated, the L1-6
            // gate silently skipped every "operator_signed" rule,
            // and Form 7 enforcement returned `allow` for every
            // action. Use `canonical_bytes_for_signing` so the
            // verifier accepts what we produce.
            let canonical = rules_store::canonical_bytes_for_signing(&rule)?;
            let sig = signing_key.sign(&canonical);
            rule.signature = Some(sig.to_bytes().to_vec());
            rule.attest_level = OPERATOR_SIGNED_LEVEL.to_string();
            rules_store::insert(&conn, &rule)?;
            emit_ok(json, out, "rules.add", &rule_to_json(&rule))?;
            Ok(())
        }
        RulesAction::List => {
            let rules = rules_store::list(&conn)?;
            let payload = serde_json::Value::Array(rules.iter().map(rule_to_json).collect());
            emit_ok(json, out, "rules.list", &payload)?;
            Ok(())
        }
        RulesAction::Check {
            kind,
            payload,
            agent_id,
        } => {
            let action = build_action(&kind, &payload)?;
            let resolved_agent = agent_id.unwrap_or_else(resolve_agent_id);
            let decision = check_agent_action(&conn, &resolved_agent, &action)?;
            emit_ok(json, out, "rules.check", &serde_json::to_value(&decision)?)?;
            Ok(())
        }
        RulesAction::Enable { id, sign } => {
            if !sign {
                bail!("governance.no_operator_key: `rules enable` requires --sign");
            }
            let signing_key = load_operator_signing_key_from_dir(&key_dir)?;
            let Some(mut rule) = rules_store::get(&conn, &id)? else {
                bail!("rules.enable: no rule with id={id}");
            };
            rule.enabled = true;
            // Issue #800 critical fix: signer must use the same
            // canonical encoding as `verify_rule_signature`
            // (otherwise the L1-6 enforcement gate skips every rule
            // and Form 7 returns `allow` for every action). See the
            // matching comment in the `Add` arm.
            let canonical = rules_store::canonical_bytes_for_signing(&rule)?;
            let sig = signing_key.sign(&canonical);
            rules_store::set_enabled(&conn, &id, true)?;
            rules_store::update_signature(&conn, &id, &sig.to_bytes(), OPERATOR_SIGNED_LEVEL)?;
            let updated =
                rules_store::get(&conn, &id)?.context("rules.enable: row vanished after update")?;
            emit_ok(json, out, "rules.enable", &rule_to_json(&updated))?;
            Ok(())
        }
        RulesAction::Disable { id, sign } => {
            if !sign {
                bail!("governance.no_operator_key: `rules disable` requires --sign");
            }
            let signing_key = load_operator_signing_key_from_dir(&key_dir)?;
            let Some(mut rule) = rules_store::get(&conn, &id)? else {
                bail!("rules.disable: no rule with id={id}");
            };
            rule.enabled = false;
            // Issue #800 critical fix: parity with the Enable arm —
            // signer + verifier must use the same canonical bytes.
            let canonical = rules_store::canonical_bytes_for_signing(&rule)?;
            let sig = signing_key.sign(&canonical);
            rules_store::set_enabled(&conn, &id, false)?;
            rules_store::update_signature(&conn, &id, &sig.to_bytes(), OPERATOR_SIGNED_LEVEL)?;
            let updated = rules_store::get(&conn, &id)?
                .context("rules.disable: row vanished after update")?;
            emit_ok(json, out, "rules.disable", &rule_to_json(&updated))?;
            Ok(())
        }
        RulesAction::Remove { id, sign } => {
            if !sign {
                bail!("governance.no_operator_key: `rules remove` requires --sign");
            }
            let signing_key = load_operator_signing_key_from_dir(&key_dir)?;
            // v0.7.0 SR — route deletion through the audited path so the
            // removal lands an operator-signed row on the signed_events
            // chain before the rule row is deleted (atomic). The old
            // bare `rules_store::remove` left no tamper-evident trace.
            let removed = rules_store::remove_signed(&conn, &id, &signing_key, OPERATOR_KEY_ID)?;
            let payload = serde_json::json!({ "id": id, "removed": removed });
            emit_ok(json, out, "rules.remove", &payload)?;
            Ok(())
        }
        RulesAction::Keygen {
            out: out_path,
            force,
        } => {
            // #1610 — keygen must write where the signing verbs read.
            // When the operator relocated the key store (`--key-dir`
            // flag or `AI_MEMORY_KEY_DIR`), the default write path is
            // `<key_dir>/operator.key` — the exact Layout-2 path
            // `load_operator_signing_key_from_dir` and the L1-6
            // verify ladder consult. Only with NO override in force
            // does the legacy singleton `<config>/ai-memory/operator.key`
            // location apply (the Layout-3 parent fallback covers it).
            let key_dir_overridden = args.key_dir.is_some() || kp::key_dir_env_override().is_some();
            let resolved =
                resolve_keygen_out_path(out_path.as_deref(), &key_dir, key_dir_overridden)?;
            let fingerprint = keygen_operator(&resolved, force, out)?;
            // #1686 — generating an operator key flips the substrate to
            // attest-active (`resolve_operator_pubkey().is_some()`), which makes
            // `enforced_rule_passes` SKIP every enabled rule that is not
            // operator-signed. The --force path warns about prior operator-signed
            // rules going invalid, but a FRESH keygen silently disables any
            // enabled-but-unsigned seed rules. Warn loudly so the operator knows
            // to run `rules sign-seed`.
            if let Ok(rules) = rules_store::list(&conn) {
                let dormant = rules
                    .iter()
                    .filter(|r| r.enabled && r.attest_level != OPERATOR_SIGNED_LEVEL)
                    .count();
                if dormant > 0 {
                    writeln!(
                        out.stderr,
                        "WARNING: {dormant} enabled rule(s) are not operator-signed. \
                         Generating this operator key activates signature enforcement, so \
                         those rules will be SKIPPED at load time until you run \
                         `ai-memory rules sign-seed`."
                    )?;
                }
            }
            let payload = serde_json::json!({
                "path": resolved.display().to_string(),
                "public_path": format!("{}.pub", resolved.display()),
                "fingerprint": fingerprint,
            });
            emit_ok(json, out, "rules.keygen", &payload)?;
            Ok(())
        }
        RulesAction::SignSeed { key, db } => {
            // The top-level `--db` flag already produced `conn` above.
            // When the operator passes `--db` on the subcommand (the
            // L1-6 ergonomic shortcut for one-shot scripts), reopen
            // against that path; otherwise reuse the open handle.
            //
            // #822: precedence for the operator key path —
            //   1. explicit `--key <PATH>` on the subcommand wins;
            //   2. else derive a path under the top-level `--key-dir` /
            //      `AI_MEMORY_KEY_DIR` resolution (line above), matching
            //      the dual-layout discipline of
            //      `load_operator_signing_key_from_dir` —
            //         a. `<key_dir>/operator.key` (the singleton
            //            layout `rules keygen` writes);
            //         b. `<key_dir>/operator.priv` (the legacy `kp::save`
            //            layout that paired with `operator.pub`).
            //      Both files are raw 32-byte ed25519 seeds, so the
            //      same `load_operator_signing_key` reader handles
            //      either path without further branching;
            //   3. else `sign_seed_rules`'s own fallback to
            //      `resolve_operator_key_path(None)` (the legacy
            //      `~/.config/ai-memory/operator.key` shape) keeps
            //      working when neither is supplied.
            let resolved_key: Option<PathBuf> = key.or_else(|| {
                let key_layout = key_dir.join(OPERATOR_KEY_FILENAME);
                if key_layout.exists() {
                    return Some(key_layout);
                }
                let priv_layout = key_dir.join("operator.priv");
                if priv_layout.exists() {
                    return Some(priv_layout);
                }
                None
            });
            if let Some(db_path) = db {
                // Migrating open (see the note on the top-level `conn`): the
                // `--db` sign-seed override must also create the schema on a
                // fresh path.
                let conn2 = crate::db::open(&db_path).with_context(|| {
                    format!("rules.sign-seed: open db at {}", db_path.display())
                })?;
                sign_seed_rules(&conn2, resolved_key.as_deref(), json, out)?;
            } else {
                sign_seed_rules(&conn, resolved_key.as_deref(), json, out)?;
            }
            Ok(())
        }
    }
}

// ---------------------------------------------------------------------------
// L1-6 — operator keypair generation + loading
// ---------------------------------------------------------------------------

/// Resolve where `rules keygen` writes (#1610). Precedence:
///
/// 1. explicit `--out <PATH>` — always wins;
/// 2. `<key_dir>/operator.key` when a key-dir override is in force
///    (`--key-dir` flag or `AI_MEMORY_KEY_DIR`) — keeps the write
///    path and the `--sign` verbs' read path
///    ([`load_operator_signing_key_from_dir`] Layout 2) on the SAME
///    directory, closing the split-brain where keygen wrote
///    `~/.config/ai-memory/operator.key` while `enable --sign` read
///    an empty `/etc/ai-memory/keys` and R001–R004 never enabled;
/// 3. the legacy singleton `<config>/ai-memory/operator.key`
///    ([`resolve_operator_key_path`]) when nothing is overridden —
///    the Layout-3 parent fallback makes `keygen → enable` work
///    there without any mirroring.
fn resolve_keygen_out_path(
    explicit_out: Option<&Path>,
    key_dir: &Path,
    key_dir_overridden: bool,
) -> Result<PathBuf> {
    if let Some(p) = explicit_out {
        return Ok(p.to_path_buf());
    }
    if key_dir_overridden {
        return Ok(key_dir.join(OPERATOR_KEY_FILENAME));
    }
    resolve_operator_key_path(None)
}

/// Resolve the operator key path: explicit `--out` override → default
/// `~/.config/ai-memory/operator.key`. The default lives next to the
/// per-agent `keys/` directory rather than under it because the
/// operator key is a singleton, not an enumerable list — see
/// `migrations/sqlite/0024_v07_governance_rules.sql` for the design
/// note.
fn resolve_operator_key_path(override_path: Option<&Path>) -> Result<PathBuf> {
    if let Some(p) = override_path {
        return Ok(p.to_path_buf());
    }
    let base = dirs::config_dir()
        .ok_or_else(|| anyhow::anyhow!("rules.keygen: OS did not advertise a config directory"))?;
    Ok(base.join("ai-memory").join(OPERATOR_KEY_FILENAME))
}

/// Generate a fresh Ed25519 keypair, write the 32-byte seed to `path`
/// (mode 0600 on Unix) and the base64-encoded verifying key to
/// `<path>.pub` (mode 0644). Returns the public-key fingerprint
/// (`sha256(pub_bytes)` truncated to 16 hex chars) for the success
/// line.
///
/// # Invariants
///
/// - Refuses to overwrite an existing private or public file unless
///   `force` is true.
/// - Even with `force`, emits a `WARNING` line to `stderr` reminding
///   the operator that all prior signatures will become invalid.
/// - On non-Unix targets the mode bits cannot be enforced; the
///   function emits a `WARNING` to `stderr` and skips the chmod.
///
/// # Security
///
/// The 32-byte seed is in scope only inside this function. It is
/// never returned, never logged, never embedded in a `tracing!`
/// macro. The caller receives only the fingerprint.
fn keygen_operator(path: &Path, force: bool, out: &mut CliOutput<'_>) -> Result<String> {
    let pub_path = pub_sibling_path(path);

    if !force && (path.exists() || pub_path.exists()) {
        bail!(
            "rules.keygen: refusing to overwrite existing key material at {} (or {}). \
             Pass --force to replace — note that all prior operator-signed rules \
             will fail signature verification with the new key.",
            path.display(),
            pub_path.display()
        );
    }
    if force && (path.exists() || pub_path.exists()) {
        writeln!(
            out.stderr,
            "WARNING: rules.keygen --force replaces existing operator key. \
             All prior operator-signed rules become INVALID and will be skipped at \
             load time until re-signed with the new key."
        )?;
    }

    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("rules.keygen: create parent dir {}", parent.display()))?;
    }

    // SECURITY: `OsRng` is the platform CSPRNG; ed25519-dalek's
    // `SigningKey::generate` consumes 32 bytes from it as the seed.
    let mut csprng = rand_core::OsRng;
    let signing = SigningKey::generate(&mut csprng);
    let verifying = signing.verifying_key();
    let seed = signing.to_bytes();
    let pub_bytes = verifying.to_bytes();

    // Private seed: mode 0600 on Unix; on Windows write the file but
    // emit a stderr warning that mode bits are unenforced.
    write_operator_private_seed(path, &seed, out)?;
    // Public key: base64(URL_SAFE_NO_PAD) of the 32-byte verifying key.
    write_operator_public_key(&pub_path, &pub_bytes)?;

    // Best-effort post-write fingerprint. We zero `seed` after use
    // out of habit; the local variable goes out of scope at function
    // end so the memory page is reclaimed on the next allocation.
    let fingerprint = pub_fingerprint(&pub_bytes);

    // SECURITY: print the fingerprint, never the seed.
    writeln!(
        out.stdout,
        "Ed25519 operator key generated: {fingerprint} -> {}",
        path.display()
    )?;

    // `seed` is `[u8; 32]`, a `Copy` type, so an explicit `drop`
    // call is a no-op. The Rust compiler reclaims the stack slot
    // automatically on scope exit; we simply rely on that.

    Ok(fingerprint)
}

/// Write the 32-byte private seed to `path` with mode 0600. The file
/// is created with `O_CREAT | O_WRONLY | O_TRUNC` so a pre-existing
/// file is truncated and the new bytes land atomically. After the
/// write we verify the mode bits via `stat` and refuse if anything
/// other than 0o600 is observed.
fn write_operator_private_seed(
    path: &Path,
    seed: &[u8; ED25519_SEED_LEN],
    #[cfg_attr(unix, allow(unused_variables))] out: &mut CliOutput<'_>,
) -> Result<()> {
    #[cfg(unix)]
    {
        use std::io::Write;
        use std::os::unix::fs::OpenOptionsExt;
        use std::os::unix::fs::PermissionsExt;

        // Remove first so a stricter pre-existing mode does not block
        // the create_new path; we already gated overwrite above.
        let _ = std::fs::remove_file(path);
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(path)
            .with_context(|| format!("rules.keygen: create {}", path.display()))?;
        file.write_all(seed)
            .with_context(|| format!("rules.keygen: write seed to {}", path.display()))?;
        file.sync_all()
            .with_context(|| format!("rules.keygen: fsync {}", path.display()))?;
        drop(file);

        // Verify the mode bits actually landed (defense against an
        // `OpenOptionsExt::mode` regression or a weird umask path).
        let mode = std::fs::metadata(path)
            .with_context(|| format!("rules.keygen: stat {}", path.display()))?
            .permissions()
            .mode()
            & 0o777;
        if mode != 0o600 {
            // Try once more to chmod to 0600 — best effort recovery.
            let mut perms = std::fs::metadata(path)?.permissions();
            perms.set_mode(0o600);
            std::fs::set_permissions(path, perms)
                .with_context(|| format!("rules.keygen: chmod 0600 {}", path.display()))?;
            let verified = std::fs::metadata(path)?.permissions().mode() & 0o777;
            if verified != 0o600 {
                bail!(
                    "rules.keygen: could not enforce mode 0600 on {} (observed {verified:o})",
                    path.display()
                );
            }
        }
        Ok(())
    }
    #[cfg(not(unix))]
    {
        writeln!(
            out.stderr,
            "WARNING: Windows: operator key permissions not enforced; protect manually"
        )?;
        std::fs::write(path, seed)
            .with_context(|| format!("rules.keygen: write seed to {}", path.display()))?;
        Ok(())
    }
}

/// Write the base64-encoded verifying key to `<path>.pub`. World-
/// readable (mode 0644 on Unix) because public keys are by definition
/// non-secret.
fn write_operator_public_key(pub_path: &Path, pub_bytes: &[u8; ED25519_PUBLIC_LEN]) -> Result<()> {
    use base64::Engine;
    let encoded = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(pub_bytes);
    #[cfg(unix)]
    {
        use std::io::Write;
        use std::os::unix::fs::OpenOptionsExt;
        let _ = std::fs::remove_file(pub_path);
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o644)
            .open(pub_path)
            .with_context(|| format!("rules.keygen: create {}", pub_path.display()))?;
        file.write_all(encoded.as_bytes())
            .with_context(|| format!("rules.keygen: write pub to {}", pub_path.display()))?;
        file.sync_all()
            .with_context(|| format!("rules.keygen: fsync {}", pub_path.display()))?;
    }
    #[cfg(not(unix))]
    {
        std::fs::write(pub_path, encoded.as_bytes())
            .with_context(|| format!("rules.keygen: write pub to {}", pub_path.display()))?;
    }
    Ok(())
}

/// Compute the `sha256(pub_bytes)` fingerprint truncated to 16 hex
/// chars. Used in the success line `Ed25519 operator key generated:
/// <fp> -> <path>` so the operator can sanity-check the public key
/// without inspecting the file. Truncated to 16 chars (64 bits) —
/// collision resistance is irrelevant here (the operator already
/// trusts the file path; this is for human-readable disambiguation).
fn pub_fingerprint(pub_bytes: &[u8; ED25519_PUBLIC_LEN]) -> String {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(pub_bytes);
    let digest = hasher.finalize();
    let mut out = String::with_capacity(16);
    for byte in digest.iter().take(8) {
        out.push_str(&format!("{byte:02x}"));
    }
    out
}

/// Resolve the public-key sibling path for a given private-seed path.
/// `~/.config/ai-memory/operator.key` → `~/.config/ai-memory/operator.key.pub`.
fn pub_sibling_path(seed_path: &Path) -> PathBuf {
    let mut s = seed_path.as_os_str().to_os_string();
    s.push(".pub");
    PathBuf::from(s)
}

/// Load the operator signing key from `path` (32 raw bytes, mode
/// 0600 on Unix). This is the public helper exposed for tests and
/// the L1-6 sign-seed pipeline.
///
/// # Errors
///
/// - Returns a clear error mentioning `0600` when the file mode is
///   anything other than 0o600 on Unix.
/// - Returns an error when the file length is not exactly 32 bytes.
/// - On non-Unix targets the mode check is skipped (file ACL applies
///   instead; the OSS layer does not enforce hardware-backed storage —
///   see `src/identity/keypair.rs` "Hardware-backed key storage"
///   section).
pub fn load_operator_signing_key(path: &Path) -> Result<SigningKey> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let meta = std::fs::metadata(path)
            .with_context(|| format!("load_operator_signing_key: stat {}", path.display()))?;
        let mode = meta.permissions().mode() & 0o777;
        if mode != 0o600 {
            bail!(
                "load_operator_signing_key: {} has mode {mode:o}; permissions too open; \
                 chmod 0600 {} to restore",
                path.display(),
                path.display()
            );
        }
    }
    let bytes = std::fs::read(path)
        .with_context(|| format!("load_operator_signing_key: read {}", path.display()))?;
    if bytes.len() != ED25519_SEED_LEN {
        bail!(
            "load_operator_signing_key: {} has {} bytes, expected {ED25519_SEED_LEN}",
            path.display(),
            bytes.len()
        );
    }
    let mut seed = [0u8; ED25519_SEED_LEN];
    seed.copy_from_slice(&bytes);
    Ok(SigningKey::from_bytes(&seed))
}

/// L1-6 Deliverable B — sign R001..R004 (and any other rows in
/// `governance_rules`) with the operator key. Idempotent: re-running
/// computes the same canonical bytes → same signature → same UPDATE;
/// a row whose `signature` already matches the freshly computed bytes
/// is a no-op.
///
/// `enabled` STAYS at whatever the row already holds — operator
/// activates manually after audit. Canonical bytes include `enabled`
/// (see [`rules_store::canonical_bytes_for_signing`]), so a post-sign
/// `UPDATE governance_rules SET enabled = 1` would invalidate the
/// recorded signature: that is the bypass-prevention property the
/// L1-6 integration tests pin.
///
/// Returns the number of rows that were freshly signed (excluding
/// idempotent no-ops).
fn sign_seed_rules(
    conn: &rusqlite::Connection,
    key_path: Option<&Path>,
    json: bool,
    out: &mut CliOutput<'_>,
) -> Result<usize> {
    let resolved = match key_path {
        Some(p) => p.to_path_buf(),
        None => resolve_operator_key_path(None)?,
    };
    let signing_key = load_operator_signing_key(&resolved).with_context(|| {
        format!(
            "rules.sign-seed: load operator key from {}",
            resolved.display()
        )
    })?;

    let rules = rules_store::list(conn)?;
    let mut signed_now = 0usize;
    let mut summary: Vec<serde_json::Value> = Vec::new();
    for rule in rules {
        let canonical = rules_store::canonical_bytes_for_signing(&rule)?;
        let signature = signing_key.sign(&canonical);
        let sig_bytes = signature.to_bytes();
        let already_signed = matches!(
            (rule.signature.as_deref(), rule.attest_level.as_str()),
            (Some(existing), OPERATOR_SIGNED_LEVEL) if existing == sig_bytes.as_slice()
        );
        if !already_signed {
            rules_store::update_signature(
                conn,
                &rule.id,
                sig_bytes.as_slice(),
                OPERATOR_SIGNED_LEVEL,
            )?;
            signed_now += 1;
        }
        summary.push(serde_json::json!({
            "id": rule.id,
            (field_names::ATTEST_LEVEL): OPERATOR_SIGNED_LEVEL,
            "signed_now": !already_signed,
        }));
    }

    let payload = serde_json::json!({
        "signed_now": signed_now,
        "rules": summary,
    });
    emit_ok(json, out, "rules.sign-seed", &payload)?;
    Ok(signed_now)
}

/// Resolve the operator key directory, honoring `--key-dir` →
/// `AI_MEMORY_KEY_DIR` → `kp::default_key_dir()`.
fn resolve_key_dir(override_dir: Option<&std::path::Path>) -> Result<PathBuf> {
    if let Some(p) = override_dir {
        return Ok(p.to_path_buf());
    }
    kp::default_key_dir()
}

/// Load the operator's signing key from `key_dir`. Auto-detects which
/// of the two operator-key naming conventions is in use:
///
/// 1. `operator.priv` (raw 32-byte seed) + `operator.pub` (raw 32-byte
///    verifying key) — the legacy dir-based layout the `add` / `enable`
///    / `disable` / `remove` verbs originally targeted, loaded via
///    [`kp::load`].
/// 2. `operator.key` (raw 32-byte seed) + `operator.key.pub` (base64url
///    no-pad encoded 32-byte verifying key) — the layout `rules keygen`
///    writes (`~/.config/ai-memory/operator.key`) per the L1-6 spec.
///
/// v0.7.0 G-PHASE-E-3 (#708) — before this fix, `rules keygen` wrote
/// files under (2) but `rules enable --sign` only looked for (1), so
/// the documented flow `keygen → enable` was broken end-to-end without
/// any error message that hinted at the naming mismatch. Now both
/// conventions are accepted; the error message when neither is found
/// names both so the operator can pick the right one.
///
/// Refuses if no matching pair is present, if the private-half mode
/// bits are not 0600 on Unix, or if the parsed bytes are not a valid
/// 32-byte Ed25519 signing key. Returns the typed `SigningKey` ready
/// to call `.sign()`.
fn load_operator_signing_key_from_dir(
    key_dir: &std::path::Path,
) -> Result<ed25519_dalek::SigningKey> {
    // Layout 1 — `operator.priv` + `operator.pub` (the legacy dir
    // layout). `kp::load` already handles mode-bit + length + curve
    // checks. Empty-dir cases that lack any operator file land in the
    // unified error path below.
    let priv_legacy = key_dir.join("operator.priv");
    let pub_legacy = key_dir.join("operator.pub");
    if priv_legacy.exists() && pub_legacy.exists() {
        let kp = kp::load(OPERATOR_KEY_ID, key_dir).with_context(|| {
            format!(
                "governance.no_operator_key: failed loading operator.priv/operator.pub at {}",
                key_dir.display()
            )
        })?;
        return kp.private.ok_or_else(|| {
            anyhow::anyhow!(
                "governance.no_operator_key: operator keypair has no private half (public-only load)"
            )
        });
    }
    // Layout 2 — `operator.key` (raw 32-byte seed) + `operator.key.pub`
    // (base64url no-pad encoded 32-byte verifying key). This is what
    // `rules keygen` writes; verify the public half decodes and matches
    // the seed's derived verifying key before returning so a tampered
    // .pub surfaces here, not on the next signature-verify call.
    let priv_keygen = key_dir.join(OPERATOR_KEY_FILENAME);
    let pub_keygen = key_dir.join("operator.key.pub");
    if priv_keygen.exists() {
        let signing = load_operator_signing_key(&priv_keygen).with_context(|| {
            format!(
                "governance.no_operator_key: failed loading {}",
                priv_keygen.display()
            )
        })?;
        if pub_keygen.exists() {
            use base64::Engine;
            let encoded = std::fs::read_to_string(&pub_keygen).with_context(|| {
                format!("governance.no_operator_key: read {}", pub_keygen.display())
            })?;
            let trimmed = encoded.trim();
            let pub_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
                .decode(trimmed)
                .with_context(|| {
                    format!(
                        "governance.no_operator_key: decode base64url public key at {}",
                        pub_keygen.display()
                    )
                })?;
            if pub_bytes.len() != ED25519_PUBLIC_LEN {
                bail!(
                    "governance.no_operator_key: public key {} decoded to {} bytes (expected {ED25519_PUBLIC_LEN})",
                    pub_keygen.display(),
                    pub_bytes.len(),
                );
            }
            if signing.verifying_key().to_bytes().as_slice() != pub_bytes.as_slice() {
                bail!(
                    "governance.no_operator_key: private key {} does not match public key {}",
                    priv_keygen.display(),
                    pub_keygen.display(),
                );
            }
        }
        return Ok(signing);
    }
    // Layout 3 (#800 Gap #6 — keygen↔enable path-mismatch fallback) —
    // `ai-memory rules keygen` writes the operator key to
    // `<config-dir>/operator.key` (parent of the key_dir), per
    // `resolve_operator_key_path`'s "singleton, not enumerable list"
    // rationale documented in
    // `migrations/sqlite/0024_v07_governance_rules.sql`. The L1-6
    // verify path (`rules_store::resolve_operator_pubkey`) reads from
    // the parent dir for the same reason. Before this fallback, the
    // `enable/disable/add --sign` verbs refused with
    // `governance.no_operator_key` even when a fresh keygen had just
    // run. The install-batman-active.sh script worked around it by
    // mirroring the key into both locations. This in-process fallback
    // closes the wart so a fresh keygen + immediate enable just works.
    if let Some(parent) = key_dir.parent() {
        let parent_priv = parent.join(OPERATOR_KEY_FILENAME);
        let parent_pub = parent.join("operator.key.pub");
        if parent_priv.exists() {
            let signing = load_operator_signing_key(&parent_priv).with_context(|| {
                format!(
                    "governance.no_operator_key: failed loading {}",
                    parent_priv.display()
                )
            })?;
            if parent_pub.exists() {
                use base64::Engine;
                let encoded = std::fs::read_to_string(&parent_pub).with_context(|| {
                    format!("governance.no_operator_key: read {}", parent_pub.display())
                })?;
                let trimmed = encoded.trim();
                let pub_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
                    .decode(trimmed)
                    .with_context(|| {
                        format!(
                            "governance.no_operator_key: decode base64url public key at {}",
                            parent_pub.display()
                        )
                    })?;
                if pub_bytes.len() != ED25519_PUBLIC_LEN {
                    bail!(
                        "governance.no_operator_key: public key {} decoded to {} bytes (expected {ED25519_PUBLIC_LEN})",
                        parent_pub.display(),
                        pub_bytes.len(),
                    );
                }
                if signing.verifying_key().to_bytes().as_slice() != pub_bytes.as_slice() {
                    bail!(
                        "governance.no_operator_key: private key {} does not match public key {}",
                        parent_priv.display(),
                        parent_pub.display(),
                    );
                }
            }
            return Ok(signing);
        }
    }

    // Neither layout present — name all three so the operator picks
    // the right one to materialise.
    bail!(
        "governance.no_operator_key: no operator key found at {dir} \
         (also checked parent dir for the keygen layout). \
         Expected either `operator.priv` + `operator.pub` (raw 32-byte pair, \
         as produced by per-agent `keypair` generation) OR \
         `operator.key` + `operator.key.pub` (raw 32-byte seed + base64url \
         verifier, as produced by `ai-memory rules keygen` — searched both \
         `{dir}/` and `{dir}/../`)",
        dir = key_dir.display(),
    )
}

/// Resolve the caller's agent_id for `created_by` provenance. Uses
/// the same NHI vocabulary as the rest of the CLI. Falls back to a
/// process-bound id if env / clientInfo resolution fails.
fn resolve_agent_id() -> String {
    crate::identity::resolve_agent_id(None, None)
        .unwrap_or_else(|_| format!("anonymous:pid-{}", std::process::id()))
}

/// Build an [`AgentAction`] from `kind` + JSON payload. Used by
/// `rules check` to mirror the harness PreToolUse hook input.
fn build_action(kind: &str, payload_json: &str) -> Result<AgentAction> {
    let payload: serde_json::Value = serde_json::from_str(payload_json)
        .with_context(|| format!("rules check: payload is not valid JSON: {payload_json}"))?;
    match kind {
        ak::BASH => {
            let command = payload
                .get("command")
                .and_then(|v| v.as_str())
                .ok_or_else(|| anyhow::anyhow!("bash payload requires `command` string"))?
                .to_string();
            let cwd = payload
                .get("cwd")
                .and_then(|v| v.as_str())
                .map(PathBuf::from);
            Ok(AgentAction::Bash { command, cwd })
        }
        ak::FILESYSTEM_WRITE => {
            let path = payload
                .get("path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| anyhow::anyhow!("filesystem_write payload requires `path` string"))?
                .to_string();
            let byte_estimate = payload
                .get("byte_estimate")
                .and_then(serde_json::Value::as_u64);
            Ok(AgentAction::FilesystemWrite {
                path: PathBuf::from(path),
                byte_estimate,
            })
        }
        ak::NETWORK_REQUEST => {
            let host = payload
                .get("host")
                .and_then(|v| v.as_str())
                .ok_or_else(|| anyhow::anyhow!("network_request payload requires `host` string"))?
                .to_string();
            let scheme = payload
                .get("scheme")
                .and_then(|v| v.as_str())
                .unwrap_or("https")
                .to_string();
            Ok(AgentAction::NetworkRequest { host, scheme })
        }
        ak::PROCESS_SPAWN => {
            let binary = payload
                .get("binary")
                .and_then(|v| v.as_str())
                .ok_or_else(|| anyhow::anyhow!("process_spawn payload requires `binary` string"))?
                .to_string();
            let args = payload
                .get("args")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            Ok(AgentAction::ProcessSpawn { binary, args })
        }
        "custom" => {
            let custom_kind = payload
                .get(field_names::CUSTOM_KIND)
                .or_else(|| payload.get("kind"))
                .and_then(|v| v.as_str())
                .ok_or_else(|| anyhow::anyhow!("custom payload requires `custom_kind` string"))?
                .to_string();
            Ok(AgentAction::Custom {
                custom_kind,
                payload,
            })
        }
        other => bail!("rules check: unknown kind `{other}`"),
    }
}

/// Render a [`Rule`] as JSON for CLI output. The signature is
/// base64-encoded (URL-safe, no padding) so the JSON is operator-
/// readable. Empty signature ⇒ null.
fn rule_to_json(rule: &Rule) -> serde_json::Value {
    use base64::Engine;
    let sig_b64 = rule
        .signature
        .as_ref()
        .map(|b| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(b));
    serde_json::json!({
        "id": rule.id,
        "kind": rule.kind,
        "matcher": rule.matcher,
        "severity": rule.severity,
        "reason": rule.reason,
        "namespace": rule.namespace,
        (field_names::CREATED_BY): rule.created_by,
        (field_names::CREATED_AT): rule.created_at,
        "enabled": rule.enabled,
        "signature_b64": sig_b64,
        (field_names::ATTEST_LEVEL): rule.attest_level,
    })
}

fn emit_ok(
    json: bool,
    out: &mut CliOutput<'_>,
    verb: &str,
    result: &serde_json::Value,
) -> Result<()> {
    if json {
        let env = CliEnvelope {
            verb,
            result: result.clone(),
        };
        writeln!(out.stdout, "{}", serde_json::to_string(&env)?)?;
    } else {
        // Human format: pretty-print the result tree. The verb header
        // is suppressed (the CLI command itself is the implicit
        // context).
        writeln!(out.stdout, "{}", serde_json::to_string_pretty(result)?)?;
    }
    Ok(())
}

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

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

    /// Issue #899 — guard against cross-test forensic-sink bleed.
    ///
    /// `RulesAction::Check` fires `check_agent_action`, which
    /// indirectly emits `crate::governance::audit::record_decision`.
    /// Tests in this module that exercise `RulesAction::Check`
    /// MUST hold this lock so a sibling `audit::tests::*` test does
    /// not see this thread's `record_decision` land in its tempdir.
    /// See `governance::audit::forensic_sink_test_lock`.
    #[must_use = "the guard must be held for the scope of the test"]
    fn forensic_lock() -> std::sync::MutexGuard<'static, ()> {
        crate::governance::audit::forensic_sink_test_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner())
    }

    #[test]
    fn build_action_bash_parses() {
        let a = build_action("bash", r#"{"command":"ls -la"}"#).unwrap();
        match a {
            AgentAction::Bash { command, cwd } => {
                assert_eq!(command, "ls -la");
                assert!(cwd.is_none());
            }
            _ => panic!("expected bash"),
        }
    }

    #[test]
    fn build_action_filesystem_write_parses() {
        let a = build_action("filesystem_write", r#"{"path":"/tmp/x"}"#).unwrap();
        match a {
            AgentAction::FilesystemWrite { path, .. } => {
                assert_eq!(path, PathBuf::from("/tmp/x"));
            }
            _ => panic!("expected filesystem_write"),
        }
    }

    #[test]
    fn build_action_network_request_parses_with_scheme_default() {
        let a = build_action("network_request", r#"{"host":"x.example.com"}"#).unwrap();
        match a {
            AgentAction::NetworkRequest { host, scheme } => {
                assert_eq!(host, "x.example.com");
                assert_eq!(scheme, "https");
            }
            _ => panic!("expected network_request"),
        }
    }

    #[test]
    fn build_action_process_spawn_parses() {
        let a = build_action(
            "process_spawn",
            r#"{"binary":"cargo","args":["build","--release"]}"#,
        )
        .unwrap();
        match a {
            AgentAction::ProcessSpawn { binary, args } => {
                assert_eq!(binary, "cargo");
                assert_eq!(args, vec!["build", "--release"]);
            }
            _ => panic!("expected process_spawn"),
        }
    }

    #[test]
    fn build_action_custom_parses() {
        let a = build_action("custom", r#"{"custom_kind":"deploy","env":"prod"}"#).unwrap();
        match a {
            AgentAction::Custom { custom_kind, .. } => assert_eq!(custom_kind, "deploy"),
            _ => panic!("expected custom"),
        }
    }

    #[test]
    fn build_action_unknown_kind_errors() {
        assert!(build_action("nope", "{}").is_err());
    }

    #[test]
    fn build_action_invalid_json_errors() {
        assert!(build_action("bash", "not json").is_err());
    }

    #[test]
    fn build_action_missing_required_field_errors() {
        assert!(build_action("bash", "{}").is_err());
        assert!(build_action("filesystem_write", "{}").is_err());
    }

    #[test]
    fn rule_to_json_encodes_signature_as_base64() {
        let mut rule = Rule {
            id: "R1".into(),
            kind: "bash".into(),
            matcher: r#"{"command_regex":"x"}"#.into(),
            severity: "refuse".into(),
            reason: "test".into(),
            namespace: "_global".into(),
            created_by: "test".into(),
            created_at: 0,
            enabled: true,
            signature: None,
            attest_level: "unsigned".into(),
        };
        let v = rule_to_json(&rule);
        assert_eq!(v["signature_b64"], serde_json::Value::Null);
        rule.signature = Some(vec![0xff, 0x00, 0xaa]);
        let v = rule_to_json(&rule);
        assert_eq!(
            v["signature_b64"],
            serde_json::Value::String("_wCq".to_string())
        );
    }

    // -----------------------------------------------------------------
    // L1-6 — keygen + load_operator_signing_key unit tests
    // -----------------------------------------------------------------

    #[test]
    fn pub_sibling_path_appends_dot_pub() {
        let p = pub_sibling_path(Path::new("/x/y/operator.key"));
        assert_eq!(p, PathBuf::from("/x/y/operator.key.pub"));
    }

    #[test]
    fn pub_fingerprint_is_deterministic_and_16_hex_chars() {
        let bytes = [0u8; 32];
        let fp1 = pub_fingerprint(&bytes);
        let fp2 = pub_fingerprint(&bytes);
        assert_eq!(fp1, fp2, "fingerprint must be deterministic");
        assert_eq!(fp1.len(), 16, "fingerprint must be 16 hex chars");
        assert!(
            fp1.chars().all(|c| c.is_ascii_hexdigit()),
            "fingerprint must be ASCII hex"
        );
        // Different input → different fingerprint.
        let mut other = [0u8; 32];
        other[0] = 1;
        let fp3 = pub_fingerprint(&other);
        assert_ne!(fp1, fp3);
    }

    #[cfg(unix)]
    #[test]
    fn keygen_writes_priv_0600_and_pub_0644_then_loads() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let fp = keygen_operator(&key_path, false, &mut out).expect("keygen");
        assert_eq!(fp.len(), 16);

        // Private file: mode 0600 + 32 bytes.
        let meta = std::fs::metadata(&key_path).unwrap();
        let mode = meta.permissions().mode() & 0o777;
        assert_eq!(mode, 0o600, "priv key must be 0600, got {mode:o}");
        let bytes = std::fs::read(&key_path).unwrap();
        assert_eq!(bytes.len(), 32, "priv seed must be 32 bytes");

        // Public file: mode 0644 + base64 of 32 bytes.
        let pub_path = pub_sibling_path(&key_path);
        let pmode = std::fs::metadata(&pub_path).unwrap().permissions().mode() & 0o777;
        assert_eq!(pmode, 0o644, "pub key must be 0644, got {pmode:o}");
        let pub_b64 = std::fs::read_to_string(&pub_path).unwrap();
        use base64::Engine;
        let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .decode(pub_b64.trim())
            .expect("pub base64 decodes");
        assert_eq!(decoded.len(), 32);

        // load_operator_signing_key round-trips and the derived
        // verifying key matches the .pub bytes.
        let signing = load_operator_signing_key(&key_path).expect("load");
        let verifying = signing.verifying_key();
        assert_eq!(verifying.to_bytes()[..], decoded[..]);

        // Stdout includes the fingerprint and the path; never the seed.
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains(&fp), "stdout must include fingerprint, got: {s}");
        // Seed bytes should never round-trip through stdout (defensive
        // check: the random seed is unlikely to be valid utf8 anyway,
        // but we assert the success line is the only stdout content).
        assert!(s.starts_with("Ed25519 operator key generated:"));
    }

    #[cfg(unix)]
    #[test]
    fn keygen_refuses_overwrite_without_force() {
        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).expect("first");
        let bytes_before = std::fs::read(&key_path).unwrap();

        // Second call without --force must refuse.
        let err = keygen_operator(&key_path, false, &mut out).unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("refusing to overwrite"), "got: {msg}");

        // Bytes on disk must not have changed.
        let bytes_after = std::fs::read(&key_path).unwrap();
        assert_eq!(bytes_before, bytes_after);
    }

    #[cfg(unix)]
    #[test]
    fn keygen_force_overwrites_and_warns_on_stderr() {
        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let fp1 = keygen_operator(&key_path, false, &mut out).expect("first");
        let fp2 = keygen_operator(&key_path, true, &mut out).expect("force");
        assert_ne!(fp1, fp2, "fresh keypair must have new fingerprint");

        let s = String::from_utf8(stderr).unwrap();
        assert!(
            s.contains("WARNING") && s.contains("INVALID"),
            "stderr must warn about prior-signature invalidation, got: {s}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn load_operator_signing_key_refuses_open_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).expect("keygen");
        // Loosen perms.
        std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o644)).unwrap();
        let err = load_operator_signing_key(&key_path).unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("0600"), "error must mention 0600, got: {msg}");
        // Restore so the tempdir cleanup works.
        std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600)).unwrap();
    }

    #[test]
    fn load_operator_signing_key_rejects_wrong_length() {
        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        // Write a short file that bypasses the mode check (or, on
        // unix, the mode check fires first — both paths exercise the
        // "refuse to sign with non-conforming material" property).
        std::fs::write(&key_path, b"too-short").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600)).unwrap();
        }
        let err = load_operator_signing_key(&key_path).unwrap_err();
        let msg = format!("{err:#}");
        // Either the length error or the stat error is acceptable —
        // both are refusals.
        assert!(
            msg.contains("expected") || msg.contains("bytes"),
            "got: {msg}"
        );
    }

    // -----------------------------------------------------------------
    // L1-6 — sign_seed_rules unit tests
    // -----------------------------------------------------------------

    /// Build a fresh in-memory rules-only schema for `sign_seed_rules`
    /// tests. Same shape as the engine's `fresh_conn` helper in
    /// `governance::agent_action::tests` but here we only need the
    /// rules table (no audit chain — sign-seed is pure SQL UPDATE).
    fn fresh_rules_conn() -> rusqlite::Connection {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        conn.execute_batch(
            "CREATE TABLE governance_rules (
                 id TEXT PRIMARY KEY,
                 kind TEXT NOT NULL,
                 matcher TEXT NOT NULL,
                 severity TEXT NOT NULL CHECK (severity IN ('refuse','warn','log')),
                 reason TEXT NOT NULL,
                 namespace TEXT NOT NULL DEFAULT '_global',
                 created_by TEXT NOT NULL,
                 created_at INTEGER NOT NULL,
                 enabled INTEGER NOT NULL DEFAULT 1,
                 signature BLOB,
                 attest_level TEXT NOT NULL DEFAULT 'unsigned'
             );",
        )
        .unwrap();
        conn
    }

    #[cfg(unix)]
    #[test]
    fn sign_seed_rules_marks_all_rows_operator_signed() {
        let tdir = tempfile::tempdir().unwrap();
        let key_path = tdir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).unwrap();

        let conn = fresh_rules_conn();
        // Seed two unsigned rules to mirror the migration's R001..R004
        // shape (enabled=false, attest_level='unsigned').
        for id in ["R001", "R002"] {
            rules_store::insert(
                &conn,
                &Rule {
                    id: id.to_string(),
                    kind: "filesystem_write".into(),
                    matcher: r#"{"glob":"/tmp/**"}"#.into(),
                    severity: "refuse".into(),
                    reason: "test".into(),
                    namespace: "_global".into(),
                    created_by: "system:seed".into(),
                    created_at: 0,
                    enabled: false,
                    signature: None,
                    attest_level: "unsigned".into(),
                },
            )
            .unwrap();
        }

        let signed = sign_seed_rules(&conn, Some(&key_path), true, &mut out).unwrap();
        assert_eq!(signed, 2);

        // Every row is now operator_signed with a 64-byte signature
        // and `enabled` UNCHANGED (audit must be operator-driven).
        for id in ["R001", "R002"] {
            let row = rules_store::get(&conn, id).unwrap().unwrap();
            assert_eq!(row.attest_level, "operator_signed");
            assert_eq!(
                row.signature.as_ref().map(Vec::len),
                Some(ed25519_dalek::SIGNATURE_LENGTH)
            );
            assert!(!row.enabled, "sign-seed must NOT flip enabled");
        }
    }

    // -----------------------------------------------------------------
    // C-3 coverage uplift — drive `run()` for every subcommand. The
    // mutation verbs require an operator keypair on disk under
    // `<key_dir>/operator.priv` (kp::save layout); the keygen + sign-seed
    // verbs use the singleton-file layout.
    // -----------------------------------------------------------------

    /// Set up a tempdir with a `db::open`-initialized SQLite at
    /// `db_path` and an operator keypair saved under `key_dir`. Returns
    /// the tempdir guard (must outlive the test) and the two paths.
    #[cfg(unix)]
    fn fresh_env_with_operator_key() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf)
    {
        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("ai-memory.db");
        // Initialize the full schema.
        drop(crate::db::open(&db_path).expect("db::open"));
        // Save an operator keypair at <key_dir>/operator.{priv,pub}.
        let kp = kp::generate(OPERATOR_KEY_ID).expect("generate");
        let key_dir = dir.path().join("keys");
        std::fs::create_dir_all(&key_dir).expect("mkdir keys");
        kp::save(&kp, &key_dir).expect("save kp");
        (dir, db_path, key_dir)
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_list_emits_seeded_rules() {
        // `db::open` runs migration 0024 which seeds R001..R004 disabled.
        // The list verb returns them with attest_level=unsigned. We pin
        // the dispatch + JSON envelope shape (not the seed content,
        // since the migration is owned by L0.7-2).
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::List,
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("list");
        let s = String::from_utf8(stdout).unwrap();
        // Envelope wraps the result under "rules.list".
        assert!(s.contains("\"verb\":\"rules.list\""), "got: {s}");
        // List result is an array; either empty or pre-seeded.
        assert!(s.contains("\"result\":["), "got: {s}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_list_human_format_emits_pretty_array() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::List,
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        // json=false → emit_ok's pretty-print branch.
        run(&db_path, args, false, &mut out).expect("list");
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("["), "got: {s}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_add_without_sign_refuses() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Add {
                id: "R-test".into(),
                kind: "bash".into(),
                matcher: r#"{"command_regex":"^ls"}"#.into(),
                severity: "refuse".into(),
                reason: "test".into(),
                namespace: "_global".into(),
                disabled: false,
                sign: false,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        let msg = format!("{err:#}");
        assert!(msg.contains("no_operator_key"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_add_with_sign_persists_signed_rule() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir.clone()),
            action: RulesAction::Add {
                id: "R-add-1".into(),
                kind: "bash".into(),
                // SEC-12/COR-10: literal substring (engine has always done
                // substring match, despite the legacy field name).
                matcher: r#"{"command_substring":"rm -rf /"}"#.into(),
                severity: "refuse".into(),
                reason: "rm-rf is bad".into(),
                namespace: "_global".into(),
                disabled: false,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("add");
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("rules.add"), "got: {s}");
        assert!(s.contains("R-add-1"), "got: {s}");
        assert!(s.contains("operator_signed"), "got: {s}");

        // Confirm the row landed.
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let r = rules_store::get(&conn, "R-add-1").unwrap().unwrap();
        assert_eq!(r.attest_level, "operator_signed");
        assert!(r.signature.is_some());
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_add_with_bad_matcher_json_errors() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Add {
                id: "R-bad".into(),
                kind: "bash".into(),
                matcher: "{ not json".into(), // malformed
                severity: "refuse".into(),
                reason: "x".into(),
                namespace: "_global".into(),
                disabled: false,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        let msg = format!("{err:#}");
        assert!(msg.contains("matcher"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_add_disabled_lands_disabled_row() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Add {
                id: "R-dis".into(),
                kind: "filesystem_write".into(),
                matcher: r#"{"glob":"/tmp/**"}"#.into(),
                severity: "warn".into(),
                reason: "noisy".into(),
                namespace: "_global".into(),
                disabled: true,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("add");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let r = rules_store::get(&conn, "R-dis").unwrap().unwrap();
        assert!(!r.enabled, "disabled flag must propagate");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_check_evaluates_action_against_empty_set() {
        let _forensic = forensic_lock();
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Check {
                kind: "bash".into(),
                payload: r#"{"command":"ls"}"#.into(),
                agent_id: Some("tester".into()),
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("check");
        let s = String::from_utf8(stdout).unwrap();
        // Decision JSON envelope — at minimum "rules.check" verb shows up.
        assert!(s.contains("rules.check"), "got: {s}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_check_without_agent_id_uses_default() {
        let _forensic = forensic_lock();
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Check {
                kind: "network_request".into(),
                payload: r#"{"host":"example.com","scheme":"https"}"#.into(),
                agent_id: None,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("check");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_enable_unsign_refuses() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Enable {
                id: "R-x".into(),
                sign: false,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        assert!(format!("{err:#}").contains("no_operator_key"));
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_enable_unknown_id_errors() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Enable {
                id: "R-does-not-exist".into(),
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must error");
        assert!(format!("{err:#}").contains("no rule with id"));
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_enable_and_disable_roundtrip() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        // First add a disabled rule.
        let args = RulesArgs {
            key_dir: Some(key_dir.clone()),
            action: RulesAction::Add {
                id: "R-toggle".into(),
                kind: "bash".into(),
                // SEC-12/COR-10: literal substring matcher.
                matcher: r#"{"command_substring":"x"}"#.into(),
                severity: "warn".into(),
                reason: "toggle me".into(),
                namespace: "_global".into(),
                disabled: true,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("add");

        // Enable.
        let args = RulesArgs {
            key_dir: Some(key_dir.clone()),
            action: RulesAction::Enable {
                id: "R-toggle".into(),
                sign: true,
            },
        };
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("enable");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        assert!(
            rules_store::get(&conn, "R-toggle")
                .unwrap()
                .unwrap()
                .enabled
        );
        drop(conn);

        // Disable.
        let args = RulesArgs {
            key_dir: Some(key_dir.clone()),
            action: RulesAction::Disable {
                id: "R-toggle".into(),
                sign: true,
            },
        };
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("disable");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        assert!(
            !rules_store::get(&conn, "R-toggle")
                .unwrap()
                .unwrap()
                .enabled
        );
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_disable_unsign_refuses() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Disable {
                id: "R-x".into(),
                sign: false,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        assert!(format!("{err:#}").contains("no_operator_key"));
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_disable_unknown_id_errors() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Disable {
                id: "R-missing".into(),
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must error");
        assert!(format!("{err:#}").contains("no rule with id"));
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_remove_unsign_refuses() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Remove {
                id: "R-x".into(),
                sign: false,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        assert!(format!("{err:#}").contains("no_operator_key"));
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_remove_signed_deletes_row() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        // Add then remove.
        let args = RulesArgs {
            key_dir: Some(key_dir.clone()),
            action: RulesAction::Add {
                id: "R-rm".into(),
                kind: "bash".into(),
                // SEC-12/COR-10: literal substring matcher.
                matcher: r#"{"command_substring":"x"}"#.into(),
                severity: "warn".into(),
                reason: "rm me".into(),
                namespace: "_global".into(),
                disabled: false,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("add");

        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Remove {
                id: "R-rm".into(),
                sign: true,
            },
        };
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("remove");
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("rules.remove"), "got: {s}");
        assert!(s.contains("\"removed\":true"), "got: {s}");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        assert!(rules_store::get(&conn, "R-rm").unwrap().is_none());
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_keygen_writes_keypair_under_explicit_out() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("ai-memory.db");
        drop(crate::db::open(&db_path).expect("db::open"));
        let key_path = dir.path().join("op.key");
        let args = RulesArgs {
            key_dir: None,
            action: RulesAction::Keygen {
                out: Some(key_path.clone()),
                force: false,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("keygen");
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("rules.keygen"), "got: {s}");
        assert!(key_path.exists(), "priv key missing");
        let pub_path = pub_sibling_path(&key_path);
        assert!(pub_path.exists(), "pub key missing");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_signs_existing_rules() {
        // Build a fully-initialized DB, add a rule via run(), then call
        // sign-seed via run() (with --db override + --key explicit).
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        // Add an unsigned-attest-level rule directly so sign_seed_rules
        // has at least one row to operate on.
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        rules_store::insert(
            &conn,
            &Rule {
                id: "R-ss".into(),
                kind: "bash".into(),
                matcher: r#"{"command_regex":"^x"}"#.into(),
                severity: "refuse".into(),
                reason: "t".into(),
                namespace: "_global".into(),
                created_by: "test".into(),
                created_at: 0,
                enabled: true,
                signature: None,
                attest_level: "unsigned".into(),
            },
        )
        .unwrap();
        drop(conn);

        // The sign-seed verb expects the singleton-file layout
        // (`~/.config/ai-memory/operator.key`). We saved the keypair
        // in dir-layout for the other tests, so generate a fresh
        // singleton-file via `keygen_operator` first.
        let dir2 = tempfile::tempdir().unwrap();
        let key_file = dir2.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_file, false, &mut out).unwrap();

        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::SignSeed {
                key: Some(key_file),
                db: Some(db_path.clone()),
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        // We pass a separate `--db` to drive the dispatch's
        // `if let Some(db_path) = db` branch (line 350-354).
        let placeholder_db = tempfile::tempdir().unwrap();
        let placeholder_path = placeholder_db.path().join("placeholder.db");
        drop(crate::db::open(&placeholder_path).unwrap());
        run(&placeholder_path, args, true, &mut out).expect("sign-seed");
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("rules.sign-seed"), "got: {s}");
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_reuses_open_conn_when_no_db_override() {
        // Drives the else-branch (line 356) where the top-level
        // `--db` flag's open connection is reused.
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let dir2 = tempfile::tempdir().unwrap();
        let key_file = dir2.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_file, false, &mut out).unwrap();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::SignSeed {
                key: Some(key_file),
                db: None,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("sign-seed reuse");
    }

    /// #822 regression helper: drive `rules sign-seed --key-dir <dir>`
    /// (with no explicit `--key`) and assert it succeeds. Pre-fix this
    /// fell through to `~/.config/ai-memory/operator.key` and failed
    /// with `No such file or directory` on CI runners with no $HOME
    /// keypair laid down.
    #[cfg(unix)]
    fn assert_sign_seed_succeeds_with_key_dir_only(
        db_path: &std::path::Path,
        key_dir: std::path::PathBuf,
    ) {
        let conn = rusqlite::Connection::open(db_path).unwrap();
        rules_store::insert(
            &conn,
            &Rule {
                id: "R-822".into(),
                kind: "bash".into(),
                matcher: r#"{"command_regex":"^x"}"#.into(),
                severity: "refuse".into(),
                reason: "t".into(),
                namespace: "_global".into(),
                created_by: "test".into(),
                created_at: 0,
                enabled: true,
                signature: None,
                attest_level: "unsigned".into(),
            },
        )
        .unwrap();
        drop(conn);

        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::SignSeed {
                key: None, // load-bearing: NO explicit --key.
                db: None,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let result = run(db_path, args, true, &mut out);
        let stderr_s = String::from_utf8_lossy(&stderr).to_string();
        assert!(
            result.is_ok(),
            "#822: sign-seed must honor --key-dir; got err={result:?} stderr={stderr_s}"
        );
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("rules.sign-seed"), "got: {s}");
    }

    /// #822 regression — layout-2 (`<key_dir>/operator.key`, the
    /// singleton-file layout `rules keygen --out <dir>/operator.key`
    /// writes; this is the layout the failing CI test used).
    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_honors_key_dir_layout_key() {
        let (dir, db_path, _kp_key_dir) = fresh_env_with_operator_key();
        // Lay down only the singleton-file layout.
        let key_dir = dir.path().join("keys-822-key");
        std::fs::create_dir_all(&key_dir).unwrap();
        let key_file = key_dir.join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_file, false, &mut out).unwrap();
        assert!(key_file.exists(), "keygen must lay down operator.key");
        assert!(
            !key_dir.join("operator.priv").exists(),
            "this branch must not have the .priv layout present"
        );
        assert_sign_seed_succeeds_with_key_dir_only(&db_path, key_dir);
    }

    /// #822 regression — layout-1 (`<key_dir>/operator.priv` +
    /// `operator.pub`, the legacy `kp::save` layout that
    /// `fresh_env_with_operator_key` writes).
    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_honors_key_dir_layout_priv() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        assert!(
            key_dir.join("operator.priv").exists(),
            "fresh_env_with_operator_key must lay down operator.priv"
        );
        assert!(
            !key_dir.join("operator.key").exists(),
            "this branch must not have the .key layout present"
        );
        assert_sign_seed_succeeds_with_key_dir_only(&db_path, key_dir);
    }

    /// #827 regression — third branch of the `key.or_else(...)` chain.
    /// When `--key-dir <dir>` is supplied and `<dir>` contains NEITHER
    /// `operator.key` nor `operator.priv`, `resolved_key` is `None`
    /// and `sign_seed_rules` falls through to
    /// `resolve_operator_key_path(None)` (the legacy
    /// `~/.config/ai-memory/operator.key` shape). With `HOME` +
    /// `XDG_CONFIG_HOME` pointed at an empty tempdir, that legacy
    /// path also fails to resolve, surfacing as a load-error citing
    /// the legacy path. The test pins that this third branch yields
    /// a clean Err (not a panic, not an Ok), closing the
    /// PR #820 cli/rules.rs coverage-floor breach.
    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_neither_layout_falls_through_to_legacy_path_and_errors() {
        // Serialize HOME/XDG mutation against parallel tests in this
        // module so we don't race other tests that read those vars.
        static HOME_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
        let _guard = HOME_ENV_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        // Snapshot prior values so we restore even on assertion panic.
        let prev_home = std::env::var("HOME").ok();
        let prev_xdg = std::env::var("XDG_CONFIG_HOME").ok();

        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("ai-memory.db");
        // Initialize the full schema — same shape as
        // `fresh_env_with_operator_key` minus the keypair.
        drop(crate::db::open(&db_path).expect("db::open"));

        // `key_dir` exists but contains neither `operator.key` nor
        // `operator.priv` — this is the load-bearing precondition that
        // forces `resolved_key = None` and triggers the third branch.
        let key_dir = dir.path().join("empty-keys");
        std::fs::create_dir_all(&key_dir).expect("mkdir empty-keys");
        assert!(
            !key_dir.join("operator.key").exists() && !key_dir.join("operator.priv").exists(),
            "preconditions: neither layout may exist for this branch"
        );

        // Point HOME + XDG_CONFIG_HOME at empty subdirs so
        // `dirs::config_dir()` (the source of the legacy default path)
        // resolves to a directory with no operator.key.
        let fake_home = dir.path().join("fake-home");
        let fake_xdg = dir.path().join("fake-xdg-config");
        std::fs::create_dir_all(&fake_home).unwrap();
        std::fs::create_dir_all(&fake_xdg).unwrap();
        // SAFETY: env mutation is serialized by `HOME_ENV_LOCK` above.
        unsafe {
            std::env::set_var("HOME", &fake_home);
            std::env::set_var("XDG_CONFIG_HOME", &fake_xdg);
        }

        // Arrange a Rule so `sign_seed_rules` has work to do; the load
        // path fails before any row is touched, but having a row makes
        // the test failure mode obvious if the third branch ever
        // silently succeeds against the real `$HOME`.
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        rules_store::insert(
            &conn,
            &Rule {
                id: "R-827".into(),
                kind: "bash".into(),
                matcher: r#"{"command_regex":"^x"}"#.into(),
                severity: "refuse".into(),
                reason: "t".into(),
                namespace: "_global".into(),
                created_by: "test".into(),
                created_at: 0,
                enabled: true,
                signature: None,
                attest_level: "unsigned".into(),
            },
        )
        .unwrap();
        drop(conn);

        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::SignSeed {
                key: None, // load-bearing: NO explicit --key.
                db: None,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let result = run(&db_path, args, true, &mut out);

        // Restore env BEFORE assertions so we don't leak state on
        // assertion panic.
        // SAFETY: env mutation is serialized by `HOME_ENV_LOCK` above.
        unsafe {
            match prev_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
            match prev_xdg {
                Some(v) => std::env::set_var("XDG_CONFIG_HOME", v),
                None => std::env::remove_var("XDG_CONFIG_HOME"),
            }
        }

        let err = result
            .expect_err("#827: third branch must Err, not silently succeed against real $HOME");
        let msg = format!("{err:#}");
        // The error chain must cite the legacy `operator.key` path —
        // that is the literal value `resolve_operator_key_path(None)`
        // returns (`<config>/ai-memory/operator.key`). Asserting on
        // the filename is brittle-resistant: it survives moves of the
        // base path so long as the suffix discipline holds.
        assert!(
            msg.contains("operator.key"),
            "#827: error must cite the legacy operator.key fallback path; got: {msg}"
        );
        assert!(
            msg.contains("sign-seed") || msg.contains("rules.sign-seed"),
            "#827: error must surface from the sign-seed verb; got: {msg}"
        );
    }

    #[test]
    fn resolve_key_dir_returns_override() {
        let p = std::path::PathBuf::from("/some/explicit/dir");
        let out = resolve_key_dir(Some(&p)).unwrap();
        assert_eq!(out, p);
    }

    #[test]
    fn resolve_operator_key_path_returns_override() {
        let p = std::path::PathBuf::from("/custom/operator.key");
        let out = resolve_operator_key_path(Some(&p)).unwrap();
        assert_eq!(out, p);
    }

    #[test]
    fn resolve_operator_key_path_default_includes_ai_memory() {
        let p = resolve_operator_key_path(None).unwrap();
        let s = p.display().to_string();
        assert!(
            s.contains("ai-memory"),
            "default path missing ai-memory: {s}"
        );
        assert!(s.ends_with("operator.key"), "got: {s}");
    }

    #[test]
    fn resolve_keygen_out_path_explicit_out_wins_1610() {
        let out = std::path::PathBuf::from("/custom/operator.key");
        let kd = std::path::PathBuf::from("/etc/ai-memory/keys");
        let r = resolve_keygen_out_path(Some(&out), &kd, true).unwrap();
        assert_eq!(r, out, "--out must win over a key-dir override");
    }

    #[test]
    fn resolve_keygen_out_path_overridden_key_dir_wins_1610() {
        // The F1 split-brain: keygen must write into the SAME dir the
        // --sign verbs read when the operator relocated the key store.
        let kd = std::path::PathBuf::from("/etc/ai-memory/keys");
        let r = resolve_keygen_out_path(None, &kd, true).unwrap();
        assert_eq!(r, kd.join(OPERATOR_KEY_FILENAME));
    }

    #[test]
    fn resolve_keygen_out_path_no_override_falls_back_to_legacy_singleton_1610() {
        let kd = std::path::PathBuf::from("/ignored/keys");
        let r = resolve_keygen_out_path(None, &kd, false).unwrap();
        let s = r.display().to_string();
        assert!(s.contains("ai-memory"), "legacy singleton path: {s}");
        assert!(
            !s.starts_with("/ignored"),
            "must NOT use key_dir when no override is in force: {s}"
        );
    }

    #[test]
    fn emit_ok_human_format_emits_pretty_json() {
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let payload = serde_json::json!({"foo":"bar","n":1});
        emit_ok(false, &mut out, "test.verb", &payload).unwrap();
        let s = String::from_utf8(stdout).unwrap();
        // Pretty-print includes newlines + 2-space indent.
        assert!(s.contains("\"foo\": \"bar\""), "got: {s}");
        assert!(s.contains("\n"), "pretty must include newlines: {s}");
    }

    #[test]
    fn emit_ok_json_format_envelopes_under_verb() {
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let payload = serde_json::json!({"x":1});
        emit_ok(true, &mut out, "test.verb", &payload).unwrap();
        let s = String::from_utf8(stdout).unwrap();
        assert!(s.contains("\"verb\":\"test.verb\""), "got: {s}");
        assert!(s.contains("\"result\":{\"x\":1}"), "got: {s}");
    }

    #[test]
    fn resolve_agent_id_returns_non_empty() {
        // The fn falls back to `anonymous:pid-<N>` if identity
        // resolution fails — never returns an empty string.
        let id = resolve_agent_id();
        assert!(!id.is_empty());
    }

    #[cfg(unix)]
    #[test]
    fn sign_seed_rules_is_idempotent() {
        let tdir = tempfile::tempdir().unwrap();
        let key_path = tdir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).unwrap();

        let conn = fresh_rules_conn();
        rules_store::insert(
            &conn,
            &Rule {
                id: "R001".into(),
                kind: "filesystem_write".into(),
                matcher: r#"{"glob":"/tmp/**"}"#.into(),
                severity: "refuse".into(),
                reason: "t".into(),
                namespace: "_global".into(),
                created_by: "system:seed".into(),
                created_at: 0,
                enabled: false,
                signature: None,
                attest_level: "unsigned".into(),
            },
        )
        .unwrap();

        // First call: signs 1 row.
        let signed1 = sign_seed_rules(&conn, Some(&key_path), true, &mut out).unwrap();
        assert_eq!(signed1, 1);
        let sig_after_first = rules_store::get(&conn, "R001").unwrap().unwrap().signature;

        // Second call: no-op because the canonical bytes + key are the
        // same so the computed signature matches the stored one.
        let signed2 = sign_seed_rules(&conn, Some(&key_path), true, &mut out).unwrap();
        assert_eq!(signed2, 0);
        let sig_after_second = rules_store::get(&conn, "R001").unwrap().unwrap().signature;
        assert_eq!(
            sig_after_first, sig_after_second,
            "idempotent sign-seed must preserve the existing signature bytes"
        );
    }

    /// Coverage restoration (post-#1558 floor dip): drive the FULL
    /// `run()` Add path with a `command_regex`-only matcher so the
    /// SEC-12 deprecation-warn branch executes, the operator-key
    /// load + sign path runs, and the rule lands in the store.
    #[cfg(unix)]
    #[test]
    fn rules_add_command_regex_only_fires_deprecation_branch_and_lands_rule() {
        let _g = forensic_lock();
        let tdir = tempfile::tempdir().unwrap();
        let key_path = tdir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).unwrap();

        // Full migration ladder so governance_rules (v30) exists at
        // the path `run()` opens.
        let db_path = tdir.path().join("rules.db");
        drop(crate::storage::open(&db_path).expect("init schema"));

        let args = RulesArgs {
            key_dir: Some(tdir.path().to_path_buf()),
            action: RulesAction::Add {
                id: "R900-cov".into(),
                kind: "bash".into(),
                matcher: r#"{"command_regex":"rm -rf"}"#.into(),
                severity: "refuse".into(),
                reason: "coverage: deprecated-field branch".into(),
                namespace: crate::quotas::GLOBAL_NAMESPACE.into(),
                disabled: false,
                sign: true,
            },
        };
        run(&db_path, args, false, &mut out).expect("rules add --sign");

        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let rule = rules_store::get(&conn, "R900-cov")
            .unwrap()
            .expect("rule landed");
        assert!(
            rule.signature.is_some(),
            "rules add --sign must store a signature"
        );
        assert_eq!(rule.namespace, crate::quotas::GLOBAL_NAMESPACE);
    }

    /// Coverage restoration: the clap `default_value =
    /// crate::quotas::GLOBAL_NAMESPACE` expansion on `--namespace`
    /// (today's #1558 batch-1 routing) only executes through a real
    /// parse — direct enum construction skips it.
    #[test]
    fn rules_add_namespace_clap_default_is_global() {
        use clap::Parser;
        #[derive(Parser)]
        struct Harness {
            #[command(flatten)]
            rules: RulesArgs,
        }
        let h = Harness::try_parse_from([
            "harness",
            "add",
            "--id",
            "RX",
            "--kind",
            "bash",
            "--matcher",
            "{}",
            "--reason",
            "cov",
            "--sign",
        ])
        .expect("parse");
        match h.rules.action {
            RulesAction::Add { namespace, .. } => {
                assert_eq!(namespace, crate::quotas::GLOBAL_NAMESPACE);
            }
            _ => panic!("expected Add"),
        }
    }

    // -----------------------------------------------------------------
    // GA-drive 2026-06-09 (per-module floor 95%) — targeted coverage
    // for the remaining uncovered branches: the keygen-layout parent-
    // dir fallback (#800 Gap #6, layout 3), the layout-2 tampered-pub
    // refusals, the `sign-seed --db` open failure, the keygen error
    // branches, and the no-key-anywhere refusal.
    // -----------------------------------------------------------------

    /// Writer that always fails — drives the `?` error branch on the
    /// keygen `writeln!` sites (broken-pipe propagation contract).
    struct FailingWriter;
    impl std::io::Write for FailingWriter {
        fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
            Err(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "test writer: broken pipe",
            ))
        }
        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    #[cfg(unix)]
    #[test]
    fn run_rules_sign_seed_db_override_open_failure_errors() {
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        // A db path whose parent directory does not exist — the
        // subcommand-level `--db` reopen must fail with the
        // `rules.sign-seed: open db at` context.
        let bad_db = _dir.path().join("no-such-dir").join("x.db");
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::SignSeed {
                key: None,
                db: Some(bad_db),
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, true, &mut out).expect_err("open must fail");
        let msg = format!("{err:#}");
        assert!(msg.contains("rules.sign-seed: open db"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn keygen_create_parent_dir_failure_errors() {
        // The `--out` parent path component is a REGULAR FILE, so
        // create_dir_all fails (ENOTDIR) and the with_context label
        // names the parent dir.
        let dir = tempfile::tempdir().unwrap();
        let blocker = dir.path().join("blocker");
        std::fs::write(&blocker, b"i am a file").unwrap();
        let key_path = blocker.join("sub").join("op.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = keygen_operator(&key_path, false, &mut out).unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("create parent dir"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn keygen_force_warning_broken_pipe_propagates() {
        // Existing key + --force: the stderr WARNING writeln must
        // propagate a write failure instead of panicking.
        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).expect("first keygen");

        let mut failing = FailingWriter;
        let mut stdout2: Vec<u8> = Vec::new();
        let mut out2 = CliOutput {
            stdout: &mut stdout2,
            stderr: &mut failing,
        };
        let res = keygen_operator(&key_path, true, &mut out2);
        assert!(res.is_err(), "stderr write failure must propagate");
    }

    #[cfg(unix)]
    #[test]
    fn keygen_success_line_broken_pipe_propagates() {
        // The final fingerprint writeln to stdout fails — keys are on
        // disk but the handler must surface the I/O error.
        let dir = tempfile::tempdir().unwrap();
        let key_path = dir.path().join("operator.key");
        let mut failing = FailingWriter;
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut failing,
            stderr: &mut stderr,
        };
        let res = keygen_operator(&key_path, false, &mut out);
        assert!(res.is_err(), "stdout write failure must propagate");
        assert!(key_path.exists(), "key material still lands on disk");
    }

    #[cfg(unix)]
    #[test]
    fn sign_seed_update_signature_failure_propagates() {
        // An abort trigger on governance_rules makes the UPDATE fail
        // so sign_seed_rules' update_signature `?` branch executes.
        let tdir = tempfile::tempdir().unwrap();
        let key_path = tdir.path().join("operator.key");
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_path, false, &mut out).unwrap();

        let conn = fresh_rules_conn();
        rules_store::insert(
            &conn,
            &Rule {
                id: "R-fail-upd".into(),
                kind: "bash".into(),
                matcher: r#"{"command_substring":"x"}"#.into(),
                severity: "refuse".into(),
                reason: "t".into(),
                namespace: "_global".into(),
                created_by: "test".into(),
                created_at: 0,
                enabled: false,
                signature: None,
                attest_level: "unsigned".into(),
            },
        )
        .unwrap();
        conn.execute_batch(
            "CREATE TRIGGER test_fail_sig_update BEFORE UPDATE ON governance_rules \
             BEGIN SELECT RAISE(ABORT, 'test trigger: signature update refused'); END;",
        )
        .unwrap();
        let err = sign_seed_rules(&conn, Some(&key_path), true, &mut out).unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("signature update refused"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn mutation_verb_legacy_layout_load_failure_cites_key_dir() {
        use std::os::unix::fs::PermissionsExt;
        // operator.priv + operator.pub both present (layout 1 entry
        // condition) but the priv mode bits are insecure → kp::load
        // refuses and the with_context label names both files.
        let (_dir, db_path, key_dir) = fresh_env_with_operator_key();
        let priv_path = key_dir.join("operator.priv");
        std::fs::set_permissions(&priv_path, std::fs::Permissions::from_mode(0o644)).unwrap();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Enable {
                id: "R-any".into(),
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        let err = run(&db_path, args, false, &mut out).expect_err("must refuse");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("failed loading operator.priv/operator.pub"),
            "got: {msg}"
        );
        // Restore so tempdir cleanup works.
        std::fs::set_permissions(&priv_path, std::fs::Permissions::from_mode(0o600)).unwrap();
    }

    #[test]
    fn list_on_fresh_unmigrated_db_succeeds() {
        // Regression (do-1461 A2A run, 2026-06-15): `ai-memory rules list`
        // against a FRESH, never-migrated db must migrate-on-open and
        // succeed — NOT fail with `rules_store::list: prepare — no such
        // table: governance_rules`. Pre-fix the rules CLI used a raw
        // `rusqlite::Connection::open` that skipped migrations, which broke
        // Form-7 governance bootstrap on fresh fleet peers (especially
        // postgres-backed ones) where the daemon — which would have
        // migrated the local sqlite — has not started yet.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("fresh-never-migrated.db");
        assert!(!db_path.exists(), "db must not exist before the rules call");
        let args = RulesArgs {
            key_dir: None,
            action: RulesAction::List,
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, true, &mut out).expect("rules list must succeed on a fresh db");
        // Migrations created + seeded governance_rules → the list verb prints
        // the seed rules (R001..R004) rather than erroring on a missing table.
        let s = String::from_utf8(stdout).unwrap();
        assert!(
            s.contains("\"verb\":\"rules.list\"") && s.contains("R001"),
            "expected the seeded rules from the migrated fresh db, got: {s}"
        );
    }

    /// Set up a key_dir holding the layout-2 singleton-file pair
    /// (`operator.key` + `operator.key.pub`) and an initialized DB.
    /// Returns (tempdir guard, db_path, key_dir).
    #[cfg(unix)]
    fn fresh_env_with_keygen_layout() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf)
    {
        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("ai-memory.db");
        drop(crate::db::open(&db_path).expect("db::open"));
        let key_dir = dir.path().join("keys-l2");
        std::fs::create_dir_all(&key_dir).expect("mkdir");
        let key_file = key_dir.join(OPERATOR_KEY_FILENAME);
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_file, false, &mut out).expect("keygen");
        (dir, db_path, key_dir)
    }

    /// Drive a mutation verb (`enable --sign`) so the key load runs;
    /// returns the error (the rule id never resolves — every test
    /// using this asserts on the key-load refusal that fires first).
    #[cfg(unix)]
    fn enable_err_with_key_dir(db_path: &Path, key_dir: std::path::PathBuf) -> anyhow::Error {
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Enable {
                id: "R-never".into(),
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(db_path, args, false, &mut out).expect_err("must error")
    }

    #[cfg(unix)]
    #[test]
    fn keygen_layout_pub_not_base64_refused() {
        let (_dir, db_path, key_dir) = fresh_env_with_keygen_layout();
        std::fs::write(key_dir.join("operator.key.pub"), "!!!not-base64!!!").unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("decode base64url public key"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn keygen_layout_pub_wrong_length_refused() {
        use base64::Engine;
        let (_dir, db_path, key_dir) = fresh_env_with_keygen_layout();
        let short = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([7u8; 16]);
        std::fs::write(key_dir.join("operator.key.pub"), short).unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("decoded to 16 bytes"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn keygen_layout_pub_mismatch_refused() {
        use base64::Engine;
        let (_dir, db_path, key_dir) = fresh_env_with_keygen_layout();
        // A syntactically valid but WRONG 32-byte public key.
        let other = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([9u8; 32]);
        std::fs::write(key_dir.join("operator.key.pub"), other).unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("does not match public key"), "got: {msg}");
    }

    /// Set up the layout-3 shape: the keygen pair lives in the PARENT
    /// of the key_dir (`resolve_operator_key_path` singleton rationale)
    /// while the key_dir itself is an empty `keys/` subdir.
    #[cfg(unix)]
    fn fresh_env_with_parent_keygen_layout()
    -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("ai-memory.db");
        drop(crate::db::open(&db_path).expect("db::open"));
        let key_file = dir.path().join(OPERATOR_KEY_FILENAME);
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        keygen_operator(&key_file, false, &mut out).expect("keygen");
        let key_dir = dir.path().join("keys");
        std::fs::create_dir_all(&key_dir).expect("mkdir keys");
        (dir, db_path, key_dir)
    }

    #[cfg(unix)]
    #[test]
    fn parent_dir_keygen_fallback_signs_mutation_verbs() {
        // #800 Gap #6 — a fresh `rules keygen` (parent-dir layout) +
        // immediate `rules add --sign` must just work.
        let (_dir, db_path, key_dir) = fresh_env_with_parent_keygen_layout();
        let args = RulesArgs {
            key_dir: Some(key_dir),
            action: RulesAction::Add {
                id: "R-l3".into(),
                kind: "bash".into(),
                matcher: r#"{"command_substring":"halt"}"#.into(),
                severity: "refuse".into(),
                reason: "layout-3 coverage".into(),
                namespace: "_global".into(),
                disabled: false,
                sign: true,
            },
        };
        let mut stdout: Vec<u8> = Vec::new();
        let mut stderr: Vec<u8> = Vec::new();
        let mut out = CliOutput {
            stdout: &mut stdout,
            stderr: &mut stderr,
        };
        run(&db_path, args, false, &mut out).expect("layout-3 add --sign");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let r = rules_store::get(&conn, "R-l3")
            .unwrap()
            .expect("rule landed");
        assert_eq!(r.attest_level, OPERATOR_SIGNED_LEVEL);
        assert!(r.signature.is_some());
    }

    #[cfg(unix)]
    #[test]
    fn parent_dir_keygen_fallback_pub_wrong_length_refused() {
        use base64::Engine;
        let (dir, db_path, key_dir) = fresh_env_with_parent_keygen_layout();
        let short = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([3u8; 8]);
        std::fs::write(dir.path().join("operator.key.pub"), short).unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("decoded to 8 bytes"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn parent_dir_keygen_fallback_pub_mismatch_refused() {
        use base64::Engine;
        let (dir, db_path, key_dir) = fresh_env_with_parent_keygen_layout();
        let other = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([4u8; 32]);
        std::fs::write(dir.path().join("operator.key.pub"), other).unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("does not match public key"), "got: {msg}");
    }

    #[cfg(unix)]
    #[test]
    fn no_operator_key_anywhere_names_all_layouts() {
        // key_dir AND its parent hold no key material — the unified
        // refusal must name every accepted layout so the operator can
        // pick one to materialise.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("ai-memory.db");
        drop(crate::db::open(&db_path).expect("db::open"));
        let key_dir = dir.path().join("empty-parent").join("empty-keys");
        std::fs::create_dir_all(&key_dir).unwrap();
        let err = enable_err_with_key_dir(&db_path, key_dir);
        let msg = format!("{err:#}");
        assert!(msg.contains("no operator key found"), "got: {msg}");
        assert!(msg.contains("operator.priv"), "got: {msg}");
        assert!(msg.contains("rules keygen"), "got: {msg}");
    }
}