stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Immutable, integrity-bound graph checkpoints for deterministic recovery.
//!
//! A checkpoint is a committed superstep barrier, not a mutable scheduler
//! snapshot. Its state, next ready-node set, graph definition, predecessor,
//! and exact journal head are all checksummed. Store implementations must
//! commit a checkpoint and its anchoring journal event in one transaction and
//! compare the predecessor with the locked current checkpoint head.

use std::{
    collections::{BTreeSet, btree_set},
    fmt,
    str::FromStr,
};

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{
    Deserialize, Deserializer, Serialize, Serializer,
    de::{self, SeqAccess},
};
use thiserror::Error;

use crate::decimal::{UnsignedDecimalError, parse_bounded_u64};
use crate::{
    BoundedJson, CanonicalJson, CanonicalJsonError, CapabilityIdentity, CheckpointId, Digest,
    JournalHead, JournalSequence, JsonLimits, RunId, SchemaReference, TenantId, Timestamp,
};

const MAX_DATABASE_ORDINAL: u64 = i64::MAX as u64;
const SUPERSTEP_PATTERN: &str = "^(0|[1-9][0-9]{0,18})$";
const NODE_ID_PATTERN: &str = "^(?!\\.{1,2}$)[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$";
const STATE_DIGEST_DOMAIN: &[u8] = b"stateknot-checkpoint-state-v1\0";
const INTENT_DIGEST_DOMAIN: &[u8] = b"stateknot-checkpoint-intent-v1\0";
const CHECKPOINT_DIGEST_DOMAIN: &[u8] = b"stateknot-checkpoint-v1\0";

/// Zero-based graph superstep committed at a deterministic barrier.
///
/// The maximum matches a signed `PostgreSQL BIGINT`. Its JSON representation
/// is a canonical decimal string so every supported language transports the
/// full range without numeric precision loss.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Superstep(u64);

impl Superstep {
    /// Initial graph checkpoint position.
    pub const INITIAL: Self = Self(0);

    /// Largest position supported by the v1 storage contract.
    pub const MAX: Self = Self(MAX_DATABASE_ORDINAL);

    /// Constructs a `PostgreSQL`-compatible superstep.
    ///
    /// # Errors
    ///
    /// Returns [`SuperstepError::AboveMaximum`] when `value` exceeds signed
    /// `BIGINT`.
    pub const fn new(value: u64) -> Result<Self, SuperstepError> {
        if value > MAX_DATABASE_ORDINAL {
            return Err(SuperstepError::AboveMaximum);
        }
        Ok(Self(value))
    }

    /// Returns the zero-based integer position.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Returns the exact successor or `None` at the storage ceiling.
    #[must_use]
    pub const fn checked_next(self) -> Option<Self> {
        if self.0 == MAX_DATABASE_ORDINAL {
            None
        } else {
            Some(Self(self.0 + 1))
        }
    }
}

impl fmt::Display for Superstep {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl FromStr for Superstep {
    type Err = SuperstepError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let value = parse_bounded_u64(value, MAX_DATABASE_ORDINAL)
            .map_err(SuperstepError::from_decimal_error)?;
        Self::new(value)
    }
}

impl Serialize for Superstep {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for Superstep {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(SuperstepVisitor)
    }
}

struct SuperstepVisitor;

impl de::Visitor<'_> for SuperstepVisitor {
    type Value = Superstep;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a canonical non-negative decimal PostgreSQL BIGINT superstep")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        value.parse().map_err(E::custom)
    }
}

impl JsonSchema for Superstep {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "Superstep".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::Superstep").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "minLength": 1,
            "maxLength": 19,
            "pattern": SUPERSTEP_PATTERN
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Invalid canonical superstep value.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum SuperstepError {
    /// The wire value was empty or contained a non-decimal byte.
    #[error("superstep must contain only unsigned ASCII decimal digits")]
    InvalidFormat,

    /// The wire value contained a leading zero.
    #[error("superstep must use canonical decimal text")]
    NonCanonical,

    /// The value exceeded signed `PostgreSQL BIGINT`.
    #[error("superstep exceeds the PostgreSQL BIGINT maximum")]
    AboveMaximum,
}

impl SuperstepError {
    const fn from_decimal_error(error: UnsignedDecimalError) -> Self {
        match error {
            UnsignedDecimalError::Empty | UnsignedDecimalError::InvalidCharacter { .. } => {
                Self::InvalidFormat
            }
            UnsignedDecimalError::LeadingZero => Self::NonCanonical,
            UnsignedDecimalError::TooLong { .. } | UnsignedDecimalError::Overflow => {
                Self::AboveMaximum
            }
        }
    }
}

/// Stable, case-sensitive identity of one compiled graph node.
///
/// Node identities are durable schema, not display labels. Renaming a node is
/// therefore a graph migration. The bounded ASCII grammar is safe for logs,
/// maps, and database indexes while deliberately excluding path separators.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NodeId(Box<str>);

impl NodeId {
    /// Maximum encoded length in bytes.
    pub const MAX_LEN: usize = 128;

    /// Validates and constructs a node identity.
    ///
    /// # Errors
    ///
    /// Returns [`NodeIdError`] for empty, oversized, path-like, or unsupported
    /// identifier text.
    pub fn new(value: impl Into<String>) -> Result<Self, NodeIdError> {
        let value = value.into();
        validate_node_id(&value)?;
        Ok(Self(value.into_boxed_str()))
    }

    /// Returns the exact case-sensitive identity.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for NodeId {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Debug for NodeId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("NodeId")
            .field(&self.as_str())
            .finish()
    }
}

impl fmt::Display for NodeId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl FromStr for NodeId {
    type Err = NodeIdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl TryFrom<String> for NodeId {
    type Error = NodeIdError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<&str> for NodeId {
    type Error = NodeIdError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl Serialize for NodeId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for NodeId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_string(NodeIdVisitor)
    }
}

struct NodeIdVisitor;

impl de::Visitor<'_> for NodeIdVisitor {
    type Value = NodeId;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a bounded canonical StateKnot graph node identifier")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        NodeId::try_from(value).map_err(E::custom)
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        NodeId::try_from(value).map_err(E::custom)
    }
}

impl JsonSchema for NodeId {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "NodeId".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::NodeId").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": NODE_ID_PATTERN
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

fn validate_node_id(value: &str) -> Result<(), NodeIdError> {
    if value.is_empty() {
        return Err(NodeIdError::Empty);
    }
    if value.len() > NodeId::MAX_LEN {
        return Err(NodeIdError::TooLong {
            max: NodeId::MAX_LEN,
            actual: value.len(),
        });
    }
    if matches!(value, "." | "..") {
        return Err(NodeIdError::PathLike);
    }
    if !value.as_bytes()[0].is_ascii_alphanumeric() {
        return Err(NodeIdError::InvalidByte { index: 0 });
    }
    if let Some((index, _)) = value
        .bytes()
        .enumerate()
        .find(|(_, byte)| !byte.is_ascii_alphanumeric() && !matches!(byte, b'.' | b'_' | b'-'))
    {
        return Err(NodeIdError::InvalidByte { index });
    }
    Ok(())
}

/// Invalid graph node identity.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum NodeIdError {
    /// The identity contained no bytes.
    #[error("node identifier must not be empty")]
    Empty,

    /// The identity exceeded [`NodeId::MAX_LEN`].
    #[error("node identifier is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted byte length.
        max: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// The identity was `.` or `..`.
    #[error("node identifier must not be path-like")]
    PathLike,

    /// A byte did not belong to the stable ASCII grammar.
    #[error("node identifier contains an invalid byte at offset {index}")]
    InvalidByte {
        /// Zero-based byte offset of the first invalid byte.
        index: usize,
    },
}

