rusty-agent-runtime 0.10.0

The durable agent runtime built in Rust — core graph engine: typed channels with reducers, Pregel/BSP super-step execution, checkpoints, interrupts, and streaming.
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
//! Checkpointing: thread-scoped, versioned state snapshots.
//!
//! Checkpoints happen **at super-step boundaries, never mid-node** — so on
//! resume the affected node re-runs from its start (node logic must be
//! idempotent). One primitive, four use cases: durable execution,
//! human-in-the-loop (interrupt → serialize → approve → resume), time travel
//! (load any historical checkpoint, fork alternate paths), and
//! partial-failure recovery.
//!
//! - [`InMemoryCheckpointer`] — RAM-only, lost on restart (dev/test).
//! - [`JsonFileCheckpointer`] — one JSON file per checkpoint under a
//!   directory, pure `serde_json` + `tokio::fs` (durable across restarts).
//!
//! # Delta checkpoints (R0.7 wave 4)
//!
//! Durable backends may store a checkpoint as a **channel-level delta**
//! against the checkpoint before it ([`Checkpoint::base`]) instead of a full
//! snapshot: only the channels whose values changed are written, which is
//! what stops a 1000-step run from writing 1000 full copies of a largely
//! unchanged state. The encoding is **checkpointer-internal**:
//!
//! - `put` always receives a full snapshot; the backend decides delta vs
//!   full ([`encode_delta`]), bounded by a [`DeltaPolicy`] (chain length +
//!   byte ratio) so resume folds stay O(1)-bounded by construction.
//! - Every read method returns a fully materialized checkpoint: chains are
//!   folded onto their base inside the backend ([`fold_delta`]). Callers
//!   never observe `base`.
//! - Checkpoints written before W4 have no `base` field and deserialize as
//!   full snapshots — exactly what they always were.
//!
//! [`JsonFileCheckpointer`] and the Postgres backend opt in;
//! [`InMemoryCheckpointer`] does not — its `put` is a sub-2 µs move, so a
//! delta would buy nothing.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::error::{Result, RustyError};
use crate::record::{CheckpointHeader, JournalRef};
use crate::state::State;

/// A versioned snapshot of one thread's state at a super-step boundary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Checkpoint {
    /// Unique checkpoint id (UUID v4; also serves as a fork handle for
    /// time travel).
    pub id: String,

    /// The thread (session) this checkpoint belongs to. Threads cannot see
    /// each other's state.
    pub thread_id: String,

    /// Zero-based super-step index at whose boundary this snapshot was taken.
    pub step: usize,

    /// The full channel state at the boundary.
    ///
    /// In a **delta checkpoint** ([`Checkpoint::base`] set — a storage
    /// detail only opting-in backends produce and no read method ever
    /// returns) this holds just the channels whose values changed since the
    /// base, as full per-channel values. Channels are never deleted from a
    /// state, so overlaying the delta onto the materialized base restores
    /// the full state ([`fold_delta`]).
    pub state: State,

    /// The node set scheduled to run in the *next* super-step. Restored on
    /// resume so execution continues exactly where it suspended.
    pub next_nodes: Vec<String>,

    /// Wall-clock creation time (UTC).
    pub created_at: DateTime<Utc>,

    /// The checkpoint this one diffs against (R0.7 wave 4 delta
    /// checkpoints). `None` is a full snapshot — every checkpoint written
    /// before W4, and every checkpoint a read method returns: folding is
    /// checkpointer-internal, so `base` is only ever observed inside a
    /// backend's storage (checkpoint files, database rows). Additive:
    /// absent from the wire when unset, so pre-W4 checkpoints deserialize
    /// unchanged.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,

    /// Flight Recorder provenance (R0.5): checkpoint format version, graph
    /// version/content hash, active policy version, and the run's logical
    /// clock value at creation. See [`CheckpointHeader`] for the semantics.
    ///
    /// `#[serde(default)]` keeps checkpoints written before R0.5 (which have
    /// no header field) deserializable: they load with
    /// [`CheckpointHeader::default`] — current format version, unversioned
    /// graph, static policy.
    #[serde(default)]
    pub header: CheckpointHeader,

    /// The journal head at this boundary (`None` pre-R0.5), binding this
    /// state snapshot to the run evidence that produced it.
    #[serde(default)]
    pub journal_ref: Option<JournalRef>,
}

impl Checkpoint {
    /// A new checkpoint with a fresh UUID v4 id and current timestamp.
    ///
    /// Convenience constructor used by tests and pre-R0.5 call paths: the
    /// header falls back to [`CheckpointHeader::default`] and no journal
    /// reference is stamped. The executor mints checkpoints field-by-field
    /// instead, sourcing id/timestamp from the run's determinism seams and
    /// stamping the real header and journal head.
    pub fn new(
        thread_id: impl Into<String>,
        step: usize,
        state: State,
        next_nodes: Vec<String>,
    ) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            thread_id: thread_id.into(),
            step,
            state,
            next_nodes,
            created_at: Utc::now(),
            header: CheckpointHeader::default(),
            journal_ref: None,
            base: None,
        }
    }
}

/// Delta-checkpoint chain policy (R0.7 wave 4): when an opting-in backend
/// rewrites a boundary as a full snapshot instead of another chain link.
///
/// Both bounds exist to keep resume reads bounded. An unbounded delta chain
/// makes every resume an O(chain) fold — the event-sourcing failure mode
/// checkpoints exist to avoid (checkpoints, not journals, are the resume
/// authority). The bound is enforced at write time and is a *policy*, never
/// a correctness condition: readers fold whatever chain they find.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DeltaPolicy {
    /// Maximum number of consecutive delta links above a full snapshot. A
    /// put that would exceed it writes a full snapshot instead, so resume
    /// folds at most `max_chain_len` channel-sets. Default: 32.
    pub max_chain_len: usize,

    /// Full-snapshot threshold as a fraction of the full state's serialized
    /// size: when a delta's serialized size reaches this share of the state
    /// it would replace, the delta has stopped paying for itself — it costs
    /// nearly a full write *and* extends the chain — so a full snapshot is
    /// written instead. Default: 0.8.
    pub max_byte_ratio: f64,
}

impl Default for DeltaPolicy {
    fn default() -> Self {
        Self {
            max_chain_len: 32,
            max_byte_ratio: 0.8,
        }
    }
}

impl DeltaPolicy {
    /// Full snapshots at every boundary: the pre-W4 write behavior. Used by
    /// backends that do not opt in, and as the measurement baseline for the
    /// wave-4 benchmarks.
    pub fn full_only() -> Self {
        Self {
            max_chain_len: 0,
            max_byte_ratio: 0.0,
        }
    }

    /// Whether delta encoding is on at all (`full_only` disables it).
    fn deltas_enabled(&self) -> bool {
        self.max_chain_len > 0
    }
}

/// The materialized head of a thread's checkpoint chain: what an opting-in
/// backend diffs the next `put` against.
#[derive(Debug, Clone)]
pub struct DeltaHead {
    /// The head checkpoint, fully materialized (never itself a delta).
    pub checkpoint: Checkpoint,

    /// Consecutive delta links below the head (0 = the head is itself a
    /// full snapshot). Backends track this at write time to enforce
    /// [`DeltaPolicy::max_chain_len`] without re-walking the chain per put.
    pub chain_len: usize,
}

/// What [`encode_delta`] decided to persist for one `put`, plus the chain
/// length the stored checkpoint leaves behind.
#[derive(Debug, Clone)]
pub struct DeltaEncoding {
    /// The checkpoint to store: either the put's checkpoint unchanged
    /// (`base == None`, a full snapshot), or a channel-level delta whose
    /// `base` names the chain head's id and whose `state` carries only the
    /// channels that changed since the head.
    pub checkpoint: Checkpoint,

    /// Consecutive delta links below this checkpoint once stored (0 for a
    /// full snapshot).
    pub chain_len: usize,
}

impl DeltaEncoding {
    /// Store the checkpoint as a full snapshot (chain resets).
    pub(crate) fn full(checkpoint: &Checkpoint) -> Self {
        Self {
            checkpoint: Checkpoint {
                base: None,
                ..checkpoint.clone()
            },
            chain_len: 0,
        }
    }
}

/// The serialized size of a state — the byte measure
/// [`DeltaPolicy::max_byte_ratio`] speaks in. `serde_json::to_vec` of a
/// `Value` tree is infallible in practice (the same argument
/// `record::canonical_json_digest` makes); panicking here would require a
/// state that cannot serialize, which no checkpoint path can carry anyway.
fn state_bytes(state: &State) -> usize {
    serde_json::to_vec(state)
        .expect("a State always serializes")
        .len()
}

