aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
//! Feature-based configuration policy shared by the resolver, the configure
//! handler and (later) the setup/doctor CLI.
//!
//! Configuration used to pick a tool surface level (`tool_surface`), a hoisting
//! mode (`hoist_builtin_tools`) and a set of index booleans. It now has one
//! registration rule — a tool is registered unless its canonical name is in
//! `disabled_tools` — plus three first-class index switches under `indexes`.
//! This module owns the literal tool inventory, the table of retired keys, the
//! per-block translation of those keys into canonical ones, validation of a
//! resolved configuration object and the migration-notice projection digest.
//!
//! Retired names in [`REFUSED_RETIRED_PATHS`] and the prefixed tool names in
//! [`LEGACY_TOOL_ALIASES`] are refused in user config with `removed_config_key`.
//! The translation below still covers those names,
//! because `aft doctor --fix` and the automatic rewrite of the user file
//! ([`crate::config_fix::auto_migrate_user_config`]) build their rewrite from
//! it; project config translates them with a notice. Names retired more
//! recently are translated in memory into their current equivalents on every
//! load, and the current keys then pass through the normal user/project trust
//! rules. The remaining data-layer rename ([`DATA_LAYER_RETIRED_PATHS`]) gets a notice until
//! [`DATA_LAYER_KEYS_REFUSED_FROM`] and are refused from that release on. A
//! project file is never written, only reported with a notice. The TypeScript plugins carry a mirror of
//! the same rules in `packages/aft-bridge/src/feature-config.ts`; the config
//! parity fixtures and the shared notice-projection fixtures keep them equal.

use std::collections::BTreeSet;

use serde_json::{Map, Value};

pub(crate) mod settings_shape;
use sha2::{Digest, Sha256};

/// Every agent-visible tool name a registration adapter may publish, in
/// sorted order. Known-name checks use this complete inventory, never the
/// currently enabled subset.
pub const CANONICAL_TOOLS: [&str; 23] = [
    "aft_callgraph",
    "aft_conflicts",
    "aft_delete",
    "aft_import",
    "aft_inspect",
    "aft_move",
    "aft_outline",
    "aft_safety",
    "aft_search",
    "aft_zoom",
    "apply_patch",
    "ast_grep_replace",
    "ast_grep_search",
    "bash",
    "bash_kill",
    "bash_status",
    "bash_watch",
    "bash_write",
    "edit",
    "glob",
    "grep",
    "read",
    "write",
];

/// The seven host tool slots AFT takes over.
pub const HOST_TOOL_NAMES: [&str; 7] = [
    "apply_patch",
    "bash",
    "edit",
    "glob",
    "grep",
    "read",
    "write",
];

/// Disables applied when the user's base configuration makes no registration
/// choice at all: move and delete stay opt-in.
pub const DEFAULT_DISABLED_TOOLS: [&str; 2] = ["aft_delete", "aft_move"];

/// Historical prefixed tool names that disabled lists may still contain,
/// mapped to the canonical host name they now mean.
pub const LEGACY_TOOL_ALIASES: [(&str, &str); 7] = [
    ("aft_read", "read"),
    ("aft_write", "write"),
    ("aft_edit", "edit"),
    ("aft_apply_patch", "apply_patch"),
    ("aft_grep", "grep"),
    ("aft_glob", "glob"),
    ("aft_bash", "bash"),
];

/// Names removed by the historical `bash` registration gate (bash turned
/// off), expressed with canonical names.
pub const BASH_GATE_DISABLES: [&str; 5] = [
    "bash",
    "bash_kill",
    "bash_status",
    "bash_watch",
    "bash_write",
];

/// Identity of the migration policy that retires the keys below.
pub const MIGRATION_POLICY_ID: &str = "feature-config-v1";
/// First minor release that translates the retired keys.
pub const POLICY_INTRODUCED_MINOR: (u64, u64) = (0, 58);

pub const REMOVED_OPENCODE_PATHS: [(&str, &str); 2] = [
    ("opencode.server_url", "permission.assert"),
    ("opencode.server_password_env", "permission.assert"),
];

/// Keys the per-repository data store leaves with nothing to do. User config
/// refuses them; project config drops them with their existing no-effect notice.
/// Mirrors the TypeScript `REMOVED_DATA_LAYER_PATHS`.
pub const REMOVED_DATA_LAYER_PATHS: [&str; 3] = [
    "views.callgraph_copies",
    "views.enabled",
    "worktree.ram_overlay",
];

/// `views.idle_checkout_hours` and the key that replaces it. It is translated
/// like any retired key (and the user file rewritten) until
/// [`DATA_LAYER_KEYS_REFUSED_FROM`], then refused.
pub const RETIRED_IDLE_CHECKOUT_HOURS: (&str, &str) =
    ("views.idle_checkout_hours", "idle.checkout_hours");

/// First release, as (major, minor), that refuses the keys in
/// [`DATA_LAYER_RETIRED_PATHS`] with `removed_config_key`. Earlier releases
/// translate or keep them, with a notice. Mirrors the TypeScript
/// `DATA_LAYER_KEYS_REFUSED_FROM`.
pub const DATA_LAYER_KEYS_REFUSED_FROM: (u64, u64) = (0, 65);

pub const CALLGRAPH_CHUNK_KEY: &str = "callgraph_chunk_size";
pub const CALLGRAPH_CHUNK_REFUSED_FROM: (u64, u64) = (0, 67);
pub const CALLGRAPH_CHUNK_NOTE: &str = "callgraph_chunk_size never had an effect, so the setting can be removed. From 0.67.0 AFT refuses a user config file that still sets it; run npx @cortexkit/aft doctor --fix.";

/// The remaining version-gated data-layer rename. The removed no-effect keys
/// already follow [`REFUSED_RETIRED_PATHS`]' user-only refusal policy.
pub const DATA_LAYER_RETIRED_PATHS: [&str; 1] = ["views.idle_checkout_hours"];

