rite-model 0.7.0

Domain model and intermediate representation for the Rite key ceremony DSL
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
//! Persisted transcript schema.
//!
//! These types are the **durable** audit surface: what gets written to
//! `transcript.jsonl`, what `rite verify` reads back, what report and
//! audit tooling consume. They are deliberately kept independent of the
//! executor and the channel plumbing, a third-party verifier can parse
//! a transcript with only `rite-model` on its dependency list.
//!
//! The live runtime↔frontend channel protocol (`ExecEvent`, `UiCommand`,
//! `Response`, `Icon`, …) lives in `rite-runtime` next to the executor
//! that owns the channels. The boundary is **persisted vs in-flight**.

use std::collections::BTreeMap;

use base64ct::Encoding as _;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

use crate::Sha256Digest;
use crate::ir::{ActId, MaterialId, ParamId, RoleId, StepId};

/// Transcript format version this crate writes and reads: the line envelope
/// and the chain rule.
///
/// 0 while rite is before 1.0: the format changes between releases without
/// a new number, and the header's `producer` names the release that wrote a
/// transcript.
pub const TRANSCRIPT_FORMAT: u32 = 0;

/// Version of the [`StepFact`] vocabulary this crate writes and reads. 0
/// before 1.0, like [`TRANSCRIPT_FORMAT`].
pub const FACT_VOCABULARY: u32 = 0;

/// URL of the JSON Schema for the transcript lines this release writes,
/// written on the header line for editors and other tools. A reader ignores
/// it.
pub const TRANSCRIPT_SCHEMA: &str = concat!(
    "https://ritely.io/schemas/",
    env!("CARGO_PKG_VERSION"),
    "/transcript.schema.json"
);

/// First line of every transcript: what the file is, read before any fact.
///
/// Chained like every other line, so it cannot be swapped without breaking
/// the fingerprint. A reader checks [`rite_transcript`](Self::rite_transcript)
/// before parsing anything else and refuses a value it does not know.
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
    test,
    schemars(
        description = "What the file is. A reader checks `rite_transcript` before reading \
        anything else, and refuses a version it does not know."
    )
)]
pub struct TranscriptHeader {
    /// Format version of the envelope and chain rule ([`TRANSCRIPT_FORMAT`]).
    #[cfg_attr(
        test,
        schemars(description = "Version of the line format and the chain rule.")
    )]
    pub rite_transcript: u32,
    /// Version of the fact vocabulary ([`FACT_VOCABULARY`]).
    #[cfg_attr(
        test,
        schemars(
            description = "Version of the fact vocabulary: which fact types and fields the \
            transcript uses."
        )
    )]
    pub vocabulary: u32,
    /// Program and version that wrote the transcript, as it reports itself.
    /// Informational: a binary cannot vouch for its own identity.
    #[cfg_attr(
        test,
        schemars(
            description = "The program and version that wrote the transcript, as it reports \
            itself. Informational: a program cannot vouch for its own identity."
        )
    )]
    pub producer: String,
    /// Random identifier of this run, 32 lowercase hex digits.
    #[cfg_attr(
        test,
        schemars(
            description = "A random identifier of this run: 32 lowercase hex digits.",
            extend("pattern" = "^[0-9a-f]{32}$")
        )
    )]
    pub run_id: String,
    /// Whether the run was a dry run. A dry-run transcript is a rehearsal
    /// record, never evidence of a ceremony.
    #[cfg_attr(
        test,
        schemars(
            description = "Whether the run was a dry run. A dry-run transcript is a rehearsal \
            record, never evidence of a ceremony."
        )
    )]
    pub dry_run: bool,
    /// Every level this transcript uses, by name. Holds the built-in levels
    /// at their fixed values, and any level the ceremony declares.
    #[cfg_attr(
        test,
        schemars(
            description = "Every level the transcript uses, by name. It holds public, \
            restricted and confidential at their fixed values, and any other level the ceremony \
            declares. No two names share a value."
        )
    )]
    pub levels: BTreeMap<String, Level>,
}

impl TranscriptHeader {
    /// A header for the current format and vocabulary.
    #[must_use]
    pub fn new(producer: &str, run_id: &str, dry_run: bool) -> Self {
        Self {
            rite_transcript: TRANSCRIPT_FORMAT,
            vocabulary: FACT_VOCABULARY,
            producer: producer.to_string(),
            run_id: run_id.to_string(),
            dry_run,
            levels: Level::BUILT_IN
                .iter()
                .map(|(name, level)| ((*name).to_string(), *level))
                .collect(),
        }
    }

    /// Whether the header declares `level`.
    #[must_use]
    pub fn declares(&self, level: Level) -> bool {
        self.levels.values().any(|declared| *declared == level)
    }

    /// A declared level, by the name the header gives it or by its number.
    #[must_use]
    pub fn level(&self, name_or_value: &str) -> Option<Level> {
        if let Some(level) = self.levels.get(name_or_value) {
            return Some(*level);
        }
        let level = Level::new(name_or_value.parse().ok()?);
        self.declares(level).then_some(level)
    }
}

/// Confidentiality level of a transcript line: who may see the fact.
///
/// An integer, ordered from widest audience to narrowest, so a disclosure is
/// a threshold and checking it needs no names. The level is committed in the
/// chain with the line. Secrets are not a level: a secret value is never
/// recorded at all.
///
/// Three levels are built in, at fixed values that mean the same thing in
/// every transcript. The gaps leave room for an organisation to declare its
/// own levels between and beyond them; the header names every level a
/// transcript uses.
///
/// | Level | Value | Audience | TLP 2.0 |
/// |---|---|---|---|
/// | `public` | 10 | anyone | CLEAR |
/// | `restricted` | 20 | auditors, under agreement | AMBER |
/// | `confidential` | 30 | the ceremony's own organisation | RED |
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
#[cfg_attr(
    test,
    schemars(
        description = "The confidentiality level of a line: who may see its fact. Levels are \
        integers ordered from the widest audience to the narrowest, so a disclosure up to a level \
        withholds every line above it. Three levels are built in at fixed values: public (10, \
        anyone), restricted (20, auditors under agreement) and confidential (30, the ceremony's \
        own organisation). Every level a transcript uses is declared in its header."
    )
)]
pub struct Level(u32);

impl Level {
    /// Anyone.
    pub const PUBLIC: Level = Level(10);
    /// Auditors, under agreement.
    pub const RESTRICTED: Level = Level(20);
    /// The ceremony's own organisation.
    pub const CONFIDENTIAL: Level = Level(30);

    /// The built-in levels with their names, widest audience first.
    pub const BUILT_IN: [(&'static str, Level); 3] = [
        ("public", Level::PUBLIC),
        ("restricted", Level::RESTRICTED),
        ("confidential", Level::CONFIDENTIAL),
    ];

    /// A level at `value`.
    #[must_use]
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// The level's value, as written in the transcript and committed in the
    /// chain.
    #[must_use]
    pub const fn value(self) -> u32 {
        self.0
    }
}

impl std::fmt::Display for Level {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match Level::BUILT_IN.iter().find(|(_, level)| level == self) {
            Some((name, _)) => f.write_str(name),
            None => write!(f, "level {}", self.0),
        }
    }
}

/// Validator applied by the runtime to a typed response before it is
/// accepted.
///
/// The rule is part of the prompt, so it is recorded with it: a transcript
/// says a six-digit secret was entered, never which one. [`check`](Self::check)
/// is the one place a rule is applied, so `rite check` and the running
/// ceremony agree on what a pattern accepts.
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
#[cfg_attr(test, schemars(description = "The check an answer had to pass."))]
pub enum ValidatorSpec {
    /// Reject empty or whitespace-only input.
    #[cfg_attr(test, schemars(description = "The answer is not empty or blank."))]
    NonEmpty,
    /// Input must match this regular expression in full.
    #[cfg_attr(
        test,
        schemars(description = "The answer matches a regular expression in full.")
    )]
    Regex(#[cfg_attr(test, schemars(description = "The regular expression."))] String),
    /// Named, runtime-defined predicate (e.g. `serial_number`).
    #[cfg_attr(
        test,
        schemars(description = "The answer passes a check the program defines, by name.")
    )]
    Predefined(
        #[cfg_attr(
            test,
            schemars(description = "The name of the check, such as serial_number.")
        )]
        String,
    ),
    /// A value of one [`Format`], with a length within bounds.
    ///
    /// What a PIN or a key component needs, stated without a pattern the
    /// person writing the ceremony has to get right. The length is counted
    /// in the format's own units: characters for text, bytes for an
    /// encoding.
    #[cfg_attr(
        test,
        schemars(
            description = "The answer is a value of one format, with a length within \
                                 bounds, counted in characters for text and in bytes for an \
                                 encoding."
        )
    )]
    Format {
        /// What kind of value this is.
        #[cfg_attr(test, schemars(description = "What kind of value the answer is."))]
        format: Format,
        /// Fewest units accepted, if bounded.
        #[cfg_attr(test, schemars(description = "The fewest units accepted, if bounded."))]
        min_length: Option<usize>,
        /// Most units accepted, if bounded.
        #[cfg_attr(test, schemars(description = "The most units accepted, if bounded."))]
        max_length: Option<usize>,
    },
}

