azure_data_cosmos_driver 0.6.0

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

//! Pipeline planner for Cosmos DB operations.
//!
//! The planner validates an operation's target against its resource type and
//! constructs the appropriate dataflow [`Pipeline`].
//!
//! For cross-partition queries, [`build_sequential_drain`] consumes a backend
//! [`QueryPlan`](super::query_plan::QueryPlan) and resolves the query's EPK
//! ranges against the current topology to produce a fan-out pipeline.

use std::sync::Arc;

use crate::{
    driver::dataflow::query_plan::DistinctType,
    models::{
        effective_partition_key::{normalized_epk_len, EffectivePartitionKey},
        CosmosOperation, FeedRange,
    },
};

use super::{
    intersect_feed_ranges,
    query_plan::{QueryInfo, QueryPlan},
    DrainedLeaf, PartitionRoutingRefresh, Pipeline, PipelineNode, PipelineNodeState, RangedToken,
    Request, RequestTarget, ResolvedRange, SequentialDrain, TopologyProvider, UnorderedMerge,
};

/// Builds a single-node [`Pipeline`] for a trivial operation.
///
/// Trivial operations are those that can be satisfied by a single request to
/// one partition (point reads, single-partition queries, metadata operations).
/// Use [`CosmosOperation::is_trivial`] to check eligibility before calling.
///
/// `operation` is shared with the resulting [`Request`] node via `Arc`; the
/// caller passes ownership in (cheap because the underlying allocation is
/// shared with any other nodes that need the same operation).
///
/// `resume` is an optional [`PipelineNodeState`] from a continuation token
/// that augments planning. Only `Request` and `Drained` shapes are accepted
/// for trivial operations; any other shape returns a `DataConversion` error.
///
/// # Panics (debug builds)
///
/// Debug-asserts that the operation is indeed trivial. In release builds,
/// returns an error if a non-trivial operation (e.g. a cross-partition query)
/// is passed.
pub(crate) fn build_trivial_pipeline(
    operation: Arc<CosmosOperation>,
    resume: Option<PipelineNodeState>,
) -> crate::error::Result<Pipeline> {
    debug_assert!(
        operation.is_trivial(),
        "build_trivial_pipeline called with non-trivial operation: {:?} targeting {:?}",
        operation.operation_type(),
        operation.target(),
    );

    let target = operation.target();

    let initial_continuation = match resume {
        None => None,
        Some(PipelineNodeState::Request {
            server_continuation,
        }) => server_continuation,
        Some(PipelineNodeState::Drained) => {
            return Ok(Pipeline::new(Box::new(DrainedLeaf)));
        }
        Some(other) => {
            return Err(crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_SHAPE_MISMATCH)
                .with_message(format!(
                    "continuation token shape {} does not match a trivial operation",
                    snapshot_kind(&other)
                ))
                .build());
        }
    };

    // We should only have been called when is_trivial() is true, which guarantees that the target is either None (non-partitioned)
    // or it holds a specific, complete, logical partition key.

    let request_target = match target {
        None => RequestTarget::NonPartitioned,
        Some(f) => {
            if let Some(pk) = f.partition_key() {
                RequestTarget::LogicalPartitionKey(pk.clone())
            } else {
                return Err(crate::error::CosmosError::builder()
                    .with_status(
                        crate::error::CosmosStatus::CLIENT_FEED_RANGE_REQUIRES_FANOUT_PIPELINE,
                    )
                    .with_message(
                        "FeedRange targeting requires a fan-out pipeline; \
                 use plan_operation for cross-partition queries",
                    )
                    .build());
            }
        }
    };

    let root = Request::new(operation, request_target, initial_continuation);
    Ok(Pipeline::new(Box::new(root)))
}

/// Builds a fan-out [`Pipeline`] from a backend query plan as a sequential drain.
///
/// Produces a [`SequentialDrain`] over one [`Request`] per resolved range.
/// Other cross-partition strategies (streaming `ORDER BY`, hybrid search,
/// read-many, etc.) will live as sibling functions.
///
/// `operation` is the underlying logical operation shared across every
/// resulting [`Request`] node via `Arc::clone`; per-partition differences
/// (e.g. partition-key-range targeting) are layered on at execution time via
/// [`OperationOverrides`](crate::pipeline::OperationOverrides) and the
/// per-node [`RequestTarget`], not by cloning the operation itself.
///
/// This function:
/// 1. Validates that the query plan contains no unsupported features (no
///    top/limit, no ordering, no hybrid search, no aggregates).
/// 2. Converts the plan's `queryRanges` to [`FeedRange`]s and resolves them
///    against the current partition topology.
/// 3. Creates a [`Request`] node per resolved range (per saved child range
///    on resume) and bundles them in a [`SequentialDrain`].
///
/// `resume` is an optional [`PipelineNodeState`] from a continuation token.
/// On resume, the `SequentialDrain { children }` list is the authoritative
/// remaining-work ledger: every still-pending range and its server
/// continuation. The planner intersects each saved range with the current
/// topology and emits one [`Request`] leaf per intersection (carrying the
/// saved server continuation, if any); saved ranges marked `Drained` emit
/// nothing. Topology gaps that fall outside every saved range have already
/// been drained and are not re-queried. If a non-`Drained` saved range can't
/// be fully covered by the current topology, the resume fails with a
/// continuation-token error rather than silently dropping work.
/// `resume` is an optional [`PipelineNodeState`] from a continuation token.
/// On resume, the `SequentialDrain { left_most_undrained_epk, active_tokens }`
/// pair describes the remaining work sparsely: anything strictly below the
/// cursor has already been drained; ranges at or above the cursor with no
/// matching `active_tokens` entry are implicitly fresh-start; ranges that
/// overlap an entry inherit that entry's server continuation (cloned across
/// every overlapping topology leaf, which transparently handles partition
/// splits since the saved snapshot was taken). If a non-empty `active_tokens`
/// entry can't be fully covered by the current topology above the cursor,
/// the resume fails with a continuation-token error rather than silently
/// dropping work.
pub(crate) async fn build_sequential_drain(
    query_plan: &QueryPlan,
    topology_provider: &mut dyn TopologyProvider,
    operation: &Arc<CosmosOperation>,
    resume: Option<PipelineNodeState>,
) -> crate::error::Result<Pipeline> {
    validate_query_plan(query_plan)?;

    let saved_snapshot = match resume {
        None => None,
        Some(PipelineNodeState::Drained) => {
            return Ok(Pipeline::new(Box::new(DrainedLeaf)));
        }
        Some(PipelineNodeState::SequentialDrain {
            left_most_undrained_epk,
            active_tokens,
        }) => Some(validate_saved_snapshot(
            left_most_undrained_epk,
            active_tokens,
        )?),
        Some(other) => {
            return Err(crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_SHAPE_MISMATCH)
                .with_message(format!(
                    "continuation token shape {} does not match a cross-partition operation",
                    snapshot_kind(&other)
                ))
                .build());
        }
    };

    let request_nodes = if let Some(saved) = saved_snapshot.as_ref() {
        plan_resume_from_saved_snapshot(query_plan, topology_provider, operation, saved).await?
    } else {
        plan_fresh(query_plan, topology_provider, operation).await?
    };

    // TODO: enforce max fan-out (default 100, configurable). See FEED_OPERATIONS_REQS.md §3.

    if request_nodes.is_empty() {
        // Resumed past every range that still has work: the pipeline is
        // fully drained. Otherwise the plan / topology yielded nothing to
        // query — that's a service contract violation.
        if saved_snapshot.is_some() {
            return Ok(Pipeline::new(Box::new(DrainedLeaf)));
        }
        return Err(crate::error::CosmosError::builder()
            .with_status(crate::error::CosmosStatus::CLIENT_QUERY_PLAN_PRODUCED_EMPTY_RANGES)
            .with_message("query plan produced no partition ranges to query")
            .build());
    }

    // Even when there's only one request node, we still need to wrap it in
    // a SequentialDrain so the pipeline can react to splits by replacing
    // the single Request with multiple Requests.
    let root = Box::new(SequentialDrain::new(request_nodes));
    Ok(Pipeline::new(root))
}

