lean-ctx 3.12.8

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! The layered policy editor (rel12 WP6) as Local Read kinds: Cloud >
//! project > personal, tighten-only.
//!
//! - `policies.layers`: `lean-ctx policy layers --json --project-root P`,
//!   every rule of the three layers with its origin, lock state and whether
//!   it can be edited, the Cloud policy's state, and each editable layer's
//!   revision (the digest a change names).
//! - `policies.changed`: `lean-ctx policy change --json --project-root P
//!   --level project|personal --expected-revision R` with `{op, rule}` on
//!   stdin (or `--op`/`--rule`); POST on the socket. One rule is added,
//!   edited or removed when the change only tightens what a higher layer
//!   sets; otherwise the document names the typed refusal and nothing is
//!   written. Each write appends its evidence to the local policy history
//!   and to the audit trail; the release receipt of the write rides the
//!   existing receipt outbox to Cloud (content-free, when uploads are allowed).
//! - `policies.history`: `lean-ctx policy history --json --project-root P
//!   [--limit N]`, the recorded changes of the project and of the personal
//!   layer, newest first.
//!
//! The Cloud layer is the installed Cloud policy floor
//! ([`lean_ctx_cloud::effective_floor`], same read and check as enforcement):
//! read only, every rule locked. The project layer is the project's
//! `.lean-ctx/policies.json` (written through PathJail). The personal layer
//! is `<data_dir>/policy-layers/personal.json`, free for everyone (paid
//! features are Cloud rules, team and audit); it is shown and stored, not
//! yet enforced by the engine.
//!
//! `policies.layers` and `policies.history` are functions of the stored data
//! (and of whether the stored Cloud policy is still in force): they generate
//! no timestamp. A `policies.changed` write result carries the time of its
//! evidence (`evidence.at`).
//!
//! A change holds one lock (`<data_dir>/policy-layers/.lock`) across reading
//! the layer, comparing its revision, recording the evidence (audit trail,
//! then history) and replacing the layer file: two writers naming the same
//! revision never both apply, and evidence is recorded before the change.

use std::path::{Path, PathBuf};

use lean_ctx_time::UtcDateTime;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use sha2::{Digest, Sha256};

use lean_ctx::core::context_policies::{ContextPolicy, PolicyAction, PolicySet, PolicySource};
use lean_ctx_cloud::effective_floor;
use lean_ctx_protocol::cloud::effective_policy::EffectivePolicyV1;

use super::policies::policy_row;
use super::routes::{Method, Param, Project, Route, Shape};
use super::{Answer, Code, Failure, Via};

/// The tool run these documents are released under.
const TOOL: &str = "ctx_policy";

const PROJECT_ROOT: Param = Param {
    name: "project_root",
    flag: "--project-root",
    shape: Shape::Value,
};
const LEVEL: Param = Param {
    name: "level",
    flag: "--level",
    shape: Shape::Value,
};
const EXPECTED_REVISION: Param = Param {
    name: "expected_revision",
    flag: "--expected-revision",
    shape: Shape::Value,
};
const OP: Param = Param {
    name: "op",
    flag: "--op",
    shape: Shape::Value,
};
const RULE: Param = Param {
    name: "rule",
    flag: "--rule",
    shape: Shape::Json,
};
const LIMIT: Param = Param {
    name: "limit",
    flag: "--limit",
    shape: Shape::Value,
};

/// This part's routes (Route as in `routes.rs`).
pub(crate) const ROUTES: &[Route] = &[
    Route {
        kind: "policies.layers",
        method: Method::Get,
        path: "/v1/local/policies/layers",
        cli: &["policy", "layers", "--json"],
        action: &["layers"],
        params: &[PROJECT_ROOT],
        project: Project::Required,
        names: None,
    },
    Route {
        kind: "policies.changed",
        method: Method::Post,
        path: "/v1/local/policies/change",
        cli: &["policy", "change", "--json"],
        action: &["change"],
        params: &[PROJECT_ROOT, LEVEL, EXPECTED_REVISION, OP, RULE],
        project: Project::Required,
        names: None,
    },
    Route {
        kind: "policies.history",
        method: Method::Get,
        path: "/v1/local/policies/history",
        cli: &["policy", "history", "--json"],
        action: &["history"],
        params: &[PROJECT_ROOT, LIMIT],
        project: Project::Required,
        names: None,
    },
];

/// This part's document kinds (sorted).
pub(crate) const KINDS: &[&str] = &["policies.changed", "policies.history", "policies.layers"];

/// Command path → CLI verbs, as `capabilities::COMMANDS`.
pub(crate) const COMMANDS: &[(&str, &[&str])] = &[("policy", &["change", "history", "layers"])];

/// The project layer's file below the project's `.lean-ctx`.
const PROJECT_FILE: &str = "policies.json";
/// The personal layer and the change history, below the data directory.
const LAYERS_DIR: &str = "policy-layers";
const PERSONAL_FILE: &str = "personal.json";
const HISTORY_FILE: &str = "history.jsonl";
/// The lock a change holds while it reads, records and writes.
const LOCK_FILE: &str = ".lock";
/// Entries the history keeps (oldest dropped first).
const MAX_HISTORY: usize = 1000;
/// The largest change body read from stdin.
const MAX_BODY: usize = 64 * 1024;

/// What the documents are built from (the data directories and the clock
/// that decides whether the stored Cloud policy is still in force).
pub(crate) struct Ctx<'a> {
    pub project_root: &'a Path,
    /// `<data_dir>/cloud` (the Cloud store), when there is one.
    pub cloud_data: Option<&'a Path>,
    /// The LeanCTX data directory (personal layer, history).
    pub data_dir: &'a Path,
    pub now: UtcDateTime,
}

// ------------------------------------------------------------------- layers

/// Where a rule comes from.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub(crate) struct Origin {
    /// `org` | `vault` (Cloud) | `project` | `default` (built-in, while the
    /// project has no policy file) | `security_default` | `personal`.
    pub level: &'static str,
    /// The vault whose Cloud policy applies to this project, for `vault`.
    pub vault: Option<String>,
    /// The Cloud policy version.
    pub version: Option<u64>,
    /// The key the Cloud policy was signed with, when recorded.
    pub key_id: Option<String>,
    pub issued_at: Option<String>,
}

impl Origin {
    fn local(level: &'static str) -> Self {
        Self {
            level,
            vault: None,
            version: None,
            key_id: None,
            issued_at: None,
        }
    }
}

/// One rule of a layer.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct RuleRow {
    /// `cloud:<member>` / `cloud:<member>[i]`, `security:<name>`,
    /// `project:<name>` or `personal:<name>`.
    pub id: String,
    pub name: String,
    /// The path glob, or `null` for a rule that matches no path.
    #[serde(rename = "match")]
    pub match_pattern: Option<String>,
    /// A context-policy action (`exclude`, `include`, `pin`, `set_view`,
    /// `max_tokens`, `mark_outdated`, `redact`, `audit`) or, for Cloud,
    /// `exclude` | `never_send` | `restrict_roots` | `block_secrets` |
    /// `filter` | `allow_tools` | `deny_tools` | `max_tokens`.
    pub action: String,
    pub action_params: Value,
    pub condition: Option<String>,
    pub condition_params: Value,
    pub reason: Option<String>,
    /// `cloud` | `project` | `personal`.
    pub layer: &'static str,
    /// No layer below may change it (Cloud rules, security defaults).
    pub locked: bool,
    /// A `policies.changed` request may change it now.
    pub editable: bool,
    pub origin: Origin,
}

/// The Cloud layer: the installed Cloud policy as this Instance checks it.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct CloudView {
    /// `in_force` | `offline_last_known` (past `not_after`: the last valid
    /// policy still restricts) | `none` | `invalid` (damaged: fails closed).
    pub state: &'static str,
    /// `verified` | `invalid` | `none`.
    pub signature: String,
    pub version: Option<u64>,
    pub digest: Option<String>,
    pub key_id: Option<String>,
    pub issued_at: Option<String>,
    pub not_after: Option<String>,
    /// When this Instance last pulled the policy from Cloud.
    pub last_pull_at: Option<String>,
    /// The vault whose entry applies to this project, else `null` (default).
    pub vault: Option<String>,
    /// The path globs the Cloud rules cover (`excluded_paths` and
    /// `never_send`), sorted.
    pub covered: Vec<String>,
    /// The coverage cannot be bounded (damaged policy): every path counts.
    pub covers_everything: bool,
    pub rules: Vec<RuleRow>,
    /// Why the installed policy could not be read, else `null`.
    pub error: Option<String>,
}

/// An editable layer (project or personal).
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct LayerDoc {
    /// `project` | `personal`.
    pub level: &'static str,
    pub file: String,
    pub exists: bool,
    /// The file exists but is no policy file (or cannot be read safely):
    /// changes are refused until it is fixed.
    pub damaged: bool,
    /// `sha256:<hex>` of the file's bytes (of no bytes when absent): the
    /// `expected_revision` of a change; `null` when the file cannot be read.
    pub revision: Option<String>,
    /// Changes are accepted now.
    pub editable: bool,
    /// The engine applies the layer's rules.
    pub enforced: bool,
    pub rules: Vec<RuleRow>,
    pub error: Option<String>,
}

/// The `policies.layers` body.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct LayersDoc {
    pub project_root: String,
    /// Highest first: a lower layer may only tighten a higher one.
    pub precedence: [&'static str; 3],
    pub cloud: CloudView,
    pub project: LayerDoc,
    pub personal: LayerDoc,
}

/// `sha256:<hex>` of `bytes`.
fn digest(bytes: &[u8]) -> String {
    format!("sha256:{}", hex::encode(Sha256::digest(bytes)))
}

