release-kit 0.8.7

A canonical release workflow: a technology-agnostic method, per-technology bindings, and the rk CLI that lands and serves them.
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
//! The committed target configuration, parsed strictly and written from
//! authored text, typed by the domain that owns each answer.
//! Comparisons continue to use the landing record alone.
//!
//! SATISFIES project-profile:the-target-configuration-is-typed-by-domain

pub mod floors;
pub mod migrate;

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::Path;

use crate::diagnostic::{Diagnostic, Reason};
use crate::error::RkError;
use crate::landing::{CheckoutMode, Integration, Style};
use crate::profile::ReleaseMode;
use serde::{Deserialize, Serialize};

/// The committed input, relative to the target root.
pub const CONFIG_PATH: &str = ".release-kit/config.toml";
/// The configuration schema this binary writes. Schema 1 reads through
/// the one migration in [`migrate`].
pub const SCHEMA_VERSION: i64 = 2;

/// Per-target answers; an omitted table uses its compiled defaults.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
    /// Version of the authored configuration shape.
    pub schema_version: i64,
    /// Project identity.
    pub project: Project,
    /// What the project is.
    pub profile: Profile,
    /// How topic branches reach the trunk.
    pub git: Git,
    /// Which optional products the target requests.
    pub capabilities: Capabilities,
    /// Report-routing facts and the two policy answers.
    pub security: Security,
    /// Forge setup inputs and the setup declaration.
    pub setup: Setup,
    /// Names and floored policy.
    pub protection: Protection,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            schema_version: SCHEMA_VERSION,
            project: Project::default(),
            profile: Profile::default(),
            git: Git::default(),
            capabilities: Capabilities::default(),
            security: Security::default(),
            setup: Setup::default(),
            protection: Protection::default(),
        }
    }
}

/// The `project` table: identity.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Project {
    /// P: project path on the forge, nested groups included. Empty where
    /// the project has no forge repository.
    pub repo: String,
}

/// The `profile` table: what the project is.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Profile {
    /// P: every technology present, zero or many. Absent means detect.
    pub technologies: Option<Vec<String>>,
    /// P: the forge. Absent means detect from the remote; empty states
    /// that the project has no forge.
    pub forge: Option<String>,
    /// P: the release intent.
    pub release: Release,
}

/// The `profile.release` table.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Release {
    /// P: automatic, external, or none. Absent means the observation
    /// proposes one.
    pub mode: Option<ReleaseMode>,
    /// P: the technology that states the version and takes the bot;
    /// automatic alone.
    pub driver: Option<String>,
    /// P: trunk or lines; automatic alone.
    pub style: Option<Style>,
    /// P: release-line branch prefix; automatic alone.
    pub line_prefix: Option<String>,
}

/// The `git` table: the Git workflow parameters.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Git {
    /// P: the one permanent branch, rendered into every landed artifact
    /// that names it. Absent means the landing has not answered it, so a
    /// record's own answer survives an upgrade that predates the key.
    pub trunk: Option<String>,
    /// P: linked-worktree or main-worktree.
    pub checkout_mode: Option<CheckoutMode>,
    /// P: local or forge, the authority that moves an implementation
    /// onto the trunk.
    pub integration: Option<Integration>,
}

/// The `capabilities` table: the optional products the target requests.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Capabilities {
    /// P: opt-in Nix capability.
    pub nix_packaging: Option<bool>,
    /// P: the landed vulnerability reporting policy.
    pub reporting_policy: Option<bool>,
    /// P: opt-in Scorecard capability.
    pub scorecard: Option<bool>,
    /// P: opt-in code scanning provider, as `codeql`, `semgrep`, or `off`.
    /// The value stays a string here so an absent key and an explicit `off`
    /// stay distinguishable; the resolution parses it.
    pub code_scanning: Option<String>,
}

/// The compiled trunk, used where neither a configuration nor a record answers.
pub const TRUNK_DEFAULT: &str = "master";

/// The compiled release-line prefix, used where nothing else answers.
pub const LINE_PREFIX_DEFAULT: &str = "release/";

/// The `security` table.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Security {
    /// N: project receiving vulnerability reports.
    pub advisories: String,
    /// P: contact when the forge channel is unavailable, rendered into the
    /// landed policy. Absent means the landing has not answered it, so a
    /// record's own answer survives an upgrade that predates the key; an
    /// explicit empty string resets the policy to the forge's own prose.
    pub contact: Option<String>,
    /// P: the acknowledgment window the landed policy promises. Absent
    /// means unanswered, exactly as `contact` does.
    pub response: Option<String>,
}

/// The compiled response stance, used where nothing else answers: the
/// policy promises no window at all.
pub const RESPONSE_DEFAULT: &str = "best-effort";

/// The one GitHub ruleset bypass local integration owns.
///
/// GitHub identifies its built-in repository administrator role as role 5.
/// The setup maps this stable policy name to the forge object, so the target
/// configuration records intent rather than an API encoding.
pub const LOCAL_GITHUB_BYPASS: &str = "repository-admin";

/// The canonical form of a security contact, or why it is refused.
///
/// One trimmed line. The value is rendered into `SECURITY.md` verbatim, so
/// a line feed, a carriage return, or any other ASCII control character
/// would break the sentence it lands in and is refused before any write.
/// Emptiness is not a refusal: it selects the forge's own authored prose.
///
/// # Errors
/// The refusal text, naming the key and what it accepts.
pub fn canonical_contact(raw: &str) -> Result<String, String> {
    let trimmed = raw.trim();
    if trimmed.chars().any(char::is_control) {
        return Err(format!(
            "security.contact carries a control character; it is one line naming an address, a URL, a person, or a team, and empty selects the forge's own wording, found {trimmed:?}"
        ));
    }
    Ok(trimmed.to_owned())
}

/// The canonical form of a response stance, or why it is refused.
///
/// Either `best-effort` or a plural-correct day count: `1 day`, `<n> days`,
/// `1 business day`, or `<n> business days`, with `n` a `u32` above one
/// written without a sign or a leading zero. The grammar is narrow because
/// the rendered sentence is a public promise, and only a value this
/// renderer can state exactly may reach it. An empty value reads as the
/// compiled default.
///
/// # Errors
/// The refusal text, naming the key and every accepted form.
pub fn canonical_response(raw: &str) -> Result<String, String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() || trimmed == RESPONSE_DEFAULT {
        return Ok(RESPONSE_DEFAULT.to_owned());
    }
    let refusal = || {
        format!(
            "security.response must be one of: best-effort, 1 day, <n> days, 1 business day, <n> business days, where n is a whole number above one; found {trimmed:?}"
        )
    };
    let (count, unit) = trimmed.split_once(' ').ok_or_else(refusal)?;
    let plural = match unit {
        "day" | "business day" => false,
        "days" | "business days" => true,
        _ => return Err(refusal()),
    };
    let number: u32 = count.parse().map_err(|_| refusal())?;
    // A canonical count round-trips, which refuses a sign and a leading
    // zero without a second pass over the text.
    if count != number.to_string() || (number > 1) != plural {
        return Err(refusal());
    }
    Ok(trimmed.to_owned())
}

/// The `setup` table.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Setup {
    /// P: the check the trunk ruleset requires in both modes. Under local
    /// integration the rendered release gate judges it too.
    pub required_check: String,
    /// P: the workflow name whose completion wakes the release gate.
    /// GitHub alone, because a `workflow_run` trigger cannot name a check
    /// and cannot omit the workflow.
    pub required_workflow: String,
    /// N: long-lived branches retired by the trunk.
    pub retired_branches: Vec<String>,
    /// N: run release-line protection in a full apply.
    pub release_lines: bool,
    /// N: the steps this target does not run, each against the reason a
    /// report prints. An exclusion narrows what the setup judges and
    /// weakens no floor: every value a step the target still runs reads is
    /// floored exactly as before.
    pub excluded_steps: BTreeMap<String, String>,
    /// Public bot identity.
    pub bot: Bot,
}

impl Default for Setup {
    fn default() -> Self {
        Self {
            required_check: String::new(),
            required_workflow: String::new(),
            retired_branches: vec!["main".into(), "develop".into()],
            release_lines: false,
            excluded_steps: BTreeMap::new(),
            bot: Bot::default(),
        }
    }
}

/// The `setup.bot` table.
///
/// The App's public identifier and nothing else. The installation id is
/// not here: it is the forge's own state, one cheap call answers it, and a
/// cached copy that goes stale buys a refusal the operator must resolve by
/// hand. The private key and the token are never here at all.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Bot {
    /// N: public App identifier; private credentials stay outside this file.
    pub app_id: String,
    /// Accepted and ignored. Version 0.3.13 wrote this key, so a target
    /// landed by it must still parse; nothing reads the value and no new
    /// configuration carries it. Removing it outright would refuse every
    /// such target, because this reader denies an unknown key by design.
    #[serde(default, skip_serializing)]
    pub installation_id: Option<i64>,
}

/// The `protection` table.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "these are independent policy switches in the committed TOML schema, not a state machine a smaller type could carry"
)]
pub struct Protection {
    /// N: trunk ruleset name. Absent derives `<trunk>-protection`, which
    /// is the name the setup script built before the key existed, so a
    /// target that states none keeps the ruleset it already has.
    pub trunk_ruleset: Option<String>,
    /// N: safety ruleset name. Absent derives `<trunk>-safety`. This is the
    /// ruleset that names no bypass actor, so the deletion and force-push
    /// protections hold against every actor including the one the trunk
    /// ruleset may excuse.
    pub safety_ruleset: Option<String>,
    /// N: tag ruleset name.
    pub tag_ruleset: String,
    /// N: release-line ruleset name.
    pub lines_ruleset: String,
    /// N: title job context.
    pub title_check: String,
    /// F: invariant, covers every published version.
    pub tag_pattern: String,
    /// F: invariant, the actors `git.integration` names. It excuses the
    /// trunk ruleset alone; the safety ruleset names nobody.
    pub bypass_actors: Vec<String>,
    /// F: invariant, exactly squash.
    pub allowed_merge_methods: Vec<String>,
    /// F: invariant, true.
    pub strict_required_status_checks: bool,
    /// F: invariant, the rules `git.integration` names. A landing writes
    /// this key with the authority, and a target that narrowed or
    /// widened it keeps what it stated.
    pub owned_trunk_rules: Vec<String>,
    /// F: floor zero; higher is stricter.
    pub required_approving_review_count: i64,
    /// F: floor false; true is stricter.
    pub dismiss_stale_reviews_on_push: bool,
    /// F: floor false; true is stricter.
    pub require_code_owner_review: bool,
    /// F: floor false; true is stricter.
    pub require_last_push_approval: bool,
    /// GitHub policy.
    pub github: Github,
    /// GitLab policy.
    pub gitlab: Gitlab,
}