/// Deterministically ordered set of nodes eligible for the next superstep.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReadyNodes(BTreeSet<NodeId>);

impl ReadyNodes {
    /// Hard maximum number of ready nodes in one checkpoint.
    pub const MAX_LEN: usize = 1024;

    /// Constructs a bounded set, rejecting duplicate identities.
    ///
    /// # Errors
    ///
    /// Returns [`ReadyNodesError`] for a duplicate or more than
    /// [`ReadyNodes::MAX_LEN`] identities.
    pub fn try_new(nodes: impl IntoIterator<Item = NodeId>) -> Result<Self, ReadyNodesError> {
        let mut values = BTreeSet::new();
        for node in nodes {
            if values.contains(&node) {
                return Err(ReadyNodesError::Duplicate { node });
            }
            if values.len() == Self::MAX_LEN {
                return Err(ReadyNodesError::TooMany {
                    maximum: Self::MAX_LEN,
                    actual: Self::MAX_LEN + 1,
                });
            }
            values.insert(node);
        }
        Ok(Self(values))
    }

    /// Returns an empty ready set for a terminal or waiting barrier.
    #[must_use]
    pub const fn empty() -> Self {
        Self(BTreeSet::new())
    }

    /// Returns the number of ready nodes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns `true` when no node is immediately runnable.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns node identities in deterministic ASCII byte order.
    pub fn iter(&self) -> btree_set::Iter<'_, NodeId> {
        self.0.iter()
    }

    /// Returns `true` when the exact node identity is present.
    #[must_use]
    pub fn contains(&self, node: &NodeId) -> bool {
        self.0.contains(node)
    }
}

impl Default for ReadyNodes {
    fn default() -> Self {
        Self::empty()
    }
}

impl<'a> IntoIterator for &'a ReadyNodes {
    type Item = &'a NodeId;
    type IntoIter = btree_set::Iter<'a, NodeId>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl TryFrom<Vec<NodeId>> for ReadyNodes {
    type Error = ReadyNodesError;

    fn try_from(nodes: Vec<NodeId>) -> Result<Self, Self::Error> {
        Self::try_new(nodes)
    }
}

impl Serialize for ReadyNodes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_seq(self.iter())
    }
}

impl<'de> Deserialize<'de> for ReadyNodes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(ReadyNodesVisitor)
    }
}

struct ReadyNodesVisitor;

impl<'de> de::Visitor<'de> for ReadyNodesVisitor {
    type Value = ReadyNodes;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "an array containing at most {} unique graph node identifiers",
            ReadyNodes::MAX_LEN
        )
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut values = BTreeSet::new();
        while let Some(node) = sequence.next_element::<NodeId>()? {
            if values.contains(&node) {
                return Err(de::Error::custom(ReadyNodesError::Duplicate { node }));
            }
            if values.len() == ReadyNodes::MAX_LEN {
                return Err(de::Error::custom(ReadyNodesError::TooMany {
                    maximum: ReadyNodes::MAX_LEN,
                    actual: ReadyNodes::MAX_LEN + 1,
                }));
            }
            values.insert(node);
        }
        Ok(ReadyNodes(values))
    }
}

impl JsonSchema for ReadyNodes {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "ReadyNodes".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::ReadyNodes").into()
    }

    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "array",
            "items": generator.subschema_for::<NodeId>(),
            "maxItems": 1024,
            "uniqueItems": true,
            "description": "The wire array is serialized in deterministic ascending NodeId order. Runtime rejects duplicate identities."
        })
    }
}

/// Invalid next-superstep ready set.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum ReadyNodesError {
    /// The input repeated a node identity.
    #[error("ready-node set contains duplicate node {node:?}")]
    Duplicate {
        /// Repeated node identity.
        node: NodeId,
    },

    /// The hard ready-node ceiling was exceeded.
    #[error("ready-node set contains {actual} nodes; maximum is {maximum}")]
    TooMany {
        /// Absolute maximum.
        maximum: usize,
        /// First observed count beyond the maximum.
        actual: usize,
    },
}

/// Exact graph definition and state schema required to resume a checkpoint.
#[derive(Clone, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct GraphReference {
    identity: CapabilityIdentity,
    definition_digest: Digest,
    state_schema: SchemaReference,
}

impl GraphReference {
    /// Constructs an immutable graph reference.
    #[must_use]
    pub const fn new(
        identity: CapabilityIdentity,
        definition_digest: Digest,
        state_schema: SchemaReference,
    ) -> Self {
        Self {
            identity,
            definition_digest,
            state_schema,
        }
    }

    /// Returns the owner-qualified graph capability identity.
    #[must_use]
    pub const fn identity(&self) -> &CapabilityIdentity {
        &self.identity
    }

    /// Returns the checksum of the canonical compiled graph definition.
    #[must_use]
    pub const fn definition_digest(&self) -> Digest {
        self.definition_digest
    }

    /// Returns the exact schema required for checkpoint state.
    #[must_use]
    pub const fn state_schema(&self) -> &SchemaReference {
        &self.state_schema
    }
}

/// Immutable, schema-pinned JSON graph state with a verified checksum.
///
/// State JSON is materialized under [`JsonLimits::MAXIMUM`] (currently 2 MiB)
/// and must still be validated against [`Self::schema`] by the graph schema
/// registry before execution. The checksum binds both schema identity and RFC
/// 8785 state bytes.
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CheckpointState {
    schema: SchemaReference,
    data: BoundedJson,
    digest: Digest,
}

impl CheckpointState {
    /// Constructs state and computes its domain-separated checksum.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointStateError::Integrity`] when the state cannot be
    /// represented as RFC 8785 interoperable JSON.
    pub fn new(schema: SchemaReference, data: BoundedJson) -> Result<Self, CheckpointStateError> {
        let digest =
            compute_state_digest(&schema, &data).map_err(CheckpointStateError::integrity)?;
        Ok(Self {
            schema,
            data,
            digest,
        })
    }

    /// Restores state from durable columns and verifies its checksum.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointStateError`] when canonicalization fails or the
    /// supplied checksum does not match the schema and state bytes.
    pub fn restore(
        schema: SchemaReference,
        data: BoundedJson,
        digest: Digest,
    ) -> Result<Self, CheckpointStateError> {
        let expected =
            compute_state_digest(&schema, &data).map_err(CheckpointStateError::integrity)?;
        if digest != expected {
            return Err(CheckpointStateError::DigestMismatch);
        }
        Ok(Self {
            schema,
            data,
            digest,
        })
    }

    /// Returns the exact state schema reference.
    #[must_use]
    pub const fn schema(&self) -> &SchemaReference {
        &self.schema
    }

    /// Borrows validated resource-bounded state JSON.
    #[must_use]
    pub const fn data(&self) -> &BoundedJson {
        &self.data
    }

    /// Returns the checksum binding schema and canonical state bytes.
    #[must_use]
    pub const fn digest(&self) -> Digest {
        self.digest
    }
}

impl fmt::Debug for CheckpointState {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CheckpointState")
            .field("schema", &self.schema)
            .field("stats", &self.data.stats())
            .field("digest", &self.digest)
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for CheckpointState {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            schema: SchemaReference,
            #[serde(deserialize_with = "deserialize_checkpoint_data")]
            data: BoundedJson,
            digest: Digest,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::restore(wire.schema, wire.data, wire.digest).map_err(de::Error::custom)
    }
}

fn deserialize_checkpoint_data<'de, D>(deserializer: D) -> Result<BoundedJson, D::Error>
where
    D: Deserializer<'de>,
{
    BoundedJson::deserialize_with_limits(deserializer, JsonLimits::MAXIMUM)
}