/// `path` canonical when it resolves, else as given.
fn canonical(path: &Path) -> PathBuf {
    std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}

fn empty_cloud(state: &'static str, signature: &str, error: Option<String>) -> CloudView {
    CloudView {
        state,
        signature: signature.to_owned(),
        version: None,
        digest: None,
        key_id: None,
        issued_at: None,
        not_after: None,
        last_pull_at: None,
        vault: None,
        covered: Vec::new(),
        covers_everything: state == "invalid",
        rules: Vec::new(),
        error,
    }
}

/// The Cloud policy members as locked rows.
fn cloud_rows(policy: &EffectivePolicyV1, origin: &Origin) -> Vec<RuleRow> {
    let mut rows = Vec::new();
    let mut push = |id: String, name: &str, glob: Option<&String>, action: &str, params: Value| {
        rows.push(RuleRow {
            id,
            name: name.to_owned(),
            match_pattern: glob.cloned(),
            action: action.to_owned(),
            action_params: params,
            condition: None,
            condition_params: json!({}),
            reason: None,
            layer: "cloud",
            locked: true,
            editable: false,
            origin: origin.clone(),
        });
    };
    let security = &policy.security;
    let context = &policy.context;
    for (member, action, globs) in [
        ("excluded_paths", "exclude", &security.excluded_paths),
        ("never_send", "never_send", &security.never_send),
    ] {
        for (i, glob) in globs.iter().flatten().enumerate() {
            push(
                format!("cloud:{member}[{i}]"),
                &format!("security.{member}"),
                Some(glob),
                action,
                json!({}),
            );
        }
    }
    if let Some(roots) = &security.readable_roots {
        push(
            "cloud:readable_roots".into(),
            "security.readable_roots",
            None,
            "restrict_roots",
            json!({ "roots": roots }),
        );
    }
    if let Some(enabled) = security.block_secrets {
        push(
            "cloud:block_secrets".into(),
            "security.block_secrets",
            None,
            "block_secrets",
            json!({ "enabled": enabled }),
        );
    }
    for (name, action) in [
        ("security.pii", security.pii),
        ("security.classification", security.classification),
        ("context.injection_filter", context.injection_filter),
    ] {
        if let Some(action) = action {
            let member = name.split('.').nth(1).unwrap_or(name);
            push(
                format!("cloud:{member}"),
                name,
                None,
                "filter",
                json!({ "action": action.as_str() }),
            );
        }
    }
    for (member, action, tools) in [
        ("allowed_tools", "allow_tools", &context.allowed_tools),
        ("denied_tools", "deny_tools", &context.denied_tools),
    ] {
        if let Some(tools) = tools {
            push(
                format!("cloud:{member}"),
                &format!("context.{member}"),
                None,
                action,
                json!({ "tools": tools }),
            );
        }
    }
    if let Some(limit) = context.max_context_tokens {
        push(
            "cloud:max_context_tokens".into(),
            "context.max_context_tokens",
            None,
            "max_tokens",
            json!({ "limit": limit }),
        );
    }
    rows
}

/// The Cloud layer for `project_root`: the installed floor, read and
/// checked exactly as enforcement does.
pub(crate) fn cloud_view(
    cloud_data: Option<&Path>,
    project_root: &Path,
    now: UtcDateTime,
) -> CloudView {
    let Some(cloud) = cloud_data else {
        return empty_cloud("none", "none", None);
    };
    let last_pull_at = lean_ctx_cloud::policy::load_pulls(cloud).last_pull_at;
    let checked = effective_floor::read(&effective_floor::floor_dir_in(cloud)).and_then(
        |(floor, required)| {
            effective_floor::check(floor.as_deref(), required.as_deref()).map(|f| (f, floor))
        },
    );
    let (floor, text) = match checked {
        Ok((Some(floor), text)) => (floor, text),
        Ok((None, _)) => {
            return CloudView {
                last_pull_at,
                ..empty_cloud("none", "none", None)
            };
        }
        Err(error) => {
            return CloudView {
                last_pull_at,
                ..empty_cloud("invalid", "invalid", Some(error))
            };
        }
    };
    let installed = lean_ctx_cloud::policy_sessions::installed(cloud, now);
    let state = if installed.state == "in_force" {
        "in_force"
    } else {
        "offline_last_known"
    };
    let vault = text
        .as_deref()
        .and_then(|text| effective_floor::vault_for(text, cloud, project_root));
    let effective = lean_ctx_cloud::policy::load_state(cloud)
        .effective
        .filter(|e| e.version == floor.version && e.digest == floor.digest);
    let origin = Origin {
        level: if vault.is_some() { "vault" } else { "org" },
        vault: vault.clone(),
        version: Some(floor.version),
        key_id: effective.as_ref().map(|e| e.key_id.clone()),
        issued_at: effective.as_ref().map(|e| e.issued_at.clone()),
    };
    let policy = floor.policy.for_vault(vault.as_deref());
    let mut covered: Vec<String> = policy
        .security
        .excluded_paths
        .iter()
        .chain(policy.security.never_send.iter())
        .flatten()
        .cloned()
        .collect();
    covered.sort();
    covered.dedup();
    CloudView {
        state,
        signature: installed.signature,
        version: Some(floor.version),
        digest: Some(floor.digest.clone()),
        key_id: origin.key_id.clone(),
        issued_at: origin.issued_at.clone(),
        not_after: Some(floor.not_after.clone()),
        last_pull_at,
        vault,
        covered,
        covers_everything: false,
        rules: cloud_rows(policy, &origin),
        error: None,
    }
}

/// One local rule as a row of `layer`.
fn local_row(
    policy: &ContextPolicy,
    source: PolicySource,
    layer: &'static str,
    layer_editable: bool,
) -> RuleRow {
    let row = policy_row(policy, source);
    let (prefix, origin, locked) = match (layer, source) {
        (_, PolicySource::SecurityDefault) => ("security", "security_default", true),
        ("personal", _) => ("personal", "personal", false),
        (_, PolicySource::Default) => ("project", "default", false),
        _ => ("project", "project", false),
    };
    RuleRow {
        id: format!("{prefix}:{}", row.name),
        name: row.name,
        match_pattern: Some(row.match_pattern),
        action: row.action.to_owned(),
        action_params: row.action_params,
        condition: row.condition.map(str::to_owned),
        condition_params: row.condition_params,
        reason: row.reason,
        layer,
        locked,
        editable: layer_editable && !locked,
        origin: Origin::local(origin),
    }
}

/// A layer file as stored: its bytes (absent: `None`) or why it cannot be
/// read safely.
struct Stored {
    file: PathBuf,
    bytes: Result<Option<Vec<u8>>, String>,
}

impl Stored {
    /// The digest of the stored bytes (absent: of no bytes); `None` when the
    /// file cannot be read.
    fn revision(&self) -> Option<String> {
        match &self.bytes {
            Ok(Some(bytes)) => Some(digest(bytes)),
            Ok(None) => Some(digest(b"")),
            Err(_) => None,
        }
    }

    /// The policies of the file: `Ok(None)` when absent, `Err` when damaged.
    fn policies(&self) -> Result<Option<PolicySet>, String> {
        match &self.bytes {
            Ok(None) => Ok(None),
            Ok(Some(bytes)) => serde_json::from_slice::<PolicySet>(bytes)
                .map(Some)
                .map_err(|_| format!("{} is not a policy file", self.file.display())),
            Err(error) => Err(error.clone()),
        }
    }
}

fn read_optional(path: &Path) -> Result<Option<Vec<u8>>, String> {
    match std::fs::read(path) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(format!("{} is unreadable: {e}", path.display())),
    }
}

/// The project layer's file, read only through PathJail (a symlinked
/// `.lean-ctx` or file is refused, never followed).
fn stored_project(root: &Path) -> Stored {
    let file = root.join(".lean-ctx").join(PROJECT_FILE);
    let bytes =
        lean_ctx::core::pathjail_write::project_data_destination(root, Path::new(PROJECT_FILE))
            .and_then(|path| read_optional(&path));
    Stored { file, bytes }
}

fn stored_personal(data_dir: &Path) -> Stored {
    let file = data_dir.join(LAYERS_DIR).join(PERSONAL_FILE);
    let bytes = read_optional(&file);
    Stored { file, bytes }
}

/// The project layer's policies in effect order, built from the jailed bytes
/// only (as `PolicySet::load_project_sourced` orders them): the security
/// defaults, then the project file's policies, or the built-in defaults
/// without a usable file. A file that cannot be read safely shows only the
/// security defaults: nothing outside the project is ever shown.
fn project_sourced(root: &Path, stored: &Stored) -> Vec<(ContextPolicy, PolicySource)> {
    let tagged =
        |set: PolicySet, source: PolicySource| set.policies.into_iter().map(move |p| (p, source));
    let mut out: Vec<(ContextPolicy, PolicySource)> = tagged(
        PolicySet::security_defaults(),
        PolicySource::SecurityDefault,
    )
    .collect();
    let Ok(bytes) = &stored.bytes else {
        return out;
    };
    let project = bytes
        .as_deref()
        .filter(|_| !lean_ctx::core::pathutil::is_data_dir_collision(root))
        .and_then(|bytes| serde_json::from_slice::<PolicySet>(bytes).ok());
    match project {
        None => out.extend(tagged(
            PolicySet::attach_compat_defaults(),
            PolicySource::Default,
        )),
        Some(project) => {
            let reserved: Vec<String> = out.iter().map(|(p, _)| p.name.clone()).collect();
            out.extend(
                project
                    .policies
                    .into_iter()
                    .filter(|p| !reserved.contains(&p.name))
                    .map(|p| (p, PolicySource::Project)),
            );
        }
    }
    out
}