/// Decide what an opting-in backend persists for one `put`: a full
/// snapshot, or a channel-level delta against the thread's head.
///
/// This is the write half of the W4 delta-checkpoint contract, shared by
/// every opting-in backend (the JSON-file checkpointer, the Postgres
/// checkpointer, and the server store's checkpoint path) so they all make
/// the *same* decision for the same `(checkpoint, head, policy)` triple.
///
/// Rules, in order:
///
/// 1. Deltas disabled, no head, or the chain is already at
///    [`DeltaPolicy::max_chain_len`] → full snapshot (the chain-length
///    bound: resume folds stay bounded by construction).
/// 2. The changed-channel delta's serialized size reaches
///    [`DeltaPolicy::max_byte_ratio`] of the full state's → full snapshot
///    (the byte-ratio bound: a delta that costs ~full buys nothing).
/// 3. Otherwise a delta: `base` names the head's id, `state` carries only
///    channels whose values changed. Unchanged channels are detected by
///    pointer first (the CoW layer shares them) and by value second, so
///    equal content is never rewritten even across a deserialize.
///
/// `put` always receives full snapshots; a caller-set `base` is never
/// honored — encoding is checkpointer-internal.
pub fn encode_delta(
    checkpoint: &Checkpoint,
    head: Option<&DeltaHead>,
    policy: &DeltaPolicy,
) -> DeltaEncoding {
    let Some(head) = head.filter(|_| policy.deltas_enabled()) else {
        return DeltaEncoding::full(checkpoint);
    };
    if head.chain_len >= policy.max_chain_len {
        return DeltaEncoding::full(checkpoint);
    }
    let delta = State::from_shared_channels(
        checkpoint
            .state
            .channels_changed_since(&head.checkpoint.state),
    );
    let delta_bytes = state_bytes(&delta);
    let full_bytes = state_bytes(&checkpoint.state);
    if (delta_bytes as f64) >= policy.max_byte_ratio * (full_bytes as f64) {
        return DeltaEncoding::full(checkpoint);
    }
    DeltaEncoding {
        checkpoint: Checkpoint {
            base: Some(head.checkpoint.id.clone()),
            state: delta,
            ..checkpoint.clone()
        },
        chain_len: head.chain_len + 1,
    }
}

/// Fold one delta link onto its materialized base: the base's channels
/// overlaid with the delta's changed channels. Sharing is preserved — the
/// materialized state shares unchanged channel values with the base rather
/// than copying them (the same CoW observation the diff ran on).
///
/// The result's `base` is cleared: materialization is checkpointer-internal,
/// so what a read method returns is always a full, self-contained
/// checkpoint.
pub fn fold_delta(base: &Checkpoint, delta: &Checkpoint) -> Checkpoint {
    let mut state = base.state.clone();
    for (channel, value) in delta.state.shared_channels() {
        state.insert_shared(channel.to_owned(), value.clone());
    }
    Checkpoint {
        id: delta.id.clone(),
        thread_id: delta.thread_id.clone(),
        step: delta.step,
        state,
        next_nodes: delta.next_nodes.clone(),
        created_at: delta.created_at,
        header: delta.header.clone(),
        journal_ref: delta.journal_ref.clone(),
        base: None,
    }
}

/// Materialize a raw stored chain — root-first, each link's `base` naming
/// the previous link's id — into the full checkpoint at its tip. Errors on
/// a broken chain (a link whose base is not the previous link, or a root
/// that is itself a delta): that is corruption, not data.
///
/// Public so every opting-in storage path folds chains identically (the
/// server store's checkpoint path included).
pub fn fold_chain(chain: &[Checkpoint]) -> Result<Checkpoint> {
    let Some((root, links)) = chain.split_first() else {
        return Err(RustyError::Checkpoint(
            "cannot fold an empty checkpoint chain".into(),
        ));
    };
    if let Some(base) = &root.base {
        return Err(RustyError::Checkpoint(format!(
            "checkpoint chain root `{}` is itself a delta against `{base}`; \
             the chain is broken",
            root.id
        )));
    }
    let mut tip = root.clone();
    for link in links {
        if link.base.as_deref() != Some(tip.id.as_str()) {
            return Err(RustyError::Checkpoint(format!(
                "checkpoint `{}` claims base `{:?}` but follows `{}`; the chain is broken",
                link.id, link.base, tip.id
            )));
        }
        tip = fold_delta(&tip, link);
    }
    Ok(tip)
}

/// Materialize a whole thread's raw checkpoints, in any order: fold each
/// delta chain onto its base, memoizing materialized links so overlapping
/// chains fold once. Deltas whose base is absent or cyclic (a corrupt or
/// partial store) are skipped with a warning, matching the forgiving read
/// paths the file backend already practices — one corrupt file must not
/// poison a thread's whole history.
pub(crate) fn materialize_all(raw: Vec<Checkpoint>) -> Vec<Checkpoint> {
    let by_id: HashMap<String, Checkpoint> = raw.into_iter().map(|c| (c.id.clone(), c)).collect();
    let mut memo: HashMap<String, Checkpoint> = HashMap::new();
    let mut out = Vec::with_capacity(by_id.len());

    'starts: for start_id in by_id.keys() {
        if let Some(materialized) = memo.get(start_id) {
            out.push(materialized.clone());
            continue;
        }
        // Walk from this checkpoint up its base chain, collecting links
        // until a full root or an already-materialized checkpoint.
        // Iterative, not recursive: a corrupt store could chain deeper than
        // any stack.
        let mut links: Vec<&Checkpoint> = Vec::new();
        let mut visiting: HashSet<&str> = HashSet::new();
        let mut cursor = by_id.get(start_id);
        let mut folded: Option<Checkpoint> = None;
        while let Some(current) = cursor {
            if !visiting.insert(current.id.as_str()) {
                tracing::warn!(
                    checkpoint_id = %start_id,
                    "checkpoint delta chain has a cycle; skipping"
                );
                continue 'starts;
            }
            if let Some(materialized) = memo.get(&current.id) {
                folded = Some(materialized.clone());
                break;
            }
            match &current.base {
                None => {
                    folded = Some(current.clone());
                    break;
                }
                Some(base_id) => {
                    links.push(current);
                    match by_id.get(base_id) {
                        Some(base) => cursor = Some(base),
                        None => {
                            tracing::warn!(
                                checkpoint_id = %current.id,
                                base = %base_id,
                                "checkpoint delta base is missing; skipping chain"
                            );
                            continue 'starts;
                        }
                    }
                }
            }
        }
        // `folded` is always set when the loop exits normally (every path
        // that cannot resolve has already continued `starts`).
        let Some(mut tip) = folded else {
            continue;
        };
        // Fold the collected links root-ward → tip: the last link pushed is
        // the closest to the resolved base.
        for link in links.iter().rev() {
            tip = fold_delta(&tip, link);
            memo.insert(link.id.clone(), tip.clone());
        }
        out.push(tip);
    }
    out
}

/// The checkpointer interface (the LangGraph `BaseCheckpointSaver` analog).
///
/// Implementations must be safe to share across tasks (`Send + Sync`) and
/// are typically held as `Arc<dyn Checkpointer>` by the executor.
#[async_trait]
pub trait Checkpointer: Send + Sync {
    /// Persist a checkpoint. Implementations must not overwrite an existing
    /// checkpoint with the same id (ids are unique by construction).
    ///
    /// The checkpoint handed to `put` is always a **full snapshot**;
    /// backends that opt into delta checkpoints (R0.7 wave 4) decide the
    /// encoding internally ([`encode_delta`]) — a caller-set
    /// [`Checkpoint::base`] is never honored.
    async fn put(&self, checkpoint: Checkpoint) -> Result<()>;

    /// The most recent checkpoint for a thread, or `None` if the thread has
    /// never been checkpointed. Recency is defined by **insertion (put)
    /// order — the last successfully stored checkpoint wins** — not by
    /// super-step number: replay on the same thread legitimately appends
    /// checkpoints whose `step` is at or below the existing head, and a
    /// later resume must continue that newest timeline.
    ///
    /// Backends without an explicit insertion sequence use `created_at` as
    /// the insertion proxy. That is exact as long as checkpoints are minted
    /// fresh ([`Checkpoint::new`]) when stored and forked histories are
    /// copied oldest-first, which [`Checkpointer::fork_thread`] does.
    async fn get_latest(&self, thread_id: &str) -> Result<Option<Checkpoint>>;