/// Builds an [`UnorderedMerge`] pipeline for change feed operations.
///
/// Unlike [`build_sequential_drain`], this does not require a query plan.
/// The operation's target [`FeedRange`] is resolved against the current
/// partition topology to produce one [`Request`] leaf per physical
/// partition. All leaves are wrapped in an [`UnorderedMerge`] that polls
/// them round-robin without evicting children on 304.
///
/// `resume` is an optional [`PipelineNodeState`] from a continuation token.
/// On resume, `UnorderedMerge { active_tokens, start_from }` carries per-
/// EPK-range server continuations plus the feed's original start position.
/// Each physical range is rebuilt by sweeping the saved tokens that overlap it
/// left to right: every saved sub-range becomes its own EPK-scoped leaf
/// resuming from that sub-range's continuation, and any slice with no saved
/// token re-applies `start_from`. A split therefore fans one parent token out
/// to its children, while a merge reads each saved sub-range independently
/// without dropping a continuation — matching the per-EPK-range change feed
/// resume used by the other Cosmos SDKs (.NET, Java, Python).
pub(crate) async fn build_unordered_merge(
    feed_range: &FeedRange,
    topology_provider: &mut dyn TopologyProvider,
    operation: &Arc<CosmosOperation>,
    resume: Option<PipelineNodeState>,
) -> crate::error::Result<Pipeline> {
    let (saved_tokens, resume_start) = match resume {
        None => (None, None),
        Some(PipelineNodeState::Drained) => {
            return Ok(Pipeline::new(Box::new(DrainedLeaf)));
        }
        Some(PipelineNodeState::UnorderedMerge {
            active_tokens,
            start_from,
        }) => (
            Some(validate_unordered_merge_tokens(active_tokens)?),
            start_from,
        ),
        Some(other) => {
            return Err(crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_SHAPE_MISMATCH)
                .with_message(format!(
                    "continuation token shape {} does not match a change feed operation",
                    snapshot_kind(&other)
                ))
                .build());
        }
    };

    // The start marker is carried so every checkpoint re-persists it. On a
    // fresh start it comes from the operation; on resume the token's persisted
    // marker wins, because the caller only hands back the token and does not
    // repeat the original start position.
    let is_resume = saved_tokens.is_some();
    let start_marker = if is_resume {
        resume_start
    } else {
        operation.change_feed_start().cloned()
    };

    // On resume the operation rebuilt by the SDK no longer carries the original
    // start headers (the caller only passed the continuation token). Re-derive
    // them from the persisted marker so partitions with no saved continuation
    // (never polled before the checkpoint) honor the original start position
    // instead of silently reading from the beginning. Partitions that do have a
    // saved continuation still take precedence via their `If-None-Match` ETag.
    let operation: Arc<CosmosOperation> = match (is_resume, &start_marker) {
        (true, Some(marker)) => {
            Arc::new((**operation).clone().with_change_feed_start(marker.clone()))
        }
        _ => Arc::clone(operation),
    };

    let resolved = topology_provider
        .resolve_ranges(feed_range, PartitionRoutingRefresh::UseCached)
        .await?;

    let mut request_nodes: Vec<Box<dyn PipelineNode>> = Vec::new();

    for resolved_range in resolved {
        let range = intersect_feed_ranges(&resolved_range.range, feed_range)
            .expect("topology provider must return ranges that overlap the feed range");

        // Rebuild this physical range's leaves by sweeping the saved tokens
        // that overlap it, left to right. Each saved sub-range resumes from its
        // own `server_continuation`; any slice with no saved token (a
        // never-polled sub-range, or a brand-new range) emits a fresh-start
        // leaf that re-applies `start_from`.
        //
        // A split appears here as one saved token spanning several physical
        // children: each child is fully covered, so it yields a single leaf
        // carrying the parent continuation (the server accepts a parent token
        // against a post-split child). A merge appears as several saved tokens
        // inside one physical range: each saved sub-range is read independently
        // from its own continuation, EPK-scoped via `x-ms-start/end-epk`, so no
        // saved continuation is dropped. This mirrors the per-EPK-range change
        // feed resume used by the other Cosmos SDKs (.NET, Java, Python).
        let mut cursor = range.min_inclusive().clone();
        let range_max = range.max_exclusive().clone();

        if let Some(tokens) = saved_tokens.as_ref() {
            // `saved_tokens` is sorted ascending and non-overlapping, so the
            // overlapping slices are produced in order with no backtracking.
            for token in tokens {
                let Some(slice) = intersect_feed_ranges(&token.range, &range) else {
                    continue;
                };
                if &cursor < slice.min_inclusive() {
                    let gap = FeedRange::new(cursor.clone(), slice.min_inclusive().clone())?;
                    push_change_feed_leaf(
                        &mut request_nodes,
                        &operation,
                        gap,
                        &resolved_range,
                        None,
                    );
                }
                cursor = slice.max_exclusive().clone();
                push_change_feed_leaf(
                    &mut request_nodes,
                    &operation,
                    slice,
                    &resolved_range,
                    Some(token.server_continuation.clone()),
                );
            }
        }

        if cursor < range_max {
            // Trailing slice with no saved continuation, or the whole range on
            // a fresh (non-resumed) start.
            let tail = FeedRange::new(cursor, range_max)?;
            push_change_feed_leaf(&mut request_nodes, &operation, tail, &resolved_range, None);
        }
    }

    if request_nodes.is_empty() {
        return Err(crate::error::CosmosError::builder()
            .with_status(crate::error::CosmosStatus::CLIENT_QUERY_PLAN_PRODUCED_EMPTY_RANGES)
            .with_message("change feed produced no partition ranges to query")
            .build());
    }

    let root = Box::new(UnorderedMerge::new(request_nodes).with_start_marker(start_marker));
    Ok(Pipeline::new(root))
}

/// Pushes one change feed [`Request`] leaf scoped to `leaf_range` within the
/// given physical partition, optionally resuming from `continuation`.
///
/// When `leaf_range` covers the whole physical partition the EPK scoping
/// collapses away (`x-ms-start/end-epk` are omitted); a narrower slice — as
/// produced after a merge — carries explicit EPK bounds.
fn push_change_feed_leaf(
    request_nodes: &mut Vec<Box<dyn PipelineNode>>,
    operation: &Arc<CosmosOperation>,
    leaf_range: FeedRange,
    resolved_range: &ResolvedRange,
    continuation: Option<String>,
) {
    let target = RequestTarget::effective_partition_key_range(
        leaf_range,
        resolved_range.partition_key_range_id.clone(),
        resolved_range.range.clone(),
    );
    request_nodes.push(Box::new(Request::new(
        Arc::clone(operation),
        target,
        continuation,
    )));
}

/// Builds the request leaves for a fresh (non-resumed) cross-partition plan.
async fn plan_fresh(
    query_plan: &QueryPlan,
    topology_provider: &mut dyn TopologyProvider,
    operation: &Arc<CosmosOperation>,
) -> crate::error::Result<Vec<Box<dyn PipelineNode>>> {
    let mut nodes: Vec<Box<dyn PipelineNode>> = Vec::new();
    // Clip each server-supplied query range to the operation scope (e.g.
    // `FeedScope::partition(partial_hpk)`), which bounds the partition-key
    // prefix. The `query_ranges` always cover the full container, so we
    // intersect to keep the fan-out (and per-pkrange wire EPK bounds) scoped.
    //
    // An equality / `IN` predicate yields a point plan range `[X, X]`, which
    // `query_range_to_feed_range` normalizes to the half-open window
    // `[X, successor(X))` so it routes like any other range (#4574 / #4638).
    let scope_range = operation.target();
    // Full EPK width for this container, used to zero-extend a closed range's
    // (or point's) inclusive upper bound to full width before making it
    // exclusive (#4574).
    let normalized_len = operation
        .container()
        .and_then(|c| normalized_epk_len(c.partition_key_definition()));
    for query_range in &query_plan.query_ranges {
        let plan_range = query_range_to_feed_range(query_range, normalized_len)?;
        let feed_range = match scope_range {
            Some(scope) => match intersect_feed_ranges(scope, &plan_range) {
                Some(r) => r,
                None => continue,
            },
            None => plan_range,
        };
        let resolved = topology_provider
            .resolve_ranges(&feed_range, PartitionRoutingRefresh::UseCached)
            .await?;
        for resolved_range in resolved {
            // Clip the resolved partition to the query range (for an equality
            // point this is the narrow `[X, successor(X))` window, emitted as a
            // `start`/`end-epk` pair alongside `partitionkeyrangeid`).
            let range =
                intersect_feed_ranges(&resolved_range.range, &feed_range).ok_or_else(|| {
                    topology_range_not_overlapping_error(&resolved_range.range, &feed_range)
                })?;

            let target = RequestTarget::effective_partition_key_range(
                range,
                resolved_range.partition_key_range_id,
                resolved_range.range,
            );
            nodes.push(Box::new(Request::new(Arc::clone(operation), target, None)));
        }
    }
    Ok(nodes)
}