/// What kind of value a person types.
///
/// Each format has one canonical representation, which is what an entry step
/// keeps: the text as typed for a text format, the decoded bytes for an
/// encoding. An encoding is forgiving of the grouping a person types it in,
/// so whitespace is dropped before decoding.
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(test, schemars(description = "What kind of value a person types."))]
pub enum Format {
    /// Anything the person can type.
    #[cfg_attr(test, schemars(description = "Anything the person can type."))]
    Text,
    /// `0` to `9`.
    #[cfg_attr(test, schemars(description = "Decimal digits only."))]
    Digits,
    /// ASCII letters and digits.
    #[cfg_attr(test, schemars(description = "ASCII letters and digits."))]
    Alphanumeric,
    /// Bytes as hexadecimal, in either case, two digits per byte.
    #[cfg_attr(
        test,
        schemars(description = "Bytes as hexadecimal, in either case, two digits per byte.")
    )]
    Hex,
    /// Bytes as standard base64 with padding, as `openssl base64` writes it.
    #[cfg_attr(test, schemars(description = "Bytes as standard base64 with padding."))]
    Base64,
}

impl Format {
    /// Whether the value stands for bytes, which is what the step then keeps.
    #[must_use]
    pub fn is_encoding(self) -> bool {
        match self {
            Format::Text | Format::Digits | Format::Alphanumeric => false,
            Format::Hex | Format::Base64 => true,
        }
    }

    /// Whether a text format accepts this character. An encoding accepts
    /// whatever decodes.
    fn accepts(self, c: char) -> bool {
        match self {
            Format::Text | Format::Hex | Format::Base64 => true,
            Format::Digits => c.is_ascii_digit(),
            Format::Alphanumeric => c.is_ascii_alphanumeric(),
        }
    }

    /// Decode a typed value of an encoding.
    ///
    /// # Errors
    ///
    /// Returns why the value is not this encoding, or that the format is not
    /// one, worded for the person who typed it and naming nothing of what
    /// they typed.
    pub fn decode(self, value: &str) -> Result<Vec<u8>, String> {
        // The value may be a secret, so the copy without its whitespace is
        // wiped with the call.
        let compact: Zeroizing<String> =
            Zeroizing::new(value.chars().filter(|c| !c.is_whitespace()).collect());
        match self {
            Format::Hex => base16ct::mixed::decode_vec(compact.as_bytes())
                .map_err(|_| "value must be hex, two digits per byte".to_string()),
            Format::Base64 => base64ct::Base64::decode_vec(&compact)
                .map_err(|_| "value must be standard base64, with padding".to_string()),
            Format::Text | Format::Digits | Format::Alphanumeric => {
                Err(format!("{} is text and does not decode", self.describe()))
            }
        }
    }

    /// The unit a length counts, as a person reads it in a hint.
    #[must_use]
    pub fn describe(self) -> &'static str {
        match self {
            Format::Text => "characters",
            Format::Digits => "digits",
            Format::Alphanumeric => "letters or digits",
            Format::Hex => "bytes as hex",
            Format::Base64 => "bytes as base64",
        }
    }

    /// A stand-in of `length` units that satisfies the format, for a run
    /// with no one at the keyboard. Never a real value.
    ///
    /// `None` above [`PLACEHOLDER_LIMIT`]: a length a ceremony declares is
    /// not bounded, and a stand-in that size would be allocated for nothing.
    /// The caller then declines to answer rather than answering with
    /// something the rule refuses, which would be asked again without end.
    #[must_use]
    pub fn placeholder(self, length: usize) -> Option<String> {
        if length > PLACEHOLDER_LIMIT {
            return None;
        }
        Some(match self {
            Format::Text | Format::Alphanumeric => "x".repeat(length),
            Format::Digits => "0".repeat(length),
            Format::Hex => base16ct::lower::encode_string(&vec![0u8; length]),
            Format::Base64 => base64ct::Base64::encode_string(&vec![0u8; length]),
        })
    }
}

/// The longest stand-in a [`Format::placeholder`] builds, in the format's
/// units. Large enough for any value a person would type or paste, and the
/// entry itself is not bounded by it.
pub const PLACEHOLDER_LIMIT: usize = 64 * 1024;

impl std::str::FromStr for Format {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "text" => Ok(Format::Text),
            "digits" => Ok(Format::Digits),
            "alphanumeric" => Ok(Format::Alphanumeric),
            "hex" => Ok(Format::Hex),
            "base64" => Ok(Format::Base64),
            other => Err(format!(
                "unknown format '{other}': expected text, digits, alphanumeric, hex or base64"
            )),
        }
    }
}

impl ValidatorSpec {
    /// Apply the rule to a typed value.
    ///
    /// # Errors
    ///
    /// Returns what the value fails to satisfy, worded for the person who
    /// typed it, or why the rule itself cannot be applied.
    pub fn check(&self, value: &str) -> Result<(), String> {
        match self {
            ValidatorSpec::NonEmpty => {
                if value.trim().is_empty() {
                    Err("value must not be empty".to_string())
                } else {
                    Ok(())
                }
            }
            ValidatorSpec::Regex(pattern) => {
                let regex = compile_pattern(pattern)?;
                if regex.is_match(value) {
                    Ok(())
                } else {
                    Err(format!("value must match {pattern}"))
                }
            }
            // Named predicates will land alongside the actions that need them.
            ValidatorSpec::Predefined(name) => Err(format!("unknown validator: {name}")),
            ValidatorSpec::Format {
                format,
                min_length,
                max_length,
            } => {
                let hint = self.hint().unwrap_or_default();
                if value.trim().is_empty() {
                    return Err("value must not be empty".to_string());
                }
                // An encoding is decoded and discarded, wiped on the way out:
                // the value may be a secret, and this runs before the step
                // holds it.
                let length = if format.is_encoding() {
                    Zeroizing::new(format.decode(value)?).len()
                } else {
                    if !value.chars().all(|c| format.accepts(c)) {
                        return Err(format!("value must be {hint}"));
                    }
                    value.chars().count()
                };
                if min_length.is_some_and(|min| length < min)
                    || max_length.is_some_and(|max| length > max)
                {
                    return Err(format!("value must be {hint}, this is {length}"));
                }
                Ok(())
            }
        }
    }

    /// The rule as a person reads it before typing, if there is one worth
    /// stating.
    ///
    /// `NonEmpty` says nothing: every prompt asks for something. A pattern is
    /// shown as written, since a regular expression has no better rendering.
    #[must_use]
    pub fn hint(&self) -> Option<String> {
        match self {
            ValidatorSpec::NonEmpty | ValidatorSpec::Predefined(_) => None,
            ValidatorSpec::Regex(pattern) => Some(format!("matching {pattern}")),
            ValidatorSpec::Format {
                format,
                min_length,
                max_length,
            } => {
                let what = format.describe();
                Some(match (min_length, max_length) {
                    (Some(min), Some(max)) if min == max => format!("{min} {what}"),
                    (Some(min), Some(max)) => format!("{min} to {max} {what}"),
                    (Some(min), None) => format!("at least {min} {what}"),
                    (None, Some(max)) => format!("at most {max} {what}"),
                    (None, None) if format.is_encoding() => what.to_string(),
                    (None, None) => format!("{what} only"),
                })
            }
        }
    }
}

/// Compile a pattern so that it must match the whole value.
///
/// A rule that read `[0-9]+` and accepted `abc123def` would pass what its
/// author meant to refuse, so the anchors are always supplied here rather
/// than expected of the author.
///
/// # Errors
///
/// Returns why the pattern is not a regular expression.
pub fn compile_pattern(pattern: &str) -> Result<regex_lite::Regex, String> {
    regex_lite::Regex::new(&format!("^(?:{pattern})$"))
        .map_err(|e| format!("invalid pattern '{pattern}': {e}"))
}

