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
// Copyright 2018-2019 Mozilla

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
    collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
    fmt, mem,
};

use crate::driver::{AbortSignal, DefaultAbortSignal, DefaultDriver, Driver};
use crate::error::{ErrorKind, Result};
use crate::guid::{Guid, IsValidGuid, TAGS_GUID};
use crate::tree::{Content, MergeState, MergedNode, Node, Tree, Validity};

/// Structure change types, used to indicate if a node on one side is moved
/// or deleted on the other.
#[derive(Eq, PartialEq)]
enum StructureChange {
    /// Node not deleted, or doesn't exist, on the other side.
    Unchanged,
    /// Node moved on the other side.
    Moved,
    /// Node deleted on the other side.
    Deleted,
}

/// Records structure change counters for telemetry.
#[derive(Clone, Copy, Default, Debug, Eq, Hash, PartialEq)]
pub struct StructureCounts {
    /// Remote non-folder change wins over local deletion.
    pub remote_revives: usize,
    /// Local folder deletion wins over remote change.
    pub local_deletes: usize,
    /// Local non-folder change wins over remote deletion.
    pub local_revives: usize,
    /// Remote folder deletion wins over local change.
    pub remote_deletes: usize,
    /// Deduped local items.
    pub dupes: usize,
    /// Total number of nodes in the merged tree, excluding the
    /// root.
    pub merged_nodes: usize,
    /// Total number of deletions to apply, local and remote.
    pub merged_deletions: usize,
}

/// Holds (matching remote dupes for local GUIDs, matching local dupes for
/// remote GUIDs).
type MatchingDupes<'t> = (HashMap<Guid, Node<'t>>, HashMap<Guid, Node<'t>>);

/// Represents an accepted local or remote deletion.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Deletion<'t> {
    pub guid: &'t Guid,
    pub local_level: i64,
    pub should_upload_tombstone: bool,
}

/// Indicates which side to take in case of a merge conflict.
#[derive(Clone, Copy, Debug)]
enum ConflictResolution {
    Local,
    Remote,
    Unchanged,
}

/// A hash key used to match dupes by content.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum DupeKey<'a> {
    /// Matches a dupe by content only. Used for bookmarks, queries, folders,
    /// and livemarks.
    WithoutPosition(&'a Content),
    /// Matches a dupe by content and position. Used for separators.
    WithPosition(&'a Content, usize),
}

/// A two-way merger that produces a complete merged tree from a complete local
/// tree and a complete remote tree with changes since the last sync.
///
/// This is ported almost directly from iOS. On iOS, the `ThreeWayMerger` takes
/// a complete "mirror" tree with the server state after the last sync, and two
/// incomplete trees with local and remote changes to the mirror: "local" and
/// "mirror", respectively. Overlaying buffer onto mirror yields the current
/// server tree; overlaying local onto mirror yields the complete local tree.
///
/// Dogear doesn't store the shared parent for changed items, so we can only
/// do two-way merges. Our local tree is the union of iOS's mirror and local,
/// and our remote tree is the union of iOS's mirror and buffer.
///
/// Unlike iOS, Dogear doesn't distinguish between structure and value changes.
/// The `needs_merge` flag notes *that* a bookmark changed, but not *how*. This
/// means we might detect conflicts, and revert changes on one side, for cases
/// that iOS can merge cleanly.
///
/// Fortunately, most of our users don't organize their bookmarks into deeply
/// nested hierarchies, or make conflicting changes on multiple devices
/// simultaneously. A simpler two-way tree merge strikes a good balance between
/// correctness and complexity.
pub struct Merger<'t, D = DefaultDriver, A = DefaultAbortSignal> {
    driver: &'t D,
    signal: &'t A,
    local_tree: &'t Tree,
    remote_tree: &'t Tree,
    matching_dupes_by_local_parent_guid: HashMap<Guid, MatchingDupes<'t>>,
    merged_guids: HashSet<Guid>,
    delete_locally: HashSet<Guid>,
    delete_remotely: HashSet<Guid>,
    structure_counts: StructureCounts,
}

impl<'t> Merger<'t, DefaultDriver, DefaultAbortSignal> {
    /// Creates a merger with the default merge driver.
    pub fn new(local_tree: &'t Tree, remote_tree: &'t Tree) -> Merger<'t> {
        Merger {
            driver: &DefaultDriver,
            signal: &DefaultAbortSignal,
            local_tree,
            remote_tree,
            matching_dupes_by_local_parent_guid: HashMap::new(),
            merged_guids: HashSet::new(),
            delete_locally: HashSet::new(),
            delete_remotely: HashSet::new(),
            structure_counts: StructureCounts::default(),
        }
    }
}