/// Builds the request leaves for a resumed cross-partition plan, using the
/// sparse saved snapshot as the authoritative remaining-work ledger.
///
/// Iterates the current topology above the cursor. For each leaf, walks
/// through `active_tokens` overlapping that leaf and emits one [`Request`]
/// per intersection carrying the saved token; gaps between (or around)
/// overlapping tokens within the leaf emit fresh-start [`Request`]s. Each
/// `active_tokens` entry's coverage is tracked so any entry that can't be
/// fully covered by the current topology above the cursor is reported as a
/// continuation-token error.
///
/// # Cosmos server continuation semantics
///
/// When an `active_tokens` entry's range straddles multiple post-split
/// resolved leaves, this function forwards the *same* server continuation
/// token to every intersecting sub-leaf. This relies on the Cosmos backend's
/// documented behavior that a continuation token issued for a parent
/// partition remains valid against each of that partition's post-split
/// children — the server uses the EPK range carried alongside the request
/// to scope which child the token applies to.
async fn plan_resume_from_saved_snapshot(
    query_plan: &QueryPlan,
    topology_provider: &mut dyn TopologyProvider,
    operation: &Arc<CosmosOperation>,
    saved: &SavedSnapshot,
) -> crate::error::Result<Vec<Box<dyn PipelineNode>>> {
    let mut nodes: Vec<Box<dyn PipelineNode>> = Vec::new();
    let mut coverage: Vec<Vec<FeedRange>> = vec![Vec::new(); saved.active_tokens.len()];
    // See `plan_fresh` for the scope-clip rationale. Equality / `IN` points are
    // normalized to `[X, successor(X))` windows by `query_range_to_feed_range`,
    // so they resume through the same half-open path as any other range.
    let scope_range = operation.target();
    // Full EPK width for this container (see `plan_fresh`).
    let normalized_len = operation
        .container()
        .and_then(|c| normalized_epk_len(c.partition_key_definition()));

    for query_range in &query_plan.query_ranges {
        let plan_range = query_range_to_feed_range(query_range, normalized_len)?;
        let feed_range = match scope_range {
            Some(scope) => match intersect_feed_ranges(scope, &plan_range) {
                Some(r) => r,
                None => continue,
            },
            None => plan_range,
        };
        let resolved = topology_provider
            .resolve_ranges(&feed_range, PartitionRoutingRefresh::UseCached)
            .await?;

        for resolved_range in resolved {
            // Clip the resolved partition to the query range.
            let leaf_scope =
                intersect_feed_ranges(&resolved_range.range, &feed_range).ok_or_else(|| {
                    topology_range_not_overlapping_error(&resolved_range.range, &feed_range)
                })?;

            // Clip to "at or above cursor". Drop leaves entirely below.
            if leaf_scope.max_exclusive() <= &saved.cursor {
                continue;
            }
            let effective_min = if leaf_scope.min_inclusive() < &saved.cursor {
                saved.cursor.clone()
            } else {
                leaf_scope.min_inclusive().clone()
            };
            let effective_leaf = FeedRange::new(effective_min, leaf_scope.max_exclusive().clone())?;

            // Walk active_tokens left-to-right against this leaf, emitting
            // a continued sub-leaf per intersection plus fresh-start
            // sub-leaves for any gaps.
            let mut cursor_within_leaf = effective_leaf.min_inclusive().clone();
            for (idx, entry) in saved.active_tokens.iter().enumerate() {
                if entry.range.max_exclusive() <= &cursor_within_leaf {
                    continue;
                }
                if entry.range.min_inclusive() >= effective_leaf.max_exclusive() {
                    break;
                }

                let overlap_min = if entry.range.min_inclusive() > &cursor_within_leaf {
                    entry.range.min_inclusive().clone()
                } else {
                    cursor_within_leaf.clone()
                };
                let overlap_max = if entry.range.max_exclusive() < effective_leaf.max_exclusive() {
                    entry.range.max_exclusive().clone()
                } else {
                    effective_leaf.max_exclusive().clone()
                };

                if overlap_min > cursor_within_leaf {
                    // Gap before this token entry — fresh-start sub-leaf.
                    let gap = FeedRange::new(cursor_within_leaf.clone(), overlap_min.clone())?;
                    let target = RequestTarget::effective_partition_key_range(
                        gap,
                        resolved_range.partition_key_range_id.clone(),
                        resolved_range.range.clone(),
                    );
                    nodes.push(Box::new(Request::new(Arc::clone(operation), target, None)));
                }

                let intersection = FeedRange::new(overlap_min, overlap_max.clone())?;
                coverage[idx].push(intersection.clone());
                let target = RequestTarget::effective_partition_key_range(
                    intersection,
                    resolved_range.partition_key_range_id.clone(),
                    resolved_range.range.clone(),
                );
                nodes.push(Box::new(Request::new(
                    Arc::clone(operation),
                    target,
                    Some(entry.server_continuation.clone()),
                )));

                cursor_within_leaf = overlap_max;
            }

            if cursor_within_leaf < *effective_leaf.max_exclusive() {
                // Trailing gap after the last overlapping token entry.
                let gap =
                    FeedRange::new(cursor_within_leaf, effective_leaf.max_exclusive().clone())?;
                let target = RequestTarget::effective_partition_key_range(
                    gap,
                    resolved_range.partition_key_range_id.clone(),
                    resolved_range.range.clone(),
                );
                nodes.push(Box::new(Request::new(Arc::clone(operation), target, None)));
            }
        }
    }

    // Verify every active token's range was fully covered by the current
    // topology above the cursor. If not, the planner cannot honor the
    // saved continuation without risking duplicate emission or data loss —
    // fail loudly.
    for (idx, entry) in saved.active_tokens.iter().enumerate() {
        if !range_fully_covered(&entry.range, &coverage[idx]) {
            const MAX_COVERAGE_PIECES_RENDERED: usize = 8;
            let coverage_summary = if coverage[idx].is_empty() {
                "(no overlapping topology ranges)".to_string()
            } else {
                let mut sorted = coverage[idx].clone();
                sorted.sort_by(|a, b| a.min_inclusive().cmp(b.min_inclusive()));
                let total = sorted.len();
                let rendered: Vec<String> = sorted
                    .iter()
                    .take(MAX_COVERAGE_PIECES_RENDERED)
                    .map(|r| {
                        format!(
                            "[{}, {})",
                            r.min_inclusive().to_hex(),
                            r.max_exclusive().to_hex()
                        )
                    })
                    .collect();
                if total > MAX_COVERAGE_PIECES_RENDERED {
                    format!("{} + ... ({} total ranges)", rendered.join(" + "), total)
                } else {
                    rendered.join(" + ")
                }
            };
            return Err(crate::error::CosmosError::builder()
                .with_status(
                    crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_SAVED_RANGE_UNHONORED,
                )
                .with_message(format!(
                    "continuation token active range [{}, {}) could not be fully covered \
                     by the current topology above the cursor (covered: {}); the query \
                     cannot be safely resumed",
                    entry.range.min_inclusive().to_hex(),
                    entry.range.max_exclusive().to_hex(),
                    coverage_summary,
                ))
                .build());
        }
    }

    Ok(nodes)
}

/// Converts a query-plan EPK range to a [`FeedRange`].
///
/// The gateway returns a *closed* range `[a, b]`
/// (`isMinInclusive == isMaxInclusive == true`) when a query filters on the
/// partition key with an equality / `IN` predicate (issue #4574) — a *point*
/// `[X, X]` per value — and a normal half-open `[min, max)` range otherwise.
///
/// Any closed upper bound is made exclusive by advancing it to its successor,
/// so a point `[X, X]` becomes the non-empty half-open range `[X, successor(X))`
/// and a closed range `[a, b]` becomes `[a, successor(b))`. Both then flow
/// through the normal `[min, max)` routing (scope intersection, topology
/// resolution, per-partition EPK-window emit) instead of collapsing to the empty
/// set (which panicked, #4574) or being special-cased to whole-partition
/// routing. Emitting the narrow `[X, successor(X))` window as a `start`/`end-epk`
/// pair alongside `partitionkeyrangeid` (with
/// `x-ms-read-key-type: EffectivePartitionKeyRange`, #4729) is honored by the
/// gateway and matches the normalize-to-EPK-range model the .NET SDK uses.
///
/// `normalized_len` is the container's full EPK width in bytes (see
/// [`normalized_epk_len`](crate::models::effective_partition_key::normalized_epk_len)).
/// It is applied **only to a point** (`min == max`, an equality / `IN` value,
/// which is always a *full* partition key): the trailing-zero-trimmed value is
/// zero-extended to full width before the increment, so the successor is the
/// exact one the backend expects (matching .NET's full-width HPK normalization).
/// When the width is unknown (V1), a point falls back to the width-preserving
/// [`successor`](EffectivePartitionKey::successor) so it still becomes a
/// non-empty window instead of collapsing to the empty set (the #4574 panic).
///
/// Every *other* range — including a closed **non-point** range such as an HPK
/// **prefix** upper bound — is passed through unchanged, exactly as upstream
/// does. Those bounds are produced at partition-boundary granularity by the
/// gateway/topology layer; advancing them with a successor over-extends the band
/// and routes incorrectly (it drops owning physical partitions — the
/// `hpk_tenant_prefix_where_full_scope` regression).
fn query_range_to_feed_range(
    query_range: &super::query_plan::QueryRange,
    normalized_len: Option<usize>,
) -> crate::error::Result<FeedRange> {
    let min = EffectivePartitionKey::from(query_range.min.as_str());
    let max = EffectivePartitionKey::from(query_range.max.as_str());
    // Only a closed *point* `[X, X]` (equality / `IN`, min == max) is
    // transformed — into the non-empty half-open window `[X, successor(X))`.
    // Every other range is left as-is (upstream behavior).
    let max = if query_range.is_max_inclusive && min == max {
        match normalized_len {
            // Full key: normalize to full EPK width before incrementing
            // (Option B, #4574 / #4638), matching .NET's HPK normalization.
            Some(len) => max.normalized_successor(len),
            // Unknown width (V1): width-preserving successor keeps `[X, X]` from
            // collapsing to the empty set.
            None => max.successor(),
        }
    } else {
        max
    };
    FeedRange::new(min, max)
}

/// Returns true if the union of `pieces` covers `range` end-to-end.
///
/// Assumes pieces are subsets of `range`. The check sorts pieces by
/// `min_inclusive` and walks left-to-right, requiring the running cursor to
/// reach `range.max_exclusive` with no gaps.
fn range_fully_covered(range: &FeedRange, pieces: &[FeedRange]) -> bool {
    if pieces.is_empty() {
        return false;
    }
    let mut sorted: Vec<&FeedRange> = pieces.iter().collect();
    sorted.sort_by(|a, b| a.min_inclusive().cmp(b.min_inclusive()));
    let mut cursor = range.min_inclusive().clone();
    for piece in sorted {
        debug_assert!(
            piece.min_inclusive() >= range.min_inclusive()
                && piece.max_exclusive() <= range.max_exclusive(),
            "range_fully_covered piece [{}, {}) is not a subset of range [{}, {})",
            piece.min_inclusive().to_hex(),
            piece.max_exclusive().to_hex(),
            range.min_inclusive().to_hex(),
            range.max_exclusive().to_hex(),
        );
        if piece.min_inclusive() > &cursor {
            return false;
        }
        if piece.max_exclusive() > &cursor {
            cursor = piece.max_exclusive().clone();
        }
    }
    &cursor >= range.max_exclusive()
}

/// Validated saved snapshot: cursor + per-range active tokens parsed into
/// strongly-typed [`EffectivePartitionKey`] / [`FeedRange`].
#[derive(Debug)]
struct SavedSnapshot {
    cursor: EffectivePartitionKey,
    active_tokens: Vec<SavedActiveToken>,
}

#[derive(Debug)]
struct SavedActiveToken {
    range: FeedRange,
    server_continuation: String,
}