/// Invalid or corrupted checkpoint state.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointStateError {
    /// Integrity material could not be canonicalized.
    #[error("checkpoint state integrity calculation failed: {source}")]
    Integrity {
        /// Underlying integrity failure.
        #[source]
        source: CheckpointIntegrityError,
    },

    /// The persisted checksum did not match state fields.
    #[error("checkpoint state digest does not match its schema and data")]
    DigestMismatch,
}

impl CheckpointStateError {
    const fn integrity(source: CheckpointIntegrityError) -> Self {
        Self::Integrity { source }
    }
}

/// Compact exact identity of a previously validated committed checkpoint.
///
/// A head is suitable for optimistic comparison but is not independently
/// self-validating because it omits state. Obtain it from [`Checkpoint::head`]
/// or trusted storage that has first restored the full checkpoint.
#[derive(Clone, Debug, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CheckpointHead {
    tenant_id: TenantId,
    run_id: RunId,
    checkpoint_id: CheckpointId,
    superstep: Superstep,
    graph: GraphReference,
    journal_head: JournalHead,
    digest: Digest,
}

impl CheckpointHead {
    /// Constructs a trusted head while enforcing its tenant/run scope.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointHeadError`] when the journal head crosses the
    /// checkpoint tenant or run boundary.
    pub fn new(
        tenant_id: TenantId,
        run_id: RunId,
        checkpoint_id: CheckpointId,
        superstep: Superstep,
        graph: GraphReference,
        journal_head: JournalHead,
        digest: Digest,
    ) -> Result<Self, CheckpointHeadError> {
        validate_journal_scope(&tenant_id, run_id, &journal_head)
            .map_err(CheckpointHeadError::from_scope)?;
        Ok(Self {
            tenant_id,
            run_id,
            checkpoint_id,
            superstep,
            graph,
            journal_head,
            digest,
        })
    }

    /// Returns the tenant boundary.
    #[must_use]
    pub const fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    /// Returns the durable run identity.
    #[must_use]
    pub const fn run_id(&self) -> RunId {
        self.run_id
    }

    /// Returns the immutable checkpoint identity.
    #[must_use]
    pub const fn checkpoint_id(&self) -> CheckpointId {
        self.checkpoint_id
    }

    /// Returns the committed barrier position.
    #[must_use]
    pub const fn superstep(&self) -> Superstep {
        self.superstep
    }

    /// Returns the exact graph definition reference.
    #[must_use]
    pub const fn graph(&self) -> &GraphReference {
        &self.graph
    }

    /// Returns the exact journal prefix included in this checkpoint.
    #[must_use]
    pub const fn journal_head(&self) -> &JournalHead {
        &self.journal_head
    }

    /// Returns the committed checkpoint checksum.
    #[must_use]
    pub const fn digest(&self) -> Digest {
        self.digest
    }
}

impl<'de> Deserialize<'de> for CheckpointHead {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            tenant_id: TenantId,
            run_id: RunId,
            checkpoint_id: CheckpointId,
            superstep: Superstep,
            graph: GraphReference,
            journal_head: JournalHead,
            digest: Digest,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::new(
            wire.tenant_id,
            wire.run_id,
            wire.checkpoint_id,
            wire.superstep,
            wire.graph,
            wire.journal_head,
            wire.digest,
        )
        .map_err(de::Error::custom)
    }
}

/// Invalid scope in a compact checkpoint head.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointHeadError {
    /// The journal head crossed a tenant boundary.
    #[error("checkpoint journal head crosses the checkpoint tenant boundary")]
    JournalTenantMismatch,

    /// The journal head named another run.
    #[error("checkpoint journal head does not belong to the checkpoint run")]
    JournalRunMismatch,
}

impl CheckpointHeadError {
    const fn from_scope(error: JournalScopeError) -> Self {
        match error {
            JournalScopeError::Tenant => Self::JournalTenantMismatch,
            JournalScopeError::Run => Self::JournalRunMismatch,
        }
    }
}

/// Stable checkpoint write intent before the anchoring journal event commits.
///
/// The intent checksum is the idempotency fingerprint for checkpoint storage.
/// It deliberately excludes the future journal head while binding every
/// caller-controlled field and the exact predecessor.
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CheckpointWrite {
    tenant_id: TenantId,
    run_id: RunId,
    checkpoint_id: CheckpointId,
    superstep: Superstep,
    graph: GraphReference,
    state: CheckpointState,
    ready_nodes: ReadyNodes,
    #[serde(skip_serializing_if = "Option::is_none")]
    parent: Option<CheckpointHead>,
    intent_digest: Digest,
}

impl CheckpointWrite {
    /// Constructs the initial superstep-zero checkpoint intent.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointWriteError`] when the state schema differs from the
    /// graph reference or integrity serialization fails.
    pub fn initial(
        tenant_id: TenantId,
        run_id: RunId,
        checkpoint_id: CheckpointId,
        graph: GraphReference,
        state: CheckpointState,
        ready_nodes: ReadyNodes,
    ) -> Result<Self, CheckpointWriteError> {
        Self::build(
            tenant_id,
            run_id,
            checkpoint_id,
            Superstep::INITIAL,
            graph,
            state,
            ready_nodes,
            None,
            None,
        )
    }

    /// Constructs the exact successor of a validated committed checkpoint.
    ///
    /// The graph reference and scope are inherited rather than accepted from
    /// the caller, preventing a recovery chain from silently switching graph
    /// code or tenant identity.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointWriteError`] for position exhaustion, a reused
    /// checkpoint ID, a mismatched state schema, or integrity failure.
    pub fn successor(
        checkpoint_id: CheckpointId,
        parent: &Checkpoint,
        state: CheckpointState,
        ready_nodes: ReadyNodes,
    ) -> Result<Self, CheckpointWriteError> {
        let superstep = parent
            .superstep
            .checked_next()
            .ok_or(CheckpointWriteError::SuperstepOverflow)?;
        Self::build(
            parent.tenant_id.clone(),
            parent.run_id,
            checkpoint_id,
            superstep,
            parent.graph.clone(),
            state,
            ready_nodes,
            Some(parent.head()),
            None,
        )
    }