impl<'t, D: Driver, A: AbortSignal> Merger<'t, D, A> {
    /// Creates a merger with the given merge driver and contents.
    pub fn with_driver(
        driver: &'t D,
        signal: &'t A,
        local_tree: &'t Tree,
        remote_tree: &'t Tree,
    ) -> Merger<'t, D, A> {
        Merger {
            driver,
            signal,
            local_tree,
            remote_tree,
            matching_dupes_by_local_parent_guid: HashMap::new(),
            merged_guids: HashSet::new(),
            delete_locally: HashSet::new(),
            delete_remotely: HashSet::new(),
            structure_counts: StructureCounts::default(),
        }
    }

    /// Builds a merged tree from the local and remote trees.
    pub fn merge(mut self) -> Result<MergedRoot<'t>> {
        let merged_root_node = {
            let local_root_node = self.local_tree.root();
            let remote_root_node = self.remote_tree.root();
            self.two_way_merge(local_root_node, remote_root_node)?
        };

        // Any remaining deletions on one side should be deleted on the other side.
        // This happens when the remote tree has tombstones for items that don't
        // exist locally, or the local tree has tombstones for items that
        // aren't on the server.
        for guid in self.local_tree.deletions() {
            self.signal.err_if_aborted()?;
            if !self.mentions(guid) {
                self.delete_remotely.insert(guid.clone());
                self.structure_counts.merged_deletions += 1;
            }
        }
        for guid in self.remote_tree.deletions() {
            self.signal.err_if_aborted()?;
            if !self.mentions(guid) {
                self.delete_locally.insert(guid.clone());
                self.structure_counts.merged_deletions += 1;
            }
        }

        // The merged tree should know about all items mentioned in the local
        // and remote trees. Otherwise, it's incomplete, and we can't apply it.
        // This indicates a bug in the merger.
        for guid in self.local_tree.guids() {
            self.signal.err_if_aborted()?;
            if !self.mentions(guid) {
                return Err(ErrorKind::UnmergedLocalItems.into());
            }
        }
        for guid in self.remote_tree.guids() {
            self.signal.err_if_aborted()?;
            if !self.mentions(guid) {
                return Err(ErrorKind::UnmergedRemoteItems.into());
            }
        }

        Ok(MergedRoot {
            local_tree: self.local_tree,
            remote_tree: self.remote_tree,
            node: merged_root_node,
            merged_guids: self.merged_guids,
            delete_locally: self.delete_locally,
            delete_remotely: self.delete_remotely,
            structure_counts: self.structure_counts,
        })
    }

    #[inline]
    fn mentions(&self, guid: &Guid) -> bool {
        self.merged_guids.contains(guid)
            || self.delete_locally.contains(guid)
            || self.delete_remotely.contains(guid)
    }

    fn merge_local_only_node(&mut self, local_node: Node<'t>) -> Result<MergedNode<'t>> {
        trace!(self.driver, "Item {} only exists locally", local_node);

        self.merged_guids.insert(local_node.guid.clone());

        let merged_guid = if local_node.guid.is_valid_guid() {
            local_node.guid.clone()
        } else {
            warn!(
                self.driver,
                "Generating new GUID for local node {}", local_node
            );
            self.signal.err_if_aborted()?;
            let new_guid = self.driver.generate_new_guid(&local_node.guid)?;
            if new_guid != local_node.guid {
                if self.merged_guids.contains(&new_guid) {
                    return Err(ErrorKind::DuplicateItem(new_guid).into());
                }
                self.merged_guids.insert(new_guid.clone());
            }
            new_guid
        };

        let mut merged_node = MergedNode::new(merged_guid, MergeState::LocalOnly(local_node));
        // The local folder doesn't exist remotely, but its children might, so
        // we still need to recursively walk and merge them. This method will
        // change the merge state from local to new if any children were moved
        // or deleted.
        for local_child_node in local_node.children() {
            self.signal.err_if_aborted()?;
            self.merge_local_child_into_merged_node(
                &mut merged_node,
                local_node,
                None,
                local_child_node,
            )?;
        }

        if local_node.diverged() {
            merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
        }

        Ok(merged_node)
    }

    fn merge_remote_only_node(&mut self, remote_node: Node<'t>) -> Result<MergedNode<'t>> {
        trace!(self.driver, "Item {} only exists remotely", remote_node);

        self.merged_guids.insert(remote_node.guid.clone());

        let merged_guid = if remote_node.guid.is_valid_guid() {
            remote_node.guid.clone()
        } else {
            warn!(
                self.driver,
                "Generating new GUID for remote node {}", remote_node
            );
            self.signal.err_if_aborted()?;
            let new_guid = self.driver.generate_new_guid(&remote_node.guid)?;
            if new_guid != remote_node.guid {
                if self.merged_guids.contains(&new_guid) {
                    return Err(ErrorKind::DuplicateItem(new_guid).into());
                }
                self.merged_guids.insert(new_guid.clone());
                // Upload tombstones for changed remote GUIDs.
                self.delete_remotely.insert(remote_node.guid.clone());
                self.structure_counts.merged_deletions += 1;
            }
            new_guid
        };
        let mut merged_node = MergedNode::new(merged_guid, MergeState::RemoteOnly(remote_node));
        // As above, a remote folder's children might still exist locally, so we
        // need to merge them and update the merge state from remote to new if
        // any children were moved or deleted.
        for remote_child_node in remote_node.children() {
            self.signal.err_if_aborted()?;
            self.merge_remote_child_into_merged_node(
                &mut merged_node,
                None,
                remote_node,
                remote_child_node,
            )?;
        }

        if remote_node.diverged()
            || merged_node.remote_guid_changed()
            || remote_node.validity != Validity::Valid
        {
            // If the remote structure diverged, the merged item's GUID changed,
            // or the item isn't valid, flag it for reupload.
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
        }

        Ok(merged_node)
    }

    /// Merges two nodes that exist locally and remotely.
    fn two_way_merge(
        &mut self,
        local_node: Node<'t>,
        remote_node: Node<'t>,
    ) -> Result<MergedNode<'t>> {
        trace!(
            self.driver,
            "Item exists locally as {} and remotely as {}",
            local_node,
            remote_node
        );

        if !local_node.has_compatible_kind(&remote_node) {
            error!(
                self.driver,
                "Merging local {} and remote {} with different kinds", local_node, remote_node
            );
            return Err(ErrorKind::MismatchedItemKind(local_node.kind, remote_node.kind).into());
        }

        self.merged_guids.insert(local_node.guid.clone());
        self.merged_guids.insert(remote_node.guid.clone());

        let merged_guid = if remote_node.guid.is_valid_guid() {
            remote_node.guid.clone()
        } else {
            warn!(
                self.driver,
                "Generating new valid GUID for node {}", remote_node
            );
            self.signal.err_if_aborted()?;
            let new_guid = self.driver.generate_new_guid(&remote_node.guid)?;
            if new_guid != remote_node.guid {
                if self.merged_guids.contains(&new_guid) {
                    return Err(ErrorKind::DuplicateItem(new_guid).into());
                }
                self.merged_guids.insert(new_guid.clone());
                // Upload tombstones for changed remote GUIDs.
                self.delete_remotely.insert(remote_node.guid.clone());
                self.structure_counts.merged_deletions += 1;
            }
            new_guid
        };

        let (item, children) = self.resolve_value_conflict(local_node, remote_node);

        let mut merged_node = MergedNode::new(
            merged_guid,
            match item {
                ConflictResolution::Local => MergeState::Local {
                    local_node,
                    remote_node,
                },
                ConflictResolution::Remote => MergeState::Remote {
                    local_node,
                    remote_node,
                },
                ConflictResolution::Unchanged => MergeState::Unchanged {
                    local_node,
                    remote_node,
                },
            },
        );

        match children {
            ConflictResolution::Local => {
                for local_child_node in local_node.children() {
                    self.signal.err_if_aborted()?;
                    self.merge_local_child_into_merged_node(
                        &mut merged_node,
                        local_node,
                        Some(remote_node),
                        local_child_node,
                    )?;
                }
                for remote_child_node in remote_node.children() {
                    self.signal.err_if_aborted()?;
                    self.merge_remote_child_into_merged_node(
                        &mut merged_node,
                        Some(local_node),
                        remote_node,
                        remote_child_node,
                    )?;
                }
            }

            ConflictResolution::Remote => {
                for remote_child_node in remote_node.children() {
                    self.signal.err_if_aborted()?;
                    self.merge_remote_child_into_merged_node(
                        &mut merged_node,
                        Some(local_node),
                        remote_node,
                        remote_child_node,
                    )?;
                }
                for local_child_node in local_node.children() {
                    self.signal.err_if_aborted()?;
                    self.merge_local_child_into_merged_node(
                        &mut merged_node,
                        local_node,
                        Some(remote_node),
                        local_child_node,
                    )?;
                }
            }

            ConflictResolution::Unchanged => {
                // The children are the same, so we only need to merge one side.
                for (local_child_node, remote_child_node) in
                    local_node.children().zip(remote_node.children())
                {
                    self.signal.err_if_aborted()?;
                    self.merge_unchanged_child_into_merged_node(
                        &mut merged_node,
                        local_node,
                        local_child_node,
                        remote_node,
                        remote_child_node,
                    )?;
                }
            }
        }

        if local_node.diverged() {
            merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
        }

        if remote_node.diverged() || remote_node.validity != Validity::Valid {
            // Flag remotely diverged and invalid items for reupload.
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
        }

        Ok(merged_node)
    }

    /// Merges two nodes with the same parents and positions.
    ///
    /// Unlike items that have been moved, or exist only on one side, unchanged
    /// children can be merged directly.
    fn merge_unchanged_child_into_merged_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        local_parent_node: Node<'t>,
        local_child_node: Node<'t>,
        remote_parent_node: Node<'t>,
        remote_child_node: Node<'t>,
    ) -> Result<()> {
        assert!(
            !self.merged_guids.contains(&local_child_node.guid),
            "Unchanged local child shouldn't have been merged"
        );
        assert!(
            !self.merged_guids.contains(&remote_child_node.guid),
            "Unchanged remote child shouldn't have been merged"
        );

        // Even though the child exists on both sides, it might still be
        // non-syncable or invalid, so we need to check for structure
        // changes.
        let local_structure_change = self.check_for_local_structure_change_of_remote_node(
            merged_node,
            remote_parent_node,
            remote_child_node,
        )?;
        let remote_structure_change = self.check_for_remote_structure_change_of_local_node(
            merged_node,
            local_parent_node,
            local_child_node,
        )?;
        match (local_structure_change, remote_structure_change) {
            (StructureChange::Deleted, StructureChange::Deleted) => {
                // The child is deleted on both sides. We'll need to reupload
                // and apply a new structure.
                merged_node.merge_state = merged_node
                    .merge_state
                    .with_new_local_structure()
                    .with_new_remote_structure();
            }
            (StructureChange::Deleted, _) => {
                // The child is deleted locally, but not remotely, so we only
                // need to reupload a new structure.
                merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
            }
            (_, StructureChange::Deleted) => {
                // The child is deleted remotely, so we only need to apply a
                // new local structure.
                merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
            }
            (_, _) => {
                // The child exists on both sides, so merge it now. If the GUID
                // changes because it's invalid, we'll need to reapply the
                // child, and reupload the child and its parent.
                let mut merged_child_node =
                    self.two_way_merge(local_child_node, remote_child_node)?;
                if merged_child_node.local_guid_changed() {
                    merged_child_node.merge_state =
                        merged_child_node.merge_state.with_new_local_structure();
                }
                if merged_node.remote_guid_changed() {
                    // The merged parent's GUID changed; flag the child for
                    // reupload with a new `parentid`.
                    merged_child_node.merge_state =
                        merged_child_node.merge_state.with_new_remote_structure();
                }
                if merged_child_node.remote_guid_changed() {
                    // The merged child's GUID changed; flag the parent for
                    // reupload with new `children`.
                    merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
                }
                merged_node.merged_children.push(merged_child_node);
                self.structure_counts.merged_nodes += 1;
            }
        }

        Ok(())
    }

    /// Merges a remote child node into a merged folder node. This handles the
    /// following cases:
    ///
    /// - The remote child is locally deleted. We recursively move all of its
    ///   descendants that don't exist locally to the merged folder.
    /// - The remote child doesn't exist locally, but has a content match in the
    ///   corresponding local folder. We dedupe the local child to the remote
    ///   child.
    /// - The remote child exists locally, but in a different folder. We compare
    ///   merge flags and timestamps to decide where to keep the child.
    /// - The remote child exists locally, and in the same folder. We merge the
    ///   local and remote children.
    ///
    /// This is the inverse of `merge_local_child_into_merged_node`.
    fn merge_remote_child_into_merged_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        local_parent_node: Option<Node<'t>>,
        remote_parent_node: Node<'t>,
        remote_child_node: Node<'t>,
    ) -> Result<()> {
        if self.merged_guids.contains(&remote_child_node.guid) {
            trace!(
                self.driver,
                "Remote child {} already seen in another folder and merged",
                remote_child_node
            );
            // Omitting a remote child that we already merged locally means we
            // have a new remote structure.
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
            return Ok(());
        }

        trace!(
            self.driver,
            "Merging remote child {} of {} into {}",
            remote_child_node,
            remote_parent_node,
            merged_node
        );

        // Check if the remote child is locally deleted. and move all
        // descendants that aren't also remotely deleted to the merged node.
        // This handles the case where a user deletes a folder on this device,
        // and adds a bookmark to the same folder on another device. We want to
        // keep the folder deleted, but we also don't want to lose the new
        // bookmark, so we move the bookmark to the deleted folder's parent.
        if self.check_for_local_structure_change_of_remote_node(
            merged_node,
            remote_parent_node,
            remote_child_node,
        )? == StructureChange::Deleted
        {
            // Flag the merged parent for reupload, since we deleted the
            // remote child.
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
            return Ok(());
        }

        // The remote child isn't locally deleted. Does it exist in the local tree?
        if let Some(local_child_node) = self.local_tree.node_for_guid(&remote_child_node.guid) {
            // The remote child exists in the local tree. Did it move?
            let local_parent_node = local_child_node
                .parent()
                .expect("Can't merge existing remote child without local parent");

            trace!(
                self.driver,
                "Remote child {} exists locally in {} and remotely in {}",
                remote_child_node,
                local_parent_node,
                remote_parent_node
            );

            if self.remote_tree.is_deleted(&local_parent_node.guid) {
                trace!(
                    self.driver,
                    "Unconditionally taking remote move for {} to {} because local parent {} is \
                     deleted remotely",
                    remote_child_node,
                    remote_parent_node,
                    local_parent_node
                );

                let mut merged_child_node =
                    self.two_way_merge(local_child_node, remote_child_node)?;
                merged_child_node.merge_state =
                    merged_child_node.merge_state.with_new_local_structure();
                if merged_node.remote_guid_changed() {
                    // If the parent's GUID changed, flag the child for reupload, so that
                    // its `parentid` is correct.
                    merged_child_node.merge_state =
                        merged_child_node.merge_state.with_new_remote_structure();
                }
                if merged_child_node.remote_guid_changed() {
                    // If the child's GUID changed, flag the parent for reupload, so that
                    // its `children` are correct.
                    merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
                }
                merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
                merged_node.merged_children.push(merged_child_node);
                self.structure_counts.merged_nodes += 1;
                return Ok(());
            }

            match self.resolve_structure_conflict(
                local_parent_node,
                local_child_node,
                remote_parent_node,
                remote_child_node,
            ) {
                ConflictResolution::Local => {
                    // The local move is newer, so we ignore the remote move.
                    // We'll merge the remote child later, when we walk its new
                    // local parent.
                    trace!(
                        self.driver,
                        "Remote child {} moved locally to {} and remotely to {}; \
                         keeping child in newer local parent and position",
                        remote_child_node,
                        local_parent_node,
                        remote_parent_node
                    );

                    // Flag the old parent for reupload, since we're moving
                    // the remote child. Note that, since we only flag the
                    // remote parent here, we don't need to handle
                    // reparenting and repositioning separately.
                    merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
                }

                ConflictResolution::Remote | ConflictResolution::Unchanged => {
                    // The remote move is newer, so we merge the remote
                    // child now and ignore the local move.
                    let mut merged_child_node = if local_parent_node.guid != remote_parent_node.guid
                    {
                        trace!(
                            self.driver,
                            "Remote child {} reparented locally to {} and remotely to {}; \
                             keeping child in newer remote parent",
                            remote_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                        let mut merged_child_node =
                            self.two_way_merge(local_child_node, remote_child_node)?;
                        merged_child_node.merge_state =
                            merged_child_node.merge_state.with_new_local_structure();
                        merged_child_node
                    } else {
                        trace!(
                            self.driver,
                            "Remote child {} repositioned locally in {} and remotely in {}; \
                             keeping child in newer remote position",
                            remote_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                        self.two_way_merge(local_child_node, remote_child_node)?
                    };
                    if merged_child_node.local_guid_changed() {
                        merged_child_node.merge_state =
                            merged_child_node.merge_state.with_new_local_structure();
                    }
                    if merged_node.remote_guid_changed() {
                        // The merged parent's GUID changed; flag the child for
                        // reupload with a new `parentid`.
                        merged_child_node.merge_state =
                            merged_child_node.merge_state.with_new_remote_structure();
                    }
                    if merged_child_node.remote_guid_changed() {
                        // The merged child's GUID changed; flag the parent for
                        // reupload with new `children`.
                        merged_node.merge_state =
                            merged_node.merge_state.with_new_remote_structure();
                    }
                    merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
                    merged_node.merged_children.push(merged_child_node);
                    self.structure_counts.merged_nodes += 1;
                }
            }

            return Ok(());
        }

        // Remote child is not a root, and doesn't exist locally. Try to find a
        // content match in the containing folder, and dedupe the local item if
        // we can.
        trace!(
            self.driver,
            "Remote child {} doesn't exist locally; looking for local content match",
            remote_child_node
        );

        let mut merged_child_node = if let Some(local_child_node_by_content) = self
            .find_local_node_matching_remote_node(
                merged_node,
                local_parent_node,
                remote_parent_node,
                remote_child_node,
            )? {
            self.two_way_merge(local_child_node_by_content, remote_child_node)
        } else {
            self.merge_remote_only_node(remote_child_node)
        }?;
        if merged_child_node.local_guid_changed() {
            merged_child_node.merge_state =
                merged_child_node.merge_state.with_new_local_structure();
        }
        if merged_node.remote_guid_changed() {
            merged_child_node.merge_state =
                merged_child_node.merge_state.with_new_remote_structure();
        }
        if merged_child_node.remote_guid_changed() {
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
        }
        merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
        merged_node.merged_children.push(merged_child_node);
        self.structure_counts.merged_nodes += 1;
        Ok(())
    }

    /// Merges a local child node into a merged folder node.
    ///
    /// This is the inverse of `merge_remote_child_into_merged_node`.
    fn merge_local_child_into_merged_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        local_parent_node: Node<'t>,
        remote_parent_node: Option<Node<'t>>,
        local_child_node: Node<'t>,
    ) -> Result<()> {
        if self.merged_guids.contains(&local_child_node.guid) {
            // We already merged the child when we walked another folder. Since
            // a tree can't have duplicate GUIDs, we must have merged the remote
            // child, so we have a new local structure.
            trace!(
                self.driver,
                "Local child {} already seen in another folder and merged",
                local_child_node
            );
            merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
            return Ok(());
        }

        trace!(
            self.driver,
            "Merging local child {} of {} into {}",
            local_child_node,
            local_parent_node,
            merged_node
        );

        // Check if the local child is remotely deleted, and move any new local
        // descendants to the merged node if so.
        if self.check_for_remote_structure_change_of_local_node(
            merged_node,
            local_parent_node,
            local_child_node,
        )? == StructureChange::Deleted
        {
            // Since we're merging local nodes, we don't need to flag the merged
            // parent for reupload.
            merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
            return Ok(());
        }

        // At this point, we know the local child isn't deleted. See if it
        // exists in the remote tree.
        if let Some(remote_child_node) = self.remote_tree.node_for_guid(&local_child_node.guid) {
            // The local child exists remotely. It must have moved; otherwise, we
            // would have seen it when we walked the remote children.
            let remote_parent_node = remote_child_node
                .parent()
                .expect("Can't merge existing local child without remote parent");

            trace!(
                self.driver,
                "Local child {} exists locally in {} and remotely in {}",
                local_child_node,
                local_parent_node,
                remote_parent_node
            );

            if self.local_tree.is_deleted(&remote_parent_node.guid) {
                trace!(
                    self.driver,
                    "Unconditionally taking local move for {} to {} because remote parent {} is \
                     deleted locally",
                    local_child_node,
                    local_parent_node,
                    remote_parent_node
                );

                // Merge and flag the new parent *and the locally moved child* for
                // reupload. The parent references the child in its `children`; the
                // child points back to the parent in its `parentid`.
                //
                // Reuploading the child isn't necessary for newer Desktops, which
                // ignore the child's `parentid` and use the parent's `children`.
                //
                // However, older Desktop and Android use the child's `parentid` as
                // canonical, while iOS is stricter and requires both to match.
                let mut merged_child_node =
                    self.two_way_merge(local_child_node, remote_child_node)?;
                if merged_child_node.local_guid_changed() {
                    merged_child_node.merge_state =
                        merged_child_node.merge_state.with_new_local_structure();
                }
                merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
                merged_child_node.merge_state =
                    merged_child_node.merge_state.with_new_remote_structure();
                merged_node.merged_children.push(merged_child_node);
                self.structure_counts.merged_nodes += 1;
                return Ok(());
            }

            match self.resolve_structure_conflict(
                local_parent_node,
                local_child_node,
                remote_parent_node,
                remote_child_node,
            ) {
                ConflictResolution::Local => {
                    // The local move is newer, so we merge the local child now
                    // and ignore the remote move.
                    if local_parent_node.guid != remote_parent_node.guid {
                        // The child moved to a different folder.
                        trace!(
                            self.driver,
                            "Local child {} reparented locally to {} and remotely to {}; \
                             keeping child in newer local parent",
                            local_child_node,
                            local_parent_node,
                            remote_parent_node
                        );

                        // Merge and flag both the new parent and child for
                        // reupload. See above for why.
                        let mut merged_child_node =
                            self.two_way_merge(local_child_node, remote_child_node)?;
                        if merged_child_node.local_guid_changed() {
                            merged_child_node.merge_state =
                                merged_child_node.merge_state.with_new_local_structure();
                        }
                        merged_node.merge_state =
                            merged_node.merge_state.with_new_remote_structure();
                        merged_child_node.merge_state =
                            merged_child_node.merge_state.with_new_remote_structure();
                        merged_node.merged_children.push(merged_child_node);
                        self.structure_counts.merged_nodes += 1;
                    } else {
                        trace!(
                            self.driver,
                            "Local child {} repositioned locally in {} and remotely in {}; \
                             keeping child in newer local position",
                            local_child_node,
                            local_parent_node,
                            remote_parent_node
                        );

                        // For position changes in the same folder, we only need to
                        // merge and flag the parent for reupload...
                        let mut merged_child_node =
                            self.two_way_merge(local_child_node, remote_child_node)?;
                        if merged_child_node.local_guid_changed() {
                            merged_child_node.merge_state =
                                merged_child_node.merge_state.with_new_local_structure();
                        }
                        merged_node.merge_state =
                            merged_node.merge_state.with_new_remote_structure();
                        if merged_node.remote_guid_changed() {
                            // ...Unless the merged parent's GUID also changed,
                            // in which case we also need to flag the
                            // repositioned child for reupload, so that its
                            // `parentid` is correct.
                            merged_child_node.merge_state =
                                merged_child_node.merge_state.with_new_remote_structure();
                        }

                        merged_node.merged_children.push(merged_child_node);
                        self.structure_counts.merged_nodes += 1;
                    }
                }

                ConflictResolution::Remote | ConflictResolution::Unchanged => {
                    // The remote move is newer, so we ignore the local
                    // move. We'll merge the local child later, when we
                    // walk its new remote parent.
                    if local_parent_node.guid != remote_parent_node.guid {
                        trace!(
                            self.driver,
                            "Local child {} reparented locally to {} and remotely to {}; \
                             keeping child in newer remote parent",
                            local_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                    } else {
                        trace!(
                            self.driver,
                            "Local child {} repositioned locally in {} and remotely in {}; \
                             keeping child in newer remote position",
                            local_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                    }
                    merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
                }
            }

            return Ok(());
        }

        // Local child is not a root, and doesn't exist remotely. Try to find a
        // content match in the containing folder, and dedupe the local item if
        // we can.
        trace!(
            self.driver,
            "Local child {} doesn't exist remotely; looking for remote content match",
            local_child_node
        );

        let merged_child_node = if let Some(remote_child_node_by_content) = self
            .find_remote_node_matching_local_node(
                merged_node,
                local_parent_node,
                remote_parent_node,
                local_child_node,
            )? {
            // The local child has a remote content match, so take the remote GUID
            // and merge.
            let mut merged_child_node =
                self.two_way_merge(local_child_node, remote_child_node_by_content)?;
            if merged_child_node.local_guid_changed() {
                merged_child_node.merge_state =
                    merged_child_node.merge_state.with_new_local_structure();
            }
            if merged_node.remote_guid_changed() {
                merged_child_node.merge_state =
                    merged_child_node.merge_state.with_new_remote_structure();
            }
            if merged_child_node.remote_guid_changed() {
                merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
            }
            merged_node.merge_state = merged_node.merge_state.with_new_local_structure();
            merged_child_node
        } else {
            // The local child doesn't exist remotely, so flag the merged parent and
            // new child for upload, and walk its descendants.
            let mut merged_child_node = self.merge_local_only_node(local_child_node)?;
            if merged_child_node.local_guid_changed() {
                merged_child_node.merge_state =
                    merged_child_node.merge_state.with_new_local_structure();
            }
            merged_node.merge_state = merged_node.merge_state.with_new_remote_structure();
            merged_child_node.merge_state =
                merged_child_node.merge_state.with_new_remote_structure();
            merged_child_node
        };
        merged_node.merged_children.push(merged_child_node);
        self.structure_counts.merged_nodes += 1;
        Ok(())
    }

    /// Determines which side to prefer, and which children to merge first,
    /// for an item that exists on both sides.
    fn resolve_value_conflict(
        &self,
        local_node: Node<'t>,
        remote_node: Node<'t>,
    ) -> (ConflictResolution, ConflictResolution) {
        if remote_node.is_root() {
            // Don't touch the Places root; it's not synced, anyway.
            return (ConflictResolution::Unchanged, ConflictResolution::Local);
        }

        match (local_node.needs_merge, remote_node.needs_merge) {
            (true, true) => {
                // The item changed locally and remotely.
                let item = if local_node.is_built_in_root() {
                    // For roots, we always prefer the local side for item
                    // changes, like the title (bug 1432614).
                    ConflictResolution::Local
                } else {
                    // For other items, we check the validity to decide
                    // which side to take.
                    match (local_node.validity, remote_node.validity) {
                        // If both are invalid, it doesn't matter which side
                        // we pick; the item will be deleted, anyway.
                        (Validity::Replace, Validity::Replace) => ConflictResolution::Unchanged,
                        // If only one side is invalid, pick the other side.
                        // This loses changes from that side, but we can't
                        // apply or upload those changes, anyway.
                        (Validity::Replace, _) => ConflictResolution::Remote,
                        (_, Validity::Replace) => ConflictResolution::Local,
                        (_, _) => {
                            // Otherwise, the item is either valid, or valid
                            // but needs to be reuploaded or reapplied, so
                            // compare timestamps to decide which side is newer.
                            if local_node.age < remote_node.age {
                                ConflictResolution::Local
                            } else {
                                ConflictResolution::Remote
                            }
                        }
                    }
                };
                // For children, it's easier: we always use the newer side, even
                // if we're taking local changes for the item.
                let children = if local_node.has_matching_children(remote_node) {
                    ConflictResolution::Unchanged
                } else if local_node.age < remote_node.age {
                    // The local change is newer, so merge local children first,
                    // followed by remaining unmerged remote children.
                    ConflictResolution::Local
                } else {
                    // The remote change is newer, so walk and merge remote
                    // children first, then remaining local children.
                    ConflictResolution::Remote
                };
                (item, children)
            }

            (true, false) => {
                // The item changed locally, but not remotely. Prefer the local
                // item, then merge local children first, followed by remote
                // children.
                let item = match local_node.validity {
                    Validity::Valid | Validity::Reupload => ConflictResolution::Local,
                    Validity::Replace => ConflictResolution::Remote,
                };
                let children = if local_node.has_matching_children(remote_node) {
                    ConflictResolution::Unchanged
                } else {
                    ConflictResolution::Local
                };
                (item, children)
            }

            (false, true) => {
                // The item changed remotely, but not locally.
                let item = if local_node.is_built_in_root() {
                    // For roots, we ignore remote item changes.
                    ConflictResolution::Unchanged
                } else {
                    match remote_node.validity {
                        Validity::Valid | Validity::Reupload => ConflictResolution::Remote,
                        // And, for invalid remote items, we must reupload the
                        // local side. This _loses remote changes_, but we can't
                        // apply those changes, anyway.
                        Validity::Replace => ConflictResolution::Local,
                    }
                };
                let children = if local_node.has_matching_children(remote_node) {
                    ConflictResolution::Unchanged
                } else {
                    ConflictResolution::Remote
                };
                // For children, we always use the remote side.
                (item, children)
            }

            (false, false) => {
                let item = match (local_node.validity, remote_node.validity) {
                    (Validity::Replace, Validity::Replace) => ConflictResolution::Unchanged,
                    (_, Validity::Replace) => ConflictResolution::Local,
                    (Validity::Replace, _) => ConflictResolution::Remote,
                    (_, _) => ConflictResolution::Unchanged,
                };
                // If the child lists are identical, the structure is unchanged.
                // Otherwise, the children differ even though the items aren't
                // flagged as unmerged, so we prefer the newer side.
                let children = if local_node.has_matching_children(remote_node) {
                    ConflictResolution::Unchanged
                } else if local_node.age < remote_node.age {
                    ConflictResolution::Local
                } else {
                    ConflictResolution::Remote
                };
                (item, children)
            }
        }
    }

    /// Determines where to keep a child of a folder that exists on both sides.
    fn resolve_structure_conflict(
        &self,
        local_parent_node: Node<'t>,
        local_child_node: Node<'t>,
        remote_parent_node: Node<'t>,
        remote_child_node: Node<'t>,
    ) -> ConflictResolution {
        if remote_child_node.is_built_in_root() {
            // Always use the local parent and position for roots.
            return ConflictResolution::Local;
        }

        match (
            local_parent_node.needs_merge,
            remote_parent_node.needs_merge,
        ) {
            (true, true) => {
                // If both parents changed, compare timestamps to decide where
                // to keep the local child.
                let latest_local_age = local_child_node.age.min(local_parent_node.age);
                let latest_remote_age = remote_child_node.age.min(remote_parent_node.age);

                if latest_local_age < latest_remote_age {
                    ConflictResolution::Local
                } else {
                    ConflictResolution::Remote
                }
            }

            // If only the local or remote parent changed, keep the child in its
            // new parent.
            (true, false) => ConflictResolution::Local,
            (false, true) => ConflictResolution::Remote,

            (false, false) => ConflictResolution::Unchanged,
        }
    }

    /// Checks if a remote node is locally moved or deleted, and reparents any
    /// descendants that aren't also remotely deleted to the merged node.
    ///
    /// This is the inverse of
    /// `check_for_remote_structure_change_of_local_node`.
    fn check_for_local_structure_change_of_remote_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        remote_parent_node: Node<'t>,
        remote_node: Node<'t>,
    ) -> Result<StructureChange> {
        if !remote_node.is_syncable() {
            // If the remote node is known to be non-syncable, we unconditionally
            // delete it, even if it's syncable or moved locally.
            trace!(
                self.driver,
                "Deleting non-syncable remote node {}",
                remote_node
            );
            return self.delete_remote_node(merged_node, remote_node);
        }

        if !self.local_tree.is_deleted(&remote_node.guid) {
            if let Some(local_node) = self.local_tree.node_for_guid(&remote_node.guid) {
                if !local_node.is_syncable() {
                    // The remote node is syncable, but the local node is
                    // non-syncable. Unconditionally delete it.
                    trace!(
                        self.driver,
                        "Remote node {} is syncable, but local node {} isn't; deleting",
                        remote_node,
                        local_node
                    );
                    return self.delete_remote_node(merged_node, remote_node);
                }
                if local_node.validity == Validity::Replace
                    && remote_node.validity == Validity::Replace
                {
                    // The nodes are invalid on both sides, so we can't apply
                    // or reupload a valid copy. Delete it.
                    return self.delete_remote_node(merged_node, remote_node);
                }
                let local_parent_node = local_node
                    .parent()
                    .expect("Can't check for structure changes without local parent");
                if local_parent_node.guid != remote_parent_node.guid {
                    return Ok(StructureChange::Moved);
                }
                return Ok(StructureChange::Unchanged);
            }
            if remote_node.validity == Validity::Replace {
                // The remote node is invalid and doesn't exist locally, so we
                // can't reupload a valid copy. We must delete it.
                return self.delete_remote_node(merged_node, remote_node);
            }
            return Ok(StructureChange::Unchanged);
        }

        if remote_node.validity == Validity::Replace {
            // The remote node is invalid and deleted locally, so we can't
            // reupload a valid copy. Delete it.
            return self.delete_remote_node(merged_node, remote_node);
        }

        if remote_node.is_built_in_root() {
            // If the remote node is a content root, don't delete it locally.
            return Ok(StructureChange::Unchanged);
        }

        if remote_node.needs_merge {
            if !remote_node.is_folder() {
                // If a non-folder child is deleted locally and changed remotely, we
                // ignore the local deletion and take the remote child.
                trace!(
                    self.driver,
                    "Remote non-folder {} deleted locally and changed remotely; \
                     taking remote change",
                    remote_node
                );
                self.structure_counts.remote_revives += 1;
                return Ok(StructureChange::Unchanged);
            }
            // For folders, we always take the local deletion and relocate remotely
            // changed grandchildren to the merged node. We could use the remote
            // tree to revive the child folder, but it's easier to relocate orphaned
            // grandchildren than to partially revive the child folder.
            trace!(
                self.driver,
                "Remote folder {} deleted locally and changed remotely; \
                 taking local deletion",
                remote_node
            );
            self.structure_counts.local_deletes += 1;
        } else {
            trace!(
                self.driver,
                "Remote node {} deleted locally and not changed remotely; \
                 taking local deletion",
                remote_node
            );
        }

        // Take the local deletion and relocate any new remote descendants to the
        // merged node.
        self.delete_remote_node(merged_node, remote_node)
    }

    /// Checks if a local node is remotely moved or deleted, and reparents any
    /// descendants that aren't also locally deleted to the merged node.
    ///
    /// This is the inverse of
    /// `check_for_local_structure_change_of_remote_node`.
    fn check_for_remote_structure_change_of_local_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        local_parent_node: Node<'t>,
        local_node: Node<'t>,
    ) -> Result<StructureChange> {
        if !local_node.is_syncable() {
            // If the local node is known to be non-syncable, we unconditionally
            // delete it, even if it's syncable or moved remotely.
            trace!(
                self.driver,
                "Deleting non-syncable local node {}",
                local_node
            );
            return self.delete_local_node(merged_node, local_node);
        }

        if !self.remote_tree.is_deleted(&local_node.guid) {
            if let Some(remote_node) = self.remote_tree.node_for_guid(&local_node.guid) {
                if !remote_node.is_syncable() {
                    // The local node is syncable, but the remote node is not.
                    // This can happen if we applied an orphaned left pane
                    // query in a previous sync, and later saw the left pane
                    // root on the server. Since we now have the complete
                    // subtree, we can remove it.
                    trace!(
                        self.driver,
                        "Local node {} is syncable, but remote node {} isn't; deleting",
                        local_node,
                        remote_node
                    );
                    return self.delete_local_node(merged_node, local_node);
                }
                if remote_node.validity == Validity::Replace
                    && local_node.validity == Validity::Replace
                {
                    // The nodes are invalid on both sides, so we can't replace
                    // the local copy with a remote one. Delete it.
                    return self.delete_local_node(merged_node, local_node);
                }
                // Otherwise, either both nodes are valid; or the remote node
                // is invalid but the local node is valid, so we can reupload a
                // valid copy.
                let remote_parent_node = remote_node
                    .parent()
                    .expect("Can't check for structure changes without remote parent");
                if remote_parent_node.guid != local_parent_node.guid {
                    return Ok(StructureChange::Moved);
                }
                return Ok(StructureChange::Unchanged);
            }
            if local_node.validity == Validity::Replace {
                // The local node is invalid and doesn't exist remotely, so
                // we can't replace the local copy. Delete it.
                return self.delete_local_node(merged_node, local_node);
            }
            return Ok(StructureChange::Unchanged);
        }

        if local_node.validity == Validity::Replace {
            // The local node is invalid and deleted remotely, so we can't
            // replace the local copy. Delete it.
            return self.delete_local_node(merged_node, local_node);
        }

        if local_node.is_built_in_root() {
            // If the local node is a content root, don't delete it remotely.
            return Ok(StructureChange::Unchanged);
        }

        // See `check_for_local_structure_change_of_remote_node` for an
        // explanation of how we decide to take or ignore a deletion.
        if local_node.needs_merge {
            if !local_node.is_folder() {
                trace!(
                    self.driver,
                    "Local non-folder {} deleted remotely and changed locally; taking local change",
                    local_node
                );
                self.structure_counts.local_revives += 1;
                return Ok(StructureChange::Unchanged);
            }
            trace!(
                self.driver,
                "Local folder {} deleted remotely and changed locally; taking remote deletion",
                local_node
            );
            self.structure_counts.remote_deletes += 1;
        } else {
            trace!(
                self.driver,
                "Local node {} deleted remotely and not changed locally; taking remote deletion",
                local_node
            );
        }

        // Take the remote deletion and relocate any new local descendants to the
        // merged node.
        self.delete_local_node(merged_node, local_node)
    }

    /// Marks a remote node as deleted, and relocates all remote descendants
    /// that aren't also locally deleted to the merged node. This avoids data
    /// loss if the user adds a bookmark to a folder on another device, and
    /// deletes that folder locally.
    ///
    /// This is the inverse of `delete_local_node`.
    fn delete_remote_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        remote_node: Node<'t>,
    ) -> Result<StructureChange> {
        self.delete_remotely.insert(remote_node.guid.clone());
        for remote_child_node in remote_node.children() {
            self.signal.err_if_aborted()?;
            if self.merged_guids.contains(&remote_child_node.guid) {
                trace!(
                    self.driver,
                    "Remote child {} can't be an orphan; already merged",
                    remote_child_node
                );
                continue;
            }
            match self.check_for_local_structure_change_of_remote_node(
                merged_node,
                remote_node,
                remote_child_node,
            )? {
                StructureChange::Moved | StructureChange::Deleted => {
                    // The remote child is already moved or deleted locally, so we should
                    // ignore it instead of treating it as a remote orphan.
                    continue;
                }
                StructureChange::Unchanged => {
                    trace!(
                        self.driver,
                        "Relocating remote orphan {} to {}",
                        remote_child_node,
                        merged_node
                    );

                    // Flag the new parent and moved remote orphan for reupload.
                    let mut merged_orphan_node = if let Some(local_child_node) =
                        self.local_tree.node_for_guid(&remote_child_node.guid)
                    {
                        self.two_way_merge(local_child_node, remote_child_node)
                    } else {
                        self.merge_remote_only_node(remote_child_node)
                    }?;
                    merged_node.merge_state = merged_node
                        .merge_state
                        .with_new_local_structure()
                        .with_new_remote_structure();
                    merged_orphan_node.merge_state = merged_orphan_node
                        .merge_state
                        .with_new_local_structure()
                        .with_new_remote_structure();
                    merged_node.merged_children.push(merged_orphan_node);
                    self.structure_counts.merged_nodes += 1;
                }
            }
        }
        self.structure_counts.merged_deletions += 1;
        Ok(StructureChange::Deleted)
    }

    /// Marks a local node as deleted, and relocates all local descendants
    /// that aren't also remotely deleted to the merged node.
    ///
    /// This is the inverse of `delete_remote_node`.
    fn delete_local_node(
        &mut self,
        merged_node: &mut MergedNode<'t>,
        local_node: Node<'t>,
    ) -> Result<StructureChange> {
        self.delete_locally.insert(local_node.guid.clone());
        for local_child_node in local_node.children() {
            self.signal.err_if_aborted()?;
            if self.merged_guids.contains(&local_child_node.guid) {
                trace!(
                    self.driver,
                    "Local child {} can't be an orphan; already merged",
                    local_child_node
                );
                continue;
            }
            match self.check_for_remote_structure_change_of_local_node(
                merged_node,
                local_node,
                local_child_node,
            )? {
                StructureChange::Moved | StructureChange::Deleted => {
                    // The local child is already moved or deleted remotely, so we should
                    // ignore it instead of treating it as a local orphan.
                    continue;
                }
                StructureChange::Unchanged => {
                    trace!(
                        self.driver,
                        "Relocating local orphan {} to {}",
                        local_child_node,
                        merged_node
                    );

                    // Flag the new parent and moved local orphan for reupload.
                    let mut merged_orphan_node = if let Some(remote_child_node) =
                        self.remote_tree.node_for_guid(&local_child_node.guid)
                    {
                        self.two_way_merge(local_child_node, remote_child_node)
                    } else {
                        self.merge_local_only_node(local_child_node)
                    }?;
                    merged_node.merge_state = merged_node
                        .merge_state
                        .with_new_local_structure()
                        .with_new_remote_structure();
                    merged_orphan_node.merge_state = merged_orphan_node
                        .merge_state
                        .with_new_local_structure()
                        .with_new_remote_structure();
                    merged_node.merged_children.push(merged_orphan_node);
                    self.structure_counts.merged_nodes += 1;
                }
            }
        }
        self.structure_counts.merged_deletions += 1;
        Ok(StructureChange::Deleted)
    }

    /// Finds all children of a local folder with similar content as children of
    /// the corresponding remote folder. This is used to dedupe local items that
    /// haven't been uploaded yet, to remote items that don't exist locally.
    ///
    /// Recall that we match items by GUID as we walk down the tree. If a GUID
    /// on one side doesn't exist on the other, we fall back to a content
    /// match in the same folder.
    ///
    /// This method is called the first time that
    /// `find_remote_node_matching_local_node` merges a local child that
    /// doesn't exist remotely, and
    /// the first time that `find_local_node_matching_remote_node` merges a
    /// remote child that doesn't exist locally.
    ///
    /// Finding all possible dupes is O(m + n) in the worst case, where `m` is
    /// the number of local children, and `n` is the number of remote
    /// children. We cache matches in
    /// `matching_dupes_by_local_parent_guid`, so deduping all
    /// remaining children of the same folder, on both sides, only needs two
    /// O(1) map lookups per child.
    fn find_all_matching_dupes_in_folders(
        &self,
        local_parent_node: Node<'t>,
        remote_parent_node: Node<'t>,
    ) -> Result<MatchingDupes<'t>> {
        let mut dupe_key_to_local_nodes: HashMap<DupeKey<'_>, VecDeque<_>> = HashMap::new();

        for (local_position, local_child_node) in local_parent_node.children().enumerate() {
            self.signal.err_if_aborted()?;
            if local_child_node.is_built_in_root() {
                continue;
            }
            if let Some(local_child_content) = local_child_node.content() {
                if let Some(remote_child_node) =
                    self.remote_tree.node_for_guid(&local_child_node.guid)
                {
                    trace!(
                        self.driver,
                        "Not deduping local child {}; already exists remotely as {}",
                        local_child_node,
                        remote_child_node
                    );
                    continue;
                }
                if self.remote_tree.is_deleted(&local_child_node.guid) {
                    trace!(
                        self.driver,
                        "Not deduping local child {}; deleted remotely",
                        local_child_node
                    );
                    continue;
                }
                // Store matching local children in an array, in case multiple children
                // have the same dupe key (for example, a toolbar containing multiple
                // empty folders, as in bug 1213369).
                let dupe_key = match local_child_content {
                    Content::Bookmark { .. } | Content::Folder { .. } => {
                        DupeKey::WithoutPosition(local_child_content)
                    }
                    Content::Separator => {
                        DupeKey::WithPosition(local_child_content, local_position)
                    }
                };
                let local_nodes_for_key = dupe_key_to_local_nodes.entry(dupe_key).or_default();
                local_nodes_for_key.push_back(local_child_node);
            } else {
                trace!(
                    self.driver,
                    "Not deduping local child {}; already uploaded",
                    local_child_node
                );
            }
        }

        let mut local_to_remote = HashMap::new();
        let mut remote_to_local = HashMap::new();

        for (remote_position, remote_child_node) in remote_parent_node.children().enumerate() {
            self.signal.err_if_aborted()?;
            if remote_to_local.contains_key(&remote_child_node.guid) {
                trace!(
                    self.driver,
                    "Not deduping remote child {}; already deduped",
                    remote_child_node
                );
                continue;
            }
            // Note that we don't need to check if the remote node is deleted
            // locally, because it wouldn't have local content entries if it
            // were.
            if let Some(remote_child_content) = remote_child_node.content() {
                let dupe_key = match remote_child_content {
                    Content::Bookmark { .. } | Content::Folder { .. } => {
                        DupeKey::WithoutPosition(remote_child_content)
                    }
                    Content::Separator => {
                        DupeKey::WithPosition(remote_child_content, remote_position)
                    }
                };
                if let Some(local_nodes_for_key) = dupe_key_to_local_nodes.get_mut(&dupe_key) {
                    if let Some(local_child_node) = local_nodes_for_key.pop_front() {
                        trace!(
                            self.driver,
                            "Deduping local child {} to remote child {}",
                            local_child_node,
                            remote_child_node
                        );
                        local_to_remote.insert(local_child_node.guid.clone(), remote_child_node);
                        remote_to_local.insert(remote_child_node.guid.clone(), local_child_node);
                    } else {
                        trace!(
                            self.driver,
                            "Not deduping remote child {}; no remaining local content matches",
                            remote_child_node
                        );
                        continue;
                    }
                } else {
                    trace!(
                        self.driver,
                        "Not deduping remote child {}; no local content matches",
                        remote_child_node
                    );
                    continue;
                }
            } else {
                trace!(
                    self.driver,
                    "Not deduping remote child {}; already merged",
                    remote_child_node
                );
            }
        }

        Ok((local_to_remote, remote_to_local))
    }

    /// Finds a remote node with a different GUID that matches the content of a
    /// local node.
    ///
    /// This is the inverse of `find_local_node_matching_remote_node`.
    fn find_remote_node_matching_local_node(
        &mut self,
        merged_node: &MergedNode<'t>,
        local_parent_node: Node<'t>,
        remote_parent_node: Option<Node<'t>>,
        local_child_node: Node<'t>,
    ) -> Result<Option<Node<'t>>> {
        if let Some(remote_parent_node) = remote_parent_node {
            let mut matching_dupes_by_local_parent_guid = mem::replace(
                &mut self.matching_dupes_by_local_parent_guid,
                HashMap::new(),
            );
            let new_remote_node = {
                let (local_to_remote, _) = match matching_dupes_by_local_parent_guid
                    .entry(local_parent_node.guid.clone())
                {
                    Entry::Occupied(entry) => entry.into_mut(),
                    Entry::Vacant(entry) => {
                        trace!(
                            self.driver,
                            "First local child {} doesn't exist remotely; \
                             finding all matching dupes in local {} and remote {}",
                            local_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                        let matching_dupes = self.find_all_matching_dupes_in_folders(
                            local_parent_node,
                            remote_parent_node,
                        )?;
                        entry.insert(matching_dupes)
                    }
                };
                let new_remote_node = local_to_remote.get(&local_child_node.guid);
                new_remote_node.map(|node| {
                    self.structure_counts.dupes += 1;
                    *node
                })
            };
            mem::replace(
                &mut self.matching_dupes_by_local_parent_guid,
                matching_dupes_by_local_parent_guid,
            );
            Ok(new_remote_node)
        } else {
            trace!(
                self.driver,
                "Merged node {} doesn't exist remotely; no potential dupes for local child {}",
                merged_node,
                local_child_node
            );
            Ok(None)
        }
    }

    /// Finds a local node with a different GUID that matches the content of a
    /// remote node.
    ///
    /// This is the inverse of `find_remote_node_matching_local_node`.
    fn find_local_node_matching_remote_node(
        &mut self,
        merged_node: &MergedNode<'t>,
        local_parent_node: Option<Node<'t>>,
        remote_parent_node: Node<'t>,
        remote_child_node: Node<'t>,
    ) -> Result<Option<Node<'t>>> {
        if let Some(local_parent_node) = local_parent_node {
            let mut matching_dupes_by_local_parent_guid = mem::replace(
                &mut self.matching_dupes_by_local_parent_guid,
                HashMap::new(),
            );
            let new_local_node = {
                let (_, remote_to_local) = match matching_dupes_by_local_parent_guid
                    .entry(local_parent_node.guid.clone())
                {
                    Entry::Occupied(entry) => entry.into_mut(),
                    Entry::Vacant(entry) => {
                        trace!(
                            self.driver,
                            "First remote child {} doesn't exist locally; \
                             finding all matching dupes in local {} and remote {}",
                            remote_child_node,
                            local_parent_node,
                            remote_parent_node
                        );
                        let matching_dupes = self.find_all_matching_dupes_in_folders(
                            local_parent_node,
                            remote_parent_node,
                        )?;
                        entry.insert(matching_dupes)
                    }
                };
                let new_local_node = remote_to_local.get(&remote_child_node.guid);
                new_local_node.map(|node| {
                    self.structure_counts.dupes += 1;
                    *node
                })
            };
            mem::replace(
                &mut self.matching_dupes_by_local_parent_guid,
                matching_dupes_by_local_parent_guid,
            );
            Ok(new_local_node)
        } else {
            trace!(
                self.driver,
                "Merged node {} doesn't exist locally; no potential dupes for remote child {}",
                merged_node,
                remote_child_node
            );
            Ok(None)
        }
    }
}