/// Request for user input, recorded into the transcript as part of
/// [`StepFact::PromptAnswered`].
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(test, schemars(description = "A prompt as it was shown."))]
pub enum Prompt {
    /// Yes / no question with an optional default.
    #[cfg_attr(test, schemars(description = "A yes or no question."))]
    Confirm {
        /// Question shown to the user.
        #[cfg_attr(test, schemars(description = "The question."))]
        question: String,
        /// Default selection if the user presses Enter without choosing.
        #[cfg_attr(
            test,
            schemars(
                description = "The answer given when the person confirms without choosing, if \
                any."
            )
        )]
        default: Option<bool>,
    },
    /// Free-form text input, validated against a [`ValidatorSpec`].
    #[cfg_attr(
        test,
        schemars(description = "A request for free text, checked before it is accepted.")
    )]
    Text {
        /// Label shown to the user.
        #[cfg_attr(test, schemars(description = "The label shown."))]
        label: String,
        /// Validator applied before the response is accepted.
        #[cfg_attr(test, schemars(description = "The check the answer had to pass."))]
        validator: ValidatorSpec,
    },
    /// Sensitive input (PIN, password). Echo is suppressed; plaintext is
    /// never serialized to the transcript.
    #[cfg_attr(
        test,
        schemars(
            description = "A request for a secret, such as a PIN. The answer is never \
            recorded."
        )
    )]
    Secret {
        /// Label shown to the user.
        #[cfg_attr(test, schemars(description = "The label shown."))]
        label: String,
        /// Validator applied before the response is accepted.
        #[cfg_attr(test, schemars(description = "The check the answer had to pass."))]
        validator: ValidatorSpec,
    },
    /// User must type a specific literal string exactly. Validation is
    /// performed by the runtime against `expected`.
    #[cfg_attr(
        test,
        schemars(
            description = "A request to type a given text exactly, such as a confirmation \
            phrase."
        )
    )]
    Literal {
        /// Label shown to the user.
        #[cfg_attr(test, schemars(description = "The label shown."))]
        label: String,
        /// Exact string the user must type.
        #[cfg_attr(test, schemars(description = "The text that had to be typed."))]
        expected: String,
    },
    /// Wait for the user to acknowledge before proceeding. Used for pacing.
    #[cfg_attr(
        test,
        schemars(description = "A pause until the person is ready to continue.")
    )]
    Continue {
        /// Optional hint such as "Press Enter when ready".
        #[cfg_attr(test, schemars(description = "The hint shown, if any."))]
        hint: Option<String>,
    },
}

/// Serializable, redacted form of a user response.
///
/// Used inside [`StepFact::PromptAnswered`] so that the transcript records
/// what was answered without ever persisting plaintext secrets. The
/// in-flight `Response` type lives next to the channel protocol in
/// `rite-runtime`; conversion happens at the moment the prompt is accepted.
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(
    test,
    schemars(
        description = "The answer to a prompt, as recorded. A secret answer is never \
        recorded, not even as a digest."
    )
)]
pub enum ResponseRecord {
    /// Yes / no answer.
    #[cfg_attr(test, schemars(description = "A yes or no answer."))]
    Bool {
        /// The answer.
        #[cfg_attr(test, schemars(description = "The answer."))]
        value: bool,
    },
    /// Free-form text answer.
    #[cfg_attr(test, schemars(description = "A free-text answer."))]
    Text {
        /// The answer.
        #[cfg_attr(test, schemars(description = "The answer, as typed."))]
        value: String,
    },
    /// Secret answer. The plaintext is never stored, and no digest of it is
    /// kept either: a hash of a low-entropy secret (such as a 6-8 digit PIV
    /// PIN) is brute-forceable from a shared transcript. The position of the
    /// enclosing `PromptAnswered` fact in the chain already records that a
    /// secret was entered at this point.
    // Note: a salted, per-run HMAC could later attest that two prompts received
    // the same secret without reintroducing the low-entropy guessing oracle.
    // Deferred until a concrete use case needs it.
    #[cfg_attr(
        test,
        schemars(
            description = "A secret was entered. Only the fact that it was entered is \
            recorded."
        )
    )]
    SecretRedacted {},
    /// Acknowledgement of a [`Prompt::Continue`].
    #[cfg_attr(test, schemars(description = "The person continued past a pause."))]
    Acknowledged,
}

/// Audit classification of a bad outcome, recorded so an auditor can tell the
/// nature of a failure apart without parsing the free-form `message`.
///
/// This is the *audit* taxonomy (what an auditor sees), distinct from the
/// runtime's `Retriability` (whether a step may re-run). For a backend error
/// the two align: a retriable error is `Environmental`. They are kept separate
/// because some classes never map cleanly onto retriability (an `Abort` is a
/// decision, not an error).
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(test, schemars(description = "What kind of bad outcome this was."))]
pub enum ErrorClass {
    /// The world wasn't ready; the step's work did not happen (token absent,
    /// loose cable, PIN required).
    #[cfg_attr(
        test,
        schemars(
            description = "Something around the ceremony was not ready, such as a device not \
            connected, and the step's work did not happen."
        )
    )]
    Environmental,
    /// The ceremony's own logic concluded badly (a verification mismatch, a
    /// refused attestation). A result, not a recoverable condition.
    #[cfg_attr(
        test,
        schemars(
            description = "The ceremony's own logic concluded badly, such as a verification \
            that did not match or a refused attestation."
        )
    )]
    Procedural,
    /// The run itself is compromised or the definition is broken (transcript
    /// write failed, channel lost, unknown action, invalid params).
    #[cfg_attr(
        test,
        schemars(
            description = "The run itself cannot be trusted to continue, or the ceremony \
            definition is broken."
        )
    )]
    Integrity,
    /// The operator chose to stop. Not an error at all, but recorded on the
    /// terminal fact so abort is distinguishable from failure.
    #[cfg_attr(
        test,
        schemars(description = "An operator chose to stop the ceremony.")
    )]
    Abort,
}

/// Structured error record for transcript serialization.
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(
    test,
    schemars(
        description = "What went wrong: a class for audit, a stable kind, and a message for \
        people."
    )
)]
pub struct ErrorRecord {
    /// Audit classification of this error.
    #[cfg_attr(test, schemars(description = "The kind of bad outcome, for audit."))]
    pub class: ErrorClass,
    /// Stable kind label (e.g. `aborted`, `step_failed`, `material_load_failed`).
    #[cfg_attr(
        test,
        schemars(description = "A stable label for the error, such as aborted or step_failed.")
    )]
    pub kind: String,
    /// Human-readable message.
    #[cfg_attr(test, schemars(description = "A description of the error for people."))]
    pub message: String,
}

impl ErrorRecord {
    /// Construct an error record.
    pub fn new(class: ErrorClass, kind: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            class,
            kind: kind.into(),
            message: message.into(),
        }
    }
}

/// Outcome of a single step, carried by [`StepFact::StepCompleted`].
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
#[cfg_attr(test, schemars(description = "How a step ended."))]
pub enum StepOutcome {
    /// Step executed successfully.
    #[cfg_attr(test, schemars(description = "The step did its work."))]
    Completed {
        /// Human-readable completion message.
        #[cfg_attr(
            test,
            schemars(description = "A short description of what the step did.")
        )]
        message: String,
    },
}