    /// Restores a write intent from durable columns and verifies its checksum.
    ///
    /// This constructor exists for storage providers that map fields to typed
    /// columns instead of round-tripping the public JSON envelope.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointWriteError`] for any invalid initial/successor
    /// shape, crossed scope, graph/schema mismatch, or checksum mismatch.
    #[allow(clippy::too_many_arguments)]
    pub fn restore(
        tenant_id: TenantId,
        run_id: RunId,
        checkpoint_id: CheckpointId,
        superstep: Superstep,
        graph: GraphReference,
        state: CheckpointState,
        ready_nodes: ReadyNodes,
        parent: Option<CheckpointHead>,
        intent_digest: Digest,
    ) -> Result<Self, CheckpointWriteError> {
        Self::build(
            tenant_id,
            run_id,
            checkpoint_id,
            superstep,
            graph,
            state,
            ready_nodes,
            parent,
            Some(intent_digest),
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn build(
        tenant_id: TenantId,
        run_id: RunId,
        checkpoint_id: CheckpointId,
        superstep: Superstep,
        graph: GraphReference,
        state: CheckpointState,
        ready_nodes: ReadyNodes,
        parent: Option<CheckpointHead>,
        supplied_intent_digest: Option<Digest>,
    ) -> Result<Self, CheckpointWriteError> {
        validate_write_shape(
            &tenant_id,
            run_id,
            checkpoint_id,
            superstep,
            &graph,
            &state,
            parent.as_ref(),
        )?;
        let intent_digest = compute_intent_digest(&CheckpointIntentDigestWire {
            tenant_id: &tenant_id,
            run_id,
            checkpoint_id,
            superstep,
            graph: &graph,
            state_digest: state.digest,
            ready_nodes: &ready_nodes,
            parent: parent.as_ref(),
        })
        .map_err(CheckpointWriteError::integrity)?;
        if supplied_intent_digest.is_some_and(|supplied| supplied != intent_digest) {
            return Err(CheckpointWriteError::IntentDigestMismatch);
        }
        Ok(Self {
            tenant_id,
            run_id,
            checkpoint_id,
            superstep,
            graph,
            state,
            ready_nodes,
            parent,
            intent_digest,
        })
    }

    /// Returns the tenant boundary.
    #[must_use]
    pub const fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    /// Returns the durable run identity.
    #[must_use]
    pub const fn run_id(&self) -> RunId {
        self.run_id
    }

    /// Returns the new immutable checkpoint identity.
    #[must_use]
    pub const fn checkpoint_id(&self) -> CheckpointId {
        self.checkpoint_id
    }

    /// Returns the intended barrier position.
    #[must_use]
    pub const fn superstep(&self) -> Superstep {
        self.superstep
    }

    /// Returns the exact graph definition reference.
    #[must_use]
    pub const fn graph(&self) -> &GraphReference {
        &self.graph
    }

    /// Returns the immutable graph state.
    #[must_use]
    pub const fn state(&self) -> &CheckpointState {
        &self.state
    }

    /// Returns the sorted next-superstep ready set.
    #[must_use]
    pub const fn ready_nodes(&self) -> &ReadyNodes {
        &self.ready_nodes
    }

    /// Returns the exact predecessor, absent only at superstep zero.
    #[must_use]
    pub const fn parent(&self) -> Option<&CheckpointHead> {
        self.parent.as_ref()
    }

    /// Returns the stable idempotency checksum for this write.
    #[must_use]
    pub const fn intent_digest(&self) -> Digest {
        self.intent_digest
    }
}

impl fmt::Debug for CheckpointWrite {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CheckpointWrite")
            .field("tenant_id", &self.tenant_id)
            .field("run_id", &self.run_id)
            .field("checkpoint_id", &self.checkpoint_id)
            .field("superstep", &self.superstep)
            .field("graph", &self.graph)
            .field("state", &self.state)
            .field("ready_nodes", &self.ready_nodes)
            .field("parent", &self.parent)
            .field("intent_digest", &self.intent_digest)
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for CheckpointWrite {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            tenant_id: TenantId,
            run_id: RunId,
            checkpoint_id: CheckpointId,
            superstep: Superstep,
            graph: GraphReference,
            state: CheckpointState,
            ready_nodes: ReadyNodes,
            parent: Option<CheckpointHead>,
            intent_digest: Digest,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::build(
            wire.tenant_id,
            wire.run_id,
            wire.checkpoint_id,
            wire.superstep,
            wire.graph,
            wire.state,
            wire.ready_nodes,
            wire.parent,
            Some(wire.intent_digest),
        )
        .map_err(de::Error::custom)
    }
}

/// Structurally invalid or corrupted checkpoint write intent.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointWriteError {
    /// Checkpoint state named a schema other than the graph's pinned schema.
    #[error("checkpoint state schema does not match the graph state schema")]
    StateSchemaMismatch,

    /// Superstep zero unexpectedly named a predecessor.
    #[error("initial checkpoint must not contain a parent")]
    InitialHasParent,

    /// A positive superstep omitted its predecessor.
    #[error("non-initial checkpoint must contain a parent")]
    SuccessorMissingParent,

    /// The predecessor crossed a tenant boundary.
    #[error("checkpoint parent crosses the checkpoint tenant boundary")]
    ParentTenantMismatch,

    /// The predecessor named another run.
    #[error("checkpoint parent does not belong to the checkpoint run")]
    ParentRunMismatch,

    /// The successor reused its predecessor's immutable identity.
    #[error("checkpoint successor must use a new checkpoint identifier")]
    ReusedCheckpointId,

    /// The predecessor had no representable successor.
    #[error("checkpoint superstep overflowed")]
    SuperstepOverflow,

    /// The supplied position was not the predecessor's exact successor.
    #[error("checkpoint superstep is {actual}; expected {expected}")]
    NonContiguousSuperstep {
        /// Exact required successor.
        expected: Superstep,
        /// Rejected position.
        actual: Superstep,
    },

    /// The graph definition differed from its predecessor.
    #[error("checkpoint graph reference differs from its parent")]
    ParentGraphMismatch,

    /// Integrity material could not be canonicalized.
    #[error("checkpoint intent integrity calculation failed: {source}")]
    Integrity {
        /// Underlying integrity failure.
        #[source]
        source: CheckpointIntegrityError,
    },

    /// The persisted intent checksum did not match caller-controlled fields.
    #[error("checkpoint intent digest does not match its fields")]
    IntentDigestMismatch,
}

impl CheckpointWriteError {
    const fn integrity(source: CheckpointIntegrityError) -> Self {
        Self::Integrity { source }
    }
}

/// One committed, self-validating graph checkpoint.
///
/// The full checksum binds the validated write intent to the exact committed
/// journal head. Restoring this value verifies state, intent, predecessor, and
/// checkpoint integrity before any graph code may observe it.
#[allow(clippy::struct_field_names)]
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Checkpoint {
    tenant_id: TenantId,
    run_id: RunId,
    checkpoint_id: CheckpointId,
    superstep: Superstep,
    graph: GraphReference,
    state: CheckpointState,
    ready_nodes: ReadyNodes,
    #[serde(skip_serializing_if = "Option::is_none")]
    parent: Option<CheckpointHead>,
    intent_digest: Digest,
    journal_head: JournalHead,
    digest: Digest,
}

impl Checkpoint {
    /// Materializes a checkpoint after its anchoring event has committed.
    ///
    /// Stores must call this using the journal head produced inside the same
    /// transaction that inserts the checkpoint and advances the run head.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointError`] for a crossed journal scope, a journal head
    /// that did not advance beyond the parent, clock regression, or integrity
    /// serialization failure.
    pub fn commit(
        write: CheckpointWrite,
        journal_head: JournalHead,
    ) -> Result<Self, CheckpointError> {
        Self::build(write, journal_head, None)
    }

    /// Restores a full checkpoint and verifies its persisted checksum.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointError`] for any structural, chronology, scope, or
    /// checksum mismatch.
    pub fn restore(
        write: CheckpointWrite,
        journal_head: JournalHead,
        digest: Digest,
    ) -> Result<Self, CheckpointError> {
        Self::build(write, journal_head, Some(digest))
    }

    fn build(
        write: CheckpointWrite,
        journal_head: JournalHead,
        supplied_digest: Option<Digest>,
    ) -> Result<Self, CheckpointError> {
        validate_journal_scope(&write.tenant_id, write.run_id, &journal_head)
            .map_err(CheckpointError::from_scope)?;
        if let Some(parent) = &write.parent {
            if journal_head.sequence() <= parent.journal_head.sequence() {
                return Err(CheckpointError::JournalDidNotAdvance {
                    previous: parent.journal_head.sequence(),
                    actual: journal_head.sequence(),
                });
            }
            if journal_head.recorded_at() < parent.journal_head.recorded_at() {
                return Err(CheckpointError::ClockRegression {
                    previous: parent.journal_head.recorded_at(),
                    actual: journal_head.recorded_at(),
                });
            }
        }

        let digest = compute_checkpoint_digest(&CheckpointDigestWire {
            intent_digest: write.intent_digest,
            journal_head: &journal_head,
        })
        .map_err(CheckpointError::integrity)?;
        if supplied_digest.is_some_and(|supplied| supplied != digest) {
            return Err(CheckpointError::DigestMismatch);
        }

        Ok(Self {
            tenant_id: write.tenant_id,
            run_id: write.run_id,
            checkpoint_id: write.checkpoint_id,
            superstep: write.superstep,
            graph: write.graph,
            state: write.state,
            ready_nodes: write.ready_nodes,
            parent: write.parent,
            intent_digest: write.intent_digest,
            journal_head,
            digest,
        })
    }