/// The (major, minor) release a version string names, ignoring the patch and
/// any pre-release or build suffix.
fn parse_release(version: &str) -> Option<(u64, u64)> {
    let mut parts = version.split('.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    Some((major, minor))
}

/// The (major, minor) release of this build, from the crate version.
pub fn running_release() -> (u64, u64) {
    #[cfg(any(test, feature = "test-support"))]
    if let Some(release) = RUNNING_RELEASE_OVERRIDE_FOR_TEST.with(|value| value.get()) {
        return release;
    }
    parse_release(env!("CARGO_PKG_VERSION")).unwrap_or((0, 0))
}

#[cfg(any(test, feature = "test-support"))]
thread_local! {
    static RUNNING_RELEASE_OVERRIDE_FOR_TEST: std::cell::Cell<Option<(u64, u64)>> = const {
        std::cell::Cell::new(None)
    };
}

/// Pin release-sensitive tests on this thread; dropping the guard restores the prior release.
#[cfg(any(test, feature = "test-support"))]
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn override_running_release_for_test(release: (u64, u64)) -> impl Drop {
    struct Reset {
        previous: Option<(u64, u64)>,
        _not_send: std::marker::PhantomData<std::rc::Rc<()>>,
    }
    impl Drop for Reset {
        fn drop(&mut self) {
            RUNNING_RELEASE_OVERRIDE_FOR_TEST.with(|value| value.set(self.previous));
        }
    }
    Reset {
        previous: RUNNING_RELEASE_OVERRIDE_FOR_TEST.with(|value| value.replace(Some(release))),
        _not_send: std::marker::PhantomData,
    }
}

/// `0.65.0` for [`DATA_LAYER_KEYS_REFUSED_FROM`], as notices print it.
fn refused_from_version() -> String {
    let (major, minor) = DATA_LAYER_KEYS_REFUSED_FROM;
    format!("{major}.{minor}.0")
}

/// Removed settings fail only in the base and first-level harness blocks.
pub fn removed_config_key_errors(map: &Map<String, Value>) -> Vec<String> {
    removed_config_key_errors_at(map, running_release())
}

/// [`removed_config_key_errors`] for one config file. Retired names in
/// [`REFUSED_RETIRED_PATHS`] and the prefixed tool names refuse
/// only the user file, which AFT rewrites itself. A project file belongs to
/// its repository, where a refusal would leave everyone who opens it without
/// AFT until a tracked file is edited, so those names are translated with a
/// notice there. Settings removed outright still refuse either file. Mirrors
/// the plugins' `removedConfigKeyErrors(..., refuseRetired)`.
pub fn removed_config_key_errors_for_file(
    map: &Map<String, Value>,
    user_file: bool,
) -> Vec<String> {
    removed_errors(map, running_release(), user_file)
}

/// [`removed_config_key_errors`] as the release `release` judges it: the
/// data-layer keys fail only from [`DATA_LAYER_KEYS_REFUSED_FROM`].
pub fn removed_config_key_errors_at(map: &Map<String, Value>, release: (u64, u64)) -> Vec<String> {
    removed_errors(map, release, true)
}

fn removed_errors(
    map: &Map<String, Value>,
    release: (u64, u64),
    refuse_retired: bool,
) -> Vec<String> {
    let mut errors = removed_block_key_errors(map, release, refuse_retired);
    if let Some(harnesses) = map.get("harnesses").and_then(Value::as_object) {
        for (name, block) in harnesses {
            if let Some(block) = block.as_object() {
                for error in removed_block_key_errors(block, release, refuse_retired) {
                    errors.push(error.replacen(
                        "removed_config_key:",
                        &format!("removed_config_key:harnesses.{name}."),
                        1,
                    ));
                }
            }
        }
    }
    errors
}

fn removed_block_key_errors(
    map: &Map<String, Value>,
    release: (u64, u64),
    refuse_retired: bool,
) -> Vec<String> {
    let mut errors = removed_opencode_key_errors(map);
    if refuse_retired {
        errors.extend(
            refused_retired_names(map).into_iter().map(|name| {
                format!("removed_config_key:{name}; run npx @cortexkit/aft doctor --fix")
            }),
        );
    }
    if refuse_retired && release >= DATA_LAYER_KEYS_REFUSED_FROM {
        for path in DATA_LAYER_RETIRED_PATHS {
            if has_path(map, path) {
                errors.push(format!(
                    "removed_config_key:{path}; run npx @cortexkit/aft doctor --fix"
                ));
            }
        }
    }
    if refuse_retired
        && release >= CALLGRAPH_CHUNK_REFUSED_FROM
        && map.contains_key(CALLGRAPH_CHUNK_KEY)
    {
        errors.push(format!(
            "removed_config_key:{CALLGRAPH_CHUNK_KEY}; run npx @cortexkit/aft doctor --fix"
        ));
    }
    errors
}

fn removed_opencode_key_errors(map: &Map<String, Value>) -> Vec<String> {
    let Some(opencode) = map.get("opencode").and_then(Value::as_object) else {
        return Vec::new();
    };
    let mut errors = Vec::new();
    for (path, _) in REMOVED_OPENCODE_PATHS {
        if opencode.contains_key(path.strip_prefix("opencode.").unwrap()) {
            errors.push(format!(
                "removed_config_key:{path}; run npx @cortexkit/aft doctor --fix"
            ));
        }
    }
    errors
}

/// Retired names refused in user config, in the order a
/// refusal lists them. A config that still sets one is refused with
/// `removed_config_key`; `doctor --fix` (or the automatic rewrite of the user
/// file) replaces it with its current equivalent or deletes it. Names retired
/// more recently (`bridge.hang_threshold`) are not
/// listed: they are still translated, with a notice. Mirrors the TypeScript
/// `REFUSED_RETIRED_PATHS`.
pub const REFUSED_RETIRED_PATHS: [&str; 23] = [
    "sandbox.enabled",
    "tool_surface",
    "hoist_builtin_tools",
    "enabled",
    "search_index",
    "experimental_search_index",
    "semantic_search",
    "experimental_semantic_search",
    "callgraph_store",
    "github.enabled",
    "gh_read",
    "gh_shim.enabled",
    "experimental_lsp_ty",
    "experimental_bash_rewrite",
    "experimental_bash_compress",
    "experimental_bash_background",
    "experimental.bash",
    "idle.lsp_ttl_minutes",
    "inspect.tier2_soft_deadline_ms",
    "inspect.max_drill_down_items",
    "views.callgraph_copies",
    "views.enabled",
    "worktree.ram_overlay",
];

/// The refused retired names one block sets: the paths in
/// [`REFUSED_RETIRED_PATHS`], then each prefixed tool name
/// ([`LEGACY_TOOL_ALIASES`]) its `disabled_tools` list contains, once each and
/// by its bare name, as the retired-key notices list it.
fn refused_retired_names(map: &Map<String, Value>) -> Vec<String> {
    let mut names: Vec<String> = REFUSED_RETIRED_PATHS
        .iter()
        .filter(|path| has_path(map, path))
        .map(|path| (*path).to_string())
        .collect();
    if let Some(Value::Array(entries)) = map.get("disabled_tools") {
        for name in entries.iter().filter_map(Value::as_str) {
            if legacy_tool_alias(name).is_some() && !names.iter().any(|known| known == name) {
                names.push(name.to_string());
            }
        }
    }
    names
}

/// Retired top-level (or nested) config paths and their replacement.
pub const RETIRED_PATHS: [(&str, &str); 15] = [
    ("sandbox.enabled", "sandbox.profile"),
    ("tool_surface", "disabled_tools"),
    ("hoist_builtin_tools", "disabled_tools"),
    ("enabled", "disabled_tools"),
    ("search_index", "indexes.trigram"),
    ("experimental_search_index", "indexes.trigram"),
    ("semantic_search", "indexes.semantic"),
    ("experimental_semantic_search", "indexes.semantic"),
    ("callgraph_store", "indexes.callgraph"),
    ("github.enabled", "github.read,github.write,github.shim"),
    ("experimental_lsp_ty", "experimental.lsp_ty"),
    ("experimental_bash_rewrite", "bash.rewrite"),
    ("experimental_bash_compress", "bash.compress"),
    ("experimental_bash_background", "bash.background"),
    ("experimental.bash", "bash"),
];

/// Retired inspect/LSP paths and their replacement. `idle.lsp_ttl_minutes`
/// moves to `lsp.idle_minutes`; the two inspect keys never had an effect and
/// are dropped.
pub const REMOVED_INSPECT_LSP_PATHS: [(&str, &str); 3] = [
    ("idle.lsp_ttl_minutes", "lsp.idle_minutes"),
    (
        "inspect.tier2_soft_deadline_ms",
        "inspect.tier2_pass_timeout_ms",
    ),
    ("inspect.max_drill_down_items", "aft_inspect.topK"),
];

/// Retired paths that no longer do anything and have no replacement. They are
/// dropped on load and removed by `doctor --fix`, and the notice says so.
/// `bridge.hang_threshold` counted request timeouts toward restarting the
/// bridge; a timeout now fails only its own request, and the bridge's hang
/// watchdog judges a stuck child by its silence instead.
/// `inspect.tier2_idle_minutes` delayed the whole-repository analyses the
/// OpenCode plugin started when a session went idle; those analyses now run
/// only when `aft_inspect` is called.
pub const REMOVED_NO_EFFECT_PATHS: [(&str, &str); 2] = [
    ("bridge.hang_threshold", ""),
    ("inspect.tier2_idle_minutes", ""),
];

/// Notice sentence for a retired `bridge.hang_threshold`. Mirrors the
/// TypeScript `RETIRED_HANG_THRESHOLD_NOTE`.
pub const RETIRED_HANG_THRESHOLD_NOTE: &str = "bridge.hang_threshold no longer does anything: a timed-out request no longer restarts AFT, so the setting can be removed.";

/// Index leaf, its immediate legacy key and (when one exists) the older
/// experimental alias, in precedence order after the canonical leaf.
pub const INDEX_INPUTS: [(&str, &str, Option<&str>); 3] = [
    ("trigram", "search_index", Some("experimental_search_index")),
    (
        "semantic",
        "semantic_search",
        Some("experimental_semantic_search"),
    ),
    ("callgraph", "callgraph_store", None),
];

/// Whether a raw tool name is a protected slot a project config may not disable.
pub fn is_project_protected_tool(name: &str) -> bool {
    name == "aft_safety" || HOST_TOOL_NAMES.contains(&name)
}

/// Whether a name belongs to the complete canonical tool inventory.
pub fn is_known_tool(name: &str) -> bool {
    CANONICAL_TOOLS.contains(&name)
}

/// Canonical name for a historical prefixed alias, if `name` is one.
pub fn legacy_tool_alias(name: &str) -> Option<&'static str> {
    LEGACY_TOOL_ALIASES
        .iter()
        .find(|(alias, _)| *alias == name)
        .map(|(_, canonical)| *canonical)
}

/// Literal disabled set a legacy `tool_surface` value translates to. The sets
/// are the canonical inventory minus what that surface registered on OpenCode
/// with hoisting and every gate on.
pub fn surface_disables(surface: &str) -> Option<Vec<&'static str>> {
    match surface {
        "all" => Some(Vec::new()),
        "recommended" => Some(vec!["aft_callgraph", "aft_delete", "aft_move"]),
        "minimal" => Some(
            CANONICAL_TOOLS
                .iter()
                .copied()
                .filter(|name| !matches!(*name, "aft_outline" | "aft_zoom" | "aft_safety"))
                .collect(),
        ),
        _ => None,
    }
}

/// A non-fatal translation notice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TranslationWarning {
    pub code: &'static str,
    pub key: String,
    pub message: String,
}

/// Result of translating one raw configuration document.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DocumentTranslation {
    pub warnings: Vec<TranslationWarning>,
    /// True when any retired key or alias was supplied (a migration notice applies).
    pub legacy_input: bool,
    /// Every retired key the document supplied, sorted and de-duplicated. A
    /// key inside a harness block carries its `harnesses.<id>.` prefix; a
    /// prefixed tool name inside `disabled_tools` is listed by that name.
    pub retired_keys: Vec<String>,
}

/// Record one supplied retired key of the block whose key prefix is `prefix`.
fn record_retired(out: &mut DocumentTranslation, prefix: &str, key: &str) {
    out.legacy_input = true;
    out.retired_keys.push(format!("{prefix}{key}"));
}

fn superseded(out: &mut DocumentTranslation, key: String, message: String) {
    out.warnings.push(TranslationWarning {
        code: "superseded_legacy_config",
        key,
        message,
    });
}

fn nested_bool(map: &Map<String, Value>, container: &str, leaf: &str) -> Option<bool> {
    map.get(container)?.as_object()?.get(leaf)?.as_bool()
}

fn has_path(map: &Map<String, Value>, path: &str) -> bool {
    match path.split_once('.') {
        Some((container, leaf)) => map
            .get(container)
            .and_then(Value::as_object)
            .is_some_and(|inner| inner.contains_key(leaf)),
        None => map.contains_key(path),
    }
}

/// Whether a false retained runtime gate (backup/inspect/bash) is supplied.
fn false_runtime_gates(map: &Map<String, Value>) -> Vec<&'static str> {
    let mut gates = Vec::new();
    if nested_bool(map, "backup", "enabled") == Some(false) {
        gates.push("backup.enabled");
    }
    if nested_bool(map, "inspect", "enabled") == Some(false) {
        gates.push("inspect.enabled");
    }
    match map.get("bash") {
        Some(Value::Bool(false)) => gates.push("bash"),
        Some(Value::Object(bash)) if bash.get("enabled") == Some(&Value::Bool(false)) => {
            gates.push("bash.enabled");
        }
        _ => {}
    }
    gates
}

/// Translate the GitHub enable aliases (`gh_read.enabled`, `gh_shim.enabled`)
/// of one block into `github.read` / `github.shim`, exactly as `doctor --fix`
/// rewrites them. Precedence: the canonical leaf, then the `false` that a
/// `github.enabled: false` in the same block sets for every leaf, then the
/// alias. The aliases are removed; `gh_shim.binary_path` is kept.
fn translate_github_aliases(
    map: &mut Map<String, Value>,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    let master_off = nested_bool(map, "github", "enabled") == Some(false);
    for (alias, leaf, retired_key) in [
        ("gh_read", "read", "gh_read"),
        ("gh_shim", "shim", "gh_shim.enabled"),
    ] {
        let supplied = if alias == "gh_read" {
            map.contains_key(alias)
        } else {
            map.get(alias)
                .and_then(Value::as_object)
                .is_some_and(|shim| shim.contains_key("enabled"))
        };
        if !supplied {
            continue;
        }
        record_retired(out, prefix, retired_key);
        let alias_value = map
            .get(alias)
            .and_then(|value| value.get("enabled"))
            .and_then(Value::as_bool);
        if let Some(value) = alias_value {
            match nested_bool(map, "github", leaf) {
                Some(canonical) => {
                    if canonical != value {
                        superseded(
                            out,
                            format!("{prefix}{retired_key}"),
                            format!("{alias}.enabled={value} is ignored because github.{leaf}={canonical} is set"),
                        );
                    }
                }
                None if master_off => {
                    if value {
                        superseded(
                            out,
                            format!("{prefix}{retired_key}"),
                            format!("{alias}.enabled=true is ignored because github.enabled=false switches github.{leaf} off"),
                        );
                    }
                }
                None => {
                    let github = map
                        .entry("github".to_string())
                        .or_insert_with(|| Value::Object(Map::new()));
                    if let Value::Object(github) = github {
                        github.insert(leaf.to_string(), Value::Bool(value));
                    }
                }
            }
        }
        if alias == "gh_read" {
            map.remove(alias);
        } else if let Some(Value::Object(shim)) = map.get_mut(alias) {
            shim.remove("enabled");
            if shim.is_empty() {
                map.remove(alias);
            }
        }
    }
}

