gen-diff 0.5.1

Diff algorithms for the gen sequence graph and version control system.
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
//! Resolves history ranges and coordinates construction of operation graph diffs.
//!
//! [`collect_operation_diff`] is the public entry point used by
//! `gen view-diff`, operation history, patch previews, and GenHub. This module
//! determines which revisions and operations a request represents, asks Dolt
//! which database rows changed, groups those rows by block group, and delegates
//! graph construction to [`crate::graph`]. It does not perform a merge or
//! mutate either revision.
//!
//! # Comparison endpoints
//!
//! In merge terminology, the **source** branch supplies incoming changes and
//! the **target** branch receives them. The `source_hash` and `target_hash`
//! parameter names here predate that convention and instead describe the
//! direction of the graph comparison: `source_hash` is the baseline/left state
//! and `target_hash` is the changed/right state that will be rendered. A
//! merge-oriented caller therefore passes the receiving merge target as
//! `source_hash` and the incoming merge source as `target_hash`.
//!
//! [`DiffRange::TwoDot`] compares those two materialized endpoint states
//! directly. [`DiffRange::ThreeDot`] replaces the graph baseline with their
//! merge base and still displays the changes leading to `target_hash`. This is
//! why a three-dot call returns target-side operations even though the two
//! named branches may have diverged. A missing `source_hash` represents the
//! parent of a first/root operation and allows that operation to be shown
//! against an empty prior state.
//!
//! # Overall approach
//!
//! The pipeline deliberately uses table diffs and endpoint graphs for different
//! purposes:
//!
//! 1. Resolve the requested commits, merge base when needed, and the operation
//!    hashes represented by the comparison.
//! 2. Query `dolt_diff_block_groups`, `dolt_diff_block_group_edges`, and
//!    `dolt_diff_nodes`. These rows identify affected block-group IDs and carry
//!    old/new keys plus operation attribution without scanning every graph.
//! 3. Group those changes in `BlockGroupChanges` and build each affected block
//!    group once through `crate::graph::build_block_group_diff`.
//! 4. Return one [`OperationDiff`] containing history metadata and unified
//!    [`BlockGroupDiff`] graphs for rendering.
//!
//! Table rows alone are awkward display objects: they omit unchanged path
//! context, shared sequence-slice boundaries, and the connected terminal nodes
//! required by graph layout. Conversely, comparing every complete graph would
//! find structural changes but lose Dolt's efficient affected-row discovery and
//! operation attribution. Combining the two lets table diffs answer “what
//! changed and in which operation?” while endpoint snapshots answer “what
//! connected biological graph should be shown?”
//!
//! A viable alternative is to reconstruct the changed endpoint by applying
//! `block_group_edges` diff rows to the baseline graph. New child block groups
//! have complete copied membership rows, so this does not require a fallback
//! query. The current graph module loads both selected endpoint graphs instead,
//! primarily to keep real path membership and edit-site marker filtering
//! explicit. The lineage-parent selection used for a child block group is a
//! separate presentation decision explained in [`crate::graph`], not a claim
//! that the child's stored rows are incomplete.

use std::collections::{HashMap, HashSet};

use gen_core::{CommitRef, DoltHashId, HashId, errors::ConfigError};
use gen_models::{
    block_group_edge::AugmentedEdge,
    db::GraphConnection,
    errors::OperationError,
    history::{HistoryError, HistoryStore, dolt::DoltHistoryStore},
};
use thiserror::Error;

pub use crate::graph::{BlockGroupChangeKind, BlockGroupDiff};
use crate::graph::{BlockGroupDiffInputs, build_block_group_diff};