fn project_layer(root: &Path) -> LayerDoc {
    let stored = stored_project(root);
    let parsed = stored.policies();
    let damaged = parsed.is_err();
    LayerDoc {
        level: "project",
        file: stored.file.display().to_string(),
        exists: matches!(stored.bytes, Ok(Some(_))),
        damaged,
        revision: stored.revision(),
        editable: !damaged,
        enforced: true,
        rules: project_sourced(root, &stored)
            .iter()
            .map(|(policy, source)| local_row(policy, *source, "project", !damaged))
            .collect(),
        error: parsed.err(),
    }
}

fn personal_layer(data_dir: &Path) -> LayerDoc {
    let stored = stored_personal(data_dir);
    let parsed = stored.policies();
    let damaged = parsed.is_err();
    let editable = !damaged;
    let rules = match &parsed {
        Ok(Some(set)) => set
            .policies
            .iter()
            .map(|policy| local_row(policy, PolicySource::Project, "personal", editable))
            .collect(),
        _ => Vec::new(),
    };
    LayerDoc {
        level: "personal",
        file: stored.file.display().to_string(),
        exists: matches!(stored.bytes, Ok(Some(_))),
        damaged,
        revision: stored.revision(),
        editable,
        enforced: false,
        rules,
        error: parsed.err(),
    }
}

/// The `policies.layers` body.
pub(crate) fn layers_doc(ctx: &Ctx<'_>) -> LayersDoc {
    LayersDoc {
        project_root: ctx.project_root.display().to_string(),
        precedence: ["cloud", "project", "personal"],
        cloud: cloud_view(ctx.cloud_data, ctx.project_root, ctx.now),
        project: project_layer(ctx.project_root),
        personal: personal_layer(ctx.data_dir),
    }
}

// ------------------------------------------------------------------- change

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Op {
    Add,
    Edit,
    Remove,
}

impl Op {
    fn as_str(self) -> &'static str {
        match self {
            Self::Add => "add",
            Self::Edit => "edit",
            Self::Remove => "remove",
        }
    }
}

/// A rule as a change names it: the row shape of `policies.layers` (its
/// other fields are ignored).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub(crate) struct RuleInput {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, rename = "match", skip_serializing_if = "Option::is_none")]
    pub match_pattern: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub action_params: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub condition: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub condition_params: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub layer: Option<String>,
}

/// One change request.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ChangeRequest {
    /// `project` | `personal` (`cloud` is accepted only to be refused).
    pub level: &'static str,
    pub op: Op,
    pub expected_revision: String,
    pub rule: RuleInput,
}

/// Why a change was not applied.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct Refusal {
    /// `policy.loosens_higher_layer` | `policy.cloud_locked` |
    /// `policy.locked` | `policy.cloud_unreachable` |
    /// `policy.revision_changed`.
    pub reason: &'static str,
    /// The layer whose rule refused it.
    pub layer: Option<&'static str>,
    /// The id of that rule.
    pub rule: Option<String>,
    pub origin: Option<Origin>,
    pub message: String,
    /// The layer's revision now (`policy.revision_changed`).
    pub current_revision: Option<String>,
}

/// What a change recorded.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub(crate) struct Evidence {
    pub at: String,
    pub actor: String,
    pub level: String,
    /// The project of a project-layer change (canonical).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_root: Option<String>,
    pub op: Op,
    pub rule_id: String,
    /// The rule as added or edited (removed: as it was).
    pub rule: Value,
    pub before_digest: String,
    pub after_digest: String,
    /// The audit-trail entry of the change (`audit:<entry hash>`): recorded
    /// before the change, which is not applied without it.
    pub evidence_ref: Option<String>,
}

/// The `policies.changed` body. A refusal is a document too (`applied:
/// false`, `refused` set): nothing was written.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct ChangedDoc {
    pub project_root: String,
    pub level: &'static str,
    pub op: Op,
    pub rule_id: Option<String>,
    pub applied: bool,
    pub refused: Option<Refusal>,
    /// The layer's revision after the change (refused: as it is).
    pub revision: Option<String>,
    pub evidence: Option<Evidence>,
}

fn parse_op(raw: &str) -> Result<Op, Failure> {
    serde_json::from_value(Value::String(raw.to_owned()))
        .map_err(|_| Failure::invalid(format!("op is add, edit or remove, not {raw}")))
}

/// The change `args` (and the stdin body, without `--op`) ask for.
pub(crate) fn parse_request(
    args: &[String],
    stdin: Option<&str>,
) -> Result<ChangeRequest, Failure> {
    let level = match super::flag(args, "--level")? {
        Some("project") => "project",
        Some("personal") => "personal",
        Some("cloud") => "cloud",
        Some(other) => {
            return Err(Failure::invalid(format!(
                "--level is project or personal, not {other}"
            )));
        }
        None => return Err(Failure::invalid("--level project|personal is required")),
    };
    let expected_revision = super::flag(args, "--expected-revision")?
        .ok_or_else(|| {
            Failure::invalid("--expected-revision R is required (the layer's revision)")
        })?
        .to_owned();
    let (op, rule) = if let Some(op) = super::flag(args, "--op")? {
        let op = parse_op(op)?;
        let text = super::flag(args, "--rule")?
            .ok_or_else(|| Failure::invalid("--rule '<json>' is required with --op"))?;
        let rule: RuleInput = serde_json::from_str(text)
            .map_err(|e| Failure::invalid(format!("--rule is not a rule object: {e}")))?;
        (op, rule)
    } else {
        #[derive(Deserialize)]
        struct Body {
            op: String,
            rule: RuleInput,
        }
        let text = stdin.ok_or_else(|| {
            Failure::invalid("give --op and --rule, or the body {op, rule} on stdin")
        })?;
        let body: Body = serde_json::from_str(text)
            .map_err(|e| Failure::invalid(format!("the body is not {{op, rule}}: {e}")))?;
        (parse_op(&body.op)?, body.rule)
    };
    Ok(ChangeRequest {
        level,
        op,
        expected_revision,
        rule,
    })
}

/// The rule name a change names: `name`, else the name of its `id`.
fn target_name(rule: &RuleInput) -> Option<String> {
    rule.name.clone().filter(|n| !n.is_empty()).or_else(|| {
        rule.id
            .as_deref()
            .and_then(|id| id.split_once(':'))
            .map(|(_, name)| name.to_owned())
    })
}

fn valid_name(name: &str) -> bool {
    (1..=64).contains(&name.len())
        && name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b"_.-".contains(&b))
}

/// `{tag: params}` when `params` is a non-empty object, else `"tag"` (the
/// context-policy JSON form).
fn tagged(tag: &str, params: Option<&Value>) -> Value {
    match params {
        Some(Value::Object(map)) if !map.is_empty() => {
            let mut out = Map::new();
            out.insert(tag.to_owned(), Value::Object(map.clone()));
            Value::Object(out)
        }
        _ => Value::String(tag.to_owned()),
    }
}

/// The context policy a change adds or edits (the one policy parser:
/// `ContextPolicy`'s own deserializer).
fn to_policy(rule: &RuleInput, name: &str) -> Result<ContextPolicy, Failure> {
    if !valid_name(name) {
        return Err(Failure::invalid(
            "a rule name is 1-64 characters of A-Z a-z 0-9 _ . -",
        ));
    }
    let glob = rule
        .match_pattern
        .as_deref()
        .filter(|m| !m.is_empty() && m.len() <= 512 && !m.contains(['\n', '\r']))
        .ok_or_else(|| Failure::invalid("match is a path glob of 1-512 characters"))?;
    // The engine matches normalised project-relative paths: these globs
    // would never match, so the rule would silently do nothing.
    if glob.starts_with('/') || glob.starts_with("./") || glob.split('/').any(|s| s == "..") {
        return Err(Failure::invalid(
            "match is relative to the project root: no leading / or ./ and no .. segment",
        ));
    }
    let action = rule
        .action
        .as_deref()
        .ok_or_else(|| Failure::invalid("action is required"))?;
    let mut out = Map::new();
    out.insert("name".into(), name.into());
    out.insert("match".into(), glob.into());
    out.insert("action".into(), tagged(action, rule.action_params.as_ref()));
    if let Some(condition) = rule.condition.as_deref() {
        out.insert(
            "condition".into(),
            tagged(condition, rule.condition_params.as_ref()),
        );
    }
    if let Some(reason) = rule.reason.as_deref() {
        if reason.len() > 512 {
            return Err(Failure::invalid("reason is at most 512 characters"));
        }
        out.insert("reason".into(), reason.into());
    }
    serde_json::from_value(Value::Object(out))
        .map_err(|e| Failure::invalid(format!("not a valid rule: {e}")))
}

/// `prefix` up to its first glob metacharacter (`*`, `?`, `[`, `{`);
/// `None` when nothing literal is left.
fn literal(prefix: &str) -> Option<&str> {
    let cut = prefix
        .find(['*', '?', '[', '{'])
        .map_or(prefix, |i| &prefix[..i]);
    (!cut.is_empty()).then_some(cut)
}

/// The literal prefix every path a local glob matches starts with, as the
/// context-policy matcher reads it; `None` when the glob is not anchored
/// (it may match anywhere).
fn anchor(glob: &str) -> Option<&str> {
    if glob == "**/*" || glob.starts_with("**") {
        return None;
    }
    let prefix = if let Some(prefix) = glob.strip_suffix("/**") {
        prefix
    } else if glob.contains("**") {
        let parts: Vec<&str> = glob.split("**").collect();
        if parts.len() != 2 {
            return None;
        }
        parts[0]
    } else {
        glob.strip_suffix('*')?
    };
    literal(prefix)
}