    /// Returns the tenant boundary.
    #[must_use]
    pub const fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    /// Returns the durable run identity.
    #[must_use]
    pub const fn run_id(&self) -> RunId {
        self.run_id
    }

    /// Returns the immutable checkpoint identity.
    #[must_use]
    pub const fn checkpoint_id(&self) -> CheckpointId {
        self.checkpoint_id
    }

    /// Returns the committed barrier position.
    #[must_use]
    pub const fn superstep(&self) -> Superstep {
        self.superstep
    }

    /// Returns the exact graph definition reference.
    #[must_use]
    pub const fn graph(&self) -> &GraphReference {
        &self.graph
    }

    /// Returns the immutable graph state.
    #[must_use]
    pub const fn state(&self) -> &CheckpointState {
        &self.state
    }

    /// Returns the sorted next-superstep ready set.
    #[must_use]
    pub const fn ready_nodes(&self) -> &ReadyNodes {
        &self.ready_nodes
    }

    /// Returns the exact predecessor, absent only at superstep zero.
    #[must_use]
    pub const fn parent(&self) -> Option<&CheckpointHead> {
        self.parent.as_ref()
    }

    /// Returns the stable write-intent checksum.
    #[must_use]
    pub const fn intent_digest(&self) -> Digest {
        self.intent_digest
    }

    /// Returns the exact journal prefix included in this checkpoint.
    #[must_use]
    pub const fn journal_head(&self) -> &JournalHead {
        &self.journal_head
    }

    /// Returns the complete committed checkpoint checksum.
    #[must_use]
    pub const fn digest(&self) -> Digest {
        self.digest
    }

    /// Returns a compact predecessor/head value for optimistic comparison.
    #[must_use]
    pub fn head(&self) -> CheckpointHead {
        CheckpointHead {
            tenant_id: self.tenant_id.clone(),
            run_id: self.run_id,
            checkpoint_id: self.checkpoint_id,
            superstep: self.superstep,
            graph: self.graph.clone(),
            journal_head: self.journal_head.clone(),
            digest: self.digest,
        }
    }

    /// Returns `true` when this checkpoint materialized the exact write intent.
    #[must_use]
    pub fn matches_write(&self, write: &CheckpointWrite) -> bool {
        self.tenant_id == write.tenant_id
            && self.run_id == write.run_id
            && self.checkpoint_id == write.checkpoint_id
            && self.superstep == write.superstep
            && self.graph == write.graph
            && self.state == write.state
            && self.ready_nodes == write.ready_nodes
            && self.parent == write.parent
            && self.intent_digest == write.intent_digest
    }

    /// Reconstructs the verified write intent that produced this checkpoint.
    #[must_use]
    pub fn write_intent(&self) -> CheckpointWrite {
        CheckpointWrite {
            tenant_id: self.tenant_id.clone(),
            run_id: self.run_id,
            checkpoint_id: self.checkpoint_id,
            superstep: self.superstep,
            graph: self.graph.clone(),
            state: self.state.clone(),
            ready_nodes: self.ready_nodes.clone(),
            parent: self.parent.clone(),
            intent_digest: self.intent_digest,
        }
    }
}

impl fmt::Debug for Checkpoint {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Checkpoint")
            .field("tenant_id", &self.tenant_id)
            .field("run_id", &self.run_id)
            .field("checkpoint_id", &self.checkpoint_id)
            .field("superstep", &self.superstep)
            .field("graph", &self.graph)
            .field("state", &self.state)
            .field("ready_nodes", &self.ready_nodes)
            .field("parent", &self.parent)
            .field("intent_digest", &self.intent_digest)
            .field("journal_head", &self.journal_head)
            .field("digest", &self.digest)
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for Checkpoint {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            tenant_id: TenantId,
            run_id: RunId,
            checkpoint_id: CheckpointId,
            superstep: Superstep,
            graph: GraphReference,
            state: CheckpointState,
            ready_nodes: ReadyNodes,
            parent: Option<CheckpointHead>,
            intent_digest: Digest,
            journal_head: JournalHead,
            digest: Digest,
        }

        let wire = Wire::deserialize(deserializer)?;
        let write = CheckpointWrite::build(
            wire.tenant_id,
            wire.run_id,
            wire.checkpoint_id,
            wire.superstep,
            wire.graph,
            wire.state,
            wire.ready_nodes,
            wire.parent,
            Some(wire.intent_digest),
        )
        .map_err(de::Error::custom)?;
        Self::restore(write, wire.journal_head, wire.digest).map_err(de::Error::custom)
    }
}

/// Invalid or corrupted committed checkpoint.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointError {
    /// The journal head crossed a tenant boundary.
    #[error("checkpoint journal head crosses the checkpoint tenant boundary")]
    JournalTenantMismatch,

    /// The journal head named another run.
    #[error("checkpoint journal head does not belong to the checkpoint run")]
    JournalRunMismatch,

    /// The successor checkpoint did not include any newer journal fact.
    #[error("checkpoint journal sequence is {actual}; it must be greater than {previous}")]
    JournalDidNotAdvance {
        /// Parent checkpoint's journal sequence.
        previous: JournalSequence,
        /// Rejected successor journal sequence.
        actual: JournalSequence,
    },

    /// Durable journal time regressed across checkpoints.
    #[error("checkpoint journal time {actual} precedes parent time {previous}")]
    ClockRegression {
        /// Parent journal observation.
        previous: Timestamp,
        /// Rejected successor observation.
        actual: Timestamp,
    },

    /// Integrity material could not be canonicalized.
    #[error("checkpoint integrity calculation failed: {source}")]
    Integrity {
        /// Underlying integrity failure.
        #[source]
        source: CheckpointIntegrityError,
    },

    /// The persisted complete checksum did not match checkpoint fields.
    #[error("checkpoint digest does not match its intent and journal head")]
    DigestMismatch,
}

impl CheckpointError {
    const fn from_scope(error: JournalScopeError) -> Self {
        match error {
            JournalScopeError::Tenant => Self::JournalTenantMismatch,
            JournalScopeError::Run => Self::JournalRunMismatch,
        }
    }

    const fn integrity(source: CheckpointIntegrityError) -> Self {
        Self::Integrity { source }
    }
}

/// Streaming verifier for a checkpoint lineage read from newest to oldest.
///
/// The verifier starts from an exact trusted tip and accepts only the full
/// checkpoint named by that head, followed by its exact parent, until the
/// superstep-zero root is reached. It buffers one compact head rather than the
/// checkpoint state history, so stores can validate arbitrarily long lineages
/// through bounded reverse pages. Rejection leaves the verifier unchanged.
#[derive(Debug)]
pub struct CheckpointLineageVerifier {
    expected: Option<CheckpointHead>,
}

impl CheckpointLineageVerifier {
    /// Constructs a reverse-lineage verifier from an exact checkpoint tip.
    #[must_use]
    pub const fn from_tip(tip: CheckpointHead) -> Self {
        Self {
            expected: Some(tip),
        }
    }

    /// Returns the exact checkpoint head required next, or `None` at the root.
    #[must_use]
    pub const fn expected(&self) -> Option<&CheckpointHead> {
        self.expected.as_ref()
    }

    /// Returns whether the complete lineage through superstep zero was accepted.
    #[must_use]
    pub const fn is_complete(&self) -> bool {
        self.expected.is_none()
    }