/// The root of a merged tree, from which all merged nodes descend.
#[derive(Debug)]
pub struct MergedRoot<'t> {
    local_tree: &'t Tree,
    remote_tree: &'t Tree,
    node: MergedNode<'t>,
    merged_guids: HashSet<Guid>,
    delete_locally: HashSet<Guid>,
    delete_remotely: HashSet<Guid>,
    structure_counts: StructureCounts,
}

impl<'t> MergedRoot<'t> {
    /// Returns the root node.
    #[inline]
    pub fn node(&self) -> &MergedNode<'_> {
        &self.node
    }

    /// Returns a sequence of completion operations, or "completion ops", to
    /// apply to the local tree so that it matches the merged tree.
    pub fn completion_ops(&self) -> CompletionOps<'_> {
        let mut ops = CompletionOps::default();
        accumulate(&mut ops, self.node(), 1, false);
        for guid in self.delete_locally.iter() {
            if self.local_tree.mentions(guid) && self.remote_tree.mentions(guid) {
                // Only flag tombstones for items that exist in both trees,
                // excluding ones for items that don't exist in the local
                // tree. This can be removed once we persist tombstones in
                // bug 1343103.
                let flag_as_merged = FlagAsMerged(guid);
                ops.flag_as_merged.push(flag_as_merged);
            }
        }
        ops
    }

    /// Returns an iterator for all accepted local and remote deletions.
    #[inline]
    pub fn deletions(&self) -> impl Iterator<Item = Deletion<'_>> {
        self.local_deletions().chain(self.remote_deletions())
    }

    /// Returns an iterator for all items that should be deleted from the
    /// local tree.
    pub fn local_deletions(&self) -> impl Iterator<Item = Deletion<'_>> {
        self.delete_locally.iter().filter_map(move |guid| {
            if self.delete_remotely.contains(guid) {
                None
            } else {
                let local_level = self
                    .local_tree
                    .node_for_guid(guid)
                    .map_or(-1, |node| node.level());
                // Items that should be deleted locally already have tombstones
                // on the server, so we don't need to upload tombstones for
                // these deletions.
                Some(Deletion {
                    guid,
                    local_level,
                    should_upload_tombstone: false,
                })
            }
        })
    }

    /// Returns an iterator for all items that should be deleted from the
    /// remote tree.
    pub fn remote_deletions(&self) -> impl Iterator<Item = Deletion<'_>> {
        self.delete_remotely.iter().map(move |guid| {
            let local_level = self
                .local_tree
                .node_for_guid(guid)
                .map_or(-1, |node| node.level());
            Deletion {
                guid,
                local_level,
                should_upload_tombstone: true,
            }
        })
    }

    /// Returns structure change counts for this merged root.
    #[inline]
    pub fn counts(&self) -> &StructureCounts {
        &self.structure_counts
    }
}