/// Validates a sparse saved snapshot from a continuation token: each
/// `active_tokens` entry has `min < max` (and is not zero-width), the list
/// is strictly sorted ascending and non-overlapping, and the cursor is at
/// or before the first entry's `min`. Returns the parsed [`SavedSnapshot`]
/// on success or a continuation-token shape error on failure.
fn validate_saved_snapshot(
    left_most_undrained_epk: String,
    active_tokens: Vec<RangedToken>,
) -> crate::error::Result<SavedSnapshot> {
    let cursor = EffectivePartitionKey::from(left_most_undrained_epk);

    let mut parsed: Vec<SavedActiveToken> = Vec::with_capacity(active_tokens.len());
    for entry in active_tokens {
        let min = EffectivePartitionKey::from(entry.min_epk);
        let max = EffectivePartitionKey::from(entry.max_epk);
        if min > max {
            return Err(crate::error::CosmosError::builder()
                .with_status(
                    crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
                )
                .with_message(format!(
                    "continuation token has invalid active_tokens entry (min `{}` > max `{}`)",
                    min.to_hex(),
                    max.to_hex(),
                ))
                .build());
        }
        if min == max {
            // A zero-width entry is structurally well-formed but cannot
            // carry remaining work; reject explicitly so the caller sees
            // a diagnostic message that points at the entry itself.
            return Err(crate::error::CosmosError::builder()
                .with_status(
                    crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
                )
                .with_message(format!(
                    "continuation token has zero-width active_tokens entry (min == max == `{}`); \
                     zero-width entries cannot carry remaining work",
                    min.to_hex(),
                ))
                .build());
        }
        let range = FeedRange::new(min, max)?;
        if let Some(prev) = parsed.last() {
            if range.min_inclusive() < prev.range.max_exclusive() {
                return Err(crate::error::CosmosError::builder()
                    .with_status(
                        crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
                    )
                    .with_message(format!(
                        "continuation token active_tokens must be sorted and non-overlapping; \
                         entry [{}, {}) is out of order or overlaps the previous entry [{}, {})",
                        range.min_inclusive().to_hex(),
                        range.max_exclusive().to_hex(),
                        prev.range.min_inclusive().to_hex(),
                        prev.range.max_exclusive().to_hex(),
                    ))
                    .build());
            }
        }
        parsed.push(SavedActiveToken {
            range,
            server_continuation: entry.server_continuation,
        });
    }

    // Cursor cannot leapfrog past a still-active token entry: anything
    // strictly below the cursor is implicitly drained, but `active_tokens`
    // entries are by definition not drained.
    if let Some(first) = parsed.first() {
        if &cursor > first.range.min_inclusive() {
            return Err(crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE)
                .with_message(format!(
                    "continuation token cursor `{}` is past the first active_tokens entry [{}, {}); \
                     cursor must be at or before every active range",
                    cursor.to_hex(),
                    first.range.min_inclusive().to_hex(),
                    first.range.max_exclusive().to_hex(),
                ))
                .build());
        }
    }

    Ok(SavedSnapshot {
        cursor,
        active_tokens: parsed,
    })
}

fn snapshot_kind(state: &PipelineNodeState) -> &'static str {
    match state {
        PipelineNodeState::Drained => "Drained",
        PipelineNodeState::Request { .. } => "Request",
        PipelineNodeState::SequentialDrain { .. } => "SequentialDrain",
        PipelineNodeState::UnorderedMerge { .. } => "UnorderedMerge",
    }
}

/// Validates the `active_tokens` from an `UnorderedMerge` continuation token.
///
/// Each entry must have `min < max` and be non-zero-width. The list must be
/// sorted ascending by `min_epk` and non-overlapping.
fn validate_unordered_merge_tokens(
    active_tokens: Vec<RangedToken>,
) -> crate::error::Result<Vec<SavedActiveToken>> {
    let mut parsed: Vec<SavedActiveToken> = Vec::with_capacity(active_tokens.len());
    for entry in active_tokens {
        let min = EffectivePartitionKey::from(entry.min_epk);
        let max = EffectivePartitionKey::from(entry.max_epk);
        if min >= max {
            return Err(crate::error::CosmosError::builder()
                .with_status(
                    crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
                )
                .with_message(format!(
                    "continuation token has invalid active_tokens entry \
                     (min `{}` >= max `{}`)",
                    min.to_hex(),
                    max.to_hex(),
                ))
                .build());
        }
        let range = FeedRange::new(min, max)?;
        if let Some(prev) = parsed.last() {
            if range.min_inclusive() < prev.range.max_exclusive() {
                return Err(crate::error::CosmosError::builder()
                    .with_status(
                        crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
                    )
                    .with_message(format!(
                        "continuation token active_tokens must be sorted and non-overlapping; \
                         entry [{}, {}) overlaps [{}, {})",
                        range.min_inclusive().to_hex(),
                        range.max_exclusive().to_hex(),
                        prev.range.min_inclusive().to_hex(),
                        prev.range.max_exclusive().to_hex(),
                    ))
                    .build());
            }
        }
        parsed.push(SavedActiveToken {
            range,
            server_continuation: entry.server_continuation,
        });
    }
    Ok(parsed)
}

/// Validates that the query plan does not require features we don't yet support.
fn validate_query_plan(plan: &QueryPlan) -> crate::error::Result<()> {
    if plan.hybrid_search_query_info.is_some() {
        return Err(unsupported_feature("hybrid search queries"));
    }

    if let Some(info) = &plan.query_info {
        validate_query_info(info)?;
    }

    Ok(())
}

fn validate_query_info(info: &QueryInfo) -> crate::error::Result<()> {
    if info.top.is_some() {
        return Err(unsupported_feature("TOP clause in cross-partition queries"));
    }
    if info.limit.is_some() {
        return Err(unsupported_feature(
            "LIMIT clause in cross-partition queries",
        ));
    }
    if !info.order_by.is_empty() {
        return Err(unsupported_feature("ORDER BY in cross-partition queries"));
    }
    if !info.aggregates.is_empty() {
        return Err(unsupported_feature("aggregates in cross-partition queries"));
    }
    if !info.group_by_expressions.is_empty() {
        return Err(unsupported_feature("GROUP BY in cross-partition queries"));
    }
    if info.distinct_type != DistinctType::None {
        return Err(unsupported_feature("DISTINCT in cross-partition queries"));
    }
    Ok(())
}

fn unsupported_feature(feature: &str) -> crate::error::CosmosError {
    crate::error::CosmosError::builder()
        .with_status(crate::error::CosmosStatus::CLIENT_UNSUPPORTED_QUERY_FEATURE)
        .with_message(format!("unsupported query feature: {feature}"))
        .build()
}

/// Builds the error returned when a topology range resolved for a query-plan
/// EPK range does not actually overlap that range.
///
/// This is a contract violation: [`TopologyProvider::resolve_ranges`] is
/// expected to return only ranges that overlap the requested feed range. It
/// should be unreachable in practice, but returning a structured error rather
/// than panicking keeps a plan that cannot be served from taking down the
/// worker thread (and deadlocking the caller) — see issue #4574.
fn topology_range_not_overlapping_error(
    resolved: &FeedRange,
    query: &FeedRange,
) -> crate::error::CosmosError {
    crate::error::CosmosError::builder()
        .with_status(crate::error::CosmosStatus::CLIENT_QUERY_PLAN_RANGE_NOT_COVERED_BY_TOPOLOGY)
        .with_message(format!(
            "resolved topology range {} does not overlap query plan EPK {}",
            render_feed_range_for_error(resolved),
            render_feed_range_for_error(query),
        ))
        .build()
}