/// Remove `container.leaf` when present, dropping the container if that left
/// it empty. Returns the removed value.
fn take_nested(map: &mut Map<String, Value>, container: &str, leaf: &str) -> Option<Value> {
    let inner = map.get_mut(container)?.as_object_mut()?;
    let value = inner.remove(leaf)?;
    if inner.is_empty() {
        map.remove(container);
    }
    Some(value)
}

/// Largest integer a JSON number keeps exactly in JavaScript
/// (`Number.MAX_SAFE_INTEGER`).
const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_991;

/// The whole number a JSON value spells, the way JavaScript's
/// `Number.isSafeInteger` reads the parsed value: `12` and `12.0` are both 12,
/// while `12.5`, non-numbers and integers beyond the exactly representable
/// range are not whole numbers. Keeps the Rust and TypeScript translations
/// equal, since JavaScript cannot tell `12.0` from `12`.
fn safe_whole_number(value: &Value) -> Option<i64> {
    if let Some(number) = value.as_i64() {
        return (number.unsigned_abs() <= MAX_SAFE_INTEGER as u64).then_some(number);
    }
    let number = value.as_f64()?;
    (number.is_finite() && number.fract() == 0.0 && number.abs() <= MAX_SAFE_INTEGER as f64)
        .then_some(number as i64)
}

/// The `lsp.idle_minutes` value a retired `idle.lsp_ttl_minutes` value
/// becomes: a whole number clamped to the current range, or the default for
/// anything else. Shared by load-time translation and `doctor --fix`; mirrors
/// the TypeScript translation.
pub fn retired_lsp_idle_minutes(value: &Value) -> i64 {
    safe_whole_number(value)
        .unwrap_or(i64::from(crate::config::DEFAULT_LSP_IDLE_MINUTES))
        .clamp(
            i64::from(crate::config::MIN_LSP_IDLE_MINUTES),
            i64::from(crate::config::MAX_LSP_IDLE_MINUTES),
        )
}

/// Translate the retired inspect/LSP keys of one block, exactly as
/// `doctor --fix` rewrites them: `idle.lsp_ttl_minutes` becomes
/// `lsp.idle_minutes` (clamped to its range) unless that is already set, and
/// the two inspect keys that never had an effect are dropped.
fn translate_inspect_lsp_paths(
    map: &mut Map<String, Value>,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    if let Some(value) = take_nested(map, "idle", "lsp_ttl_minutes") {
        record_retired(out, prefix, "idle.lsp_ttl_minutes");
        if map
            .get("lsp")
            .and_then(|lsp| lsp.get("idle_minutes"))
            .is_some()
        {
            superseded(
                out,
                format!("{prefix}idle.lsp_ttl_minutes"),
                "idle.lsp_ttl_minutes is ignored because lsp.idle_minutes is set".to_string(),
            );
        } else {
            let minutes = retired_lsp_idle_minutes(&value);
            let lsp = map
                .entry("lsp".to_string())
                .or_insert_with(|| Value::Object(Map::new()));
            if let Value::Object(lsp) = lsp {
                lsp.insert("idle_minutes".to_string(), Value::from(minutes));
            }
        }
    }
    for leaf in ["tier2_soft_deadline_ms", "max_drill_down_items"] {
        if take_nested(map, "inspect", leaf).is_some() {
            record_retired(out, prefix, &format!("inspect.{leaf}"));
        }
    }
}

/// Drop the retired keys that no longer do anything
/// ([`REMOVED_NO_EFFECT_PATHS`]), exactly as `doctor --fix` removes them.
fn drop_no_effect_paths(map: &mut Map<String, Value>, prefix: &str, out: &mut DocumentTranslation) {
    for (path, _) in REMOVED_NO_EFFECT_PATHS {
        if let Some((container, leaf)) = path.split_once('.') {
            if take_nested(map, container, leaf).is_some() {
                record_retired(out, prefix, path);
            }
        }
    }
}

/// The `idle.checkout_hours` value a retired `views.idle_checkout_hours`
/// value becomes: a whole number clamped to the current range, or the default
/// for anything else. Shared by load-time translation and `doctor --fix`;
/// mirrors the TypeScript translation.
pub fn retired_idle_checkout_hours(value: &Value) -> i64 {
    safe_whole_number(value)
        .unwrap_or(i64::from(crate::config::DEFAULT_IDLE_CHECKOUT_HOURS))
        .clamp(
            i64::from(crate::config::MIN_IDLE_CHECKOUT_HOURS),
            i64::from(crate::config::MAX_IDLE_CHECKOUT_HOURS),
        )
}

/// Translate the keys the data-layer cutover retires in one block:
/// `views.idle_checkout_hours` becomes `idle.checkout_hours` (clamped to its
/// range) unless that is already set, exactly as `doctor --fix` rewrites it.
/// Removed no-effect keys are dropped before strict schema validation.
fn translate_data_layer_paths(
    map: &mut Map<String, Value>,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    if map.remove(CALLGRAPH_CHUNK_KEY).is_some() {
        record_retired(out, prefix, CALLGRAPH_CHUNK_KEY);
    }
    let (retired, replacement) = RETIRED_IDLE_CHECKOUT_HOURS;
    if let Some(value) = take_nested(map, "views", "idle_checkout_hours") {
        record_retired(out, prefix, retired);
        if map
            .get("idle")
            .and_then(|idle| idle.get("checkout_hours"))
            .is_some()
        {
            superseded(
                out,
                format!("{prefix}{retired}"),
                format!("{retired} is ignored because {replacement} is set"),
            );
        } else {
            let hours = retired_idle_checkout_hours(&value);
            let idle = map
                .entry("idle".to_string())
                .or_insert_with(|| Value::Object(Map::new()));
            if let Value::Object(idle) = idle {
                idle.insert("checkout_hours".to_string(), Value::from(hours));
            }
        }
    }
    for path in REMOVED_DATA_LAYER_PATHS {
        let (container, leaf) = path.split_once('.').unwrap();
        if take_nested(map, container, leaf).is_some() {
            record_retired(out, prefix, path);
        }
    }
}

/// What a retired `enabled: false` no longer does. The translation only hides
/// tools; indexing continues, so a user who relied on it to keep AFT out of a
/// repository must also switch the indexes off. Mirrors the TypeScript
/// `RETIRED_ENABLED_FALSE_INDEXES_NOTE`.
pub const RETIRED_ENABLED_FALSE_INDEXES_NOTE: &str = "enabled: false no longer turns AFT off: it is translated to disabling every tool, but the trigram, semantic and callgraph indexes still build. To keep AFT from indexing this repository, also set indexes.trigram, indexes.semantic and indexes.callgraph to false.";

/// Translate flat experimental aliases to nested settings and move the
/// `experimental.bash` block to top-level `bash`. Graduating a block that sets
/// a feature flag explicitly sets its omitted flags to false; otherwise the
/// current default-on bash settings would silently enable them.
fn translate_experimental_paths(
    map: &mut Map<String, Value>,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    for (old_key, leaf) in [
        ("experimental_lsp_ty", "lsp_ty"),
        ("experimental_bash_rewrite", "rewrite"),
        ("experimental_bash_compress", "compress"),
        ("experimental_bash_background", "background"),
    ] {
        let Some(value) = map.remove(old_key) else {
            continue;
        };
        record_retired(out, prefix, old_key);
        let experimental = map
            .entry("experimental")
            .or_insert_with(|| Value::Object(Map::new()));
        if !experimental.is_object() {
            *experimental = Value::Object(Map::new());
        }
        let experimental = experimental.as_object_mut().unwrap();
        let destination = if leaf == "lsp_ty" {
            experimental
        } else {
            let bash = experimental
                .entry("bash")
                .or_insert_with(|| Value::Object(Map::new()));
            if !bash.is_object() {
                *bash = Value::Object(Map::new());
            }
            bash.as_object_mut().unwrap()
        };
        if destination.contains_key(leaf) {
            let path = if leaf == "lsp_ty" {
                leaf.to_string()
            } else {
                format!("bash.{leaf}")
            };
            superseded(
                out,
                old_key.to_string(),
                format!("{old_key} is ignored because experimental.{path} is already set"),
            );
        } else {
            destination.insert(leaf.to_string(), value);
        }
    }

    let Some(experimental) = map.get("experimental").and_then(Value::as_object) else {
        return;
    };
    let Some(legacy) = experimental.get("bash") else {
        return;
    };
    if let Some(legacy) = legacy.as_object() {
        if legacy.is_empty() {
            record_retired(out, prefix, "experimental.bash");
        }
        for leaf in legacy.keys() {
            record_retired(out, prefix, &format!("experimental.bash.{leaf}"));
        }
        if map.contains_key("bash") {
            superseded(
                out,
                "experimental.bash".to_string(),
                "experimental.bash is ignored because top-level \"bash\" is already set"
                    .to_string(),
            );
        } else {
            let mut bash = Map::new();
            // A block with only reminder settings never opted into the
            // feature flags, so they keep their current defaults instead of
            // being written as false.
            if ["rewrite", "compress", "background"]
                .iter()
                .any(|leaf| legacy.contains_key(*leaf))
            {
                for leaf in ["rewrite", "compress", "background"] {
                    bash.insert(
                        leaf.to_string(),
                        Value::Bool(legacy.get(leaf) == Some(&Value::Bool(true))),
                    );
                }
            }
            for leaf in [
                "long_running_reminder_enabled",
                "long_running_reminder_interval_ms",
            ] {
                if let Some(value) = legacy.get(leaf) {
                    bash.insert(leaf.to_string(), value.clone());
                }
            }
            if !bash.is_empty() {
                map.insert("bash".to_string(), Value::Object(bash));
            }
        }
    } else {
        record_retired(out, prefix, "experimental.bash");
    }
    let experimental = map
        .get_mut("experimental")
        .unwrap()
        .as_object_mut()
        .unwrap();
    experimental.remove("bash");
    if experimental.is_empty() {
        map.remove("experimental");
    }
}