    /// All checkpoints for a thread, oldest first (time-travel listing).
    ///
    /// The order is total and identical across backends — ascending
    /// `(step, created_at, id)` — so that [`Checkpointer::fork_thread`]'s
    /// truncation-by-position is deterministic even when replay has appended
    /// several checkpoints sharing the same `step`.
    async fn list(&self, thread_id: &str) -> Result<Vec<Checkpoint>>;

    /// Fetch one specific checkpoint of a thread by id (time-travel handle).
    ///
    /// The default implementation lists the thread and finds the id, which is
    /// correct (if not maximally efficient) for every reasonable backend.
    /// Returns `None` when the thread has no checkpoint with that id.
    async fn get_by_id(&self, thread_id: &str, checkpoint_id: &str) -> Result<Option<Checkpoint>> {
        let all = self.list(thread_id).await?;
        Ok(all.into_iter().find(|c| c.id == checkpoint_id))
    }

    /// Fork a thread's history into a new thread (time travel).
    ///
    /// Copies the source thread's checkpoints — oldest first — into
    /// `dst_thread`, preserving each checkpoint's `id`, `step`, `state`,
    /// `next_nodes`, and `created_at`; only the `thread_id` changes. When
    /// `at_checkpoint_id` is given, only checkpoints up to and including that
    /// checkpoint are copied (fork from a mid-history point); when `None`,
    /// the full history is copied. Returns the number of checkpoints copied.
    ///
    /// The default implementation re-`put`s each selected checkpoint with the
    /// destination thread id. This is correct for every implementation whose
    /// `put` uniqueness scope is per-thread — including both built-in impls
    /// ([`InMemoryCheckpointer`] keys its map by thread, and
    /// [`JsonFileCheckpointer`] stores under `{dir}/{thread_id}/`), so reused
    /// ids cannot collide across threads. An implementation whose `put`
    /// enforces globally unique ids, or whose storage path ignores
    /// `checkpoint.thread_id`, **must override this method** (e.g. a SQL
    /// backend with a global primary key would mint fresh ids or insert with
    /// an explicit thread column).
    ///
    /// Delta-checkpoint backends (R0.7 wave 4) additionally override this to
    /// write the forked checkpoints as **full snapshots**: a fork is a new
    /// timeline and the natural compaction point, so time-travel reads on
    /// the fork never fold a chain — and never another timeline's.
    ///
    /// Errors when the source thread has no checkpoints, when
    /// `at_checkpoint_id` does not exist on the source thread, or when
    /// `src_thread == dst_thread` (ids would collide within one thread).
    async fn fork_thread(
        &self,
        src_thread: &str,
        dst_thread: &str,
        at_checkpoint_id: Option<&str>,
    ) -> Result<usize> {
        if src_thread == dst_thread {
            return Err(RustyError::Checkpoint(format!(
                "cannot fork thread `{src_thread}` onto itself: checkpoint ids would collide"
            )));
        }
        let all = self.list(src_thread).await?;
        if all.is_empty() {
            return Err(RustyError::Checkpoint(format!(
                "cannot fork thread `{src_thread}`: no checkpoints found"
            )));
        }
        let selected: Vec<Checkpoint> = match at_checkpoint_id {
            None => all,
            Some(id) => {
                let pos = all.iter().position(|c| c.id == id).ok_or_else(|| {
                    RustyError::Checkpoint(format!(
                        "cannot fork thread `{src_thread}`: unknown checkpoint id `{id}`"
                    ))
                })?;
                all[..=pos].to_vec()
            }
        };
        let copied = selected.len();
        for mut checkpoint in selected {
            checkpoint.thread_id = dst_thread.to_string();
            self.put(checkpoint).await?;
        }
        tracing::info!(
            src_thread = %src_thread,
            dst_thread = %dst_thread,
            copied = copied,
            "thread history forked"
        );
        Ok(copied)
    }
}

/// In-memory checkpointer: thread-safe (all operations take a single mutex
/// over the store), lost on restart. Suitable for development, tests, and
/// ephemeral runs.
#[derive(Debug, Default, Clone)]
pub struct InMemoryCheckpointer {
    // thread_id -> checkpoints in insertion (super-step) order.
    inner: Arc<Mutex<HashMap<String, Vec<Checkpoint>>>>,
}

impl InMemoryCheckpointer {
    /// An empty store. Clones of the returned checkpointer share the same
    /// underlying map.
    pub fn new() -> Self {
        Self::default()
    }

    fn lock(&self) -> Result<std::sync::MutexGuard<'_, HashMap<String, Vec<Checkpoint>>>> {
        self.inner
            .lock()
            .map_err(|_| RustyError::Checkpoint("in-memory checkpointer lock poisoned".into()))
    }
}

#[async_trait]
impl Checkpointer for InMemoryCheckpointer {
    async fn put(&self, checkpoint: Checkpoint) -> Result<()> {
        let mut guard = self.lock()?;
        let entry = guard.entry(checkpoint.thread_id.clone()).or_default();
        if entry.iter().any(|c| c.id == checkpoint.id) {
            return Err(RustyError::Checkpoint(format!(
                "checkpoint id `{}` already exists for thread `{}`",
                checkpoint.id, checkpoint.thread_id
            )));
        }
        tracing::debug!(
            thread_id = %checkpoint.thread_id,
            checkpoint_id = %checkpoint.id,
            step = checkpoint.step,
            "checkpoint stored (in-memory)"
        );
        entry.push(checkpoint);
        Ok(())
    }

    async fn get_latest(&self, thread_id: &str) -> Result<Option<Checkpoint>> {
        let guard = self.lock()?;
        Ok(guard.get(thread_id).and_then(|v| v.last()).cloned())
    }

    async fn list(&self, thread_id: &str) -> Result<Vec<Checkpoint>> {
        let guard = self.lock()?;
        let mut all = guard.get(thread_id).cloned().unwrap_or_default();
        // Same total order as every other backend (ascending
        // `(step, created_at, id)`), not raw insertion order: replay can
        // append out-of-step-order checkpoints, and fork truncation must be
        // deterministic across backends.
        all.sort_by(|a, b| {
            a.step
                .cmp(&b.step)
                .then(a.created_at.cmp(&b.created_at))
                .then(a.id.cmp(&b.id))
        });
        Ok(all)
    }
}

/// File-backed checkpointer: one pretty-printed JSON file per checkpoint
/// (`{dir}/{thread_id}/{checkpoint_id}.json`) plus a `latest` pointer file
/// (`{dir}/{thread_id}/latest` holding the most recent checkpoint id), using
/// only `serde_json` and `tokio::fs` — no database dependencies.
///
/// Writes are atomic: payload is written to a uniquely named temp file in the
/// same directory and then renamed over the target path, so a crash mid-write
/// can never leave a truncated checkpoint file behind. Puts are serialized
/// per thread (an in-process lock per `thread_id`), so concurrent same-thread
/// puts cannot interleave the checkpoint file and pointer writes and leave
/// `latest` pointing at the older checkpoint. The lock is per-process:
/// multiple writer PROCESSES over the same directory are not serialized —
/// treat one writer process per thread directory as a precondition.
///
/// Read paths are forgiving: a missing thread directory yields `None` / an
/// empty list, a missing or corrupt `latest` pointer falls back to scanning
/// the checkpoint files, and individual corrupt checkpoint files are skipped
/// during scans. Genuine IO failures surface as
/// [`RustyError::Checkpoint`].
///
/// # Delta checkpoints (R0.7 wave 4)
///
/// This backend opts into delta encoding: `put` diffs the incoming full
/// snapshot against the thread's materialized head and stores only the
/// channels that changed, bounded by a [`DeltaPolicy`]
/// ([`JsonFileCheckpointer::with_delta_policy`] reconfigures or disables
/// it). The head is cached in-process (`heads`), so the hot path diffs
/// against the previous put's shared channel values — pointer-equal for
/// unchanged channels, no re-read — and a cold process rebuilds the cache
/// entry by reading the current chain once. Reads fold chains transparently
/// ([`fold_chain`], `materialize_all`); checkpoint files with no `base`
/// field (everything written before W4) load exactly as before.
#[derive(Debug, Clone)]
pub struct JsonFileCheckpointer {
    dir: PathBuf,
    // Per-thread put locks behind a map mutex: the map is locked only long
    // enough to clone the per-thread `Arc`, never across the put itself.
    put_locks: Arc<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>>,
    // The materialized chain head per thread, for the write-side delta
    // diff. Coherent in-process because puts are serialized per thread (the
    // same one-writer-process precondition the whole backend documents);
    // readers never consult it.
    heads: Arc<Mutex<HashMap<String, DeltaHead>>>,
    policy: DeltaPolicy,
}