/// Renders a feed range for diagnostics as a half-open `[min, max)` range.
fn render_feed_range_for_error(range: &FeedRange) -> String {
    format!(
        "range [{}, {})",
        range.min_inclusive().to_hex(),
        range.max_exclusive().to_hex(),
    )
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use super::*;
    use crate::{
        driver::dataflow::{mocks::*, query_plan::QueryRange, RangedToken, ResolvedRange},
        models::{
            effective_partition_key::EffectivePartitionKey, AccountReference, ContainerProperties,
            ContainerReference, DatabaseReference, ItemReference, OperationType, PartitionKey,
            PartitionKeyDefinition, ResourceType, SystemProperties,
        },
    };

    fn test_account() -> AccountReference {
        AccountReference::with_master_key(
            url::Url::parse("https://test.documents.azure.com:443/").unwrap(),
            "dGVzdA==",
        )
    }

    fn test_database() -> DatabaseReference {
        DatabaseReference::from_name(test_account(), "db".to_owned())
    }

    fn test_partition_key_definition() -> PartitionKeyDefinition {
        // Explicit `version: 2`: this fixture models a modern V2 hash container,
        // which is the shape the EPK-window normalization path targets. An
        // absent `version` now deserializes to legacy V1 (see
        // `models::default_pk_version`), which would route these point queries
        // through the width-preserving `successor()` path instead of the
        // full-width `normalized_successor(16)` EPK window.
        serde_json::from_str(r#"{"paths":["/pk"],"version":2}"#).unwrap()
    }

    fn test_container_props() -> ContainerProperties {
        ContainerProperties {
            id: Cow::Owned("coll".into()),
            partition_key: test_partition_key_definition(),
            system_properties: SystemProperties::default(),
        }
    }

    fn test_container() -> ContainerReference {
        ContainerReference::new(
            test_account(),
            "db",
            "db_rid",
            "coll",
            "coll_rid",
            &test_container_props(),
        )
    }

    fn cross_partition_query_operation() -> CosmosOperation {
        CosmosOperation::query_items(test_container(), Some(FeedRange::full()))
            .with_body(br#"{"query":"SELECT * FROM c"}"#.to_vec())
    }

    // --- build_trivial_pipeline tests ---

    #[test]
    fn plans_non_partitioned_pipeline_for_database_read() {
        let op = CosmosOperation::read_database(test_database());
        let pipeline = build_trivial_pipeline(Arc::new(op), None).unwrap();

        let request = pipeline.root().downcast_ref::<Request>().unwrap();
        assert_eq!(*request.target(), RequestTarget::NonPartitioned);
        assert_eq!(request.operation().operation_type(), OperationType::Read);
        assert_eq!(request.operation().resource_type(), ResourceType::Database);
    }

    #[test]
    fn plans_logical_partition_pipeline_for_item_read() {
        let pk = PartitionKey::from("pk-value");
        let item = ItemReference::from_name(&test_container(), pk.clone(), "doc1");
        let op = CosmosOperation::read_item(item);
        let pipeline = build_trivial_pipeline(Arc::new(op), None).unwrap();

        let request = pipeline.root().downcast_ref::<Request>().unwrap();
        assert_eq!(
            *request.target(),
            RequestTarget::LogicalPartitionKey(pk.clone())
        );
        assert_eq!(request.operation().operation_type(), OperationType::Read);
        assert_eq!(request.operation().resource_type(), ResourceType::Document);
    }

    #[test]
    fn plans_logical_partition_pipeline_for_partition_scoped_query() {
        // Regression for the SDK→driver differentiation (issue #4574 follow-up):
        // a query scoped to a COMPLETE partition key via `FeedScope::Partition`
        // / `cosmos_feed_range_for_partition_key` (a `LogicalPartition` feed
        // range) must still route by the logical partition key — emitting
        // `x-ms-documentdb-partitionkey` — NOT through the planner's EPK-point
        // path. This is the path that already works and must stay distinct from
        // the predicate-derived gateway point.
        let pk = PartitionKey::from("pk-value");
        let feed_range = FeedRange::for_partition(pk.clone(), &test_partition_key_definition());
        let op = CosmosOperation::query_items(test_container(), Some(feed_range))
            .with_body(br#"{"query":"SELECT * FROM c"}"#.to_vec());

        // The operation is trivial (complete PK), so it routes through the
        // single-request trivial pipeline rather than the cross-partition planner.
        assert!(op.is_trivial());
        let pipeline = build_trivial_pipeline(Arc::new(op), None).unwrap();

        let request = pipeline.root().downcast_ref::<Request>().unwrap();
        assert_eq!(*request.target(), RequestTarget::LogicalPartitionKey(pk));
    }

    #[test]
    fn rejects_feed_range_target() {
        let op = CosmosOperation::read_all_items_cross_partition(test_container());

        // In debug builds, this panics via debug_assert; in release builds it returns Err.
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            build_trivial_pipeline(Arc::new(op), None)
        }));

        match result {
            // Panicked in debug mode (expected)
            Err(_) if cfg!(debug_assertions) => {}
            // Panicked in release mode (bad)
            Err(_) => panic!("did not expect panic for FeedRange target"),
            // Returned Err in release mode (also acceptable)
            Ok(Err(err)) => {
                let rendered = err.to_string();
                assert!(
                    rendered.ends_with(
                        "FeedRange targeting requires a fan-out pipeline; \
                         use plan_operation for cross-partition queries"
                    ),
                    "unexpected: {rendered}"
                );
            }
            _ => panic!("expected error or panic for FeedRange target"),
        }
    }

    // --- build_sequential_drain tests ---

    /// Shorthand to build a `QueryRange` from hex-prefix EPK strings.
    fn qr(min: &str, max: &str) -> QueryRange {
        QueryRange {
            min: min.to_string(),
            max: max.to_string(),
            is_min_inclusive: true,
            is_max_inclusive: false,
        }
    }

    /// Shorthand to build a `ResolvedRange` from (min, max, pk_range_id).
    fn rr(min: &str, max: &str, pk_range_id: &str) -> ResolvedRange {
        ResolvedRange {
            partition_key_range_id: pk_range_id.to_string(),
            range: FeedRange::new(
                EffectivePartitionKey::from(min),
                EffectivePartitionKey::from(max),
            )
            .unwrap(),
        }
    }

    /// Builds a query plan with the given query ranges (and no query info).
    fn plan_with_ranges(ranges: Vec<QueryRange>) -> QueryPlan {
        QueryPlan {
            partitioned_query_execution_info_version: 1,
            query_info: None,
            query_ranges: ranges,
            hybrid_search_query_info: None,
        }
    }

    /// Asserts that the pipeline is a `SequentialDrain` containing `Request` nodes
    /// targeting the given EPK ranges (in order).
    type ExpectedDrainRequestWithPartition<'a> = (&'a str, &'a str, &'a str, &'a str, &'a str);
    type ExpectedDrainRequestWithContinuation<'a> =
        (&'a str, &'a str, &'a str, &'a str, &'a str, Option<&'a str>);

    fn assert_drain_requests(pipeline: Pipeline, expected: &[(&str, &str, &str)]) {
        let expected = expected
            .iter()
            .map(|&(min, max, pk_range_id)| (min, max, pk_range_id, min, max))
            .collect::<Vec<_>>();
        assert_drain_requests_with_partitions(pipeline, &expected);
    }

    fn assert_drain_requests_with_partitions(
        pipeline: Pipeline,
        expected: &[ExpectedDrainRequestWithPartition<'_>],
    ) {
        let drain = pipeline
            .into_root()
            .downcast::<SequentialDrain>()
            .expect("expected SequentialDrain root");
        let children = drain.into_children();
        assert_eq!(
            children.len(),
            expected.len(),
            "expected {} request nodes, got {}",
            expected.len(),
            children.len(),
        );
        for (child, &(min, max, pk_range_id, partition_min, partition_max)) in
            children.into_iter().zip(expected)
        {
            let request = child
                .downcast::<Request>()
                .expect("expected Request child node");
            assert_eq!(
                *request.target(),
                RequestTarget::effective_partition_key_range(
                    FeedRange::new(
                        EffectivePartitionKey::from(min),
                        EffectivePartitionKey::from(max),
                    )
                    .unwrap(),
                    pk_range_id.to_string(),
                    FeedRange::new(
                        EffectivePartitionKey::from(partition_min),
                        EffectivePartitionKey::from(partition_max),
                    )
                    .unwrap(),
                ),
                "mismatch for pk range {pk_range_id}"
            );
        }
    }

    fn assert_drain_requests_with_partitions_and_continuation(
        pipeline: Pipeline,
        expected: &[ExpectedDrainRequestWithContinuation<'_>],
    ) {
        let drain = pipeline
            .into_root()
            .downcast::<SequentialDrain>()
            .expect("expected SequentialDrain root");
        let children = drain.into_children();
        assert_eq!(
            children.len(),
            expected.len(),
            "expected {} request nodes, got {}",
            expected.len(),
            children.len(),
        );

        for (child, &(min, max, pk_range_id, partition_min, partition_max, continuation)) in
            children.into_iter().zip(expected)
        {
            let request = child
                .downcast::<Request>()
                .expect("expected Request child node");
            assert_eq!(
                *request.target(),
                RequestTarget::effective_partition_key_range(
                    FeedRange::new(
                        EffectivePartitionKey::from(min),
                        EffectivePartitionKey::from(max),
                    )
                    .unwrap(),
                    pk_range_id.to_string(),
                    FeedRange::new(
                        EffectivePartitionKey::from(partition_min),
                        EffectivePartitionKey::from(partition_max),
                    )
                    .unwrap(),
                ),
                "mismatch for pk range {pk_range_id}"
            );

            let expected_state = PipelineNodeState::Request {
                server_continuation: continuation.map(ToOwned::to_owned),
            };
            assert_eq!(request.snapshot_state().unwrap(), expected_state);
        }
    }

    #[tokio::test]
    async fn builds_single_node_pipeline_for_one_partition() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(pipeline, &[("", "FF", "pkrange-0")]);
    }

    #[tokio::test]
    async fn builds_sequential_drain_for_multiple_partitions() {
        // Query targets full range, topology has two partitions split at "80".
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "80", "pkrange-left"),
            rr("80", "FF", "pkrange-right"),
        ])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(
            pipeline,
            &[("", "80", "pkrange-left"), ("80", "FF", "pkrange-right")],
        );
    }

    #[tokio::test]
    async fn builds_pipeline_for_multiple_query_ranges() {
        // Query plan specifies two disjoint query ranges; each resolves to one partition.
        let plan = plan_with_ranges(vec![qr("", "40"), qr("80", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "40", "pkrange-A")]),
            Ok(vec![rr("80", "FF", "pkrange-C")]),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(
            pipeline,
            &[("", "40", "pkrange-A"), ("80", "FF", "pkrange-C")],
        );
    }

    #[tokio::test]
    async fn query_range_spans_multiple_topology_partitions() {
        // A single query range [00, C0) spans three topology partitions.
        let plan = plan_with_ranges(vec![qr("00", "C0")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("00", "40", "pkrange-1"),
            rr("40", "80", "pkrange-2"),
            rr("80", "C0", "pkrange-3"),
        ])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(
            pipeline,
            &[
                ("00", "40", "pkrange-1"),
                ("40", "80", "pkrange-2"),
                ("80", "C0", "pkrange-3"),
            ],
        );
    }

    #[tokio::test]
    async fn multiple_query_ranges_each_spanning_multiple_partitions() {
        // Two query ranges, each resolving to multiple partitions. The resulting
        // pipeline should have all resolved ranges in order.
        let plan = plan_with_ranges(vec![qr("", "60"), qr("A0", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![
            // First query range [, 60) spans two partitions.
            Ok(vec![
                rr("", "30", "pkrange-alpha"),
                rr("30", "60", "pkrange-beta"),
            ]),
            // Second query range [A0, FF) spans two partitions.
            Ok(vec![
                rr("A0", "D0", "pkrange-gamma"),
                rr("D0", "FF", "pkrange-delta"),
            ]),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(
            pipeline,
            &[
                ("", "30", "pkrange-alpha"),
                ("30", "60", "pkrange-beta"),
                ("A0", "D0", "pkrange-gamma"),
                ("D0", "FF", "pkrange-delta"),
            ],
        );
    }

    #[tokio::test]
    async fn topology_partition_wider_than_query_range() {
        // The topology partition [, FF) is wider than query range [20, 80).
        let plan = plan_with_ranges(vec![qr("20", "80")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-wide")])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests_with_partitions(pipeline, &[("20", "80", "pkrange-wide", "", "FF")]);
    }

    #[tokio::test]
    async fn closed_point_query_range_emits_epk_window() {
        // Regression for issues #4574 / #4638: an equality / `IN` predicate on
        // the partition key makes the gateway return a *closed* point range
        // `[X, X]` (isMinInclusive == isMaxInclusive == true). Option B: the
        // planner normalizes it to the half-open window `[X, successor(X))` and
        // emits it as a `start`/`end-epk` pair scoped to the owning partition
        // (with `x-ms-read-key-type: EffectivePartitionKeyRange`, #4729). The
        // empty-intersection panic is avoided because `min != max`.
        let point = QueryRange {
            min: "30".to_string(),
            max: "30".to_string(),
            is_min_inclusive: true,
            is_max_inclusive: true,
        };
        let plan = plan_with_ranges(vec![point]);
        let op = cross_partition_query_operation();
        // The single physical partition `["", "FF")` owns EPK "30".
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();

        // Narrow `[30, successor(30))` EPK window over the owning partition.
        let s30 = EffectivePartitionKey::from("30")
            .normalized_successor(16)
            .to_hex();
        assert_drain_requests_with_partitions(
            pipeline,
            &[("30", s30.as_str(), "pkrange-0", "", "FF")],
        );
    }

    #[tokio::test]
    async fn in_predicate_colocated_points_emit_one_window_each() {
        // `WHERE c.pk IN (@a, @b)` where both values hash into the same
        // physical partition: the gateway returns two point ranges, both
        // resolving to `pkrange-0`. Option B emits a distinct, disjoint EPK
        // window (`[X, successor(X))`) per value — no de-duplication needed
        // because each window matches only its own value.
        let plan = plan_with_ranges(vec![
            QueryRange {
                min: "30".to_string(),
                max: "30".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
            QueryRange {
                min: "50".to_string(),
                max: "50".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
        ]);
        let op = cross_partition_query_operation();
        // Both points resolve to the same single partition.
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "FF", "pkrange-0")]),
            Ok(vec![rr("", "FF", "pkrange-0")]),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();

        let s30 = EffectivePartitionKey::from("30")
            .normalized_successor(16)
            .to_hex();
        let s50 = EffectivePartitionKey::from("50")
            .normalized_successor(16)
            .to_hex();
        assert_drain_requests_with_partitions(
            pipeline,
            &[
                ("30", s30.as_str(), "pkrange-0", "", "FF"),
                ("50", s50.as_str(), "pkrange-0", "", "FF"),
            ],
        );
    }

    #[tokio::test]
    async fn in_predicate_points_across_partitions_emit_one_window_each() {
        // `WHERE c.pk IN (@a, @b)` where the values live in different
        // partitions: each point normalizes to its own `[X, successor(X))`
        // EPK window scoped to the owning partition.
        let plan = plan_with_ranges(vec![
            QueryRange {
                min: "20".to_string(),
                max: "20".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
            QueryRange {
                min: "C0".to_string(),
                max: "C0".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
        ]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "80", "pkrange-left")]),
            Ok(vec![rr("80", "FF", "pkrange-right")]),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();

        let s20 = EffectivePartitionKey::from("20")
            .normalized_successor(16)
            .to_hex();
        let sc0 = EffectivePartitionKey::from("C0")
            .normalized_successor(16)
            .to_hex();
        assert_drain_requests_with_partitions(
            pipeline,
            &[
                ("20", s20.as_str(), "pkrange-left", "", "80"),
                ("C0", sc0.as_str(), "pkrange-right", "80", "FF"),
            ],
        );
    }

    #[test]
    fn query_range_to_feed_range_normalizes_closed_point_to_window() {
        // A closed point range `[X, X]` (isMaxInclusive=true) is normalized to
        // the half-open window `[X, successor(X))` (Option B, issues #4574 /
        // #4638) rather than kept as an empty point.
        let point = QueryRange {
            min: "30".to_string(),
            max: "30".to_string(),
            is_min_inclusive: true,
            is_max_inclusive: true,
        };
        let fr = query_range_to_feed_range(&point, None).unwrap();
        assert_eq!(fr.min_inclusive().to_hex(), "30");
        // successor("30") = "31" (no width normalization requested).
        assert_eq!(fr.max_exclusive().to_hex(), "31");
    }

    #[test]
    fn query_range_to_feed_range_preserves_half_open() {
        // A half-open range `[A, B)` (isMaxInclusive=false) is a plain range —
        // the common full-container / split case.
        let fr = query_range_to_feed_range(&qr("20", "80"), None).unwrap();
        assert_eq!(fr.min_inclusive().to_hex(), "20");
        assert_eq!(fr.max_exclusive().to_hex(), "80");
    }

    #[test]
    fn query_range_to_feed_range_closed_non_point_range_passes_through_when_len_unknown() {
        // A closed non-point range `[A, B]` (`min != max`) with no known EPK
        // width (V1) is passed through unchanged — only equality / `IN` *points*
        // are transformed.
        let closed = QueryRange {
            min: "20".to_string(),
            max: "3AFF".to_string(),
            is_min_inclusive: true,
            is_max_inclusive: true,
        };
        let fr = query_range_to_feed_range(&closed, None).unwrap();
        assert_eq!(fr.min_inclusive().to_hex(), "20");
        // Non-point range: upper bound is passed through, no successor applied.
        assert_eq!(fr.max_exclusive().to_hex(), "3AFF");
    }

    #[test]
    fn query_range_to_feed_range_closed_non_point_range_passes_through_even_with_known_len() {
        // Regression guard (#4574 / #4638): a closed *non-point* range
        // (`min != max`) is an HPK **prefix** bound, not a full key. It must be
        // passed through unchanged even when the full EPK width is known —
        // advancing it with a successor over-extends the prefix band and drops
        // owning partitions (the in-memory-emulator
        // `hpk_tenant_prefix_where_full_scope` regression: touched {5,6} instead
        // of {4,5,6}).
        let closed = QueryRange {
            min: "20".to_string(),
            max: "3A".to_string(),
            is_min_inclusive: true,
            is_max_inclusive: true,
        };
        let fr = query_range_to_feed_range(&closed, Some(16)).unwrap();
        assert_eq!(fr.min_inclusive().to_hex(), "20");
        // Non-point range: passed through, NOT normalized / incremented.
        assert_eq!(fr.max_exclusive().to_hex(), "3A");
    }

    #[test]
    fn query_range_to_feed_range_normalizes_point_to_full_width() {
        // A closed *point* (`min == max`, an equality / `IN` value — always a
        // full key) on a single-path V2 container (16-byte EPK): the
        // trailing-zero-trimmed value `3A` is zero-extended to 16 bytes, then
        // incremented at the last byte (Option B full-width normalization,
        // matching .NET).
        let point = QueryRange {
            min: "3A".to_string(),
            max: "3A".to_string(),
            is_min_inclusive: true,
            is_max_inclusive: true,
        };
        let fr = query_range_to_feed_range(&point, Some(16)).unwrap();
        assert_eq!(fr.min_inclusive().to_hex(), "3A");
        // normalized_successor("3A", 16) = "3A" zero-extended to 16 bytes, then
        // +1 at the last byte: [0x3A, 0x00 x14, 0x01].
        let mut expected = vec![0x3Au8];
        expected.resize(16, 0x00);
        expected[15] = 0x01;
        let expected_hex: String = expected.iter().map(|b| format!("{:02X}", b)).collect();
        assert_eq!(fr.max_exclusive().to_hex(), expected_hex);
    }

    #[tokio::test]
    async fn rejects_query_plan_with_top() {
        let plan = QueryPlan {
            query_info: Some(QueryInfo {
                top: Some(10),
                ..Default::default()
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("unsupported query feature: TOP clause in cross-partition queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn rejects_query_plan_with_limit() {
        let plan = QueryPlan {
            query_info: Some(QueryInfo {
                limit: Some(20),
                ..Default::default()
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered
                .ends_with("unsupported query feature: LIMIT clause in cross-partition queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn rejects_query_plan_with_order_by() {
        use super::super::query_plan::SortOrder;
        let plan = QueryPlan {
            query_info: Some(QueryInfo {
                order_by: vec![SortOrder::Ascending],
                ..Default::default()
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("unsupported query feature: ORDER BY in cross-partition queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn rejects_query_plan_with_aggregates() {
        let plan = QueryPlan {
            query_info: Some(QueryInfo {
                aggregates: vec!["Count".to_string()],
                ..Default::default()
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("unsupported query feature: aggregates in cross-partition queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn rejects_query_plan_with_group_by() {
        let plan = QueryPlan {
            query_info: Some(QueryInfo {
                group_by_expressions: vec!["c.category".to_string()],
                ..Default::default()
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("unsupported query feature: GROUP BY in cross-partition queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn rejects_query_plan_with_hybrid_search() {
        let plan = QueryPlan {
            hybrid_search_query_info: Some(super::super::query_plan::HybridSearchQueryInfo {
                global_statistics_query: "SELECT COUNT(1) FROM c".to_string(),
                component_query_infos: vec![],
                component_weights: vec![],
                skip: None,
                take: Some(10),
                requires_global_statistics: true,
            }),
            ..plan_with_ranges(vec![qr("", "FF")])
        };
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("unsupported query feature: hybrid search queries"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn accepts_query_plan_with_no_query_info() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap();
        assert_drain_requests(pipeline, &[("", "FF", "pkrange-0")]);
    }

    #[tokio::test]
    async fn rejects_empty_query_ranges() {
        let plan = plan_with_ranges(vec![]);
        let op = cross_partition_query_operation();
        let mut topology = NoopTopologyProvider;

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("query plan produced no partition ranges to query"),
            "unexpected: {rendered}"
        );
    }

    #[tokio::test]
    async fn propagates_topology_resolution_error() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology =
            MockTopologyProvider::new(vec![Err(crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::new(
                    azure_core::http::StatusCode::BadRequest,
                ))
                .with_message("topology resolution failed")
                .build())]);

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), None)
            .await
            .unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.ends_with("topology resolution failed"),
            "unexpected: {rendered}"
        );
    }

    // -----------------------------------------------------------------
    // Resume tests
    // -----------------------------------------------------------------

    /// Builds a sparse `SequentialDrain` resume state from the legacy
    /// `(min, max, state)` triple shape. Drained entries set / advance the
    /// cursor; `Request { Some(token) }` entries become `active_tokens`;
    /// `Request { None }` entries are skipped (sparse encoding treats them
    /// as implicit fresh-start). The triples are assumed sorted; tests
    /// that want to exercise the validator with malformed sparse shapes
    /// build `PipelineNodeState::SequentialDrain { ... }` directly.
    fn saved_drain(children: Vec<(&str, &str, PipelineNodeState)>) -> PipelineNodeState {
        let mut cursor: Option<String> = None;
        let mut active_tokens: Vec<RangedToken> = Vec::new();
        for (min, max, state) in children {
            match state {
                PipelineNodeState::Drained => {
                    debug_assert!(
                        cursor.is_none(),
                        "saved_drain helper does not support drained children after the cursor; \
                         construct the sparse shape directly for that case",
                    );
                    cursor = Some(max.to_owned());
                }
                PipelineNodeState::Request {
                    server_continuation,
                } => {
                    if cursor.is_none() {
                        cursor = Some(min.to_owned());
                    }
                    if let Some(token) = server_continuation {
                        active_tokens.push(RangedToken {
                            min_epk: min.to_owned(),
                            max_epk: max.to_owned(),
                            server_continuation: token,
                        });
                    }
                }
                other => panic!(
                    "saved_drain helper does not accept nested SequentialDrain states; \
                     construct the sparse shape directly. Got: {other:?}"
                ),
            }
        }
        PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: cursor.unwrap_or_default(),
            active_tokens,
        }
    }

    fn saved_request(server_continuation: Option<&str>) -> PipelineNodeState {
        PipelineNodeState::Request {
            server_continuation: server_continuation.map(str::to_owned),
        }
    }

    #[tokio::test]
    async fn resume_drained_state_yields_drained_pipeline() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let pipeline = build_sequential_drain(
            &plan,
            &mut topology,
            &Arc::new(op),
            Some(PipelineNodeState::Drained),
        )
        .await
        .unwrap();

        assert!(matches!(
            pipeline.snapshot_state().unwrap(),
            PipelineNodeState::Drained
        ));
    }

    #[tokio::test]
    async fn resume_skips_topology_below_first_saved_child() {
        // Saved children cover only `[55, FF)`. The topology has a range
        // `[, 55)` that falls outside every saved range — that range has
        // already been drained on a prior page and must not be re-queried.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "55", "pk-a"),
            rr("55", "AA", "pk-b"),
            rr("AA", "FF", "pk-c"),
        ])]);

        let resume = saved_drain(vec![
            ("55", "AA", saved_request(None)),
            ("AA", "FF", saved_request(None)),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests(pipeline, &[("55", "AA", "pk-b"), ("AA", "FF", "pk-c")]);
    }

    #[tokio::test]
    async fn resume_propagates_server_continuation_to_every_surviving_leaf_after_split() {
        // The saved `[55, AA)` child held a server continuation. Between
        // sessions the underlying partition split into `[55, 70)` + `[70, AA)`;
        // every surviving leaf in the saved child's scope must carry the
        // saved continuation, otherwise the continuation-less leaves execute
        // a fresh query and re-emit items the caller already consumed.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "55", "pk-a"),
            rr("55", "70", "pk-b1"),
            rr("70", "AA", "pk-b2"),
            rr("AA", "FF", "pk-c"),
        ])]);

        let resume = saved_drain(vec![
            ("55", "AA", saved_request(Some("server-token-xyz"))),
            ("AA", "FF", saved_request(None)),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("55", "70", "pk-b1", "55", "70", Some("server-token-xyz")),
                ("70", "AA", "pk-b2", "70", "AA", Some("server-token-xyz")),
                ("AA", "FF", "pk-c", "AA", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_does_not_leak_continuation_into_siblings_past_saved_scope() {
        // Saved child `[55, AA)` holds a continuation; sibling `[AA, FF)`
        // does not. Topology unchanged across sessions: each saved child
        // maps 1:1 to its leaf, and the continuation must not propagate
        // into the following sibling.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "55", "pk-a"),
            rr("55", "AA", "pk-b"),
            rr("AA", "FF", "pk-c"),
        ])]);

        let resume = saved_drain(vec![
            ("55", "AA", saved_request(Some("server-token-xyz"))),
            ("AA", "FF", saved_request(None)),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("55", "AA", "pk-b", "55", "AA", Some("server-token-xyz")),
                ("AA", "FF", "pk-c", "AA", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_does_not_leak_continuation_across_query_ranges() {
        // Two disjoint query-plan ranges. The first saved child holds the
        // continuation; every leaf in the second range must start fresh.
        let plan = plan_with_ranges(vec![qr("", "55"), qr("80", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "30", "pk-a"), rr("30", "55", "pk-b")]),
            Ok(vec![rr("80", "C0", "pk-c"), rr("C0", "FF", "pk-d")]),
        ]);

        let resume = saved_drain(vec![
            ("30", "55", saved_request(Some("server-token-xyz"))),
            ("80", "C0", saved_request(None)),
            ("C0", "FF", saved_request(None)),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("30", "55", "pk-b", "30", "55", Some("server-token-xyz")),
                ("80", "C0", "pk-c", "80", "C0", None),
                ("C0", "FF", "pk-d", "C0", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_with_cursor_past_topology_yields_drained_pipeline() {
        // Wire form `SequentialDrain { cursor = "FF", active_tokens = [] }`
        // means every range has been drained: the cursor is at or past
        // the last topology max, and no range above it owes a token.
        // The planner emits no leaves → pipeline is drained.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pk-a")])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: "FF".to_owned(),
            active_tokens: vec![],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert!(matches!(
            pipeline.snapshot_state().unwrap(),
            PipelineNodeState::Drained
        ));
    }

    #[tokio::test]
    async fn resume_on_merged_range_splits_resumed_slice_and_tail() {
        // Two saved children: `[55, AA)` with a token, `[AA, FF)` without.
        // Between sessions the topology merged into one wide `[, FF)` range;
        // each saved child intersects the merged range and produces its own
        // leaf, preserving the token/no-token distinction.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pk-merged")])]);

        let resume = saved_drain(vec![
            ("55", "AA", saved_request(Some("server-token-xyz"))),
            ("AA", "FF", saved_request(None)),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();

        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("55", "AA", "pk-merged", "", "FF", Some("server-token-xyz")),
                ("AA", "FF", "pk-merged", "", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_validates_saved_children_sorted_non_overlapping() {
        // Out-of-order active_tokens: [55, AA) then [00, 55) violates
        // strict ascending order.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pk-a")])]);

        let resume = saved_drain(vec![
            ("55", "AA", saved_request(Some("tok-a"))),
            ("00", "55", saved_request(Some("tok-b"))),
        ]);

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap_err();
        assert_eq!(
            err.status().sub_status(),
            Some(crate::error::SubStatusCode::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE),
            "expected invalid-children sub-status, got: {err}",
        );
    }

    #[tokio::test]
    async fn resume_validates_saved_children_no_overlap() {
        // Overlapping active_tokens: [00, 80) and [55, FF) overlap on
        // [55, 80).
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pk-a")])]);

        let resume = saved_drain(vec![
            ("00", "80", saved_request(Some("tok-a"))),
            ("55", "FF", saved_request(Some("tok-b"))),
        ]);

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap_err();
        assert_eq!(
            err.status().sub_status(),
            Some(crate::error::SubStatusCode::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE),
            "expected invalid-children sub-status, got: {err}",
        );
    }

    #[tokio::test]
    async fn resume_errors_when_non_drained_saved_range_unhonored() {
        // Saved child `[55, AA)` holds a continuation, but the topology
        // only covers `[00, 40)`. The planner cannot honor the saved
        // continuation without risking duplicate emission or data loss.
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "40", "pk-a")])]);

        let resume = saved_drain(vec![("55", "AA", saved_request(Some("server-token-xyz")))]);

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap_err();
        assert_eq!(
            err.status().sub_status(),
            Some(crate::error::SubStatusCode::CLIENT_CONTINUATION_TOKEN_SAVED_RANGE_UNHONORED),
            "expected saved-range-unhonored sub-status, got: {err}",
        );
    }

    #[tokio::test]
    async fn resume_with_cursor_skips_drained_prefix_and_fresh_starts_uncovered_tail() {
        // Sparse semantics: the cursor marks the end of the drained
        // prefix. Anything above the cursor that has no active token is
        // implicitly fresh-start — there's no "drained range past the
        // cursor" in the sparse encoding. So with cursor="55", one
        // active token covering [55, AA), and a topology of three
        // resolved ranges [, 55), [55, AA), [AA, FF), the planner must:
        //   - skip [, 55)        (fully below cursor → drained prefix)
        //   - emit Request(tok)  for [55, AA)  (overlaps active token)
        //   - emit Request(None) for [AA, FF)  (above cursor, no active
        //                                       token → fresh-start)
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "55", "pk-a"),
            rr("55", "AA", "pk-b"),
            rr("AA", "FF", "pk-c"),
        ])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: "55".to_owned(),
            active_tokens: vec![RangedToken {
                min_epk: "55".to_owned(),
                max_epk: "AA".to_owned(),
                server_continuation: "server-token-xyz".to_owned(),
            }],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("55", "AA", "pk-b", "55", "AA", Some("server-token-xyz")),
                ("AA", "FF", "pk-c", "AA", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_in_predicate_drops_point_partition_below_cursor() {
        // Regression for issues #4574 / #4638 resume path (Option B): an
        // `IN (@a, @b)` whose values hash into two different partitions, resumed
        // with a cursor that has fully drained the first point's window. Each
        // point normalizes to `[X, successor(X))`. The first window lies entirely
        // at/below the cursor and is dropped; only the second point's window is
        // emitted (fresh-start, no token).
        let plan = plan_with_ranges(vec![
            QueryRange {
                min: "20".to_string(),
                max: "20".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
            QueryRange {
                min: "C0".to_string(),
                max: "C0".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
        ]);
        let op = cross_partition_query_operation();
        // One resolve_ranges call per query range: "20" → left, "C0" → right.
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "80", "pk-left")]),
            Ok(vec![rr("80", "FF", "pk-right")]),
        ]);

        // Cursor at "80": the left window `[20, successor(20))` is fully drained.
        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: "80".to_owned(),
            active_tokens: vec![],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        // Left window dropped (at/below cursor); right window emitted fresh-start.
        let sc0 = EffectivePartitionKey::from("C0")
            .normalized_successor(16)
            .to_hex();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[("C0", sc0.as_str(), "pk-right", "80", "FF", None)],
        );
    }

    #[tokio::test]
    async fn resume_in_predicate_colocated_windows_carry_their_continuations() {
        // Resume path, Option B: an `IN (@a, @b)` whose values are co-located in
        // ONE partition. Each equality value is its own `[X, successor(X))` EPK
        // window with an independent server continuation, so the saved snapshot
        // carries one token per window. On resume each window re-emits carrying
        // its own token — disjoint windows, no de-duplication.
        let plan = plan_with_ranges(vec![
            QueryRange {
                min: "20".to_string(),
                max: "20".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
            QueryRange {
                min: "50".to_string(),
                max: "50".to_string(),
                is_min_inclusive: true,
                is_max_inclusive: true,
            },
        ]);
        let op = cross_partition_query_operation();
        // Both points resolve to the same single partition.
        let mut topology = MockTopologyProvider::new(vec![
            Ok(vec![rr("", "FF", "pk-0")]),
            Ok(vec![rr("", "FF", "pk-0")]),
        ]);

        let s20 = EffectivePartitionKey::from("20")
            .normalized_successor(16)
            .to_hex();
        let s50 = EffectivePartitionKey::from("50")
            .normalized_successor(16)
            .to_hex();

        // Each in-flight window has its own saved server continuation.
        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: "".to_owned(),
            active_tokens: vec![
                RangedToken {
                    min_epk: "20".to_owned(),
                    max_epk: s20.clone(),
                    server_continuation: "tok-a".to_owned(),
                },
                RangedToken {
                    min_epk: "50".to_owned(),
                    max_epk: s50.clone(),
                    server_continuation: "tok-b".to_owned(),
                },
            ],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("20", s20.as_str(), "pk-0", "", "FF", Some("tok-a")),
                ("50", s50.as_str(), "pk-0", "", "FF", Some("tok-b")),
            ],
        );
    }

    #[tokio::test]
    async fn resume_multiple_saved_children_in_one_resolved_range_no_duplicate_leaves() {
        // The topology has merged the saved children into one wide range.
        // Each active token produces exactly one leaf scoped to its own
        // range, and the trailing portion of the merged range above the
        // last active token is emitted as a single fresh-start leaf
        // covering the gap to the topology max (sparse semantics:
        // ranges above the cursor not covered by an active token are
        // implicitly fresh-start).
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pk-merged")])]);

        let resume = saved_drain(vec![
            ("10", "30", saved_request(Some("tok-a"))),
            ("30", "60", saved_request(Some("tok-b"))),
        ]);

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("10", "30", "pk-merged", "", "FF", Some("tok-a")),
                ("30", "60", "pk-merged", "", "FF", Some("tok-b")),
                ("60", "FF", "pk-merged", "", "FF", None),
            ],
        );
    }

    #[tokio::test]
    async fn resume_emits_fresh_leaves_for_topology_gaps_above_cursor() {
        // Sparse semantics: any topology range above the cursor that is
        // NOT covered by an active token is fresh-start (not drained).
        // This is the O(S) trade-off — only ranges below the cursor are
        // skipped as drained. With cursor="40", one active token at
        // [40, 60), and topology [, 20), [20, 40), [40, 60), [60, 80),
        // [80, FF), the planner emits:
        //   - skip [, 20)        (below cursor)
        //   - skip [20, 40)      (below cursor)
        //   - Request(tok)       for [40, 60)
        //   - Request(None)      for [60, 80)  (fresh-start, no token)
        //   - Request(None)      for [80, FF)  (fresh-start, no token)
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "20", "pk-a"),
            rr("20", "40", "pk-b"),
            rr("40", "60", "pk-c"),
            rr("60", "80", "pk-d"),
            rr("80", "FF", "pk-e"),
        ])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: "40".to_owned(),
            active_tokens: vec![RangedToken {
                min_epk: "40".to_owned(),
                max_epk: "60".to_owned(),
                server_continuation: "tok".to_owned(),
            }],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .unwrap();
        assert_drain_requests_with_partitions_and_continuation(
            pipeline,
            &[
                ("40", "60", "pk-c", "40", "60", Some("tok")),
                ("60", "80", "pk-d", "60", "80", None),
                ("80", "FF", "pk-e", "80", "FF", None),
            ],
        );
    }

    /// An older serialized shape — a top-level bare `Request` continuation
    /// for what is now a `SequentialDrain` — must be rejected on resume
    /// rather than silently re-interpreted as a full-range cursor. Guards
    /// the planner's existing rejection of that shape.
    #[tokio::test]
    async fn legacy_top_level_bare_request_shape_fails_to_resume() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let legacy = PipelineNodeState::Request {
            server_continuation: Some("OLD".to_owned()),
        };

        let result =
            build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(legacy)).await;
        let err = result.expect_err("bare top-level Request shape must be rejected on resume");
        assert_eq!(
            err.status(),
            crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_SHAPE_MISMATCH,
            "expected SHAPE_MISMATCH for top-level bare Request shape; got {err:?}",
        );
    }

    /// Zero-width active_tokens entries are well-formed JSON but cannot
    /// carry remaining work. They must be rejected with a message that
    /// points at the entry itself rather than at a downstream "could not
    /// be fully covered" error.
    #[tokio::test]
    async fn rejects_zero_width_saved_child_entry_with_clear_message() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: String::new(),
            active_tokens: vec![RangedToken {
                min_epk: "40".to_owned(),
                max_epk: "40".to_owned(),
                server_continuation: "tok".to_owned(),
            }],
        };

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .expect_err("zero-width active_tokens entry must be rejected");
        assert_eq!(
            err.status(),
            crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
        );
        let rendered = err.to_string();
        assert!(
            rendered.contains("zero-width"),
            "error message should describe the zero-width entry; got: {rendered}"
        );
    }

    /// The continuation-token validator must reject an `active_tokens`
    /// entry with `min >= max` regardless of how that wire payload was
    /// produced (corrupted token, hand-rolled, future-version rollback).
    /// Pins the validator behavior so a future change to the comparison
    /// semantics (e.g., `EffectivePartitionKey::Ord`) can't silently
    /// downgrade this fail-loud path to a silent re-query.
    #[tokio::test]
    async fn malformed_min_greater_than_max_child_is_rejected_by_validator() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: String::new(),
            active_tokens: vec![RangedToken {
                min_epk: "FF".to_owned(),
                max_epk: "00".to_owned(),
                server_continuation: "tok".to_owned(),
            }],
        };

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .expect_err("malformed min>max entry must be rejected by the validator");
        assert_eq!(
            err.status(),
            crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
            "malformed min>max entry must trip the EPK-range validator path",
        );
    }

    /// Companion to the test above: when a malformed `min >= max` entry
    /// is *appended* to legitimate entries, the validator must still
    /// reject the whole payload. Guards against a future "skip invalid
    /// entries, continue with the valid ones" relaxation that would
    /// silently swallow snapshot corruption.
    #[tokio::test]
    async fn malformed_min_greater_than_max_appended_to_valid_children_still_rejects() {
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![rr("", "FF", "pkrange-0")])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: String::new(),
            active_tokens: vec![
                RangedToken {
                    min_epk: String::new(),
                    max_epk: "80".to_owned(),
                    server_continuation: "real-token".to_owned(),
                },
                RangedToken {
                    min_epk: "FF".to_owned(),
                    max_epk: "00".to_owned(),
                    server_continuation: "tok".to_owned(),
                },
            ],
        };

        let err = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .expect_err("appended malformed min>max entry must still be rejected");
        assert_eq!(
            err.status(),
            crate::error::CosmosStatus::CLIENT_CONTINUATION_TOKEN_INVALID_EPK_RANGE,
        );
    }

    /// Symmetric variant of the cascading-split scenario — the FRONT
    /// sibling splits between snapshots instead of the back one. The
    /// planner's interval-join logic is symmetric in the two siblings,
    /// so this test guards against an accidental asymmetry (e.g.,
    /// assuming the "still-pending" sibling is always the back one)
    /// that would be invisible to the existing back-split test.
    #[tokio::test]
    async fn cascading_split_of_front_sibling_propagates_token_to_grand_children() {
        // Saved state: cursor at start, active_tokens has one entry for
        // [, 80) owing T1 (front sibling is in progress). The back range
        // [80, FF) is not in active_tokens, so it's implicitly fresh-start.
        // Then topology resolves the front into two grand-children
        // [, 40) + [40, 80) on top of the unchanged back [80, FF).
        let plan = plan_with_ranges(vec![qr("", "FF")]);
        let op = cross_partition_query_operation();
        let mut topology = MockTopologyProvider::new(vec![Ok(vec![
            rr("", "40", "pkrange-front-left"),
            rr("40", "80", "pkrange-front-right"),
            rr("80", "FF", "pkrange-back"),
        ])]);

        let resume = PipelineNodeState::SequentialDrain {
            left_most_undrained_epk: String::new(),
            active_tokens: vec![RangedToken {
                min_epk: String::new(),
                max_epk: "80".to_owned(),
                server_continuation: "T1".to_owned(),
            }],
        };

        let pipeline = build_sequential_drain(&plan, &mut topology, &Arc::new(op), Some(resume))
            .await
            .expect("front-sibling cascading split must plan cleanly");

        // Walk the planned children via snapshot: the two front grand-
        // children must each carry T1; the back range must be a
        // fresh-start leaf (implicit — appears in the planned children
        // but not in active_tokens). The exact structure mirrors the
        // back-split case in `query_resume_integration_tests::
        // cascading_split_..._grand_child`.
        let snap = pipeline.snapshot_state().unwrap();
        let (cursor, active_tokens) = match snap {
            PipelineNodeState::SequentialDrain {
                left_most_undrained_epk,
                active_tokens,
            } => (left_most_undrained_epk, active_tokens),
            other => panic!("expected SequentialDrain, got {other:?}"),
        };
        assert_eq!(cursor, "", "cursor must remain at start");
        assert_eq!(
            active_tokens.len(),
            2,
            "expected 2 active tokens for the front grand-children, got {active_tokens:?}",
        );
        for (idx, expected_min, expected_max) in [(0, "", "40"), (1, "40", "80")] {
            assert_eq!(
                active_tokens[idx].min_epk, expected_min,
                "active_tokens[{idx}] min_epk mismatch",
            );
            assert_eq!(
                active_tokens[idx].max_epk, expected_max,
                "active_tokens[{idx}] max_epk mismatch",
            );
            assert_eq!(
                active_tokens[idx].server_continuation, "T1",
                "front grand-child {idx} must carry T1",
            );
        }
    }
}