/// Translate the retired keys of one tier/harness block in place.
///
/// The false runtime gates (`backup.enabled`, `inspect.enabled`, `bash`,
/// `bash.enabled`) are current keys and only switch their behaviour off; they
/// never remove a tool registration. Only `disabled_tools` does that.
fn translate_block(
    map: &mut Map<String, Value>,
    // True only for the user file's base block, the one place the
    // absent-base default disables may be added.
    user_base: bool,
    block_label: &str,
    out: &mut DocumentTranslation,
) {
    let prefix = if block_label == "base" {
        String::new()
    } else {
        format!("{block_label}.")
    };
    translate_github_aliases(map, &prefix, out);
    translate_inspect_lsp_paths(map, &prefix, out);
    translate_data_layer_paths(map, &prefix, out);
    drop_no_effect_paths(map, &prefix, out);
    translate_experimental_paths(map, &prefix, out);
    if let Some(Value::Object(sandbox)) = map.get_mut("sandbox") {
        if let Some(enabled) = sandbox.remove("enabled") {
            record_retired(out, &prefix, "sandbox.enabled");
            match enabled.as_bool() {
                Some(enabled) => {
                    let profile = sandbox
                        .entry("profile")
                        .or_insert_with(|| Value::Object(Map::new()));
                    if let Value::Object(profile) = profile {
                        for (role, mode) in [
                            ("worker", if enabled { "strict" } else { "relaxed" }),
                            ("head", if enabled { "strict" } else { "off" }),
                        ] {
                            if profile.get(role).is_some_and(|value| value != mode) {
                                superseded(out, format!("{prefix}sandbox.enabled"), format!("sandbox.profile.{role} takes precedence over sandbox.enabled"));
                            }
                            profile
                                .entry(role)
                                .or_insert_with(|| Value::String(mode.to_string()));
                        }
                    }
                }
                None => out.warnings.push(TranslationWarning {
                    code: "invalid_legacy_config",
                    key: format!("{prefix}sandbox.enabled"),
                    message: "Ignoring non-boolean sandbox.enabled; use sandbox.profile"
                        .to_string(),
                }),
            }
        }
    }

    // Canonicalize the retired `aft_`-prefixed host tool names (for example
    // `aft_read` -> `read`) inside the disabled list.
    let mut explicit_list = None;
    if let Some(Value::Array(entries)) = map.get("disabled_tools") {
        if entries.iter().all(Value::is_string) {
            let mut canonical = Vec::with_capacity(entries.len());
            let mut aliases = Vec::new();
            for name in entries.iter().filter_map(Value::as_str) {
                match legacy_tool_alias(name) {
                    Some(host) => {
                        aliases.push(name.to_string());
                        canonical.push(host.to_string());
                    }
                    None => canonical.push(name.to_string()),
                }
            }
            for alias in aliases {
                record_retired(out, &prefix, &alias);
            }
            explicit_list = Some(canonical);
        }
    }

    // `experimental.bash` was already recorded, leaf by leaf, and removed by
    // `translate_experimental_paths`.
    for (path, _) in RETIRED_PATHS {
        if has_path(map, path) {
            record_retired(out, &prefix, path);
        }
    }
    let gates = false_runtime_gates(map);

    if let Some(list) = &explicit_list {
        map.insert(
            "disabled_tools".to_string(),
            Value::Array(list.iter().cloned().map(Value::String).collect()),
        );
    }

    // Index switches: canonical leaf, then immediate legacy name, then the
    // older experimental alias.
    for (leaf, legacy, experimental) in INDEX_INPUTS {
        let canonical = map
            .get("indexes")
            .and_then(Value::as_object)
            .and_then(|indexes| indexes.get(leaf))
            .and_then(Value::as_bool);
        let legacy_value = map.remove(legacy);
        let experimental_value = experimental.and_then(|key| map.remove(key));
        let mut chosen = None;
        for (key, value) in [
            (Some(legacy), legacy_value),
            (experimental, experimental_value),
        ] {
            let (Some(key), Some(value)) = (key, value) else {
                continue;
            };
            let Some(value) = value.as_bool() else {
                out.warnings.push(TranslationWarning {
                    code: "invalid_legacy_config",
                    key: key.to_string(),
                    message: format!("Ignoring non-boolean {key}; use indexes.{leaf}"),
                });
                continue;
            };
            if let Some(canonical) = canonical {
                if canonical != value {
                    superseded(
                        out,
                        key.to_string(),
                        format!(
                            "{key}={value} is ignored because indexes.{leaf}={canonical} is set"
                        ),
                    );
                }
            } else if let Some(previous) = chosen {
                if previous != value {
                    superseded(
                        out,
                        key.to_string(),
                        format!(
                            "{key}={value} is ignored because {legacy}={previous} takes precedence"
                        ),
                    );
                }
            } else {
                chosen = Some(value);
            }
        }
        if canonical.is_none() {
            if let Some(value) = chosen {
                let indexes = map
                    .entry("indexes".to_string())
                    .or_insert_with(|| Value::Object(Map::new()));
                if let Value::Object(indexes) = indexes {
                    indexes.insert(leaf.to_string(), Value::Bool(value));
                }
            }
        }
    }

    // github.enabled: false switches every leaf off unless the leaf is explicit.
    if let Some(Value::Object(github)) = map.get_mut("github") {
        if let Some(master) = github.remove("enabled") {
            if master == Value::Bool(false) {
                let mut notes = Vec::new();
                for leaf in ["read", "write", "shim"] {
                    match github.get(leaf) {
                        None => {
                            github.insert(leaf.to_string(), Value::Bool(false));
                        }
                        Some(Value::Bool(true)) => notes.push(format!(
                            "github.enabled=false is ignored for github.{leaf} because github.{leaf}=true is set"
                        )),
                        Some(_) => {}
                    }
                }
                for note in notes {
                    superseded(out, "github.enabled".to_string(), note);
                }
            }
        }
    }

    // Registration generators.
    let surface = map.remove("tool_surface");
    let hoist = map.remove("hoist_builtin_tools");
    let enabled = map.remove("enabled");
    let mut generated: BTreeSet<String> = BTreeSet::new();
    let mut explicit_surface = false;
    if let Some(surface) = surface {
        match surface.as_str().and_then(surface_disables) {
            Some(names) => {
                explicit_surface = true;
                generated.extend(names.into_iter().map(str::to_string));
            }
            None => out.warnings.push(TranslationWarning {
                code: "invalid_legacy_config",
                key: "tool_surface".to_string(),
                message: "Ignoring unknown tool_surface value; use disabled_tools".to_string(),
            }),
        }
    }
    if hoist == Some(Value::Bool(false)) {
        generated.extend(HOST_TOOL_NAMES.iter().map(|name| (*name).to_string()));
    }
    if enabled == Some(Value::Bool(false)) {
        generated.extend(CANONICAL_TOOLS.iter().map(|name| (*name).to_string()));
        out.warnings.push(TranslationWarning {
            code: "legacy_enabled_false_indexes_still_build",
            key: if block_label == "base" {
                "enabled".to_string()
            } else {
                format!("{block_label}.enabled")
            },
            message: RETIRED_ENABLED_FALSE_INDEXES_NOTE.to_string(),
        });
    }

    if !gates.is_empty() && explicit_list.is_none() {
        out.warnings.push(TranslationWarning {
            code: "legacy_runtime_gate_runtime_only",
            key: block_label.to_string(),
            message: format!(
                "{} now restricts runtime behavior only and no longer removes tool registrations; list tools in disabled_tools to unregister them",
                gates.join(", ")
            ),
        });
    }

    if explicit_list.is_some() {
        if explicit_surface || !generated.is_empty() {
            superseded(
                out,
                block_label.to_string(),
                "disabled_tools is set explicitly, so legacy tool_surface/hoist_builtin_tools/enabled do not change registration".to_string(),
            );
        }
        return;
    }

    let list = if user_base {
        if explicit_surface {
            Some(generated)
        } else if !generated.is_empty() {
            let mut with_default = generated;
            with_default.extend(
                DEFAULT_DISABLED_TOOLS
                    .iter()
                    .map(|name| (*name).to_string()),
            );
            Some(with_default)
        } else {
            None
        }
    } else {
        (!generated.is_empty()).then_some(generated)
    };
    if let Some(list) = list {
        map.insert(
            "disabled_tools".to_string(),
            Value::Array(list.into_iter().map(Value::String).collect()),
        );
    }
}

/// Which trust tier a raw document belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DocumentTier {
    User,
    Project,
}

/// Translate every block (base plus each `harnesses.<id>` object) of one raw
/// configuration document, in place.
///
/// Only the user file's base block receives the absent-base default
/// (`aft_move`/`aft_delete`) when its legacy keys generate disables: that
/// default applies once, at user-base resolution. A project file's base block
/// contributes only the names its own legacy keys imply, so a legacy key in a
/// repository can never re-disable tools the user enabled. The translated
/// values are current keys, so the resolver's project-tier rules apply to them
/// exactly as if the file had spelled them out.
pub fn translate_document(map: &mut Map<String, Value>, tier: DocumentTier) -> DocumentTranslation {
    let mut out = DocumentTranslation::default();
    translate_runon_enabled(map, tier, "", &mut out);
    translate_block(map, tier == DocumentTier::User, "base", &mut out);
    translate_shapes(map, tier, "", &mut out);
    if let Some(Value::Object(harnesses)) = map.get_mut("harnesses") {
        for (name, block) in harnesses.iter_mut() {
            if let Value::Object(block) = block {
                translate_runon_enabled(block, tier, &format!("harnesses.{name}."), &mut out);
                translate_block(block, false, &format!("harnesses.{name}"), &mut out);
                translate_shapes(block, tier, &format!("harnesses.{name}."), &mut out);
            }
        }
    }
    out.retired_keys.sort();
    out.retired_keys.dedup();
    out
}

fn translate_shapes(
    map: &mut Map<String, Value>,
    tier: DocumentTier,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    for edit in settings_shape::edits(map, tier, prefix.is_empty()) {
        record_retired(out, prefix, edit.notice);
        edit.apply(map);
    }
}

/// The retired bash gate was user-only. Projects drop it without changing
/// their current remote-run setting; an explicit false in a user block keeps
/// that block's remote runs off. Absence already uses the current setting.
fn translate_runon_enabled(
    map: &mut Map<String, Value>,
    tier: DocumentTier,
    prefix: &str,
    out: &mut DocumentTranslation,
) {
    // Keep an empty bash object: its normal defaults still take precedence
    // over retired experimental bash flags, unlike an absent bash setting.
    if let Some(value) = map
        .get_mut("bash")
        .and_then(Value::as_object_mut)
        .and_then(|bash| bash.remove("runon_enabled"))
    {
        record_retired(out, prefix, "bash.runon_enabled");
        if tier == DocumentTier::User && value == Value::Bool(false) {
            let remote = map
                .entry("remote_exec".to_string())
                .or_insert_with(|| Value::Object(Map::new()));
            if let Value::Object(remote) = remote {
                remote.insert("enabled".to_string(), Value::Bool(false));
            }
        }
    }
}

/// Whether a translation carries the retired `enabled: false` note.
fn has_enabled_false_note(translation: &DocumentTranslation) -> bool {
    translation
        .warnings
        .iter()
        .any(|warning| warning.code == "legacy_enabled_false_indexes_still_build")
}