#[derive(Debug, Error)]
pub enum OperationDiffError {
    #[error("No current operation is checked out.")]
    NoCurrentOperation,
    #[error("Operation {0} not found.")]
    OperationMissing(DoltHashId),
    #[error("Unable to find path between {0} and {1}.")]
    PathNotFound(DoltHashId, DoltHashId),
    #[error("Config error: {0}")]
    Config(#[from] ConfigError),
    #[error("SQLite error: {0}")]
    Sqlite(#[from] rusqlite::Error),
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    OperationError(#[from] OperationError),
    #[error(transparent)]
    History(#[from] HistoryError),
}

/// Complete result of comparing two operation-history revisions.
///
/// Command and view code uses `operations` to identify the represented history and `diff_graph` to
/// render the consolidated changes for each affected block group.
#[derive(Clone, Debug)]
pub struct OperationDiff {
    pub operations: Vec<DoltHashId>,
    pub diff_graph: Vec<BlockGroupDiff>,
}

/// Selects the Git-style revision range semantics used to build a diff.
///
/// Given `source` and `target`, two-dot syntax (`source..target`) compares the
/// materialized source snapshot directly with the materialized target snapshot.
/// If the refs diverged, the result therefore reflects how both endpoint states
/// differ from one another.
///
/// Three-dot syntax (`source...target`) first finds the merge base, meaning the
/// best common ancestor commit from which both refs descended. It then uses
/// that common snapshot as the graph baseline and compares it with `target`.
/// This isolates the target-side changes made since the refs diverged, which is
/// what merge previews and branch-focused reviews generally need.
///
/// `collect_operation_diff` resolves these semantics before asking Dolt for
/// table changes and building the block-group graphs consumed by CLI, TUI,
/// patch-preview, and GenHub callers.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DiffRange {
    /// Compare the source and target endpoint snapshots directly (`source..target`).
    TwoDot,
    /// Compare the source/target merge base with the target (`source...target`).
    ThreeDot,
}

impl DiffRange {
    fn between(self, source_ref: &str, target_ref: &str) -> String {
        let separator = match self {
            Self::TwoDot => "..",
            Self::ThreeDot => "...",
        };
        format!("{source_ref}{separator}{target_ref}")
    }
}

impl OperationDiff {
    fn empty() -> Self {
        Self {
            operations: Vec::new(),
            diff_graph: Vec::new(),
        }
    }
}

/// Builds the complete graph diff between two operation commits.
///
/// Returns the operations represented by the comparison and one consolidated graph diff for each
/// affected block group. The command and view layers use this as the complete comparison result.
pub fn collect_operation_diff(
    graph_conn: &GraphConnection,
    source_hash: Option<DoltHashId>,
    target_hash: DoltHashId,
    range: DiffRange,
) -> Result<OperationDiff, OperationDiffError> {
    let history_store = DoltHistoryStore::new(graph_conn);
    let target_ref = target_hash.to_string();
    let source_ref = source_hash
        .map(|hash| hash.to_string())
        .unwrap_or_else(|| format!("{target_ref}~1"));
    let (graph_source_ref, operation_start_hash) = match (range, source_hash) {
        (DiffRange::ThreeDot, Some(source_commit)) => {
            if !history_store.commit_exists(&source_commit)? {
                return Err(OperationDiffError::OperationMissing(source_commit));
            }
            if !history_store.commit_exists(&target_hash)? {
                return Err(OperationDiffError::OperationMissing(target_hash));
            }
            let merge_base = history_store.merge_base(
                &CommitRef(source_ref.clone()),
                &CommitRef(target_ref.clone()),
            )?;
            (merge_base.to_string(), Some(merge_base))
        }
        _ => (source_ref.clone(), source_hash),
    };
    let operations = operation_hashes_for_diff(&history_store, operation_start_hash, target_hash)?;
    if operations.is_empty() {
        return Ok(OperationDiff::empty());
    }

    let diff_ref = range.between(&source_ref, &target_ref);
    build_dolt_operation_diff(
        graph_conn,
        &graph_source_ref,
        &target_ref,
        &diff_ref,
        operations,
        target_hash,
    )
}

/// Returns an operation diff assembled from Dolt's table-level changes.
///
/// It groups the changed rows by block group, then builds the source and target graph states that
/// the command and view layers render for each group. What is added or removed is tracked as the
/// graph is being built, removing the need to compare graphs to figure out differences.
fn build_dolt_operation_diff(
    graph_conn: &GraphConnection,
    source_ref: &str,
    target_ref: &str,
    diff_ref: &str,
    operations: Vec<DoltHashId>,
    target_hash: DoltHashId,
) -> Result<OperationDiff, OperationDiffError> {
    let changes = collect_block_group_changes(graph_conn, diff_ref)?;
    let node_changes = collect_node_changes(graph_conn, diff_ref)?;
    let BlockGroupChanges {
        block_group_ids,
        edge_changes: edge_changes_by_block_group,
        operations_by_block_group_id: block_group_operations_by_id,
    } = changes;

    let mut block_group_ids = block_group_ids.into_iter().collect::<Vec<_>>();
    block_group_ids.sort();
    let diff_inputs = BlockGroupDiffInputs {
        source_ref,
        target_ref,
        default_operation: target_hash,
        edge_changes_by_block_group: &edge_changes_by_block_group,
        block_group_operations_by_id: &block_group_operations_by_id,
        node_changes: &node_changes,
    };
    let mut diff_graph = Vec::new();
    for block_group_id in block_group_ids {
        if let Some(block_group_diff) =
            build_block_group_diff(graph_conn, block_group_id, &diff_inputs)?
        {
            diff_graph.push(block_group_diff);
        }
    }

    Ok(OperationDiff {
        operations,
        diff_graph,
    })
}

/// Returns the operation hashes represented by a comparison.
///
/// For an ancestor range, the output is the target-side history after `source_hash`. For divergent
/// endpoints, it contains the source and target commits. Consumers use the result as comparison
/// metadata and to detect a comparison with no changes. Importantly, this just returns 0, 1, or 2
/// hashes indicating no changes, a target_hash, or two hashes marking the start/end of the diff.
fn operation_hashes_for_diff(
    history_store: &impl HistoryStore,
    source_hash: Option<DoltHashId>,
    target_hash: DoltHashId,
) -> Result<Vec<DoltHashId>, OperationDiffError> {
    if source_hash == Some(target_hash) {
        return Ok(Vec::new());
    }

    let target_ref = target_hash.to_string();
    let history_entries = history_store.log_for_ref(&CommitRef(target_ref), None)?;
    let history_hashes = history_entries
        .into_iter()
        .rev()
        .map(|entry| entry.commit_hash)
        .collect::<Vec<_>>();
    let target_index = history_hashes
        .iter()
        .position(|candidate| *candidate == target_hash)
        .ok_or(OperationDiffError::OperationMissing(target_hash))?;

    let operations = match source_hash {
        None => vec![target_hash],
        Some(source_hash) => match history_hashes
            .iter()
            .position(|candidate| *candidate == source_hash)
        {
            Some(source_index) if source_index > target_index => {
                return Err(OperationDiffError::PathNotFound(source_hash, target_hash));
            }
            Some(source_index) => history_hashes[source_index + 1..=target_index].to_vec(),
            None if history_store.commit_exists(&source_hash)? => {
                vec![source_hash, target_hash]
            }
            None => return Err(OperationDiffError::OperationMissing(source_hash)),
        },
    };
    Ok(operations)
}

/// Returns the affected block groups, their edge changes, and their operation attribution.
///
/// `build_dolt_operation_diff` consumes this aggregate to decide which block-group graphs to build
/// and how to match and attribute edge changes within each graph.
fn collect_block_group_changes(
    graph_conn: &GraphConnection,
    diff_ref: &str,
) -> Result<BlockGroupChanges, OperationDiffError> {
    let mut changes = BlockGroupChanges::default();
    let block_group_query = format!(
        "SELECT to_id,
                from_id,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                to_commit,
                diff_type
         FROM dolt_diff_block_groups('{diff_ref}')"
    );
    let mut block_group_statement = graph_conn.prepare(&block_group_query)?;
    let block_group_rows =
        block_group_statement.query_map([], DoltBlockGroupChangeRow::from_row)?;
    for row in block_group_rows {
        let row = row?;
        row.record_block_group(&mut changes);
    }

    let edge_query = format!(
        "SELECT to_block_group_id,
                from_block_group_id,
                to_edge_id,
                from_edge_id,
                to_chromosome_index,
                from_chromosome_index,
                to_phased,
                from_phased,
                to_commit,
                diff_type
         FROM dolt_diff_block_group_edges('{diff_ref}')"
    );
    let mut edge_statement = graph_conn.prepare(&edge_query)?;
    let edge_rows = edge_statement.query_map([], DoltBlockGroupChangeRow::from_row)?;
    for row in edge_rows {
        let row = row?;
        row.record_block_group(&mut changes);
        if let Some(edge_change) = row.edge_change() {
            changes
                .edge_changes
                .entry(edge_change.block_group_id())
                .or_default()
                .push(edge_change);
        }
    }

    Ok(changes)
}

/// Returns the added, removed, and modified node-ID transitions for a revision range.
///
/// Graph construction uses these transitions to match source nodes with target nodes and to
/// attribute the resulting graph changes to operations.
fn collect_node_changes(
    graph_conn: &GraphConnection,
    diff_ref: &str,
) -> Result<Vec<NodeChange>, OperationDiffError> {
    let query =
        format!("SELECT to_id, from_id, to_commit, diff_type FROM dolt_diff_nodes('{diff_ref}')");
    let mut node_changes = Vec::new();
    let mut statement = graph_conn.prepare(&query)?;
    let rows = statement.query_map([], |row| {
        Ok((
            row.get::<_, Option<HashId>>(0)?,
            row.get::<_, Option<HashId>>(1)?,
            row.get::<_, Option<DoltHashId>>(2)?,
            row.get::<_, String>(3)?,
        ))
    })?;
    for row in rows {
        let (to_node_id, from_node_id, to_commit, diff_type) = row?;
        let operation = to_commit;
        match diff_type.as_str() {
            "added" => {
                if let Some(new_node_id) = to_node_id {
                    node_changes.push(NodeChange::Added {
                        new_node_id,
                        operation,
                    });
                }
            }
            "removed" => {
                if let Some(old_node_id) = from_node_id {
                    node_changes.push(NodeChange::Removed {
                        old_node_id,
                        operation,
                    });
                }
            }
            "modified" => {
                if let (Some(old_node_id), Some(new_node_id)) = (from_node_id, to_node_id) {
                    node_changes.push(NodeChange::Modified {
                        old_node_id,
                        new_node_id,
                        old_operation: operation,
                        new_operation: operation,
                    });
                }
            }
            _ => {}
        }
    }

    Ok(node_changes)
}

/// Table-level changes collected before block-group graph construction.
///
/// This groups Dolt diff rows by block group so graph construction can determine which graphs to
/// build, which edges changed, and which operations should be used for attribution.
#[derive(Debug, Default)]
struct BlockGroupChanges {
    /// Every block group affected by a block-group or edge diff row.
    block_group_ids: HashSet<HashId>,
    /// Normalized edge changes keyed by their owning block group.
    edge_changes: HashMap<HashId, Vec<BlockGroupEdgeChange>>,
    /// Operations associated with each affected block group.
    operations_by_block_group_id: HashMap<HashId, HashSet<DoltHashId>>,
}

impl BlockGroupChanges {
    fn record_operation(&mut self, block_group_id: HashId, operation: Option<DoltHashId>) {
        if let Some(operation) = operation {
            self.operations_by_block_group_id
                .entry(block_group_id)
                .or_default()
                .insert(operation);
        }
    }
}

/// Normalized result row shared by the block-group and block-group-edge Dolt diff queries.
///
/// The optional source (`from_*`) and target (`to_*`) columns are converted into affected block
/// groups and `BlockGroupEdgeChange` values by the collection step.
#[derive(Debug)]
struct DoltBlockGroupChangeRow {
    /// Target-side block-group ID, absent when the block group was removed.
    to_block_group_id: Option<HashId>,
    /// Source-side block-group ID, absent when the block group was added.
    from_block_group_id: Option<HashId>,
    /// Target-side edge ID for edge rows.
    to_edge_id: Option<HashId>,
    /// Source-side edge ID for edge rows.
    from_edge_id: Option<HashId>,
    /// Target-side chromosome index for edge rows.
    to_chromosome_index: Option<i64>,
    /// Source-side chromosome index for edge rows.
    from_chromosome_index: Option<i64>,
    /// Target-side phasing value for edge rows.
    to_phased: Option<i64>,
    /// Source-side phasing value for edge rows.
    from_phased: Option<i64>,
    /// Commit Dolt reports for attributing the change.
    to_commit: Option<DoltHashId>,
    /// Dolt change classification: added, removed, or modified.
    diff_type: String,
}

impl DoltBlockGroupChangeRow {
    /// Decodes the normalized column shape shared by the block-group and edge diff queries.
    fn from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Self> {
        Ok(Self {
            to_block_group_id: row.get(0)?,
            from_block_group_id: row.get(1)?,
            to_edge_id: row.get(2)?,
            from_edge_id: row.get(3)?,
            to_chromosome_index: row.get(4)?,
            from_chromosome_index: row.get(5)?,
            to_phased: row.get(6)?,
            from_phased: row.get(7)?,
            to_commit: row.get(8)?,
            diff_type: row.get(9)?,
        })
    }

    /// Adds this row's affected block group and operation to the aggregate.
    fn record_block_group(&self, changes: &mut BlockGroupChanges) {
        if matches!(self.diff_type.as_str(), "added" | "removed" | "modified")
            && let Some(block_group_id) = self.to_block_group_id.or(self.from_block_group_id)
        {
            changes.block_group_ids.insert(block_group_id);
            changes.record_operation(block_group_id, self.change_operation());
        }
    }

    /// Returns the source/target edge keys represented by this diff row.
    ///
    /// The graph builder uses the result to match the row with the corresponding edges in the
    /// endpoint graphs. Rows without a recognized change type or complete edge key return `None`.
    fn edge_change(&self) -> Option<BlockGroupEdgeChange> {
        match self.diff_type.as_str() {
            "added" => {
                let block_group_id = self.to_block_group_id?;
                let new_key =
                    self.edge_key(self.to_edge_id, self.to_chromosome_index, self.to_phased)?;
                Some(BlockGroupEdgeChange {
                    block_group_id,
                    old_key: None,
                    new_key: Some(new_key),
                    old_operation: None,
                    new_operation: self.change_operation(),
                })
            }
            "removed" => {
                let block_group_id = self.from_block_group_id?;
                let old_key = self.edge_key(
                    self.from_edge_id,
                    self.from_chromosome_index,
                    self.from_phased,
                )?;
                Some(BlockGroupEdgeChange {
                    block_group_id,
                    old_key: Some(old_key),
                    new_key: None,
                    old_operation: self.change_operation(),
                    new_operation: None,
                })
            }
            "modified" => {
                let block_group_id = self.to_block_group_id.or(self.from_block_group_id)?;
                let old_key = self.edge_key(
                    self.from_edge_id,
                    self.from_chromosome_index,
                    self.from_phased,
                )?;
                let new_key =
                    self.edge_key(self.to_edge_id, self.to_chromosome_index, self.to_phased)?;
                Some(BlockGroupEdgeChange {
                    block_group_id,
                    old_key: Some(old_key),
                    new_key: Some(new_key),
                    old_operation: self.change_operation(),
                    new_operation: self.change_operation(),
                })
            }
            _ => None,
        }
    }

    /// Returns the stable identity of one side of an edge row when all key columns are present.
    fn edge_key(
        &self,
        edge_id: Option<HashId>,
        chromosome_index: Option<i64>,
        phased: Option<i64>,
    ) -> Option<BlockGroupEdgeKey> {
        Some(BlockGroupEdgeKey {
            edge_id: edge_id?,
            chromosome_index: chromosome_index?,
            phased: phased?,
        })
    }

    /// Returns the `to_commit` value Dolt supplies to attribute this row's change.
    fn change_operation(&self) -> Option<DoltHashId> {
        self.to_commit
    }
}

/// Stable identity of a block-group edge on one side of a comparison.
///
/// Graph construction uses this key to match edges loaded from an endpoint graph with rows returned
/// by Dolt's edge diff table.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct BlockGroupEdgeKey {
    edge_id: HashId,
    chromosome_index: i64,
    phased: i64,
}

impl From<&AugmentedEdge> for BlockGroupEdgeKey {
    /// Returns the stable key used to match a graph edge with a Dolt edge-diff row.
    fn from(edge: &AugmentedEdge) -> Self {
        Self {
            edge_id: edge.edge.id,
            chromosome_index: edge.chromosome_index,
            phased: edge.phased,
        }
    }
}

/// Source/target node IDs and operation attribution for one Dolt node diff row.
///
/// Graph construction uses these values to identify nodes present on only one endpoint and annotate
/// their resulting graph changes with the responsible operation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum NodeChange {
    Added {
        new_node_id: HashId,
        operation: Option<DoltHashId>,
    },
    Removed {
        old_node_id: HashId,
        operation: Option<DoltHashId>,
    },
    Modified {
        old_node_id: HashId,
        new_node_id: HashId,
        old_operation: Option<DoltHashId>,
        new_operation: Option<DoltHashId>,
    },
}

impl NodeChange {
    /// Returns the source-side node ID, or `None` when the node exists only in the target.
    pub(crate) fn old_node_id(&self) -> Option<HashId> {
        match self {
            NodeChange::Added { .. } => None,
            NodeChange::Removed { old_node_id, .. } => Some(*old_node_id),
            NodeChange::Modified { old_node_id, .. } => Some(*old_node_id),
        }
    }

