palim 0.1.2

Reversible JSON diff and patch for Rust, with array moves, text deltas, JSON Patch and Merge Patch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
use crate::{Error, Patch, PatchOperation, apply_json_patch, delta, json_equal, pointer};
use serde::Serialize;
use serde_json::{Value, json};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::io::{self, Write};

/// Options for producing RFC 6902 patches.
#[derive(Clone, Debug)]
pub struct JsonPatchOptions {
    /// Reuse existing values with move/copy when that produces a smaller patch.
    pub factorize: bool,
    /// Replace a changed parent when its serialized patch is smaller.
    pub rationalize: bool,
    /// Include tests for the values and containers affected by each operation.
    pub tests: bool,
}

impl Default for JsonPatchOptions {
    fn default() -> Self {
        Self {
            factorize: true,
            rationalize: true,
            tests: false,
        }
    }
}

/// Compare documents and return a standard JSON Patch.
pub fn diff_json_patch(
    left: &Value,
    right: &Value,
    options: &JsonPatchOptions,
) -> Result<Patch, Error> {
    crate::DiffPatcher::default().diff_json_patch(left, right, options)
}

pub(crate) fn optimize(
    left: &Value,
    right: &Value,
    mut patch: Patch,
    options: &JsonPatchOptions,
) -> Result<Patch, Error> {
    if options.factorize {
        patch = factorize(left, right, patch)?;
    }
    if options.rationalize {
        patch = rationalize(left, right, patch, options.tests)?;
    }
    if options.tests {
        patch = guard(left.clone(), &patch)?;
    }
    Ok(patch)
}

pub(crate) fn decode(value: Value) -> Result<PatchOperation, Error> {
    use json_patch::{
        AddOperation, CopyOperation, MoveOperation, RemoveOperation, ReplaceOperation,
        TestOperation, jsonptr::PointerBuf,
    };
    let Value::Object(mut fields) = value else {
        return Err(Error::new(
            "",
            "generated JSON Patch operation is not an object",
        ));
    };
    let Some(Value::String(operation)) = fields.remove("op") else {
        return Err(Error::new(
            "",
            "generated JSON Patch operation is missing op",
        ));
    };
    let take_pointer = |fields: &mut serde_json::Map<String, Value>, name: &str| {
        let Some(Value::String(value)) = fields.remove(name) else {
            return Err(Error::new(
                "",
                format!("generated JSON Patch operation is missing {name}"),
            ));
        };
        PointerBuf::parse(value).map_err(|error| Error::new("", error.to_string()))
    };
    let path = take_pointer(&mut fields, "path")?;
    let take_value = |fields: &mut serde_json::Map<String, Value>| {
        fields
            .remove("value")
            .ok_or_else(|| Error::new("", "generated JSON Patch operation is missing value"))
    };
    // Construct typed operations directly. Serde's internally tagged enum
    // buffer cannot consume Value's u128 representation for arbitrary-precision
    // integers; passing owned Value payloads avoids both that buffer and a JSON
    // serialization/parsing round trip.
    Ok(match operation.as_str() {
        "add" => PatchOperation::Add(AddOperation {
            path,
            value: take_value(&mut fields)?,
        }),
        "remove" => PatchOperation::Remove(RemoveOperation { path }),
        "replace" => PatchOperation::Replace(ReplaceOperation {
            path,
            value: take_value(&mut fields)?,
        }),
        "move" => PatchOperation::Move(MoveOperation {
            from: take_pointer(&mut fields, "from")?,
            path,
        }),
        "copy" => PatchOperation::Copy(CopyOperation {
            from: take_pointer(&mut fields, "from")?,
            path,
        }),
        "test" => PatchOperation::Test(TestOperation {
            path,
            value: take_value(&mut fields)?,
        }),
        _ => {
            return Err(Error::new(
                "",
                "unrecognized generated JSON Patch operation",
            ));
        }
    })
}

fn path(op: &PatchOperation) -> &str {
    match op {
        PatchOperation::Add(op) => op.path.as_str(),
        PatchOperation::Remove(op) => op.path.as_str(),
        PatchOperation::Replace(op) => op.path.as_str(),
        PatchOperation::Move(op) => op.path.as_str(),
        PatchOperation::Copy(op) => op.path.as_str(),
        PatchOperation::Test(op) => op.path.as_str(),
    }
}

fn from(op: &PatchOperation) -> Option<&str> {
    match op {
        PatchOperation::Move(op) => Some(op.from.as_str()),
        PatchOperation::Copy(op) => Some(op.from.as_str()),
        _ => None,
    }
}

fn parent(path: &str) -> Option<&str> {
    path.rsplit_once('/').map(|(parent, _)| parent)
}

fn contains(parent: &str, path: &str) -> bool {
    parent == path || (path.starts_with(parent) && path.as_bytes().get(parent.len()) == Some(&b'/'))
}

struct ByteCount(usize);