/// Retired keys that no longer do anything, each with the notice sentence that
/// says so. Nothing is applied for them, so a notice never counts them among
/// the keys whose "current equivalents" were applied. Kept in key order;
/// mirrors the TypeScript `NO_EFFECT_RETIRED_NOTES`.
pub const NO_EFFECT_RETIRED_NOTES: [(&str, &str); 4] = [
    ("bridge.hang_threshold", RETIRED_HANG_THRESHOLD_NOTE),
    (
        "inspect.max_drill_down_items",
        "inspect.max_drill_down_items never had an effect, so the setting can be removed.",
    ),
    (
        "inspect.tier2_idle_minutes",
        "inspect.tier2_idle_minutes no longer does anything: AFT runs whole-repository analyses only when aft_inspect is called, so the setting can be removed.",
    ),
    (
        "inspect.tier2_soft_deadline_ms",
        "inspect.tier2_soft_deadline_ms never had an effect, so the setting can be removed.",
    ),
];

/// The no-effect sentence for a retired key, which may carry a
/// `harnesses.<id>.` prefix.
fn no_effect_note(key: &str) -> Option<&'static str> {
    if names_path(key, CALLGRAPH_CHUNK_KEY) {
        return Some(CALLGRAPH_CHUNK_NOTE);
    }
    NO_EFFECT_RETIRED_NOTES
        .iter()
        .chain(REMOVED_DATA_LAYER_NOTES.iter())
        .find(|(path, _)| names_path(key, path))
        .map(|(_, note)| *note)
}

/// Whether `key`, which may carry a `harnesses.<id>.` prefix, is `path`.
fn names_path(key: &str, path: &str) -> bool {
    key == path
        || key
            .strip_suffix(path)
            .is_some_and(|prefix| prefix.ends_with('.'))
}

/// Why each removed data-layer key no longer does anything, in
/// [`REMOVED_DATA_LAYER_PATHS`] order. Mirrors the TypeScript
/// `REMOVED_DATA_LAYER_NOTES`.
pub const REMOVED_DATA_LAYER_NOTES: [(&str, &str); 3] = [
    (
        "views.callgraph_copies",
        "views.callgraph_copies is gone because no checkout borrows another checkout's call graph any more.",
    ),
    (
        "views.enabled",
        "views.enabled is gone because AFT now keeps one index store per repository and always uses it.",
    ),
    (
        "worktree.ram_overlay",
        "worktree.ram_overlay is gone because every worktree now has its own index state, so there is no borrowed index to patch in memory.",
    ),
];

/// The sentence saying a user file that still names `views.idle_checkout_hours`
/// will be refused. Mirrors the TypeScript `retiredIdleCheckoutHoursNote`.
pub fn retired_idle_checkout_hours_note() -> String {
    let (retired, replacement) = RETIRED_IDLE_CHECKOUT_HOURS;
    format!(
        "{retired} is now {replacement}; from {} AFT refuses a user config file that still sets {retired}.",
        refused_from_version()
    )
}

/// Build a retired-key notice. `with_equivalents` renders the sentence about
/// keys whose current equivalents were applied, given those keys joined by
/// ", "; it is used only when there are such keys. The sentences for keys
/// that no longer do anything follow, once each. A file whose only retired
/// keys have no effect gets just those sentences. `still_in_file` is true
/// when the file keeps the retired keys (it was not rewritten), so the notice
/// warns about keys a later release refuses.
fn retired_keys_notice(
    translation: &DocumentTranslation,
    still_in_file: bool,
    with_equivalents: impl FnOnce(&str) -> String,
) -> String {
    let mut equivalents = Vec::new();
    let mut notes: Vec<&str> = Vec::new();
    for key in &translation.retired_keys {
        match no_effect_note(key) {
            Some(note) => {
                if !notes.contains(&note) {
                    notes.push(note);
                }
            }
            None => equivalents.push(key.as_str()),
        }
    }
    notes.sort_by_key(|note| {
        NO_EFFECT_RETIRED_NOTES
            .iter()
            .chain(REMOVED_DATA_LAYER_NOTES.iter())
            .position(|(_, known)| known == note)
    });
    let mut parts = Vec::new();
    if !equivalents.is_empty() {
        parts.push(with_equivalents(&equivalents.join(", ")));
        if has_enabled_false_note(translation) {
            parts.push(RETIRED_ENABLED_FALSE_INDEXES_NOTE.to_string());
        }
        if still_in_file
            && equivalents
                .iter()
                .any(|key| names_path(key, RETIRED_IDLE_CHECKOUT_HOURS.0))
        {
            parts.push(retired_idle_checkout_hours_note());
        }
    }
    parts.extend(notes.into_iter().map(str::to_string));
    parts.join(" ")
}

/// Notice for a project config file whose retired keys were translated in
/// memory. The file itself is never written: it is shared through the repository. Mirrors the
/// TypeScript `legacyConfigNoticeMessage`.
pub fn project_retired_keys_notice(path: &str, translation: &DocumentTranslation) -> String {
    retired_keys_notice(translation, true, |keys| {
        format!(
            "{path} uses retired keys ({keys}); AFT applied their current equivalents, with the same limits a project config has for those keys. Run `npx @cortexkit/aft doctor --fix` to update the file."
        )
    })
}

/// Notice for the user config file after AFT rewrote its retired keys. It
/// names only the keys the rewrite replaced. Removed data-layer keys remain
/// in the file for the loader to refuse until an explicit doctor fix removes them.
pub fn user_config_migrated_notice(
    path: &str,
    backup: &str,
    translation: &DocumentTranslation,
) -> String {
    retired_keys_notice(translation, false, |keys| {
        format!(
            "AFT updated {path}: its retired keys ({keys}) now use their current equivalents. The previous file is saved as {backup}."
        )
    })
}

/// Notice for the user config file when AFT could not rewrite it and applied
/// the current equivalents in memory instead.
pub fn user_config_not_migrated_notice(
    path: &str,
    reason: &str,
    translation: &DocumentTranslation,
) -> String {
    retired_keys_notice(translation, true, |keys| {
        format!(
            "{path} uses retired keys ({keys}); AFT applied their current equivalents but could not update the file ({reason}). Run `npx @cortexkit/aft doctor --fix` to update it."
        )
    })
}

/// Sorted, de-duplicated disabled list.
pub fn normalize_tool_list<I, S>(names: I) -> Vec<String>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    names
        .into_iter()
        .map(Into::into)
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

/// Distinct unknown names in a resolved disabled list, sorted.
pub fn unknown_disabled_tools(disabled: &[String]) -> Vec<String> {
    normalize_tool_list(disabled.iter().filter(|name| !is_known_tool(name)).cloned())
}

/// Validate a resolved configuration object before anything consumes it.
///
/// A resolved configuration must carry the registration choice and every index
/// switch explicitly: a missing field must never be silently replaced by a
/// serde or historical default. Missing containers are reported instead of
/// their children. Errors are sorted by path.
pub fn validate_resolved_config(value: &Value) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();
    let Some(object) = value.as_object() else {
        return Err(vec!["invalid_resolved_config:type:$".to_string()]);
    };
    match object.get("disabled_tools") {
        None => errors.push("invalid_resolved_config:missing:disabled_tools".to_string()),
        Some(Value::Array(entries)) if entries.iter().all(Value::is_string) => {}
        Some(_) => errors.push("invalid_resolved_config:type:disabled_tools".to_string()),
    }
    match object.get("indexes") {
        None => errors.push("invalid_resolved_config:missing:indexes".to_string()),
        Some(Value::Object(indexes)) => {
            for leaf in ["callgraph", "semantic", "trigram"] {
                match indexes.get(leaf) {
                    None => errors.push(format!("invalid_resolved_config:missing:indexes.{leaf}")),
                    Some(Value::Bool(_)) => {}
                    Some(_) => errors.push(format!("invalid_resolved_config:type:indexes.{leaf}")),
                }
            }
        }
        Some(_) => errors.push("invalid_resolved_config:type:indexes".to_string()),
    }
    errors.sort_by(|left, right| {
        let path = |error: &String| error.rsplit(':').next().unwrap_or_default().to_string();
        path(left).cmp(&path(right))
    });
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

/// Explicit absent marker used by the notice projection.
fn absent() -> Value {
    let mut marker = Map::new();
    marker.insert("absent".to_string(), Value::Bool(true));
    Value::Object(marker)
}

fn project_block(block: &Map<String, Value>) -> Value {
    let mut projected = Map::new();

    let mut legacy_paths = Map::new();
    // Retired keys with no replacement are projected too: otherwise adding one
    // to a file whose notice was already acknowledged would keep the old
    // digest and the saved record would hide the new notice. The data-layer
    // keys are projected for the same reason.
    for path in RETIRED_PATHS
        .into_iter()
        .chain(REMOVED_NO_EFFECT_PATHS)
        .map(|(path, _)| path)
        .chain(DATA_LAYER_RETIRED_PATHS)
        .chain(["bash.runon_enabled"])
        .chain(REMOVED_DATA_LAYER_PATHS)
        .chain([CALLGRAPH_CHUNK_KEY])
    {
        let value = match path.split_once('.') {
            Some((container, leaf)) => block
                .get(container)
                .and_then(Value::as_object)
                .and_then(|inner| inner.get(leaf)),
            None => block.get(path),
        };
        if let Some(value) = value {
            legacy_paths.insert(path.to_string(), value.clone());
        }
    }
    projected.insert("legacy_paths".to_string(), Value::Object(legacy_paths));

    let mut gates = Map::new();
    if let Some(value) = block
        .get("backup")
        .and_then(Value::as_object)
        .and_then(|backup| backup.get("enabled"))
    {
        gates.insert("backup.enabled".to_string(), value.clone());
    }
    if let Some(value) = block
        .get("inspect")
        .and_then(Value::as_object)
        .and_then(|inspect| inspect.get("enabled"))
    {
        gates.insert("inspect.enabled".to_string(), value.clone());
    }
    match block.get("bash") {
        Some(Value::Bool(value)) => {
            gates.insert("bash".to_string(), Value::Bool(*value));
        }
        Some(Value::Object(bash)) => {
            if let Some(value) = bash.get("enabled") {
                gates.insert("bash.enabled".to_string(), value.clone());
            }
        }
        _ => {}
    }
    projected.insert("runtime_gates".to_string(), Value::Object(gates));
    // The notice identity records which shapes changed, not the potentially
    // sensitive initialization options. Bash booleans are projected above.
    let shapes: std::collections::BTreeSet<_> =
        settings_shape::edits(block, DocumentTier::Project, false)
            .into_iter()
            .filter(|edit| edit.path[0] != "bash")
            .map(|edit| edit.notice)
            .collect();
    if !shapes.is_empty() {
        projected.insert("legacy_shapes".into(), serde_json::json!(shapes));
    }

    let (aliases, disabled) = match block.get("disabled_tools") {
        Some(Value::Array(entries)) => {
            let names: Vec<String> = entries
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect();
            let aliases = normalize_tool_list(
                names
                    .iter()
                    .filter(|name| legacy_tool_alias(name).is_some())
                    .cloned(),
            );
            (
                aliases,
                Value::Array(
                    normalize_tool_list(names)
                        .into_iter()
                        .map(Value::String)
                        .collect(),
                ),
            )
        }
        Some(other) => (Vec::new(), other.clone()),
        None => (Vec::new(), absent()),
    };
    projected.insert(
        "legacy_disabled_entries".to_string(),
        Value::Array(aliases.into_iter().map(Value::String).collect()),
    );
    projected.insert("disabled_tools".to_string(), disabled);

    let mut github = Map::new();
    for leaf in ["read", "shim", "write"] {
        let value = block
            .get("github")
            .and_then(Value::as_object)
            .and_then(|github| github.get(leaf))
            .cloned()
            .unwrap_or_else(absent);
        github.insert(leaf.to_string(), value);
    }
    projected.insert("github".to_string(), Value::Object(github));

    let mut indexes = Map::new();
    for (leaf, legacy, experimental) in INDEX_INPUTS {
        let mut inputs = Map::new();
        inputs.insert(
            "canonical".to_string(),
            block
                .get("indexes")
                .and_then(Value::as_object)
                .and_then(|indexes| indexes.get(leaf))
                .cloned()
                .unwrap_or_else(absent),
        );
        inputs.insert(
            legacy.to_string(),
            block.get(legacy).cloned().unwrap_or_else(absent),
        );
        if let Some(experimental) = experimental {
            inputs.insert(
                experimental.to_string(),
                block.get(experimental).cloned().unwrap_or_else(absent),
            );
        }
        indexes.insert(leaf.to_string(), Value::Object(inputs));
    }
    projected.insert("indexes".to_string(), Value::Object(indexes));
    Value::Object(projected)
}