/// Completion operations to apply to the local tree after a merge. These are
/// represented as separate structs in `Vec`s instead of enums yielded from an
/// iterator so that consumers can easily chunk them.
#[derive(Clone, Debug, Default)]
pub struct CompletionOps<'t> {
    pub change_guids: Vec<ChangeGuid<'t>>,
    pub apply_remote_items: Vec<ApplyRemoteItem<'t>>,
    pub apply_new_local_structure: Vec<ApplyNewLocalStructure<'t>>,
    pub flag_for_upload: Vec<FlagForUpload<'t>>,
    pub skip_upload: Vec<SkipUpload<'t>>,
    pub flag_as_merged: Vec<FlagAsMerged<'t>>,
    pub upload: Vec<Upload<'t>>,
}

impl<'t> CompletionOps<'t> {
    /// Returns `true` if there are no completion ops to apply.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.change_guids.is_empty()
            && self.apply_remote_items.is_empty()
            && self.apply_new_local_structure.is_empty()
            && self.flag_for_upload.is_empty()
            && self.skip_upload.is_empty()
            && self.flag_as_merged.is_empty()
            && self.upload.is_empty()
    }
}

/// A completion op to change the local GUID to the merged GUID. This is used
/// to dedupe new local items to remote ones, as well as to fix up invalid
/// GUIDs.
#[derive(Clone, Copy, Debug)]
pub struct ChangeGuid<'t> {
    /// The merged node to update.
    pub merged_node: &'t MergedNode<'t>,
    /// The level of the node in the merged tree. Desktop uses this to ensure
    /// that GUID change observers are notified in level order (parents before
    /// children).
    pub level: usize,
}