impl Write for ByteCount {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        self.0 = self
            .0
            .checked_add(bytes.len())
            .ok_or_else(|| io::Error::other("JSON size exceeds address space"))?;
        Ok(bytes.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn bytes<T: Serialize + ?Sized>(value: &T) -> Result<usize, Error> {
    let mut output = ByteCount(0);
    serde_json::to_writer(&mut output, value).map_err(|error| Error::new("", error.to_string()))?;
    Ok(output.0)
}

struct LimitedByteCount {
    count: usize,
    limit: usize,
    exceeded: bool,
}

impl Write for LimitedByteCount {
    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        let next = self
            .count
            .checked_add(data.len())
            .ok_or_else(|| io::Error::other("JSON size exceeds address space"))?;
        if next >= self.limit {
            self.exceeded = true;
            return Err(io::Error::other("candidate cannot fit budget"));
        }
        self.count = next;
        Ok(data.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn bytes_below<T: Serialize + ?Sized>(value: &T, limit: usize) -> Result<Option<usize>, Error> {
    let mut output = LimitedByteCount {
        count: 0,
        limit,
        exceeded: false,
    };
    match serde_json::to_writer(&mut output, value) {
        Ok(()) => Ok((output.count < limit).then_some(output.count)),
        Err(_) if output.exceeded => Ok(None),
        Err(error) => Err(Error::new("", error.to_string())),
    }
}

#[derive(Serialize)]
struct ValueOperation<'a> {
    op: &'a str,
    path: &'a str,
    value: &'a Value,
}

#[derive(Serialize)]
struct CopyOperation<'a> {
    op: &'a str,
    from: &'a str,
    path: &'a str,
}

fn factorize(left: &Value, right: &Value, mut patch: Patch) -> Result<Patch, Error> {
    // Pair removes with additions first. A candidate is accepted only if its
    // sequential indices still produce the target; intervening array edits can
    // otherwise make a seemingly valid remove/add pair unsafe to turn into move.
    while patch
        .0
        .iter()
        .any(|op| matches!(op, PatchOperation::Remove(_)))
        && patch
            .0
            .iter()
            .any(|op| matches!(op, PatchOperation::Add(_)))
    {
        let mut shadow = left.clone();
        let mut removed: HashMap<Value, Vec<(usize, String)>> = HashMap::new();
        for (index, op) in patch.0.iter().enumerate() {
            if let PatchOperation::Remove(op) = op {
                if let Some(value) = shadow.pointer(op.path.as_str()) {
                    removed
                        .entry(value.clone())
                        .or_default()
                        .push((index, op.path.to_string()));
                }
            }
            crate::apply_json_patch_step(&mut shadow, op, index)?;
        }
        let mut replacement = None;
        'additions: for (add_index, op) in patch.0.iter().enumerate() {
            let PatchOperation::Add(add) = op else {
                continue;
            };
            let Some(sources) = removed.get(&add.value) else {
                continue;
            };
            for (remove_index, source) in sources {
                if contains(source, add.path.as_str()) {
                    continue;
                }
                let moved = decode(json!({"op": "move", "from": source, "path": add.path}))?;
                // Replacing two operations with one also removes a comma.
                // Reject expensive pairs before cloning the entire patch.
                if bytes(&moved)? > bytes(&patch.0[*remove_index])? + bytes(op)? {
                    continue;
                }
                let candidate = Patch(
                    patch
                        .0
                        .iter()
                        .enumerate()
                        .filter_map(|(index, op)| {
                            if index == *remove_index {
                                None
                            } else if index == add_index {
                                Some(moved.clone())
                            } else {
                                Some(op.clone())
                            }
                        })
                        .collect(),
                );
                if apply_json_patch(left, &candidate).is_ok_and(|value| json_equal(&value, right)) {
                    replacement = Some(candidate);
                    break 'additions;
                }
            }
        }
        match replacement {
            Some(candidate) => patch = candidate,
            None => break,
        }
    }

    // A copy source must exist immediately before that operation. Do not use
    // values from the initial document after an earlier operation removed them.
    if !patch
        .0
        .iter()
        .any(|op| matches!(op, PatchOperation::Add(_) | PatchOperation::Replace(_)))
    {
        return Ok(patch);
    }
    // A single operation has no earlier edits to replay. Search the initial
    // document directly instead of building a whole-tree scalar cache.
    if patch.0.len() == 1 {
        let op = &patch.0[0];
        let (value, destination, replace) = match op {
            PatchOperation::Add(op) => (&op.value, op.path.as_str(), false),
            PatchOperation::Replace(op) => (&op.value, op.path.as_str(), true),
            _ => return Ok(patch),
        };
        let eligible = !replace
            || parent(destination).is_none_or(|p| !left.pointer(p).is_some_and(Value::is_array));
        if eligible {
            let original_size = bytes(op)?;
            let minimum_size = bytes(&CopyOperation {
                op: "copy",
                from: "",
                path: destination,
            })?;
            if minimum_size < original_size {
                let mut best = None;
                find_copy(
                    left,
                    "",
                    value,
                    destination,
                    original_size - minimum_size,
                    &mut best,
                )?;
                if let Some((source, _)) = best {
                    let copied =
                        decode(json!({"op": "copy", "from": source, "path": destination}))?;
                    if bytes(&copied)? < original_size {
                        patch.0[0] = copied;
                    }
                }
            }
        }
        return Ok(patch);
    }
    // Retain removed scalars too: absence from this overapproximation proves
    // that searching the current document cannot find a matching copy source.
    // Neither this cache nor the sequential shadow is needed until a copy can
    // actually be smaller. Advance the shadow only when a search needs it.
    let mut scalars = HashMap::new();
    let mut loaded_scalars = 0;
    let mut source_reduction = 0;
    let mut shadow = None;
    let mut applied = 0;
    for index in 0..patch.0.len() {
        let op = &patch.0[index];
        let value = match op {
            PatchOperation::Add(op) => Some(&op.value),
            PatchOperation::Replace(op) => Some(&op.value),
            _ => None,
        };
        let mut replacement = None;
        if let Some(value) = value {
            let destination = path(op);
            let original_size = bytes(op)?;
            let minimum_size = bytes(&CopyOperation {
                op: "copy",
                from: "",
                path: destination,
            })?;
            if minimum_size < original_size {
                let possible = if let Some(key) = scalar_key(value) {
                    let kind = scalar_kind(value);
                    if loaded_scalars & kind == 0 {
                        if loaded_scalars == 0 {
                            for previous in &patch.0 {
                                if let Some(source) = from(previous) {
                                    source_reduction += path_minimum(source)?
                                        .saturating_sub(path_minimum(path(previous))?);
                                }
                            }
                        }
                        collect_scalars(left, 0, &mut scalars, kind)?;
                        for previous in &patch.0[..index] {
                            let prefix = path_minimum(path(previous))?;
                            match previous {
                                PatchOperation::Add(op) => {
                                    collect_scalars(&op.value, prefix, &mut scalars, kind)?;
                                }
                                PatchOperation::Replace(op) => {
                                    collect_scalars(&op.value, prefix, &mut scalars, kind)?;
                                }
                                _ => {}
                            }
                        }
                        loaded_scalars |= kind;
                    }
                    scalars.get(&key).is_some_and(|cost: &usize| {
                        cost.saturating_sub(source_reduction) < original_size - minimum_size
                    })
                } else {
                    true
                };
                if possible {
                    if index > 0 {
                        let shadow = shadow.get_or_insert_with(|| left.clone());
                        for (offset, previous) in patch.0[applied..index].iter().enumerate() {
                            crate::apply_json_patch_step(shadow, previous, applied + offset)?;
                        }
                        applied = index;
                    }
                    let current = shadow.as_ref().unwrap_or(left);
                    // Copy inserts into arrays; it cannot directly replace an
                    // array element. Check the parent in the current document.
                    let eligible = !matches!(op, PatchOperation::Replace(_))
                        || parent(destination).is_none_or(|parent| {
                            !current.pointer(parent).is_some_and(Value::is_array)
                        });
                    if eligible {
                        let mut best = None;
                        find_copy(
                            current,
                            "",
                            value,
                            destination,
                            original_size - minimum_size,
                            &mut best,
                        )?;
                        if let Some((source, _)) = best {
                            let copied =
                                decode(json!({"op": "copy", "from": source, "path": destination}))?;
                            if bytes(&copied)? < original_size {
                                replacement = Some(copied);
                            }
                        }
                    }
                }
            }
        }
        if let Some(replacement) = replacement {
            if loaded_scalars != 0 {
                if let Some(source) = from(&replacement) {
                    source_reduction +=
                        path_minimum(source)?.saturating_sub(path_minimum(path(&replacement))?);
                }
            }
            patch.0[index] = replacement;
        }
        // Include nested payloads for later operations. Moves and copies only
        // reuse values already present, so they introduce no new scalar keys.
        if loaded_scalars != 0 {
            let prefix = path_minimum(path(&patch.0[index]))?;
            match &patch.0[index] {
                PatchOperation::Add(op) => {
                    collect_scalars(&op.value, prefix, &mut scalars, loaded_scalars)?;
                }
                PatchOperation::Replace(op) => {
                    collect_scalars(&op.value, prefix, &mut scalars, loaded_scalars)?;
                }
                _ => {}
            }
        }
    }
    Ok(patch)
}

#[derive(Hash, PartialEq, Eq)]
enum ScalarKey {
    Null,
    Bool(bool),
    String(String),
    Number(crate::numbers::NumberKey<'static>),
}

fn scalar_key(value: &Value) -> Option<ScalarKey> {
    Some(match value {
        Value::Null => ScalarKey::Null,
        Value::Bool(value) => ScalarKey::Bool(*value),
        Value::String(value) => ScalarKey::String(value.clone()),
        Value::Number(value) => ScalarKey::Number(crate::numbers::number_key(value).into_owned()),
        Value::Array(_) | Value::Object(_) => return None,
    })
}

fn scalar_kind(value: &Value) -> u8 {
    match value {
        Value::Null => 1,
        Value::Bool(_) => 2,
        Value::String(_) => 4,
        Value::Number(_) => 8,
        Value::Array(_) | Value::Object(_) => 0,
    }
}

fn numeric_token(token: &str) -> bool {
    !token.is_empty() && token.bytes().all(|byte| byte.is_ascii_digit())
}

fn path_minimum(path: &str) -> Result<usize, Error> {
    // Array insertions/removals can shorten decimal indices. Treat every
    // numeric token as one digit, even numeric object keys, for a safe bound.
    let mut shortest = String::with_capacity(path.len());
    for token in path.split('/').skip(1) {
        shortest.push('/');
        shortest.push_str(if numeric_token(token) { "0" } else { token });
    }
    bytes(&shortest).map(|size| size - 2)
}

fn collect_scalars(
    value: &Value,
    prefix: usize,
    output: &mut HashMap<ScalarKey, usize>,
    kinds: u8,
) -> Result<(), Error> {
    let kind = scalar_kind(value);
    if kind != 0 {
        if kind & kinds != 0 {
            if let Some(key) = scalar_key(value) {
                output
                    .entry(key)
                    .and_modify(|cost| *cost = (*cost).min(prefix))
                    .or_insert(prefix);
            }
        }
        return Ok(());
    }
    match value {
        Value::Array(values) => {
            for child in values {
                collect_scalars(child, prefix + 2, output, kinds)?;
            }
        }
        Value::Object(values) => {
            for (key, child) in values {
                let suffix = if numeric_token(key) {
                    2
                } else {
                    bytes(&pointer("", key))? - 2
                };
                collect_scalars(child, prefix + suffix, output, kinds)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn find_copy(
    value: &Value,
    path: &str,
    wanted: &Value,
    destination: &str,
    budget: usize,
    best: &mut Option<(String, usize)>,
) -> Result<(), Error> {
    // A source pointer adds its serialized string contents to the empty-from
    // copy's size. Its raw UTF-8 length is a safe lower bound on that cost.
    // Descendant pointers only grow, so unprofitable branches need no search.
    let limit = best.as_ref().map_or(budget, |(_, cost)| budget.min(*cost));
    if path.len() >= limit {
        return Ok(());
    }
    if path != destination && json_equal(value, wanted) {
        let cost = bytes(path)? - 2;
        if cost < limit {
            *best = Some((path.to_owned(), cost));
        }
    }
    match value {
        Value::Array(values) => {
            for (index, child) in values.iter().enumerate() {
                let limit = best.as_ref().map_or(budget, |(_, cost)| budget.min(*cost));
                let digits = index.checked_ilog10().map_or(1, |power| power as usize + 1);
                if path.len() + 1 + digits >= limit {
                    // Later decimal array indices cannot have shorter paths.
                    break;
                }
                find_copy(
                    child,
                    &pointer(path, index),
                    wanted,
                    destination,
                    budget,
                    best,
                )?;
            }
        }
        Value::Object(values) => {
            for (key, child) in values {
                let limit = best.as_ref().map_or(budget, |(_, cost)| budget.min(*cost));
                let escaped = key
                    .bytes()
                    .filter(|byte| matches!(byte, b'~' | b'/'))
                    .count();
                if path.len() + 1 + key.len() + escaped >= limit {
                    continue;
                }
                find_copy(
                    child,
                    &pointer(path, key),
                    wanted,
                    destination,
                    budget,
                    best,
                )?;
            }
        }
        _ => {}
    }
    Ok(())
}

// Index destinations once while the original patch is unchanged. A rejected
// candidate does not affect these lists or their original operation order.
fn selection_index(patch: &Patch) -> (HashMap<String, Vec<usize>>, HashSet<String>, Vec<usize>) {
    let mut selected: HashMap<String, Vec<usize>> = HashMap::new();
    let mut destinations = HashSet::new();
    let mut moves = Vec::new();
    for (index, operation) in patch.0.iter().enumerate() {
        destinations.insert(path(operation).to_owned());
        let mut cursor = path(operation);
        loop {
            selected.entry(cursor.to_owned()).or_default().push(index);
            let Some(ancestor) = parent(cursor) else {
                break;
            };
            cursor = ancestor;
        }
        if matches!(operation, PatchOperation::Move(_)) {
            moves.push(index);
        }
    }
    (selected, destinations, moves)
}

fn ancestor_arrays<'a>(left: &Value, ancestor: &'a str) -> Vec<&'a str> {
    let mut arrays = Vec::new();
    let mut cursor = ancestor;
    while let Some(above) = parent(cursor) {
        if left.pointer(above).is_some_and(Value::is_array) {
            arrays.push(above);
        }
        cursor = above;
    }
    arrays
}

#[inline(always)]
fn independent_operation(
    operation: &PatchOperation,
    ancestor: &str,
    destination_inside: bool,
    arrays: &[&str],
) -> bool {
    let destination = path(operation);
    // The caller has already rejected strict ancestor destination writes.
    if let Some(source) = from(operation) {
        let source_inside = contains(ancestor, source);
        if source_inside != destination_inside || contains(source, ancestor) && !source_inside {
            return false;
        }
    }
    // Structural writes directly into any array on the boundary's path
    // can shift that boundary. Deeper writes and Replace do not shift it.
    if matches!(
        operation,
        PatchOperation::Add(_)
            | PatchOperation::Remove(_)
            | PatchOperation::Move(_)
            | PatchOperation::Copy(_)
    ) && parent(destination).is_some_and(|p| arrays.contains(&p))
    {
        return false;
    }
    if let PatchOperation::Move(operation) = operation {
        if parent(operation.from.as_str()).is_some_and(|p| arrays.contains(&p)) {
            return false;
        }
    }
    true
}

// Replacing this complete subtree commutes with the other operations only
// when neither pointers nor reads outside it depend on its intermediate state.
fn independent_replacement(left: &Value, patch: &Patch, ancestor: &str) -> bool {
    let arrays = ancestor_arrays(left, ancestor);
    patch.0.iter().all(|operation| {
        let inside = contains(ancestor, path(operation));
        (inside || !contains(path(operation), ancestor))
            && independent_operation(operation, ancestor, inside, &arrays)
    })
}

// Add-like guards can snapshot their destination parent, even when the
// destination lies outside the replacement. Do not reuse such an ancestor
// snapshot: it may observe the subtree before all selected edits finish.
fn independent_guard_read(
    operation: &PatchOperation,
    ancestor: &str,
    destination_inside: bool,
) -> bool {
    !matches!(
        operation,
        PatchOperation::Add(_) | PatchOperation::Copy(_) | PatchOperation::Move(_)
    ) || destination_inside
        || parent(path(operation)).is_some_and(|parent| !contains(parent, ancestor))
}

fn independent_guard_reads(patch: &Patch, ancestor: &str) -> bool {
    patch.0.iter().all(|operation| {
        independent_guard_read(operation, ancestor, contains(ancestor, path(operation)))
    })
}

fn candidate_applies<'a>(
    left: &Value,
    right: &Value,
    operations: impl Iterator<Item = &'a PatchOperation>,
) -> bool {
    if delta::check_depth(left, 128).is_err() {
        return false;
    }
    let mut shadow = left.clone();
    for (index, operation) in operations.enumerate() {
        if crate::apply_json_patch_step(&mut shadow, operation, index).is_err() {
            return false;
        }
    }
    json_equal(&shadow, right)
}

// Replace-only patches have no shifting indices, source reads or parent
// snapshot guards. Fine-first candidates can only contain a previously accepted
// boundary or be disjoint from it, so the original selection lists remain valid
// when removed slots are filtered out. Keep one exact byte weight per slot,
// including its group's leading comma, and compact the vector once at the end.
fn rationalize_replacements(
    left: &Value,
    right: &Value,
    patch: &mut Patch,
    parents: &[String],
    mut current_size: usize,
    guard_costs: &[GuardCost],
    baseline_verified: &mut Option<bool>,
) -> Result<bool, Error> {
    let tests = !guard_costs.is_empty();
    // Leaf destinations that are never parent candidates need no selection
    // entry. Borrow the already owned candidate paths instead of cloning them.
    let mut selections: HashMap<&str, Vec<usize>> = parents
        .iter()
        .map(|ancestor| (ancestor.as_str(), Vec::new()))
        .collect();
    for (index, op) in patch.0.iter().enumerate() {
        let mut cursor = path(op);
        loop {
            if let Some(selected) = selections.get_mut(cursor) {
                selected.push(index);
            }
            let Some(above) = parent(cursor) else {
                break;
            };
            cursor = above;
        }
    }
    // A strict ancestor write cannot be removed by an earlier fine-first
    // candidate. Freeze these flags while paths can still be borrowed, then
    // release the destination set before replacing any operation slots.
    let crossed: Vec<_> = {
        let destinations: HashSet<_> = patch.0.iter().map(path).collect();
        parents
            .iter()
            .map(|ancestor| {
                let mut cursor = ancestor.as_str();
                while let Some(above) = parent(cursor) {
                    if destinations.contains(above) {
                        return true;
                    }
                    cursor = above;
                }
                false
            })
            .collect()
    };
    let mut costs = if tests {
        guard_costs
            .windows(2)
            .map(|group| group[1].bytes - group[0].bytes + usize::from(group[0].operations == 0))
            .collect::<Vec<_>>()
    } else {
        patch
            .0
            .iter()
            .map(|op| bytes(op).map(|size| size + 1))
            .collect::<Result<Vec<_>, _>>()?
    };
    let original_length = patch.0.len();
    let mut length = original_length;
    for (ancestor, crossed) in parents.iter().zip(crossed) {
        let (Some(old), Some(new)) = (left.pointer(ancestor), right.pointer(ancestor)) else {
            continue;
        };
        if crossed {
            continue;
        }
        let selected: Vec<_> = selections
            .get(ancestor.as_str())
            .into_iter()
            .flatten()
            .copied()
            .filter(|index| costs[*index] != 0)
            .collect();
        if selected.is_empty() {
            continue;
        }
        let removed: usize = selected.iter().map(|index| costs[*index]).sum();
        let budget = if tests {
            current_size.saturating_sub(3)
        } else {
            removed - 1
        };
        let Some(replacement_size) = bytes_below(
            &ValueOperation {
                op: "replace",
                path: ancestor,
                value: new,
            },
            budget,
        )?
        else {
            continue;
        };
        let test_size = if tests {
            let Some(size) = bytes_below(
                &ValueOperation {
                    op: "test",
                    path: ancestor,
                    value: old,
                },
                budget - replacement_size,
            )?
            else {
                continue;
            };
            size + 1
        } else {
            0
        };
        let replaced = decode(json!({"op": "replace", "path": ancestor, "value": new}))?;
        let replacement_cost = replacement_size + test_size + 1;
        let candidate_size = current_size - removed + replacement_cost;
        if candidate_size >= current_size {
            continue;
        }
        if ancestor.is_empty() {
            // Root is the last fine-first candidate. No later selection needs
            // the stable slots or index, so release them before real validation
            // clones the complete document and root replacement payload.
            drop(selections);
            if length < original_length {
                let mut index = 0;
                patch.0.retain(|_| {
                    let keep = costs[index] != 0;
                    index += 1;
                    keep
                });
                patch.0.shrink_to_fit();
            }
            drop(costs);
            if candidate_applies(left, right, std::iter::once(&replaced)) {
                patch.0 = vec![replaced];
            }
            return Ok(true);
        }
        if length == 1 {
            if !candidate_applies(left, right, std::iter::once(&replaced)) {
                continue;
            }
        } else if !*baseline_verified.get_or_insert_with(|| {
            apply_json_patch(left, patch).is_ok_and(|value| json_equal(&value, right))
        }) {
            // No replacement has been accepted before this lazy check. Leave
            // the original patch intact for the full candidate-validation path.
            return Ok(false);
        }
        let first = selected[0];
        patch.0[first] = replaced;
        costs[first] = replacement_cost;
        for &index in &selected[1..] {
            costs[index] = 0;
            if let PatchOperation::Replace(op) = &mut patch.0[index] {
                // Removed groups need no pointer or payload. Only their stable
                // vector slot remains until the final compaction.
                op.path = Default::default();
                op.value = Value::Null;
            }
        }
        length -= selected.len() - 1;
        current_size = candidate_size;
        *baseline_verified = Some(true);
    }
    if length < original_length {
        let mut index = 0;
        patch.0.retain(|_| {
            let keep = costs[index] != 0;
            index += 1;
            keep
        });
        patch.0.shrink_to_fit();
    }
    Ok(true)
}

fn rationalize(left: &Value, right: &Value, mut patch: Patch, tests: bool) -> Result<Patch, Error> {
    let mut parents = BTreeSet::new();
    for op in &patch.0 {
        let mut cursor = path(op);
        while let Some(ancestor) = parent(cursor) {
            parents.insert(ancestor.to_owned());
            cursor = ancestor;
        }
    }
    if parents.is_empty() {
        return Ok(patch);
    }
    let mut parents: Vec<_> = parents.into_iter().collect();
    let broad_first = tests
        && patch
            .0
            .iter()
            .any(|op| matches!(op, PatchOperation::Move(_)));
    parents.sort_by(|a, b| {
        let depth = a.matches('/').count().cmp(&b.matches('/').count());
        // Guarded array moves can snapshot the whole container for every
        // candidate. Try broad replacements first so an accepted parent removes
        // its descendants before they trigger those repeated simulations.
        // Other patches retain the finer-first compression heuristic.
        (if broad_first { depth } else { depth.reverse() }).then_with(|| a.cmp(b))
    });
    let original_length = patch.0.len();
    let (mut current_size, mut guard_costs, mut baseline_verified) = if tests {
        let (size, costs, shadow) =
            guarded_operation_bytes(left, patch.0.iter(), patch.0.len(), &[])?;
        // Only a multi-operation patch with non-root candidates can reuse
        // this proof. Root and single-operation candidates are applied directly.
        let verified = (patch.0.len() > 1 && parents.len() > 1).then(|| json_equal(&shadow, right));
        (size, costs, verified)
    } else {
        let (size, costs) = guarded_bytes(left, &patch, false, &[])?;
        (size, costs, None)
    };
    if patch.0.len() > 1
        && parents.len() > 1
        && patch
            .0
            .iter()
            .all(|op| matches!(op, PatchOperation::Replace(_)))
        && baseline_verified != Some(false)
        && rationalize_replacements(
            left,
            right,
            &mut patch,
            &parents,
            current_size,
            &guard_costs,
            &mut baseline_verified,
        )?
    {
        return Ok(patch);
    }
    let mut selections = None;
    // After an accepted replacement, use the original scan rather than
    // rebuilding an index for every independently compressible parent.
    let mut index_unchanged = true;
    for ancestor in parents {
        let (Some(old), Some(new)) = (left.pointer(&ancestor), right.pointer(&ancestor)) else {
            continue;
        };
        let mut selection_proof = None;
        let (selected, boundary_crossed) = if ancestor.is_empty() {
            ((0..patch.0.len()).collect::<Vec<_>>(), false)
        } else if patch.0.len() == 1 {
            let operation = &patch.0[0];
            let destination_inside = contains(&ancestor, path(operation));
            let crossed = (contains(path(operation), &ancestor) && !destination_inside)
                || matches!(operation, PatchOperation::Move(_))
                    && from(operation)
                        .is_some_and(|source| contains(&ancestor, source) != destination_inside);
            (
                if destination_inside {
                    vec![0]
                } else {
                    Vec::new()
                },
                crossed,
            )
        } else if !index_unchanged {
            let mut selected = Vec::new();
            let mut crossed = false;
            let arrays = if tests {
                ancestor_arrays(left, &ancestor)
            } else {
                Vec::new()
            };
            let mut independent = true;
            for (index, operation) in patch.0.iter().enumerate() {
                let inside = contains(&ancestor, path(operation));
                if contains(path(operation), &ancestor) && !inside {
                    crossed = true;
                    break;
                }
                if matches!(operation, PatchOperation::Move(_))
                    && from(operation).is_some_and(|source| contains(&ancestor, source) != inside)
                {
                    crossed = true;
                    break;
                }
                // Guard costing needs this proof for viable candidates.
                // Reuse the scan's destination classification only in guarded
                // mode; unguarded size bounds can reject the candidate first.
                // Copy/read failures only disable the shortcut; they do not
                // change the original Move/ancestor boundary rejection above.
                if tests {
                    independent = independent
                        && independent_operation(operation, &ancestor, inside, &arrays)
                        && independent_guard_read(operation, &ancestor, inside);
                }
                if inside {
                    selected.push(index);
                }
            }
            selection_proof = tests.then_some(independent);
            (selected, crossed)
        } else {
            let (selected_by_parent, destinations, moves) =
                selections.get_or_insert_with(|| selection_index(&patch));
            let selected = selected_by_parent
                .get(&ancestor)
                .cloned()
                .unwrap_or_default();
            let mut crossed = false;
            let mut cursor = ancestor.as_str();
            while let Some(above) = parent(cursor) {
                if destinations.contains(above) {
                    crossed = true;
                    break;
                }
                cursor = above;
            }
            if !crossed {
                for &index in moves.iter() {
                    if let PatchOperation::Move(operation) = &patch.0[index] {
                        if contains(&ancestor, operation.from.as_str())
                            != contains(&ancestor, operation.path.as_str())
                        {
                            crossed = true;
                            break;
                        }
                    }
                }
            }
            (selected, crossed)
        };
        if boundary_crossed || selected.is_empty() {
            continue;
        }
        let removed_size = if tests {
            0
        } else {
            selected
                .iter()
                .try_fold(0, |size, index| bytes(&patch.0[*index]).map(|n| size + n))?
                + selected.len()
                - 1
        };
        let budget = if tests {
            current_size.saturating_sub(3)
        } else {
            removed_size
        };
        let Some(replacement_size) = bytes_below(
            &ValueOperation {
                op: "replace",
                path: &ancestor,
                value: new,
            },
            budget,
        )?
        else {
            continue;
        };
        let (candidate_size, replacement_test_size) = if tests {
            let Some(test_size) = bytes_below(
                &ValueOperation {
                    op: "test",
                    path: &ancestor,
                    value: old,
                },
                budget - replacement_size,
            )?
            else {
                continue;
            };
            (None, test_size)
        } else {
            (Some(current_size - removed_size + replacement_size), 0)
        };
        let replaced = decode(json!({"op": "replace", "path": ancestor, "value": new}))?;
        let first = selected[0];
        let selected: BTreeSet<_> = selected.into_iter().collect();
        let candidate = || {
            patch.0.iter().enumerate().filter_map(|(index, operation)| {
                if index == first {
                    Some(&replaced)
                } else if selected.contains(&index) {
                    None
                } else {
                    Some(operation)
                }
            })
        };
        // Independent boundaries preserve both external applications and
        // their guard reads. Their exact per-operation costs can be reused;
        // otherwise retain the complete guard simulation and its final shadow.
        let independent_guards = tests
            && !ancestor.is_empty()
            && baseline_verified == Some(true)
            && selection_proof.unwrap_or_else(|| {
                independent_replacement(left, &patch, &ancestor)
                    && independent_guard_reads(&patch, &ancestor)
            });
        let mut guarded_shadow = None;
        let cost = if let Some(size) = candidate_size {
            Ok((size, Vec::new()))
        } else if ancestor.is_empty() {
            // The one root replacement has no outside guard reads. Its exact
            // cost is known independently of the original patch's final value;
            // candidate_applies below still performs real validation.
            let size = replacement_size + replacement_test_size + 3;
            Ok((
                size,
                vec![
                    GuardCost {
                        bytes: 2,
                        operations: 0,
                    },
                    GuardCost {
                        bytes: size,
                        operations: 2,
                    },
                ],
            ))
        } else if independent_guards {
            Ok(replacement_guard_costs(
                &guard_costs,
                &selected,
                first,
                replacement_size + replacement_test_size + 1,
            ))
        } else {
            guarded_operation_bytes(
                left,
                candidate(),
                patch.0.len() - selected.len() + 1,
                &guard_costs[..=first],
            )
            .map(|(size, costs, shadow)| {
                guarded_shadow = Some(shadow);
                (size, costs)
            })
        };
        if let Ok((candidate_size, candidate_costs)) = cost {
            if candidate_size >= current_size {
                continue;
            }
            let valid = if let Some(shadow) = guarded_shadow {
                json_equal(&shadow, right)
            } else if ancestor.is_empty() || patch.0.len() == 1 {
                // Validating the one-step candidate is cheaper than replaying
                // a potentially large original sequence for a root replacement.
                candidate_applies(left, right, candidate())
            } else if independent_guards {
                true
            } else if selection_proof
                .unwrap_or_else(|| independent_replacement(left, &patch, &ancestor))
                && *baseline_verified.get_or_insert_with(|| {
                    apply_json_patch(left, &patch).is_ok_and(|value| json_equal(&value, right))
                })
            {
                // A verified original patch plus an independent boundary proves
                // that replacing all its interior edits with the known target
                // leaves every external operation and final value unchanged.
                true
            } else {
                candidate_applies(left, right, candidate())
            };
            if valid {
                let mut replacement = Some(replaced);
                patch.0 = std::mem::take(&mut patch.0)
                    .into_iter()
                    .enumerate()
                    .filter_map(|(index, operation)| {
                        if index == first {
                            replacement.take()
                        } else if selected.contains(&index) {
                            None
                        } else {
                            Some(operation)
                        }
                    })
                    .collect();
                selections = None;
                index_unchanged = false;
                baseline_verified = Some(true);
                current_size = candidate_size;
                guard_costs = candidate_costs;
            }
        }
    }
    if patch.0.len() < original_length {
        // In-place filtering can retain the original operation allocation.
        // Release it once before guard generation clones the final payloads.
        patch.0.shrink_to_fit();
    }
    Ok(patch)
}

#[derive(Clone, Copy)]
struct GuardCost {
    bytes: usize,
    operations: usize,
}

// Each prefix difference contains one complete operation group: generated
// tests followed by the original operation. Strip its old leading comma and
// rebuild every prefix after replacing the selected groups, including counts.
fn replacement_guard_costs(
    costs: &[GuardCost],
    selected: &BTreeSet<usize>,
    first: usize,
    replacement_bytes: usize,
) -> (usize, Vec<GuardCost>) {
    let mut total = GuardCost {
        bytes: 2,
        operations: 0,
    };
    let mut result = Vec::with_capacity(costs.len() - selected.len() + 1);
    result.push(total);
    for (index, group) in costs.windows(2).enumerate() {
        let (group_bytes, operations) = if index == first {
            (replacement_bytes, 2)
        } else if selected.contains(&index) {
            continue;
        } else {
            (
                group[1].bytes - group[0].bytes - usize::from(group[0].operations > 0),
                group[1].operations - group[0].operations,
            )
        };
        total.bytes += group_bytes + usize::from(total.operations > 0);
        total.operations += operations;
        result.push(total);
    }
    (total.bytes, result)
}

fn guarded_bytes(
    left: &Value,
    patch: &Patch,
    tests: bool,
    prefix: &[GuardCost],
) -> Result<(usize, Vec<GuardCost>), Error> {
    if !tests {
        return Ok((bytes(patch)?, Vec::new()));
    }
    guarded_operation_bytes(left, patch.0.iter(), patch.0.len(), prefix)
        .map(|(size, costs, _)| (size, costs))
}

fn guarded_operation_bytes<'a>(
    left: &Value,
    operations: impl Iterator<Item = &'a PatchOperation> + Clone,
    length: usize,
    prefix: &[GuardCost],
) -> Result<(usize, Vec<GuardCost>, Value), Error> {
    let mut shadow = left.clone();
    let first = prefix.len().saturating_sub(1);
    // Only the operations before the candidate's first change are identical.
    // Execute them on the same shadow, reusing their exact serialization cost.
    // Recompute every later guard: copy sources and array indices may depend on
    // the replacement even when their operations lie outside its subtree.
    for (index, op) in operations.clone().take(first).enumerate() {
        crate::apply_json_patch_step(&mut shadow, op, index)?;
    }
    let mut costs = Vec::with_capacity(length + 1);
    if prefix.is_empty() {
        costs.push(GuardCost {
            bytes: 2, // Array brackets.
            operations: 0,
        });
    } else {
        costs.extend_from_slice(prefix);
    }
    let mut total = costs[first];
    for (index, op) in operations.enumerate().skip(first) {
        for (path, value) in guard_values(&shadow, op)?.into_iter().flatten() {
            total.bytes += bytes(&ValueOperation {
                op: "test",
                path,
                value,
            })? + usize::from(total.operations > 0);
            total.operations += 1;
        }
        total.bytes += bytes(op)? + usize::from(total.operations > 0);
        total.operations += 1;
        crate::apply_json_patch_step(&mut shadow, op, index)?;
        costs.push(total);
    }
    Ok((total.bytes, costs, shadow))
}

fn test_source<'a>(shadow: &'a Value, path: &str) -> Result<&'a Value, Error> {
    shadow
        .pointer(path)
        .ok_or_else(|| Error::new(path, "test source is missing"))
}