    /// Returns the target-side node ID, or `None` when the node exists only in the source.
    pub(crate) fn new_node_id(&self) -> Option<HashId> {
        match self {
            NodeChange::Added { new_node_id, .. } => Some(*new_node_id),
            NodeChange::Removed { .. } => None,
            NodeChange::Modified { new_node_id, .. } => Some(*new_node_id),
        }
    }

    /// Returns the operation attributed to the source-side half of this node change.
    pub(crate) fn old_operation(&self) -> Option<DoltHashId> {
        match self {
            NodeChange::Added { .. } => None,
            NodeChange::Removed { operation, .. } => *operation,
            NodeChange::Modified { old_operation, .. } => *old_operation,
        }
    }

    /// Returns the operation attributed to the target-side half of this node change.
    pub(crate) fn new_operation(&self) -> Option<DoltHashId> {
        match self {
            NodeChange::Added { operation, .. } => *operation,
            NodeChange::Removed { .. } => None,
            NodeChange::Modified { new_operation, .. } => *new_operation,
        }
    }
}

/// Source/target edge identities and operation attribution for one Dolt edge diff row.
///
/// Graph construction uses this to match endpoint edges, filter changes relevant to a block group,
/// and annotate the resulting graph changes with the responsible operation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct BlockGroupEdgeChange {
    block_group_id: HashId,
    /// Source-side edge identity, absent for an added edge.
    pub(crate) old_key: Option<BlockGroupEdgeKey>,
    /// Target-side edge identity, absent for a removed edge.
    pub(crate) new_key: Option<BlockGroupEdgeKey>,
    /// Operation attributed to the source-side half of the change.
    old_operation: Option<DoltHashId>,
    /// Operation attributed to the target-side half of the change.
    new_operation: Option<DoltHashId>,
}

impl BlockGroupEdgeChange {
    /// Returns the block group whose edge set contains this change.
    fn block_group_id(&self) -> HashId {
        self.block_group_id
    }

    /// Returns the source-side edge ID, or `None` for an added edge.
    pub(crate) fn old_edge_id(&self) -> Option<HashId> {
        self.old_key.map(|key| key.edge_id)
    }

    /// Returns the target-side edge ID, or `None` for a removed edge.
    pub(crate) fn new_edge_id(&self) -> Option<HashId> {
        self.new_key.map(|key| key.edge_id)
    }

    /// Returns the operation attributed to the source-side half of this edge change.
    pub(crate) fn old_operation(&self) -> Option<DoltHashId> {
        self.old_operation
    }

    /// Returns the operation attributed to the target-side half of this edge change.
    pub(crate) fn new_operation(&self) -> Option<DoltHashId> {
        self.new_operation
    }
}

#[cfg(test)]
mod tests {
    use std::{
        collections::{HashMap, HashSet},
        fs,
    };

    use gen_core::{
        BranchName, CommitRef, DoltHashId, HashId, PATH_END_NODE_ID, PATH_START_NODE_ID, Strand,
        Workspace,
    };
    use gen_graph::{GenGraph, GraphEdge, GraphNode};
    use gen_models::{
        block_group::{BlockGroup, NewBlockGroup},
        block_group_edge::{AugmentedEdge, BlockGroupEdge, BlockGroupEdgeData},
        collection::Collection,
        db::DbContext,
        edge::{Edge, GroupBlock},
        history::{HistoryStore, dolt::DoltHistoryStore},
        node::Node,
        path::Path,
        sample::{NewSample, Sample},
        sequence::Sequence,
    };
    use petgraph::Direction;
    use rusqlite::params;
    use tempfile::tempdir;

    use super::{
        BlockGroupDiff, BlockGroupEdgeChange, BlockGroupEdgeKey, DiffRange, OperationDiffError,
        collect_block_group_changes, collect_operation_diff,
    };
    use crate::{
        graph::{
            DiffChangeKind, DiffGenGraph, DiffInputGraph, GraphEdgeKey, build_diff_input_graph,
            build_unified_diff_graph, path_end_graph_node, path_start_graph_node,
        },
        test_helpers::{TestContext, get_config_connection, get_connection as test_get_connection},
    };

    fn setup_history_context() -> TestContext {
        let tmp_dir = tempdir().expect("should create temp dir");
        let workspace = Workspace::new(tmp_dir.path());
        workspace.ensure_gen_dir();
        let graph_conn = test_get_connection(workspace.graph_db_path().unwrap().to_str().unwrap())
            .expect("should create graph database");
        let config_conn = get_config_connection(workspace.gen_db_path().unwrap().to_str().unwrap())
            .expect("should create config database");
        TestContext::new(
            DbContext::new(workspace, graph_conn, config_conn).expect("should create db context"),
            tmp_dir,
        )
    }