/// The literal prefix of a Cloud glob (gitignore grammar: `excluded_paths`,
/// `never_send`). A glob that starts with `/` or `!`, or has no `/` other
/// than a trailing one (it matches at any depth), counts as unanchored.
fn cloud_anchor(glob: &str) -> Option<&str> {
    if glob.starts_with(['/', '!']) {
        return None;
    }
    let trimmed = glob.trim_end_matches('/');
    if !trimmed.contains('/') {
        return None;
    }
    literal(trimmed)
}

/// Whether two literal prefixes may lead to one path (ASCII case folded:
/// case-insensitive file systems).
fn prefixes_meet(x: &str, y: &str) -> bool {
    let (x, y) = (x.to_ascii_lowercase(), y.to_ascii_lowercase());
    x.starts_with(&y) || y.starts_with(&x)
}

/// Whether some path may match both local globs (conservative: unanchored
/// globs overlap everything).
fn may_overlap(a: &str, b: &str) -> bool {
    match (anchor(a), anchor(b)) {
        (Some(x), Some(y)) => prefixes_meet(x, y),
        _ => true,
    }
}

/// Whether some path may match the Cloud glob `cloud` and the local glob
/// `local` (conservative, as [`may_overlap`]).
fn may_overlap_cloud(cloud: &str, local: &str) -> bool {
    match (cloud_anchor(cloud), anchor(local)) {
        (Some(x), Some(y)) => prefixes_meet(x, y),
        _ => true,
    }
}

/// An action that admits or keeps more context than without it.
fn permissive(action: &PolicyAction) -> bool {
    matches!(
        action,
        PolicyAction::Include | PolicyAction::Pin | PolicyAction::MarkOutdated
    )
}

/// An action that keeps context out or smaller.
fn restrictive(action: &PolicyAction) -> bool {
    matches!(
        action,
        PolicyAction::Exclude | PolicyAction::Redact | PolicyAction::MaxTokens { .. }
    )
}

fn security_default_names() -> Vec<String> {
    PolicySet::security_defaults()
        .policies
        .into_iter()
        .map(|p| p.name)
        .collect()
}

fn refused(
    ctx: &Ctx<'_>,
    req: &ChangeRequest,
    rule_id: Option<String>,
    refusal: Refusal,
    revision: Option<String>,
) -> ChangedDoc {
    ChangedDoc {
        project_root: ctx.project_root.display().to_string(),
        level: req.level,
        op: req.op,
        rule_id,
        applied: false,
        refused: Some(refusal),
        revision,
        evidence: None,
    }
}

/// The Cloud path rule `glob` overlaps, if any.
fn cloud_rule_over<'a>(cloud: &'a CloudView, glob: &str) -> Option<&'a RuleRow> {
    cloud.rules.iter().find(|row| {
        matches!(row.action.as_str(), "exclude" | "never_send")
            && row
                .match_pattern
                .as_deref()
                .is_some_and(|m| may_overlap_cloud(m, glob))
    })
}

/// Apply `req` when it only tightens the higher layers; the document says
/// what was written, or why nothing was. `audit` records the evidence in
/// the audit trail and returns its reference; when it fails, nothing is
/// written.
pub(crate) fn change(
    ctx: &Ctx<'_>,
    req: &ChangeRequest,
    actor: &str,
    audit: &dyn Fn(&Evidence) -> Result<String, String>,
) -> Result<ChangedDoc, Failure> {
    let cloud = cloud_view(ctx.cloud_data, ctx.project_root, ctx.now);
    let cloud_origin = || Origin {
        level: if cloud.vault.is_some() {
            "vault"
        } else {
            "org"
        },
        vault: cloud.vault.clone(),
        version: cloud.version,
        key_id: cloud.key_id.clone(),
        issued_at: cloud.issued_at.clone(),
    };
    // Cloud rules are changed in Cloud only.
    let cloud_id = req.rule.id.as_deref().filter(|id| id.starts_with("cloud:"));
    if req.level == "cloud" || cloud_id.is_some() || req.rule.layer.as_deref() == Some("cloud") {
        let row = cloud_id.and_then(|id| cloud.rules.iter().find(|r| r.id == id));
        return Ok(refused(
            ctx,
            req,
            req.rule.id.clone(),
            Refusal {
                reason: "policy.cloud_locked",
                layer: Some("cloud"),
                rule: req.rule.id.clone(),
                origin: Some(row.map_or_else(cloud_origin, |r| r.origin.clone())),
                message: "Cloud rules are set in LeanCTX Cloud; no local layer changes them".into(),
                current_revision: None,
            },
            None,
        ));
    }
    let name = target_name(&req.rule).ok_or_else(|| Failure::invalid("the rule needs a name"))?;
    let rule_id = format!("{}:{name}", req.level);
    let security_id = req
        .rule
        .id
        .as_deref()
        .filter(|id| id.starts_with("security:"));
    if security_default_names().contains(&name) || security_id.is_some() {
        return Ok(refused(
            ctx,
            req,
            Some(rule_id),
            Refusal {
                reason: "policy.locked",
                layer: Some("project"),
                rule: Some(format!("security:{name}")),
                origin: Some(Origin::local("security_default")),
                message: format!("{name} is a security default: it cannot be changed"),
                current_revision: None,
            },
            None,
        ));
    }
    // An id names its layer: a rule of another layer is never retargeted.
    if let Some((layer, _)) = req.rule.id.as_deref().and_then(|id| id.split_once(':')) {
        if layer != req.level {
            return Err(Failure::invalid(format!(
                "rule id {} is not a {} rule",
                req.rule.id.as_deref().unwrap_or_default(),
                req.level
            )));
        }
    }
    let new_policy = match req.op {
        Op::Remove => None,
        Op::Add | Op::Edit => Some(to_policy(&req.rule, &name)?),
    };
    // One writer at a time from here to the evidence and the write: the
    // revision compared is the one replaced.
    let layers_dir = ctx.data_dir.join(LAYERS_DIR);
    lean_ctx::core::secure_fs::private_dir(&layers_dir)
        .map_err(|e| Failure::new(Code::Internal, e.to_string()))?;
    let _lock = lean_ctx::core::agents::FileLock::acquire(&layers_dir.join(LOCK_FILE))
        .map_err(|e| Failure::new(Code::Internal, e))?;
    let stored = if req.level == "project" {
        stored_project(ctx.project_root)
    } else {
        stored_personal(ctx.data_dir)
    };
    let before = match (&stored.bytes, stored.revision()) {
        (Ok(_), Some(revision)) => revision,
        (Err(error), _) => return Err(Failure::new(Code::Forbidden, error.clone())),
        (Ok(_), None) => return Err(Failure::new(Code::Internal, "no revision")),
    };
    if req.expected_revision != before {
        return Ok(refused(
            ctx,
            req,
            Some(rule_id),
            Refusal {
                reason: "policy.revision_changed",
                layer: Some(req.level),
                rule: None,
                origin: None,
                message: "the layer changed since it was read; read policies.layers again".into(),
                current_revision: Some(before.clone()),
            },
            Some(before),
        ));
    }
    let mut set = match stored.policies() {
        Ok(Some(set)) => set,
        Ok(None) if req.level == "project" => PolicySet::attach_compat_defaults(),
        Ok(None) => PolicySet::default(),
        Err(error) => return Err(Failure::new(Code::Conflict, error)),
    };
    let index = set.policies.iter().position(|p| p.name == name);
    let old_policy = match (req.op, index) {
        (Op::Add, Some(_)) => {
            return Err(Failure::new(
                Code::Conflict,
                format!("a {} rule named {name} exists", req.level),
            ));
        }
        (Op::Edit | Op::Remove, None) => {
            return Err(Failure::new(
                Code::NotFound,
                format!("no {} rule named {name}", req.level),
            ));
        }
        (_, Some(i)) => Some(set.policies[i].clone()),
        (_, None) => None,
    };
    // Offline (or damaged), what the Cloud policy covers cannot be
    // confirmed: a change touching it fails closed.
    if matches!(cloud.state, "offline_last_known" | "invalid") {
        let globs: Vec<&str> = old_policy
            .iter()
            .chain(new_policy.iter())
            .map(|p| p.match_pattern.as_str())
            .collect();
        let over = globs.iter().find_map(|glob| cloud_rule_over(&cloud, glob));
        if cloud.covers_everything || over.is_some() {
            return Ok(refused(
                ctx,
                req,
                Some(rule_id),
                Refusal {
                    reason: "policy.cloud_unreachable",
                    layer: Some("cloud"),
                    rule: over.map(|r| r.id.clone()),
                    origin: Some(over.map_or_else(cloud_origin, |r| r.origin.clone())),
                    message: format!(
                        "the Cloud policy is {} and this rule touches what it covers; reconnect to change it",
                        cloud.state
                    ),
                    current_revision: None,
                },
                Some(before),
            ));
        }
    }
    if let Some(new) = new_policy.as_ref().filter(|p| permissive(&p.action)) {
        if let Some(row) = cloud_rule_over(&cloud, &new.match_pattern) {
            return Ok(refused(
                ctx,
                req,
                Some(rule_id),
                Refusal {
                    reason: "policy.loosens_higher_layer",
                    layer: Some("cloud"),
                    rule: Some(row.id.clone()),
                    origin: Some(row.origin.clone()),
                    message: format!(
                        "{} would admit what the Cloud rule {} keeps out",
                        new.match_pattern, row.id
                    ),
                    current_revision: None,
                },
                Some(before),
            ));
        }
        if req.level == "personal" {
            let project = project_sourced(ctx.project_root, &stored_project(ctx.project_root));
            let over = project.iter().find(|(p, source)| {
                *source != PolicySource::SecurityDefault
                    && restrictive(&p.action)
                    && may_overlap(&p.match_pattern, &new.match_pattern)
            });
            if let Some((p, source)) = over {
                let row = local_row(p, *source, "project", false);
                return Ok(refused(
                    ctx,
                    req,
                    Some(rule_id),
                    Refusal {
                        reason: "policy.loosens_higher_layer",
                        layer: Some("project"),
                        rule: Some(row.id.clone()),
                        origin: Some(row.origin),
                        message: format!(
                            "{} would admit what the project rule {} keeps out",
                            new.match_pattern, row.id
                        ),
                        current_revision: None,
                    },
                    Some(before),
                ));
            }
        }
    }
    match (index, new_policy.clone()) {
        (Some(i), Some(new)) => set.policies[i] = new,
        (Some(i), None) => {
            set.policies.remove(i);
        }
        (None, Some(new)) => set.policies.push(new),
        (None, None) => {}
    }
    let bytes = serde_json::to_string_pretty(&set)
        .map_err(|e| Failure::new(Code::Internal, e.to_string()))?;
    let after = digest(bytes.as_bytes());
    let recorded = new_policy
        .as_ref()
        .or(old_policy.as_ref())
        .map_or(Value::Null, |p| {
            serde_json::to_value(local_row(p, PolicySource::Project, req.level, true))
                .unwrap_or(Value::Null)
        });
    let mut evidence = Evidence {
        at: ctx
            .now
            .to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true),
        actor: actor.to_owned(),
        level: req.level.to_owned(),
        project_root: (req.level == "project")
            .then(|| canonical(ctx.project_root).display().to_string()),
        op: req.op,
        rule_id: rule_id.clone(),
        rule: recorded,
        before_digest: before,
        after_digest: after.clone(),
        evidence_ref: None,
    };
    // Evidence first, under the lock: a change whose evidence cannot be
    // recorded is not applied.
    let audit_ref = audit(&evidence).map_err(|e| {
        Failure::new(
            Code::Internal,
            format!("the change was not applied: its audit record failed: {e}"),
        )
    })?;
    evidence.evidence_ref = Some(audit_ref);
    let previous = history_lines(ctx.data_dir).map_err(|e| {
        Failure::new(
            Code::Internal,
            format!("the change was not applied: the history is unreadable: {e}"),
        )
    })?;
    append_history(ctx.data_dir, previous.clone(), &evidence)?;
    let written = if req.level == "project" {
        set.save_project(ctx.project_root)
            .map_err(|e| Failure::new(Code::Forbidden, e))
    } else {
        lean_ctx::core::secure_fs::write_private(&layers_dir, PERSONAL_FILE, bytes.as_bytes())
            .map_err(|e| Failure::new(Code::Internal, e.to_string()))
    };
    if let Err(failure) = written {
        // The layer is as it was: its history entry goes again (the audit
        // record stays; its digest names no revision the layer had).
        let _ = write_history(ctx.data_dir, &previous);
        return Err(failure);
    }
    Ok(ChangedDoc {
        project_root: ctx.project_root.display().to_string(),
        level: req.level,
        op: req.op,
        rule_id: Some(rule_id),
        applied: true,
        refused: None,
        revision: Some(after),
        evidence: Some(evidence),
    })
}