fn guard_add_value<'a, 'b>(
    shadow: &'a Value,
    path: &'b str,
) -> Result<(&'b str, &'a Value), Error> {
    let Some(parent) = parent(path) else {
        return Ok(("", shadow));
    };
    let container = shadow
        .pointer(parent)
        .ok_or_else(|| Error::new(parent, "addition parent is missing"))?;
    // RFC test has no "must be absent" operation. Testing the parent protects
    // missing object keys, append positions and the order of array insertions.
    if container.is_array() {
        return Ok((parent, container));
    }
    Ok(shadow
        .pointer(path)
        .map_or((parent, container), |value| (path, value)))
}

fn guard_values<'a, 'b>(
    shadow: &'a Value,
    op: &'b PatchOperation,
) -> Result<[Option<(&'b str, &'a Value)>; 2], Error> {
    Ok(match op {
        PatchOperation::Add(op) => [Some(guard_add_value(shadow, op.path.as_str())?), None],
        PatchOperation::Remove(op) => [
            Some((op.path.as_str(), test_source(shadow, op.path.as_str())?)),
            None,
        ],
        PatchOperation::Replace(op) => [
            Some((op.path.as_str(), test_source(shadow, op.path.as_str())?)),
            None,
        ],
        PatchOperation::Move(op) => [
            Some((op.from.as_str(), test_source(shadow, op.from.as_str())?)),
            Some(guard_add_value(shadow, op.path.as_str())?),
        ],
        PatchOperation::Copy(op) => [
            Some((op.from.as_str(), test_source(shadow, op.from.as_str())?)),
            Some(guard_add_value(shadow, op.path.as_str())?),
        ],
        PatchOperation::Test(_) => [None, None],
    })
}