/// Durable, transcript-worthy fact.
///
/// Every variant is recorded by the runtime's transcript sink synchronously
/// before being forwarded to the UI. Action handlers emit
/// [`BackendOperation`] and [`AttestationRecorded`]; all other variants are
/// emitted by the executor at the corresponding lifecycle boundary.
///
/// [`BackendOperation`]: StepFact::BackendOperation
/// [`AttestationRecorded`]: StepFact::AttestationRecorded
#[non_exhaustive]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(
    test,
    schemars(
        description = "One recorded fact, identified by its `type`. Each fact type has a \
        fixed set of fields in a given vocabulary."
    )
)]
pub enum StepFact {
    /// Ceremony has started running.
    #[cfg_attr(test, schemars(description = "The ceremony started."))]
    CeremonyStarted {
        /// Ceremony name from the DSL.
        #[cfg_attr(test, schemars(description = "The ceremony's name."))]
        name: String,
        /// Digest of the ceremony YAML the run was resolved from. The inputs
        /// supplied for this run are recorded as their own facts, one each,
        /// so this digest covers the template only.
        #[cfg_attr(
            test,
            schemars(
                description = "Digest of the ceremony definition file the run was resolved \
                from. The inputs of the run are recorded as their own facts, so this digest covers \
                the definition only."
            )
        )]
        template: Sha256Digest,
    },
    /// A role the ceremony defines, with its display name.
    ///
    /// Recorded once per role at ceremony start, whether or not anyone is
    /// assigned to it. Later facts name the role by id only.
    #[cfg_attr(
        test,
        schemars(
            description = "A role the ceremony defines. Recorded once per role at the start, \
            whether or not anyone is assigned to it; later facts name the role by its identifier."
        )
    )]
    RoleDeclared {
        /// Role identifier.
        #[cfg_attr(test, schemars(description = "The role."))]
        role: RoleId,
        /// Human-readable role name, from the ceremony's role definition.
        #[cfg_attr(test, schemars(description = "The role's display name."))]
        name: String,
    },
    /// A person was assigned to a role for this run.
    ///
    /// One fact per assignment, so each can be withheld on its own.
    #[cfg_attr(
        test,
        schemars(
            description = "A person was assigned to a role for this run. One fact per \
            assignment, so each can be withheld on its own."
        )
    )]
    RoleAssigned {
        /// Role identifier.
        #[cfg_attr(test, schemars(description = "The role."))]
        role: RoleId,
        /// The person, as supplied at run time. Recorded as given; nothing
        /// in the ceremony proves who the person is.
        #[cfg_attr(
            test,
            schemars(
                description = "The person, as supplied when the run started. Recorded as \
                given: nothing in the transcript proves who the person is."
            )
        )]
        person: String,
    },
    /// A parameter took its value for this run, supplied or defaulted.
    #[cfg_attr(
        test,
        schemars(description = "A parameter took its value for this run, supplied or defaulted.")
    )]
    ParameterBound {
        /// Parameter identifier.
        #[cfg_attr(test, schemars(description = "The parameter."))]
        name: ParamId,
        /// The resolved value.
        #[cfg_attr(test, schemars(description = "The value, as the ceremony used it."))]
        value: serde_json::Value,
    },
    /// A material was loaded at ceremony start.
    ///
    /// The digest of a digital material is a separate fact,
    /// [`MaterialDigest`](StepFact::MaterialDigest), so the two can be
    /// disclosed to different audiences.
    #[cfg_attr(
        test,
        schemars(
            description = "A material was loaded at the start of the run. The digest of a \
            digital material is a separate fact, material_digest, so the two can be disclosed to \
            different audiences."
        )
    )]
    MaterialLoaded {
        /// Material identifier.
        #[cfg_attr(test, schemars(description = "The material."))]
        name: MaterialId,
        /// Identifier of a physical or pre-provisioned material, such as a
        /// serial number, when the ceremony supplied one.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[cfg_attr(
            test,
            schemars(
                description = "The identifier of a physical or pre-provisioned material, such \
                as a serial number, when one was supplied."
            )
        )]
        identifier: Option<String>,
    },
    /// The digest of a digital material's bytes.
    #[cfg_attr(
        test,
        schemars(description = "The digest of a digital material's bytes, as loaded.")
    )]
    MaterialDigest {
        /// Material identifier.
        #[cfg_attr(test, schemars(description = "The material."))]
        name: MaterialId,
        /// Digest of the file as loaded.
        #[cfg_attr(test, schemars(description = "The digest of the material's bytes."))]
        digest: Sha256Digest,
    },
    /// A backend was first acquired in this run.
    ///
    /// Recorded once per backend, before the first operation on it. Backend
    /// operations name the backend and do not repeat its identity, which can
    /// carry device serials.
    #[cfg_attr(
        test,
        schemars(
            description = "A backend was used for the first time in this run. Recorded once \
            per backend, before its first operation; operations name the backend and do not repeat \
            its identity."
        )
    )]
    BackendBound {
        /// Backend name from the ceremony.
        #[cfg_attr(test, schemars(description = "The backend's name in the ceremony."))]
        name: String,
        /// Backend provider (e.g. `openssl`, `yubikey`, `pkcs11`).
        #[cfg_attr(
            test,
            schemars(description = "The kind of backend, such as openssl, yubikey or pkcs11.")
        )]
        provider: String,
        /// Identity the backend reports for itself, such as a device serial
        /// and firmware version.
        #[cfg_attr(
            test,
            schemars(
                description = "The identity the backend reports for itself, such as a device \
                serial number and firmware version."
            )
        )]
        identity: String,
    },
    /// A snapshot of the machine the ceremony ran on.
    #[cfg_attr(
        test,
        schemars(description = "A description of the machine the ceremony ran on.")
    )]
    MachineInfoRecorded {
        /// Step that captured the snapshot.
        #[cfg_attr(test, schemars(description = "The step that recorded it."))]
        step: StepId,
        /// The parts of the host snapshot the step was asked to record.
        #[cfg_attr(
            test,
            schemars(
                description = "The parts of the machine description the step was asked to \
                record."
            )
        )]
        info: serde_json::Value,
    },
    /// Beginning of an act (named subdivision of a ceremony).
    #[cfg_attr(
        test,
        schemars(description = "An act, a named part of the ceremony, started.")
    )]
    ActStarted {
        /// Act identifier.
        #[cfg_attr(test, schemars(description = "The act."))]
        id: ActId,
        /// Act label as authored in the DSL.
        #[cfg_attr(
            test,
            schemars(description = "The act's label, as written in the ceremony.")
        )]
        label: String,
    },
    /// Beginning of a step.
    #[cfg_attr(test, schemars(description = "A step started."))]
    StepStarted {
        /// Step identifier.
        #[cfg_attr(test, schemars(description = "The step."))]
        id: StepId,
        /// Step label as authored in the DSL.
        #[cfg_attr(
            test,
            schemars(description = "The step's label, as written in the ceremony.")
        )]
        label: String,
        /// Role responsible for this step. Its name is on the role's
        /// [`RoleDeclared`](StepFact::RoleDeclared) fact.
        #[cfg_attr(
            test,
            schemars(
                description = "The role responsible for the step. Its display name is on the \
                role's role_declared fact."
            )
        )]
        role: RoleId,
    },
    /// A prompt has been answered and validated.
    #[cfg_attr(
        test,
        schemars(description = "Someone answered a prompt, and the answer was accepted.")
    )]
    PromptAnswered {
        /// Step that issued the prompt, if any. `None` for ceremony-level
        /// prompts emitted before the first step (e.g. the ceremony-start
        /// confirmation) or after the last step.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[cfg_attr(
            test,
            schemars(
                description = "The step that asked, or absent for a prompt asked outside any \
                step."
            )
        )]
        step: Option<StepId>,
        /// The prompt as issued.
        #[cfg_attr(test, schemars(description = "The prompt, as shown."))]
        prompt: Prompt,
        /// Redacted response record.
        #[cfg_attr(test, schemars(description = "The answer, as recorded."))]
        response: ResponseRecord,
    },
    /// A backend operation completed and produced structured evidence.
    #[cfg_attr(
        test,
        schemars(
            description = "A backend performed an operation. The inputs and outputs are \
            specific to the kind of operation."
        )
    )]
    BackendOperation {
        /// Step under which the operation ran.
        #[cfg_attr(test, schemars(description = "The step the operation ran in."))]
        step: StepId,
        /// Stable operation kind (e.g. `generate_key`, `sign_data`).
        #[cfg_attr(
            test,
            schemars(description = "The kind of operation, such as generate_key or sign_data.")
        )]
        kind: String,
        /// The backend the step ran with, by the name the ceremony gives it;
        /// its identity is on that backend's [`StepFact::BackendBound`].
        /// Absent for an operation done in software.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[cfg_attr(
            test,
            schemars(
                description = "The backend the step ran with, by the name the ceremony gives it.                 Its identity is on that backend's backend_bound fact. Absent for an operation                 done in software."
            )
        )]
        backend: Option<String>,
        /// Structured inputs to the operation (parameters, references).
        #[cfg_attr(
            test,
            schemars(
                description = "What the operation was given: parameters and references to \
                artifacts or materials."
            )
        )]
        inputs: serde_json::Value,
        /// Structured outputs from the operation (artifact ids, hashes).
        #[cfg_attr(
            test,
            schemars(description = "What the operation produced: artifact names and digests.")
        )]
        outputs: serde_json::Value,
        /// Optional fingerprint of the produced material.
        #[cfg_attr(
            test,
            schemars(
                description = "A fingerprint of the material the operation produced, when it \
                has one."
            )
        )]
        fingerprint: Option<String>,
    },
    /// A key held outside the ceremony was used as the recipient of a wrap.
    ///
    /// Its own fact rather than a field on the wrap operation: the method of a
    /// wrap is publishable and the identity of a custodian may not be, and a
    /// fact carries one confidentiality level.
    ///
    /// The fingerprint records the bytes that were present. Nothing in the
    /// ceremony corroborates that they belong to the intended recipient, and
    /// nothing shows the recipient can open the result. The fact states an
    /// assumption the ceremony rests on, not something Rite proved.
    #[cfg_attr(
        test,
        schemars(
            description = "A key held outside the ceremony received a wrapped key. A separate \
            fact from the operation, so the method of a wrap can be published while the recipient \
            stays withheld."
        )
    )]
    WrapRecipientRecorded {
        /// Step that wrapped to this recipient.
        #[cfg_attr(test, schemars(description = "The step that wrapped the key."))]
        step: StepId,
        /// The artifact or material the recipient key came from, as named in
        /// the ceremony.
        #[cfg_attr(
            test,
            schemars(
                description = "The artifact or material the recipient's key came from, by its \
                name in the ceremony."
            )
        )]
        source: String,
        /// `sha256:<hex>` over the recipient's SPKI DER.
        #[cfg_attr(
            test,
            schemars(
                description = "The digest of the recipient's public key, as DER-encoded \
                SubjectPublicKeyInfo.",
                with = "Sha256Digest"
            )
        )]
        fingerprint: String,
        /// Whether the ceremony declared this fingerprint in advance, so the
        /// runtime checked the key it received against the definition rather
        /// than recording whatever arrived.
        #[cfg_attr(
            test,
            schemars(
                description = "Whether the ceremony named this fingerprint in advance. When \
                it did, the key received was checked against it; otherwise the fingerprint records \
                whatever key arrived."
            )
        )]
        declared: bool,
    },
    /// A human attestation was recorded.
    #[cfg_attr(test, schemars(description = "A person made an attestation."))]
    AttestationRecorded {
        /// Step under which the attestation was recorded.
        #[cfg_attr(test, schemars(description = "The step the attestation was made in."))]
        step: StepId,
        /// Role that issued the attestation.
        #[cfg_attr(test, schemars(description = "The role that made the attestation."))]
        role: RoleId,
        /// Verbatim attestation statement.
        #[cfg_attr(test, schemars(description = "The statement, word for word."))]
        statement: String,
    },
    /// An artifact was written to the run's `artifacts/` directory.
    #[cfg_attr(
        test,
        schemars(description = "An artifact was written to the run's artifacts directory.")
    )]
    ArtifactWritten {
        /// Step that produced the artifact.
        #[cfg_attr(test, schemars(description = "The step that produced the artifact."))]
        step: StepId,
        /// Artifact name as declared in the DSL.
        #[cfg_attr(
            test,
            schemars(description = "The artifact's name, as declared in the ceremony.")
        )]
        name: String,
        /// File name under `artifacts/`: one path component, no directory.
        #[cfg_attr(
            test,
            schemars(
                description = "The file name in the artifacts directory: a single path \
                component."
            )
        )]
        file: String,
        /// Digest of the file's bytes. Absent when the artifact is content a
        /// step opened: a digest of a secret can be tested against guesses,
        /// so none is recorded.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[cfg_attr(
            test,
            schemars(
                description = "The digest of the file's bytes. Absent for content a step \
                opened, since a digest of a secret can be tested against guesses."
            )
        )]
        digest: Option<Sha256Digest>,
    },
    /// A deviation was logged by the operator.
    #[cfg_attr(test, schemars(description = "An operator recorded a deviation."))]
    DeviationRecorded {
        /// Step in which the deviation was logged, if any. `None` for
        /// deviations logged outside of a step (before the first step or
        /// while a ceremony-level prompt is pending).
        #[serde(skip_serializing_if = "Option::is_none")]
        #[cfg_attr(
            test,
            schemars(
                description = "The step during which the deviation was recorded, or absent \
                outside any step."
            )
        )]
        step: Option<StepId>,
        /// Verbatim deviation text.
        #[cfg_attr(test, schemars(description = "The deviation, word for word."))]
        text: String,
    },
    /// A step attempt failed. Recorded per attempt, so a retried step shows
    /// `StepAttemptFailed{attempt: 1}` followed by the operator's retry
    /// decision and, on success, `StepCompleted`. The final attempt of a step
    /// that the run gives up on is followed by the terminal `CeremonyFailed`.
    #[cfg_attr(
        test,
        schemars(
            description = "One attempt at a step failed. Recorded per attempt: a step that is \
            retried and then succeeds has this fact for each failed attempt, then step_completed."
        )
    )]
    StepAttemptFailed {
        /// Step whose attempt failed.
        #[cfg_attr(test, schemars(description = "The step."))]
        step: StepId,
        /// 1-based attempt number within this step.
        #[cfg_attr(
            test,
            schemars(description = "The attempt's number within the step, starting at 1.")
        )]
        attempt: u32,
        /// Structured error record for the failed attempt.
        #[cfg_attr(test, schemars(description = "What went wrong in this attempt."))]
        error: ErrorRecord,
    },
    /// Step finished executing.
    #[cfg_attr(test, schemars(description = "A step finished."))]
    StepCompleted {
        /// Step identifier.
        #[cfg_attr(test, schemars(description = "The step."))]
        id: StepId,
        /// How the step ended.
        #[cfg_attr(test, schemars(description = "How the step ended."))]
        outcome: StepOutcome,
    },
    /// Ceremony finished successfully.
    ///
    /// The transcript's fingerprint is the `chain` value of this line, which
    /// any reader recomputes, so the fact itself carries no fingerprint
    /// field. The runtime forwards the value to frontends through an
    /// out-of-band channel event.
    #[cfg_attr(
        test,
        schemars(
            description = "The ceremony finished. It is the last line of a transcript whose \
            run completed."
        )
    )]
    CeremonyCompleted {},
    /// Ceremony failed or was aborted.
    #[cfg_attr(
        test,
        schemars(
            description = "The ceremony failed or was aborted. It is the last line of a \
            transcript whose run did not complete."
        )
    )]
    CeremonyFailed {
        /// Structured error record.
        #[cfg_attr(test, schemars(description = "What went wrong."))]
        error: ErrorRecord,
    },
    /// The ceremony entropy source was seeded with machine randomness.
    ///
    /// Emitted once by the runner at ceremony start (run-metadata). Records
    /// the machine contribution `m` and the frozen derivation scheme so any
    /// value the ceremony later draws is re-derivable from the transcript
    /// alone. Part of the [entropy source](StepFact::EntropyDrawn) family.
    ///
    /// Like every fact, it is timed by the `at` on its chain envelope.
    #[cfg_attr(
        test,
        schemars(
            description = "The ceremony's entropy source was seeded with machine randomness. \
            Recorded once, at the start, so every value drawn from the source can be derived again \
            from the transcript."
        )
    )]
    EntropySeeded {
        /// Lowercase hex of the gathered machine entropy `m`.
        #[cfg_attr(
            test,
            schemars(
                description = "The machine randomness, as lowercase hex.",
                extend("pattern" = "^(?:[0-9a-f]{2})+$")
            )
        )]
        m: String,
        /// Provenance of `m` (e.g. `os`). A single label today; comma-separated
        /// if more than one source is ever mixed.
        #[cfg_attr(
            test,
            schemars(description = "Where the machine randomness came from, such as os.")
        )]
        source: String,
        /// Frozen derivation-scheme tag (e.g. `rite-kdf/v1`) that pins the
        /// entire construction. A verifier rejects an unrecognised value.
        #[cfg_attr(
            test,
            schemars(
                description = "The derivation scheme, such as rite-kdf/v1. A verifier refuses \
                a scheme it does not know."
            )
        )]
        derivation: String,
    },
    /// A human folded additional entropy into the seed, advancing the ratchet.
    ///
    /// Emitted by the authored `gather_entropy` step. The verbatim operator
    /// contribution is recorded so the epoch chain re-folds identically; it is
    /// public, witnessed entropy, not a secret. Timed by its enclosing step
    /// boundaries (and by the `PromptAnswered` that captured the input).
    #[cfg_attr(
        test,
        schemars(
            description = "A person added their own randomness to the entropy source, \
            starting a new epoch."
        )
    )]
    EntropyContributed {
        /// Step under which the contribution was gathered.
        #[cfg_attr(test, schemars(description = "The step the contribution was made in."))]
        step: StepId,
        /// Epoch index produced by this fold (1 for the first contribution).
        #[cfg_attr(
            test,
            schemars(description = "The epoch this contribution starts, counting from 1.")
        )]
        epoch: u32,
        /// Verbatim operator contribution, fed as UTF-8 into the ratchet.
        #[cfg_attr(
            test,
            schemars(
                description = "The contribution, word for word, mixed into the source as \
                UTF-8."
            )
        )]
        contribution: String,
    },
    /// A value was drawn from the entropy source (a nonce, certificate serial,
    /// or challenge).
    ///
    /// Emitted whenever an action draws bytes from the entropy source. The
    /// derivation `path` plus the recorded seed let `rite verify` re-derive
    /// the value and confirm the right value reached the right consumer. Like
    /// other action-emitted evidence, it is timed by its enclosing step.
    #[cfg_attr(
        test,
        schemars(
            description = "A value was drawn from the entropy source, such as a nonce, a \
            certificate serial number or a challenge. A verifier derives it again from the seed, \
            the contributions and the path."
        )
    )]
    EntropyDrawn {
        /// Step that drew the value.
        #[cfg_attr(test, schemars(description = "The step that drew the value."))]
        step: StepId,
        /// Derivation path `<epoch>/<step>/<purpose>`.
        #[cfg_attr(
            test,
            schemars(description = "The derivation path: `<epoch>/<step>/<purpose>`.")
        )]
        path: String,
        /// Lowercase hex of the derived bytes. Its length fixes the byte count,
        /// so `rite verify` re-derives exactly this many bytes from the seed.
        #[cfg_attr(
            test,
            schemars(
                description = "The value, as lowercase hex. Its length is the number of bytes \
                drawn.",
                extend("pattern" = "^(?:[0-9a-f]{2})*$")
            )
        )]
        value: String,
    },
}