    /// Verifies and accepts the next checkpoint in newest-to-oldest order.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointLineageError::HeadMismatch`] when `checkpoint` is
    /// not the exact full value named by the expected head, or
    /// [`CheckpointLineageError::AlreadyComplete`] after the root was accepted.
    pub fn verify_next(&mut self, checkpoint: &Checkpoint) -> Result<(), CheckpointLineageError> {
        let Some(expected) = &self.expected else {
            return Err(CheckpointLineageError::AlreadyComplete);
        };
        if checkpoint.head() != *expected {
            return Err(CheckpointLineageError::HeadMismatch);
        }
        self.expected.clone_from(&checkpoint.parent);
        Ok(())
    }
}

/// Rejected checkpoint in reverse lineage verification.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointLineageError {
    /// A checkpoint did not materialize the exact expected compact head.
    #[error("checkpoint does not match the expected lineage head")]
    HeadMismatch,

    /// Another checkpoint appeared after the superstep-zero root.
    #[error("checkpoint lineage is already complete")]
    AlreadyComplete,
}

/// Failure to produce a domain-separated checkpoint checksum.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointIntegrityError {
    /// State JSON was outside the RFC 8785 interoperable domain.
    #[error("checkpoint state canonicalization failed: {source}")]
    StateCanonicalization {
        /// Exact canonical JSON failure.
        #[source]
        source: CanonicalJsonError,
    },

    /// Canonical serialization of a closed typed checksum preimage failed.
    #[error("checkpoint checksum preimage canonical serialization failed")]
    CanonicalSerialization,
}

#[derive(Serialize)]
struct StateDigestWire<'a> {
    schema: &'a SchemaReference,
    data_digest: Digest,
}

#[derive(Serialize)]
struct CheckpointIntentDigestWire<'a> {
    tenant_id: &'a TenantId,
    run_id: RunId,
    checkpoint_id: CheckpointId,
    superstep: Superstep,
    graph: &'a GraphReference,
    state_digest: Digest,
    ready_nodes: &'a ReadyNodes,
    parent: Option<&'a CheckpointHead>,
}

#[derive(Serialize)]
struct CheckpointDigestWire<'a> {
    intent_digest: Digest,
    journal_head: &'a JournalHead,
}

fn compute_state_digest(
    schema: &SchemaReference,
    data: &BoundedJson,
) -> Result<Digest, CheckpointIntegrityError> {
    let canonical = CanonicalJson::new(data)
        .map_err(|source| CheckpointIntegrityError::StateCanonicalization { source })?;
    domain_separated_digest(
        STATE_DIGEST_DOMAIN,
        &StateDigestWire {
            schema,
            data_digest: canonical.digest(),
        },
    )
}

fn compute_intent_digest(
    value: &CheckpointIntentDigestWire<'_>,
) -> Result<Digest, CheckpointIntegrityError> {
    domain_separated_digest(INTENT_DIGEST_DOMAIN, value)
}

fn compute_checkpoint_digest(
    value: &CheckpointDigestWire<'_>,
) -> Result<Digest, CheckpointIntegrityError> {
    domain_separated_digest(CHECKPOINT_DIGEST_DOMAIN, value)
}

fn domain_separated_digest<T: Serialize>(
    domain: &[u8],
    value: &T,
) -> Result<Digest, CheckpointIntegrityError> {
    let canonical = serde_json_canonicalizer::to_vec(value)
        .map_err(|_| CheckpointIntegrityError::CanonicalSerialization)?;
    let mut preimage = Vec::with_capacity(domain.len() + canonical.len());
    preimage.extend_from_slice(domain);
    preimage.extend_from_slice(&canonical);
    Ok(Digest::sha256(preimage))
}

#[allow(clippy::too_many_arguments)]
fn validate_write_shape(
    tenant_id: &TenantId,
    run_id: RunId,
    checkpoint_id: CheckpointId,
    superstep: Superstep,
    graph: &GraphReference,
    state: &CheckpointState,
    parent: Option<&CheckpointHead>,
) -> Result<(), CheckpointWriteError> {
    if state.schema != graph.state_schema {
        return Err(CheckpointWriteError::StateSchemaMismatch);
    }

    match (superstep, parent) {
        (Superstep::INITIAL, None) => Ok(()),
        (Superstep::INITIAL, Some(_)) => Err(CheckpointWriteError::InitialHasParent),
        (_, None) => Err(CheckpointWriteError::SuccessorMissingParent),
        (actual, Some(parent)) => {
            if parent.tenant_id != *tenant_id {
                return Err(CheckpointWriteError::ParentTenantMismatch);
            }
            if parent.run_id != run_id {
                return Err(CheckpointWriteError::ParentRunMismatch);
            }
            if parent.checkpoint_id == checkpoint_id {
                return Err(CheckpointWriteError::ReusedCheckpointId);
            }
            let expected = parent
                .superstep
                .checked_next()
                .ok_or(CheckpointWriteError::SuperstepOverflow)?;
            if actual != expected {
                return Err(CheckpointWriteError::NonContiguousSuperstep { expected, actual });
            }
            if parent.graph != *graph {
                return Err(CheckpointWriteError::ParentGraphMismatch);
            }
            Ok(())
        }
    }
}

#[derive(Clone, Copy)]
enum JournalScopeError {
    Tenant,
    Run,
}