impl Default for Protection {
    fn default() -> Self {
        Self {
            trunk_ruleset: None,
            safety_ruleset: None,
            tag_ruleset: "release-tags".into(),
            lines_ruleset: "release-lines".into(),
            title_check: "pr-title".into(),
            tag_pattern: "refs/tags/v*".into(),
            bypass_actors: Vec::new(),
            allowed_merge_methods: vec!["squash".into()],
            strict_required_status_checks: true,
            owned_trunk_rules: vec![
                "deletion".into(),
                "non_fast_forward".into(),
                "pull_request".into(),
                "required_status_checks".into(),
            ],
            required_approving_review_count: 0,
            dismiss_stale_reviews_on_push: false,
            require_code_owner_review: false,
            require_last_push_approval: false,
            github: Github::default(),
            gitlab: Gitlab::default(),
        }
    }
}

impl Protection {
    /// The trunk ruleset's name: the target's own answer, or the name the
    /// setup script derived before the key existed.
    #[must_use]
    pub fn trunk_ruleset(&self, trunk: &str) -> String {
        self.trunk_ruleset
            .clone()
            .unwrap_or_else(|| format!("{trunk}-protection"))
    }

    /// The safety ruleset's name: the target's own answer, or the derived
    /// `<trunk>-safety`.
    ///
    /// A separate ruleset because a bypass actor is recorded on a ruleset
    /// and never on a rule. The deletion and force-push rules therefore
    /// cannot sit beside a bypassed rule without inheriting its excuse.
    #[must_use]
    pub fn safety_ruleset(&self, trunk: &str) -> String {
        self.safety_ruleset
            .clone()
            .unwrap_or_else(|| format!("{trunk}-safety"))
    }
}

/// The rules the safety ruleset carries, in the order the setup writes them.
///
/// These hold against every actor, so they live apart from the rules a
/// bypass may excuse. `protection.owned_trunk_rules` still names them, and
/// the floor table still requires them, so one key remains the answer to
/// what the setup owns on the trunk.
pub const SAFETY_RULES: [&str; 2] = ["deletion", "non_fast_forward"];

/// The rules the trunk ruleset carries: the request and the check it must
/// carry, which a recorded bypass actor may be excused from.
pub const REQUEST_RULES: [&str; 2] = ["pull_request", "required_status_checks"];

/// The `protection.github` table.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Github {
    /// F: invariant, `PR_TITLE`.
    pub squash_title_source: String,
    /// F: invariant, `PR_BODY`.
    pub squash_body_source: String,
}

impl Default for Github {
    fn default() -> Self {
        Self {
            squash_title_source: "PR_TITLE".into(),
            squash_body_source: "PR_BODY".into(),
        }
    }
}

/// The `protection.gitlab` table.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Gitlab {
    /// F: invariant, linear history.
    pub merge_method: String,
    /// F: invariant, always squash.
    pub squash_option: String,
    /// F: invariant, references title and description.
    pub squash_commit_template: String,
    /// F: invariant, the level `git.integration` names: zero under forge
    /// integration, which takes no push at all, and the narrowest level
    /// that admits one under local integration, whose integrations end in
    /// that push.
    pub push_access_level: i64,
    /// F: floor thirty.
    pub merge_access_level: i64,
}

impl Default for Gitlab {
    fn default() -> Self {
        Self {
            merge_method: "ff".into(),
            squash_option: "always".into(),
            squash_commit_template: include_str!("../blocks/gitlab-squash-commit-template.in")
                .trim_end_matches('\n')
                .to_owned(),
            push_access_level: 0,
            merge_access_level: 40,
        }
    }
}

/// Read the optional file; content errors refuse instead of falling back.
///
/// # Errors
/// Returns a config-invalid refusal for invalid content, and preserves I/O errors.
pub fn load(target: &Path) -> Result<Option<Config>, RkError> {
    let text = match std::fs::read_to_string(target.join(CONFIG_PATH)) {
        Ok(text) => text,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(error.into()),
    };
    parse(&text).map(Some)
}

/// The text as this binary's schema: a schema 1 file migrated, any other
/// text as it is, so the strict reader judges the schema afterwards.
fn current_text(text: &str) -> Result<String, RkError> {
    let raw: toml::Value =
        toml::from_str(text).map_err(|error: toml::de::Error| invalid(error.to_string()))?;
    match raw.get("schema_version").and_then(toml::Value::as_integer) {
        Some(1) => migrate::to_schema_2(text),
        _ => Ok(text.to_owned()),
    }
}

fn parse(text: &str) -> Result<Config, RkError> {
    let text = current_text(text)?;
    let raw: toml::Value =
        toml::from_str(&text).map_err(|error: toml::de::Error| invalid(error.to_string()))?;
    if raw.get("schema_version").and_then(toml::Value::as_integer) != Some(SCHEMA_VERSION) {
        return Err(invalid(format!(
            "schema_version must be {SCHEMA_VERSION}, or 1 for a file this binary migrates"
        )));
    }
    // The release mode is read as a string first, so a value outside the
    // vocabulary refuses naming its own key rather than a span and a
    // variant list the reader must locate for itself.
    if let Some(mode) = raw
        .get("profile")
        .and_then(|profile| profile.get("release"))
        .and_then(|release| release.get("mode"))
    {
        let named = mode.as_str().ok_or_else(|| {
            invalid("profile.release.mode must be a string: automatic, external, or none")
        })?;
        crate::profile::ReleaseMode::parse(named)
            .map_err(|error| invalid(format!("profile.release.mode: {error}")))?;
    }
    let config: Config = toml::from_str(&text).map_err(|error: toml::de::Error| {
        let mut message = error.to_string();
        if let Some(rest) = error.message().strip_prefix("unknown field `") {
            let names: Vec<_> = rest.split('`').collect();
            if let Some(unknown) = names.first()
                && let Some(nearest) = names
                    .iter()
                    .skip(2)
                    .step_by(2)
                    .min_by_key(|name| distance(unknown, name))
            {
                let _ = write!(message, "; nearest known key: {nearest}");
            }
        }
        invalid(message)
    })?;
    if let Some(technologies) = &config.profile.technologies {
        crate::profile::canonical_list("profile.technologies", technologies).map_err(invalid)?;
    }
    if let Some(forge) = &config.profile.forge
        && !forge.is_empty()
    {
        crate::profile::canonical_category(forge)
            .map_err(|reason| invalid(format!("profile.forge: {reason}")))?;
    }
    if let Some(driver) = &config.profile.release.driver {
        crate::profile::canonical_category(driver)
            .map_err(|reason| invalid(format!("profile.release.driver: {reason}")))?;
    }
    if matches!(
        config.profile.release.mode,
        Some(ReleaseMode::External | ReleaseMode::None)
    ) {
        let mode = config
            .profile
            .release
            .mode
            .map_or("none", ReleaseMode::as_str);
        for (key, present) in [
            (
                "profile.release.driver",
                config.profile.release.driver.is_some(),
            ),
            (
                "profile.release.style",
                config.profile.release.style.is_some(),
            ),
            (
                "profile.release.line_prefix",
                config.profile.release.line_prefix.is_some(),
            ),
        ] {
            if present {
                return Err(invalid(format!(
                    "{key} is set while profile.release.mode is {mode}; an external or none release names no driver, style, or line prefix"
                )));
            }
        }
    }
    if let Some(contact) = &config.security.contact {
        canonical_contact(contact).map_err(invalid)?;
    }
    if let Some(response) = &config.security.response {
        canonical_response(response).map_err(invalid)?;
    }
    exclusions(&config.setup.excluded_steps)?;
    floors::check(&config)?;
    Ok(config)
}

/// Judge the declared exclusions: every id names a step this binary runs,
/// and every exclusion states why.
///
/// A reason is required because the exclusions are the audit trail. A
/// reader must be able to tell a chosen subset from an incomplete setup,
/// and a line that names a step and says nothing tells them neither.
fn exclusions(excluded: &BTreeMap<String, String>) -> Result<(), RkError> {
    for (name, reason) in excluded {
        if crate::setup::steps::spec(name).is_none() {
            let nearest = crate::setup::steps::STEPS
                .iter()
                .min_by_key(|step| distance(name, step.name))
                .map_or("", |step| step.name);
            return Err(invalid(format!(
                "setup.excluded_steps names {name}, which is no setup step; nearest known step: {nearest}"
            )));
        }
        if reason.trim().is_empty() {
            return Err(invalid(format!(
                "setup.excluded_steps names {name} with no reason; an excluded step is reported with why it is out of scope"
            )));
        }
    }
    Ok(())
}

pub(super) fn invalid(message: impl std::fmt::Display) -> RkError {
    RkError::refusal(
        Diagnostic::new(Reason::ConfigInvalid, format!("{CONFIG_PATH}: {message}"))
            .action(format!("edit {CONFIG_PATH} and retry"))
            .target_state("nothing was written"),
    )
}

fn distance(left: &str, right: &str) -> usize {
    let mut row: Vec<_> = (0..=right.chars().count()).collect();
    for (i, a) in left.chars().enumerate() {
        let mut previous = row[0];
        row[0] = i + 1;
        for (j, b) in right.chars().enumerate() {
            let old = row[j + 1];
            row[j + 1] = (previous + usize::from(a != b))
                .min(row[j] + 1)
                .min(old + 1);
            previous = old;
        }
    }
    row.last().copied().unwrap_or(0)
}