/// The `type` tag of every [`StepFact`] variant in this vocabulary.
///
/// A reader uses it to tell a fact from a newer vocabulary, which it counts and
/// skips, from a known fact that does not parse, which is an error.
pub const FACT_TYPES: &[&str] = &[
    "ceremony_started",
    "role_declared",
    "role_assigned",
    "parameter_bound",
    "material_loaded",
    "material_digest",
    "backend_bound",
    "machine_info_recorded",
    "act_started",
    "step_started",
    "prompt_answered",
    "backend_operation",
    "wrap_recipient_recorded",
    "attestation_recorded",
    "artifact_written",
    "deviation_recorded",
    "step_attempt_failed",
    "step_completed",
    "ceremony_completed",
    "ceremony_failed",
    "entropy_seeded",
    "entropy_contributed",
    "entropy_drawn",
];

impl StepFact {
    /// The level a fact of this type is recorded at.
    ///
    /// Method facts (what was done, in what order, with what result) are
    /// public, and so are attestations, whose statement is authored in the
    /// ceremony. Free text, answers typed at prompts, the error message of a
    /// failed attempt, and identities of devices and inputs are restricted.
    /// Persons, material digests and the host snapshot are confidential.
    ///
    /// A failed ceremony stays public despite its error message: a public
    /// disclosure needs its terminal fact.
    #[must_use]
    pub fn default_level(&self) -> Level {
        match self {
            StepFact::CeremonyStarted { .. }
            | StepFact::RoleDeclared { .. }
            | StepFact::ActStarted { .. }
            | StepFact::StepStarted { .. }
            | StepFact::BackendOperation { .. }
            | StepFact::ArtifactWritten { .. }
            | StepFact::AttestationRecorded { .. }
            | StepFact::StepCompleted { .. }
            | StepFact::CeremonyCompleted {}
            | StepFact::CeremonyFailed { .. }
            // Re-deriving the entropy source needs all three.
            | StepFact::EntropySeeded { .. }
            | StepFact::EntropyContributed { .. }
            | StepFact::EntropyDrawn { .. } => Level::PUBLIC,
            StepFact::ParameterBound { .. }
            | StepFact::MaterialLoaded { .. }
            | StepFact::BackendBound { .. }
            | StepFact::PromptAnswered { .. }
            | StepFact::WrapRecipientRecorded { .. }
            | StepFact::StepAttemptFailed { .. }
            | StepFact::DeviationRecorded { .. } => Level::RESTRICTED,
            StepFact::RoleAssigned { .. }
            | StepFact::MaterialDigest { .. }
            | StepFact::MachineInfoRecorded { .. } => Level::CONFIDENTIAL,
        }
    }