fn guard(mut shadow: Value, patch: &Patch) -> Result<Patch, Error> {
    let mut output = Vec::new();
    for (index, op) in patch.0.iter().enumerate() {
        for (path, value) in guard_values(&shadow, op)?.into_iter().flatten() {
            output.push(decode(json!({"op": "test", "path": path, "value": value}))?);
        }
        output.push(op.clone());
        crate::apply_json_patch_step(&mut shadow, op, index)?;
    }
    Ok(Patch(output))
}

/// Produce an inverse from the original operations and their complete baseline.
/// Tests make no changes and therefore produce no inverse operation. A move may
/// need remove/add/replace operations to restore a destination it overwrote.
pub fn invert_json_patch(left: &Value, patch: &Patch) -> Result<Patch, Error> {
    invert_with_target(left, patch).map(|(inverse, _)| inverse)
}

/// Generate an inverse with tests for the values and containers it will touch.
/// Guards are computed from the successfully patched baseline. They do not
/// authenticate unrelated document fields, and container tests can be large.
pub fn invert_json_patch_guarded(left: &Value, patch: &Patch) -> Result<Patch, Error> {
    let (inverse, right) = invert_with_target(left, patch)?;
    guard(right, &inverse)
}

fn invert_with_target(left: &Value, patch: &Patch) -> Result<(Patch, Value), Error> {
    delta::check_depth(left, 128)?;
    let mut shadow = left.clone();
    let mut groups = Vec::new();
    for (index, op) in patch.0.iter().enumerate() {
        let inverse = match op {
            PatchOperation::Add(op) => undo_add(&shadow, op.path.as_str(), None),
            PatchOperation::Copy(op) => undo_add(&shadow, op.path.as_str(), None),
            PatchOperation::Remove(op) => old_operation(&shadow, "add", op.path.as_str()),
            PatchOperation::Replace(op) => old_operation(&shadow, "replace", op.path.as_str()),
            PatchOperation::Test(_) => Ok(Vec::new()),
            PatchOperation::Move(op) => undo_move(&shadow, op.from.as_str(), op.path.as_str()),
        };
        // Execute the original operation first so malformed patches preserve
        // its real error, operation index and path rather than an inverse error.
        crate::apply_json_patch_step(&mut shadow, op, index)?;
        groups.push(inverse?);
    }
    Ok((Patch(groups.into_iter().rev().flatten().collect()), shadow))
}