impl JsonFileCheckpointer {
    /// A checkpointer rooted at `dir` (created lazily on first `put`), with
    /// the default [`DeltaPolicy`] (delta encoding on, chain bounded at 32).
    pub fn new(dir: impl Into<PathBuf>) -> Self {
        Self::with_delta_policy(dir, DeltaPolicy::default())
    }

    /// A checkpointer rooted at `dir` with an explicit delta policy —
    /// [`DeltaPolicy::full_only`] restores the pre-W4 full-snapshot write
    /// behavior (the benchmark baseline).
    pub fn with_delta_policy(dir: impl Into<PathBuf>, policy: DeltaPolicy) -> Self {
        Self {
            dir: dir.into(),
            put_locks: Arc::new(Mutex::new(HashMap::new())),
            heads: Arc::new(Mutex::new(HashMap::new())),
            policy,
        }
    }

    /// The delta-chain policy this checkpointer writes under.
    pub fn delta_policy(&self) -> DeltaPolicy {
        self.policy
    }

    /// The root directory checkpoints are stored under.
    pub fn dir(&self) -> &std::path::Path {
        &self.dir
    }

    /// The lock serializing `put` for one thread. `Clone`s of this
    /// checkpointer share the same lock map, so clones still serialize
    /// against each other.
    fn put_lock(&self, thread_id: &str) -> Result<Arc<tokio::sync::Mutex<()>>> {
        let mut map = self
            .put_locks
            .lock()
            .map_err(|_| RustyError::Checkpoint("put-lock map poisoned".into()))?;
        Ok(map
            .entry(thread_id.to_owned())
            .or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
            .clone())
    }

    /// `{dir}/{thread_id}/` — per-thread subdirectory.
    fn thread_dir(&self, thread_id: &str) -> PathBuf {
        self.dir.join(thread_id)
    }

    /// `{dir}/{thread_id}/{checkpoint_id}.json`.
    fn checkpoint_path(&self, thread_id: &str, checkpoint_id: &str) -> PathBuf {
        self.thread_dir(thread_id)
            .join(format!("{checkpoint_id}.json"))
    }

    /// `{dir}/{thread_id}/latest` — pointer file holding the most recent
    /// checkpoint id (plain text).
    fn latest_path(&self, thread_id: &str) -> PathBuf {
        self.thread_dir(thread_id).join("latest")
    }

    /// Atomically write `bytes` to `path` via a unique temp file + rename.
    /// The temp file lives in the same directory so the rename stays on one
    /// filesystem. Best-effort temp cleanup on failure.
    ///
    /// `pub(crate)`: the W4 artifact store (`journal::FileArtifactStore`)
    /// writes blobs under the same discipline.
    pub(crate) async fn atomic_write(path: &PathBuf, bytes: &[u8]) -> Result<()> {
        let tmp = path.with_file_name(format!(
            ".{}.tmp-{}",
            path.file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| "checkpoint".into()),
            uuid::Uuid::new_v4()
        ));
        if let Err(e) = tokio::fs::write(&tmp, bytes).await {
            let _ = tokio::fs::remove_file(&tmp).await;
            return Err(RustyError::Checkpoint(format!(
                "failed to write temp file `{}`: {e}",
                tmp.display()
            )));
        }
        if let Err(e) = tokio::fs::rename(&tmp, path).await {
            let _ = tokio::fs::remove_file(&tmp).await;
            return Err(RustyError::Checkpoint(format!(
                "failed to rename `{}` -> `{}`: {e}",
                tmp.display(),
                path.display()
            )));
        }
        Ok(())
    }

    /// Read and deserialize one checkpoint file, mapping IO and JSON errors
    /// to [`RustyError::Checkpoint`].
    async fn read_checkpoint(path: &PathBuf) -> Result<Checkpoint> {
        let bytes = tokio::fs::read(path).await.map_err(|e| {
            RustyError::Checkpoint(format!(
                "failed to read checkpoint file `{}`: {e}",
                path.display()
            ))
        })?;
        serde_json::from_slice(&bytes).map_err(|e| {
            RustyError::Checkpoint(format!("corrupt checkpoint file `{}`: {e}", path.display()))
        })
    }

    /// Load every parseable `*.json` checkpoint in a thread directory,
    /// **unfolded** (a delta checkpoint stays a delta here — folding happens
    /// in the read methods above this layer). A missing directory yields an
    /// empty vec; corrupt files are skipped.
    async fn scan_thread_raw(&self, thread_id: &str) -> Result<Vec<Checkpoint>> {
        let dir = self.thread_dir(thread_id);
        let mut entries = match tokio::fs::read_dir(&dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
            Err(e) => {
                return Err(RustyError::Checkpoint(format!(
                    "failed to read thread directory `{}`: {e}",
                    dir.display()
                )))
            }
        };

        let mut checkpoints = Vec::new();
        loop {
            let entry = match entries.next_entry().await {
                Ok(Some(entry)) => entry,
                Ok(None) => break,
                Err(e) => {
                    return Err(RustyError::Checkpoint(format!(
                        "failed to iterate thread directory `{}`: {e}",
                        dir.display()
                    )))
                }
            };
            let path = entry.path();
            let is_json = path.extension().is_some_and(|ext| ext == "json");
            if !is_json {
                continue;
            }
            match Self::read_checkpoint(&path).await {
                Ok(cp) => checkpoints.push(cp),
                // Graceful degradation: one corrupt file must not poison the
                // whole thread's history.
                Err(e) => {
                    tracing::warn!(
                        thread_id = %thread_id,
                        path = %path.display(),
                        error = %e,
                        "skipping corrupt checkpoint file during scan"
                    );
                    continue;
                }
            }
        }
        Ok(checkpoints)
    }

    /// The most recently stored raw checkpoint (unfolded — possibly a
    /// delta): the `latest` pointer fast path, falling back to a directory
    /// scan when the pointer is missing, corrupt, or dangling.
    async fn read_latest_raw(&self, thread_id: &str) -> Result<Option<Checkpoint>> {
        let latest_path = self.latest_path(thread_id);
        if let Ok(id_bytes) = tokio::fs::read(&latest_path).await {
            if let Ok(id) = std::str::from_utf8(&id_bytes) {
                let id = id.trim();
                if !id.is_empty() {
                    let path = self.checkpoint_path(thread_id, id);
                    match Self::read_checkpoint(&path).await {
                        Ok(cp) => return Ok(Some(cp)),
                        Err(e) => tracing::warn!(
                            thread_id = %thread_id,
                            path = %path.display(),
                            error = %e,
                            "latest pointer target unreadable; falling back to directory scan"
                        ),
                    }
                }
            }
        }
        // Fallback: the checkpoint with the greatest `created_at` — the
        // insertion-order proxy shared with the other backends (see the
        // trait's `get_latest` contract), not the highest step, so a replay
        // that appended lower-step checkpoints still resumes the newest
        // timeline.
        Ok(self
            .scan_thread_raw(thread_id)
            .await?
            .into_iter()
            .max_by(|a, b| a.created_at.cmp(&b.created_at).then(a.id.cmp(&b.id))))
    }

    /// Load the raw delta chain ending at `tip`, root-first. Defensively
    /// bounded: writes cap chains at the policy bound, so a chain far past
    /// it means corruption or a foreign writer — and a cycle is always
    /// corruption. Both error rather than fold forever.
    async fn load_chain(&self, thread_id: &str, tip: Checkpoint) -> Result<Vec<Checkpoint>> {
        // The fold bound: generous headroom over the write bound, so any
        // chain this backend could have written folds, and only corrupt or
        // foreign chains hit the limit.
        let fold_bound = self.policy.max_chain_len.saturating_mul(4).max(128);
        let mut chain = vec![tip];
        let mut visited = HashSet::new();
        while let Some(base_id) = chain.last().expect("chain starts non-empty").base.clone() {
            if !visited.insert(base_id.clone()) {
                return Err(RustyError::Checkpoint(format!(
                    "checkpoint delta chain in thread `{thread_id}` has a cycle at `{base_id}`"
                )));
            }
            if chain.len() > fold_bound {
                return Err(RustyError::Checkpoint(format!(
                    "checkpoint delta chain in thread `{thread_id}` exceeds {fold_bound} links; \
                     refusing to fold (write bound is {})",
                    self.policy.max_chain_len
                )));
            }
            let path = self.checkpoint_path(thread_id, &base_id);
            chain.push(Self::read_checkpoint(&path).await?);
        }
        chain.reverse();
        Ok(chain)
    }

    /// The thread's materialized chain head for the write-side delta diff:
    /// cached, or rebuilt by reading and folding the current chain (a cold
    /// process pays this once per thread). `None` when the thread has no
    /// checkpoints.
    async fn head_of(&self, thread_id: &str) -> Result<Option<DeltaHead>> {
        if let Some(head) = self.lock_heads()?.get(thread_id) {
            return Ok(Some(head.clone()));
        }
        let Some(raw) = self.read_latest_raw(thread_id).await? else {
            return Ok(None);
        };
        let chain = self.load_chain(thread_id, raw).await?;
        let head = DeltaHead {
            chain_len: chain.len() - 1,
            checkpoint: fold_chain(&chain)?,
        };
        self.lock_heads()?
            .insert(thread_id.to_owned(), head.clone());
        Ok(Some(head))
    }

    fn lock_heads(&self) -> Result<std::sync::MutexGuard<'_, HashMap<String, DeltaHead>>> {
        self.heads
            .lock()
            .map_err(|_| RustyError::Checkpoint("delta-head cache poisoned".into()))
    }

    /// The shared write path. `force_full` stores the checkpoint as a full
    /// snapshot regardless of policy — the fork compaction semantics
    /// ([`Checkpointer::fork_thread`]).
    async fn put_internal(&self, checkpoint: Checkpoint, force_full: bool) -> Result<()> {
        // Serialize the whole put (checkpoint file THEN pointer file) per
        // thread: without this, two concurrent same-thread puts can
        // interleave (file A, file B, pointer B, pointer A) and leave
        // `latest` pointing at the older checkpoint. Held across `.await`s,
        // hence a tokio mutex — a std guard would make the future `!Send`.
        let lock = self.put_lock(&checkpoint.thread_id)?;
        let _put_guard = lock.lock().await;

        let thread_dir = self.thread_dir(&checkpoint.thread_id);
        tokio::fs::create_dir_all(&thread_dir).await.map_err(|e| {
            RustyError::Checkpoint(format!(
                "failed to create thread directory `{}`: {e}",
                thread_dir.display()
            ))
        })?;

        let path = self.checkpoint_path(&checkpoint.thread_id, &checkpoint.id);
        // Preserve the no-overwrite contract: checkpoint ids are unique by
        // construction, so an existing file means a duplicate `put`.
        if tokio::fs::try_exists(&path).await.map_err(|e| {
            RustyError::Checkpoint(format!(
                "failed to stat checkpoint file `{}`: {e}",
                path.display()
            ))
        })? {
            return Err(RustyError::Checkpoint(format!(
                "checkpoint id `{}` already exists for thread `{}`",
                checkpoint.id, checkpoint.thread_id
            )));
        }

        // Delta encoding (W4): diff the incoming full snapshot against the
        // thread's materialized head and store only the channels that
        // changed, bounded by the chain policy. The encoded file is what
        // later reads fold back.
        let encoding = if force_full {
            DeltaEncoding::full(&checkpoint)
        } else {
            let head = self.head_of(&checkpoint.thread_id).await?;
            encode_delta(&checkpoint, head.as_ref(), &self.policy)
        };

        let bytes = serde_json::to_vec_pretty(&encoding.checkpoint)?;
        Self::atomic_write(&path, &bytes).await?;

        // Update the latest pointer (also atomically). Written after the
        // checkpoint file itself so a crash never leaves a dangling pointer.
        Self::atomic_write(
            &self.latest_path(&checkpoint.thread_id),
            checkpoint.id.as_bytes(),
        )
        .await?;

        tracing::debug!(
            thread_id = %checkpoint.thread_id,
            checkpoint_id = %checkpoint.id,
            step = checkpoint.step,
            delta = encoding.checkpoint.base.is_some(),
            path = %path.display(),
            "checkpoint persisted (json file)"
        );

        // The next put diffs against THIS put's full state, held shared:
        // channels the next step does not write stay pointer-identical,
        // which is what makes the diff cheap.
        self.lock_heads()?.insert(
            checkpoint.thread_id.clone(),
            DeltaHead {
                checkpoint,
                chain_len: encoding.chain_len,
            },
        );
        Ok(())
    }
}

