pointbreak 0.5.0

Durable terminal code review for changes humans and coding agents collaborate on together
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
use std::collections::{BTreeMap, HashSet};
use std::path::{Component, Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::canonical_hash::{sha256_bytes_hex, sha256_json_prefixed};
use crate::error::{Result, ShoreError};
use crate::model::id_prefix;
use crate::session::event::{EventType, IngestVia, ShoreEvent, stamp_ingest_provenance};
use crate::session::object_artifact::decode_and_validate_object_artifact;
use crate::session::projection::ArtifactRemovalProjection;
use crate::session::store::body_artifact::{NoteBodyEnvelope, body_artifact_field};
use crate::session::store::{EventStore, ObjectArtifact};
use crate::session::{
    EventVerificationPolicy, IngestEventVerification, SessionState, TrustSet, current_timestamp,
    verify_events_for_ingest,
};
use crate::storage::{CreateOutcome, Durability, LocalStorage};

const EXPORT_MANIFEST_SCHEMA: &str = "shore.store-export-manifest";
const EXPORT_MANIFEST_VERSION: u32 = 1;

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExportManifest {
    pub schema: String,
    pub version: u32,
    pub fidelity_status: ExportFidelityStatus,
    pub events: Vec<ExportEvent>,
    pub artifacts: Vec<ExportArtifact>,
    pub diagnostics: Vec<ExportManifestDiagnostic>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ExportFidelityStatus {
    Full,
    Incomplete,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExportEvent {
    pub event_id: String,
    pub event_type: String,
    pub idempotency_key: String,
    pub payload_hash: String,
    pub event_envelope_hash: String,
    pub event_file_hash: String,
    pub artifact_refs: Vec<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExportArtifact {
    pub artifact_ref: String,
    pub artifact_kind: ExportArtifactKind,
    pub schema: String,
    pub version: u32,
    pub content_hash: String,
    pub byte_size: u64,
    pub required_by_event_ids: Vec<String>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ExportArtifactKind {
    Object,
    NoteBody,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExportManifestDiagnostic {
    pub code: String,
    pub artifact_ref: String,
    pub event_id: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ImportBundleResult {
    pub events_created: usize,
    pub events_existing: usize,
    pub artifacts_created: usize,
    pub artifacts_existing: usize,
    /// Referenced artifacts the source could not resolve to bytes and no
    /// `ArtifactRemoved` claim explains — an unaccountable absence (e.g. an old
    /// snapshot compacted/migrated away without a claim). The fold carries the
    /// referencing events regardless; this count is disclosed as advisory. A
    /// claim-covered absence never counts here.
    pub absent_artifact_count: usize,
    pub verification: Vec<IngestEventVerification>,
    pub commit_order: Vec<ImportCommitStep>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ImportCommitStep {
    Artifacts,
    Events,
    State,
}

pub(crate) fn build_export_manifest(store_dir: impl AsRef<Path>) -> Result<ExportManifest> {
    let store_dir = store_dir.as_ref();
    let event_store = EventStore::open(store_dir);
    let mut event_entries = Vec::new();
    let mut all_events = Vec::new();
    let mut artifact_requirements = BTreeMap::<String, ArtifactRequirement>::new();
    let mut diagnostics = Vec::new();

    for path in event_file_paths(store_dir)? {
        let bytes = std::fs::read(&path).map_err(|error| {
            ShoreError::Message(format!("failed to read event for export manifest: {error}"))
        })?;
        let event = event_store.read_event(&path)?;
        all_events.push(event.clone());
        let event_id = event.event_id.as_str().to_owned();
        let event_envelope_hash = sha256_json_prefixed(&serde_json::to_value(&event)?)?;
        let event_requirements = event_artifact_requirements(&event, &event_id)?;
        let artifact_refs = event_requirements
            .iter()
            .map(|requirement| requirement.artifact_ref.clone())
            .collect::<Vec<_>>();

        for requirement in event_requirements {
            artifact_requirements
                .entry(requirement.artifact_ref.clone())
                .and_modify(|existing| existing.required_by_event_ids.push(event_id.clone()))
                .or_insert(requirement);
        }

        event_entries.push(ExportEvent {
            event_id,
            event_type: event_type_string(event.event_type)?,
            idempotency_key: event.idempotency_key,
            payload_hash: event.payload_hash,
            event_envelope_hash,
            event_file_hash: format!("sha256:{}", sha256_bytes_hex(&bytes)),
            artifact_refs,
        });
    }

    // A referenced artifact whose bytes are absent is only a fidelity defect when the
    // absence is unaccountable. An artifact carrying a recorded `ArtifactRemoved` claim
    // is legitimately gone — compact only ever erases claim-covered blobs
    // (`artifact_removal`'s sweep floor), so claim-presence exactly characterizes a
    // deliberate removal. Such an artifact has no bytes to transfer: omit it, don't
    // diagnose it. The referencing event and the `ArtifactRemoved` claim still export,
    // and the reader-relative operative-status layer renders it as removed downstream.
    let removal = ArtifactRemovalProjection::from_events(&all_events)?;
    let mut artifacts = Vec::new();
    for requirement in artifact_requirements.into_values() {
        match read_required_artifact(store_dir, &requirement)? {
            Some(artifact) => artifacts.push(artifact),
            None => {
                let removed = requirement
                    .content_hash
                    .as_deref()
                    .is_some_and(|content_hash| removal.is_removed(content_hash));
                if !removed {
                    diagnostics.push(ExportManifestDiagnostic {
                        code: "missing_referenced_artifact".to_owned(),
                        artifact_ref: requirement.artifact_ref,
                        event_id: requirement
                            .required_by_event_ids
                            .first()
                            .expect("requirement has at least one event id")
                            .clone(),
                    });
                }
            }
        }
    }

    Ok(ExportManifest {
        schema: EXPORT_MANIFEST_SCHEMA.to_owned(),
        version: EXPORT_MANIFEST_VERSION,
        fidelity_status: if diagnostics.is_empty() {
            ExportFidelityStatus::Full
        } else {
            ExportFidelityStatus::Incomplete
        },
        events: event_entries,
        artifacts,
        diagnostics,
    })
}

pub(crate) fn import_store_bundle(
    source_store_dir: impl AsRef<Path>,
    target_store_dir: impl AsRef<Path>,
) -> Result<ImportBundleResult> {
    import_store_bundle_with_verification(
        source_store_dir,
        target_store_dir,
        EventVerificationPolicy::advisory(),
        TrustSet::default(),
    )
}

pub(crate) fn import_store_bundle_with_verification(
    source_store_dir: impl AsRef<Path>,
    target_store_dir: impl AsRef<Path>,
    verification_policy: EventVerificationPolicy,
    trust_set: TrustSet,
) -> Result<ImportBundleResult> {
    let source_store_dir = source_store_dir.as_ref();
    let target_store_dir = target_store_dir.as_ref();
    // The fold tolerates an incomplete source: a referenced artifact absent from
    // disk (with no `ArtifactRemoved` claim to explain it) is the source's existing
    // state — an old snapshot compacted or migrated away. Carrying its referencing
    // events forward preserves that state without introducing NEW corruption, so the
    // fold proceeds and discloses the absent count rather than refusing. Only the
    // present artifacts land (`read_source_artifacts` reads `manifest.artifacts`,
    // which never lists an absent one); every event still folds.
    let manifest = build_export_manifest(source_store_dir)?;
    let absent_artifact_count = absent_artifact_count(&manifest);

    let events = read_source_events(source_store_dir)?;
    let source_events = events
        .iter()
        .map(|source| source.event.clone())
        .collect::<Vec<_>>();
    let verification = verify_events_for_ingest(&source_events, verification_policy, &trust_set)?;
    let artifacts = read_source_artifacts(source_store_dir, &manifest)?;
    let target_event_store = EventStore::open(target_store_dir);
    preflight_event_conflicts(&target_event_store, &events)?;

    let (artifacts_created, artifacts_existing) = commit_artifacts(target_store_dir, &artifacts)?;
    let (events_created, events_existing) = commit_events(&target_event_store, &events)?;
    rebuild_target_state(target_store_dir, &target_event_store)?;

    Ok(ImportBundleResult {
        events_created,
        events_existing,
        artifacts_created,
        artifacts_existing,
        absent_artifact_count,
        verification,
        commit_order: vec![
            ImportCommitStep::Artifacts,
            ImportCommitStep::Events,
            ImportCommitStep::State,
        ],
    })
}

/// Referenced artifacts the manifest could not resolve to bytes — the
/// `missing_referenced_artifact` diagnostics. A claim-covered absence is never a
/// diagnostic, so it never counts here; this is the unaccountable-absence tally the
/// fold discloses.
fn absent_artifact_count(manifest: &ExportManifest) -> usize {
    manifest
        .diagnostics
        .iter()
        .filter(|diagnostic| diagnostic.code == "missing_referenced_artifact")
        .count()
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ImportPreview {
    pub fidelity_status: ExportFidelityStatus,
    pub events_created: usize,
    pub events_existing: usize,
    pub artifacts_created: usize,
    pub artifacts_existing: usize,
    /// Absent referenced artifacts the fold would carry events past without their
    /// bytes (mirrors `ImportBundleResult::absent_artifact_count`).
    pub absent_artifact_count: usize,
}

/// Run the fold preflight — `build_export_manifest` + `preflight_event_conflicts`
/// — and count what the fold WOULD create vs. already find present, without
/// committing anything to the target. Reuses the exact predicates
/// `import_store_bundle_with_verification` runs, so the preview cannot drift from
/// the real fold: it tolerates an incomplete source the same way (disclosing the
/// absent-artifact count rather than refusing), and surfaces the same
/// `event conflict` message for a divergent same-key payload.
pub(crate) fn preview_import_store_bundle(
    source_store_dir: impl AsRef<Path>,
    target_store_dir: impl AsRef<Path>,
) -> Result<ImportPreview> {
    let source_store_dir = source_store_dir.as_ref();
    let target_store_dir = target_store_dir.as_ref();

    let manifest = build_export_manifest(source_store_dir)?;
    let absent_artifact_count = absent_artifact_count(&manifest);

    let events = read_source_events(source_store_dir)?;
    let target_event_store = EventStore::open(target_store_dir);
    preflight_event_conflicts(&target_event_store, &events)?;

    let mut events_created = 0;
    let mut events_existing = 0;
    for source in &events {
        let path = target_event_store.event_path_for_idempotency_key(&source.event.idempotency_key);
        if path.exists() {
            events_existing += 1;
        } else {
            events_created += 1;
        }
    }

    let artifacts = read_source_artifacts(source_store_dir, &manifest)?;
    let mut artifacts_created = 0;
    let mut artifacts_existing = 0;
    for artifact in &artifacts {
        let path = target_store_dir.join(&artifact.relative_path);
        match std::fs::read(&path) {
            Ok(existing_bytes) => {
                if existing_bytes != artifact.bytes {
                    return Err(ShoreError::Message(format!(
                        "artifact conflict for {}",
                        artifact.relative_path.display()
                    )));
                }
                artifacts_existing += 1;
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => artifacts_created += 1,
            Err(error) => {
                return Err(ShoreError::Message(format!(
                    "read target artifact {} for import preview: {error}",
                    artifact.relative_path.display()
                )));
            }
        }
    }

    Ok(ImportPreview {
        fidelity_status: manifest.fidelity_status,
        events_created,
        events_existing,
        artifacts_created,
        artifacts_existing,
        absent_artifact_count,
    })
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct SourceSubsetVerification {
    pub verified_events: usize,
    pub verified_artifacts: usize,
}

/// Independently re-verify, from disk, that everything durable in
/// `source_store_dir` is present in `target_store_dir`. Every physical file
/// under the source's `events/` and `artifacts/` (recursively) must be covered:
///
/// - **Events** are matched by store-relative path: the target must hold a file
///   at the same path, canonically identical **modulo the `ingest` provenance
///   stamp** (the import deliberately stamps `ingest` onto committed target
///   events, so raw byte identity cannot hold there; everything else, including
///   the envelope and any signature, must match). Event filenames are stable.
/// - **Artifacts** are matched by **content**, not path: the target must hold
///   some file under `artifacts/` with byte-identical content, regardless of
///   filename. The fold re-canonicalizes object artifacts to content-hash
///   filenames, so a source file the opaque-identity migration left under a
///   legacy name (`filename != sha256(bytes)`) has no counterpart at its own
///   relative path in the target — but its bytes are safe there under a
///   different name. Content is the artifact's real identity; comparing by
///   filename false-negatives whenever a name drifts.
///
/// Enumerates the source's physical directories — never a manifest or
/// projection, which cannot see orphan/unreferenced files — because this gate
/// fronts an irreversible delete: a source artifact whose bytes are in NO
/// target file (an orphan the fold never carried, or a corrupt file) is a real
/// divergence and still blocks. Only in-flight `*.tmp` files are excluded; the
/// regenerable store-root `state.json` sits outside the walked trees and a
/// nested file merely named `state.json` is verified like any other. Never
/// consults import counters; re-reads both stores.
pub(crate) fn verify_source_subset_of_target(
    source_store_dir: &Path,
    target_store_dir: &Path,
) -> Result<SourceSubsetVerification> {
    let mut relative_paths = Vec::new();
    for top in ["events", "artifacts"] {
        collect_durable_files(
            &source_store_dir.join(top),
            &PathBuf::from(top),
            &mut relative_paths,
        )?;
    }

    // The fold renames object artifacts to content-hash filenames, so a source
    // artifact will not sit at the same relative path in the target. Index the
    // target's artifact bytes by content hash once and verify artifacts by
    // content; events keep stable filenames and are verified path-by-path.
    let target_artifact_hashes = collect_target_artifact_hashes(target_store_dir)?;

    let mut verified_events = 0usize;
    let mut verified_artifacts = 0usize;
    let mut divergences: Vec<String> = Vec::new();
    for relative in &relative_paths {
        let source_bytes = std::fs::read(source_store_dir.join(relative)).map_err(|error| {
            ShoreError::Message(format!(
                "read source store file {} for subset verification: {error}",
                relative.display()
            ))
        })?;
        if relative.starts_with("events") {
            match std::fs::read(target_store_dir.join(relative)) {
                Ok(target_bytes) => {
                    if events_match_modulo_ingest_stamp(&source_bytes, &target_bytes)? {
                        verified_events += 1;
                    } else {
                        divergences.push(format!("{} diverges", store_relative_display(relative)));
                    }
                }
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                    divergences.push(format!(
                        "{} is missing in the target",
                        store_relative_display(relative)
                    ));
                }
                Err(error) => {
                    return Err(ShoreError::Message(format!(
                        "read target store file {} for subset verification: {error}",
                        relative.display()
                    )));
                }
            }
        } else if target_artifact_hashes.contains(&sha256_bytes_hex(&source_bytes)) {
            verified_artifacts += 1;
        } else {
            divergences.push(format!(
                "{} is missing in the target",
                store_relative_display(relative)
            ));
        }
    }

    if divergences.is_empty() {
        return Ok(SourceSubsetVerification {
            verified_events,
            verified_artifacts,
        });
    }
    let shown = divergences.iter().take(3).cloned().collect::<Vec<_>>();
    Err(ShoreError::Message(format!(
        "the source store is not a verified subset of the target ({} of {} files diverge; \
         {}); nothing was deleted — the source store is left untouched",
        divergences.len(),
        relative_paths.len(),
        shown.join("; ")
    )))
}

/// Index every durable file under the target's `artifacts/` tree by the hex
/// SHA-256 of its bytes. Verifying artifacts by content rather than filename
/// tolerates the fold's re-canonicalization of object files to content-hash
/// names (a source file the opaque-identity migration left under a legacy name
/// is still covered when its bytes live in the target under a different name).
/// A physical walk — never a manifest — so orphan/unreferenced target bytes are
/// counted too; a missing `artifacts/` directory yields an empty index (then
/// every source artifact diverges, as it must).
fn collect_target_artifact_hashes(target_store_dir: &Path) -> Result<HashSet<String>> {
    let mut relative_paths = Vec::new();
    collect_durable_files(
        &target_store_dir.join("artifacts"),
        &PathBuf::from("artifacts"),
        &mut relative_paths,
    )?;
    let mut hashes = HashSet::with_capacity(relative_paths.len());
    for relative in &relative_paths {
        let bytes = std::fs::read(target_store_dir.join(relative)).map_err(|error| {
            ShoreError::Message(format!(
                "read target artifact {} for subset verification: {error}",
                relative.display()
            ))
        })?;
        hashes.insert(sha256_bytes_hex(&bytes));
    }
    Ok(hashes)
}

/// Render a store-relative path with `/` separators on every platform: these
/// strings name store entries (`events/<hash>.json`), not filesystem paths,
/// and the divergence messages must read the same on Windows.
fn store_relative_display(path: &Path) -> String {
    path.components()
        .map(|component| component.as_os_str().to_string_lossy())
        .collect::<Vec<_>>()
        .join("/")
}

/// Compare two stored event files canonically, ignoring only the `ingest`
/// provenance stamp on either side — the one envelope field the import adds by
/// design. Unparseable bytes on either side compare as a divergence rather
/// than an error: an irreversible retire must treat a corrupt file as
/// unverified, never as ignorable.
fn events_match_modulo_ingest_stamp(source_bytes: &[u8], target_bytes: &[u8]) -> Result<bool> {
    let (Ok(mut source), Ok(mut target)) = (
        serde_json::from_slice::<serde_json::Value>(source_bytes),
        serde_json::from_slice::<serde_json::Value>(target_bytes),
    ) else {
        return Ok(false);
    };
    for value in [&mut source, &mut target] {
        if let Some(map) = value.as_object_mut() {
            map.remove("ingest");
        }
    }
    Ok(sha256_json_prefixed(&source)? == sha256_json_prefixed(&target)?)
}

/// Recursively collect the durable files under `dir` as store-relative paths,
/// skipping only in-flight `*.tmp` files. The regenerable store-root
/// `state.json` needs no filename rule: the walk roots at `events/` and
/// `artifacts/`, so the root projection is never enumerated — and a file
/// merely NAMED `state.json` nested inside those trees is durable bytes that
/// must be verified like any other (a filename skip here would let a retire
/// delete it unverified). A missing directory contributes zero files (the
/// walk is total).
fn collect_durable_files(dir: &Path, relative: &Path, collected: &mut Vec<PathBuf>) -> Result<()> {
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => {
            return Err(ShoreError::Message(format!(
                "read store directory {} for subset verification: {error}",
                dir.display()
            )));
        }
    };
    for entry in entries {
        let entry = entry.map_err(|error| {
            ShoreError::Message(format!(
                "read store directory entry under {} for subset verification: {error}",
                dir.display()
            ))
        })?;
        let name = entry.file_name();
        let child_relative = relative.join(&name);
        if entry.path().is_dir() {
            collect_durable_files(&entry.path(), &child_relative, collected)?;
        } else {
            if name.to_string_lossy().ends_with(".tmp") {
                continue;
            }
            collected.push(child_relative);
        }
    }
    Ok(())
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct SourceEvent {
    event: ShoreEvent,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct SourceArtifact {
    relative_path: PathBuf,
    bytes: Vec<u8>,
}

fn read_source_events(source_store_dir: &Path) -> Result<Vec<SourceEvent>> {
    let event_store = EventStore::open(source_store_dir);
    event_file_paths(source_store_dir)?
        .into_iter()
        .map(|path| {
            Ok(SourceEvent {
                event: event_store.read_event(&path)?,
            })
        })
        .collect()
}

fn read_source_artifacts(
    source_store_dir: &Path,
    manifest: &ExportManifest,
) -> Result<Vec<SourceArtifact>> {
    manifest
        .artifacts
        .iter()
        .map(|artifact| match artifact.artifact_kind {
            ExportArtifactKind::Object => read_source_object_artifact(source_store_dir, artifact),
            ExportArtifactKind::NoteBody => {
                read_source_note_body_artifact(source_store_dir, artifact)
            }
        })
        .collect()
}

fn read_source_object_artifact(
    source_store_dir: &Path,
    artifact: &ExportArtifact,
) -> Result<SourceArtifact> {
    let Some((path, parsed)) =
        find_object_artifact(source_store_dir, None, &artifact.content_hash)?
    else {
        return Err(ShoreError::Message(format!(
            "missing object artifact {}",
            artifact.artifact_ref
        )));
    };
    if parsed.content_hash != artifact.content_hash {
        return Err(ShoreError::Message(format!(
            "object artifact content hash mismatch for {}",
            artifact.artifact_ref
        )));
    }

    Ok(SourceArtifact {
        relative_path: object_relative_path(&artifact.content_hash),
        bytes: std::fs::read(&path).map_err(|error| {
            ShoreError::Message(format!("failed to read source object artifact: {error}"))
        })?,
    })
}

fn read_source_note_body_artifact(
    source_store_dir: &Path,
    artifact: &ExportArtifact,
) -> Result<SourceArtifact> {
    let relative_path = note_body_relative_path(&artifact.artifact_ref);
    validate_relative_note_body_path(&relative_path)?;
    let path = source_store_dir.join(&relative_path);
    let bytes = std::fs::read(&path).map_err(|error| {
        ShoreError::Message(format!("failed to read source note body artifact: {error}"))
    })?;
    let parsed: NoteBodyEnvelope = serde_json::from_slice(&bytes)?;
    let content_hash = format!("sha256:{}", sha256_bytes_hex(parsed.body.as_bytes()));
    if parsed.schema != artifact.schema
        || parsed.version != artifact.version
        || content_hash != artifact.content_hash
    {
        return Err(ShoreError::Message(format!(
            "note body artifact content mismatch for {}",
            artifact.artifact_ref
        )));
    }

    Ok(SourceArtifact {
        relative_path,
        bytes,
    })
}

fn preflight_event_conflicts(target_store: &EventStore, events: &[SourceEvent]) -> Result<()> {
    for source in events {
        let path = target_store.event_path_for_idempotency_key(&source.event.idempotency_key);
        if !path.exists() {
            continue;
        }

        let existing = target_store.read_event(&path)?;
        if existing.payload_hash != source.event.payload_hash {
            return Err(ShoreError::Message(format!(
                "event conflict for idempotency key {}",
                source.event.idempotency_key
            )));
        }
    }

    Ok(())
}

fn commit_artifacts(
    target_store_dir: &Path,
    artifacts: &[SourceArtifact],
) -> Result<(usize, usize)> {
    let storage = LocalStorage::new(target_store_dir);
    let mut created = 0;
    let mut existing = 0;

    for artifact in artifacts {
        match storage.create_file_exclusive(
            &artifact.relative_path,
            &artifact.bytes,
            Durability::Durable,
        )? {
            CreateOutcome::Created => created += 1,
            CreateOutcome::AlreadyExists => {
                let existing_bytes = storage.read_bytes(&artifact.relative_path)?;
                if existing_bytes != artifact.bytes {
                    return Err(ShoreError::Message(format!(
                        "artifact conflict for {}",
                        artifact.relative_path.display()
                    )));
                }
                existing += 1;
            }
        }
    }

    Ok((created, existing))
}

fn commit_events(target_store: &EventStore, events: &[SourceEvent]) -> Result<(usize, usize)> {
    let source_events: Vec<ShoreEvent> = events.iter().map(|source| source.event.clone()).collect();
    let stamped =
        stamp_ingest_provenance(&source_events, IngestVia::BundleApply, &current_timestamp());
    let mut created = 0;
    let mut existing = 0;

    for event in &stamped {
        match target_store.record_event_once(event)? {
            crate::session::EventWriteOutcome::Created => created += 1,
            crate::session::EventWriteOutcome::Existing
            | crate::session::EventWriteOutcome::ExistingDivergentSignature => existing += 1,
        }
    }

    Ok((created, existing))
}

fn rebuild_target_state(target_store_dir: &Path, target_store: &EventStore) -> Result<()> {
    let events = target_store.list_events()?;
    let state = SessionState::from_events(&events)?;
    LocalStorage::new(target_store_dir).write_json_atomic(
        Path::new("state.json"),
        &state,
        Durability::Projection,
    )
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct ArtifactRequirement {
    artifact_ref: String,
    kind: ExportArtifactKind,
    locator: ArtifactLocator,
    content_hash: Option<String>,
    required_by_event_ids: Vec<String>,
}

fn insert_artifact_requirement(
    refs: &mut BTreeMap<String, ArtifactRequirement>,
    requirement: ArtifactRequirement,
) -> Result<()> {
    if let Some(existing) = refs.get(&requirement.artifact_ref) {
        if existing.kind == requirement.kind
            && existing.locator == requirement.locator
            && existing.content_hash == requirement.content_hash
        {
            return Ok(());
        }
        return Err(ShoreError::Message(format!(
            "conflicting artifact reference for {}",
            requirement.artifact_ref
        )));
    }
    refs.insert(requirement.artifact_ref.clone(), requirement);
    Ok(())
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum ArtifactLocator {
    Object { object_id: String },
    NoteBody { relative_path: PathBuf },
}

fn event_file_paths(store_dir: &Path) -> Result<Vec<PathBuf>> {
    let events_dir = store_dir.join("events");
    let entries = match std::fs::read_dir(&events_dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(error) => {
            return Err(ShoreError::Message(format!(
                "failed to list events for export manifest: {error}"
            )));
        }
    };

    let mut paths = entries
        .map(|entry| {
            entry.map(|entry| entry.path()).map_err(|error| {
                ShoreError::Message(format!("failed to read event entry: {error}"))
            })
        })
        .collect::<Result<Vec<_>>>()?;
    paths.retain(|path| is_event_file(path));
    paths.sort();
    Ok(paths)
}

fn is_event_file(path: &Path) -> bool {
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.len() == 69 && name.ends_with(".json"))
}

fn event_artifact_requirements(
    event: &ShoreEvent,
    event_id: &str,
) -> Result<Vec<ArtifactRequirement>> {
    let mut refs = BTreeMap::new();
    if event.event_type == EventType::WorkObjectProposed
        && let Some(work_object) = event.payload.get("workObject")
        && let Some(revision) = work_object.get("revision")
        && let (Some(object_id), Some(content_hash)) = (
            revision.get("objectId").and_then(|value| value.as_str()),
            work_object
                .get("objectArtifactContentHash")
                .and_then(|value| value.as_str()),
        )
    {
        insert_artifact_requirement(
            &mut refs,
            ArtifactRequirement {
                artifact_ref: format!("{}:{content_hash}", id_prefix::ARTIFACT_OBJECT),
                kind: ExportArtifactKind::Object,
                locator: ArtifactLocator::Object {
                    object_id: object_id.to_owned(),
                },
                content_hash: Some(content_hash.to_owned()),
                required_by_event_ids: vec![event_id.to_owned()],
            },
        )?;
    }

    for path in note_body_artifact_paths_for_event(event.event_type, &event.payload) {
        if let Some(stem) = note_body_hash_from_path(path) {
            let artifact_ref = format!("{}:sha256:{stem}", id_prefix::NOTE_BODY);
            insert_artifact_requirement(
                &mut refs,
                ArtifactRequirement {
                    artifact_ref: artifact_ref.clone(),
                    kind: ExportArtifactKind::NoteBody,
                    locator: ArtifactLocator::NoteBody {
                        relative_path: note_body_relative_path(&artifact_ref),
                    },
                    content_hash: Some(format!("sha256:{stem}")),
                    required_by_event_ids: vec![event_id.to_owned()],
                },
            )?;
        }
    }
    Ok(refs.into_values().collect())
}

fn note_body_artifact_paths_for_event(
    event_type: EventType,
    payload: &serde_json::Value,
) -> Vec<&str> {
    // Derived from the shared registry, so a new body-bearing family cannot be
    // silently dropped here (the former `_ => Vec::new()` wildcard did exactly
    // that until a family was added on both paths).
    match body_artifact_field(event_type) {
        Some(field) => optional_payload_path(payload, field.payload_field()),
        None => Vec::new(),
    }
}

fn optional_payload_path<'a>(payload: &'a serde_json::Value, field: &str) -> Vec<&'a str> {
    payload
        .get(field)
        .and_then(|value| value.as_str())
        .into_iter()
        .collect()
}

fn note_body_hash_from_path(path: &str) -> Option<&str> {
    path.strip_prefix("artifacts/notes/")
        .and_then(|path| path.strip_suffix(".json"))
        .filter(|stem| stem.len() == 64 && stem.bytes().all(|byte| byte.is_ascii_hexdigit()))
}

fn note_body_relative_path(artifact_ref: &str) -> PathBuf {
    let stem = artifact_ref
        .strip_prefix("note-body:sha256:")
        .unwrap_or_default();
    PathBuf::from("artifacts")
        .join("notes")
        .join(format!("{stem}.json"))
}

fn read_required_artifact(
    store_dir: &Path,
    requirement: &ArtifactRequirement,
) -> Result<Option<ExportArtifact>> {
    match &requirement.locator {
        ArtifactLocator::Object { object_id } => {
            read_object_export_artifact(store_dir, object_id, requirement)
        }
        ArtifactLocator::NoteBody { relative_path } => {
            read_note_body_export_artifact(store_dir, relative_path, requirement)
        }
    }
}

fn read_object_export_artifact(
    store_dir: &Path,
    object_id: &str,
    requirement: &ArtifactRequirement,
) -> Result<Option<ExportArtifact>> {
    let content_hash = requirement
        .content_hash
        .as_deref()
        .ok_or_else(|| ShoreError::Message("object artifact missing content hash".to_owned()))?;
    let Some((path, artifact)) = find_object_artifact(store_dir, Some(object_id), content_hash)?
    else {
        return Ok(None);
    };
    let byte_size = file_size(&path)?;

    Ok(Some(ExportArtifact {
        artifact_ref: requirement.artifact_ref.clone(),
        artifact_kind: requirement.kind,
        schema: artifact.schema,
        version: artifact.version,
        content_hash: artifact.content_hash,
        byte_size,
        required_by_event_ids: requirement.required_by_event_ids.clone(),
    }))
}

fn object_relative_path(content_hash: &str) -> PathBuf {
    PathBuf::from("artifacts")
        .join("objects")
        .join(format!("{}.json", content_hash_file_stem(content_hash)))
}

fn find_object_artifact(
    store_dir: &Path,
    object_id: Option<&str>,
    content_hash: &str,
) -> Result<Option<(PathBuf, ObjectArtifact)>> {
    let artifact_dir = store_dir.join("artifacts/objects");
    let direct_path = store_dir.join(object_relative_path(content_hash));
    if direct_path.exists() {
        let bytes = std::fs::read(&direct_path).map_err(|error| {
            ShoreError::Message(format!("failed to read object artifact: {error}"))
        })?;
        let artifact = decode_and_validate_object_artifact(&bytes)?;
        if artifact.content_hash == content_hash
            && object_id.is_none_or(|object_id| artifact.snapshot.object_id.as_str() == object_id)
        {
            return Ok(Some((direct_path, artifact)));
        }
    }

    let entries = match std::fs::read_dir(&artifact_dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(ShoreError::Message(format!(
                "failed to list object artifacts for export manifest: {error}"
            )));
        }
    };

    for entry in entries {
        let path = entry
            .map_err(|error| {
                ShoreError::Message(format!("failed to read object artifact entry: {error}"))
            })?
            .path();
        if path.extension().and_then(|extension| extension.to_str()) != Some("json") {
            continue;
        }

        let bytes = std::fs::read(&path).map_err(|error| {
            ShoreError::Message(format!("failed to read object artifact: {error}"))
        })?;
        let parsed: ObjectArtifact = serde_json::from_slice(&bytes)?;
        if parsed.content_hash == content_hash
            && object_id.is_none_or(|object_id| parsed.snapshot.object_id.as_str() == object_id)
        {
            let artifact = decode_and_validate_object_artifact(&bytes)?;
            return Ok(Some((path, artifact)));
        }
    }

    Ok(None)
}

fn content_hash_file_stem(content_hash: &str) -> String {
    content_hash
        .strip_prefix("sha256:")
        .filter(|stem| stem.len() == 64 && stem.bytes().all(|byte| byte.is_ascii_hexdigit()))
        .map(str::to_owned)
        .unwrap_or_else(|| sha256_bytes_hex(content_hash.as_bytes()))
}

fn read_note_body_export_artifact(
    store_dir: &Path,
    relative_path: &Path,
    requirement: &ArtifactRequirement,
) -> Result<Option<ExportArtifact>> {
    validate_relative_note_body_path(relative_path)?;
    let path = store_dir.join(relative_path);
    if !path.exists() {
        return Ok(None);
    }

    let bytes = std::fs::read(&path).map_err(|error| {
        ShoreError::Message(format!("failed to read note body artifact: {error}"))
    })?;
    let artifact: NoteBodyEnvelope = serde_json::from_slice(&bytes)?;
    if artifact.schema != "shore.note-body" || artifact.version != 1 {
        return Err(ShoreError::Message(format!(
            "unsupported note body artifact schema/version: {} v{}",
            artifact.schema, artifact.version
        )));
    }
    let content_hash = format!("sha256:{}", sha256_bytes_hex(artifact.body.as_bytes()));
    if requirement.content_hash.as_deref() != Some(content_hash.as_str()) {
        return Err(ShoreError::Message(format!(
            "note body artifact content hash mismatch for {}",
            requirement.artifact_ref
        )));
    }

    Ok(Some(ExportArtifact {
        artifact_ref: requirement.artifact_ref.clone(),
        artifact_kind: requirement.kind,
        schema: artifact.schema,
        version: artifact.version,
        content_hash,
        byte_size: bytes.len() as u64,
        required_by_event_ids: requirement.required_by_event_ids.clone(),
    }))
}

fn validate_relative_note_body_path(path: &Path) -> Result<()> {
    if path.components().any(|component| {
        matches!(
            component,
            Component::ParentDir | Component::RootDir | Component::Prefix(_)
        )
    }) || !path.starts_with("artifacts/notes")
    {
        return Err(ShoreError::Message(format!(
            "invalid note body artifact locator: {}",
            path.display()
        )));
    }

    Ok(())
}

fn file_size(path: &Path) -> Result<u64> {
    Ok(std::fs::metadata(path)
        .map_err(|error| ShoreError::Message(format!("failed to stat export artifact: {error}")))?
        .len())
}

fn event_type_string(event_type: EventType) -> Result<String> {
    Ok(serde_json::to_value(event_type)?
        .as_str()
        .expect("event type serializes as string")
        .to_owned())
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;
    use std::fs;
    use std::path::Path;
    use std::process::Command;

    use serde_json::json;

    use super::*;
    use crate::canonical_hash::{sha256_bytes_hex, sha256_json_prefixed};
    use crate::crypto::{EventVerificationStatus, SignerId};
    use crate::model::{
        EventId, JournalId, RevisionId, TrackId, ValidationCheckId, ValidationStatus,
        ValidationTarget, ValidationTrigger,
    };
    use crate::session::body_artifact::BODY_INLINE_LIMIT;
    use crate::session::event::{
        ArtifactRemovedPayload, AssertionMode, EventSignature, EventTarget, EventType,
        IngestProvenance, IngestVia, ShoreEvent, ValidationCheckRecordedPayload, Writer,
    };
    use crate::session::{
        CaptureOptions, EventStore, EventVerificationPolicy, ObservationAddOptions, TrustSet,
        capture_worktree_review, event_signature_trust_set, record_observation,
        verify_event_signature,
    };

    #[test]
    fn export_manifest_includes_events_and_object_artifacts() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let store = EventStore::open(resolved_store_dir(repo.path()));
        let capture_event = store
            .list_events()
            .unwrap()
            .into_iter()
            .find(|event| event.event_type == EventType::WorkObjectProposed)
            .expect("capture event");

        let manifest = build_export_manifest(resolved_store_dir(repo.path())).unwrap();

        assert_eq!(manifest.schema, "shore.store-export-manifest");
        assert_eq!(manifest.version, 1);
        assert_eq!(manifest.fidelity_status, ExportFidelityStatus::Full);
        // A worktree capture records the capture event plus the auto-recorded
        // capture-time ref association.
        assert_eq!(manifest.events.len(), 2);
        assert!(manifest.diagnostics.is_empty());

        let event = manifest
            .events
            .iter()
            .find(|event| event.event_type == "work_object_proposed")
            .expect("capture event in manifest");
        assert_eq!(event.event_id, capture_event.event_id.as_str());
        assert_eq!(event.event_type, "work_object_proposed");
        assert_eq!(event.idempotency_key, capture_event.idempotency_key);
        assert_eq!(event.payload_hash, capture_event.payload_hash);
        assert!(event.event_envelope_hash.starts_with("sha256:"));
        assert!(event.event_file_hash.starts_with("sha256:"));
        assert_ne!(event.event_envelope_hash, event.event_file_hash);

        let object_ref = format!("object:{}", capture.object_artifact_content_hash);
        assert_eq!(event.artifact_refs, vec![object_ref.clone()]);
        let artifact = manifest
            .artifacts
            .iter()
            .find(|artifact| artifact.artifact_ref == object_ref)
            .expect("object artifact");
        assert_eq!(artifact.artifact_kind, ExportArtifactKind::Object);
        assert_eq!(artifact.schema, "shore.object");
        assert_eq!(artifact.version, 2);
        assert_eq!(artifact.content_hash, capture.object_artifact_content_hash);
        assert!(artifact.byte_size > 0);
        assert_eq!(
            artifact.required_by_event_ids,
            vec![capture_event.event_id.as_str().to_owned()]
        );

        let json = serde_json::to_string(&manifest).unwrap();
        assert!(!json.contains(".shore/data"));
        assert!(!json.contains("artifacts/objects"));
        assert!(!json.contains("events/"));
    }

    #[test]
    fn export_manifest_includes_referenced_note_body_artifacts() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let body = "x".repeat(BODY_INLINE_LIMIT + 1);
        let observation = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Large body")
                .with_body(body),
        )
        .unwrap();

        let manifest = build_export_manifest(resolved_store_dir(repo.path())).unwrap();

        assert_eq!(manifest.fidelity_status, ExportFidelityStatus::Full);
        let body_hash = observation.body_content_hash.expect("body hash");
        let artifact_ref = format!("note-body:{body_hash}");
        let event = manifest
            .events
            .iter()
            .find(|event| event.event_id == observation.event_id.as_str())
            .expect("observation event");
        assert_eq!(event.artifact_refs, vec![artifact_ref.clone()]);

        let artifact = manifest
            .artifacts
            .iter()
            .find(|artifact| artifact.artifact_ref == artifact_ref)
            .expect("note body artifact");
        assert_eq!(artifact.artifact_kind, ExportArtifactKind::NoteBody);
        assert_eq!(artifact.schema, "shore.note-body");
        assert_eq!(artifact.version, 1);
        assert_eq!(artifact.content_hash, body_hash);
        assert!(artifact.byte_size > BODY_INLINE_LIMIT as u64);
        assert_eq!(
            artifact.required_by_event_ids,
            vec![observation.event_id.as_str().to_owned()]
        );

        let json = serde_json::to_string(&manifest).unwrap();
        assert!(!json.contains("artifacts/notes"));
        assert!(!json.contains("Large body"));
    }

    #[test]
    fn store_bundle_import_preserves_externalized_validation_summary_artifacts() {
        let source = tempfile::tempdir().unwrap();
        let source_store_dir = source.path().join(".shore/data");
        let summary = "validation summary\n".repeat(BODY_INLINE_LIMIT);
        let (summary_artifact_path, summary_content_hash, summary_byte_size) =
            write_note_body_artifact(&source_store_dir, summary.clone());
        let validation_event = validation_event_with_summary_artifact(
            &summary_artifact_path,
            &summary_content_hash,
            summary_byte_size,
        );
        let validation_event_id = validation_event.event_id.as_str().to_owned();
        write_event_to_store(&source_store_dir, validation_event);

        let manifest = build_export_manifest(&source_store_dir).unwrap();

        assert_eq!(manifest.fidelity_status, ExportFidelityStatus::Full);
        let artifact_ref = format!("note-body:{summary_content_hash}");
        let event = manifest
            .events
            .iter()
            .find(|event| event.event_id == validation_event_id)
            .expect("validation event");
        assert_eq!(event.artifact_refs, vec![artifact_ref.clone()]);
        assert!(
            manifest
                .artifacts
                .iter()
                .any(|artifact| artifact.artifact_ref == artifact_ref)
        );

        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");
        import_store_bundle(&source_store_dir, &target_store_dir).unwrap();

        let imported_bytes = fs::read(target_store_dir.join(&summary_artifact_path)).unwrap();
        let imported: NoteBodyEnvelope = serde_json::from_slice(&imported_bytes).unwrap();
        assert_eq!(imported.body, summary);
    }

    #[test]
    fn artifact_refs_ignore_unenumerated_artifact_path_payload_fields() {
        let path = note_body_path_for_hash("0".repeat(64));
        let mut event = review_initialized_event("schema-enumeration", 1);
        event.payload = json!({
            "unexpectedArtifactPath": path,
            "nested": {
                "anotherArtifactPath": note_body_path_for_hash("1".repeat(64))
            }
        });

        let refs = event_artifact_requirements(&event, event.event_id.as_str()).unwrap();
        assert_eq!(refs, Vec::<ArtifactRequirement>::new());
    }

    #[test]
    fn artifact_refs_collect_enumerated_note_body_payload_fields() {
        let path = note_body_path_for_hash("2".repeat(64));
        let mut event = review_initialized_event("known-note-body-field", 1);
        event.event_type = EventType::ReviewObservationRecorded;
        event.payload = json!({
            "bodyArtifactPath": path,
        });

        let refs = event_artifact_requirements(&event, event.event_id.as_str())
            .unwrap()
            .into_iter()
            .map(|requirement| requirement.artifact_ref)
            .collect::<Vec<_>>();
        assert_eq!(refs, vec![format!("note-body:sha256:{}", "2".repeat(64))]);
    }

    #[test]
    fn every_registry_body_family_yields_a_note_body_requirement() {
        use crate::session::store::body_artifact::body_artifact_field;

        let hash = "3".repeat(64);
        let path = note_body_path_for_hash(hash.clone());

        for event_type in EventType::ALL {
            let Some(field) = body_artifact_field(event_type) else {
                continue;
            };
            let field_name = field.payload_field(); // the wire field the registry names
            let mut event = review_initialized_event("registry-agreement", 1);
            event.event_type = event_type;
            event.payload = json!({ field_name: path });

            let refs = event_artifact_requirements(&event, event.event_id.as_str())
                .unwrap()
                .into_iter()
                .map(|requirement| requirement.artifact_ref)
                .collect::<Vec<_>>();
            assert_eq!(
                refs,
                vec![format!("{}:sha256:{hash}", id_prefix::NOTE_BODY)],
                "path 2 dropped the body artifact for {event_type:?}"
            );
        }
    }

    #[test]
    fn export_manifest_marks_missing_artifacts_as_not_full_fidelity() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        remove_object_artifacts(repo.path());

        let manifest = build_export_manifest(resolved_store_dir(repo.path())).unwrap();

        assert_eq!(manifest.fidelity_status, ExportFidelityStatus::Incomplete);
        assert_eq!(manifest.artifacts.len(), 0);
        assert_eq!(manifest.diagnostics.len(), 1);
        assert_eq!(manifest.diagnostics[0].code, "missing_referenced_artifact");
        assert!(
            manifest.events.iter().any(|event| event
                .artifact_refs
                .iter()
                .any(|artifact_ref| artifact_ref.starts_with("object:sha256:"))),
            "the capture event references the object artifact"
        );
    }

    #[test]
    fn strict_import_treats_same_event_payload_as_noop() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        let first =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();
        let second =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();

        // The capture event plus the auto-recorded ref association.
        assert_eq!(first.events_created, 2);
        assert_eq!(first.events_existing, 0);
        assert_eq!(first.artifacts_created, 1);
        assert_eq!(second.events_created, 0);
        assert_eq!(second.events_existing, 2);
        assert_eq!(second.artifacts_created, 0);
        assert_eq!(second.artifacts_existing, 1);
    }

    #[test]
    fn bundle_import_uses_verification_policy_before_event_commit() {
        let source = tempfile::tempdir().unwrap();
        let target = tempfile::tempdir().unwrap();
        let source_store_dir = source.path().join(".shore/data");
        let target_store_dir = target.path().join(".shore/data");
        write_event_to_store(
            &source_store_dir,
            invalid_signed_review_initialized_event("signed-invalid"),
        );

        let advisory = import_store_bundle_with_verification(
            &source_store_dir,
            &target_store_dir,
            EventVerificationPolicy::advisory(),
            TrustSet::default(),
        )
        .unwrap();

        assert_eq!(advisory.events_created, 1);
        assert_eq!(
            advisory.verification[0].status,
            EventVerificationStatus::Invalid
        );

        let strict_target = tempfile::tempdir().unwrap();
        let strict_target_store_dir = strict_target.path().join(".shore/data");
        let error = import_store_bundle_with_verification(
            &source_store_dir,
            &strict_target_store_dir,
            EventVerificationPolicy::integrity_strict(),
            TrustSet::default(),
        )
        .expect_err("integrity-strict rejects invalid signature");

        assert!(
            error.to_string().contains("invalid"),
            "unexpected error: {error}"
        );
        assert!(
            !strict_target_store_dir.join("events").exists()
                || EventStore::open(&strict_target_store_dir)
                    .list_events()
                    .unwrap()
                    .is_empty()
        );
    }

    #[test]
    fn strict_import_rejects_same_idempotency_key_with_different_payload() {
        let source_one = tempfile::tempdir().unwrap();
        let source_two = tempfile::tempdir().unwrap();
        let target = tempfile::tempdir().unwrap();
        let idempotency_key = "review_initialized:session:default:work:default";
        write_event_to_store(
            &source_one.path().join(".shore/data"),
            review_initialized_event(idempotency_key, 1),
        );
        write_event_to_store(
            &source_two.path().join(".shore/data"),
            review_initialized_event(idempotency_key, 2),
        );
        import_store_bundle(
            source_one.path().join(".shore/data"),
            target.path().join(".shore/data"),
        )
        .unwrap();

        let error = import_store_bundle(
            source_two.path().join(".shore/data"),
            target.path().join(".shore/data"),
        )
        .expect_err("conflicting event is rejected");

        assert!(error.to_string().contains("event conflict"));
        let stored = EventStore::open(target.path().join(".shore/data"))
            .list_events()
            .unwrap();
        assert_eq!(stored.len(), 1);
        assert_eq!(stored[0].payload["value"], json!(1));
    }

    #[test]
    fn import_tolerates_an_absent_referenced_artifact_and_discloses_the_count() {
        // A referenced artifact absent with NO removal claim is the source's existing
        // state (an old snapshot GC'd/migrated away). The fold no longer refuses: it
        // carries the referencing events forward and discloses the absent count.
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        remove_object_artifacts(repo.path());
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        let result =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();

        // The events fold; the absent object is not carried but is disclosed.
        assert!(result.events_created >= 1);
        assert_eq!(result.artifacts_created, 0);
        assert_eq!(result.absent_artifact_count, 1);
        assert!(
            !EventStore::open(&target_store_dir)
                .list_events()
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn strict_import_commits_artifacts_before_events_and_rebuilds_state() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        fs::write(
            resolved_store_dir(repo.path()).join("state.json"),
            r#"{"sourceState":"must not be imported as authority"}"#,
        )
        .unwrap();
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        let result =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();

        assert_eq!(
            result.commit_order,
            vec![
                ImportCommitStep::Artifacts,
                ImportCommitStep::Events,
                ImportCommitStep::State,
            ]
        );
        assert!(target_store_dir.join("artifacts/objects").is_dir());
        assert!(target_store_dir.join("events").is_dir());
        let rebuilt_state = fs::read_to_string(target_store_dir.join("state.json")).unwrap();
        assert!(!rebuilt_state.contains("must not be imported"));
        assert!(rebuilt_state.contains("journalId"));
    }

    #[test]
    fn bundle_apply_stamps_bundle_apply_provenance_on_target_events() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();

        let stored = EventStore::open(&target_store_dir).list_events().unwrap();
        assert!(!stored.is_empty());
        for event in &stored {
            let stamp = event
                .ingest
                .as_ref()
                .expect("every bundle-applied event is stamped");
            assert_eq!(stamp.via, IngestVia::BundleApply);
            assert!(stamp.received_at.starts_with("unix-ms:"));
        }
        // The source store is never modified.
        let source = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        assert!(source.iter().all(|event| event.ingest.is_none()));
    }

    #[test]
    fn bundle_apply_overwrites_inbound_ingest_stamp() {
        // The source store's copy carries its own importer's stamp; the target
        // re-stamps with via: bundle-apply — foreign bookkeeping is not a fact.
        let source = tempfile::tempdir().unwrap();
        let target = tempfile::tempdir().unwrap();
        let mut event = review_initialized_event("inbound-stamped", 1);
        event.ingest = Some(IngestProvenance {
            via: IngestVia::IngestEvents,
            received_at: "unix-ms:1".to_owned(),
        });
        write_event_to_store(&source.path().join(".shore/data"), event);

        import_store_bundle(
            source.path().join(".shore/data"),
            target.path().join(".shore/data"),
        )
        .unwrap();

        let stored = EventStore::open(target.path().join(".shore/data"))
            .list_events()
            .unwrap();
        let stamp = stored[0].ingest.as_ref().unwrap();
        assert_eq!(stamp.via, IngestVia::BundleApply);
        assert_ne!(stamp.received_at, "unix-ms:1");
    }

    #[test]
    fn bundle_apply_reimport_keeps_first_stamp_and_reports_existing() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();
        let first_stamps: Vec<_> = EventStore::open(&target_store_dir)
            .list_events()
            .unwrap()
            .into_iter()
            .map(|event| event.ingest)
            .collect();
        assert!(first_stamps.iter().all(Option::is_some));

        let second =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();
        assert_eq!(second.events_created, 0);
        assert!(second.events_existing > 0);

        let second_stamps: Vec<_> = EventStore::open(&target_store_dir)
            .list_events()
            .unwrap()
            .into_iter()
            .map(|event| event.ingest)
            .collect();
        assert_eq!(second_stamps, first_stamps);
    }

    #[test]
    fn bundle_apply_preflight_and_signature_semantics_unaffected_by_stamps() {
        let source = tempfile::tempdir().unwrap();
        let target = tempfile::tempdir().unwrap();
        let signed: ShoreEvent = serde_json::from_str(include_str!(
            "../../../tests/fixtures/event_signatures/friendly-valid-event.json"
        ))
        .unwrap();
        write_event_to_store(&source.path().join(".shore/data"), signed);
        let trust = event_signature_trust_set(
            serde_json::from_str(include_str!(
                "../../../tests/fixtures/event_signatures/did-key-ed25519.json"
            ))
            .unwrap(),
        )
        .unwrap();

        let result = import_store_bundle_with_verification(
            source.path().join(".shore/data"),
            target.path().join(".shore/data"),
            EventVerificationPolicy::advisory(),
            trust.clone(),
        )
        .unwrap();
        assert_eq!(result.events_created, 1);
        assert_eq!(
            result.verification[0].status,
            EventVerificationStatus::Valid
        );

        // The stamped target copy still verifies valid.
        let stored = EventStore::open(target.path().join(".shore/data"))
            .list_events()
            .unwrap();
        assert!(stored[0].ingest.is_some());
        assert_eq!(
            verify_event_signature(&stored[0], &trust).unwrap(),
            EventVerificationStatus::Valid
        );

        // Preflight does not treat the stamp difference (target stamped,
        // source unstamped) as a conflict: payload_hash comparison only.
        let again = import_store_bundle_with_verification(
            source.path().join(".shore/data"),
            target.path().join(".shore/data"),
            EventVerificationPolicy::advisory(),
            trust,
        )
        .unwrap();
        assert_eq!(again.events_created, 0);
        assert_eq!(again.events_existing, 1);
    }

    #[test]
    fn preview_import_reports_to_create_counts_against_an_empty_target() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let source = resolved_store_dir(repo.path());
        let target_root = tempfile::tempdir().unwrap();
        let target = target_root.path().join(".shore/data");

        let preview = preview_import_store_bundle(&source, &target).unwrap();

        // A worktree capture records the capture event + the ref association, and
        // one object artifact — all absent from the fresh target.
        assert_eq!(preview.fidelity_status, ExportFidelityStatus::Full);
        assert_eq!(preview.events_created, 2);
        assert_eq!(preview.events_existing, 0);
        assert_eq!(preview.artifacts_created, 1);
        assert_eq!(preview.artifacts_existing, 0);
        // The preview writes nothing: the target store never materializes.
        assert!(!target.join("events").exists());
        assert!(!target.join("artifacts").exists());
    }

    #[test]
    fn preview_import_reports_existing_counts_after_a_real_import() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let source = resolved_store_dir(repo.path());
        let target_root = tempfile::tempdir().unwrap();
        let target = target_root.path().join(".shore/data");
        import_store_bundle(&source, &target).unwrap();

        let preview = preview_import_store_bundle(&source, &target).unwrap();

        assert_eq!(preview.events_created, 0);
        assert_eq!(preview.events_existing, 2);
        assert_eq!(preview.artifacts_created, 0);
        assert_eq!(preview.artifacts_existing, 1);
    }

    #[test]
    fn preview_import_tolerates_incomplete_fidelity_and_reports_the_absent_count() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        remove_object_artifacts(repo.path());
        let target_root = tempfile::tempdir().unwrap();
        let target = target_root.path().join(".shore/data");

        // Mirrors the real fold: no refusal, the absent object is disclosed, and the
        // preview still writes nothing.
        let preview =
            preview_import_store_bundle(resolved_store_dir(repo.path()), &target).unwrap();

        assert_eq!(preview.fidelity_status, ExportFidelityStatus::Incomplete);
        assert_eq!(preview.absent_artifact_count, 1);
        assert!(!target.join("events").exists());
    }

    #[test]
    fn preview_import_reports_an_event_conflict() {
        let source_one = tempfile::tempdir().unwrap();
        let source_two = tempfile::tempdir().unwrap();
        let target = tempfile::tempdir().unwrap();
        let key = "review_initialized:session:default:work:default";
        write_event_to_store(
            &source_one.path().join(".shore/data"),
            review_initialized_event(key, 1),
        );
        write_event_to_store(
            &source_two.path().join(".shore/data"),
            review_initialized_event(key, 2),
        );
        import_store_bundle(
            source_one.path().join(".shore/data"),
            target.path().join(".shore/data"),
        )
        .unwrap();

        let error = preview_import_store_bundle(
            source_two.path().join(".shore/data"),
            target.path().join(".shore/data"),
        )
        .expect_err("a divergent payload under the same key must conflict");

        assert!(error.to_string().contains("event conflict"), "{error}");
    }

    #[test]
    fn export_manifest_treats_a_removed_referenced_artifact_as_full_fidelity() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let content_hash = capture.object_artifact_content_hash;
        // Record a removal claim for the object, then erase its bytes (what compact does).
        write_event_to_store(
            &resolved_store_dir(repo.path()),
            artifact_removed_event(&content_hash),
        );
        remove_object_artifacts(repo.path());

        let manifest = build_export_manifest(resolved_store_dir(repo.path())).unwrap();

        assert_eq!(manifest.fidelity_status, ExportFidelityStatus::Full);
        assert!(
            manifest.diagnostics.is_empty(),
            "{:?}",
            manifest.diagnostics
        );
        // The removed object carries no bytes, so it is omitted from the transfer set...
        assert!(
            !manifest
                .artifacts
                .iter()
                .any(|artifact| artifact.content_hash == content_hash)
        );
        // ...but the event that references it is still exported.
        assert!(
            manifest.events.iter().any(|event| event
                .artifact_refs
                .iter()
                .any(|artifact_ref| artifact_ref.ends_with(&content_hash))),
            "the referencing event is still exported"
        );
    }

    #[test]
    fn strict_import_folds_a_store_with_a_removed_referenced_artifact() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let content_hash = capture.object_artifact_content_hash;
        write_event_to_store(
            &resolved_store_dir(repo.path()),
            artifact_removed_event(&content_hash),
        );
        remove_object_artifacts(repo.path());
        let target = tempfile::tempdir().unwrap();
        let target_store_dir = target.path().join(".shore/data");

        let result =
            import_store_bundle(resolved_store_dir(repo.path()), &target_store_dir).unwrap();

        // The referencing event + the removal claim fold; the erased artifact does not.
        assert!(result.events_created >= 1);
        assert_eq!(result.artifacts_created, 0);
        assert!(
            EventStore::open(&target_store_dir)
                .list_events()
                .unwrap()
                .iter()
                .any(|event| event.event_type == EventType::ArtifactRemoved)
        );
    }

    /// An unsigned `ArtifactRemoved` claim over `content_hash` — the record that
    /// explains a legitimately compacted-away artifact.
    fn artifact_removed_event(content_hash: &str) -> ShoreEvent {
        ShoreEvent::new(
            EventType::ArtifactRemoved,
            ArtifactRemovedPayload::idempotency_key(content_hash),
            EventTarget::for_journal(JournalId::new("journal:default")),
            Writer::shore_local("test"),
            ArtifactRemovedPayload {
                content_hash: content_hash.to_owned(),
            },
            "2026-05-30T00:00:00Z",
        )
        .expect("removal event builds")
    }

    fn modified_repo() -> TestRepo {
        let repo = TestRepo::new();
        repo.write("src/lib.rs", "pub fn value() -> u32 { 1 }\n");
        repo.commit_all("base");
        repo.write("src/lib.rs", "pub fn value() -> u32 { 2 }\n");
        repo
    }

    /// A captured source store faithfully imported into a fresh target: the
    /// baseline every subset-verification test perturbs.
    fn imported_pair() -> (
        TestRepo,
        std::path::PathBuf,
        tempfile::TempDir,
        std::path::PathBuf,
    ) {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let source = resolved_store_dir(repo.path());
        let target_root = tempfile::tempdir().unwrap();
        let target = target_root.path().join(".shore/data");
        import_store_bundle(&source, &target).unwrap();
        (repo, source, target_root, target)
    }

    #[test]
    fn verify_source_subset_passes_after_a_faithful_import() {
        let (_repo, source, _target_root, target) = imported_pair();
        let source_events_before = EventStore::open(&source).list_event_file_names().unwrap();
        let target_events_before = EventStore::open(&target).list_event_file_names().unwrap();

        let verification = verify_source_subset_of_target(&source, &target).unwrap();

        assert!(verification.verified_events >= 1);
        assert!(verification.verified_artifacts >= 1);
        // Verification is read-only on both stores.
        assert_eq!(
            EventStore::open(&source).list_event_file_names().unwrap(),
            source_events_before
        );
        assert_eq!(
            EventStore::open(&target).list_event_file_names().unwrap(),
            target_events_before
        );
    }

    #[test]
    fn verify_source_subset_fails_when_a_target_event_is_missing() {
        let (_repo, source, _target_root, target) = imported_pair();
        let name = EventStore::open(&target)
            .list_event_file_names()
            .unwrap()
            .into_iter()
            .next()
            .unwrap();
        fs::remove_file(target.join("events").join(&name)).unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("a missing target event must fail verification");
        let message = error.to_string();
        assert!(
            message.contains("events/"),
            "names the missing file: {message}"
        );
        assert!(
            message.contains("not deleted") || message.contains("left"),
            "says the source survives: {message}"
        );
    }

    #[test]
    fn verify_source_subset_fails_on_envelope_divergent_target_event() {
        let (_repo, source, _target_root, target) = imported_pair();
        // Same path, same payload, divergent envelope — e.g. a first-stored
        // record from another worktree with its own occurredAt. Deleting the
        // source would lose its envelope, so the comparison must catch every
        // field except the import's own `ingest` stamp.
        let name = EventStore::open(&target)
            .list_event_file_names()
            .unwrap()
            .into_iter()
            .next()
            .unwrap();
        let path = target.join("events").join(&name);
        let mut value: serde_json::Value =
            serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
        value["occurredAt"] = serde_json::Value::String("2020-01-01T00:00:00Z".to_owned());
        fs::write(&path, serde_json::to_vec(&value).unwrap()).unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("an envelope-divergent target event must fail verification");
        assert!(
            error.to_string().contains("diverge"),
            "explains the divergence: {error}"
        );
    }

    #[test]
    fn verify_source_subset_ignores_the_imports_own_ingest_stamp() {
        // The faithful-import baseline already has stamped target events; the
        // pass test covers it end to end. This pins the comparator directly:
        // identical events differing ONLY in the `ingest` stamp match, and a
        // corrupt (unparseable) side is a divergence, never a pass.
        let source = serde_json::json!({"eventId": "evt:x", "occurredAt": "t"});
        let mut target = source.clone();
        target["ingest"] = serde_json::json!({"via": "bundle_apply", "receivedAt": "t2"});
        assert!(
            events_match_modulo_ingest_stamp(
                &serde_json::to_vec(&source).unwrap(),
                &serde_json::to_vec(&target).unwrap(),
            )
            .unwrap()
        );
        assert!(
            !events_match_modulo_ingest_stamp(&serde_json::to_vec(&source).unwrap(), b"{ not json")
                .unwrap()
        );
    }

    #[test]
    fn verify_source_subset_fails_when_a_target_artifact_is_missing() {
        let (_repo, source, _target_root, target) = imported_pair();
        let objects = target.join("artifacts/objects");
        let artifact = fs::read_dir(&objects)
            .unwrap()
            .next()
            .expect("imported target has an object artifact")
            .unwrap();
        fs::remove_file(artifact.path()).unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("a missing target artifact must fail verification");
        assert!(error.to_string().contains("artifacts/"));
    }

    #[test]
    fn verify_source_subset_fails_on_an_orphan_source_artifact_the_fold_never_carried() {
        let (_repo, source, _target_root, target) = imported_pair();
        // An artifact file no event references: absent from the import manifest,
        // so only a physical walk can see it — deleting the source would destroy
        // the only copy.
        fs::write(source.join("artifacts/objects/orphan.json"), "{}").unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("an unreferenced source artifact absent from the target must fail");
        assert!(
            error.to_string().contains("orphan.json"),
            "names the file: {error}"
        );
    }

    #[test]
    fn verify_source_subset_passes_when_a_source_object_is_legacy_named() {
        let (_repo, source, _target_root, target) = imported_pair();
        // A pre-content-hash-naming migration (plan 0078) leaves some source
        // object files named by a legacy stem (filename != sha256(bytes)); the
        // fold re-canonicalizes them to content-hash filenames in the target.
        // Their bytes are present in the target under a *different* name, so a
        // filename-based check reports them missing and wrongly blocks
        // --retire-source. Content-based verification must pass.
        let objects = source.join("artifacts/objects");
        let entry = fs::read_dir(&objects)
            .unwrap()
            .next()
            .expect("imported source has an object artifact")
            .unwrap();
        let legacy = objects.join("dead00000000000000000000legacyname.json");
        fs::rename(entry.path(), &legacy).unwrap();
        // The target has no file at the source's legacy relative path: only its
        // content, under the content-hash name, matches.
        assert!(
            !target
                .join("artifacts/objects/dead00000000000000000000legacyname.json")
                .exists()
        );

        let verification = verify_source_subset_of_target(&source, &target).unwrap();
        assert!(verification.verified_artifacts >= 1);
    }

    #[test]
    fn verify_source_subset_still_blocks_a_legacy_named_source_object_absent_by_content() {
        let (_repo, source, _target_root, target) = imported_pair();
        // Content-based verification must not become a rename-blind pass: a
        // legacy-named source object whose *bytes* are in no target file is a
        // real data-loss risk and must still block the retire.
        let objects = source.join("artifacts/objects");
        let entry = fs::read_dir(&objects)
            .unwrap()
            .next()
            .expect("imported source has an object artifact")
            .unwrap();
        let legacy = objects.join("dead00000000000000000000legacyname.json");
        fs::rename(entry.path(), &legacy).unwrap();
        // Perturb the bytes so no target file shares this content.
        fs::write(&legacy, b"{\"tampered\":true}").unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("a legacy-named source object absent by content must still block");
        assert!(
            error.to_string().contains("artifacts/"),
            "names the file: {error}"
        );
    }

    #[test]
    fn verify_source_subset_does_not_skip_nested_files_named_state_json() {
        let (_repo, source, _target_root, target) = imported_pair();
        // Only the STORE-ROOT state.json is a regenerable projection — and the
        // walk roots at events/ + artifacts/, so it is never enumerated at all.
        // A file merely NAMED state.json nested inside artifacts/ is durable
        // bytes like any other and must fail verification when the target lacks
        // it, never be skipped.
        fs::write(source.join("artifacts/objects/state.json"), "{}").unwrap();

        let error = verify_source_subset_of_target(&source, &target)
            .expect_err("a nested file named state.json must be verified, not skipped");
        assert!(
            error.to_string().contains("state.json"),
            "names the file: {error}"
        );
    }

    #[test]
    fn verify_source_subset_ignores_state_json_and_temp_files() {
        let (_repo, source, _target_root, target) = imported_pair();
        // state.json is a regenerable projection and *.tmp is an in-flight temp
        // file; neither is durable, so neither is required in the target. The
        // capture already wrote the source state.json.
        assert!(source.join("state.json").is_file());
        fs::write(source.join("events/.shore-write.fresh.tmp"), "in flight").unwrap();

        let verification = verify_source_subset_of_target(&source, &target).unwrap();
        assert!(verification.verified_events >= 1);
    }

    /// The store a capture/workflow actually lands in for `repo` — the shared
    /// common-dir store by default. A repo used as an export/import bundle source
    /// is read from here, not the raw worktree-local `.shore/data`.
    fn resolved_store_dir(repo: &std::path::Path) -> std::path::PathBuf {
        crate::git::git_common_dir(repo).unwrap().join("shore")
    }

    fn review_initialized_event(idempotency_key: &str, value: u32) -> ShoreEvent {
        let payload = json!({ "value": value });
        ShoreEvent {
            schema: "shore.event".to_owned(),
            version: 1,
            event_id: EventId::new(format!(
                "evt:sha256:{}",
                sha256_bytes_hex(idempotency_key.as_bytes())
            )),
            event_type: EventType::ReviewInitialized,
            idempotency_key: idempotency_key.to_owned(),
            target: EventTarget::for_journal(JournalId::new("journal:default")),
            writer: Writer::shore_local("test"),
            occurred_at: "2026-05-30T00:00:00Z".to_owned(),
            payload_hash: sha256_json_prefixed(&payload).unwrap(),
            assertion_mode: AssertionMode::Advisory,
            signer: None,
            signature: None,
            source_ref: None,
            ingest: None,
            content_encoding: Vec::new(),
            payload_version: 1,
            payload,
        }
    }

    fn invalid_signed_review_initialized_event(idempotency_key: &str) -> ShoreEvent {
        let mut event = review_initialized_event(idempotency_key, 1);
        event.signer = Some(
            SignerId::parse("did:key:z6MkehRgf7yJbgaGfYsdoAsKdBPE3dj2CYhowQdcjqSJgvVd").unwrap(),
        );
        event.signature = Some(
            EventSignature::new_ed25519_v1(
                "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
            )
            .unwrap(),
        );
        event
    }

    fn validation_event_with_summary_artifact(
        summary_artifact_path: &str,
        summary_content_hash: &str,
        summary_byte_size: u64,
    ) -> ShoreEvent {
        let revision_id = RevisionId::new("review-unit:sha256:bundle");
        let track_id = TrackId::new("agent:codex");
        let target = EventTarget::for_revision(
            JournalId::new("journal:default"),
            revision_id.clone(),
            Some(track_id.clone()),
        )
        .unwrap();

        ShoreEvent::new(
            EventType::ValidationCheckRecorded,
            ValidationCheckRecordedPayload::idempotency_key(
                &revision_id,
                &track_id,
                "validation:sha256:bundle",
            ),
            target,
            Writer::shore_local("test"),
            ValidationCheckRecordedPayload {
                validation_check_id: ValidationCheckId::new("validation:sha256:bundle"),
                target: ValidationTarget::Revision { revision_id },
                check_name: "cargo nextest run".to_owned(),
                command: None,
                status: ValidationStatus::Passed,
                exit_code: Some(0),
                trigger: ValidationTrigger::Manual,
                source_fingerprint: None,
                summary: None,
                summary_content_type: Default::default(),
                summary_artifact_path: Some(summary_artifact_path.to_owned()),
                summary_byte_size: Some(summary_byte_size),
                summary_content_hash: Some(summary_content_hash.to_owned()),
                started_at: None,
                completed_at: None,
                log_artifact_content_hashes: Vec::new(),
            },
            "2026-05-30T00:00:00Z",
        )
        .unwrap()
    }

    fn write_event_to_store(store_dir: &Path, event: ShoreEvent) {
        EventStore::open(store_dir)
            .record_event_once(&event)
            .expect("write test event");
    }

    fn remove_object_artifacts(repo: &Path) {
        let artifact_dir = resolved_store_dir(repo).join("artifacts/objects");
        for entry in fs::read_dir(artifact_dir).unwrap() {
            let path = entry.unwrap().path();
            if path.extension() == Some(OsStr::new("json")) {
                fs::remove_file(path).unwrap();
            }
        }
    }

    fn note_body_path_for_hash(hash: String) -> String {
        format!("artifacts/notes/{hash}.json")
    }

    fn write_note_body_artifact(store_dir: &Path, body: String) -> (String, String, u64) {
        let byte_size = body.len() as u64;
        let content_hash = format!("sha256:{}", sha256_bytes_hex(body.as_bytes()));
        let artifact_path = note_body_path_for_hash(
            content_hash
                .strip_prefix("sha256:")
                .expect("sha256 content hash")
                .to_owned(),
        );
        let bytes = NoteBodyEnvelope::new(body).to_json_bytes().unwrap();
        LocalStorage::new(store_dir)
            .write_bytes_atomic(Path::new(&artifact_path), &bytes, Durability::Durable)
            .expect("write note body artifact");
        (artifact_path, content_hash, byte_size)
    }

    struct TestRepo {
        root: tempfile::TempDir,
    }

    impl TestRepo {
        fn new() -> Self {
            let root = tempfile::tempdir().expect("create temp git repository directory");
            let repo = Self { root };

            repo.git(["init"]);
            repo.git(["config", "user.name", "Shore Tests"]);
            repo.git(["config", "user.email", "shore-tests@example.com"]);
            repo.git(["config", "commit.gpgsign", "false"]);

            repo
        }

        fn path(&self) -> &Path {
            self.root.path()
        }

        fn write(&self, path: impl AsRef<Path>, contents: impl AsRef<[u8]>) {
            let path = self.root.path().join(path);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).expect("create parent directories");
            }
            fs::write(path, contents).expect("write test repository file");
        }

        fn commit_all(&self, message: &str) {
            self.git(["add", "--all"]);
            self.git(["commit", "-m", message]);
        }

        fn git<I, S>(&self, args: I)
        where
            I: IntoIterator<Item = S>,
            S: AsRef<OsStr>,
        {
            let args = args
                .into_iter()
                .map(|arg| arg.as_ref().to_owned())
                .collect::<Vec<_>>();
            let output = Command::new("git")
                .args(&args)
                .current_dir(self.root.path())
                .output()
                .unwrap_or_else(|error| panic!("run git {:?}: {error}", args));

            assert!(
                output.status.success(),
                "git {:?} failed\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
                args,
                output.status,
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}