fn old_operation(before: &Value, op: &str, path: &str) -> Result<Vec<PatchOperation>, Error> {
    let value = before
        .pointer(path)
        .ok_or_else(|| Error::new(path, "inverse source is missing"))?;
    Ok(vec![decode(
        json!({"op": op, "path": path, "value": value}),
    )?])
}

// Translate a lookup in the document after a removal to the original document.
// Only removing an array element shifts a later sibling and its descendants.
fn before_removal_path(before: &Value, path: &str, removed: Option<&str>) -> Result<String, Error> {
    let Some(removed) = removed else {
        return Ok(path.to_owned());
    };
    let Some((container, removed_index)) = removed.rsplit_once('/') else {
        return Ok(path.to_owned());
    };
    if !before.pointer(container).is_some_and(Value::is_array)
        || path == container
        || !contains(container, path)
    {
        return Ok(path.to_owned());
    }
    let removed_index = removed_index
        .parse::<usize>()
        .map_err(|_| Error::new(removed, "invalid removal index"))?;
    let suffix = &path[container.len() + 1..];
    let (index, remainder) = suffix
        .split_once('/')
        .map_or((suffix, ""), |(index, tail)| (index, tail));
    let index = index
        .parse::<usize>()
        .map_err(|_| Error::new(path, "invalid array index"))?;
    if index < removed_index {
        return Ok(path.to_owned());
    }
    let index = index
        .checked_add(1)
        .ok_or_else(|| Error::new(path, "array index overflow"))?;
    let translated = pointer(container, index);
    Ok(if remainder.is_empty() {
        translated
    } else {
        format!("{translated}/{remainder}")
    })
}