// ------------------------------------------------------------------ history

/// The recorded history lines (absent: none).
fn history_lines(data_dir: &Path) -> std::io::Result<Vec<String>> {
    match std::fs::read_to_string(data_dir.join(LAYERS_DIR).join(HISTORY_FILE)) {
        Ok(text) => Ok(text
            .lines()
            .filter(|l| !l.trim().is_empty())
            .map(str::to_owned)
            .collect()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
        Err(e) => Err(e),
    }
}

/// Replace the history with `lines` (the newest [`MAX_HISTORY`]).
fn write_history(data_dir: &Path, lines: &[String]) -> std::io::Result<()> {
    let keep = &lines[lines.len().saturating_sub(MAX_HISTORY)..];
    let mut text = keep.join("\n");
    if !text.is_empty() {
        text.push('\n');
    }
    lean_ctx::core::secure_fs::write_private(
        &data_dir.join(LAYERS_DIR),
        HISTORY_FILE,
        text.as_bytes(),
    )
}

/// `lines` (the history as read under the change lock) plus `evidence`.
fn append_history(
    data_dir: &Path,
    mut lines: Vec<String>,
    evidence: &Evidence,
) -> Result<(), Failure> {
    lines.push(
        serde_json::to_string(evidence).map_err(|e| Failure::new(Code::Internal, e.to_string()))?,
    );
    write_history(data_dir, &lines).map_err(|e| {
        Failure::new(
            Code::Internal,
            format!("the change was not applied: its history entry failed: {e}"),
        )
    })
}

/// The `policies.history` body.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(crate) struct HistoryDoc {
    pub project_root: String,
    /// Newest first: the project's changes and the personal layer's.
    pub entries: Vec<Evidence>,
    /// Matching entries recorded (before `limit`).
    pub total: usize,
}

pub(crate) fn history_doc(ctx: &Ctx<'_>, limit: usize) -> HistoryDoc {
    let root = canonical(ctx.project_root).display().to_string();
    let matching: Vec<Evidence> = history_lines(ctx.data_dir)
        .unwrap_or_default()
        .iter()
        .filter_map(|line| serde_json::from_str::<Evidence>(line).ok())
        .filter(|e| e.level == "personal" || e.project_root.as_deref() == Some(root.as_str()))
        .collect();
    let total = matching.len();
    HistoryDoc {
        project_root: ctx.project_root.display().to_string(),
        entries: matching.into_iter().rev().take(limit).collect(),
        total,
    }
}

// ----------------------------------------------------------------- dispatch

/// The machine's directories and clock for one answer.
struct Env {
    data_dir: PathBuf,
    cloud_data: Option<PathBuf>,
    now: UtcDateTime,
}

impl Env {
    fn load() -> Result<Self, Failure> {
        let data_dir = lean_ctx::core::data_dir::lean_ctx_data_dir()
            .map_err(|e| Failure::new(Code::Internal, e))?;
        let cloud_data = lean_ctx_cloud::CloudStore::open()
            .ok()
            .map(|store| store.data_dir().to_path_buf())
            .or_else(|| {
                effective_floor::floor_dir()
                    .and_then(|dir| dir.parent()?.parent().map(Path::to_path_buf))
            });
        Ok(Self {
            data_dir,
            cloud_data,
            now: UtcDateTime::now(),
        })
    }

    fn ctx<'a>(&'a self, project_root: &'a Path) -> Ctx<'a> {
        Ctx {
            project_root,
            cloud_data: self.cloud_data.as_deref(),
            data_dir: &self.data_dir,
            now: self.now,
        }
    }
}

/// The person changing a layer on this machine.
fn actor() -> String {
    std::env::var("USER")
        .or_else(|_| std::env::var("USERNAME"))
        .ok()
        .filter(|user| valid_name(user))
        .map_or_else(|| "local".to_owned(), |user| format!("local:{user}"))
}

/// The change's audit-trail entry (`x-policy_changed`, digests only).
fn record_audit(evidence: &Evidence) -> Result<String, String> {
    use lean_ctx::core::audit_trail::{AuditEntryData, AuditEventType, try_record};
    try_record(AuditEntryData {
        agent_id: evidence.actor.clone(),
        tool: TOOL.into(),
        action: Some(format!(
            "policies.changed:{}:{}",
            evidence.level,
            evidence.op.as_str()
        )),
        input_hash: evidence.after_digest.clone(),
        output_tokens: 0,
        role: "person".into(),
        event_type: AuditEventType::Unknown("x-policy_changed".into()),
    })
    .map(|entry| format!("audit:{}", entry.entry_hash))
}

/// The change body on stdin (CLI only; never on a terminal).
fn read_stdin() -> Option<String> {
    use std::io::{IsTerminal, Read};
    let stdin = std::io::stdin();
    if stdin.is_terminal() {
        return None;
    }
    let mut text = String::new();
    stdin
        .lock()
        .take(MAX_BODY as u64 + 1)
        .read_to_string(&mut text)
        .ok()?;
    (text.len() <= MAX_BODY && !text.trim().is_empty()).then_some(text)
}

/// `f` with the project of `args`, or the `no_project` error document.
fn with_root(args: &[String], f: impl FnOnce(&str) -> Answer) -> Answer {
    match super::project_root(args) {
        Ok(root) => f(&root),
        Err(failure) => Answer::failure(&failure),
    }
}

/// The document of `kind` for `args` (same bytes on the CLI and the
/// socket), released through the `ctx_policy` tool run.
pub(crate) fn answer(kind: &str, args: &[String], via: Via) -> Answer {
    match kind {
        "policies.layers" => with_root(args, |root| {
            super::answer_released(TOOL, root, kind, via, || {
                let env = Env::load()?;
                Ok(layers_doc(&env.ctx(Path::new(root))))
            })
        }),
        "policies.history" => with_root(args, |root| {
            super::answer_released(TOOL, root, kind, via, || {
                let limit = super::limit(args, 50, 500)?;
                let env = Env::load()?;
                Ok(history_doc(&env.ctx(Path::new(root)), limit))
            })
        }),
        "policies.changed" => with_root(args, |root| {
            let stdin = match (via, super::flag(args, "--op")) {
                (Via::Cli, Ok(None)) => read_stdin(),
                _ => None,
            };
            let request = match parse_request(args, stdin.as_deref()) {
                Ok(request) => request,
                Err(failure) => return Answer::failure(&failure),
            };
            let preview = json!({
                "level": request.level,
                "op": request.op,
                "expected_revision": request.expected_revision,
                "rule": request.rule,
            });
            super::answer_released_write(TOOL, root, kind, via, &preview, || {
                let env = Env::load()?;
                change(&env.ctx(Path::new(root)), &request, &actor(), &record_audit)
            })
        }),
        other => Answer::failure(&Failure::invalid(format!("unknown kind {other}"))),
    }
}