impl<'t> ChangeGuid<'t> {
    /// Returns the local node for this completion op. Panics if the local node
    /// isn't set, as we should never emit a `ChangeGuid` op in that case.
    #[inline]
    pub fn local_node(&self) -> &'t Node<'t> {
        self.merged_node
            .merge_state
            .local_node()
            .expect("Can't change local GUID without local node")
    }
}

impl<'t> fmt::Display for ChangeGuid<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Change {} to {}",
            self.local_node().guid,
            self.merged_node.guid
        )
    }
}

/// A completion op to insert a new remote item into the local tree, or apply
/// synced changes to an existing item.
#[derive(Clone, Copy, Debug)]
pub struct ApplyRemoteItem<'t> {
    pub merged_node: &'t MergedNode<'t>,
    pub level: usize,
}

impl<'t> ApplyRemoteItem<'t> {
    /// Returns the remote node for this completion op. Panics if the remote
    /// node isn't set, as we should never emit an `ApplyRemoteItem` op in
    /// that case.
    #[inline]
    pub fn remote_node(&self) -> &'t Node<'t> {
        self.merged_node
            .merge_state
            .remote_node()
            .expect("Can't apply remote item without remote node")
    }
}

impl<'t> fmt::Display for ApplyRemoteItem<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.merged_node.remote_guid_changed() {
            write!(
                f,
                "Apply remote {} as {}",
                self.remote_node().guid,
                self.merged_node.guid
            )
        } else {
            write!(f, "Apply remote {}", self.merged_node.guid)
        }
    }
}