    /// Whether this fact evidences work performed on the world: a backend
    /// operation, a written artifact, a captured attestation, or consumed
    /// entropy. Interaction records (an answered prompt, an operator
    /// deviation note) and lifecycle markers are not side effects: repeating
    /// the interaction is safe and simply produces a fresh record.
    ///
    /// The runtime's retry gate refuses to re-execute a step attempt that
    /// already emitted a side-effect fact.
    #[must_use]
    pub fn is_side_effect(&self) -> bool {
        match self {
            StepFact::BackendOperation { .. }
            | StepFact::AttestationRecorded { .. }
            | StepFact::ArtifactWritten { .. }
            | StepFact::EntropyContributed { .. }
            | StepFact::EntropyDrawn { .. } => true,
            StepFact::CeremonyStarted { .. }
            // Run inputs and identities, recorded before any step works.
            | StepFact::RoleDeclared { .. }
            | StepFact::RoleAssigned { .. }
            | StepFact::ParameterBound { .. }
            | StepFact::MaterialLoaded { .. }
            | StepFact::MaterialDigest { .. }
            | StepFact::BackendBound { .. }
            // Records the host, not work performed on it.
            | StepFact::MachineInfoRecorded { .. }
            | StepFact::ActStarted { .. }
            | StepFact::StepStarted { .. }
            | StepFact::PromptAnswered { .. }
            // Records an input the step read, not work it performed.
            | StepFact::WrapRecipientRecorded { .. }
            | StepFact::DeviationRecorded { .. }
            | StepFact::StepAttemptFailed { .. }
            | StepFact::StepCompleted { .. }
            | StepFact::CeremonyCompleted {}
            | StepFact::CeremonyFailed { .. }
            | StepFact::EntropySeeded { .. } => false,
        }
    }
}

/// JSON-shape snapshot tests, the tripwire for accidental wire-format breaks.
///
/// Every variant of [`StepFact`], [`Prompt`], [`ResponseRecord`], [`StepOutcome`],
/// and [`ValidatorSpec`] is serialized once with fixed payloads and compared
/// against an inline JSON literal. The on-disk transcript schema is what
/// `rite verify`, `rite report`, and any third-party verifier consume; a
/// rename, a `serde(tag)` change, a `rename_all` flip, or a timestamp format
/// swap must surface here, not in the field.
///
/// Breaking the format is allowed in early beta, **deliberately**, update
/// the fixture in the same commit so the diff documents the wire change.
#[cfg(test)]
mod validator_tests {
    use super::*;

    fn shape(format: Format, min: usize, max: usize) -> ValidatorSpec {
        ValidatorSpec::Format {
            format,
            min_length: Some(min),
            max_length: Some(max),
        }
    }

    #[test]
    fn a_text_format_checks_characters_and_length() {
        let pin = shape(Format::Digits, 6, 8);
        assert!(pin.check("123456").is_ok());
        assert!(pin.check("12345678").is_ok());
        assert!(pin.check("12345").unwrap_err().contains("this is 5"));
        assert!(pin.check("123456789").unwrap_err().contains("this is 9"));
        assert!(pin.check("12345a").unwrap_err().contains("digits"));

        assert!(shape(Format::Alphanumeric, 1, 4).check("ab12").is_ok());
        assert!(shape(Format::Alphanumeric, 1, 4).check("ab-1").is_err());
        assert!(shape(Format::Text, 1, 4).check("ab-1").is_ok());
        assert!(
            ValidatorSpec::Format {
                format: Format::Text,
                min_length: None,
                max_length: None,
            }
            .check("  ")
            .is_err(),
            "a value is still something"
        );
    }

    /// The pattern covers the whole value: a rule that read `[0-9]+` and
    /// accepted `abc123` would pass what its author meant to refuse.
    #[test]
    fn a_pattern_must_match_the_whole_value() {
        let digits = ValidatorSpec::Regex("[0-9]+".to_string());
        assert!(digits.check("123").is_ok());
        assert!(digits.check("abc123").is_err());
        assert!(digits.check("123abc").is_err());
        // Alternation is grouped before anchoring.
        let either = ValidatorSpec::Regex("yes|no".to_string());
        assert!(either.check("no").is_ok());
        assert!(either.check("nope").is_err());
    }

    #[test]
    fn a_refusal_names_the_rule_and_not_the_value() {
        let err = shape(Format::Digits, 6, 6).check("hunter2").unwrap_err();
        assert!(!err.contains("hunter2"));
        let err = ValidatorSpec::Regex("[0-9]+".to_string())
            .check("hunter2")
            .unwrap_err();
        assert!(!err.contains("hunter2"));
    }