#[async_trait]
impl Checkpointer for JsonFileCheckpointer {
    async fn put(&self, checkpoint: Checkpoint) -> Result<()> {
        self.put_internal(checkpoint, false).await
    }

    async fn get_latest(&self, thread_id: &str) -> Result<Option<Checkpoint>> {
        let Some(raw) = self.read_latest_raw(thread_id).await? else {
            return Ok(None);
        };
        if raw.base.is_none() {
            // A full snapshot — everything written before W4, chain roots,
            // forked checkpoints — needs no folding.
            return Ok(Some(raw));
        }
        let chain = self.load_chain(thread_id, raw).await?;
        fold_chain(&chain).map(Some)
    }

    async fn list(&self, thread_id: &str) -> Result<Vec<Checkpoint>> {
        // Fold every delta chain, then impose the same total order as every
        // other backend (ascending `(step, created_at, id)`), not raw file
        // order: replay can append out-of-step-order checkpoints, and fork
        // truncation must be deterministic across backends.
        let mut all = materialize_all(self.scan_thread_raw(thread_id).await?);
        all.sort_by(|a, b| {
            a.step
                .cmp(&b.step)
                .then(a.created_at.cmp(&b.created_at))
                .then(a.id.cmp(&b.id))
        });
        Ok(all)
    }