    #[test]
    fn test_setup_history_context_temp_dir_cleanup() {
        let context = setup_history_context();
        let base_dir = context.workspace().base_dir().to_path_buf();
        let nested_asset = context
            .workspace()
            .asset_dir()
            .expect("should resolve workspace asset directory")
            .join("nested/fixture.txt");
        fs::create_dir_all(
            nested_asset
                .parent()
                .expect("should have a parent directory"),
        )
        .expect("should create nested fixture asset directory");
        fs::write(&nested_asset, "temporary workspace asset")
            .expect("should write nested fixture asset");

        let context_clone = context.clone();
        drop(context);
        assert!(
            base_dir.is_dir(),
            "context clone should keep workspace alive"
        );
        assert_eq!(
            fs::read_to_string(nested_asset).expect("should read nested fixture asset"),
            "temporary workspace asset"
        );

        drop(context_clone);
        assert!(
            !base_dir.exists(),
            "workspace should be removed after its last context drops"
        );
    }

    fn create_linear_block_group(
        context: &gen_models::db::DbContext,
        collection_name: &str,
        sample_name: &str,
        block_group_name: &str,
        node_name: &str,
        sequence_bases: &str,
    ) -> BlockGroup {
        let graph_conn = context.graph().conn();
        Collection::get_or_create(graph_conn, collection_name).expect("should create collection");
        Sample::get_or_create(
            graph_conn,
            NewSample {
                name: sample_name,
                is_reference: false,
            },
        )
        .expect("should create sample");
        let block_group = BlockGroup::create(
            graph_conn,
            NewBlockGroup {
                collection_name,
                sample_name,
                name: block_group_name,
                ..Default::default()
            },
        )
        .expect("should create block group");
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence(sequence_bases)
            .name(node_name)
            .save(graph_conn)
            .expect("should save sequence");
        let node_id = Node::create(
            graph_conn,
            &sequence.hash,
            &HashId::convert_str(&format!("{block_group_name}-{node_name}")),
        )
        .expect("should create node");
        let start_edge = Edge::create(
            graph_conn,
            PATH_START_NODE_ID,
            0,
            Strand::Forward,
            node_id,
            0,
            Strand::Forward,
        )
        .expect("should create start edge");
        let end_edge = Edge::create(
            graph_conn,
            node_id,
            sequence.length,
            Strand::Forward,
            PATH_END_NODE_ID,
            0,
            Strand::Forward,
        )
        .expect("should create end edge");
        BlockGroupEdge::bulk_create(
            graph_conn,
            &[
                BlockGroupEdgeData {
                    block_group_id: block_group.id,
                    edge_id: start_edge.id,
                    chromosome_index: 0,
                    phased: 0,
                },
                BlockGroupEdgeData {
                    block_group_id: block_group.id,
                    edge_id: end_edge.id,
                    chromosome_index: 0,
                    phased: 0,
                },
            ],
        );
        Path::create(
            graph_conn,
            block_group_name,
            &block_group.id,
            &[start_edge.id, end_edge.id],
        )
        .expect("should create path");
        block_group
    }

    fn replace_linear_block_group_path(
        context: &gen_models::db::DbContext,
        block_group: &BlockGroup,
        node_name: &str,
        sequence_bases: &str,
    ) {
        let graph_conn = context.graph().conn();
        let existing_edges =
            BlockGroupEdge::edges_for_block_group(graph_conn, &block_group.id, None)
                .into_iter()
                .map(|edge| BlockGroupEdgeData {
                    block_group_id: block_group.id,
                    edge_id: edge.edge.id,
                    chromosome_index: edge.chromosome_index,
                    phased: edge.phased,
                })
                .collect::<Vec<_>>();
        Path::delete(graph_conn, &block_group.name, &block_group.id);
        BlockGroupEdge::bulk_delete(graph_conn, &existing_edges);

        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence(sequence_bases)
            .name(node_name)
            .save(graph_conn)
            .expect("should save replacement sequence");
        let node_id = Node::create(
            graph_conn,
            &sequence.hash,
            &HashId::convert_str(&format!("{}-{node_name}", block_group.name)),
        )
        .expect("should create replacement node");
        let start_edge = Edge::create(
            graph_conn,
            PATH_START_NODE_ID,
            0,
            Strand::Forward,
            node_id,
            0,
            Strand::Forward,
        )
        .expect("should create replacement start edge");
        let end_edge = Edge::create(
            graph_conn,
            node_id,
            sequence.length,
            Strand::Forward,
            PATH_END_NODE_ID,
            0,
            Strand::Forward,
        )
        .expect("should create replacement end edge");
        BlockGroupEdge::bulk_create(
            graph_conn,
            &[
                BlockGroupEdgeData {
                    block_group_id: block_group.id,
                    edge_id: start_edge.id,
                    chromosome_index: 0,
                    phased: 0,
                },
                BlockGroupEdgeData {
                    block_group_id: block_group.id,
                    edge_id: end_edge.id,
                    chromosome_index: 0,
                    phased: 0,
                },
            ],
        );
        Path::create(
            graph_conn,
            &block_group.name,
            &block_group.id,
            &[start_edge.id, end_edge.id],
        )
        .expect("should recreate path");
    }

    fn commit_operation(context: &gen_models::db::DbContext, message: &str) -> DoltHashId {
        let history_store = DoltHistoryStore::new(context.graph().conn());
        history_store
            .commit_all(message)
            .expect("should commit graph changes")
    }

    fn commit_hash_ref(operation_hash: DoltHashId) -> CommitRef {
        CommitRef(operation_hash.to_string())
    }

    struct DivergentHistory {
        context: TestContext,
        source_block_group: BlockGroup,
        target_block_group: BlockGroup,
        source_hash: DoltHashId,
        target_hash: DoltHashId,
    }

    // Builds two branches with one unique block group on each side:
    //
    //                    source branch: source_hash (+ source_block_group)
    //                   /
    //     base_hash ----*
    //                   \
    //                    main branch:   target_hash (+ target_block_group)
    //
    // A two-dot diff compares the endpoint states, so the source block group is
    // removed and the target block group is added. A three-dot diff compares the
    // common base with the target, so only the target block group is added.
    fn setup_divergent_history() -> DivergentHistory {
        let mut context = setup_history_context();
        let graph_conn = context.graph().conn();
        let graph_db_path = context
            .workspace()
            .graph_db_path()
            .expect("should resolve graph database path");
        let history_store = DoltHistoryStore::new(graph_conn);

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let source_block_group =
            create_linear_block_group(&context, "c", "s", "main", "main", "CCCCC");
        let source_hash = commit_operation(&context, "main op");
        history_store
            .create_branch(
                &BranchName("source".to_string()),
                Some(&commit_hash_ref(source_hash)),
            )
            .expect("should preserve the source branch");
        history_store
            .reset_hard(&commit_hash_ref(base))
            .expect("should reset main to the common base");
        let reopened_graph = gen_models::db::get_connection(&graph_db_path)
            .expect("should reopen graph database after reset");
        gen_models::history::dolt::connect_branch(&reopened_graph, "main")
            .expect("should reconnect the main branch");
        context.set_graph(reopened_graph);
        let target_block_group =
            create_linear_block_group(&context, "c", "s", "feature", "feature", "GGGGG");
        let target_hash = commit_operation(&context, "feature op");

        DivergentHistory {
            context,
            source_block_group,
            target_block_group,
            source_hash,
            target_hash,
        }
    }

    fn switch_branch(context: &gen_models::db::DbContext, branch_name: &str) {
        let history_store = DoltHistoryStore::new(context.graph().conn());
        history_store
            .checkout_branch(&BranchName(branch_name.to_string()))
            .expect("should checkout history branch");
    }

    fn create_feature_branch(
        context: &gen_models::db::DbContext,
        base_operation_hash: DoltHashId,
        branch_name: &str,
    ) {
        let history_store = DoltHistoryStore::new(context.graph().conn());
        history_store
            .create_branch(
                &BranchName(branch_name.to_string()),
                Some(&commit_hash_ref(base_operation_hash)),
            )
            .expect("should create history branch");
        switch_branch(context, branch_name);
    }

    fn test_augmented_edge(
        edge_id: HashId,
        source_node_id: HashId,
        source_coordinate: i64,
        target_node_id: HashId,
        target_coordinate: i64,
    ) -> AugmentedEdge {
        AugmentedEdge {
            edge: Edge {
                id: edge_id,
                source_node_id,
                source_coordinate,
                source_strand: Strand::Forward,
                target_node_id,
                target_coordinate,
                target_strand: Strand::Forward,
            },
            chromosome_index: 0,
            phased: 0,
            created_on: 0,
        }
    }