/// Write the authored template with TOML-escaped scalar substitutions.
///
/// # Errors
/// Returns invalid configuration or I/O failures before or during the atomic write.
pub fn write(target: &Path, config: &Config) -> Result<(), RkError> {
    let bytes = render(config)?;
    parse(&String::from_utf8_lossy(&bytes))?;
    crate::atomic::write(&target.join(CONFIG_PATH), &bytes)?;
    Ok(())
}

fn array(values: &[String]) -> toml_edit::Value {
    toml_edit::Value::Array(values.iter().collect())
}

/// The exclusions as the one-line inline table the template carries. A
/// landing writes an empty one; an operator who wants several may turn it
/// into a `[setup.excluded_steps]` table, which this reader parses the
/// same way.
fn inline(values: &BTreeMap<String, String>) -> toml_edit::Value {
    let mut table = toml_edit::InlineTable::new();
    for (key, value) in values {
        table.insert(key, value.clone().into());
    }
    toml_edit::Value::InlineTable(table)
}

/// The value of every landing parameter as the template renders it, or
/// `None` for a key the resolved answers omit: the repository where the
/// project has no forge, and the automatic-only release keys under
/// another mode.
#[allow(
    clippy::too_many_lines,
    reason = "the render list is one token per authored line of the config template, and splitting it would hide that correspondence"
)]
fn fields(config: &Config) -> Result<Vec<(&'static str, Option<toml_edit::Value>)>, RkError> {
    // The trunk names the ruleset the setup installs, so the written
    // configuration states the name a target actually gets rather than a
    // literal that would be wrong for any trunk but the default.
    let trunk = config
        .git
        .trunk
        .clone()
        .ok_or_else(|| invalid("git.trunk is unresolved"))?;
    let technologies = config
        .profile
        .technologies
        .clone()
        .ok_or_else(|| invalid("profile.technologies is unresolved"))?;
    let forge = config
        .profile
        .forge
        .clone()
        .ok_or_else(|| invalid("profile.forge is unresolved"))?;
    let mode = config
        .profile
        .release
        .mode
        .ok_or_else(|| invalid("profile.release.mode is unresolved"))?;
    let mut fields: Vec<(&'static str, Option<toml_edit::Value>)> = vec![
        (
            "RK_CONFIG_SCHEMA_VERSION",
            Some(config.schema_version.into()),
        ),
        (
            "RK_CONFIG_PROJECT_REPO",
            (!config.project.repo.is_empty()).then(|| config.project.repo.clone().into()),
        ),
        ("RK_CONFIG_PROFILE_TECHNOLOGIES", Some(array(&technologies))),
        ("RK_CONFIG_PROFILE_FORGE", Some(forge.into())),
        ("RK_CONFIG_PROFILE_RELEASE_MODE", Some(mode.as_str().into())),
        (
            "RK_CONFIG_PROFILE_RELEASE_DRIVER",
            config
                .profile
                .release
                .driver
                .clone()
                .map(toml_edit::Value::from),
        ),
        (
            "RK_CONFIG_PROFILE_RELEASE_STYLE",
            config
                .profile
                .release
                .style
                .map(|style| style.as_str().into()),
        ),
        (
            "RK_CONFIG_PROFILE_RELEASE_LINE_PREFIX",
            config
                .profile
                .release
                .line_prefix
                .clone()
                .map(toml_edit::Value::from),
        ),
        ("RK_CONFIG_GIT_TRUNK", Some(trunk.clone().into())),
        (
            "RK_CONFIG_GIT_CHECKOUT_MODE",
            Some(
                config
                    .git
                    .checkout_mode
                    .ok_or_else(|| invalid("git.checkout_mode is unresolved"))?
                    .as_str()
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_GIT_INTEGRATION",
            Some(
                config
                    .git
                    .integration
                    .ok_or_else(|| invalid("git.integration is unresolved"))?
                    .as_str()
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_CAPABILITIES_NIX_PACKAGING",
            Some(
                config
                    .capabilities
                    .nix_packaging
                    .ok_or_else(|| invalid("capabilities.nix_packaging is unresolved"))?
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_CAPABILITIES_REPORTING_POLICY",
            Some(
                config
                    .capabilities
                    .reporting_policy
                    .ok_or_else(|| invalid("capabilities.reporting_policy is unresolved"))?
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_CAPABILITIES_SCORECARD",
            Some(
                config
                    .capabilities
                    .scorecard
                    .ok_or_else(|| invalid("capabilities.scorecard is unresolved"))?
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_CAPABILITIES_CODE_SCANNING",
            Some(
                config
                    .capabilities
                    .code_scanning
                    .clone()
                    .ok_or_else(|| invalid("capabilities.code_scanning is unresolved"))?
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_SECURITY_ADVISORIES",
            Some(config.security.advisories.clone().into()),
        ),
        (
            "RK_CONFIG_SECURITY_CONTACT",
            Some(config.security.contact.clone().unwrap_or_default().into()),
        ),
        (
            "RK_CONFIG_SECURITY_RESPONSE",
            Some(
                config
                    .security
                    .response
                    .clone()
                    .unwrap_or_else(|| RESPONSE_DEFAULT.to_owned())
                    .into(),
            ),
        ),
        (
            "RK_CONFIG_SETUP_REQUIRED_CHECK",
            Some(config.setup.required_check.clone().into()),
        ),
        (
            "RK_CONFIG_SETUP_REQUIRED_WORKFLOW",
            Some(config.setup.required_workflow.clone().into()),
        ),
        (
            "RK_CONFIG_SETUP_RETIRED_BRANCHES",
            Some(array(&config.setup.retired_branches)),
        ),
        (
            "RK_CONFIG_SETUP_RELEASE_LINES",
            Some(config.setup.release_lines.into()),
        ),
        (
            "RK_CONFIG_SETUP_EXCLUDED_STEPS",
            Some(inline(&config.setup.excluded_steps)),
        ),
        (
            "RK_CONFIG_SETUP_BOT_APP_ID",
            Some(config.setup.bot.app_id.clone().into()),
        ),
    ];
    fields.extend(
        protection_fields(&config.protection, trunk.as_str())
            .into_iter()
            .map(|(token, value)| (token, Some(value))),
    );
    Ok(fields)
}

fn render(config: &Config) -> Result<Vec<u8>, RkError> {
    let fields = fields(config)?;
    let template = crate::embedded::BLOCKS
        .get_file("target-config.toml.in")
        .and_then(include_dir::File::contents_utf8)
        .ok_or_else(|| invalid("the binary lacks its configuration template"))?;
    // Each authored line has one token. Substitute in the source line once,
    // so a user's string containing another token stays literal. A line
    // whose token has no value is dropped: the key is absent rather than
    // written empty, and a table every one of whose keys dropped goes with
    // its own header rather than standing empty.
    let lines: Vec<&str> = template.split_inclusive('\n').collect();
    let dropped: Vec<bool> = lines
        .iter()
        .map(|line| {
            matches!(
                fields.iter().find(|(token, _)| line.contains(token)),
                Some((_, None))
            )
        })
        .collect();
    let mut bytes = Vec::new();
    let mut at = 0;
    while at < lines.len() {
        let line = lines[at];
        if line.trim_start().starts_with('[') && line.trim_end().ends_with(']') {
            let mut end = at + 1;
            let mut keeps = false;
            while end < lines.len()
                && !(lines[end].trim_start().starts_with('[')
                    && lines[end].trim_end().ends_with(']'))
            {
                keeps |=
                    fields.iter().any(|(token, _)| lines[end].contains(token)) && !dropped[end];
                end += 1;
            }
            if !keeps {
                // The header, its lines, and the blank line that opened it.
                while bytes.last() == Some(&b'\n')
                    && bytes.len() >= 2
                    && bytes[bytes.len() - 2] == b'\n'
                {
                    bytes.pop();
                }
                at = end;
                continue;
            }
        }
        if !dropped[at] {
            match fields.iter().find(|(token, _)| line.contains(token)) {
                Some((token, Some(value))) => bytes.extend(crate::landing::substitute(
                    line.as_bytes(),
                    token.as_bytes(),
                    value.to_string().as_bytes(),
                )),
                Some((_, None)) => {}
                None => bytes.extend_from_slice(line.as_bytes()),
            }
        }
        at += 1;
    }
    Ok(bytes)
}

fn protection_fields(
    protection: &Protection,
    trunk: &str,
) -> Vec<(&'static str, toml_edit::Value)> {
    vec![
        (
            "RK_CONFIG_PROTECTION_TRUNK_RULESET",
            protection.trunk_ruleset(trunk).into(),
        ),
        (
            "RK_CONFIG_PROTECTION_SAFETY_RULESET",
            protection.safety_ruleset(trunk).into(),
        ),
        (
            "RK_CONFIG_PROTECTION_TAG_RULESET",
            protection.tag_ruleset.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_LINES_RULESET",
            protection.lines_ruleset.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_TITLE_CHECK",
            protection.title_check.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_TAG_PATTERN",
            protection.tag_pattern.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_BYPASS_ACTORS",
            array(&protection.bypass_actors),
        ),
        (
            "RK_CONFIG_PROTECTION_ALLOWED_MERGE_METHODS",
            array(&protection.allowed_merge_methods),
        ),
        (
            "RK_CONFIG_PROTECTION_STRICT_REQUIRED_STATUS_CHECKS",
            protection.strict_required_status_checks.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_OWNED_TRUNK_RULES",
            array(&protection.owned_trunk_rules),
        ),
        (
            "RK_CONFIG_PROTECTION_REQUIRED_APPROVING_REVIEW_COUNT",
            protection.required_approving_review_count.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_DISMISS_STALE_REVIEWS_ON_PUSH",
            protection.dismiss_stale_reviews_on_push.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_REQUIRE_CODE_OWNER_REVIEW",
            protection.require_code_owner_review.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_REQUIRE_LAST_PUSH_APPROVAL",
            protection.require_last_push_approval.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITHUB_SQUASH_TITLE_SOURCE",
            protection.github.squash_title_source.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITHUB_SQUASH_BODY_SOURCE",
            protection.github.squash_body_source.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITLAB_MERGE_METHOD",
            protection.gitlab.merge_method.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITLAB_SQUASH_OPTION",
            protection.gitlab.squash_option.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITLAB_SQUASH_COMMIT_TEMPLATE",
            protection.gitlab.squash_commit_template.clone().into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITLAB_PUSH_ACCESS_LEVEL",
            protection.gitlab.push_access_level.into(),
        ),
        (
            "RK_CONFIG_PROTECTION_GITLAB_MERGE_ACCESS_LEVEL",
            protection.gitlab.merge_access_level.into(),
        ),
    ]
}

/// The two floored keys a write-back carries beside `git.integration`.
///
/// They are derived companions of the authority rather than recorded
/// parameters: the record holds no baseline for them, so a target that
/// narrowed either within its floor would read as pending forever and
/// route an upgrade that changes nothing.
/// `target-config:a-flag-overrides-and-a-landing-writes-back` leaves
/// class F policy with its use-time readers, and excluding these from
/// what status judges keeps that true.
const DERIVED_POLICY_KEYS: [&str; 3] = [
    "protection.bypass_actors",
    "protection.owned_trunk_rules",
    "protection.gitlab.push_access_level",
];

/// The keys a landing writes back: every class P answer.
const PARAMETER_KEYS: [&str; 22] = [
    "project.repo",
    "profile.technologies",
    "profile.forge",
    "profile.release.mode",
    "profile.release.driver",
    "profile.release.style",
    "profile.release.line_prefix",
    "git.trunk",
    "git.checkout_mode",
    "git.integration",
    // The three floored keys the integration authority decides. They are
    // written back with it, because changing the authority without them
    // leaves a configuration whose own floor table refuses it.
    "protection.bypass_actors",
    "protection.owned_trunk_rules",
    "protection.gitlab.push_access_level",
    "capabilities.nix_packaging",
    "capabilities.reporting_policy",
    "capabilities.scorecard",
    "capabilities.code_scanning",
    "security.contact",
    "security.response",
    // The release gate's two answers. One names the check the gate
    // believes, the other names the workflow whose completion wakes it.
    "setup.required_check",
    "setup.required_workflow",
    "schema_version",
];

/// Change one landing parameter while preserving comments and table ordering.
///
/// # Errors
/// Refuses an invalid key, invalid resulting content, or unreadable file; writes atomically.
pub fn rewrite_key(target: &Path, key: &str, value: toml_edit::Value) -> Result<(), RkError> {
    let path = target.join(CONFIG_PATH);
    let text = std::fs::read_to_string(&path)?;
    let next = rewrite_text(&text, key, Some(value))?;
    crate::atomic::write(&path, next.as_bytes())?;
    Ok(())
}

/// The text with `key` set to `value`, or removed where `value` is
/// `None`, every other byte kept. A schema 1 text migrates first.
fn rewrite_text(text: &str, key: &str, value: Option<toml_edit::Value>) -> Result<String, RkError> {
    rewrite_all(text, vec![(key, value)])
}

/// The text with every named key set or removed in one document, every
/// other byte kept. A schema 1 text migrates first.
///
/// One document rather than one per key, because the answers are one
/// decision: writing `profile.release.mode = "none"` before removing the
/// driver it retires would make an intermediate text the reader rejects,
/// and an operator would have no command that performs the transition.
fn rewrite_all(
    text: &str,
    values: Vec<(&str, Option<toml_edit::Value>)>,
) -> Result<String, RkError> {
    for (key, _) in &values {
        if !PARAMETER_KEYS.contains(key) {
            return Err(invalid(format!("{key} is not a landing parameter")));
        }
    }
    parse(text)?;
    let text = current_text(text)?;
    let mut document = text
        .parse::<toml_edit::DocumentMut>()
        .map_err(|error| invalid(error.to_string()))?;
    let mut changed = false;
    // Only a table this call emptied may be pruned, so the names come from
    // the removals rather than from the whole key list.
    let mut emptied: Vec<&str> = Vec::new();
    for (key, value) in values {
        let removing = value.is_none();
        let applied = apply_key(&mut document, key, value)?;
        changed |= applied;
        if applied
            && removing
            && let Some(parent) = key.split('.').next()
        {
            emptied.push(parent);
        }
    }
    changed |= prune_empty_tables(&mut document, &emptied);
    // A value this writer rewrites keeps the comment the file already
    // carried, so the template's own sentence about a key outlives the
    // answer it describes: a target moving to local integration would read
    // `# F: invariant, contains all four rules` beside two of them. The
    // refresh replaces the template's comments alone and leaves every one
    // the operator wrote.
    changed |= migrate::refresh_comments(&mut document);
    if !changed {
        return Ok(text);
    }
    let next = document.to_string();
    parse(&next)?;
    Ok(next)
}

/// Drop the named tables that are now empty, answering whether the
/// document changed.
///
/// `target-config:an-unanswered-key-is-absent-and-not-empty` asks the
/// writer to leave out a table every one of whose keys it omitted, and the
/// fresh render already does. This is the same rule on the update path,
/// which edits authored text instead: without it a target that drops its
/// forge keeps a bare `[project]` header, and the two writers disagree
/// about one resolved answer.
///
/// Only the named tables, because a table the operator authored empty is
/// theirs and this writer emptied nothing in it.
///
/// Every comment the removed header carried, standing above it or inline
/// beside it, moves to the next table, or to the end of the file where the
/// removed table was last. The header is this binary's; the comment may be
/// the operator's, and an upgrade that silently deleted one would be
/// losing authored text.
pub(crate) fn prune_empty_tables(document: &mut toml_edit::DocumentMut, names: &[&str]) -> bool {
    let order: Vec<String> = document
        .as_table()
        .iter()
        .map(|(key, _)| key.to_owned())
        .collect();
    let mut changed = false;
    for (index, name) in order.iter().enumerate() {
        if !names.contains(&name.as_str())
            || !document
                .get(name)
                .and_then(toml_edit::Item::as_table)
                .is_some_and(toml_edit::Table::is_empty)
        {
            continue;
        }
        let carried = document
            .get(name)
            .and_then(toml_edit::Item::as_table)
            .and_then(|table| carried_comment(table.decor()));
        document.remove(name);
        changed = true;
        let Some(carried) = carried else { continue };
        // The next header the file actually prints: an implicit table
        // emits no header of its own, so its decor would take the comment
        // out of the rendered text with it.
        let next = order
            .iter()
            .skip(index + 1)
            .find(|name| {
                document
                    .get(name)
                    .and_then(toml_edit::Item::as_table)
                    .is_some_and(|table| !table.is_implicit())
            })
            .cloned();
        if let Some(next) = next
            && let Some(table) = document
                .get_mut(&next)
                .and_then(toml_edit::Item::as_table_mut)
        {
            let existing = table
                .decor()
                .prefix()
                .and_then(toml_edit::RawString::as_str)
                .unwrap_or_default()
                .trim_start_matches('\n')
                .to_owned();
            table
                .decor_mut()
                .set_prefix(format!("\n{carried}{existing}"));
        } else {
            let mut trailing = document.trailing().as_str().unwrap_or_default().to_owned();
            if !trailing.is_empty() && !trailing.ends_with('\n') {
                trailing.push('\n');
            }
            trailing.push_str(&carried);
            document.set_trailing(trailing);
        }
    }
    changed
}

/// Every comment in a decor, one per line, or `None` where it carries
/// none.
///
/// An inline comment beside a header becomes a free-standing line, because
/// the header it sat beside is going and a comment needs a line of its own
/// to survive.
fn carried_comment(decor: &toml_edit::Decor) -> Option<String> {
    let mut lines = String::new();
    for raw in [decor.prefix(), decor.suffix()] {
        let Some(text) = raw.and_then(toml_edit::RawString::as_str) else {
            continue;
        };
        for line in text
            .lines()
            .map(str::trim)
            .filter(|line| line.starts_with('#'))
            .filter(|line| !is_template_comment(line))
        {
            lines.push_str(line);
            lines.push('\n');
        }
    }
    (!lines.is_empty()).then_some(lines)
}

/// Whether the comment is the template's own rather than the operator's.
///
/// The template marks every comment it writes with the class of the key it
/// sits beside: `# P:` a landing parameter, `# N:` a free name, `# F:` an
/// invariant floor. A comment for a key that is going describes nothing
/// once the key is gone, so it goes too, while anything the operator wrote
/// is carried. `refresh_comment` owns the same three markers on the
/// migration path.
fn is_template_comment(line: &str) -> bool {
    let rest = line.trim_start_matches('#').trim_start();
    ["P:", "N:", "F:"]
        .iter()
        .any(|marker| rest.starts_with(marker))
}

/// Put carried comment lines where the rendered file will still show
/// them: above the first header it prints, or at its end where it prints
/// none.
///
/// The last resort for text whose own domain is not rendered. A comment
/// with no home is still the operator's, and the end of the file is where
/// it survives.
pub(crate) fn place_carried(document: &mut toml_edit::DocumentMut, carried: &str) {
    let first = document
        .as_table()
        .iter()
        .find(|(_, item)| item.as_table().is_some_and(|table| !table.is_implicit()))
        .map(|(name, _)| name.to_owned());
    if let Some(first) = first
        && let Some(table) = document
            .get_mut(&first)
            .and_then(toml_edit::Item::as_table_mut)
    {
        let existing = table
            .decor()
            .prefix()
            .and_then(toml_edit::RawString::as_str)
            .unwrap_or_default()
            .trim_start_matches('\n')
            .to_owned();
        table
            .decor_mut()
            .set_prefix(format!("\n{carried}{existing}"));
        return;
    }
    let mut trailing = document.trailing().as_str().unwrap_or_default().to_owned();
    if !trailing.is_empty() && !trailing.ends_with('\n') {
        trailing.push('\n');
    }
    trailing.push_str(carried);
    document.set_trailing(trailing);
}

/// The operator's comments standing above `key`, or `None` where it
/// carries none.
///
/// Read before a move, so the comment travels with the value it describes
/// rather than staying beside a key that is gone.
pub(crate) fn key_comments(table: &toml_edit::Table, key: &str) -> Option<String> {
    let (name, _) = table.get_key_value(key)?;
    carried_comment(name.leaf_decor())
}

/// Put `carried` above `key`, keeping whatever decor it already has.
pub(crate) fn set_key_comments(table: &mut toml_edit::Table, key: &str, carried: &str) {
    let Some(mut name) = table.key_mut(key) else {
        return;
    };
    let decor = name.leaf_decor_mut();
    let existing = decor
        .prefix()
        .and_then(toml_edit::RawString::as_str)
        .unwrap_or_default()
        .trim_start_matches('\n')
        .to_owned();
    decor.set_prefix(format!("\n{carried}{existing}"));
}

/// The nearest known name to `unknown`, for a refusal that helps.
#[must_use]
pub(crate) fn nearest_known<'a>(unknown: &str, known: &[&'a str]) -> Option<&'a str> {
    known
        .iter()
        .min_by_key(|name| distance(unknown, name))
        .copied()
}

/// The operator's comments standing on a table's own header, removed from
/// it.
pub(crate) fn take_header_comments(table: &mut toml_edit::Table) -> Option<String> {
    let carried = carried_comment(table.decor())?;
    table.decor_mut().set_prefix("\n");
    Some(carried)
}

/// Remove `key` from `table`, answering the operator's comments it
/// carried.
///
/// A comment the operator wrote above or beside a key outlives the answer
/// it described: `project-profile:a-schema-one-configuration-migrates-in-place`
/// asks for every free-standing comment to survive, and an upgrade that
/// retires a key is the same promise on the other writer. The comments
/// move to the table's own header, where they read as a note on the domain
/// the key belonged to, and travel further with that header if the table
/// itself empties.
pub(crate) fn take_comments(table: &mut toml_edit::Table, key: &str) -> bool {
    let carried = table.get_key_value(key).and_then(|(name, item)| {
        let mut lines = carried_comment(name.leaf_decor()).unwrap_or_default();
        if let Some(value) = item.as_value()
            && let Some(more) = carried_comment(value.decor())
        {
            lines.push_str(&more);
        }
        (!lines.is_empty()).then_some(lines)
    });
    let removed = table.remove(key).is_some();
    if let Some(carried) = carried {
        let existing = table
            .decor()
            .prefix()
            .and_then(toml_edit::RawString::as_str)
            .unwrap_or_default()
            .trim_start_matches('\n')
            .to_owned();
        table
            .decor_mut()
            .set_prefix(format!("\n{carried}{existing}"));
    }
    removed
}

/// Set or remove one key in an open document, answering whether the
/// document changed. No validation: the caller validates the whole.
fn apply_key(
    document: &mut toml_edit::DocumentMut,
    key: &str,
    value: Option<toml_edit::Value>,
) -> Result<bool, RkError> {
    let segments: Vec<&str> = key.split('.').collect();
    // Every key in `PARAMETER_KEYS` has at least one segment, so the split
    // answers; a key that did not would have refused above.
    let Some((last, parents)) = segments.split_last() else {
        return Err(invalid(format!("{key} names no key")));
    };
    let Some(mut value) = value else {
        let mut item = document.as_item_mut();
        for segment in parents {
            if item.get(segment).is_none() {
                return Ok(false);
            }
            item = &mut item[segment];
        }
        let removed = item
            .as_table_mut()
            .is_some_and(|table| take_comments(table, last));
        return Ok(removed);
    };
    let mut item = document.as_item_mut();
    for segment in parents {
        if item.get(segment).is_none() {
            let mut table = toml_edit::Table::new();
            table.set_implicit(true);
            item[segment] = toml_edit::Item::Table(table);
        }
        item = &mut item[segment];
    }
    if let Some(old) = item.get(last).and_then(toml_edit::Item::as_value) {
        if old.to_string().trim() == value.to_string().trim() {
            return Ok(false);
        }
        *value.decor_mut() = old.decor().clone();
    } else if let Some(comment) = migrate::template_comment(&segments) {
        // A key this writer is adding rather than changing carries no
        // decor of the operator's, so it takes the template's own
        // comment. Without this a parameter that arrives in a later
        // release lands bare in every existing target's configuration,
        // beside keys that all state their class and their meaning.
        value.decor_mut().set_suffix(comment);
    }
    item[last] = toml_edit::Item::Value(value);
    Ok(true)
}

/// Resolved landing input, including every key a preview would write.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Plan {
    /// Added or updated configuration.
    pub action: &'static str,
    /// Keys whose configured answers differ from the record.
    pub changes: Vec<String>,
    /// The exact authored TOML the apply writes.
    pub content: String,
}

impl Plan {
    /// Resolve the output without writing it; existing comments survive.
    ///
    /// # Errors
    /// Propagates unreadable or invalid configuration.
    pub fn new(
        target: &Path,
        params: &crate::landing::Params,
        existing: Option<&Config>,
        record: Option<&crate::landing::manifest::Manifest>,
    ) -> Result<Self, RkError> {
        let text = existing
            .map(|_| std::fs::read_to_string(target.join(CONFIG_PATH)))
            .transpose()?;
        Self::compose(text.as_deref(), params, existing, record)
    }

    /// Resolve the output from the existing text already read, so a
    /// planner that owns no filesystem can compose it from its
    /// observation; existing comments survive.
    ///
    /// # Errors
    /// Propagates invalid configuration.
    pub fn compose(
        text: Option<&str>,
        params: &crate::landing::Params,
        existing: Option<&Config>,
        record: Option<&crate::landing::manifest::Manifest>,
    ) -> Result<Self, RkError> {
        let mut resolved = existing.cloned().unwrap_or_default();
        resolved.schema_version = SCHEMA_VERSION;
        params.repo().clone_into(&mut resolved.project.repo);
        resolved.profile = Profile {
            technologies: Some(params.technologies().to_vec()),
            forge: Some(params.forge().unwrap_or_default().to_owned()),
            release: Release {
                mode: Some(params.release_mode()),
                driver: params.driver().map(str::to_owned),
                style: params.style(),
                line_prefix: params.profile().release.line_prefix.clone(),
            },
        };
        resolved.git = Git {
            trunk: Some(params.trunk().to_owned()),
            checkout_mode: Some(params.checkout_mode()),
            integration: Some(params.integration()),
        };
        resolved.protection = protection_for(
            &resolved.protection,
            params.integration(),
            existing.is_none(),
        );
        resolved.capabilities = Capabilities {
            nix_packaging: Some(params.nix_packaging()),
            reporting_policy: Some(params.reporting_policy()),
            scorecard: Some(params.scorecard()),
            code_scanning: Some(
                params
                    .code_scanning()
                    .map_or("off", crate::landing::Provider::as_str)
                    .to_owned(),
            ),
        };
        resolved.security.contact = Some(params.security_contact().to_owned());
        resolved.security.response = Some(params.security_response().to_owned());
        params
            .required_check()
            .clone_into(&mut resolved.setup.required_check);
        params
            .required_workflow()
            .clone_into(&mut resolved.setup.required_workflow);
        let content = if let Some(text) = text.filter(|_| existing.is_some()) {
            rewrite_all(text, parameter_values(&resolved))?
        } else {
            String::from_utf8(render(&resolved)?).map_err(|e| invalid(e.to_string()))?
        };
        parse(&content)?;
        Ok(Self {
            action: if existing.is_some() {
                "updated"
            } else {
                "added"
            },
            changes: record.map_or_else(Vec::new, |record| pending(&resolved, record)),
            content,
        })
    }

    /// Write the prepared configuration before the landing record.
    ///
    /// # Errors
    /// Propagates an atomic write failure.
    pub fn apply(&self, target: &Path) -> Result<(), RkError> {
        crate::atomic::write(&target.join(CONFIG_PATH), self.content.as_bytes())?;
        Ok(())
    }
}

/// Every class P key with its value, `None` for a key the answers omit.
fn parameter_values(config: &Config) -> Vec<(&'static str, Option<toml_edit::Value>)> {
    let mut values: Vec<(&'static str, Option<toml_edit::Value>)> = vec![(
        "project.repo",
        (!config.project.repo.is_empty()).then(|| config.project.repo.clone().into()),
    )];
    if let Some(list) = &config.profile.technologies {
        values.push(("profile.technologies", Some(array(list))));
    }
    if let Some(forge) = config.profile.forge.clone() {
        values.push(("profile.forge", Some(forge.into())));
    }
    if let Some(mode) = config.profile.release.mode {
        values.push(("profile.release.mode", Some(mode.as_str().into())));
        values.push((
            "profile.release.driver",
            config
                .profile
                .release
                .driver
                .clone()
                .map(toml_edit::Value::from),
        ));
        values.push((
            "profile.release.style",
            config
                .profile
                .release
                .style
                .map(|style| style.as_str().into()),
        ));
        values.push((
            "profile.release.line_prefix",
            config
                .profile
                .release
                .line_prefix
                .clone()
                .map(toml_edit::Value::from),
        ));
    }
    if let Some(trunk) = config.git.trunk.clone() {
        values.push(("git.trunk", Some(trunk.into())));
    }
    if let Some(mode) = config.git.checkout_mode {
        values.push(("git.checkout_mode", Some(mode.as_str().into())));
    }
    if let Some(mode) = config.git.integration {
        values.push(("git.integration", Some(mode.as_str().into())));
        // The policy tuple the authority decides travels with it: a write-back
        // that moved the mode alone would leave a configuration the
        // floor table refuses on the next read.
        let mut bypass = toml_edit::Array::new();
        for actor in &config.protection.bypass_actors {
            bypass.push(actor.as_str());
        }
        values.push((
            "protection.bypass_actors",
            Some(toml_edit::Value::Array(bypass)),
        ));
        let mut rules = toml_edit::Array::new();
        for rule in &config.protection.owned_trunk_rules {
            rules.push(rule.as_str());
        }
        values.push((
            "protection.owned_trunk_rules",
            Some(toml_edit::Value::Array(rules)),
        ));
        values.push((
            "protection.gitlab.push_access_level",
            Some(config.protection.gitlab.push_access_level.into()),
        ));
    }
    if let Some(value) = config.capabilities.nix_packaging {
        values.push(("capabilities.nix_packaging", Some(value.into())));
    }
    if let Some(value) = config.capabilities.reporting_policy {
        values.push(("capabilities.reporting_policy", Some(value.into())));
    }
    if let Some(value) = config.capabilities.scorecard {
        values.push(("capabilities.scorecard", Some(value.into())));
    }
    if let Some(value) = config.capabilities.code_scanning.clone() {
        values.push(("capabilities.code_scanning", Some(value.into())));
    }
    // An empty contact is an answer, not an absence: it resets the landed
    // policy to the forge's own prose, so it projects like any other value.
    if let Some(value) = config.security.contact.clone() {
        values.push(("security.contact", Some(value.into())));
    }
    if let Some(value) = config.security.response.clone() {
        values.push(("security.response", Some(value.into())));
    }
    // The gate's two answers project like any other class P string. An
    // empty answer is an answer: it says this target renders no standing
    // release gate, which is true of every target outside GitHub's local
    // trunk release shape.
    values.push((
        "setup.required_check",
        Some(config.setup.required_check.clone().into()),
    ));
    values.push((
        "setup.required_workflow",
        Some(config.setup.required_workflow.clone().into()),
    ));
    values
}

/// Only explicit class P answers can be pending; comparisons still use the record.
#[must_use]
pub fn pending(config: &Config, record: &crate::landing::manifest::Manifest) -> Vec<String> {
    let recorded = {
        let params = crate::landing::Params::from_record(record);
        Plan::compose(None, &params, None, None)
            .ok()
            .and_then(|plan| parse(&plan.content).ok())
    };
    let Some(recorded) = recorded else {
        return Vec::new();
    };
    let render = |value: &Option<toml_edit::Value>| {
        value.as_ref().map_or_else(
            || "<absent>".to_owned(),
            |value| value.to_string().trim().to_owned(),
        )
    };
    let baseline: Vec<(&str, String)> = parameter_values(&recorded)
        .iter()
        .filter(|(key, _)| !DERIVED_POLICY_KEYS.contains(key))
        .map(|(key, value)| (*key, render(value)))
        .collect();
    parameter_values(config)
        .into_iter()
        .filter(|(key, _)| !DERIVED_POLICY_KEYS.contains(key))
        .map(|(key, value)| (key, render(&value)))
        .filter(|(key, value)| {
            baseline
                .iter()
                .any(|(other, old)| key == other && value != old)
        })
        .map(|(key, _)| key.to_owned())
        .collect()
}

/// The compiled protection floors a locally integrated trunk carries.
///
/// The ordinary defaults describe forge integration, because that is the
/// shape this convention had before the authority became an axis. A
/// locally integrated trunk keeps the full GitHub rule set and grants the
/// repository-administrator role the bypass its deliberate push needs. On
/// GitLab it takes the narrowest access level that still admits that push.
#[must_use]
pub(crate) fn local_protection() -> Protection {
    /// The narrowest GitLab access level that still admits a push.
    const MAINTAINER: i64 = 40;
    Protection {
        bypass_actors: vec![LOCAL_GITHUB_BYPASS.into()],
        gitlab: Gitlab {
            push_access_level: MAINTAINER,
            ..Gitlab::default()
        },
        ..Protection::default()
    }
}

/// Whether this is the compiled local policy from before release-request
/// freshness moved back into the trunk ruleset.
#[must_use]
pub(crate) fn legacy_local_protection(policy: &Protection) -> bool {
    policy.bypass_actors.is_empty()
        && policy.owned_trunk_rules == ["deletion", "non_fast_forward"]
        && policy.gitlab.push_access_level >= 40
}

/// The protection values a landing writes, for one integration authority.
///
/// Exactly three keys differ between the authorities, and they are the
/// tuple this looks at: the GitHub bypass actors, the owned trunk rules,
/// and the GitLab push access level. A target whose tuple matches one
/// authority's compiled defaults never stated them; it took them, so a
/// landing that resolves the other authority writes that authority's tuple
/// instead, and a fresh landing
/// writes its own. A target whose tuple matches neither is one an operator
/// narrowed or widened, and it keeps every value it stated: the floor
/// table already judged it under the same mode, and an operator who
/// changed a protection meant it.
///
/// The tuple alone, never the whole policy: every other key here is a name
/// or a review policy the authority does not decide, and a target that
/// renamed its ruleset would otherwise read as having stated the pair.
///
/// This is what makes the committed configuration the one source: the
/// floors judge these values, the setup installs them, and its check
/// reads them back, so a local-integration target is never handed a trunk
/// its own integrations cannot push.
#[must_use]
fn protection_for(held: &Protection, integration: Integration, fresh: bool) -> Protection {
    let forge = Protection::default();
    let local = local_protection();
    let tuple = |policy: &Protection| {
        (
            policy.bypass_actors.clone(),
            policy.owned_trunk_rules.clone(),
            policy.gitlab.push_access_level,
        )
    };
    let taken = fresh
        || tuple(held) == tuple(&forge)
        || tuple(held) == tuple(&local)
        || legacy_local_protection(held);
    if !taken {
        return held.clone();
    }
    let mut next = held.clone();
    let source = match integration {
        Integration::Forge => forge,
        Integration::Local => local,
    };
    next.bypass_actors = source.bypass_actors;
    next.owned_trunk_rules = source.owned_trunk_rules;
    next.gitlab.push_access_level = source.gitlab.push_access_level;
    next
}

/// The trunk accessor for callers without a setup context.
///
/// # Errors
/// Propagates invalid configuration and I/O failures.
pub fn trunk_of(target: &Path) -> Result<String, RkError> {
    Ok(load(target)?
        .and_then(|config| config.git.trunk)
        .unwrap_or_else(|| TRUNK_DEFAULT.to_owned()))
}

/// The release-line prefix for callers without a setup context.
///
/// # Errors
/// Propagates invalid configuration and I/O failures.
pub fn line_prefix_of(target: &Path) -> Result<String, RkError> {
    Ok(load(target)?
        .and_then(|config| config.profile.release.line_prefix)
        .unwrap_or_else(|| LINE_PREFIX_DEFAULT.to_owned()))
}

#[cfg(test)]
mod tests {
    use super::{CONFIG_PATH, Config, load, parse, rewrite_key, trunk_of, write};
    use crate::landing::{CheckoutMode, Integration, Style};
    use crate::profile::ReleaseMode;

    /// The resolved defaults a landing writes for an automatic rust
    /// release on GitHub.
    fn resolved_defaults() -> Config {
        Config {
            project: super::Project {
                repo: "acme/widget".into(),
            },
            profile: super::Profile {
                technologies: Some(vec!["rust".into()]),
                forge: Some("github".into()),
                release: super::Release {
                    mode: Some(ReleaseMode::Automatic),
                    driver: Some("rust".into()),
                    style: Some(Style::Trunk),
                    line_prefix: Some(super::LINE_PREFIX_DEFAULT.into()),
                },
            },
            git: super::Git {
                trunk: Some(super::TRUNK_DEFAULT.into()),
                checkout_mode: Some(CheckoutMode::LinkedWorktree),
                integration: Some(Integration::Local),
            },
            capabilities: super::Capabilities {
                nix_packaging: Some(false),
                reporting_policy: Some(true),
                scorecard: Some(false),
                code_scanning: Some("off".to_owned()),
            },
            // Writing states both security answers, so a reader sees the
            // policy the target landed rather than an implied one.
            security: super::Security {
                contact: Some(String::new()),
                response: Some(super::RESPONSE_DEFAULT.into()),
                ..super::Security::default()
            },
            // Writing resolves the derived ruleset name, so the file states
            // the name the setup installs rather than leaving it implied.
            protection: super::Protection {
                trunk_ruleset: Some(format!("{}-protection", super::TRUNK_DEFAULT)),
                safety_ruleset: Some(format!("{}-safety", super::TRUNK_DEFAULT)),
                ..super::local_protection()
            },
            ..Config::default()
        }
    }

    #[test]
    fn an_omitted_key_is_distinguishable_from_an_explicit_default() {
        let omitted = parse("schema_version = 2\n").expect("omitted answers parse");
        let explicit = parse(
            "schema_version = 2\n[git]\ncheckout_mode = 'linked-worktree'\n[profile.release]\nmode = 'automatic'\nstyle = 'trunk'\n[capabilities]\nnix_packaging = false\nreporting_policy = true\nscorecard = false\ncode_scanning = 'off'\n",
        )
        .expect("explicit defaults parse");
        assert_eq!(omitted.git, super::Git::default());
        assert_eq!(omitted.capabilities, super::Capabilities::default());
        assert_eq!(
            explicit.git.checkout_mode,
            Some(CheckoutMode::LinkedWorktree)
        );
        assert_eq!(explicit.profile.release.style, Some(Style::Trunk));
        assert_eq!(explicit.capabilities.nix_packaging, Some(false));
        assert_eq!(explicit.capabilities.reporting_policy, Some(true));
        assert_eq!(explicit.capabilities.scorecard, Some(false));
        assert_eq!(explicit.capabilities.code_scanning.as_deref(), Some("off"));
        assert_ne!(omitted, explicit);
        // The older spellings of the checkout mode still read.
        let older = parse("schema_version = 2\n[git]\ncheckout_mode = 'worktree'\n")
            .expect("the older spelling reads");
        assert_eq!(older.git.checkout_mode, Some(CheckoutMode::LinkedWorktree));
    }

    /// A configuration written by 0.3.13 carries `installation_id`, which
    /// this version reads and ignores. Refusing it would strand every
    /// target that release landed.
    #[test]
    fn a_config_from_the_release_that_wrote_installation_id_still_reads() {
        let dir = tempfile::tempdir().expect("a tempdir");
        std::fs::create_dir_all(dir.path().join(".release-kit")).expect("the directory exists");
        std::fs::write(
            dir.path().join(CONFIG_PATH),
            "schema_version = 1\n\n[setup.bot]\napp_id = \"123\"\ninstallation_id = 0\n",
        )
        .expect("the config writes");
        let held = load(dir.path())
            .expect("the config reads")
            .expect("it is present");
        assert_eq!(held.setup.bot.app_id, "123");
        assert_eq!(
            held.setup.bot.installation_id,
            Some(0),
            "the key parses; nothing reads it"
        );
    }

    /// SATISFIES project-profile:a-schema-one-configuration-migrates-in-place
    #[test]
    fn a_schema_1_config_migrates_into_its_domains() {
        let held = parse(
            "schema_version = 1\n[project]\nrepo = 'acme/widget'\nforge = 'gitlab'\ntech = 'bash'\ntrunk = 'main'\n[landing]\nworkflow = 'branches'\nstyle = 'lines'\nnix = false\n[setup]\nline_prefix = 'stable/'\n",
        )
        .expect("a schema 1 file reads");
        assert_eq!(held.schema_version, 2);
        assert_eq!(held.profile.technologies, Some(vec!["bash".to_owned()]));
        assert_eq!(held.profile.forge.as_deref(), Some("gitlab"));
        assert_eq!(held.profile.release.mode, Some(ReleaseMode::Automatic));
        assert_eq!(held.profile.release.driver.as_deref(), Some("bash"));
        assert_eq!(held.profile.release.style, Some(Style::Lines));
        assert_eq!(held.profile.release.line_prefix.as_deref(), Some("stable/"));
        assert_eq!(held.git.trunk.as_deref(), Some("main"));
        assert_eq!(held.git.checkout_mode, Some(CheckoutMode::MainWorktree));
        assert_eq!(held.capabilities.nix_packaging, Some(false));
        assert_eq!(held.capabilities.reporting_policy, Some(true));
    }

    /// SATISFIES project-profile:release-intent-has-three-modes
    #[test]
    fn every_invalid_release_state_names_its_key() {
        for (text, key) in [
            (
                "[profile.release]\nmode = 'none'\nstyle = 'trunk'\n",
                "profile.release.style",
            ),
            (
                "[profile.release]\nmode = 'external'\ndriver = 'rust'\n",
                "profile.release.driver",
            ),
            (
                "[profile.release]\nmode = 'none'\nline_prefix = 'release/'\n",
                "profile.release.line_prefix",
            ),
            (
                "[profile]\ntechnologies = ['rust', 'rust']\n",
                "profile.technologies",
            ),
            (
                "[profile]\ntechnologies = ['Rust!']\n",
                "profile.technologies",
            ),
            ("[profile]\nforge = 'Git Hub'\n", "profile.forge"),
            (
                "[profile.release]\nmode = 'manual'\n",
                "profile.release.mode",
            ),
        ] {
            let error = parse(&format!("schema_version = 2\n{text}"))
                .expect_err("an invalid release state refuses")
                .to_string();
            assert!(error.contains(key), "{key}: {error}");
        }
    }

    /// A parameter that arrives in a later release lands beside the keys
    /// that were already there, stating its class and its meaning the way
    /// they do, rather than bare.
    #[test]
    fn a_newly_written_key_takes_the_templates_comment() {
        let text = "schema_version = 2\n\n[git]\ntrunk = 'main' # mine\n";
        let next = super::rewrite_text(
            text,
            "git.integration",
            Some(toml_edit::Value::from("forge")),
        )
        .expect("the key writes");
        assert!(
            next.contains("integration = \"forge\" # P: local or forge"),
            "{next}"
        );
        // A key the operator already commented keeps their words.
        let next = super::rewrite_text(&next, "git.trunk", Some(toml_edit::Value::from("master")))
            .expect("the key writes");
        assert!(next.contains("trunk = \"master\" # mine"), "{next}");
    }

    #[test]
    fn the_landed_config_template_round_trips() {
        let dir = tempfile::tempdir().expect("a target exists");
        let mut config = Config::default();
        config.project.repo = "acme/nested/widget".into();
        config.profile.technologies = Some(vec!["bash".into(), "python".into()]);
        config.profile.forge = Some("gitlab".into());
        config.profile.release = super::Release {
            mode: Some(ReleaseMode::Automatic),
            driver: Some("bash".into()),
            style: Some(Style::Lines),
            line_prefix: Some("stable/".into()),
        };
        config.git.trunk = Some("main".into());
        config.git.checkout_mode = Some(CheckoutMode::MainWorktree);
        config.git.integration = Some(Integration::Forge);
        config.capabilities.nix_packaging = Some(true);
        config.capabilities.reporting_policy = Some(false);
        config.capabilities.scorecard = Some(true);
        config.capabilities.code_scanning = Some("semgrep".to_owned());
        // The escaping subject moved to the one unrestricted string in this
        // table: `contact` is now a class P value the reader holds to a
        // single control-free line, so it can carry neither.
        config.security.advisories =
            "A \"quoted\" project\nRK_CONFIG_SECURITY_RESPONSE\\end".into();
        config.security.contact = Some("security team, room 3 \"the vault\"".into());
        config.security.response = Some("14 business days".into());
        config.setup.required_check = "build / test".into();
        config.setup.retired_branches = vec!["develop".into(), "old\"branch".into()];
        config.setup.release_lines = true;
        config.setup.excluded_steps = [
            (
                "package-check".to_owned(),
                "nothing is published".to_owned(),
            ),
            (
                "protect-trunk".to_owned(),
                "this project merges \"locally\"".to_owned(),
            ),
        ]
        .into_iter()
        .collect();
        config.setup.bot.app_id = "123".into();
        config.protection.trunk_ruleset = Some("primary".into());
        config.protection.safety_ruleset = Some("guardrail".into());
        config.protection.tag_ruleset = "versions".into();
        config.protection.lines_ruleset = "maintenance".into();
        config.protection.title_check = "intent".into();
        config.protection.tag_pattern = "refs/tags/*".into();
        config
            .protection
            .owned_trunk_rules
            .push("required_signatures".into());
        config.protection.required_approving_review_count = 2;
        config.protection.dismiss_stale_reviews_on_push = true;
        config.protection.require_code_owner_review = true;
        config.protection.require_last_push_approval = true;
        config.protection.gitlab.squash_commit_template =
            "%{title}\n\nContext: %{description}".into();
        config.protection.gitlab.merge_access_level = 40;
        // A release-less profile with no forge: the automatic-only keys
        // and the repository are absent from the written file.
        let mut release_less = resolved_defaults();
        release_less.project.repo = String::new();
        release_less.profile.technologies = Some(Vec::new());
        release_less.profile.forge = Some(String::new());
        release_less.profile.release = super::Release {
            mode: Some(ReleaseMode::None),
            driver: None,
            style: None,
            line_prefix: None,
        };
        release_less.capabilities.reporting_policy = Some(false);
        for expected in [resolved_defaults(), config, release_less] {
            write(dir.path(), &expected).expect("the template renders");
            assert_eq!(load(dir.path()).expect("the config reads"), Some(expected));
            let text =
                std::fs::read_to_string(dir.path().join(CONFIG_PATH)).expect("the text reads");
            assert!(text.contains("# P: every technology"));
            assert!(text.contains("# F: invariant"));
        }
        let text = std::fs::read_to_string(dir.path().join(CONFIG_PATH)).expect("the text reads");
        assert!(
            !text.contains("style ="),
            "a none release writes no style: {text}"
        );
        assert!(!text.contains("repo ="), "no forge writes no repo: {text}");
    }

    #[test]
    fn a_config_with_an_unknown_key_refuses_by_name() {
        for (table, typo, nearest) in [
            ("", "schemax_version", "schema_version"),
            ("git", "trunkx", "trunk"),
            ("profile.release", "stile", "style"),
            ("capabilities", "nix_packagingg", "nix_packaging"),
            ("security", "contactx", "contact"),
            ("setup", "required_checkx", "required_check"),
            ("setup.bot", "app_i", "app_id"),
            ("protection", "trunk_rulesett", "trunk_ruleset"),
            (
                "protection.github",
                "squash_body_sourcex",
                "squash_body_source",
            ),
            ("protection.gitlab", "squash_optionx", "squash_option"),
        ] {
            let header = if table.is_empty() {
                String::new()
            } else {
                format!("[{table}]\n")
            };
            let text = format!("schema_version = 2\n{header}{typo} = 'value'\n");
            let error = parse(&text).expect_err("unknown keys refuse").to_string();
            for expected in [CONFIG_PATH, typo, &format!("nearest known key: {nearest}")] {
                assert!(error.contains(expected), "{error}");
            }
        }
    }

    /// An exclusion removes a step from scope, so a typo in one would
    /// silently keep judging a step the target does not run, and a
    /// reasonless one would leave a report nobody can audit.
    #[test]
    fn an_exclusion_names_a_real_step_and_states_why() {
        for (text, expected) in [
            (
                "[setup.excluded_steps]\nprotect-trunkk = 'we merge locally'\n",
                vec!["protect-trunkk", "nearest known step: protect-trunk"],
            ),
            (
                "[setup.excluded_steps]\nprotect-trunk = '  '\n",
                vec!["protect-trunk", "no reason"],
            ),
        ] {
            let error = parse(&format!("schema_version = 2\n{text}"))
                .expect_err("the exclusion refuses")
                .to_string();
            for want in expected {
                assert!(error.contains(want), "{error}");
            }
        }
        let held = parse(
            "schema_version = 2\n[setup.excluded_steps]\nprotect-trunk = 'we merge locally'\n",
        )
        .expect("a named step with a reason parses");
        assert_eq!(
            held.setup
                .excluded_steps
                .get("protect-trunk")
                .map(String::as_str),
            Some("we merge locally")
        );
    }

    /// An exclusion narrows what the setup judges. It never weakens the
    /// method's policy, so the floors bind a target that runs a subset
    /// exactly as they bind one that runs every step.
    #[test]
    fn an_exclusion_does_not_lift_a_floor() {
        let error = parse(
            "schema_version = 2\n[setup.excluded_steps]\nprotect-trunk = 'we merge locally'\n\n[protection]\nallowed_merge_methods = ['squash', 'merge']\n",
        )
        .expect_err("the floor binds an excluded step's keys too")
        .to_string();
        assert!(
            error.contains("protection.allowed_merge_methods"),
            "{error}"
        );
    }

    #[test]
    fn a_config_at_an_unknown_schema_refuses() {
        for text in ["schema_version = 999", "schema_version = '2'", ""] {
            let error = parse(text)
                .expect_err("a schema must be declared and known")
                .to_string();
            assert!(
                error.contains(CONFIG_PATH) && error.contains("schema_version"),
                "{error}"
            );
        }
    }

    #[test]
    fn an_unparsable_config_refuses_naming_the_position() {
        let error = parse("schema_version = 2\n[project\n")
            .expect_err("bad TOML refuses")
            .to_string();
        for expected in [CONFIG_PATH, "line 2", "column"] {
            assert!(error.contains(expected), "{error}");
        }
    }

    #[test]
    fn an_absent_config_reads_as_none() {
        let dir = tempfile::tempdir().expect("a target exists");
        assert_eq!(load(dir.path()).expect("absence is compatible"), None);
        assert_eq!(trunk_of(dir.path()).expect("the default reads"), "master");
    }

    #[test]
    fn loading_checks_floors_and_trunk_of_propagates_invalid_content() {
        let dir = tempfile::tempdir().expect("a target exists");
        std::fs::create_dir(dir.path().join(".release-kit")).expect("the directory exists");
        std::fs::write(
            dir.path().join(CONFIG_PATH),
            "schema_version = 2\n[protection]\nstrict_required_status_checks = false\n",
        )
        .expect("a config exists");
        let error =
            trunk_of(dir.path()).expect_err("invalid policy refuses even through the accessor");
        assert_eq!(error.exit_code(), 73);
        assert!(
            error
                .to_string()
                .contains("protection.strict_required_status_checks")
        );
    }

    #[test]
    fn rewrite_key_preserves_comments() {
        let dir = tempfile::tempdir().expect("a target exists");
        std::fs::create_dir(dir.path().join(".release-kit")).expect("the directory exists");
        let original = "# Project answers\nschema_version = 2\n\n[security] # first table stays first\ncontact = 'team' # keep me\n\n[profile.release]\n# Our release choice\nmode = 'automatic'\nstyle  = 'trunk'  # keep this reason\n\n[git]\ncheckout_mode = 'main-worktree'\n";
        let path = dir.path().join(CONFIG_PATH);
        std::fs::write(&path, original).expect("a config exists");
        rewrite_key(dir.path(), "profile.release.style", "lines".into())
            .expect("the style writes back");
        let text = std::fs::read_to_string(&path).expect("the text reads");
        assert_eq!(text, original.replace("'trunk'", "\"lines\""));
        assert_eq!(
            load(dir.path())
                .expect("the config reads")
                .expect("present")
                .profile
                .release
                .style,
            Some(Style::Lines)
        );
        rewrite_key(dir.path(), "project.repo", "acme/widget".into())
            .expect("an omitted table can be added");
        assert_eq!(
            load(dir.path())
                .expect("reads")
                .expect("present")
                .project
                .repo,
            "acme/widget"
        );
        rewrite_key(
            dir.path(),
            "security.contact",
            "security@acme.example".into(),
        )
        .expect("the contact is a landing parameter");
        rewrite_key(dir.path(), "security.response", "14 days".into())
            .expect("the response is a landing parameter");
        let held = load(dir.path()).expect("reads").expect("present");
        assert_eq!(
            held.security.contact.as_deref(),
            Some("security@acme.example")
        );
        assert_eq!(held.security.response.as_deref(), Some("14 days"));
        let text = std::fs::read_to_string(&path).expect("the text reads");
        assert!(text.contains("# keep me"), "the comment survives: {text}");
        let before = std::fs::read(&path).expect("the bytes read");
        for (key, value) in [
            ("security.advisories", "acme/private"),
            ("security.response", "90d"),
            ("profile.release.style", "unknown"),
        ] {
            assert!(rewrite_key(dir.path(), key, value.into()).is_err());
            assert_eq!(std::fs::read(&path).expect("the bytes read"), before);
        }
    }

    /// The two security answers are one line and one narrow grammar,
    /// because both land verbatim in a public policy.
    #[test]
    fn the_security_answers_are_held_to_their_grammar() {
        for value in ["team@acme.example", "  https://acme.example/report  ", ""] {
            super::canonical_contact(value).expect("a control-free line is a contact");
        }
        for value in ["one\ntwo", "one\rtwo", "one\u{7}two"] {
            let refusal = super::canonical_contact(value).expect_err("a control character refuses");
            assert!(refusal.contains("security.contact"), "{refusal}");
        }
        assert_eq!(
            super::canonical_contact("  team@acme.example  "),
            Ok("team@acme.example".to_owned()),
            "surrounding whitespace is trimmed"
        );
        for value in [
            "best-effort",
            "1 day",
            "2 days",
            "14 days",
            "1 business day",
            "14 business days",
        ] {
            assert_eq!(super::canonical_response(value), Ok(value.to_owned()));
        }
        assert_eq!(
            super::canonical_response(""),
            Ok(super::RESPONSE_DEFAULT.to_owned()),
            "an empty answer reads as the compiled default"
        );
        for value in [
            "0 days",
            "1 days",
            "2 day",
            "+2 days",
            "02 days",
            "4294967296 days",
            "90d",
            "two days",
            "we answer quickly",
            "2 weeks",
        ] {
            let refusal =
                super::canonical_response(value).expect_err("an unstateable window refuses");
            assert!(refusal.contains("security.response"), "{value}: {refusal}");
            assert!(refusal.contains("business days"), "{value}: {refusal}");
        }
        let refusal = parse("schema_version = 2\n[security]\nresponse = '90d'\n")
            .expect_err("the reader refuses it too")
            .to_string();
        assert!(refusal.contains("security.response"), "{refusal}");
    }

    /// SATISFIES target-config:an-unanswered-key-is-absent-and-not-empty
    /// A header whose every key the writer omitted goes, and every comment
    /// it carried survives: standing above it, standing beside it, and in
    /// either position when the emptied table is the file's last.
    #[test]
    fn a_pruned_header_leaves_no_comment_behind() {
        let cases = [
            (
                "a middle table, comment above",
                "schema_version = 2\n\n# the operator's note\n[project]\nrepo = \"acme/widget\"\n\n[git]\ntrunk = \"main\"\n",
            ),
            (
                "a middle table, comment inline",
                "schema_version = 2\n\n[project] # the operator's note\nrepo = \"acme/widget\"\n\n[git]\ntrunk = \"main\"\n",
            ),
            (
                "the last table, comment above",
                "schema_version = 2\n\n[git]\ntrunk = \"main\"\n\n# the operator's note\n[project]\nrepo = \"acme/widget\"\n",
            ),
            (
                "the last table, comment inline",
                "schema_version = 2\n\n[git]\ntrunk = \"main\"\n\n[project] # the operator's note\nrepo = \"acme/widget\"\n",
            ),
        ];
        for (case, text) in cases {
            let next = super::rewrite_text(text, "project.repo", None).expect("the key removes");
            assert!(!next.contains("repo ="), "{case}: {next}");
            assert!(
                !next.contains("[project]"),
                "{case}: a table every one of whose keys dropped goes with them: {next}"
            );
            assert!(
                next.contains("# the operator's note"),
                "{case}: the comment the header carried survives: {next}"
            );
            parse(&next).unwrap_or_else(|error| panic!("{case}: {error}"));
        }
    }

    /// SATISFIES target-config:a-flag-overrides-and-a-landing-writes-back
    /// A key the writer retires takes the template's own comment with it
    /// and leaves the operator's behind, on the header of the domain the
    /// key belonged to. The template comment describes a key that is gone;
    /// the operator's comment is authored text this writer does not delete.
    #[test]
    fn a_retired_key_drops_the_template_comment_and_keeps_the_operators() {
        let text = concat!(
            "schema_version = 2\n\n[project]\n",
            "# the operator's note\n",
            "repo = \"acme/widget\" # P: project path on the forge\n\n",
            "[git]\ntrunk = \"main\"\n"
        );
        let next = super::rewrite_text(text, "project.repo", None).expect("the key removes");
        assert!(!next.contains("repo ="), "{next}");
        assert!(!next.contains("[project]"), "{next}");
        assert!(
            next.contains("# the operator's note"),
            "authored text survives: {next}"
        );
        assert!(
            !next.contains("# P:"),
            "the template's comment describes a key that is gone: {next}"
        );
        parse(&next).expect("the result parses");

        // A domain that keeps other keys keeps the note on its own header.
        let text = concat!(
            "schema_version = 2\n\n[profile]\ntechnologies = [\"rust\"]\n\n",
            "[profile.release]\nmode = \"automatic\"\ndriver = \"rust\"\n",
            "# why this project names its own prefix\n",
            "line_prefix = \"stable/\"\n"
        );
        let next = super::rewrite_text(text, "profile.release.line_prefix", None)
            .expect("the key removes");
        assert!(!next.contains("line_prefix ="), "{next}");
        assert!(next.contains("[profile.release]"), "{next}");
        assert!(
            next.contains("# why this project names its own prefix"),
            "authored text survives: {next}"
        );
    }
}