/// Build the `notice_projection_v1` object for one raw (untranslated) config
/// document. Only inputs that influence translation are projected, so
/// comments, formatting, key order and unrelated settings never change it.
pub fn notice_projection(raw: Option<&Map<String, Value>>) -> Value {
    let mut blocks = Map::new();
    if let Some(raw) = raw {
        blocks.insert("base".to_string(), project_block(raw));
        if let Some(Value::Object(harnesses)) = raw.get("harnesses") {
            for (name, block) in harnesses {
                if let Value::Object(block) = block {
                    blocks.insert(format!("harnesses.{name}"), project_block(block));
                }
            }
        }
    }
    let mut projection = Map::new();
    projection.insert(
        "projection".to_string(),
        Value::String("notice_projection_v1".to_string()),
    );
    projection.insert("file_absent".to_string(), Value::Bool(raw.is_none()));
    projection.insert("blocks".to_string(), Value::Object(blocks));
    Value::Object(projection)
}

/// Compact JSON with object keys sorted at every level.
pub fn canonical_json(value: &Value) -> String {
    match value {
        Value::Object(map) => {
            let mut keys: Vec<&String> = map.keys().collect();
            keys.sort();
            let body = keys
                .into_iter()
                .map(|key| {
                    format!(
                        "{}:{}",
                        serde_json::to_string(key).unwrap_or_default(),
                        canonical_json(&map[key])
                    )
                })
                .collect::<Vec<_>>()
                .join(",");
            format!("{{{body}}}")
        }
        Value::Array(items) => format!(
            "[{}]",
            items
                .iter()
                .map(canonical_json)
                .collect::<Vec<_>>()
                .join(",")
        ),
        other => serde_json::to_string(other).unwrap_or_default(),
    }
}