    fn apply_edge_changes(
        graph_conn: &gen_models::db::GraphConnection,
        source_edges: &[AugmentedEdge],
        edge_changes: &[BlockGroupEdgeChange],
        target_ref: &str,
    ) -> Result<Vec<AugmentedEdge>, OperationDiffError> {
        let mut edges_by_key = source_edges
            .iter()
            .cloned()
            .map(|edge| (BlockGroupEdgeKey::from(&edge), edge))
            .collect::<HashMap<_, _>>();

        for edge_change in edge_changes {
            if let Some(old_key) = edge_change.old_key {
                edges_by_key.remove(&old_key);
            }
            if let Some(new_key) = edge_change.new_key {
                let edge = load_augmented_edge(
                    graph_conn,
                    edge_change.block_group_id,
                    new_key,
                    target_ref,
                )?;
                edges_by_key.insert(new_key, edge);
            }
        }

        let mut edges = edges_by_key.into_values().collect::<Vec<_>>();
        edges.sort_by(|left, right| {
            right
                .created_on
                .cmp(&left.created_on)
                .then_with(|| left.edge.id.cmp(&right.edge.id))
                .then_with(|| left.chromosome_index.cmp(&right.chromosome_index))
                .then_with(|| left.phased.cmp(&right.phased))
        });
        Ok(edges)
    }

    fn load_augmented_edge(
        graph_conn: &gen_models::db::GraphConnection,
        block_group_id: HashId,
        edge_key: BlockGroupEdgeKey,
        history_ref: &str,
    ) -> Result<AugmentedEdge, OperationDiffError> {
        let query = "SELECT
                edges.id,
                edges.source_node_id,
                edges.source_coordinate,
                edges.source_strand,
                edges.target_node_id,
                edges.target_coordinate,
                edges.target_strand,
                block_group_edges.chromosome_index,
                block_group_edges.phased,
                block_group_edges.created_on
            FROM dolt_at_block_group_edges(?1) AS block_group_edges
            JOIN dolt_at_edges(?1) AS edges ON edges.id = block_group_edges.edge_id
            WHERE block_group_edges.block_group_id = ?2
              AND block_group_edges.edge_id = ?3
              AND block_group_edges.chromosome_index = ?4
              AND block_group_edges.phased = ?5";
        graph_conn
            .query_row(
                query,
                params![
                    history_ref,
                    block_group_id,
                    edge_key.edge_id,
                    edge_key.chromosome_index,
                    edge_key.phased
                ],
                |row| {
                    Ok(AugmentedEdge {
                        edge: Edge {
                            id: row.get(0)?,
                            source_node_id: row.get(1)?,
                            source_coordinate: row.get(2)?,
                            source_strand: row.get(3)?,
                            target_node_id: row.get(4)?,
                            target_coordinate: row.get(5)?,
                            target_strand: row.get(6)?,
                        },
                        chromosome_index: row.get(7)?,
                        phased: row.get(8)?,
                        created_on: row.get(9)?,
                    })
                },
            )
            .map_err(OperationDiffError::from)
    }

    fn build_diff_input(
        input_blocks: &[GroupBlock],
        input_edges: &[AugmentedEdge],
        node_operations_by_id: &HashMap<HashId, DoltHashId>,
        edge_operations_by_id: &HashMap<HashId, DoltHashId>,
    ) -> DiffInputGraph {
        let input_nodes = input_blocks
            .iter()
            .map(|block| GraphNode {
                node_id: block.node_id,
                sequence_start: block.start,
                sequence_end: block.end,
            })
            .collect::<HashSet<_>>();
        let continuation_spans = input_nodes.clone();
        build_diff_input_graph(
            input_nodes,
            &continuation_spans,
            input_edges,
            node_operations_by_id,
            edge_operations_by_id,
        )
    }

    fn graph_edge_keys(graph: &GenGraph) -> HashSet<GraphEdgeKey> {
        graph
            .all_edges()
            .flat_map(|(source, target, edges)| {
                edges
                    .iter()
                    .copied()
                    .map(move |edge| GraphEdgeKey::new(source, target, edge))
            })
            .collect()
    }

    fn test_graph_node(node_id: HashId, sequence_start: i64, sequence_end: i64) -> GraphNode {
        GraphNode {
            node_id,
            sequence_start,
            sequence_end,
        }
    }

    fn test_terminal_block(block_index: i64, node_id: HashId) -> GroupBlock {
        GroupBlock::new(
            block_index,
            node_id,
            &Sequence::new().sequence_type("DNA").sequence("").build(),
            0,
            0,
        )
    }

    fn assert_diff_edge_kind(
        graph: &DiffGenGraph,
        source: GraphNode,
        target: GraphNode,
        expected_kind: DiffChangeKind,
    ) {
        let source = graph
            .nodes()
            .find(|node| node.node == source)
            .expect("should contain source node");
        let target = graph
            .nodes()
            .find(|node| node.node == target)
            .expect("should contain target node");
        let edge_changes = graph
            .edge_weight(source, target)
            .expect("should contain edge between nodes");
        assert!(
            edge_changes
                .iter()
                .any(|edge| edge.change.kind == expected_kind),
            "edge should contain expected change kind {expected_kind:?}: {edge_changes:?}"
        );
    }

    fn assert_all_nodes_connected_to(graph: &DiffGenGraph, start: GraphNode) {
        let start = graph
            .nodes()
            .find(|node| node.node == start)
            .expect("should contain start node");
        let mut visited = HashSet::new();
        let mut stack = vec![start];
        while let Some(current) = stack.pop() {
            if !visited.insert(current) {
                continue;
            }
            for neighbor in graph
                .neighbors_directed(current, Direction::Outgoing)
                .chain(graph.neighbors_directed(current, Direction::Incoming))
            {
                if !visited.contains(&neighbor) {
                    stack.push(neighbor);
                }
            }
        }
        assert_eq!(
            visited.len(),
            graph.nodes().count(),
            "unified diff graph should be a single connected component"
        );
    }

    #[test]
    fn test_one_operation_diff_uses_dolt_history() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base_op = commit_operation(&context, "seed");
        let expected_block_group =
            create_linear_block_group(&context, "c", "s", "bg", "one", "CCCCC");
        let head = commit_operation(&context, "add");