    #[test]
    fn a_hint_reads_as_a_person_would_say_it() {
        assert_eq!(shape(Format::Digits, 6, 6).hint().unwrap(), "6 digits");
        assert_eq!(
            shape(Format::Text, 6, 8).hint().unwrap(),
            "6 to 8 characters"
        );
        assert_eq!(
            ValidatorSpec::Format {
                format: Format::Hex,
                min_length: Some(32),
                max_length: None,
            }
            .hint()
            .unwrap(),
            "at least 32 bytes as hex"
        );
        assert_eq!(
            ValidatorSpec::Format {
                format: Format::Alphanumeric,
                min_length: None,
                max_length: None,
            }
            .hint()
            .unwrap(),
            "letters or digits only"
        );
        assert_eq!(
            ValidatorSpec::Format {
                format: Format::Base64,
                min_length: None,
                max_length: None,
            }
            .hint()
            .unwrap(),
            "bytes as base64"
        );
        assert!(ValidatorSpec::NonEmpty.hint().is_none());
    }

    /// The string is transport: grouping and case are dropped, and what is
    /// checked is the bytes.
    #[test]
    fn an_encoding_decodes_and_counts_bytes() {
        let key = shape(Format::Hex, 4, 4);
        assert!(key.check("deadbeef").is_ok());
        assert!(key.check("DE AD be ef").is_ok());
        assert!(key.check("deadbe").unwrap_err().contains("this is 3"));
        assert!(key.check("deadbeefx").unwrap_err().contains("hex"));
        assert_eq!(Format::Hex.decode("DE AD").unwrap(), vec![0xde, 0xad]);

        let b64 = ValidatorSpec::Format {
            format: Format::Base64,
            min_length: None,
            max_length: None,
        };
        assert!(b64.check("aGVsbG8=").is_ok());
        assert!(b64.check("aGVsbG8").is_err(), "padding is required");
        assert_eq!(Format::Base64.decode("aGVs\nbG8=").unwrap(), b"hello");
        assert!(Format::Digits.decode("12").is_err(), "text does not decode");
    }