    /// Forked histories are written as **full snapshots** (eager
    /// compaction): a fork is a new timeline and the natural compaction
    /// point, so time-travel reads on the fork never fold a chain — and
    /// never another timeline's. The source thread is untouched.
    async fn fork_thread(
        &self,
        src_thread: &str,
        dst_thread: &str,
        at_checkpoint_id: Option<&str>,
    ) -> Result<usize> {
        if src_thread == dst_thread {
            return Err(RustyError::Checkpoint(format!(
                "cannot fork thread `{src_thread}` onto itself: checkpoint ids would collide"
            )));
        }
        let all = self.list(src_thread).await?;
        if all.is_empty() {
            return Err(RustyError::Checkpoint(format!(
                "cannot fork thread `{src_thread}`: no checkpoints found"
            )));
        }
        let selected: Vec<Checkpoint> = match at_checkpoint_id {
            None => all,
            Some(id) => {
                let pos = all.iter().position(|c| c.id == id).ok_or_else(|| {
                    RustyError::Checkpoint(format!(
                        "cannot fork thread `{src_thread}`: unknown checkpoint id `{id}`"
                    ))
                })?;
                all[..=pos].to_vec()
            }
        };
        let copied = selected.len();
        for mut checkpoint in selected {
            checkpoint.thread_id = dst_thread.to_string();
            checkpoint.base = None;
            self.put_internal(checkpoint, true).await?;
        }
        tracing::info!(
            src_thread = %src_thread,
            dst_thread = %dst_thread,
            copied = copied,
            "thread history forked"
        );
        Ok(copied)
    }
}

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

    fn cp(thread: &str, step: usize) -> Checkpoint {
        Checkpoint::new(thread, step, State::new(), vec!["next".into()])
    }

    #[tokio::test]
    async fn in_memory_roundtrip() {
        let store = InMemoryCheckpointer::new();
        assert!(store.get_latest("t1").await.unwrap().is_none());
        assert!(store.list("t1").await.unwrap().is_empty());

        store.put(cp("t1", 0)).await.unwrap();
        store.put(cp("t1", 1)).await.unwrap();
        store.put(cp("t2", 0)).await.unwrap();

        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.step, 1);
        assert_eq!(latest.next_nodes, vec!["next".to_string()]);

        let all = store.list("t1").await.unwrap();
        assert_eq!(all.len(), 2);
        assert_eq!(all[0].step, 0);
        assert_eq!(all[1].step, 1);

        // Threads are isolated.
        assert_eq!(store.list("t2").await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn in_memory_rejects_duplicate_id() {
        let store = InMemoryCheckpointer::new();
        let checkpoint = cp("t1", 0);
        store.put(checkpoint.clone()).await.unwrap();
        let err = store.put(checkpoint).await.unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));
    }

    #[tokio::test]
    async fn checkpoint_serializes() {
        let checkpoint = cp("t1", 2);
        let json = serde_json::to_string(&checkpoint).unwrap();
        let back: Checkpoint = serde_json::from_str(&json).unwrap();
        assert_eq!(back.id, checkpoint.id);
        assert_eq!(back.step, 2);
    }

    /// Unique temp root under the OS temp dir, removed on drop.
    struct TestDir(PathBuf);

    impl TestDir {
        fn new() -> Self {
            Self(
                std::env::temp_dir()
                    .join(format!("rusty-checkpoint-test-{}", uuid::Uuid::new_v4())),
            )
        }
    }

    impl Drop for TestDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[tokio::test]
    async fn json_file_roundtrip() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        assert!(store.get_latest("t1").await.unwrap().is_none());
        assert!(store.list("t1").await.unwrap().is_empty());

        let mut state = State::new();
        state.insert("answer", serde_json::json!(42));
        let cp0 = Checkpoint::new("t1", 0, state.clone(), vec!["node_b".into()]);
        let id0 = cp0.id.clone();
        store.put(cp0).await.unwrap();

        // File layout: <root>/<thread_id>/<checkpoint_id>.json
        assert!(tmp.0.join("t1").join(format!("{id0}.json")).exists());

        let back = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(back.id, id0);
        assert_eq!(back.thread_id, "t1");
        assert_eq!(back.step, 0);
        assert_eq!(back.state, state);
        assert_eq!(back.next_nodes, vec!["node_b".to_string()]);

        // Threads are isolated.
        assert!(store.get_latest("t2").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn json_file_latest_pointer_tracks_most_recent_put() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        store.put(cp("t1", 0)).await.unwrap();
        let cp1 = cp("t1", 1);
        let id1 = cp1.id.clone();
        store.put(cp1).await.unwrap();

        // The pointer file holds the most recent checkpoint id as text.
        let pointer = std::fs::read_to_string(tmp.0.join("t1").join("latest")).unwrap();
        assert_eq!(pointer, id1);

        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.id, id1);
        assert_eq!(latest.step, 1);
    }

    #[tokio::test]
    async fn json_file_list_sorted_by_step_regardless_of_put_order() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        store.put(cp("t1", 2)).await.unwrap();
        store.put(cp("t1", 0)).await.unwrap();
        store.put(cp("t1", 1)).await.unwrap();

        let all = store.list("t1").await.unwrap();
        assert_eq!(all.len(), 3);
        assert_eq!(all[0].step, 0);
        assert_eq!(all[1].step, 1);
        assert_eq!(all[2].step, 2);

        // Recency is insertion order, not highest step: the pointer tracks
        // the most recent put (step 1), and the scan fallback agrees because
        // the freshest `created_at` is also the last put's.
        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.step, 1);
    }

    /// The `get_latest`/`list` contract is backend-independent: recency =
    /// insertion order, listing = ascending `(step, created_at, id)`. Every
    /// `Checkpointer` impl must agree with this test.
    #[tokio::test]
    async fn recency_and_list_order_agree_across_backends() {
        let tmp = TestDir::new();
        let memory = InMemoryCheckpointer::new();
        let json_file = JsonFileCheckpointer::new(tmp.0.clone());

        // Out-of-step-order puts (as replay-on-same-thread produces): each
        // checkpoint is minted fresh, so `created_at` increases per put.
        let steps = [2usize, 0, 1];
        for step in steps {
            memory.put(cp("t1", step)).await.unwrap();
            json_file.put(cp("t1", step)).await.unwrap();
        }

        let stores: [&dyn Checkpointer; 2] = [&memory, &json_file];
        for store in stores {
            // Latest = last put (step 1), not highest step (step 2).
            let latest = store.get_latest("t1").await.unwrap().unwrap();
            assert_eq!(latest.step, 1, "backend disagrees on recency");
            // List = ascending step order regardless of put order.
            let listed: Vec<usize> = store
                .list("t1")
                .await
                .unwrap()
                .iter()
                .map(|c| c.step)
                .collect();
            assert_eq!(listed, [0, 1, 2], "backend disagrees on list order");
        }
    }

    #[tokio::test]
    async fn json_file_missing_thread_returns_none_and_empty() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        assert!(store.get_latest("never-seen").await.unwrap().is_none());
        assert!(store.list("never-seen").await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn json_file_rejects_duplicate_id() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        let checkpoint = cp("t1", 0);
        store.put(checkpoint.clone()).await.unwrap();
        let err = store.put(checkpoint).await.unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));
    }

    #[tokio::test]
    async fn json_file_durable_across_instances() {
        let tmp = TestDir::new();
        let cp0 = cp("t1", 0);
        let id0 = cp0.id.clone();

        JsonFileCheckpointer::new(tmp.0.clone())
            .put(cp0)
            .await
            .unwrap();

        // A fresh instance over the same root sees the checkpoint
        // (simulates process restart).
        let reopened = JsonFileCheckpointer::new(tmp.0.clone());
        let latest = reopened.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.id, id0);
        assert_eq!(reopened.list("t1").await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn json_file_corrupt_files_are_handled_gracefully() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        let good = cp("t1", 0);
        let good_id = good.id.clone();
        store.put(good).await.unwrap();

        // A corrupt checkpoint file next to a valid one must not break
        // list/get_latest.
        std::fs::write(tmp.0.join("t1").join("garbage.json"), b"{not json!!").unwrap();
        let all = store.list("t1").await.unwrap();
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].id, good_id);

        // A corrupt latest pointer falls back to scanning.
        std::fs::write(tmp.0.join("t1").join("latest"), b"no-such-checkpoint-id").unwrap();
        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.id, good_id);

        // Garbage bytes in the pointer also fall back to scanning.
        std::fs::write(tmp.0.join("t1").join("latest"), [0xff, 0xfe, 0x00]).unwrap();
        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.id, good_id);
    }

    #[tokio::test]
    async fn get_by_id_hit_and_miss() {
        let store = InMemoryCheckpointer::new();
        let cp0 = cp("t1", 0);
        let id0 = cp0.id.clone();
        store.put(cp0).await.unwrap();
        store.put(cp("t1", 1)).await.unwrap();
        store.put(cp("t2", 0)).await.unwrap();

        let hit = store.get_by_id("t1", &id0).await.unwrap().unwrap();
        assert_eq!(hit.id, id0);
        assert_eq!(hit.step, 0);
        assert_eq!(hit.thread_id, "t1");

        // Unknown id on an existing thread, and any id on an unknown thread.
        assert!(store.get_by_id("t1", "no-such-id").await.unwrap().is_none());
        assert!(store.get_by_id("t2", &id0).await.unwrap().is_none());
        assert!(store.get_by_id("never-seen", &id0).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn fork_full_history_copies_all_checkpoints() {
        let store = InMemoryCheckpointer::new();
        let mut src = Vec::new();
        for step in 0..3 {
            let mut state = State::new();
            state.insert("n", serde_json::json!(step));
            let checkpoint = Checkpoint::new("src", step, state, vec!["next".into()]);
            src.push(checkpoint.clone());
            store.put(checkpoint).await.unwrap();
        }

        let copied = store.fork_thread("src", "dst", None).await.unwrap();
        assert_eq!(copied, 3);

        let dst = store.list("dst").await.unwrap();
        assert_eq!(dst.len(), 3);
        for (forked, original) in dst.iter().zip(src.iter()) {
            // Everything is preserved except the thread id (ids may be reused
            // across threads; uniqueness is per-thread).
            assert_eq!(forked.id, original.id);
            assert_eq!(forked.step, original.step);
            assert_eq!(forked.state, original.state);
            assert_eq!(forked.next_nodes, original.next_nodes);
            assert_eq!(forked.created_at, original.created_at);
            assert_eq!(forked.thread_id, "dst");
        }
        assert_eq!(store.get_latest("dst").await.unwrap().unwrap().step, 2);

        // The source thread is untouched.
        assert_eq!(store.list("src").await.unwrap().len(), 3);
    }

    #[tokio::test]
    async fn fork_at_mid_checkpoint_truncates_history() {
        let store = InMemoryCheckpointer::new();
        let mut ids = Vec::new();
        for step in 0..4 {
            let checkpoint = cp("src", step);
            ids.push(checkpoint.id.clone());
            store.put(checkpoint).await.unwrap();
        }

        // Fork at the step-1 checkpoint: only steps 0 and 1 are copied.
        let copied = store
            .fork_thread("src", "dst", Some(&ids[1]))
            .await
            .unwrap();
        assert_eq!(copied, 2);

        let dst = store.list("dst").await.unwrap();
        assert_eq!(dst.len(), 2);
        assert_eq!(dst[0].id, ids[0]);
        assert_eq!(dst[1].id, ids[1]);
        assert_eq!(dst[0].step, 0);
        assert_eq!(dst[1].step, 1);
        // Latest of the fork is the cut point, not the source's head.
        assert_eq!(store.get_latest("dst").await.unwrap().unwrap().id, ids[1]);
    }

    #[tokio::test]
    async fn fork_errors_on_empty_src_unknown_id_and_self_fork() {
        let store = InMemoryCheckpointer::new();
        let checkpoint = cp("src", 0);
        let id0 = checkpoint.id.clone();
        store.put(checkpoint).await.unwrap();

        let err = store.fork_thread("empty", "dst", None).await.unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));

        let err = store
            .fork_thread("src", "dst", Some("no-such-id"))
            .await
            .unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));

        let err = store
            .fork_thread("src", "src", Some(&id0))
            .await
            .unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));

        // Failed forks leave no partial state behind on the destination.
        assert!(store.list("dst").await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn json_file_fork_across_threads_persists_correctly() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        let mut ids = Vec::new();
        for step in 0..3 {
            let mut state = State::new();
            state.insert("n", serde_json::json!(step));
            let checkpoint = Checkpoint::new("src", step, state, vec!["next".into()]);
            ids.push(checkpoint.id.clone());
            store.put(checkpoint).await.unwrap();
        }

        let copied = store
            .fork_thread("src", "dst", Some(&ids[1]))
            .await
            .unwrap();
        assert_eq!(copied, 2);

        // Files land under the destination thread's own directory (reused
        // ids live in a different path, so no collision).
        assert!(tmp.0.join("dst").join(format!("{}.json", ids[0])).exists());
        assert!(tmp.0.join("dst").join(format!("{}.json", ids[1])).exists());
        assert!(!tmp.0.join("dst").join(format!("{}.json", ids[2])).exists());

        // The forked files carry the destination thread id in their payload.
        let latest = store.get_latest("dst").await.unwrap().unwrap();
        assert_eq!(latest.id, ids[1]);
        assert_eq!(latest.thread_id, "dst");
        assert_eq!(latest.step, 1);

        // Durable across instances (process restart): the fork survives.
        let reopened = JsonFileCheckpointer::new(tmp.0.clone());
        let dst = reopened.list("dst").await.unwrap();
        assert_eq!(dst.len(), 2);
        assert_eq!(dst[0].id, ids[0]);
        assert_eq!(dst[1].id, ids[1]);
        assert!(dst.iter().all(|c| c.thread_id == "dst"));

        // The source thread is untouched.
        assert_eq!(reopened.list("src").await.unwrap().len(), 3);
        assert_eq!(
            reopened.get_latest("src").await.unwrap().unwrap().id,
            ids[2]
        );
    }

    // ---- Delta checkpoints (R0.7 wave 4) ----

    /// A state whose `blob` channel stays constant while `step` turns over —
    /// the shape delta checkpoints exist for.
    fn evolving_state(step: usize, blob: &str) -> State {
        let mut state = State::new();
        state.insert("blob", serde_json::json!(blob));
        state.insert("step", serde_json::json!(step));
        state
    }

    /// Read one checkpoint file straight off disk as raw JSON.
    fn file_json(tmp: &TestDir, thread: &str, id: &str) -> serde_json::Value {
        let text = std::fs::read_to_string(tmp.0.join(thread).join(format!("{id}.json"))).unwrap();
        serde_json::from_str(&text).unwrap()
    }

    #[tokio::test]
    async fn delta_chain_roundtrips_through_read_paths() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());
        let blob = "x".repeat(1024);

        let mut ids = Vec::new();
        for step in 0..5 {
            let cp = Checkpoint::new("t1", step, evolving_state(step, &blob), vec!["next".into()]);
            ids.push(cp.id.clone());
            store.put(cp).await.unwrap();
        }

        // Puts after the first are deltas: `base` names the previous
        // checkpoint and `state` carries only the changed `step` channel.
        let on_disk = file_json(&tmp, "t1", &ids[2]);
        assert_eq!(on_disk["base"], serde_json::json!(ids[1]));
        assert_eq!(
            on_disk["state"],
            serde_json::json!({"step": 2}),
            "delta file must carry only changed channels"
        );

        // Reads materialize: get_latest folds the chain back to full state.
        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.base, None, "read methods never return a delta");
        assert_eq!(latest.state, evolving_state(4, &blob));

        // list materializes every link.
        let all = store.list("t1").await.unwrap();
        assert_eq!(all.len(), 5);
        for (step, cp) in all.iter().enumerate() {
            assert_eq!(cp.state, evolving_state(step, &blob));
            assert_eq!(cp.base, None);
        }

        // get_by_id (trait default, over list) materializes mid-chain links.
        let mid = store.get_by_id("t1", &ids[1]).await.unwrap().unwrap();
        assert_eq!(mid.state, evolving_state(1, &blob));
    }

    #[tokio::test]
    async fn delta_chain_is_bounded_by_policy() {
        let tmp = TestDir::new();
        let policy = DeltaPolicy {
            max_chain_len: 3,
            ..DeltaPolicy::default()
        };
        let store = JsonFileCheckpointer::with_delta_policy(tmp.0.clone(), policy);

        let mut ids = Vec::new();
        for step in 0..7 {
            let cp = Checkpoint::new("t1", step, evolving_state(step, "b"), vec![]);
            ids.push(cp.id.clone());
            store.put(cp).await.unwrap();
        }

        // Chain: full, delta, delta, delta, FULL (bound hit), delta, delta.
        let expect_full = [true, false, false, false, true, false, false];
        for (i, id) in ids.iter().enumerate() {
            let on_disk = file_json(&tmp, "t1", id);
            assert_eq!(
                on_disk.get("base").is_some(),
                !expect_full[i],
                "checkpoint {i} full/delta mismatch: {on_disk}"
            );
        }
        // Reads are unaffected by where the chain compacted.
        let all = store.list("t1").await.unwrap();
        for (step, cp) in all.iter().enumerate() {
            assert_eq!(cp.state, evolving_state(step, "b"));
        }
    }

    #[tokio::test]
    async fn delta_byte_ratio_forces_full_when_delta_stops_paying() {
        let tmp = TestDir::new();
        let policy = DeltaPolicy {
            max_chain_len: 32,
            max_byte_ratio: 0.5,
        };
        let store = JsonFileCheckpointer::with_delta_policy(tmp.0.clone(), policy);

        let base_state = State::from_value(serde_json::json!({"a": "AAAA", "b": "BBBB"})).unwrap();
        let cp0 = Checkpoint::new("t1", 0, base_state, vec![]);
        let id0 = cp0.id.clone();
        store.put(cp0).await.unwrap();

        // Every channel changed: the delta is the whole state — past the
        // 0.5 ratio, so a full snapshot is written instead of a chain link.
        let changed = State::from_value(serde_json::json!({"a": "CCCC", "b": "DDDD"})).unwrap();
        let cp1 = Checkpoint::new("t1", 1, changed, vec![]);
        let id1 = cp1.id.clone();
        store.put(cp1).await.unwrap();

        assert!(file_json(&tmp, "t1", &id0).get("base").is_none());
        assert!(
            file_json(&tmp, "t1", &id1).get("base").is_none(),
            "a delta worth ~full must reset the chain"
        );
        assert_eq!(
            store.get_latest("t1").await.unwrap().unwrap().state,
            State::from_value(serde_json::json!({"a": "CCCC", "b": "DDDD"})).unwrap()
        );
    }

    #[tokio::test]
    async fn delta_dedupes_equal_content_without_pointer_sharing() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        store
            .put(Checkpoint::new(
                "t1",
                0,
                State::from_value(serde_json::json!({"k": "v"})).unwrap(),
                vec![],
            ))
            .await
            .unwrap();
        // A fresh, independently built state with identical content: no
        // pointer sharing, so only value equality can dedupe it.
        let cp1 = Checkpoint::new(
            "t1",
            1,
            State::from_value(serde_json::json!({"k": "v"})).unwrap(),
            vec![],
        );
        let id1 = cp1.id.clone();
        store.put(cp1).await.unwrap();

        let on_disk = file_json(&tmp, "t1", &id1);
        assert!(on_disk.get("base").is_some());
        assert_eq!(
            on_disk["state"],
            serde_json::json!({}),
            "identical content must produce an empty delta"
        );
        assert_eq!(
            store.get_latest("t1").await.unwrap().unwrap().state,
            State::from_value(serde_json::json!({"k": "v"})).unwrap()
        );
    }

    #[tokio::test]
    async fn reopened_store_rebuilds_the_head_and_keeps_differencing() {
        let tmp = TestDir::new();
        let blob = "y".repeat(512);

        let id0;
        {
            let store = JsonFileCheckpointer::new(tmp.0.clone());
            let cp = Checkpoint::new("t1", 0, evolving_state(0, &blob), vec![]);
            id0 = cp.id.clone();
            store.put(cp).await.unwrap();
        }

        // A fresh instance over the same root (process restart): the head
        // cache is cold and must be rebuilt from the on-disk chain.
        let reopened = JsonFileCheckpointer::new(tmp.0.clone());
        let cp1 = Checkpoint::new("t1", 1, evolving_state(1, &blob), vec![]);
        let id1 = cp1.id.clone();
        reopened.put(cp1).await.unwrap();

        let on_disk = file_json(&tmp, "t1", &id1);
        assert_eq!(on_disk["base"], serde_json::json!(id0));
        assert_eq!(on_disk["state"], serde_json::json!({"step": 1}));
        assert_eq!(
            reopened.get_latest("t1").await.unwrap().unwrap().state,
            evolving_state(1, &blob)
        );
    }

    #[tokio::test]
    async fn pre_delta_checkpoints_load_unchanged() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        // Hand-write a checkpoint file in the pre-W4 shape (no `base`
        // field), as an older binary would have produced it.
        let cp = Checkpoint::new("t1", 0, evolving_state(3, "legacy"), vec!["next".into()]);
        let id = cp.id.clone();
        let dir = tmp.0.join("t1");
        std::fs::create_dir_all(&dir).unwrap();
        let mut json: serde_json::Value =
            serde_json::from_str(&serde_json::to_string_pretty(&cp).unwrap()).unwrap();
        json.as_object_mut().unwrap().remove("base");
        let text = serde_json::to_string_pretty(&json).unwrap();
        assert!(!text.contains("\"base\""));
        std::fs::write(dir.join(format!("{id}.json")), &text).unwrap();
        std::fs::write(dir.join("latest"), &id).unwrap();

        let latest = store.get_latest("t1").await.unwrap().unwrap();
        assert_eq!(latest.id, id);
        assert_eq!(latest.state, evolving_state(3, "legacy"));
        assert_eq!(latest.next_nodes, vec!["next".to_string()]);
        assert_eq!(latest.base, None);

        // A delta put on top of a legacy checkpoint diffs against it fine.
        let cp1 = Checkpoint::new("t1", 1, evolving_state(4, "legacy"), vec![]);
        let id1 = cp1.id.clone();
        store.put(cp1).await.unwrap();
        assert_eq!(file_json(&tmp, "t1", &id1)["base"], serde_json::json!(id));
    }

    #[tokio::test]
    async fn fork_compacts_to_full_snapshots() {
        let tmp = TestDir::new();
        let store = JsonFileCheckpointer::new(tmp.0.clone());

        let mut ids = Vec::new();
        for step in 0..4 {
            let cp = Checkpoint::new("src", step, evolving_state(step, "f"), vec![]);
            ids.push(cp.id.clone());
            store.put(cp).await.unwrap();
        }

        let copied = store
            .fork_thread("src", "dst", Some(&ids[2]))
            .await
            .unwrap();
        assert_eq!(copied, 3);

        // Every forked checkpoint is a full snapshot on disk — the fork is
        // the compaction point, so its timeline never folds a chain.
        for id in &ids[..3] {
            let on_disk = file_json(&tmp, "dst", id);
            assert!(
                on_disk.get("base").is_none(),
                "forked checkpoint must be a full snapshot: {on_disk}"
            );
        }
        // And reads agree, on both timelines.
        assert_eq!(
            store.get_latest("dst").await.unwrap().unwrap().state,
            evolving_state(2, "f")
        );
        assert_eq!(
            store.get_latest("src").await.unwrap().unwrap().state,
            evolving_state(3, "f")
        );
    }

    #[tokio::test]
    async fn full_only_policy_restores_pre_w4_writes() {
        let tmp = TestDir::new();
        let store =
            JsonFileCheckpointer::with_delta_policy(tmp.0.clone(), DeltaPolicy::full_only());

        let mut ids = Vec::new();
        for step in 0..3 {
            let cp = Checkpoint::new("t1", step, evolving_state(step, "z"), vec![]);
            ids.push(cp.id.clone());
            store.put(cp).await.unwrap();
        }
        for id in &ids {
            assert!(file_json(&tmp, "t1", id).get("base").is_none());
        }
        assert_eq!(store.list("t1").await.unwrap().len(), 3);
    }

    #[test]
    fn encode_delta_decides_full_delta_and_bounds() {
        let head_state = evolving_state(0, "blob");
        let head_cp = Checkpoint::new("t", 0, head_state.clone(), vec![]);
        let head = DeltaHead {
            checkpoint: head_cp.clone(),
            chain_len: 0,
        };
        let policy = DeltaPolicy::default();

        // Delta: one small channel changed.
        let next = Checkpoint::new("t", 1, evolving_state(1, "blob"), vec![]);
        let encoding = encode_delta(&next, Some(&head), &policy);
        assert_eq!(encoding.checkpoint.base, Some(head_cp.id.clone()));
        assert_eq!(
            encoding.checkpoint.state,
            State::from_value(serde_json::json!({"step": 1})).unwrap()
        );
        assert_eq!(encoding.chain_len, 1);

        // Chain bound: a head already at the limit forces a full snapshot.
        let at_bound = DeltaHead {
            chain_len: policy.max_chain_len,
            ..head.clone()
        };
        let encoding = encode_delta(&next, Some(&at_bound), &policy);
        assert_eq!(encoding.checkpoint.base, None);
        assert_eq!(encoding.chain_len, 0);

        // No head or disabled policy: full snapshot.
        assert!(encode_delta(&next, None, &policy).checkpoint.base.is_none());
        assert!(encode_delta(&next, Some(&head), &DeltaPolicy::full_only())
            .checkpoint
            .base
            .is_none());

        // A caller-set base is never honored: put contract is full snapshots.
        let mut with_base = next.clone();
        with_base.base = Some("caller-lie".into());
        let encoding = encode_delta(&with_base, Some(&at_bound), &policy);
        assert_eq!(encoding.checkpoint.base, None);
    }

    #[test]
    fn fold_chain_materializes_and_rejects_broken_chains() {
        let blob = "q".repeat(64);
        let mut chain: Vec<Checkpoint> = Vec::new();
        // The materialized tip the backend would diff the next put against.
        let mut head: Option<DeltaHead> = None;
        for step in 0..4 {
            let full = Checkpoint::new("t", step, evolving_state(step, &blob), vec![]);
            let encoding = encode_delta(&full, head.as_ref(), &DeltaPolicy::default());
            chain.push(encoding.checkpoint);
            head = Some(DeltaHead {
                checkpoint: full,
                chain_len: encoding.chain_len,
            });
        }

        let tip = fold_chain(&chain).unwrap();
        assert_eq!(tip.state, evolving_state(3, &blob));
        assert_eq!(tip.base, None);

        // A root that is itself a delta is a broken chain.
        let err = fold_chain(&chain[1..]).unwrap_err();
        assert!(matches!(err, RustyError::Checkpoint(_)));

        // A link pointing at the wrong base is a broken chain.
        let mut broken = chain.clone();
        broken[2].base = Some("not-the-parent".into());
        assert!(fold_chain(&broken).is_err());
    }

    #[test]
    fn materialize_all_folds_memoized_and_skips_corruption() {
        let full = Checkpoint::new("t", 0, evolving_state(0, "m"), vec![]);
        let mut link = Checkpoint::new("t", 1, evolving_state(1, "m"), vec![]);
        link.base = Some(full.id.clone());
        link.state = State::from_value(serde_json::json!({"step": 1})).unwrap();
        let mut dangling = Checkpoint::new("t", 2, State::new(), vec![]);
        dangling.base = Some("missing-base".into());
        let mut cyclic = Checkpoint::new("t", 3, State::new(), vec![]);
        cyclic.base = Some(cyclic.id.clone());

        let materialized = materialize_all(vec![link.clone(), dangling, cyclic, full.clone()]);
        // The dangling and cyclic links are skipped (corruption must not
        // poison the thread); the full root and its link fold.
        assert_eq!(materialized.len(), 2);
        let by_step: HashMap<usize, &Checkpoint> =
            materialized.iter().map(|c| (c.step, c)).collect();
        assert_eq!(by_step[&0].state, evolving_state(0, "m"));
        assert_eq!(by_step[&1].state, evolving_state(1, "m"));
        assert!(materialized.iter().all(|c| c.base.is_none()));
    }
}