/// A completion op to update the parent and position of a local item.
#[derive(Clone, Copy, Debug)]
pub struct ApplyNewLocalStructure<'t> {
    pub merged_node: &'t MergedNode<'t>,
    pub merged_parent_node: &'t MergedNode<'t>,
    pub position: usize,
    pub level: usize,
}

impl<'t> fmt::Display for ApplyNewLocalStructure<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Move {} into {} at {}",
            self.merged_node.guid, self.merged_parent_node.guid, self.position
        )
    }
}

/// A completion op to flag a local item for upload.
#[derive(Clone, Copy, Debug)]
pub struct FlagForUpload<'t> {
    pub merged_node: &'t MergedNode<'t>,
}

impl<'t> fmt::Display for FlagForUpload<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Flag {} for upload", self.merged_node.guid)
    }
}

/// A completion op to skip uploading a local item after resolving merge
/// conflicts.
#[derive(Clone, Copy, Debug)]
pub struct SkipUpload<'t> {
    pub merged_node: &'t MergedNode<'t>,
}

impl<'t> fmt::Display for SkipUpload<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Don't upload {}", self.merged_node.guid)
    }
}

/// A completion op to upload or reupload a merged item.
#[derive(Clone, Copy, Debug)]
pub struct Upload<'t> {
    pub merged_node: &'t MergedNode<'t>,
}