fn validate_journal_scope(
    tenant_id: &TenantId,
    run_id: RunId,
    journal_head: &JournalHead,
) -> Result<(), JournalScopeError> {
    if journal_head.tenant_id() != tenant_id {
        return Err(JournalScopeError::Tenant);
    }
    if journal_head.run_id() != run_id {
        return Err(JournalScopeError::Run);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        CapabilityName, CapabilityReference, EventId, IssuerId, PrincipalIdentity, SchemaId,
        SubjectId, Version,
    };
    use proptest::prelude::*;
    use serde_json::{Value, from_value, json, to_value};

    fn tenant() -> TenantId {
        TenantId::try_from("tenant-production").unwrap()
    }

    fn other_tenant() -> TenantId {
        TenantId::try_from("tenant-other").unwrap()
    }

    fn run() -> RunId {
        "01912345-6789-7abc-8def-0123456789ae".parse().unwrap()
    }

    fn other_run() -> RunId {
        "01912345-6789-7abc-8def-0123456789af".parse().unwrap()
    }

    fn checkpoint_id(suffix: &str) -> CheckpointId {
        format!("01912345-6789-7abc-8def-0123456789{suffix}")
            .parse()
            .unwrap()
    }

    fn state_schema(label: &[u8]) -> SchemaReference {
        SchemaReference::new(
            "https://stateknot.github.io/schema/test/workflow-state/1.0.0"
                .parse::<SchemaId>()
                .unwrap(),
            Version::new(1, 0, 0),
            Digest::sha256(label),
        )
    }

    fn graph_with_schema(schema: SchemaReference) -> GraphReference {
        let owner = PrincipalIdentity::new(
            "https://issuer.example.com/stateknot"
                .parse::<IssuerId>()
                .unwrap(),
            "workflow-registry".parse::<SubjectId>().unwrap(),
        );
        let capability = CapabilityReference::new(
            "orders.fulfillment".parse::<CapabilityName>().unwrap(),
            Version::new(1, 4, 0),
        );
        GraphReference::new(
            CapabilityIdentity::new(owner, capability),
            Digest::sha256(b"compiled-order-graph-v1.4.0"),
            schema,
        )
    }

    fn graph() -> GraphReference {
        graph_with_schema(state_schema(b"order-state-schema-v1"))
    }

    fn state(value: Value) -> CheckpointState {
        CheckpointState::new(
            graph().state_schema().clone(),
            BoundedJson::try_from_value(value).unwrap(),
        )
        .unwrap()
    }

    fn ready(values: &[&str]) -> ReadyNodes {
        ReadyNodes::try_new(values.iter().map(|value| value.parse::<NodeId>().unwrap())).unwrap()
    }

    fn at(offset_micros: i64) -> Timestamp {
        let base = "2030-01-01T00:00:00.000000Z".parse::<Timestamp>().unwrap();
        Timestamp::from_unix_micros(base.unix_micros() + offset_micros).unwrap()
    }

    fn journal(sequence: u64, observed_at: Timestamp) -> JournalHead {
        let event_id = format!("01912345-6789-7abc-8def-0123456789{sequence:02x}")
            .parse::<EventId>()
            .unwrap();
        JournalHead::new(
            tenant(),
            run(),
            JournalSequence::new(sequence).unwrap(),
            event_id,
            observed_at,
            Digest::sha256(format!("journal-event-{sequence}")),
        )
    }

    fn initial_checkpoint() -> Checkpoint {
        let write = CheckpointWrite::initial(
            tenant(),
            run(),
            checkpoint_id("d1"),
            graph(),
            state(json!({"order_id": "order-42", "status": "pending"})),
            ready(&["authorize", "reserve-stock"]),
        )
        .unwrap();
        Checkpoint::commit(write, journal(1, at(1))).unwrap()
    }

    fn successor_checkpoint() -> Checkpoint {
        let parent = initial_checkpoint();
        let write = CheckpointWrite::successor(
            checkpoint_id("d2"),
            &parent,
            state(json!({"order_id": "order-42", "status": "reserved"})),
            ready(&["capture-payment"]),
        )
        .unwrap();
        Checkpoint::commit(write, journal(3, at(3))).unwrap()
    }

    #[test]
    fn supersteps_use_canonical_full_width_decimal_text() {
        for (text, expected) in [
            ("0", Superstep::INITIAL),
            ("1", Superstep::new(1).unwrap()),
            ("9223372036854775807", Superstep::MAX),
        ] {
            let decoded = text.parse::<Superstep>().unwrap();
            assert_eq!(decoded, expected);
            assert_eq!(decoded.to_string(), text);
            assert_eq!(to_value(decoded).unwrap(), Value::from(text));
        }

        for invalid in ["", "00", "01", "-1", "1.0", "9223372036854775808"] {
            assert!(invalid.parse::<Superstep>().is_err(), "accepted {invalid}");
        }
        assert!(from_value::<Superstep>(json!(0)).is_err());
        assert_eq!(Superstep::MAX.checked_next(), None);
    }

    #[test]
    fn node_ids_and_ready_sets_are_bounded_unique_and_sorted() {
        for value in ["a", "Authorize", "order.capture-v2", "node_42"] {
            let node = NodeId::try_from(value).unwrap();
            assert_eq!(node.as_str(), value);
            assert_eq!(node.to_string(), value);
        }
        for invalid in ["", ".", "..", "_private", "node/name", "node name", "节点"] {
            assert!(NodeId::try_from(invalid).is_err(), "accepted {invalid:?}");
        }
        assert!(NodeId::new("a".repeat(NodeId::MAX_LEN)).is_ok());
        assert!(NodeId::new("a".repeat(NodeId::MAX_LEN + 1)).is_err());

        let set = ready(&["z-last", "a-first", "middle"]);
        assert_eq!(set.len(), 3);
        assert!(set.contains(&NodeId::try_from("middle").unwrap()));
        assert_eq!(
            to_value(&set).unwrap(),
            json!(["a-first", "middle", "z-last"])
        );
        assert!(from_value::<ReadyNodes>(json!(["same", "same"])).is_err());

        let too_many =
            (0..=ReadyNodes::MAX_LEN).map(|index| NodeId::new(format!("node-{index:04}")).unwrap());
        assert_eq!(
            ReadyNodes::try_new(too_many),
            Err(ReadyNodesError::TooMany {
                maximum: ReadyNodes::MAX_LEN,
                actual: ReadyNodes::MAX_LEN + 1,
            })
        );
    }

    #[test]
    fn state_digest_binds_schema_and_canonical_json() {
        let schema = state_schema(b"schema-a");
        let first = CheckpointState::new(
            schema.clone(),
            BoundedJson::from_str(r#"{"z":0,"a":[true,{"name":"é"}]}"#).unwrap(),
        )
        .unwrap();
        let equivalent = CheckpointState::new(
            schema.clone(),
            BoundedJson::from_str(" { \"a\" : [ true, { \"name\" : \"é\" } ], \"z\" : -0.0 }")
                .unwrap(),
        )
        .unwrap();
        assert_eq!(first.digest(), equivalent.digest());

        let different_schema =
            CheckpointState::new(state_schema(b"schema-b"), equivalent.data().clone()).unwrap();
        assert_ne!(first.digest(), different_schema.digest());

        let mut tampered = to_value(&first).unwrap();
        tampered["data"]["z"] = json!(1);
        assert!(from_value::<CheckpointState>(tampered).is_err());

        let unsafe_integer = BoundedJson::from_str("9007199254740992").unwrap();
        assert!(matches!(
            CheckpointState::new(schema, unsafe_integer),
            Err(CheckpointStateError::Integrity { .. })
        ));
    }

    #[test]
    fn checkpoint_state_deserialization_uses_the_explicit_two_mib_ceiling() {
        let large_data = BoundedJson::from_str_with_limits(
            &format!("\"{}\"", "x".repeat(JsonLimits::DEFAULT.max_bytes() + 1)),
            JsonLimits::MAXIMUM,
        )
        .unwrap();
        let state = CheckpointState::new(state_schema(b"large-state"), large_data).unwrap();
        let encoded = serde_json::to_vec(&state).unwrap();
        let decoded = serde_json::from_slice::<CheckpointState>(&encoded).unwrap();
        assert_eq!(decoded, state);

        let oversized = json!({
            "schema": state_schema(b"oversized"),
            "data": "x".repeat(JsonLimits::MAXIMUM.max_string_bytes() + 1),
            "digest": Digest::sha256(b"irrelevant"),
        });
        assert!(from_value::<CheckpointState>(oversized).is_err());
    }

    #[test]
    fn initial_checkpoint_round_trips_and_redacts_state() {
        let checkpoint = initial_checkpoint();
        assert_eq!(checkpoint.superstep(), Superstep::INITIAL);
        assert!(checkpoint.parent().is_none());
        assert_eq!(checkpoint.journal_head().sequence(), JournalSequence::FIRST);
        assert_eq!(checkpoint.graph(), &graph());
        assert_eq!(
            checkpoint.ready_nodes(),
            &ready(&["authorize", "reserve-stock"])
        );
        assert!(checkpoint.matches_write(&checkpoint.write_intent()));

        let encoded = to_value(&checkpoint).unwrap();
        assert_eq!(from_value::<Checkpoint>(encoded).unwrap(), checkpoint);
        assert_eq!(
            Checkpoint::restore(
                checkpoint.write_intent(),
                checkpoint.journal_head().clone(),
                checkpoint.digest(),
            )
            .unwrap(),
            checkpoint
        );

        let debug = format!("{checkpoint:?}");
        assert!(debug.contains("CheckpointState"));
        assert!(!debug.contains("order-42"));
        assert!(!debug.contains("pending"));
    }

    #[test]
    fn successor_is_contiguous_and_inherits_scope_and_graph() {
        let parent = initial_checkpoint();
        let write = CheckpointWrite::successor(
            checkpoint_id("d2"),
            &parent,
            state(json!({"status": "authorized"})),
            ready(&["capture-payment"]),
        )
        .unwrap();
        assert_eq!(write.superstep(), Superstep::new(1).unwrap());
        assert_eq!(write.tenant_id(), parent.tenant_id());
        assert_eq!(write.run_id(), parent.run_id());
        assert_eq!(write.graph(), parent.graph());
        assert_eq!(write.parent(), Some(&parent.head()));

        let restored = CheckpointWrite::restore(
            write.tenant_id().clone(),
            write.run_id(),
            write.checkpoint_id(),
            write.superstep(),
            write.graph().clone(),
            write.state().clone(),
            write.ready_nodes().clone(),
            write.parent().cloned(),
            write.intent_digest(),
        )
        .unwrap();
        assert_eq!(restored, write);

        let checkpoint = Checkpoint::commit(write, journal(3, at(3))).unwrap();
        assert_eq!(checkpoint.superstep(), Superstep::new(1).unwrap());
        assert_eq!(checkpoint.parent(), Some(&parent.head()));
        assert_eq!(checkpoint.journal_head().sequence().get(), 3);
    }

    #[test]
    fn write_shape_rejects_schema_scope_position_graph_and_identity_changes() {
        let parent = initial_checkpoint();
        let wrong_schema_state = CheckpointState::new(
            state_schema(b"wrong-schema"),
            BoundedJson::try_from_value(json!({})).unwrap(),
        )
        .unwrap();
        assert_eq!(
            CheckpointWrite::successor(
                checkpoint_id("d2"),
                &parent,
                wrong_schema_state,
                ReadyNodes::empty(),
            ),
            Err(CheckpointWriteError::StateSchemaMismatch)
        );
        assert_eq!(
            CheckpointWrite::successor(
                parent.checkpoint_id(),
                &parent,
                state(json!({})),
                ReadyNodes::empty(),
            ),
            Err(CheckpointWriteError::ReusedCheckpointId)
        );

        let valid = CheckpointWrite::successor(
            checkpoint_id("d2"),
            &parent,
            state(json!({})),
            ReadyNodes::empty(),
        )
        .unwrap();
        let mut wire = to_value(&valid).unwrap();

        wire["superstep"] = json!("3");
        assert!(from_value::<CheckpointWrite>(wire.clone()).is_err());
        wire["superstep"] = json!("1");

        wire["tenant_id"] = json!(other_tenant());
        assert!(from_value::<CheckpointWrite>(wire.clone()).is_err());
        wire["tenant_id"] = json!(tenant());

        wire["run_id"] = json!(other_run());
        assert!(from_value::<CheckpointWrite>(wire.clone()).is_err());
        wire["run_id"] = json!(run());

        wire["parent"]["graph"]["definition_digest"] = json!(Digest::sha256(b"other-graph"));
        assert!(from_value::<CheckpointWrite>(wire).is_err());
    }

    #[test]
    fn committed_checkpoint_rejects_nonadvancing_or_regressing_journal_heads() {
        let parent = initial_checkpoint();
        let make_write = || {
            CheckpointWrite::successor(
                checkpoint_id("d2"),
                &parent,
                state(json!({"status": "next"})),
                ReadyNodes::empty(),
            )
            .unwrap()
        };

        assert_eq!(
            Checkpoint::commit(make_write(), journal(1, at(2))),
            Err(CheckpointError::JournalDidNotAdvance {
                previous: JournalSequence::FIRST,
                actual: JournalSequence::FIRST,
            })
        );
        assert_eq!(
            Checkpoint::commit(make_write(), journal(2, at(0))),
            Err(CheckpointError::ClockRegression {
                previous: at(1),
                actual: at(0),
            })
        );

        let crossed_tenant = JournalHead::new(
            other_tenant(),
            run(),
            JournalSequence::new(2).unwrap(),
            "01912345-6789-7abc-8def-012345678902".parse().unwrap(),
            at(2),
            Digest::sha256(b"other-tenant"),
        );
        assert_eq!(
            Checkpoint::commit(make_write(), crossed_tenant),
            Err(CheckpointError::JournalTenantMismatch)
        );
    }

    #[test]
    fn every_integrity_layer_rejects_tampering() {
        let checkpoint = successor_checkpoint();
        let original = to_value(&checkpoint).unwrap();

        let mut state_data = original.clone();
        state_data["state"]["data"]["status"] = json!("captured");
        assert!(from_value::<Checkpoint>(state_data).is_err());

        let mut state_digest = original.clone();
        state_digest["state"]["digest"] = json!(Digest::sha256(b"wrong-state"));
        assert!(from_value::<Checkpoint>(state_digest).is_err());

        let mut ready_nodes = original.clone();
        ready_nodes["ready_nodes"] = json!(["different-node"]);
        assert!(from_value::<Checkpoint>(ready_nodes).is_err());

        let mut intent_digest = original.clone();
        intent_digest["intent_digest"] = json!(Digest::sha256(b"wrong-intent"));
        assert!(from_value::<Checkpoint>(intent_digest).is_err());

        let mut journal_head = original.clone();
        journal_head["journal_head"]["digest"] = json!(Digest::sha256(b"wrong-journal"));
        assert!(from_value::<Checkpoint>(journal_head).is_err());

        let mut digest = original;
        digest["digest"] = json!(Digest::sha256(b"wrong-checkpoint"));
        assert!(from_value::<Checkpoint>(digest).is_err());
    }

    #[test]
    fn compact_head_enforces_scope_and_public_schemas_are_closed() {
        let checkpoint = initial_checkpoint();
        let head = checkpoint.head();
        assert_eq!(
            from_value::<CheckpointHead>(to_value(&head).unwrap()).unwrap(),
            head
        );

        assert_eq!(
            CheckpointHead::new(
                tenant(),
                other_run(),
                checkpoint.checkpoint_id(),
                checkpoint.superstep(),
                checkpoint.graph().clone(),
                checkpoint.journal_head().clone(),
                checkpoint.digest(),
            ),
            Err(CheckpointHeadError::JournalRunMismatch)
        );

        for schema in [
            to_value(schemars::schema_for!(GraphReference)).unwrap(),
            to_value(schemars::schema_for!(CheckpointState)).unwrap(),
            to_value(schemars::schema_for!(CheckpointHead)).unwrap(),
            to_value(schemars::schema_for!(CheckpointWrite)).unwrap(),
            to_value(schemars::schema_for!(Checkpoint)).unwrap(),
        ] {
            assert_eq!(schema["additionalProperties"], false);
        }
        let ready_schema = to_value(schemars::schema_for!(ReadyNodes)).unwrap();
        assert_eq!(ready_schema["maxItems"], ReadyNodes::MAX_LEN);
        assert_eq!(ready_schema["uniqueItems"], true);
    }

    #[test]
    fn reverse_lineage_verifier_is_streaming_exact_and_transactional() {
        let root = initial_checkpoint();
        let tip = successor_checkpoint();
        let mut verifier = CheckpointLineageVerifier::from_tip(tip.head());

        assert_eq!(verifier.expected(), Some(&tip.head()));
        assert_eq!(
            verifier.verify_next(&root),
            Err(CheckpointLineageError::HeadMismatch)
        );
        assert_eq!(
            verifier.expected(),
            Some(&tip.head()),
            "rejection must not advance the verifier"
        );

        verifier.verify_next(&tip).unwrap();
        assert_eq!(verifier.expected(), Some(&root.head()));
        verifier.verify_next(&root).unwrap();
        assert!(verifier.is_complete());
        assert_eq!(verifier.expected(), None);
        assert_eq!(
            verifier.verify_next(&root),
            Err(CheckpointLineageError::AlreadyComplete)
        );
    }

    proptest! {
        #[test]
        fn every_supported_superstep_round_trips(value in 0_u64..=MAX_DATABASE_ORDINAL) {
            let step = Superstep::new(value).unwrap();
            prop_assert_eq!(step.to_string().parse::<Superstep>().unwrap(), step);
            prop_assert_eq!(serde_json::from_value::<Superstep>(to_value(step).unwrap()).unwrap(), step);
        }
    }
}