fn undo_add(
    before: &Value,
    path: &str,
    removed: Option<&str>,
) -> Result<Vec<PatchOperation>, Error> {
    let Some(container_path) = parent(path) else {
        return old_operation(before, "replace", "");
    };
    let lookup_parent = before_removal_path(before, container_path, removed)?;
    let container = before
        .pointer(&lookup_parent)
        .ok_or_else(|| Error::new(path, "inverse destination parent is missing"))?;
    if let Value::Array(values) = container {
        let token = path.rsplit_once('/').map(|(_, token)| token).unwrap_or("");
        let actual_path = if token == "-" {
            let same_array_removal =
                removed.is_some_and(|removed| parent(removed) == Some(container_path));
            let index = values
                .len()
                .checked_sub(usize::from(same_array_removal))
                .ok_or_else(|| Error::new(path, "invalid append index"))?;
            pointer(container_path, index)
        } else {
            path.to_owned()
        };
        return Ok(vec![decode(json!({"op": "remove", "path": actual_path}))?]);
    }
    if !container.is_object() {
        return Err(Error::new(
            path,
            "inverse destination parent is not a container",
        ));
    }
    let lookup = before_removal_path(before, path, removed)?;
    match before.pointer(&lookup) {
        Some(value) => Ok(vec![decode(
            json!({"op": "replace", "path": path, "value": value}),
        )?]),
        None => Ok(vec![decode(json!({"op": "remove", "path": path}))?]),
    }
}