        let diff = collect_operation_diff(graph_conn, Some(base_op), head, DiffRange::TwoDot)
            .expect("diff");
        assert_eq!(diff.operations, vec![head]);
        assert_eq!(diff.diff_graph.len(), 1);
        assert_eq!(diff.diff_graph[0].id, expected_block_group.id);
        assert!(diff.diff_graph[0].source_block_group.is_none());
        assert_eq!(
            diff.diff_graph[0].target_block_group.as_ref(),
            Some(&expected_block_group)
        );
        assert_block_group_graph_shape(&diff.diff_graph[0]);
    }

    #[test]
    fn test_initial_operation_diff_contains_added_block_groups_from_history() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        let expected_block_group =
            create_linear_block_group(&context, "c", "s", "bg", "one", "AAAAA");
        let head = commit_operation(&context, "add");

        let diff = collect_operation_diff(graph_conn, None, head, DiffRange::TwoDot).expect("diff");
        assert_eq!(diff.operations, vec![head]);
        assert_eq!(diff.diff_graph.len(), 1);
        assert_eq!(diff.diff_graph[0].id, expected_block_group.id);
        assert!(diff.diff_graph[0].source_block_group.is_none());
        assert_eq!(
            diff.diff_graph[0].target_block_group.as_ref(),
            Some(&expected_block_group)
        );
        assert_block_group_graph_shape(&diff.diff_graph[0]);
    }

    #[test]
    fn test_operation_diff_rejects_missing_from_commit() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        let missing_parent_hash = DoltHashId([1; 20]);
        create_linear_block_group(&context, "c", "s", "bg", "one", "AAAAA");
        let head = commit_operation(&context, "add");

        let error = collect_operation_diff(
            graph_conn,
            Some(missing_parent_hash),
            head,
            DiffRange::TwoDot,
        )
        .expect_err("should reject a missing from commit");
        assert!(matches!(
            error,
            OperationDiffError::OperationMissing(hash) if hash == missing_parent_hash
        ));
    }

    #[test]
    fn test_one_operation_diff_returns_removed_markers_in_unified_diff_graph() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        let removed_block_group =
            create_linear_block_group(&context, "c", "s", "bg", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let block_group_edges =
            BlockGroupEdge::edges_for_block_group(graph_conn, &removed_block_group.id, None)
                .into_iter()
                .map(|edge| BlockGroupEdgeData {
                    block_group_id: removed_block_group.id,
                    edge_id: edge.edge.id,
                    chromosome_index: edge.chromosome_index,
                    phased: edge.phased,
                })
                .collect::<Vec<_>>();
        Path::delete(
            graph_conn,
            &removed_block_group.name,
            &removed_block_group.id,
        );
        BlockGroupEdge::bulk_delete(graph_conn, &block_group_edges);
        BlockGroup::delete(
            graph_conn,
            &removed_block_group.collection_name,
            &removed_block_group.sample_name,
            &removed_block_group.name,
        )
        .expect("should delete block group");
        let head = commit_operation(&context, "remove bg");

        let diff =
            collect_operation_diff(graph_conn, Some(base), head, DiffRange::TwoDot).expect("diff");
        assert_eq!(diff.operations, vec![head]);
        assert_eq!(diff.diff_graph.len(), 1);
        assert_eq!(diff.diff_graph[0].id, removed_block_group.id);
        assert_eq!(
            diff.diff_graph[0].source_block_group.as_ref(),
            Some(&removed_block_group)
        );
        assert!(diff.diff_graph[0].target_block_group.is_none());
        assert!(
            diff.diff_graph[0]
                .graph
                .nodes()
                .any(|node| node.change.kind == DiffChangeKind::Removed
                    && node.change.operation == Some(head)),
            "removed nodes should be marked as removed and attributed to the removing operation"
        );
        assert!(
            diff.diff_graph[0]
                .graph
                .all_edges()
                .any(|(_, _, edges)| edges
                    .iter()
                    .any(|edge| edge.change.kind == DiffChangeKind::Removed
                        && edge.change.operation == Some(head))),
            "removed edges should be marked as removed and attributed to the removing operation"
        );
    }

    #[test]
    fn test_modified_block_group_returns_added_and_removed_markers_in_unified_diff_graph() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        let block_group = create_linear_block_group(&context, "c", "s", "bg", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        replace_linear_block_group_path(&context, &block_group, "replacement", "CCCCC");
        let head = commit_operation(&context, "replace bg path");

        let diff =
            collect_operation_diff(graph_conn, Some(base), head, DiffRange::TwoDot).expect("diff");
        assert_eq!(diff.diff_graph.len(), 1);
        assert!(diff.diff_graph[0].source_block_group.is_some());
        assert!(diff.diff_graph[0].target_block_group.is_some());
        assert!(
            diff.diff_graph[0]
                .graph
                .nodes()
                .any(|node| node.change.kind == DiffChangeKind::Added),
            "modified block group should contain added nodes"
        );
        assert!(
            diff.diff_graph[0]
                .graph
                .nodes()
                .any(|node| node.change.kind == DiffChangeKind::Removed),
            "modified block group should contain removed nodes"
        );
        assert!(
            diff.diff_graph[0]
                .graph
                .all_edges()
                .any(|(_, _, edges)| edges
                    .iter()
                    .any(|edge| edge.change.kind == DiffChangeKind::Added)),
            "modified block group should contain added edges"
        );
        assert!(
            diff.diff_graph[0]
                .graph
                .all_edges()
                .any(|(_, _, edges)| edges
                    .iter()
                    .any(|edge| edge.change.kind == DiffChangeKind::Removed)),
            "modified block group should contain removed edges"
        );
    }

    #[test]
    fn test_source_graph_plus_dolt_changes_reconstructs_target_graph() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        let block_group = create_linear_block_group(&context, "c", "s", "bg", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        replace_linear_block_group_path(&context, &block_group, "replacement", "CCCCC");
        let head = commit_operation(&context, "replace bg path");
        let source_ref = commit_hash_ref(base).0;
        let target_ref = commit_hash_ref(head).0;

        let source_edges =
            BlockGroupEdge::edges_for_block_group(graph_conn, &block_group.id, Some(&source_ref));
        let changes = collect_block_group_changes(
            graph_conn,
            &DiffRange::TwoDot.between(&source_ref, &target_ref),
        )
        .expect("should collect block group changes");
        let edge_changes = changes
            .edge_changes
            .get(&block_group.id)
            .expect("should contain edge changes for replaced path");
        let reconstructed_edges =
            apply_edge_changes(graph_conn, &source_edges, edge_changes, &target_ref)
                .expect("should apply edge changes");

        let reconstructed_blocks = Edge::blocks_from_edges(
            graph_conn,
            context.workspace(),
            &block_group.id,
            &reconstructed_edges,
            Some(&target_ref),
        )
        .expect("should build reconstructed blocks");
        let target_edges =
            BlockGroupEdge::edges_for_block_group(graph_conn, &block_group.id, Some(&target_ref));
        let target_blocks = Edge::blocks_from_edges(
            graph_conn,
            context.workspace(),
            &block_group.id,
            &target_edges,
            Some(&target_ref),
        )
        .expect("should build target blocks");
        let reconstructed_graph = build_diff_input(
            &reconstructed_blocks,
            &reconstructed_edges,
            &HashMap::new(),
            &HashMap::new(),
        );
        let target_graph = build_diff_input(
            &target_blocks,
            &target_edges,
            &HashMap::new(),
            &HashMap::new(),
        );

        assert_eq!(
            graph_edge_keys(reconstructed_graph.graph()),
            graph_edge_keys(target_graph.graph())
        );
        assert_eq!(
            reconstructed_graph.graph().nodes().collect::<HashSet<_>>(),
            target_graph.graph().nodes().collect::<HashSet<_>>()
        );
    }

    #[test]
    fn test_graph_edge_keys_ignore_created_on() {
        let source = GraphNode {
            node_id: HashId::convert_str("source"),
            sequence_start: 0,
            sequence_end: 4,
        };
        let target = GraphNode {
            node_id: HashId::convert_str("target"),
            sequence_start: 0,
            sequence_end: 4,
        };
        let edge_id = HashId::convert_str("edge");
        let mut left_graph = GenGraph::new();
        left_graph.add_edge(
            source,
            target,
            vec![GraphEdge {
                edge_id,
                source_strand: Strand::Forward,
                target_strand: Strand::Forward,
                chromosome_index: 0,
                phased: 0,
                created_on: 1,
            }],
        );
        let mut right_graph = GenGraph::new();
        right_graph.add_edge(
            source,
            target,
            vec![GraphEdge {
                edge_id,
                source_strand: Strand::Forward,
                target_strand: Strand::Forward,
                chromosome_index: 0,
                phased: 0,
                created_on: 2,
            }],
        );

        assert_eq!(
            graph_edge_keys(&left_graph),
            graph_edge_keys(&right_graph),
            "edge comparison should ignore block-group edge timestamps"
        );
    }

    #[test]
    fn test_diff_input_graph_keeps_endpoint_local_node_slices() {
        let operation = DoltHashId([100; 20]);
        let node_id = HashId::convert_str("reference-node");
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("AAAGTTTTTT")
            .build();
        let start_edge_id = HashId::convert_str("start-edge");
        let end_edge_id = HashId::convert_str("end-edge");
        let source_blocks = vec![GroupBlock::new(0, node_id, &sequence, 0, 10)];
        let target_blocks = vec![
            GroupBlock::new(0, node_id, &sequence, 0, 3),
            GroupBlock::new(1, node_id, &sequence, 4, 10),
        ];
        let input_edges = vec![
            test_augmented_edge(start_edge_id, PATH_START_NODE_ID, 0, node_id, 0),
            test_augmented_edge(end_edge_id, node_id, 10, PATH_END_NODE_ID, 0),
        ];
        let source_input_graph = build_diff_input(
            &source_blocks,
            &input_edges,
            &HashMap::new(),
            &HashMap::new(),
        );
        let target_input_graph = build_diff_input(
            &target_blocks,
            &input_edges,
            &HashMap::new(),
            &HashMap::from([(node_id, operation)]),
        );

        let source_nodes = source_input_graph.graph().nodes().collect::<HashSet<_>>();
        assert!(
            source_nodes.contains(&GraphNode {
                node_id,
                sequence_start: 0,
                sequence_end: 10,
            }),
            "source graph should preserve its own unsplit block"
        );
        assert!(
            !source_nodes.contains(&GraphNode {
                node_id,
                sequence_start: 3,
                sequence_end: 4,
            }),
            "source graph should not synthesize slices from target-only boundaries"
        );
        let target_nodes = target_input_graph.graph().nodes().collect::<HashSet<_>>();
        assert!(
            target_nodes.contains(&GraphNode {
                node_id,
                sequence_start: 0,
                sequence_end: 3,
            }) && target_nodes.contains(&GraphNode {
                node_id,
                sequence_start: 4,
                sequence_end: 10,
            }),
            "target graph should use target-local blocks"
        );
    }

    #[test]
    fn test_diff_input_graph_keeps_terminal_blocks_for_endpoint_lookup() {
        let node_id = HashId::convert_str("regular-node");
        let sequence = Sequence::new().sequence_type("DNA").sequence("ATC").build();
        let edge_id = HashId::convert_str("edge-to-start");
        let input_blocks = vec![
            test_terminal_block(0, PATH_START_NODE_ID),
            GroupBlock::new(1, node_id, &sequence, 0, 3),
            test_terminal_block(2, PATH_END_NODE_ID),
        ];
        let input_edges = vec![test_augmented_edge(
            edge_id,
            node_id,
            3,
            PATH_START_NODE_ID,
            0,
        )];

        let input_graph = build_diff_input(
            &input_blocks,
            &input_edges,
            &HashMap::new(),
            &HashMap::new(),
        );

        let regular_node = test_graph_node(node_id, 0, 3);
        assert!(
            input_graph
                .graph()
                .edge_weight(regular_node, path_start_graph_node())
                .is_some(),
            "terminal blocks should participate in endpoint lookup regardless of edge direction"
        );
    }

    #[test]
    fn test_unified_diff_graph_connects_deletion_and_addition_through_common_terminals() {
        // Before: start -> prefix -> deleted -> middle ------------> suffix -> end
        // After:  start -> prefix ------------> middle -> inserted -> suffix -> end
        // Ensure replacing the explicit middle -> suffix edge removes it even
        // though both endpoints survive, and adds both edges through inserted.
        let operation = DoltHashId([100; 20]);
        let prefix_node_id = HashId::convert_str("prefix");
        let deleted_node_id = HashId::convert_str("deleted");
        let middle_node_id = HashId::convert_str("middle");
        let inserted_node_id = HashId::convert_str("inserted");
        let suffix_node_id = HashId::convert_str("suffix");
        let prefix_sequence = Sequence::new().sequence_type("DNA").sequence("ATC").build();
        let deleted_sequence = Sequence::new().sequence_type("DNA").sequence("G").build();
        let middle_sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("ATCGAT")
            .build();
        let inserted_sequence = Sequence::new().sequence_type("DNA").sequence("AGA").build();
        let suffix_sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("CGATC")
            .build();
        let prefix_node = test_graph_node(prefix_node_id, 0, 3);
        let deleted_node = test_graph_node(deleted_node_id, 0, 1);
        let middle_node = test_graph_node(middle_node_id, 0, 6);
        let inserted_node = test_graph_node(inserted_node_id, 0, 3);
        let suffix_node = test_graph_node(suffix_node_id, 0, 5);
        let source_blocks = vec![
            test_terminal_block(0, PATH_START_NODE_ID),
            GroupBlock::new(1, prefix_node_id, &prefix_sequence, 0, 3),
            GroupBlock::new(2, deleted_node_id, &deleted_sequence, 0, 1),
            GroupBlock::new(3, middle_node_id, &middle_sequence, 0, 6),
            GroupBlock::new(4, suffix_node_id, &suffix_sequence, 0, 5),
            test_terminal_block(5, PATH_END_NODE_ID),
        ];
        let target_blocks = vec![
            test_terminal_block(0, PATH_START_NODE_ID),
            GroupBlock::new(1, prefix_node_id, &prefix_sequence, 0, 3),
            GroupBlock::new(2, middle_node_id, &middle_sequence, 0, 6),
            GroupBlock::new(3, inserted_node_id, &inserted_sequence, 0, 3),
            GroupBlock::new(4, suffix_node_id, &suffix_sequence, 0, 5),
            test_terminal_block(5, PATH_END_NODE_ID),
        ];
        let source_edges = vec![
            test_augmented_edge(
                HashId::convert_str("shared-start-edge"),
                PATH_START_NODE_ID,
                0,
                prefix_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("removed-prefix-deleted"),
                prefix_node_id,
                3,
                deleted_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("removed-deleted-middle"),
                deleted_node_id,
                1,
                middle_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("source-middle-suffix"),
                middle_node_id,
                6,
                suffix_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("shared-end-edge"),
                suffix_node_id,
                5,
                PATH_END_NODE_ID,
                0,
            ),
        ];
        let target_edges = vec![
            test_augmented_edge(
                HashId::convert_str("shared-start-edge"),
                PATH_START_NODE_ID,
                0,
                prefix_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("added-prefix-middle"),
                prefix_node_id,
                3,
                middle_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("added-middle-inserted"),
                middle_node_id,
                6,
                inserted_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("added-inserted-suffix"),
                inserted_node_id,
                3,
                suffix_node_id,
                0,
            ),
            test_augmented_edge(
                HashId::convert_str("shared-end-edge"),
                suffix_node_id,
                5,
                PATH_END_NODE_ID,
                0,
            ),
        ];
        let source_graph = build_diff_input(
            &source_blocks,
            &source_edges,
            &HashMap::new(),
            &HashMap::new(),
        );
        let target_graph = build_diff_input(
            &target_blocks,
            &target_edges,
            &HashMap::new(),
            &HashMap::new(),
        );
        let graph = build_unified_diff_graph(&source_graph, &target_graph, Some(operation));

        assert_eq!(
            graph
                .nodes()
                .filter(|node| node.node.node_id == PATH_START_NODE_ID)
                .count(),
            1,
            "source and target paths should share one start terminal"
        );
        assert_eq!(
            graph
                .nodes()
                .filter(|node| node.node.node_id == PATH_END_NODE_ID)
                .count(),
            1,
            "source and target paths should share one end terminal"
        );
        assert!(
            graph.nodes().any(
                |node| node.node == deleted_node && node.change.kind == DiffChangeKind::Removed
            ),
            "deleted G node should be rendered as removed"
        );
        assert!(
            graph.nodes().any(|node| node.node == inserted_node
                && node.change.kind == DiffChangeKind::Added),
            "inserted AGA node should be rendered as added"
        );
        assert_diff_edge_kind(&graph, prefix_node, deleted_node, DiffChangeKind::Removed);
        assert_diff_edge_kind(&graph, deleted_node, middle_node, DiffChangeKind::Removed);
        assert_diff_edge_kind(&graph, prefix_node, middle_node, DiffChangeKind::Added);
        assert_diff_edge_kind(&graph, middle_node, suffix_node, DiffChangeKind::Removed);
        assert_diff_edge_kind(&graph, middle_node, inserted_node, DiffChangeKind::Added);
        assert_diff_edge_kind(&graph, inserted_node, suffix_node, DiffChangeKind::Added);
        assert_all_nodes_connected_to(&graph, path_start_graph_node());
        assert_diff_edge_kind(
            &graph,
            path_start_graph_node(),
            prefix_node,
            DiffChangeKind::Unchanged,
        );
        assert_diff_edge_kind(
            &graph,
            suffix_node,
            path_end_graph_node(),
            DiffChangeKind::Unchanged,
        );
    }

    #[test]
    fn test_merges_multiple_history_operations_preserve_operation_attribution() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        create_linear_block_group(&context, "c", "s", "bg1", "one", "CCCCC");
        let first_operation = commit_operation(&context, "add one");
        create_linear_block_group(&context, "c", "s", "bg2", "two", "GGGGG");
        let second_operation = commit_operation(&context, "add two");

        let expected_first_change =
            collect_operation_diff(graph_conn, Some(base), first_operation, DiffRange::TwoDot)
                .expect("first operation diff")
                .diff_graph
                .into_iter()
                .flat_map(|block_group| block_group.graph.nodes().collect::<Vec<_>>())
                .find(|node| {
                    node.change.kind == DiffChangeKind::Added
                        && node.change.operation == Some(first_operation)
                })
                .expect("first operation should add a node");
        let expected_second_change = collect_operation_diff(
            graph_conn,
            Some(first_operation),
            second_operation,
            DiffRange::TwoDot,
        )
        .expect("second operation diff")
        .diff_graph
        .into_iter()
        .flat_map(|block_group| block_group.graph.nodes().collect::<Vec<_>>())
        .find(|node| {
            node.change.kind == DiffChangeKind::Added
                && node.change.operation == Some(second_operation)
        })
        .expect("second operation should add a node");

        let diff =
            collect_operation_diff(graph_conn, Some(base), second_operation, DiffRange::TwoDot)
                .expect("diff");
        assert_eq!(diff.operations, vec![first_operation, second_operation]);

        let merged_changes = diff
            .diff_graph
            .iter()
            .flat_map(|block_group| block_group.graph.nodes())
            .map(|node| (node.node, node.change.kind))
            .collect::<HashSet<_>>();

        assert!(
            merged_changes.contains(&(
                expected_first_change.node,
                expected_first_change.change.kind
            )),
            "merged diff should contain the node change introduced by the first operation"
        );
        assert!(
            merged_changes.contains(&(
                expected_second_change.node,
                expected_second_change.change.kind,
            )),
            "merged diff should contain the node change introduced by the second operation"
        );
    }

    #[test]
    fn test_merges_multiple_history_operations() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let op1 = commit_operation(&context, "seed");
        let bg_one = create_linear_block_group(&context, "c", "s", "bg1", "one", "CCCCC");
        let op2 = commit_operation(&context, "add one");
        let bg_two = create_linear_block_group(&context, "c", "s", "bg2", "two", "GGGGG");
        let op3 = commit_operation(&context, "add two");

        let diff =
            collect_operation_diff(graph_conn, Some(op1), op3, DiffRange::TwoDot).expect("diff");
        assert_eq!(diff.operations, vec![op2, op3]);
        assert_eq!(diff.diff_graph.len(), 2);
        assert_eq!(
            diff.diff_graph
                .iter()
                .map(|block_group| block_group.id)
                .collect::<Vec<_>>(),
            vec![bg_one.id, bg_two.id]
        );
    }

    #[test]
    fn test_diff_to_non_head_commit_uses_target_history_state() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let bg_one = create_linear_block_group(&context, "c", "s", "bg1", "one", "CCCCC");
        let op_one = commit_operation(&context, "add one");
        let bg_two = create_linear_block_group(&context, "c", "s", "bg2", "two", "GGGGG");
        let op_two = commit_operation(&context, "add two");

        let diff = collect_operation_diff(graph_conn, Some(base), op_one, DiffRange::TwoDot)
            .expect("diff");
        assert_eq!(diff.operations, vec![op_one]);
        assert_eq!(diff.diff_graph.len(), 1);
        assert_eq!(diff.diff_graph[0].id, bg_one.id);
        assert_ne!(diff.diff_graph[0].id, bg_two.id);

        let later_diff =
            collect_operation_diff(graph_conn, Some(op_one), op_two, DiffRange::TwoDot)
                .expect("diff");
        assert_eq!(later_diff.operations, vec![op_two]);
        assert_eq!(later_diff.diff_graph.len(), 1);
        assert_eq!(later_diff.diff_graph[0].id, bg_two.id);
    }

    #[test]
    fn diff_against_itself_is_empty() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();
        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let diff =
            collect_operation_diff(graph_conn, Some(base), base, DiffRange::TwoDot).expect("diff");
        assert!(diff.operations.is_empty());
    }

    #[test]
    fn test_two_dot_diff_reports_changes_on_both_divergent_branches() {
        let history = setup_divergent_history();
        let graph_conn = history.context.graph().conn();
        let source_ref = history.source_hash.to_string();
        let target_ref = history.target_hash.to_string();
        let raw_changes = collect_block_group_changes(
            graph_conn,
            &DiffRange::TwoDot.between(&source_ref, &target_ref),
        )
        .expect("should collect changes across branches");
        assert_eq!(
            raw_changes.block_group_ids,
            HashSet::from([history.source_block_group.id, history.target_block_group.id,]),
            "Dolt diff rows should contain the source removal and target addition"
        );

        let diff = collect_operation_diff(
            graph_conn,
            Some(history.source_hash),
            history.target_hash,
            DiffRange::TwoDot,
        )
        .expect("should collect endpoint diff");
        assert_eq!(
            diff.operations,
            vec![history.source_hash, history.target_hash]
        );
        assert_eq!(
            diff.diff_graph.len(),
            2,
            "expected source {source:?} and target {target:?} changes, got block groups {:?}",
            diff.diff_graph
                .iter()
                .map(|block_group| block_group.id)
                .collect::<Vec<_>>(),
            source = history.source_block_group.id,
            target = history.target_block_group.id,
        );
        let source_diff = diff
            .diff_graph
            .iter()
            .find(|block_group| block_group.id == history.source_block_group.id)
            .expect("source-only block group should be reported as removed");
        assert!(source_diff.source_block_group.is_some());
        assert!(source_diff.target_block_group.is_none());
        let target_diff = diff
            .diff_graph
            .iter()
            .find(|block_group| block_group.id == history.target_block_group.id)
            .expect("target-only block group should be reported as added");
        assert!(target_diff.source_block_group.is_none());
        assert!(target_diff.target_block_group.is_some());
    }

    #[test]
    fn test_three_dot_diff_reports_target_changes_from_merge_base() {
        let history = setup_divergent_history();
        let merge_base_diff = collect_operation_diff(
            history.context.graph().conn(),
            Some(history.source_hash),
            history.target_hash,
            DiffRange::ThreeDot,
        )
        .expect("should collect merge-base diff");
        assert_eq!(merge_base_diff.operations, vec![history.target_hash]);
        assert_eq!(
            merge_base_diff.diff_graph.len(),
            1,
            "three-dot diff should only report target changes from the merge base"
        );
        let target_diff = &merge_base_diff.diff_graph[0];
        assert_eq!(target_diff.id, history.target_block_group.id);
        assert!(target_diff.source_block_group.is_none());
        assert!(target_diff.target_block_group.is_some());
    }

    #[test]
    fn test_uses_default_graph_database_for_history_diffs() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let expected_block_group =
            create_linear_block_group(&context, "c", "s", "db-one", "one", "CCCCC");
        let head = commit_operation(&context, "add");

        let diff =
            collect_operation_diff(graph_conn, Some(base), head, DiffRange::TwoDot).expect("diff");
        assert_eq!(diff.operations, vec![head]);
        assert_eq!(diff.diff_graph.len(), 1);
        assert_eq!(diff.diff_graph[0].id, expected_block_group.id);
    }

    #[test]
    fn test_diffs_follow_reset_replacement_history() {
        let mut context = setup_history_context();
        let graph_conn = context.graph().conn();
        let graph_db_path = context
            .workspace()
            .graph_db_path()
            .expect("should resolve graph db path");
        let history_store = DoltHistoryStore::new(graph_conn);

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let retained_block_group =
            create_linear_block_group(&context, "c", "s", "retained", "one", "CCCCC");
        let retained_commit = commit_operation(&context, "retained");
        let discarded_block_group =
            create_linear_block_group(&context, "c", "s", "discarded", "two", "GGGGG");
        commit_operation(&context, "discarded");

        history_store
            .reset_hard(&commit_hash_ref(retained_commit))
            .expect("should reset to retained commit");
        let reopened_graph =
            gen_models::db::get_connection(&graph_db_path).expect("should reopen graph db");
        gen_models::history::dolt::connect_branch(&reopened_graph, "main")
            .expect("should reconnect reopened main branch");
        context.set_graph(reopened_graph);
        let replacement_block_group =
            create_linear_block_group(&context, "c", "s", "replacement", "three", "TTTTT");
        let replacement_commit = commit_operation(&context, "replacement");
        let reopened_graph_after_replacement =
            gen_models::db::get_connection(&graph_db_path).expect("should reopen graph db");
        gen_models::history::dolt::connect_branch(&reopened_graph_after_replacement, "main")
            .expect("should reconnect reopened main branch");
        let discarded_exists = reopened_graph_after_replacement
            .query_row(
                "SELECT EXISTS(SELECT 1 FROM block_groups WHERE id = ?1)",
                params![discarded_block_group.id],
                |row| row.get::<_, bool>(0),
            )
            .expect("should query reopened reset snapshot");
        let graph_conn = context.graph().conn();
        let diff = collect_operation_diff(
            graph_conn,
            Some(base),
            replacement_commit,
            DiffRange::TwoDot,
        )
        .expect("diff");
        assert_eq!(diff.operations, vec![retained_commit, replacement_commit]);
        assert!(
            !discarded_exists,
            "discarded block group should not remain in the post-reset snapshot"
        );
        assert_eq!(diff.diff_graph.len(), 2);
        let added_ids = diff
            .diff_graph
            .iter()
            .map(|block_group| block_group.id)
            .collect::<HashSet<_>>();
        assert_eq!(
            added_ids,
            HashSet::from([retained_block_group.id, replacement_block_group.id])
        );
        assert!(
            diff.diff_graph
                .iter()
                .all(|block_group| block_group.id != discarded_block_group.id),
            "discarded commit should not survive the reset path"
        );
    }

    #[test]
    fn test_merge_commit_diff_includes_merged_branch_state() {
        let context = setup_history_context();
        let graph_conn = context.graph().conn();
        let history_store = DoltHistoryStore::new(graph_conn);

        create_linear_block_group(&context, "c", "s", "seed", "seed", "AAAAA");
        let base = commit_operation(&context, "seed");
        let main_block_group =
            create_linear_block_group(&context, "c", "s", "main", "main", "CCCCC");
        let main_commit = commit_operation(&context, "main");

        create_feature_branch(&context, base, "feature");
        let feature_block_group =
            create_linear_block_group(&context, "c", "s", "feature", "feature", "GGGGG");
        commit_operation(&context, "feature");

        switch_branch(&context, "main");
        graph_conn
            .with_transaction(|| history_store.merge(&CommitRef("feature".to_string())))
            .expect("should merge feature branch");
        let merge_commit = history_store
            .current_head()
            .expect("should resolve current head")
            .expect("should have merge head");
        let merge_hash = merge_commit;

        let diff = collect_operation_diff(graph_conn, Some(base), merge_hash, DiffRange::TwoDot)
            .expect("diff");
        assert!(
            diff.operations.contains(&main_commit),
            "diff should include main-branch history"
        );
        assert!(
            diff.operations.contains(&merge_hash),
            "diff should include the merge commit"
        );
        let added_ids = diff
            .diff_graph
            .iter()
            .map(|block_group| block_group.id)
            .collect::<HashSet<_>>();
        assert_eq!(
            added_ids,
            HashSet::from([main_block_group.id, feature_block_group.id])
        );
    }

    fn assert_block_group_graph_shape(block_group_diff: &BlockGroupDiff) {
        assert_eq!(block_group_diff.graph.nodes().count(), 3);
        assert_eq!(block_group_diff.graph.all_edges().count(), 2);
    }
}