/// `lean-ctx policy layers|change|history --json …`.
pub(crate) fn run(verb: &str, args: &[String]) {
    let kind = match verb {
        "layers" => "policies.layers",
        "change" => "policies.changed",
        _ => "policies.history",
    };
    super::print(&answer(kind, args, Via::Cli));
}

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

    const IN_FORCE: &str = "2999-01-01T00:00:00Z";
    const EXPIRED: &str = "2026-01-01T00:00:00Z";

    fn now() -> UtcDateTime {
        "2026-11-15T00:00:00Z".parse().unwrap()
    }

    /// A project, a data dir and a Cloud store dir.
    struct World {
        project: tempfile::TempDir,
        data: tempfile::TempDir,
        cloud: tempfile::TempDir,
    }

    impl World {
        fn new() -> Self {
            let project = tempfile::tempdir().unwrap();
            std::fs::create_dir_all(project.path().join(".git")).unwrap();
            Self {
                project,
                data: tempfile::tempdir().unwrap(),
                cloud: tempfile::tempdir().unwrap(),
            }
        }

        fn ctx(&self) -> Ctx<'_> {
            self.ctx_at(now())
        }

        fn ctx_at(&self, now: UtcDateTime) -> Ctx<'_> {
            Ctx {
                project_root: self.project.path(),
                cloud_data: Some(self.cloud.path()),
                data_dir: self.data.path(),
                now,
            }
        }

        fn revision(&self, level: &str) -> String {
            let doc = layers_doc(&self.ctx());
            if level == "project" {
                doc.project.revision
            } else {
                doc.personal.revision
            }
            .unwrap_or_default()
        }

        fn request(&self, level: &'static str, op: Op, rule: Value) -> ChangeRequest {
            ChangeRequest {
                level,
                op,
                expected_revision: self.revision(level),
                rule: serde_json::from_value(rule).unwrap(),
            }
        }

        fn apply(&self, level: &'static str, op: Op, rule: Value) -> ChangedDoc {
            let req = self.request(level, op, rule);
            change(
                &self.ctx(),
                &req,
                "local:test",
                &|_| Ok("audit:test".into()),
            )
            .unwrap()
        }

        /// Install a verified Cloud policy excluding `excluded` (sorted).
        fn install_floor(&self, excluded: &[&str], not_after: &str) {
            use lean_ctx_cloud::effective_floor::{
                FLOOR_FILE, FLOOR_SCHEMA, Floor, REQUIRED_FILE, floor_dir_in,
            };
            use lean_ctx_protocol::cloud::effective_policy::{
                BundlePolicyV1, POLICY_DOCUMENT_SCHEMA, SecurityPolicyV1,
            };
            let floor = Floor {
                schema: FLOOR_SCHEMA.into(),
                version: 3,
                digest: format!("sha256:{}", "a".repeat(64)),
                not_after: not_after.into(),
                policy: BundlePolicyV1 {
                    effective: EffectivePolicyV1 {
                        schema: POLICY_DOCUMENT_SCHEMA.into(),
                        security: SecurityPolicyV1 {
                            excluded_paths: Some(
                                excluded.iter().map(|s| (*s).to_owned()).collect(),
                            ),
                            block_secrets: Some(true),
                            ..Default::default()
                        },
                        context: Default::default(),
                        sharing: Default::default(),
                        agents: None,
                        orchestrator: None,
                        other: Default::default(),
                    },
                    vaults: Vec::new(),
                },
            };
            let dir = floor_dir_in(self.cloud.path());
            std::fs::create_dir_all(&dir).unwrap();
            std::fs::write(dir.join(FLOOR_FILE), serde_json::to_vec(&floor).unwrap()).unwrap();
            std::fs::write(dir.join(REQUIRED_FILE), br#"{"version":3}"#).unwrap();
        }

        fn project_file(&self) -> PathBuf {
            self.project.path().join(".lean-ctx").join(PROJECT_FILE)
        }
    }

    fn reason(doc: &ChangedDoc) -> Option<&'static str> {
        doc.refused.as_ref().map(|r| r.reason)
    }

    fn rule(name: &str, glob: &str, action: &str) -> Value {
        json!({"name": name, "match": glob, "action": action})
    }

    /// Tighten-only: project against Cloud, personal against Cloud and
    /// project.
    #[test]
    fn tighten_only_matrix() {
        let w = World::new();
        w.install_floor(&["secrets/**"], IN_FORCE);

        let loosen = w.apply("project", Op::Add, rule("api", "secrets/api/**", "include"));
        assert_eq!(reason(&loosen), Some("policy.loosens_higher_layer"));
        let refusal = loosen.refused.as_ref().unwrap();
        assert_eq!(refusal.layer, Some("cloud"));
        assert_eq!(refusal.rule.as_deref(), Some("cloud:excluded_paths[0]"));
        let origin = refusal.origin.as_ref().unwrap();
        assert_eq!((origin.level, origin.version), ("org", Some(3)));
        assert!(!w.project_file().exists(), "nothing written on refusal");

        let tighter = w.apply(
            "project",
            Op::Add,
            rule("keys", "secrets/keys/**", "exclude"),
        );
        assert!(tighter.applied, "{tighter:?}");
        let disjoint = w.apply("project", Op::Add, rule("docs", "docs/**", "pin"));
        assert!(disjoint.applied, "{disjoint:?}");
        let unanchored = w.apply("project", Op::Add, rule("rs", "**/*.rs", "pin"));
        assert_eq!(reason(&unanchored), Some("policy.loosens_higher_layer"));

        let build = w.apply("project", Op::Add, rule("build", "build/**", "exclude"));
        assert!(build.applied);
        let personal = w.apply("personal", Op::Add, rule("out", "build/out/**", "pin"));
        assert_eq!(reason(&personal), Some("policy.loosens_higher_layer"));
        let refusal = personal.refused.as_ref().unwrap();
        assert_eq!(refusal.layer, Some("project"));
        assert_eq!(refusal.rule.as_deref(), Some("project:build"));
        let over_cloud = w.apply("personal", Op::Add, rule("s", "secrets/x/**", "include"));
        assert_eq!(over_cloud.refused.as_ref().unwrap().layer, Some("cloud"));
        let personal_tight = w.apply("personal", Op::Add, rule("tmp", "build/tmp/**", "exclude"));
        assert!(personal_tight.applied, "{personal_tight:?}");

        let doc = layers_doc(&w.ctx());
        let names: Vec<&str> = doc.project.rules.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"keys") && names.contains(&"docs") && names.contains(&"build"));
        assert!(!names.contains(&"api"));
        assert!(
            names.contains(&"compress_large_files"),
            "the first write keeps the built-in defaults in effect: {names:?}"
        );
        assert_eq!(doc.personal.rules.len(), 1);
        assert_eq!(doc.personal.rules[0].id, "personal:tmp");
    }

    /// Cloud rules and security defaults are locked and refused.
    #[test]
    fn cloud_and_security_rules_are_locked() {
        let w = World::new();
        w.install_floor(&["secrets/**"], IN_FORCE);
        let doc = layers_doc(&w.ctx());
        assert_eq!(doc.cloud.state, "in_force");
        assert_eq!(doc.cloud.signature, "verified");
        assert_eq!(doc.cloud.covered, ["secrets/**"]);
        assert!(
            doc.cloud
                .rules
                .iter()
                .all(|r| r.locked && !r.editable && r.layer == "cloud")
        );
        let ids: Vec<&str> = doc.cloud.rules.iter().map(|r| r.id.as_str()).collect();
        assert_eq!(ids, ["cloud:excluded_paths[0]", "cloud:block_secrets"]);
        let secrets = doc
            .project
            .rules
            .iter()
            .find(|r| r.name == "never_include_secrets")
            .unwrap();
        assert!(secrets.locked && !secrets.editable);
        assert_eq!(secrets.id, "security:never_include_secrets");
        assert_eq!(secrets.origin.level, "security_default");
        let default = doc
            .project
            .rules
            .iter()
            .find(|r| r.name == "compress_large_files")
            .unwrap();
        assert!(!default.locked && default.editable);
        assert_eq!(default.origin.level, "default");

        let by_id = w.apply(
            "project",
            Op::Edit,
            json!({"id": "cloud:excluded_paths[0]", "match": "secrets/**", "action": "include"}),
        );
        assert_eq!(reason(&by_id), Some("policy.cloud_locked"));
        assert_eq!(by_id.refused.unwrap().origin.unwrap().version, Some(3));
        let mut at_cloud = w.request("project", Op::Add, rule("x", "a/**", "exclude"));
        at_cloud.level = "cloud";
        let doc = change(&w.ctx(), &at_cloud, "local:test", &|_| {
            Ok("audit:none".into())
        })
        .unwrap();
        assert_eq!(reason(&doc), Some("policy.cloud_locked"));
        let security = w.apply(
            "project",
            Op::Remove,
            json!({"name": "never_include_secrets"}),
        );
        assert_eq!(reason(&security), Some("policy.locked"));
        assert!(!w.project_file().exists());
    }

    /// Past `not_after` (or damaged) the Cloud coverage cannot be
    /// confirmed: changes touching it fail closed, others go through.
    #[test]
    fn offline_cloud_fails_closed() {
        let w = World::new();
        w.install_floor(&["secrets/**"], EXPIRED);
        assert_eq!(layers_doc(&w.ctx()).cloud.state, "offline_last_known");
        let touching = w.apply("project", Op::Add, rule("k", "secrets/k/**", "exclude"));
        assert_eq!(reason(&touching), Some("policy.cloud_unreachable"));
        assert_eq!(
            touching.refused.unwrap().rule.as_deref(),
            Some("cloud:excluded_paths[0]")
        );
        let disjoint = w.apply("project", Op::Add, rule("d", "docs/**", "exclude"));
        assert!(disjoint.applied, "{disjoint:?}");

        let dir = effective_floor::floor_dir_in(w.cloud.path());
        std::fs::write(dir.join(effective_floor::FLOOR_FILE), b"{").unwrap();
        let doc = layers_doc(&w.ctx());
        assert_eq!(doc.cloud.state, "invalid");
        assert!(doc.cloud.covers_everything);
        let any = w.apply("project", Op::Add, rule("e", "elsewhere/**", "exclude"));
        assert_eq!(reason(&any), Some("policy.cloud_unreachable"));
    }

    /// A stale revision is refused with the current one; nothing is written.
    #[test]
    fn revision_conflict_is_refused() {
        let w = World::new();
        let first = w.apply("project", Op::Add, rule("a", "a/**", "exclude"));
        assert!(first.applied);
        let mut stale = w.request("project", Op::Add, rule("b", "b/**", "exclude"));
        stale.expected_revision = digest(b"");
        let before = std::fs::read(w.project_file()).unwrap();
        let doc = change(&w.ctx(), &stale, "local:test", &|_| Ok("audit:none".into())).unwrap();
        assert_eq!(reason(&doc), Some("policy.revision_changed"));
        let current = w.revision("project");
        assert_eq!(
            doc.refused.unwrap().current_revision.as_deref(),
            Some(current.as_str())
        );
        assert_eq!(first.revision.as_deref(), Some(current.as_str()));
        assert_eq!(std::fs::read(w.project_file()).unwrap(), before);
    }

    /// The history lists this project's and the personal changes, newest
    /// first, with the digest chain.
    #[test]
    fn history_is_newest_first() {
        let w = World::new();
        let other = World::new();
        let add = w.apply("project", Op::Add, rule("a", "a/**", "exclude"));
        let personal = w.apply("personal", Op::Add, rule("p", "p/**", "exclude"));
        let edit = w.apply("project", Op::Edit, rule("a", "a/b/**", "exclude"));
        assert!(add.applied && personal.applied && edit.applied);
        // Another project's change in the same data dir is not listed.
        let ctx = Ctx {
            data_dir: w.data.path(),
            ..other.ctx()
        };
        let req = ChangeRequest {
            level: "project",
            op: Op::Add,
            expected_revision: digest(b""),
            rule: serde_json::from_value(rule("o", "o/**", "exclude")).unwrap(),
        };
        assert!(
            change(&ctx, &req, "local:test", &|_| Ok("audit:none".into()))
                .unwrap()
                .applied
        );

        let doc = history_doc(&w.ctx(), 2);
        assert_eq!(doc.total, 3);
        let ops: Vec<(String, Op, String)> = doc
            .entries
            .iter()
            .map(|e| (e.level.clone(), e.op, e.rule_id.clone()))
            .collect();
        assert_eq!(
            ops,
            [
                ("project".into(), Op::Edit, "project:a".into()),
                ("personal".into(), Op::Add, "personal:p".into()),
            ]
        );
        let all = history_doc(&w.ctx(), 50);
        let (newest, oldest) = (&all.entries[0], &all.entries[2]);
        assert_eq!(newest.before_digest, oldest.after_digest);
        assert_eq!(oldest.before_digest, digest(b""));
        assert_eq!(newest.after_digest, w.revision("project"));
        assert_eq!(newest.evidence_ref.as_deref(), Some("audit:test"));
        assert_eq!(newest.actor, "local:test");
        assert_eq!(newest.at, "2026-11-15T00:00:00Z");
        assert_eq!(newest.rule["match"], "a/b/**");
    }

    /// A symlinked `.lean-ctx` is never followed: neither read nor written.
    #[cfg(unix)]
    #[test]
    fn project_layer_writes_through_pathjail() {
        let w = World::new();
        let outside = tempfile::tempdir().unwrap();
        std::os::unix::fs::symlink(outside.path(), w.project.path().join(".lean-ctx")).unwrap();
        let doc = layers_doc(&w.ctx());
        assert!(
            doc.project.damaged && !doc.project.editable,
            "{:?}",
            doc.project
        );
        let req = ChangeRequest {
            level: "project",
            op: Op::Add,
            expected_revision: digest(b""),
            rule: serde_json::from_value(rule("a", "a/**", "exclude")).unwrap(),
        };
        let failure =
            change(&w.ctx(), &req, "local:test", &|_| Ok("audit:none".into())).unwrap_err();
        assert_eq!(failure.code, Code::Forbidden);
        assert!(!outside.path().join(PROJECT_FILE).exists());
    }

    /// The document is a function of the stored data, not of the clock.
    #[test]
    fn layers_document_is_deterministic() {
        let w = World::new();
        w.install_floor(&["secrets/**"], IN_FORCE);
        assert!(
            w.apply("project", Op::Add, rule("a", "a/**", "exclude"))
                .applied
        );
        let text = |at: &str| {
            super::super::document(
                "policies.layers",
                &layers_doc(&w.ctx_at(at.parse().unwrap())),
            )
        };
        assert_eq!(text("2026-11-15T00:00:00Z"), text("2027-03-01T12:34:56Z"));
        let history = |at: &str| {
            super::super::document(
                "policies.history",
                &history_doc(&w.ctx_at(at.parse().unwrap()), 10),
            )
        };
        assert_eq!(
            history("2026-11-15T00:00:00Z"),
            history("2028-01-01T00:00:00Z")
        );
    }

    /// Personal rules are free: add, edit and remove need no license.
    #[test]
    fn personal_layer_needs_no_license() {
        let w = World::new();
        let doc = layers_doc(&w.ctx());
        assert!(doc.personal.editable && !doc.personal.enforced);
        let add = w.apply("personal", Op::Add, rule("p", "p/**", "exclude"));
        assert!(add.applied, "{add:?}");
        let edit = w.apply("personal", Op::Edit, rule("p", "p/q/**", "exclude"));
        assert!(edit.applied, "{edit:?}");
        let doc = layers_doc(&w.ctx());
        assert_eq!(doc.personal.rules.len(), 1);
        let remove = w.apply("personal", Op::Remove, json!({"id": "personal:p"}));
        assert!(remove.applied, "{remove:?}");
        assert!(layers_doc(&w.ctx()).personal.rules.is_empty());
    }

    /// Writers naming the same revision: exactly one per layer applies, the
    /// others see `policy.revision_changed`, and no history line is lost.
    #[test]
    fn concurrent_writers_with_one_revision_apply_once() {
        let w = World::new();
        let project = w.revision("project");
        let personal = w.revision("personal");
        let threads = 12;
        let barrier = std::sync::Barrier::new(threads);
        let docs: Vec<ChangedDoc> = std::thread::scope(|scope| {
            let handles: Vec<_> = (0..threads)
                .map(|i| {
                    let (w, barrier) = (&w, &barrier);
                    let (level, expected) = if i % 2 == 0 {
                        ("project", project.clone())
                    } else {
                        ("personal", personal.clone())
                    };
                    scope.spawn(move || {
                        let req = ChangeRequest {
                            level,
                            op: Op::Add,
                            expected_revision: expected,
                            rule: serde_json::from_value(rule(
                                &format!("r{i}"),
                                &format!("r{i}/**"),
                                "exclude",
                            ))
                            .unwrap(),
                        };
                        barrier.wait();
                        change(&w.ctx(), &req, "local:test", &|_| Ok("audit:test".into())).unwrap()
                    })
                })
                .collect();
            handles.into_iter().map(|h| h.join().unwrap()).collect()
        });
        for level in ["project", "personal"] {
            let of_level: Vec<&ChangedDoc> = docs.iter().filter(|d| d.level == level).collect();
            let applied = of_level.iter().filter(|d| d.applied).count();
            assert_eq!(applied, 1, "{level}: {of_level:?}");
            assert!(
                of_level
                    .iter()
                    .filter(|d| !d.applied)
                    .all(|d| reason(d) == Some("policy.revision_changed")),
                "{of_level:?}"
            );
        }
        let doc = layers_doc(&w.ctx());
        assert_eq!(doc.personal.rules.len(), 1);
        let history = history_doc(&w.ctx(), 50);
        assert_eq!(history.total, 2, "{:?}", history.entries);
        let personal_entry = history
            .entries
            .iter()
            .find(|e| e.level == "personal")
            .unwrap();
        assert_eq!(
            Some(personal_entry.after_digest.clone()),
            doc.personal.revision
        );
    }

    /// The evidence comes first: when the audit record or the history entry
    /// fails, the layer is not changed.
    #[test]
    fn failed_evidence_leaves_the_layer_unchanged() {
        let w = World::new();
        assert!(
            w.apply("personal", Op::Add, rule("p", "p/**", "exclude"))
                .applied
        );
        let personal_file = w.data.path().join(LAYERS_DIR).join(PERSONAL_FILE);
        let before = std::fs::read(&personal_file).unwrap();
        let req = w.request("personal", Op::Add, rule("q", "q/**", "exclude"));
        let failure =
            change(&w.ctx(), &req, "local:test", &|_| Err("disk full".into())).unwrap_err();
        assert_eq!(failure.code, Code::Internal);
        assert_eq!(std::fs::read(&personal_file).unwrap(), before);
        assert_eq!(history_doc(&w.ctx(), 50).total, 1);

        // A history that cannot be written: nothing is applied either.
        let history = w.data.path().join(LAYERS_DIR).join(HISTORY_FILE);
        std::fs::remove_file(&history).unwrap();
        std::fs::create_dir(&history).unwrap();
        let project_req = w.request("project", Op::Add, rule("a", "a/**", "exclude"));
        let failure = change(&w.ctx(), &project_req, "local:test", &|_| {
            Ok("audit:test".into())
        })
        .unwrap_err();
        assert_eq!(failure.code, Code::Internal);
        assert!(!w.project_file().exists());
        let failure =
            change(&w.ctx(), &req, "local:test", &|_| Ok("audit:test".into())).unwrap_err();
        assert_eq!(failure.code, Code::Internal);
        assert_eq!(std::fs::read(&personal_file).unwrap(), before);
    }

    /// Cloud globs are gitignore globs: anything not provably disjoint
    /// overlaps.
    #[test]
    fn cloud_globs_overlap_with_gitignore_grammar() {
        for (cloud, local) in [
            ("secrets*", "a/**"),
            ("/secrets/**", "secrets/x/**"),
            ("src/*/keys/**", "src/app/keys/**"),
            ("secret?/**", "secrets/x/**"),
            ("[s]ecrets/**", "secrets/x/**"),
            ("{a,b}/**", "a/x/**"),
            ("!keep/**", "a/**"),
            ("secrets/", "a/secrets/**"),
            ("secrets/**", "Secrets/x/**"),
        ] {
            assert!(may_overlap_cloud(cloud, local), "{cloud} vs {local}");
        }
        for (cloud, local) in [
            ("secrets/**", "docs/**"),
            ("src/*/keys/**", "docs/**"),
            ("secrets/**", "secretsx/**"),
        ] {
            assert!(!may_overlap_cloud(cloud, local), "{cloud} vs {local}");
        }
        // Local globs: the anchor stops at the first metacharacter.
        assert!(may_overlap("src/*/keys/**", "src/app/**"));

        let w = World::new();
        w.install_floor(&["secrets*"], IN_FORCE);
        let include = w.apply("project", Op::Add, rule("a", "a/**", "include"));
        assert_eq!(reason(&include), Some("policy.loosens_higher_layer"));
        let w = World::new();
        w.install_floor(&["src/*/keys/**"], IN_FORCE);
        let include = w.apply("project", Op::Add, rule("k", "src/app/keys/**", "include"));
        assert_eq!(reason(&include), Some("policy.loosens_higher_layer"));
    }

    /// The project layer's rules come from the jailed read only: a
    /// symlinked `.lean-ctx` shows the security defaults, never the outside
    /// file's rules.
    #[cfg(unix)]
    #[test]
    fn project_rules_are_read_through_pathjail() {
        let w = World::new();
        let outside = tempfile::tempdir().unwrap();
        std::fs::write(
            outside.path().join(PROJECT_FILE),
            serde_json::to_vec(&json!({"policies": [
                {"name": "outside", "match": "o/**", "action": "exclude"}
            ]}))
            .unwrap(),
        )
        .unwrap();
        std::os::unix::fs::symlink(outside.path(), w.project.path().join(".lean-ctx")).unwrap();
        let doc = layers_doc(&w.ctx());
        assert!(doc.project.damaged);
        assert!(doc.project.revision.is_none());
        assert!(!doc.project.rules.is_empty());
        assert!(
            doc.project
                .rules
                .iter()
                .all(|r| r.origin.level == "security_default"),
            "{:?}",
            doc.project.rules
        );
    }

    /// A match that never meets a project-relative path is refused.
    #[test]
    fn non_relative_matches_are_refused() {
        let w = World::new();
        for glob in ["/abs/**", "./a/**", "a/../b/**", ".."] {
            let req = w.request("project", Op::Add, rule("x", glob, "exclude"));
            let failure =
                change(&w.ctx(), &req, "local:test", &|_| Ok("audit:test".into())).unwrap_err();
            assert_eq!(failure.code, Code::InvalidArgument, "{glob}");
        }
        assert!(!w.project_file().exists());
    }

    /// An unreadable personal file has no revision (not the empty one).
    #[test]
    fn unreadable_personal_file_has_no_revision() {
        let w = World::new();
        std::fs::create_dir_all(w.data.path().join(LAYERS_DIR).join(PERSONAL_FILE)).unwrap();
        let doc = layers_doc(&w.ctx());
        assert!(doc.personal.damaged && !doc.personal.editable);
        assert_eq!(doc.personal.revision, None);
        assert!(doc.personal.error.is_some());
    }

    /// An id of another layer is refused, never retargeted.
    #[test]
    fn an_id_of_another_layer_is_refused() {
        let w = World::new();
        assert!(
            w.apply("project", Op::Add, rule("p", "p/**", "exclude"))
                .applied
        );
        assert!(
            w.apply("personal", Op::Add, rule("p", "p/**", "exclude"))
                .applied
        );
        let before = std::fs::read(w.project_file()).unwrap();
        let req = w.request(
            "project",
            Op::Edit,
            json!({"id": "personal:p", "match": "p/q/**", "action": "exclude"}),
        );
        let failure =
            change(&w.ctx(), &req, "local:test", &|_| Ok("audit:test".into())).unwrap_err();
        assert_eq!(failure.code, Code::InvalidArgument);
        assert_eq!(std::fs::read(w.project_file()).unwrap(), before);
    }

    #[test]
    fn requests_parse_from_flags_and_body() {
        let args = |w: &[&str]| w.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>();
        let body = r#"{"op":"edit","rule":{"id":"project:a","match":"a/**","action":"max_tokens","action_params":{"limit":500},"locked":false}}"#;
        let req = parse_request(
            &args(&[
                "--level",
                "project",
                "--expected-revision",
                "sha256:x",
                "--json",
            ]),
            Some(body),
        )
        .unwrap();
        assert_eq!((req.level, req.op), ("project", Op::Edit));
        assert_eq!(target_name(&req.rule).as_deref(), Some("a"));
        let policy = to_policy(&req.rule, "a").unwrap();
        assert!(matches!(
            policy.action,
            PolicyAction::MaxTokens { limit: 500 }
        ));
        assert!(
            parse_request(
                &args(&["--level", "team", "--expected-revision", "r"]),
                Some(body)
            )
            .is_err()
        );
        assert!(parse_request(&args(&["--level", "project"]), Some(body)).is_err());
        assert!(
            parse_request(
                &args(&["--level", "project", "--expected-revision", "r"]),
                None
            )
            .is_err()
        );
        assert!(
            to_policy(
                &RuleInput {
                    match_pattern: Some("a".into()),
                    action: Some("explode".into()),
                    ..Default::default()
                },
                "a"
            )
            .is_err()
        );
        assert!(to_policy(&RuleInput::default(), "bad name").is_err());
    }

    /// Routed, listed with the `policy` verbs, and a socket body's rule
    /// object reaches the CLI parser intact.
    #[test]
    fn the_policy_layer_kinds_are_served_and_listed() {
        use super::super::{capabilities, routes};
        let mut routed: Vec<&str> = ROUTES.iter().map(|r| r.kind).collect();
        routed.sort_unstable();
        assert_eq!(routed, KINDS);
        let table = routes::table_json();
        let caps = capabilities::build_with(table.clone(), routes::events_json());
        let commands = capabilities::commands_of(&table);
        for (kind, method, path, verb) in [
            (
                "policies.layers",
                "GET",
                "/v1/local/policies/layers",
                "layers",
            ),
            (
                "policies.changed",
                "POST",
                "/v1/local/policies/change",
                "change",
            ),
            (
                "policies.history",
                "GET",
                "/v1/local/policies/history",
                "history",
            ),
        ] {
            let row = table
                .as_array()
                .unwrap()
                .iter()
                .find(|r| r["path"] == path)
                .unwrap_or_else(|| panic!("{path} in capabilities.routes"));
            assert_eq!(
                (row["kind"].as_str(), row["method"].as_str()),
                (Some(kind), Some(method))
            );
            assert_eq!(row["command"], "policy", "{row}");
            assert_eq!(row["verb"], verb, "{row}");
            assert!(
                commands["policy"]
                    .as_array()
                    .is_some_and(|v| v.contains(&json!({"verb": verb, "via": ["cli", "socket"]}))),
                "{verb}: {commands:?}"
            );
            assert!(caps.kinds.contains(&kind));
            assert!(super::super::part_of(kind).is_some());
        }
        let route = ROUTES
            .iter()
            .find(|r| r.kind == "policies.changed")
            .unwrap();
        assert_eq!(
            routes::args_of(route, None, &[]).unwrap_err().code,
            Code::NoProject
        );
        let dir = tempfile::tempdir().unwrap();
        let args = routes::args_of(
            route,
            None,
            &[
                ("project_root".into(), json!(dir.path().to_string_lossy())),
                ("level".into(), json!("project")),
                ("expected_revision".into(), json!("sha256:abc")),
                ("op".into(), json!("add")),
                (
                    "rule".into(),
                    json!({"name": "a", "match": "a/**", "action": "exclude"}),
                ),
            ],
        )
        .unwrap();
        let req = parse_request(&args, None).unwrap();
        assert_eq!(req.op, Op::Add);
        assert_eq!(req.rule.match_pattern.as_deref(), Some("a/**"));
        assert_eq!(req.expected_revision, "sha256:abc");
        assert!(routes::args_of(route, None, &[("rule".into(), json!(3))]).is_err());
    }
}