fn undo_move(
    before: &Value,
    source: &str,
    destination: &str,
) -> Result<Vec<PatchOperation>, Error> {
    if source == destination {
        return Ok(Vec::new());
    }
    // An object/root destination can overwrite an ancestor containing the
    // source. Restoring that whole value also restores the removed descendant.
    if contains(destination, source)
        && parent(destination)
            .is_none_or(|parent| before.pointer(parent).is_some_and(Value::is_object))
    {
        return old_operation(before, "replace", destination);
    }
    let mut result = undo_add(before, destination, Some(source))?;
    result.extend(old_operation(before, "add", source)?);
    Ok(result)
}

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

    fn patch(value: Value) -> Patch {
        serde_json::from_value(value).unwrap()
    }

    #[test]
    fn stable_replacement_slots_preserve_interleaved_children_and_repeated_paths() {
        for nested in [false, true] {
            let left = if nested {
                json!({
                    "a": {"leaf": {"a_long_property_name": 0, "b_long_property_name": 0}},
                    "keep": "unchanged 🦀".repeat(500),
                })
            } else {
                json!({
                    "a": {
                        "left": {"a_long_property_name": 0, "b_long_property_name": 0},
                        "right": {"a_long_property_name": 0, "b_long_property_name": 0},
                    },
                    "keep": "unchanged 🦀".repeat(500),
                })
            };
            let original = if nested {
                patch(json!([
                    {"op":"replace","path":"/a","value":{"leaf":{"a_long_property_name":1,"b_long_property_name":1}}},
                    {"op":"replace","path":"/a/leaf/a_long_property_name","value":2},
                    {"op":"replace","path":"/a/leaf/a_long_property_name","value":3},
                    {"op":"replace","path":"/a/leaf/b_long_property_name","value":4},
                ]))
            } else {
                patch(json!([
                    {"op":"replace","path":"/a/left/a_long_property_name","value":1},
                    {"op":"replace","path":"/a/right/a_long_property_name","value":3},
                    {"op":"replace","path":"/a/left/b_long_property_name","value":2},
                    {"op":"replace","path":"/a/right/b_long_property_name","value":4},
                ]))
            };
            let right = apply_json_patch(&left, &original).unwrap();
            // Complete wire frozen from the published 0.1.1 implementation.
            // Interleaved siblings first compress, then their common ancestor;
            // nested original writes retain their intermediate guard costs.
            let expected = patch(json!([
                {"op":"replace","path":"/a","value":right["a"]},
            ]));
            for tests in [false, true] {
                let optimized = rationalize(&left, &right, original.clone(), tests).unwrap();
                assert_eq!(
                    serde_json::to_vec(&optimized).unwrap(),
                    serde_json::to_vec(&expected).unwrap()
                );
                let guarded = if tests {
                    guard(left.clone(), &optimized).unwrap()
                } else {
                    optimized
                };
                assert_eq!(apply_json_patch(&left, &guarded).unwrap(), right);
                let inverse = invert_json_patch(&left, &guarded).unwrap();
                assert_eq!(apply_json_patch(&right, &inverse).unwrap(), left);
            }
        }
    }

    #[test]
    fn invalid_original_replacements_keep_plain_replay_and_guard_errors() {
        let left = json!({"a":{"x":0},"keep":"unchanged 🦀".repeat(500)});
        let right = json!({"a":{"x":1,"missing":2},"keep":"unchanged 🦀".repeat(500)});
        let original = patch(json!([
            {"op":"replace","path":"/a/x","value":1},
            {"op":"replace","path":"/a/missing","value":2},
        ]));
        // Frozen 0.1.1 behavior: the invalid original is not an error gate for
        // plain compression, since the full parent candidate repairs it.
        let expected = patch(json!([
            {"op":"replace","path":"/a","value":{"missing":2,"x":1}},
        ]));
        let optimized = rationalize(&left, &right, original.clone(), false).unwrap();
        assert_eq!(
            serde_json::to_vec(&optimized).unwrap(),
            serde_json::to_vec(&expected).unwrap()
        );
        assert_eq!(apply_json_patch(&left, &optimized).unwrap(), right);
        let inverse = invert_json_patch(&left, &optimized).unwrap();
        assert_eq!(apply_json_patch(&right, &inverse).unwrap(), left);
        let error = rationalize(&left, &right, original, true).unwrap_err();
        assert_eq!(error.path, "/a/missing");
        assert_eq!(error.message, "test source is missing");
    }

    #[test]
    fn pointer_tokens_preserve_parent_escapes_and_unicode() {
        for (parent, token, expected) in [
            ("", "", "/"),
            ("", "a/b~c", "/a~1b~0c"),
            ("/~0~1🦀", "~/🦀", "/~0~1🦀/~0~1🦀"),
            ("/parent", "~1", "/parent/~01"),
            ("/parent", "a~0b", "/parent/a~00b"),
        ] {
            assert_eq!(pointer(parent, token), expected);
        }
        assert_eq!(pointer("/🦀", 12), "/🦀/12");
    }

    #[test]
    fn selection_scan_keeps_copy_guard_and_array_fallbacks_after_parent_acceptance() {
        for kind in 0..3 {
            let mut left = json!({
                "0accepted": {"leaf": {"a_long_property_name": 0, "b_long_property_name": 0}},
                "a": {"a_long_property_name": 0, "b_long_property_name": 0},
                "sink": 0,
                "source": 7,
                "keep": "unchanged 🦀".repeat(500),
            });
            let prefix = json!([
                {"op":"replace","path":"/0accepted/leaf/a_long_property_name","value":1},
                {"op":"replace","path":"/0accepted/leaf/b_long_property_name","value":2},
            ]);
            let suffix = match kind {
                0 => json!([
                    {"op":"replace","path":"/a/a_long_property_name","value":11111},
                    {"op":"copy","from":"/a/b_long_property_name","path":"/sink"},
                    {"op":"replace","path":"/a/b_long_property_name","value":22222},
                    {"op":"replace","path":"/sink","value":9},
                ]),
                1 => json!([
                    {"op":"replace","path":"/a/a_long_property_name","value":11111},
                    {"op":"add","path":"/new","value":7},
                    {"op":"replace","path":"/a/b_long_property_name","value":22222},
                ]),
                _ => {
                    left["a"] = json!([
                        {"tag":"shifted"},
                        {"a_long_property_name":0,"b_long_property_name":0},
                        {"a_long_property_name":9,"b_long_property_name":9},
                    ]);
                    json!([
                        {"op":"replace","path":"/a/1/a_long_property_name","value":11111},
                        {"op":"remove","path":"/a/0"},
                        {"op":"replace","path":"/a/1/b_long_property_name","value":22222},
                    ])
                }
            };
            let mut original = patch(prefix);
            original.0.extend(patch(suffix).0);
            let right = apply_json_patch(&left, &original).unwrap();
            // Frozen from 08f3735: /0accepted/leaf is accepted first, then
            // unsafe intermediate reads/shifts still use full candidate replay.
            let target = if kind == 2 {
                json!([
                    {"a_long_property_name":11111,"b_long_property_name":0},
                    {"a_long_property_name":9,"b_long_property_name":22222},
                ])
            } else {
                json!({"a_long_property_name":11111,"b_long_property_name":22222})
            };
            let mut expected = patch(json!([
                {"op":"replace","path":"/0accepted/leaf","value":{"a_long_property_name":1,"b_long_property_name":2}},
                {"op":"replace","path":"/a","value":target},
            ]));
            if kind == 0 {
                expected.0.extend(
                    patch(json!([
                        {"op":"copy","from":"/a/b_long_property_name","path":"/sink"},
                        {"op":"replace","path":"/sink","value":9},
                    ]))
                    .0,
                );
            } else if kind == 1 {
                expected
                    .0
                    .push(decode(json!({"op":"add","path":"/new","value":7})).unwrap());
            }
            for tests in [false, true] {
                let optimized = rationalize(&left, &right, original.clone(), tests).unwrap();
                assert_eq!(
                    serde_json::to_vec(&optimized).unwrap(),
                    serde_json::to_vec(&expected).unwrap()
                );
                let output = if tests {
                    guard(left.clone(), &optimized).unwrap()
                } else {
                    optimized
                };
                assert_eq!(apply_json_patch(&left, &output).unwrap(), right);
                let inverse = invert_json_patch(&left, &output).unwrap();
                assert_eq!(apply_json_patch(&right, &inverse).unwrap(), left);
            }
        }
    }

    #[test]
    fn independent_guard_costs_match_full_replay_after_multiple_parent_replacements() {
        let left = json!({
            "a/b~": {"a_long_property_name": 0, "b_long_property_name": "old 🦀\\\""},
            "z~q": {"a_long_property_name": 0, "b_long_property_name": 0},
            "unrelated": {"source": "copy 🦀\n", "old": false},
            "before": 0,
            "after": 0,
            "flag": true,
        });
        for prefix in [false, true] {
            let mut original = patch(json!([
                {"op":"replace","path":"/a~1b~0/a_long_property_name","value":11111},
                {"op":"copy","from":"/unrelated/source","path":"/unrelated/new"},
                {"op":"replace","path":"/a~1b~0/b_long_property_name","value":"new 🦀\n\\\""},
                {"op":"test","path":"/flag","value":true},
                {"op":"replace","path":"/z~0q/a_long_property_name","value":33333},
                {"op":"remove","path":"/unrelated/old"},
                {"op":"replace","path":"/z~0q/b_long_property_name","value":44444},
                {"op":"replace","path":"/after","value":5},
            ]));
            if prefix {
                original.0.insert(
                    0,
                    decode(json!({"op":"replace","path":"/before","value":1})).unwrap(),
                );
            }
            let right = apply_json_patch(&left, &original).unwrap();
            let (_, mut costs) = guarded_bytes(&left, &original, true, &[]).unwrap();
            for ancestor in ["/a~1b~0", "/z~0q"] {
                assert!(independent_replacement(&left, &original, ancestor));
                assert!(independent_guard_reads(&original, ancestor));
                let selected: BTreeSet<_> = original
                    .0
                    .iter()
                    .enumerate()
                    .filter_map(|(index, operation)| {
                        contains(ancestor, path(operation)).then_some(index)
                    })
                    .collect();
                let first = *selected.first().unwrap();
                let replacement = decode(json!({"op":"replace","path":ancestor,"value":right.pointer(ancestor).unwrap()})).unwrap();
                let test_size = bytes(&ValueOperation {
                    op: "test",
                    path: ancestor,
                    value: left.pointer(ancestor).unwrap(),
                })
                .unwrap();
                let (size, next_costs) = replacement_guard_costs(
                    &costs,
                    &selected,
                    first,
                    test_size + bytes(&replacement).unwrap() + 1,
                );
                let candidate = Patch(
                    original
                        .0
                        .iter()
                        .enumerate()
                        .filter_map(|(index, operation)| {
                            if index == first {
                                Some(replacement.clone())
                            } else if selected.contains(&index) {
                                None
                            } else {
                                Some(operation.clone())
                            }
                        })
                        .collect(),
                );
                let actual_guards = guard(left.clone(), &candidate).unwrap();
                let (expected_size, expected_costs) =
                    guarded_bytes(&left, &candidate, true, &[]).unwrap();
                assert_eq!(size, bytes(&actual_guards).unwrap());
                assert_eq!(size, expected_size);
                assert_eq!(next_costs.len(), expected_costs.len());
                for (actual, expected) in next_costs.iter().zip(&expected_costs) {
                    assert_eq!(
                        (actual.bytes, actual.operations),
                        (expected.bytes, expected.operations)
                    );
                }
                assert_eq!(apply_json_patch(&left, &actual_guards).unwrap(), right);
                original = candidate;
                costs = next_costs;
            }
        }
    }

    #[test]
    fn external_add_copy_and_move_can_change_ancestor_guard_snapshot_costs() {
        let left = json!({
            "a/b~": {"a_long_property_name": 0, "b_long_property_name": 0},
            "source": 4,
        });
        for outside in [
            json!({"op":"add","path":"/new","value":4}),
            json!({"op":"copy","from":"/source","path":"/new"}),
            json!({"op":"move","from":"/source","path":"/new"}),
        ] {
            let original = patch(json!([
                {"op":"replace","path":"/a~1b~0/a_long_property_name","value":11111},
                outside,
                {"op":"replace","path":"/a~1b~0/b_long_property_name","value":22222},
            ]));
            let right = apply_json_patch(&left, &original).unwrap();
            let replacement =
                decode(json!({"op":"replace","path":"/a~1b~0","value":right["a/b~"]})).unwrap();
            let candidate = Patch(vec![replacement.clone(), original.0[1].clone()]);
            assert!(independent_replacement(&left, &original, "/a~1b~0"));
            assert!(!independent_guard_reads(&original, "/a~1b~0"));
            assert_eq!(apply_json_patch(&left, &candidate).unwrap(), right);
            let (_, costs) = guarded_bytes(&left, &original, true, &[]).unwrap();
            let test_size = bytes(&ValueOperation {
                op: "test",
                path: "/a~1b~0",
                value: &left["a/b~"],
            })
            .unwrap();
            let (invalid_reused_size, _) = replacement_guard_costs(
                &costs,
                &BTreeSet::from([0, 2]),
                0,
                test_size + bytes(&replacement).unwrap() + 1,
            );
            let full_size = bytes(&guard(left.clone(), &candidate).unwrap()).unwrap();
            assert_ne!(invalid_reused_size, full_size);
        }
    }

    #[test]
    fn guarded_reuse_requires_the_original_patch_to_reach_the_requested_target() {
        let left = json!({
            "a": {"a_long_property_name": 0, "b_long_property_name": 0},
            "omitted": 0,
            "keep": "unchanged 🦀".repeat(1000),
        });
        let original = patch(json!([
            {"op":"replace","path":"/a/a_long_property_name","value":11111},
            {"op":"replace","path":"/a/b_long_property_name","value":22222},
        ]));
        let mut right = apply_json_patch(&left, &original).unwrap();
        right["omitted"] = json!(1);
        assert!(independent_replacement(&left, &original, "/a"));
        assert!(independent_guard_reads(&original, "/a"));
        for tests in [false, true] {
            // A projected/filtered original patch is valid but cannot prove
            // this candidate reaches the complete requested target.
            assert_eq!(
                rationalize(&left, &right, original.clone(), tests).unwrap(),
                original
            );
        }

        let invalid = patch(json!([
            {"op":"replace","path":"/a/missing","value":1},
        ]));
        let expected_error = guarded_bytes(&left, &invalid, true, &[]).err().unwrap();
        assert_eq!(
            rationalize(&left, &right, invalid, true).unwrap_err(),
            expected_error
        );
    }

    #[test]
    fn rationalization_preserves_interleaved_copy_reads_of_a_subtree_and_its_ancestor() {
        let left = json!({
            "a/b~": {"a_long_property_name": 0, "b_long_property_name": 0},
            "copied": null,
            "keep": "unchanged 🦀 ".repeat(1000),
        });
        for source in ["/a~1b~0", ""] {
            let original = patch(json!([
                {"op": "replace", "path": "/a~1b~0/a_long_property_name", "value": 1},
                {"op": "copy", "from": source, "path": "/copied"},
                {"op": "replace", "path": "/a~1b~0/b_long_property_name", "value": 2},
            ]));
            let right = apply_json_patch(&left, &original).unwrap();
            // A parent replacement would expose the final second field to the
            // interleaved copy, which must instead preserve its earlier value.
            let copied = if source.is_empty() {
                &right["copied"]["a/b~"]
            } else {
                &right["copied"]
            };
            assert_eq!(copied["b_long_property_name"], json!(0));
            for tests in [false, true] {
                let optimized = rationalize(&left, &right, original.clone(), tests).unwrap();
                assert_eq!(optimized, original);
                assert_eq!(apply_json_patch(&left, &optimized).unwrap(), right);
            }
        }
    }

    #[test]
    fn rationalization_preserves_interleaved_array_removal_and_move_indices() {
        let left = json!([
            {"tag": "shifted"},
            {"a_long_property_name": 0, "b_long_property_name": 0},
            {"a_long_property_name": 9, "b_long_property_name": 9},
            {"keep": "unchanged 🦀 ".repeat(1000)},
        ]);
        for structural in [
            json!({"op": "remove", "path": "/0"}),
            json!({"op": "move", "from": "/0", "path": "/2"}),
        ] {
            let original = patch(json!([
                {"op": "replace", "path": "/1/a_long_property_name", "value": 1},
                structural,
                {"op": "replace", "path": "/1/b_long_property_name", "value": 2},
            ]));
            let right = apply_json_patch(&left, &original).unwrap();
            // The two operations at /1 act on different original elements.
            // Combining them must not move the replacement across the shift.
            assert_eq!(right[0]["a_long_property_name"], json!(1));
            assert_eq!(right[1]["b_long_property_name"], json!(2));
            for tests in [false, true] {
                let optimized = rationalize(&left, &right, original.clone(), tests).unwrap();
                assert_eq!(optimized, original);
                assert_eq!(apply_json_patch(&left, &optimized).unwrap(), right);
            }
        }
    }

    #[test]
    fn bounded_cost_handles_escaped_utf8_and_exact_budget_boundaries() {
        let value = json!({"a~/\"": ["line\n🦀\\", 1, null]});
        for op in ["replace", "test"] {
            let candidate = ValueOperation {
                op,
                path: "/a~1b~0/\"🦀",
                value: &value,
            };
            let actual = serde_json::to_vec(&candidate).unwrap().len();
            assert_eq!(bytes_below(&candidate, actual + 1).unwrap(), Some(actual));
            assert_eq!(bytes_below(&candidate, actual).unwrap(), None);
            assert_eq!(bytes_below(&candidate, actual - 1).unwrap(), None);
            assert_eq!(bytes_below(&candidate, 0).unwrap(), None);
        }
    }

    #[test]
    fn bounded_cost_stops_traversal_and_preserves_real_serializer_errors() {
        use serde::{Serializer, ser::SerializeSeq};
        use std::cell::Cell;

        struct Counted<'a>(&'a Cell<usize>);
        impl Serialize for Counted<'_> {
            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                let mut sequence = serializer.serialize_seq(Some(10_000))?;
                for _ in 0..10_000 {
                    self.0.set(self.0.get() + 1);
                    sequence.serialize_element("unused payload")?;
                }
                sequence.end()
            }
        }
        let visits = Cell::new(0);
        assert_eq!(bytes_below(&Counted(&visits), 64).unwrap(), None);
        assert!(visits.get() < 10);

        struct Fails;
        impl Serialize for Fails {
            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                let mut sequence = serializer.serialize_seq(None)?;
                sequence.serialize_element("written before failure")?;
                Err(serde::ser::Error::custom(
                    "deliberate serialization failure",
                ))
            }
        }
        let expected = bytes(&Fails).unwrap_err();
        assert_eq!(bytes_below(&Fails, usize::MAX).unwrap_err(), expected);
    }

    #[test]
    fn single_replace_in_an_array_cannot_become_an_inserting_copy() {
        let text = "reusable payload".repeat(100);
        let left = json!([text, "old"]);
        let right = json!([text, text]);
        let standard = patch(json!([{"op":"replace","path":"/1","value":text}]));
        assert_eq!(
            factorize(&left, &right, standard.clone()).unwrap(),
            standard
        );
        assert_eq!(apply_json_patch(&left, &standard).unwrap(), right);
    }

    #[test]
    fn guard_costs_match_serialized_guards_for_empty_root_and_two_guard_operations() {
        let cases = [
            (Value::Null, Patch::default()),
            (
                json!(0),
                patch(json!([{"op":"replace","path":"","value":true}])),
            ),
            (
                json!(["first", "second"]),
                patch(json!([
                    {"op":"copy","from":"/0","path":"/1"}
                ])),
            ),
        ];
        for (left, patch) in cases {
            let actual = guard(left.clone(), &patch).unwrap();
            let expected = bytes(&actual).unwrap();
            let (size, costs) = guarded_bytes(&left, &patch, true, &[]).unwrap();
            assert_eq!(size, expected);
            assert_eq!(costs.len(), patch.0.len() + 1);
            assert_eq!(costs[0].bytes, 2);
            assert_eq!(costs[0].operations, 0);
            assert_eq!(costs.last().unwrap().operations, actual.0.len());
            for end in 0..=patch.0.len() {
                assert_eq!(
                    guarded_bytes(&left, &patch, true, &costs[..=end])
                        .unwrap()
                        .0,
                    expected
                );
            }
            let (size, costs) = guarded_bytes(&left, &patch, false, &[]).unwrap();
            assert_eq!(size, bytes(&patch).unwrap());
            assert!(costs.is_empty());
        }
    }

    #[test]
    fn guard_cost_prefixes_replay_dependent_copy_and_array_suffixes() {
        let left = json!({"a":[1,2,3],"source":{"x":0,"y":"old"},"dest":[]});
        let body = "new 中文🦀 \" body".repeat(20);
        let original = patch(json!([
            {"op":"move","from":"/a/2","path":"/a/0"},
            {"op":"copy","from":"/a/0","path":"/dest/0"},
            {"op":"replace","path":"/source/x","value":1},
            {"op":"copy","from":"/source/y","path":"/dest/1"},
            {"op":"replace","path":"/source/y","value":body},
            {"op":"remove","path":"/a/1"},
            {"op":"move","from":"/a/1","path":"/dest/2"}
        ]));
        let (_, costs) = guarded_bytes(&left, &original, true, &[]).unwrap();
        let candidate = patch(json!([
            {"op":"move","from":"/a/2","path":"/a/0"},
            {"op":"copy","from":"/a/0","path":"/dest/0"},
            {"op":"replace","path":"/source","value":{"x":1,"y":body}},
            {"op":"copy","from":"/source/y","path":"/dest/1"},
            {"op":"remove","path":"/a/1"},
            {"op":"move","from":"/a/1","path":"/dest/2"}
        ]));
        let actual = guard(left.clone(), &candidate).unwrap();
        let (size, candidate_costs) = guarded_bytes(&left, &candidate, true, &costs[..=2]).unwrap();
        assert_eq!(size, bytes(&actual).unwrap());
        assert_eq!(candidate_costs.last().unwrap().operations, actual.0.len());
        assert!(
            actual
                .0
                .iter()
                .any(|op| matches!(op, PatchOperation::Test(op)
            if op.path.as_str() == "/source/y" && op.value == json!(body)))
        );
        // The copy now reads the replacement's new y, not the original y.
        // Correct pricing does not replace the complete candidate validation.
        assert_ne!(
            apply_json_patch(&left, &original).unwrap(),
            apply_json_patch(&left, &candidate).unwrap()
        );

        // After dropping an earlier operation, the next candidate's first
        // changed index uses the new patch's costs, not the original indices.
        let next = patch(json!([
            {"op":"move","from":"/a/2","path":"/a/0"},
            {"op":"copy","from":"/a/0","path":"/dest/0"},
            {"op":"replace","path":"/source","value":{"x":1,"y":body}},
            {"op":"copy","from":"/source/y","path":"/dest/1"},
            {"op":"replace","path":"/a","value":[3]},
            {"op":"add","path":"/dest/2","value":2}
        ]));
        let actual = guard(left.clone(), &next).unwrap();
        let (size, costs) = guarded_bytes(&left, &next, true, &candidate_costs[..=4]).unwrap();
        assert_eq!(size, bytes(&actual).unwrap());
        assert_eq!(costs.last().unwrap().operations, actual.0.len());
        assert_eq!(
            apply_json_patch(&left, &next).unwrap(),
            apply_json_patch(&left, &candidate).unwrap()
        );
    }
}