impl<'t> fmt::Display for Upload<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Upload {}", self.merged_node.guid)
    }
}

/// A completion op to flag a remote item as merged.
#[derive(Clone, Copy, Debug)]
pub struct FlagAsMerged<'t>(&'t Guid);

impl<'t> FlagAsMerged<'t> {
    /// Returns the remote GUID for the item to flag as merged.
    #[inline]
    pub fn guid(self) -> &'t Guid {
        self.0
    }
}

impl<'t> fmt::Display for FlagAsMerged<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Flag {} as merged", self.guid())
    }
}

/// Recursively accumulates completion ops, starting at `merged_node` and
/// drilling down into all its descendants.
fn accumulate<'t>(
    ops: &mut CompletionOps<'t>,
    merged_node: &'t MergedNode<'t>,
    level: usize,
    is_tagging: bool,
) {
    for (position, merged_child_node) in merged_node.merged_children.iter().enumerate() {
        let is_tagging = if merged_child_node.guid == TAGS_GUID {
            true
        } else {
            is_tagging
        };
        if merged_child_node.merge_state.should_apply_item() {
            let apply_remote_item = ApplyRemoteItem {
                merged_node: merged_child_node,
                level,
            };
            ops.apply_remote_items.push(apply_remote_item);
        }
        if merged_child_node.local_guid_changed() {
            let change_guid = ChangeGuid {
                merged_node: merged_child_node,
                level,
            };
            ops.change_guids.push(change_guid);
        }
        let local_child_node = merged_node
            .merge_state
            .local_node()
            .and_then(|local_parent_node| local_parent_node.child(position));
        let merged_local_child_node = merged_child_node.merge_state.local_node();
        if local_child_node
            .and_then(|m| merged_local_child_node.map(|n| m.guid != n.guid))
            .unwrap_or(true)
        {
            // As an optimization, we only emit ops to apply a new local
            // structure for items that actually moved. For example, if the
            // local children are (A B C D) and the merged children are
            // (A D C B), only (B D) need new structure.
            let apply_new_local_structure = ApplyNewLocalStructure {
                merged_node: merged_child_node,
                merged_parent_node: merged_node,
                position,
                level,
            };
            ops.apply_new_local_structure
                .push(apply_new_local_structure);
        }
        let local_needs_merge = merged_child_node
            .merge_state
            .local_node()
            .map(|node| node.needs_merge)
            .unwrap_or(false);
        let should_upload = merged_child_node.merge_state.should_upload();
        match (local_needs_merge, should_upload) {
            (false, true) => {
                // Local item isn't flagged for upload, but should be.
                let flag_for_upload = FlagForUpload {
                    merged_node: merged_child_node,
                };
                ops.flag_for_upload.push(flag_for_upload);
            }
            (true, false) => {
                // Local item flagged for upload when it doesn't need to be.
                let skip_upload = SkipUpload {
                    merged_node: merged_child_node,
                };
                ops.skip_upload.push(skip_upload);
            }
            _ => {}
        }
        if should_upload && !is_tagging {
            // (Re)upload items. Ignore the tags root and its descendants:
            // they're part of the local tree on Desktop (and will be removed
            // in bug 424160), but aren't synced as part of the structure.
            ops.upload.push(Upload {
                merged_node: merged_child_node,
            });
        }
        if let Some(remote_child_node) = merged_child_node.merge_state.remote_node() {
            if remote_child_node.needs_merge && !should_upload {
                // If the remote item was merged, and doesn't need to be
                // reuploaded, flag it as merged in the remote tree. Note that
                // we _don't_ emit this for locally revived items, or items with
                // new remote structure.
                let flag_as_merged = FlagAsMerged(&remote_child_node.guid);
                ops.flag_as_merged.push(flag_as_merged);
            }
        }
        accumulate(ops, merged_child_node, level + 1, is_tagging);
    }
}