/// SHA-256 hex digest of the canonical projection.
pub fn notice_digest(projection: &Value) -> String {
    let digest = Sha256::digest(canonical_json(projection).as_bytes());
    digest.iter().map(|byte| format!("{byte:02x}")).collect()
}

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

    fn translate(doc: Value) -> (Value, DocumentTranslation) {
        let Value::Object(mut map) = doc else {
            panic!("object")
        };
        let out = translate_document(&mut map, DocumentTier::User);
        (Value::Object(map), out)
    }

    fn translate_project(doc: Value) -> Value {
        let Value::Object(mut map) = doc else {
            panic!("object")
        };
        translate_document(&mut map, DocumentTier::Project);
        Value::Object(map)
    }

    #[test]
    fn removed_config_key_errors_match_shared_rust_typescript_fixtures() {
        let fixture: Value = serde_json::from_str(include_str!(
            "../../aft/tests/fixtures/feature_config/removed_config_key_errors.json"
        ))
        .unwrap();
        for case in fixture["cases"].as_array().unwrap() {
            let doc = case["doc"].as_object().unwrap();
            let release = parse_release(case["release"].as_str().unwrap()).unwrap();
            let tier = match case["tier"].as_str().unwrap() {
                "user" => DocumentTier::User,
                "project" => DocumentTier::Project,
                other => panic!("unknown fixture tier: {other}"),
            };
            let errors = match tier {
                DocumentTier::User => removed_config_key_errors_at(doc, release),
                DocumentTier::Project => removed_errors(doc, release, false),
            };
            let expected = case["errors"]
                .as_array()
                .unwrap()
                .iter()
                .map(|error| error.as_str().unwrap())
                .collect::<Vec<_>>();
            assert_eq!(errors, expected, "fixture {}", case["name"]);
            if let Some(translated) = case.get("translated") {
                assert!(errors.is_empty(), "only accepted files translate");
                let mut map = doc.clone();
                let out = translate_document(&mut map, tier);
                assert_eq!(Value::Object(map), *translated, "fixture {}", case["name"]);
                if let Some(notice) = case.get("notice") {
                    assert_eq!(
                        project_retired_keys_notice("/project/aft.jsonc", &out),
                        notice.as_str().unwrap(),
                        "fixture {}",
                        case["name"]
                    );
                }
            }
        }
    }

    #[test]
    fn legacy_enabled_false_warns_that_indexes_still_build() {
        let (doc, out) = translate(json!({"enabled": false}));
        assert!(
            doc.get("indexes").is_none(),
            "indexes stay at their defaults"
        );
        let warning = out
            .warnings
            .iter()
            .find(|warning| warning.code == "legacy_enabled_false_indexes_still_build")
            .expect("enabled:false notice");
        assert_eq!(warning.key, "enabled");
        assert!(warning.message.contains("indexes still build"));
        assert!(warning.message.contains("indexes.trigram"));
    }

    /// A project block contributes only what its own legacy keys imply; the
    /// move/delete default belongs to the user base alone.
    #[test]
    fn project_base_blocks_never_receive_the_default_disables() {
        assert_eq!(
            translate_project(json!({"hoist_builtin_tools": false}))["disabled_tools"],
            json!([
                "apply_patch",
                "bash",
                "edit",
                "glob",
                "grep",
                "read",
                "write"
            ])
        );
        assert_eq!(translate_project(json!({"tool_surface": "all"})), json!({}));
        assert_eq!(
            translate_project(json!({"tool_surface": "recommended"}))["disabled_tools"],
            json!(["aft_callgraph", "aft_delete", "aft_move"])
        );
    }

    /// A false runtime gate only switches its behaviour off: it never removes
    /// a registration, alone or beside a retired key, and the translation
    /// warns `legacy_runtime_gate_runtime_only` so the user knows to list the
    /// tool in `disabled_tools`.
    #[test]
    fn false_runtime_gates_never_generate_disables() {
        for doc in [
            json!({"backup": {"enabled": false}}),
            json!({"inspect": {"enabled": false}}),
            json!({"bash": false}),
            json!({"bash": {"enabled": false}}),
        ] {
            let (value, out) = translate(doc.clone());
            assert!(value.get("disabled_tools").is_none(), "{doc}");
            assert_eq!(out.legacy_input, doc["bash"] == false, "{doc}");
            assert!(
                out.warnings
                    .iter()
                    .any(|warning| warning.code == "legacy_runtime_gate_runtime_only"),
                "{doc}"
            );
            assert!(translate_project(doc.clone())
                .get("disabled_tools")
                .is_none());
        }
        let (value, _) = translate(json!({"bash": false, "hoist_builtin_tools": false}));
        assert_eq!(
            value["disabled_tools"],
            json!([
                "aft_delete",
                "aft_move",
                "apply_patch",
                "bash",
                "edit",
                "glob",
                "grep",
                "read",
                "write"
            ]),
            "only the retired hoist key generates disables"
        );
    }

    #[test]
    fn policy_constants_match_the_shared_artifact() {
        let path = std::path::Path::new(crate::test_support::manifest_dir())
            .join("../../spec/feature-config/migration-policy.json");
        let Ok(text) = std::fs::read_to_string(path) else {
            return;
        };
        let artifact: Value = serde_json::from_str(&text).unwrap();
        assert_eq!(artifact["id"], MIGRATION_POLICY_ID);
        assert_eq!(artifact["introduced_minor"], "0.58");
        assert!(
            artifact.get("reject_from_minor").is_none(),
            "retired keys are never rejected"
        );
        for (path, replacement) in RETIRED_PATHS {
            assert_eq!(artifact["paths"][path], replacement, "{path}");
        }
        for (alias, canonical) in LEGACY_TOOL_ALIASES {
            assert_eq!(artifact["disabled_list_names"][alias], canonical);
        }
    }

    #[test]
    fn surfaces_translate_to_literal_sets() {
        let (all, _) = translate(json!({"tool_surface": "all"}));
        assert_eq!(all, json!({"disabled_tools": []}));
        let (recommended, _) = translate(json!({"tool_surface": "recommended"}));
        assert_eq!(
            recommended["disabled_tools"],
            json!(["aft_callgraph", "aft_delete", "aft_move"])
        );
        let (minimal, _) = translate(json!({"tool_surface": "minimal"}));
        assert_eq!(minimal["disabled_tools"].as_array().unwrap().len(), 20);
    }

    #[test]
    fn retired_base_key_unions_with_default_and_explicit_empty_wins() {
        let (hoist, out) = translate(json!({"hoist_builtin_tools": false}));
        assert_eq!(
            hoist["disabled_tools"],
            json!([
                "aft_delete",
                "aft_move",
                "apply_patch",
                "bash",
                "edit",
                "glob",
                "grep",
                "read",
                "write"
            ])
        );
        assert!(out.legacy_input);
        assert_eq!(out.retired_keys, vec!["hoist_builtin_tools"]);
        let (explicit, _) = translate(json!({"hoist_builtin_tools": false, "disabled_tools": []}));
        assert_eq!(explicit["disabled_tools"], json!([]));
    }

    /// Every retired key translates; none is ever refused, and the translation
    /// names each one (harness keys with their block prefix).
    #[test]
    fn retired_keys_translate_and_are_listed() {
        let (value, out) = translate(json!({
            "disabled_tools": ["aft_glob"],
            "search_index": true,
            "harnesses": {"pi": {"semantic_search": false}}
        }));
        assert_eq!(
            value,
            json!({
                "disabled_tools": ["glob"],
                "indexes": {"trigram": true},
                "harnesses": {"pi": {"indexes": {"semantic": false}}}
            })
        );
        assert!(out.legacy_input);
        assert_eq!(
            out.retired_keys,
            vec!["aft_glob", "harnesses.pi.semantic_search", "search_index"]
        );
    }

    #[test]
    fn index_precedence_is_canonical_then_legacy_then_experimental() {
        let (value, _) =
            translate(json!({"experimental_search_index": false, "search_index": true}));
        assert_eq!(value, json!({"indexes": {"trigram": true}}));
        let (value, out) =
            translate(json!({"indexes": {"semantic": false}, "semantic_search": true}));
        assert_eq!(value, json!({"indexes": {"semantic": false}}));
        assert_eq!(out.warnings[0].code, "superseded_legacy_config");
    }

    /// The GitHub enable aliases translate to the canonical leaves with the
    /// same precedence `doctor --fix` uses: canonical leaf, then a
    /// `github.enabled: false` in the same block, then the alias.
    #[test]
    fn retired_github_aliases_translate_like_doctor_fix() {
        let (value, out) = translate(json!({
            "gh_read": {"enabled": true},
            "gh_shim": {"enabled": false, "binary_path": "/opt/aft"}
        }));
        assert_eq!(
            value,
            json!({
                "github": {"read": true, "shim": false},
                "gh_shim": {"binary_path": "/opt/aft"}
            })
        );
        assert_eq!(out.retired_keys, vec!["gh_read", "gh_shim.enabled"]);

        let (value, out) =
            translate(json!({"gh_read": {"enabled": true}, "github": {"read": false}}));
        assert_eq!(value, json!({"github": {"read": false}}));
        assert_eq!(out.warnings[0].code, "superseded_legacy_config");

        let (value, _) = translate(json!({
            "harnesses": {"pi": {"gh_shim": {"enabled": true}, "github": {"enabled": false}}}
        }));
        assert_eq!(
            value,
            json!({"harnesses": {"pi": {"github": {"read": false, "write": false, "shim": false}}}})
        );

        let (value, out) = translate(json!({"gh_shim": {"binary_path": "/opt/aft"}}));
        assert_eq!(value, json!({"gh_shim": {"binary_path": "/opt/aft"}}));
        assert!(!out.legacy_input);
    }

    #[test]
    fn retired_inspect_and_lsp_keys_translate_like_doctor_fix() {
        let (value, out) = translate(json!({
            "idle": {"lsp_ttl_minutes": 3, "root_ttl_minutes": 20},
            "inspect": {"tier2_soft_deadline_ms": 50, "max_drill_down_items": 20}
        }));
        assert_eq!(
            value,
            json!({"idle": {"root_ttl_minutes": 20}, "lsp": {"idle_minutes": 5}})
        );
        assert_eq!(
            out.retired_keys,
            vec![
                "idle.lsp_ttl_minutes",
                "inspect.max_drill_down_items",
                "inspect.tier2_soft_deadline_ms"
            ]
        );
        let (value, _) = translate(json!({"idle": {"lsp_ttl_minutes": "x"}}));
        assert_eq!(value, json!({"lsp": {"idle_minutes": 60}}));
        // A whole number written as a float is that number, as JavaScript
        // (and so the TypeScript translation) reads it; anything else is the
        // default.
        for (text, minutes) in [
            ("12.0", 12),
            ("12", 12),
            ("1e3", 1000),
            ("12.5", 60),
            ("1e20", 60),
            ("9007199254740993", 60),
        ] {
            let doc: Value =
                serde_json::from_str(&format!(r#"{{"idle": {{"lsp_ttl_minutes": {text}}}}}"#))
                    .unwrap();
            let (value, _) = translate(doc);
            assert_eq!(value, json!({"lsp": {"idle_minutes": minutes}}), "{text}");
        }
        let (value, out) = translate(json!({
            "idle": {"lsp_ttl_minutes": 10},
            "lsp": {"idle_minutes": "never"}
        }));
        assert_eq!(value, json!({"lsp": {"idle_shutdown": false}}));
        assert_eq!(out.warnings[0].code, "superseded_legacy_config");
    }

    #[test]
    fn github_master_false_fills_absent_leaves() {
        let (value, _) = translate(json!({"github": {"enabled": false, "shim": true}}));
        assert_eq!(
            value,
            json!({"github": {"shim": true, "read": false, "write": false}})
        );
    }

    #[test]
    fn notices_name_the_file_and_the_retired_keys() {
        let (_, out) = translate(json!({"search_index": false, "hoist_builtin_tools": true}));
        let project = project_retired_keys_notice("/repo/.cortexkit/aft.jsonc", &out);
        assert!(project.starts_with(
            "/repo/.cortexkit/aft.jsonc uses retired keys (hoist_builtin_tools, search_index); AFT applied their current equivalents"
        ));
        assert!(project.contains("npx @cortexkit/aft doctor --fix"));
        let migrated = user_config_migrated_notice("/u/aft.jsonc", "/u/aft.jsonc.bak-1", &out);
        assert!(migrated.contains("/u/aft.jsonc.bak-1"));
        let (_, out) = translate(json!({"enabled": false}));
        assert!(
            user_config_not_migrated_notice("/u/aft.jsonc", "read-only", &out)
                .ends_with(RETIRED_ENABLED_FALSE_INDEXES_NOTE)
        );
    }

    #[test]
    fn retired_hang_threshold_is_dropped_and_the_notice_says_it_does_nothing() {
        let (value, out) = translate(json!({
            "bridge": {"hang_threshold": 3, "request_timeout_ms": 45000},
            "harnesses": {"pi": {"bridge": {"hang_threshold": 5}}}
        }));
        assert_eq!(
            value,
            json!({"bridge": {"request_timeout_ms": 45000}, "harnesses": {"pi": {}}})
        );
        assert!(out.legacy_input);
        assert_eq!(
            out.retired_keys,
            vec![
                "bridge.hang_threshold",
                "harnesses.pi.bridge.hang_threshold"
            ]
        );
        // Nothing is applied for a key that no longer does anything, so a
        // file whose only retired keys are such keys gets just their sentence.
        for notice in [
            project_retired_keys_notice("/repo/.cortexkit/aft.jsonc", &out),
            user_config_migrated_notice("/u/aft.jsonc", "/u/aft.jsonc.bak-1", &out),
            user_config_not_migrated_notice("/u/aft.jsonc", "read-only", &out),
        ] {
            assert_eq!(notice, RETIRED_HANG_THRESHOLD_NOTE);
        }
        let (_, out) = translate(json!({
            "inspect": {"tier2_soft_deadline_ms": 50, "max_drill_down_items": 20}
        }));
        assert_eq!(
            project_retired_keys_notice("/p", &out),
            "inspect.max_drill_down_items never had an effect, so the setting can be removed. inspect.tier2_soft_deadline_ms never had an effect, so the setting can be removed."
        );

        // Mixed with keys that have equivalents: the generic sentence names
        // only those keys, and the no-effect sentence follows.
        let (_, out) = translate(json!({
            "search_index": false,
            "bridge": {"hang_threshold": 3},
            "harnesses": {"pi": {"bridge": {"hang_threshold": 5}}}
        }));
        assert_eq!(
            project_retired_keys_notice("/repo/.cortexkit/aft.jsonc", &out),
            format!("/repo/.cortexkit/aft.jsonc uses retired keys (search_index); AFT applied their current equivalents, with the same limits a project config has for those keys. Run `npx @cortexkit/aft doctor --fix` to update the file. {RETIRED_HANG_THRESHOLD_NOTE}")
        );
        assert_eq!(
            user_config_migrated_notice("/u/aft.jsonc", "/u/aft.jsonc.bak-1", &out),
            format!("AFT updated /u/aft.jsonc: its retired keys (search_index) now use their current equivalents. The previous file is saved as /u/aft.jsonc.bak-1. {RETIRED_HANG_THRESHOLD_NOTE}")
        );
        assert_eq!(
            user_config_not_migrated_notice("/u/aft.jsonc", "read-only", &out),
            format!("/u/aft.jsonc uses retired keys (search_index); AFT applied their current equivalents but could not update the file (read-only). Run `npx @cortexkit/aft doctor --fix` to update it. {RETIRED_HANG_THRESHOLD_NOTE}")
        );
        let (_, out) = translate(json!({"enabled": false, "bridge": {"hang_threshold": 3}}));
        assert!(project_retired_keys_notice("/p", &out).ends_with(&format!(
            "{RETIRED_ENABLED_FALSE_INDEXES_NOTE} {RETIRED_HANG_THRESHOLD_NOTE}"
        )));

        // Only the retired leaf goes; an emptied bridge block goes with it.
        let (value, _) = translate(json!({"bridge": {"hang_threshold": 2}}));
        assert_eq!(value, json!({}));
        let (_, out) = translate(json!({"search_index": false}));
        assert!(!project_retired_keys_notice("/p", &out).contains("hang_threshold"));
    }
    #[test]
    fn resolved_validation_reports_sorted_paths_and_containers_only() {
        assert_eq!(
            validate_resolved_config(&json!({})).unwrap_err(),
            vec![
                "invalid_resolved_config:missing:disabled_tools",
                "invalid_resolved_config:missing:indexes"
            ]
        );
        assert_eq!(
            validate_resolved_config(&json!({
                "disabled_tools": "x",
                "indexes": {"trigram": 1, "semantic": true}
            }))
            .unwrap_err(),
            vec![
                "invalid_resolved_config:type:disabled_tools",
                "invalid_resolved_config:missing:indexes.callgraph",
                "invalid_resolved_config:type:indexes.trigram"
            ]
        );
        assert!(validate_resolved_config(&json!({
            "disabled_tools": [],
            "indexes": {"trigram": true, "semantic": false, "callgraph": true}
        }))
        .is_ok());
    }

    #[test]
    fn adding_a_no_effect_key_changes_the_notice_identity_and_nothing_else_does() {
        let digest = |doc: Value| notice_digest(&notice_projection(doc.as_object()));
        let acknowledged = digest(json!({"search_index": false}));
        // Nothing a notice depends on changed: same identity, no repeat.
        assert_eq!(acknowledged, digest(json!({"search_index": false})));
        assert_eq!(
            acknowledged,
            digest(json!({"search_index": false, "bridge": {"request_timeout_ms": 45000}}))
        );
        // Adding the retired key, in the base or a harness block, is a new
        // identity, so a saved record for the old one cannot hide it.
        let added = json!({"search_index": false, "bridge": {"hang_threshold": 3}});
        assert_ne!(acknowledged, digest(added.clone()));
        assert_ne!(
            acknowledged,
            digest(
                json!({"search_index": false, "harnesses": {"pi": {"bridge": {"hang_threshold": 5}}}})
            )
        );
        // The notice for the new identity names it.
        let Value::Object(mut map) = added else {
            panic!("object")
        };
        let out = translate_document(&mut map, DocumentTier::Project);
        assert_eq!(
            project_retired_keys_notice("/p", &out),
            format!("/p uses retired keys (search_index); AFT applied their current equivalents, with the same limits a project config has for those keys. Run `npx @cortexkit/aft doctor --fix` to update the file. {RETIRED_HANG_THRESHOLD_NOTE}")
        );
    }

    #[test]
    fn notice_digests_match_the_shared_fixtures() {
        let path = std::path::Path::new(crate::test_support::manifest_dir())
            .join("../aft/tests/fixtures/feature_config/notice_projection.json");
        let fixtures: Value =
            serde_json::from_str(&std::fs::read_to_string(path).expect("fixture")).unwrap();
        for case in fixtures["cases"].as_array().unwrap() {
            let doc = case.get("doc").and_then(Value::as_object);
            let projection = notice_projection(doc);
            assert_eq!(
                notice_digest(&projection),
                case["digest"].as_str().unwrap(),
                "{}",
                case["name"]
            );
        }
    }

    #[test]
    fn release_parsing_reads_major_and_minor_only() {
        assert_eq!(parse_release("0.65.0"), Some((0, 65)));
        assert_eq!(parse_release("0.64.12-beta.1"), Some((0, 64)));
        assert_eq!(parse_release("1.2"), Some((1, 2)));
        assert_eq!(parse_release("latest"), None);
        assert_eq!(
            running_release(),
            parse_release(env!("CARGO_PKG_VERSION")).unwrap()
        );
    }

    #[test]
    fn config_test_release_override_is_thread_local_and_restored() {
        let actual = running_release();
        {
            let _outer = override_running_release_for_test((0, 64));
            assert_eq!(running_release(), (0, 64));
            {
                let _inner = override_running_release_for_test((0, 65));
                assert_eq!(running_release(), (0, 65));
            }
            assert_eq!(running_release(), (0, 64));
            assert_eq!(std::thread::spawn(running_release).join().unwrap(), actual);
        }
        assert_eq!(running_release(), actual);
    }

    /// The idle-checkout rename keeps its existing version-gated policy.
    #[test]
    fn each_data_layer_key_is_refused_only_from_the_refusing_release() {
        assert_eq!(DATA_LAYER_KEYS_REFUSED_FROM, (0, 65));
        for path in DATA_LAYER_RETIRED_PATHS {
            let (container, leaf) = path.split_once('.').unwrap();
            let base = json!({ container: { leaf: 1 } });
            let harness = json!({ "harnesses": { "pi": { container: { leaf: 1 } } } });
            for doc in [&base, &harness] {
                let doc = doc.as_object().unwrap();
                assert!(
                    removed_config_key_errors_at(doc, (0, 64)).is_empty(),
                    "{path}"
                );
            }
            assert_eq!(
                removed_config_key_errors_at(base.as_object().unwrap(), (0, 65)),
                vec![format!(
                    "removed_config_key:{path}; run npx @cortexkit/aft doctor --fix"
                )]
            );
            assert_eq!(
                removed_config_key_errors_at(harness.as_object().unwrap(), (0, 65)),
                vec![format!(
                    "removed_config_key:harnesses.pi.{path}; run npx @cortexkit/aft doctor --fix"
                )]
            );
            assert_eq!(
                removed_config_key_errors_at(base.as_object().unwrap(), (1, 0)).len(),
                1,
                "{path}"
            );
            for doc in [&base, &harness] {
                assert!(removed_errors(doc.as_object().unwrap(), (0, 65), false).is_empty());
            }
        }
    }

    #[test]
    fn callgraph_chunk_size_config_refuses_user_only_from_067() {
        for (doc, prefix) in [
            (json!({"callgraph_chunk_size":100}), ""),
            (
                json!({"harnesses":{"pi":{"callgraph_chunk_size":100}}}),
                "harnesses.pi.",
            ),
        ] {
            let map = doc.as_object().unwrap();
            assert!(removed_errors(map, (0, 66), true).is_empty());
            assert_eq!(removed_errors(map, (0, 67), true), vec![format!("removed_config_key:{prefix}callgraph_chunk_size; run npx @cortexkit/aft doctor --fix")]);
            assert!(removed_errors(map, (0, 67), false).is_empty());
            let mut translated = map.clone();
            let out = translate_document(&mut translated, DocumentTier::Project);
            assert!(out.legacy_input);
            assert!(!serde_json::to_string(&translated)
                .unwrap()
                .contains("callgraph_chunk_size"));
            assert_eq!(
                project_retired_keys_notice("/project/aft.jsonc", &out),
                CALLGRAPH_CHUNK_NOTE
            );
        }
    }

    #[test]
    fn removed_data_layer_keys_refuse_only_user_files_including_harness_blocks() {
        for path in REMOVED_DATA_LAYER_PATHS {
            let (container, leaf) = path.split_once('.').unwrap();
            for (doc, prefix) in [
                (json!({container: {leaf: false}}), ""),
                (
                    json!({"harnesses": {"pi": {container: {leaf: false}}}}),
                    "harnesses.pi.",
                ),
            ] {
                let map = doc.as_object().unwrap();
                assert_eq!(
                    removed_errors(map, (0, 65), true),
                    vec![format!(
                        "removed_config_key:{prefix}{path}; run npx @cortexkit/aft doctor --fix"
                    )]
                );
                assert!(removed_errors(map, (0, 65), false).is_empty(), "{path}");
            }
        }
        assert!(removed_errors(
            json!({"inspect": {"tier2_idle_minutes": 5}})
                .as_object()
                .unwrap(),
            (0, 65),
            true
        )
        .is_empty());
    }

    /// `views.idle_checkout_hours` moves to `idle.checkout_hours`, clamped to
    /// its range, unless that is already set; it is listed as a retired key
    /// and so triggers the user file's automatic rewrite.
    #[test]
    fn retired_views_idle_checkout_hours_translates_like_doctor_fix() {
        let (value, out) = translate(json!({
            "views": {"idle_checkout_hours": 48},
            "idle": {"root_ttl_minutes": 20},
            "harnesses": {"pi": {"views": {"idle_checkout_hours": 0, "enabled": true}}}
        }));
        assert_eq!(
            value,
            json!({
                "idle": {"root_ttl_minutes": 20, "checkout_hours": 48},
                "harnesses": {"pi": {"idle": {"checkout_hours": 1}}}
            })
        );
        assert!(out.legacy_input);
        assert_eq!(
            out.retired_keys,
            vec![
                "harnesses.pi.views.enabled",
                "harnesses.pi.views.idle_checkout_hours",
                "views.idle_checkout_hours"
            ]
        );

        for (raw, hours) in [
            (json!(9000), 8760),
            (json!(12.0), 12),
            (json!(12.5), 168),
            (json!("never"), 168),
        ] {
            assert_eq!(retired_idle_checkout_hours(&raw), hours, "{raw}");
        }

        let (value, out) = translate(json!({
            "views": {"idle_checkout_hours": 2},
            "idle": {"checkout_hours": 5}
        }));
        assert_eq!(value, json!({"idle": {"checkout_hours": 5}}));
        assert_eq!(out.warnings[0].code, "superseded_legacy_config");
        assert_eq!(
            out.warnings[0].message,
            "views.idle_checkout_hours is ignored because idle.checkout_hours is set"
        );
    }

    /// Project translation removes the obsolete settings before strict parsing.
    #[test]
    fn removed_data_layer_keys_are_dropped_and_listed_as_retired() {
        let (value, out) = translate(json!({
            "views": {"enabled": true, "callgraph_copies": 2},
            "worktree": {"ram_overlay": false},
            "harnesses": {"opencode": {"worktree": {"ram_overlay": true}}}
        }));
        assert_eq!(value, json!({"harnesses": {"opencode": {}}}));
        assert!(out.legacy_input);
        assert_eq!(
            out.retired_keys,
            vec![
                "harnesses.opencode.worktree.ram_overlay",
                "views.callgraph_copies",
                "views.enabled",
                "worktree.ram_overlay"
            ]
        );
    }

    #[test]
    fn data_layer_project_notices_reuse_only_existing_no_effect_sentences() {
        let (_, removed) = translate(json!({
            "views": {"enabled": true, "callgraph_copies": 2},
            "worktree": {"ram_overlay": false}
        }));
        assert_eq!(project_retired_keys_notice("/p", &removed),
            "views.callgraph_copies is gone because no checkout borrows another checkout's call graph any more. views.enabled is gone because AFT now keeps one index store per repository and always uses it. worktree.ram_overlay is gone because every worktree now has its own index state, so there is no borrowed index to patch in memory.");
        let (_, one) = translate(json!({"harnesses": {"pi": {"worktree": {"ram_overlay": true}}}}));
        assert_eq!(
            project_retired_keys_notice("/p", &one),
            REMOVED_DATA_LAYER_NOTES[2].1
        );
        assert_eq!(
            project_retired_keys_notice("/p", &DocumentTranslation::default()),
            ""
        );
        let (_, both) = translate(json!({"views": {"idle_checkout_hours": 24, "enabled": true}}));
        assert_eq!(project_retired_keys_notice("/p", &both),
            format!("/p uses retired keys (views.idle_checkout_hours); AFT applied their current equivalents, with the same limits a project config has for those keys. Run `npx @cortexkit/aft doctor --fix` to update the file. {} {}", retired_idle_checkout_hours_note(), REMOVED_DATA_LAYER_NOTES[1].1));
    }

    /// Adding a data-layer key to a file whose notice was acknowledged is a
    /// new notice identity, so the new notice is not hidden.
    #[test]
    fn adding_a_data_layer_key_changes_the_notice_identity() {
        let digest = |doc: Value| notice_digest(&notice_projection(doc.as_object()));
        let acknowledged = digest(json!({"search_index": false}));
        for path in DATA_LAYER_RETIRED_PATHS {
            let (container, leaf) = path.split_once('.').unwrap();
            assert_ne!(
                acknowledged,
                digest(json!({"search_index": false, container: { leaf: 1 }})),
                "{path}"
            );
        }
    }
}