    #[test]
    fn a_placeholder_satisfies_its_own_format() {
        for format in [
            Format::Text,
            Format::Digits,
            Format::Alphanumeric,
            Format::Hex,
            Format::Base64,
        ] {
            let stand_in = format.placeholder(32).unwrap();
            assert!(shape(format, 32, 32).check(&stand_in).is_ok());
        }
        assert!(Format::Text.placeholder(PLACEHOLDER_LIMIT + 1).is_none());
    }
}

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

    fn assert_json(fact: &StepFact, expected: &serde_json::Value) {
        let actual = serde_json::to_value(fact).expect("serialize StepFact");
        assert_eq!(&actual, expected, "wire-format drift for {fact:?}");
        let tag = actual
            .get("type")
            .and_then(serde_json::Value::as_str)
            .expect("a type tag");
        assert!(FACT_TYPES.contains(&tag), "{tag} missing from FACT_TYPES");
        crate::schema::assert_fact_matches_schema(&actual);
    }

    #[test]
    fn a_level_is_named_or_numbered_and_must_be_declared() {
        let header = TranscriptHeader::new("rite test", "0", false);
        assert_eq!(header.level("public"), Some(Level::PUBLIC));
        assert_eq!(header.level("20"), Some(Level::RESTRICTED));
        assert_eq!(header.level("15"), None);
        assert_eq!(header.level("partners"), None);
    }

    #[test]
    fn levels_are_ordered_integers() {
        assert!(Level::PUBLIC < Level::RESTRICTED);
        assert!(Level::RESTRICTED < Level::CONFIDENTIAL);
        assert!(Level::new(15) > Level::PUBLIC && Level::new(15) < Level::RESTRICTED);
        assert_eq!(
            serde_json::to_value(Level::CONFIDENTIAL).expect("serialize"),
            json!(30)
        );
        assert_eq!(Level::RESTRICTED.to_string(), "restricted");
        assert_eq!(Level::new(15).to_string(), "level 15");
    }

    #[test]
    fn ceremony_started() {
        assert_json(
            &StepFact::CeremonyStarted {
                name: "Root CA".to_string(),
                template: Sha256Digest::of(b"ceremony"),
            },
            &json!({
                "type": "ceremony_started",
                "name": "Root CA",
                "template": Sha256Digest::of(b"ceremony").as_str(),
            }),
        );
    }

    #[test]
    fn header() {
        let header = TranscriptHeader::new("rite 0.6.0", &"0".repeat(32), false);
        assert_eq!(
            serde_json::to_value(&header).expect("serialize header"),
            json!({
                "rite_transcript": 0,
                "vocabulary": 0,
                "producer": "rite 0.6.0",
                "run_id": "0".repeat(32),
                "dry_run": false,
                "levels": { "public": 10, "restricted": 20, "confidential": 30 },
            }),
        );
    }

    #[test]
    fn role_assigned() {
        assert_json(
            &StepFact::RoleAssigned {
                role: RoleId::new("crypto_officer"),
                person: "Alice Rivera".to_string(),
            },
            &json!({
                "type": "role_assigned",
                "role": "crypto_officer",
                "person": "Alice Rivera",
            }),
        );
    }

    #[test]
    fn parameter_bound() {
        assert_json(
            &StepFact::ParameterBound {
                name: ParamId::new("validity_days"),
                value: json!(3650),
            },
            &json!({
                "type": "parameter_bound",
                "name": "validity_days",
                "value": 3650,
            }),
        );
    }

    #[test]
    fn material_loaded() {
        assert_json(
            &StepFact::MaterialLoaded {
                name: MaterialId::new("usb_drive"),
                identifier: Some("SN-1234".to_string()),
            },
            &json!({
                "type": "material_loaded",
                "name": "usb_drive",
                "identifier": "SN-1234",
            }),
        );
        assert_json(
            &StepFact::MaterialLoaded {
                name: MaterialId::new("csr"),
                identifier: None,
            },
            &json!({ "type": "material_loaded", "name": "csr" }),
        );
    }

    #[test]
    fn material_digest() {
        assert_json(
            &StepFact::MaterialDigest {
                name: MaterialId::new("csr"),
                digest: Sha256Digest::of(b"csr"),
            },
            &json!({
                "type": "material_digest",
                "name": "csr",
                "digest": Sha256Digest::of(b"csr").as_str(),
            }),
        );
    }

    #[test]
    fn backend_bound() {
        assert_json(
            &StepFact::BackendBound {
                name: "token".to_string(),
                provider: "yubikey".to_string(),
                identity: "yubikey-serial=12345678+firmware=5.7.1".to_string(),
            },
            &json!({
                "type": "backend_bound",
                "name": "token",
                "provider": "yubikey",
                "identity": "yubikey-serial=12345678+firmware=5.7.1",
            }),
        );
    }

    #[test]
    fn machine_info_recorded() {
        assert_json(
            &StepFact::MachineInfoRecorded {
                step: StepId::new("capture"),
                info: json!({ "arch": "x86_64", "hostname": null }),
            },
            &json!({
                "type": "machine_info_recorded",
                "step": "capture",
                "info": { "arch": "x86_64", "hostname": null },
            }),
        );
    }

    #[test]
    fn act_started() {
        assert_json(
            &StepFact::ActStarted {
                id: ActId::new("setup"),
                label: "Setup".to_string(),
            },
            &json!({
                "type": "act_started",
                "id": "setup",
                "label": "Setup",
            }),
        );
    }

    #[test]
    fn step_started() {
        assert_json(
            &StepFact::StepStarted {
                id: StepId::new("s1"),
                label: "2.1".to_string(),
                role: RoleId::new("crypto_officer"),
            },
            &json!({
                "type": "step_started",
                "id": "s1",
                "label": "2.1",
                "role": "crypto_officer",
            }),
        );
    }

    #[test]
    fn role_declared() {
        assert_json(
            &StepFact::RoleDeclared {
                role: RoleId::new("crypto_officer"),
                name: "Crypto Officer".to_string(),
            },
            &json!({
                "type": "role_declared",
                "role": "crypto_officer",
                "name": "Crypto Officer",
            }),
        );
    }

    #[test]
    fn prompt_answered_confirm_bool() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Confirm {
                    question: "Proceed?".to_string(),
                    default: Some(true),
                },
                response: ResponseRecord::Bool { value: true },
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": { "type": "confirm", "question": "Proceed?", "default": true },
                "response": { "type": "bool", "value": true },
            }),
        );
    }

    #[test]
    fn prompt_answered_text_nonempty() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Text {
                    label: "Name".to_string(),
                    validator: ValidatorSpec::NonEmpty,
                },
                response: ResponseRecord::Text {
                    value: "Alice".to_string(),
                },
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": {
                    "type": "text",
                    "label": "Name",
                    "validator": { "kind": "non_empty" },
                },
                "response": { "type": "text", "value": "Alice" },
            }),
        );
    }

    #[test]
    fn prompt_answered_text_regex() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Text {
                    label: "SN".to_string(),
                    validator: ValidatorSpec::Regex(r"^[A-Z0-9]+$".to_string()),
                },
                response: ResponseRecord::Text {
                    value: "AB12".to_string(),
                },
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": {
                    "type": "text",
                    "label": "SN",
                    "validator": { "kind": "regex", "value": "^[A-Z0-9]+$" },
                },
                "response": { "type": "text", "value": "AB12" },
            }),
        );
    }

    #[test]
    fn prompt_answered_text_predefined() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Text {
                    label: "SN".to_string(),
                    validator: ValidatorSpec::Predefined("serial_number".to_string()),
                },
                response: ResponseRecord::Text {
                    value: "ABCD".to_string(),
                },
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": {
                    "type": "text",
                    "label": "SN",
                    "validator": { "kind": "predefined", "value": "serial_number" },
                },
                "response": { "type": "text", "value": "ABCD" },
            }),
        );
    }

    #[test]
    fn prompt_answered_secret_redacted() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Secret {
                    label: "PIN".to_string(),
                    validator: ValidatorSpec::NonEmpty,
                },
                response: ResponseRecord::SecretRedacted {},
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": {
                    "type": "secret",
                    "label": "PIN",
                    "validator": { "kind": "non_empty" },
                },
                "response": { "type": "secret_redacted" },
            }),
        );
    }

    #[test]
    fn prompt_answered_literal_text() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Literal {
                    label: "Type 'attest'".to_string(),
                    expected: "attest".to_string(),
                },
                response: ResponseRecord::Text {
                    value: "attest".to_string(),
                },
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": { "type": "literal", "label": "Type 'attest'", "expected": "attest" },
                "response": { "type": "text", "value": "attest" },
            }),
        );
    }

    #[test]
    fn prompt_answered_continue_acknowledged() {
        assert_json(
            &StepFact::PromptAnswered {
                step: Some(StepId::new("s1")),
                prompt: Prompt::Continue {
                    hint: Some("Press Enter".to_string()),
                },
                response: ResponseRecord::Acknowledged,
            },
            &json!({
                "type": "prompt_answered",
                "step": "s1",
                "prompt": { "type": "continue", "hint": "Press Enter" },
                "response": { "type": "acknowledged" },
            }),
        );
    }

    #[test]
    fn backend_operation() {
        assert_json(
            &StepFact::BackendOperation {
                step: StepId::new("s1"),
                kind: "generate_key".to_string(),
                backend: Some("hsm".to_string()),
                inputs: json!({ "algorithm": "rsa", "bits": 4096 }),
                outputs: json!({ "key_id": "k1" }),
                fingerprint: Some("sha256:deadbeef".to_string()),
            },
            &json!({
                "type": "backend_operation",
                "step": "s1",
                "kind": "generate_key",
                "backend": "hsm",
                "inputs": { "algorithm": "rsa", "bits": 4096 },
                "outputs": { "key_id": "k1" },
                "fingerprint": "sha256:deadbeef",
            }),
        );
    }

    #[test]
    fn wrap_recipient_recorded() {
        let fingerprint = Sha256Digest::of(b"escrow spki").to_string();
        assert_json(
            &StepFact::WrapRecipientRecorded {
                step: StepId::new("s1"),
                source: "escrow_pubkey".to_string(),
                fingerprint: fingerprint.clone(),
                declared: true,
            },
            &json!({
                "type": "wrap_recipient_recorded",
                "step": "s1",
                "source": "escrow_pubkey",
                "fingerprint": fingerprint,
                "declared": true,
            }),
        );
    }

    #[test]
    fn attestation_recorded() {
        assert_json(
            &StepFact::AttestationRecorded {
                step: StepId::new("s1"),
                role: RoleId::new("crypto_officer"),
                statement: "I confirm.".to_string(),
            },
            &json!({
                "type": "attestation_recorded",
                "step": "s1",
                "role": "crypto_officer",
                "statement": "I confirm.",
            }),
        );
    }

    #[test]
    fn artifact_written() {
        assert_json(
            &StepFact::ArtifactWritten {
                step: StepId::new("s1"),
                name: "root_cert".to_string(),
                file: "root_cert.crt".to_string(),
                digest: Some(Sha256Digest::of(b"cert")),
            },
            &json!({
                "type": "artifact_written",
                "step": "s1",
                "name": "root_cert",
                "file": "root_cert.crt",
                "digest": Sha256Digest::of(b"cert").as_str(),
            }),
        );
    }

    #[test]
    fn artifact_written_secret_has_no_digest() {
        assert_json(
            &StepFact::ArtifactWritten {
                step: StepId::new("s1"),
                name: "opened".to_string(),
                file: "opened.bin".to_string(),
                digest: None,
            },
            &json!({
                "type": "artifact_written",
                "step": "s1",
                "name": "opened",
                "file": "opened.bin",
            }),
        );
    }

    #[test]
    fn deviation_recorded() {
        assert_json(
            &StepFact::DeviationRecorded {
                step: Some(StepId::new("s1")),
                text: "phone rang".to_string(),
            },
            &json!({
                "type": "deviation_recorded",
                "step": "s1",
                "text": "phone rang",
            }),
        );
    }

    #[test]
    fn step_completed_completed() {
        assert_json(
            &StepFact::StepCompleted {
                id: StepId::new("s1"),
                outcome: StepOutcome::Completed {
                    message: "done".to_string(),
                },
            },
            &json!({
                "type": "step_completed",
                "id": "s1",
                "outcome": { "status": "completed", "message": "done" },
            }),
        );
    }

    #[test]
    fn ceremony_completed() {
        assert_json(
            &StepFact::CeremonyCompleted {},
            &json!({
                "type": "ceremony_completed",
            }),
        );
    }

    #[test]
    fn ceremony_failed() {
        assert_json(
            &StepFact::CeremonyFailed {
                error: ErrorRecord::new(
                    ErrorClass::Abort,
                    "aborted",
                    "ceremony aborted by operator",
                ),
            },
            &json!({
                "type": "ceremony_failed",
                "error": {
                    "class": "abort",
                    "kind": "aborted",
                    "message": "ceremony aborted by operator",
                },
            }),
        );
    }

    #[test]
    fn step_attempt_failed() {
        assert_json(
            &StepFact::StepAttemptFailed {
                step: StepId::new("import_key"),
                attempt: 1,
                error: ErrorRecord::new(
                    ErrorClass::Environmental,
                    "backend_error",
                    "Token not present",
                ),
            },
            &json!({
                "type": "step_attempt_failed",
                "step": "import_key",
                "attempt": 1,
                "error": {
                    "class": "environmental",
                    "kind": "backend_error",
                    "message": "Token not present",
                },
            }),
        );
    }

    #[test]
    fn entropy_seeded() {
        assert_json(
            &StepFact::EntropySeeded {
                m: "00112233".to_string(),
                source: "os".to_string(),
                derivation: "rite-kdf/v1".to_string(),
            },
            &json!({
                "type": "entropy_seeded",
                "m": "00112233",
                "source": "os",
                "derivation": "rite-kdf/v1",
            }),
        );
    }

    #[test]
    fn entropy_contributed() {
        assert_json(
            &StepFact::EntropyContributed {
                step: StepId::new("roll_dice"),
                epoch: 1,
                contribution: "3 1 6 4 2 5".to_string(),
            },
            &json!({
                "type": "entropy_contributed",
                "step": "roll_dice",
                "epoch": 1,
                "contribution": "3 1 6 4 2 5",
            }),
        );
    }

    #[test]
    fn entropy_drawn() {
        assert_json(
            &StepFact::EntropyDrawn {
                step: StepId::new("issue"),
                path: "0/issue/cert-serial".to_string(),
                value: "aabbccddeeff00112233".to_string(),
            },
            &json!({
                "type": "entropy_drawn",
                "step": "issue",
                "path": "0/issue/cert-serial",
                "value": "aabbccddeeff00112233",
            }),
        );
    }
}