jsoncompat 0.4.1

JSON Schema and OpenAPI Compatibility Checker
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
//! Boolean-applicator complement and partition helpers.
//!
//! These specialized `oneOf`/`anyOf`/`not` identities are proof helpers: false
//! means unknown, not incompatible.

use super::*;

pub(super) fn one_of_universal_arm_contains_subset(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }
    for universal_idx in 0..2 {
        let universal = unwrap_singleton_applicators(&branches[universal_idx]);
        if !schema_is_trivially_universal(universal) {
            continue;
        }
        let excluded = unwrap_singleton_applicators(&branches[1 - universal_idx]);
        if schemas_definitely_disjoint_for_negation(sub, excluded, context)
            || schema_disjoint_from_conditional(sub, excluded, context)
        {
            return true;
        }
    }
    false
}

/// Conservative disjointness for a schema against a conditional.  If every
/// subset value is known to take one side of the guard, it suffices to prove it
/// disjoint from that side's branch. Missing branches are `true`, so they cannot
/// witness disjointness.
pub(super) fn schema_disjoint_from_conditional(
    sub: &SchemaNode,
    conditional: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    let SchemaNodeKind::IfThenElse {
        if_schema,
        then_schema,
        else_schema,
    } = conditional.kind()
    else {
        return false;
    };

    let sub_implies_guard =
        analyze_subschema_with_context(sub, if_schema, context, ExplanationMode::VerdictOnly)
            .is_subschema;
    if sub_implies_guard {
        return then_schema.as_ref().is_some_and(|then_branch| {
            schemas_definitely_disjoint_for_negation(sub, then_branch, context)
        });
    }

    if schemas_definitely_disjoint_for_negation(sub, if_schema, context) {
        return else_schema.as_ref().is_some_and(|else_branch| {
            schemas_definitely_disjoint_for_negation(sub, else_branch, context)
        });
    }

    false
}

/// An `anyOf` is universal as soon as one branch is known universal.  The
/// generic trivial-universal recognizer intentionally stays syntactic; this
/// wrapper also recognizes the small conditional tautologies proved by
/// `conditional_is_known_universal` (for example `if: A, then: A`).
pub(super) fn any_of_contains_known_universal_branch(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    branches.iter().any(|branch| {
        let branch = unwrap_singleton_applicators(branch);
        if schema_is_trivially_universal(branch) {
            return true;
        }
        if let SchemaNodeKind::IfThenElse {
            if_schema,
            then_schema,
            else_schema,
        } = branch.kind()
        {
            return conditional_is_known_universal(
                if_schema,
                then_schema.as_ref(),
                else_schema.as_ref(),
                context,
            );
        }
        false
    })
}

/// Return true when an `anyOf` is syntactically universal because it contains
/// a branch `A` plus a complement branch `not B` with `B <= A`.
///
/// For every instance, either it satisfies `B` (and therefore `A`) or it does
/// not satisfy `B` (and therefore satisfies `not B`).  The inner implication
/// is delegated to the ordinary conservative subset prover; failure simply
/// means we do not recognize the cover.
pub(super) fn any_of_complement_cover_is_universal(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    for (not_index, branch) in branches.iter().enumerate() {
        let SchemaNodeKind::Not(excluded) = branch.kind() else {
            continue;
        };
        for (cover_index, cover) in branches.iter().enumerate() {
            if cover_index == not_index {
                continue;
            }
            if analyze_subschema_with_context(
                excluded,
                cover,
                context,
                ExplanationMode::VerdictOnly,
            )
            .is_subschema
            {
                return true;
            }
        }

        // The positive side of the partition may itself be split across
        // several finite branches rather than appearing as one syntactic
        // schema. If `B` has a finite upper bound and every live candidate of
        // `B` is accepted by some sibling branch, then those siblings cover B;
        // together with `not B` the union is universal. We deliberately use an
        // upper bound plus definite concrete-value checks, so unsupported
        // keywords only make this fail closed.
        if let Some(values) = finite_schema_value_superset(excluded)
            && values.iter().all(|value| {
                context.schema_definitely_rejects_value(excluded, value)
                    || branches.iter().enumerate().any(|(cover_index, cover)| {
                        cover_index != not_index && context.superset_contains_value(cover, value)
                    })
            })
        {
            return true;
        }
    }
    false
}

/// Prove an `anyOf` is universal when it contains the complement of a
/// syntactic union plus siblings covering every union arm.  Overlap among the
/// positive siblings is harmless for `anyOf`; for any instance, either it is
/// outside the excluded union (so the complement branch accepts it) or it is
/// inside one union arm that is contained by a sibling.
pub(super) fn any_of_complement_union_cover_is_universal(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    for (not_index, branch) in branches.iter().enumerate() {
        let SchemaNodeKind::Not(excluded_union) = branch.kind() else {
            continue;
        };
        let union_children = match excluded_union.kind() {
            SchemaNodeKind::AnyOf(children) | SchemaNodeKind::OneOf(children) => children,
            _ => continue,
        };

        if union_children.iter().all(|child| {
            branches.iter().enumerate().any(|(index, cover)| {
                index != not_index
                    && analyze_subschema_with_context(
                        child,
                        cover,
                        context,
                        ExplanationMode::VerdictOnly,
                    )
                    .is_subschema
            })
        }) {
            return true;
        }
    }
    false
}

/// Prove a subset relation for a `oneOf` made only of complements.
/// With two or more complement branches, any accepted value must be excluded
/// by at least one inner schema (otherwise it would match every complement
/// branch, not exactly one). Therefore the whole oneOf is contained in the
/// union of the excluded inners. If each inner is contained by the target, the
/// complement-only oneOf is contained as well.
pub(super) fn complement_only_oneof_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() < 2 {
        return false;
    }
    let mut inners = Vec::with_capacity(branches.len());
    for branch in branches {
        let SchemaNodeKind::Not(inner) = branch.kind() else {
            return false;
        };
        inners.push(inner);
    }
    // For a branch `not A_j` to be the *only* matching complement, the
    // instance must satisfy every other inner `A_i`.  Thus it is enough that,
    // for each omitted position j, at least one of the remaining inners is
    // known to be contained by the target.  With two branches this reduces to
    // the familiar requirement that both inners imply the target; with three
    // or more branches, two target-contained inners already cover every xor
    // arm.  Keep the proof at the level of whole inners so we do not assume
    // anything about intersections we cannot model.
    let contained: Vec<bool> = inners
        .iter()
        .map(|inner| {
            analyze_subschema_with_context(inner, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
        })
        .collect();
    (0..inners.len()).all(|omitted| {
        if contained
            .iter()
            .enumerate()
            .any(|(index, ok)| index != omitted && *ok)
        {
            return true;
        }

        // The xor arm for `not A_omitted` also vanishes when two of the
        // required remaining inners are definitely disjoint.  This catches
        // empty complement xors such as not-string/not-number/not-boolean
        // without needing a general intersection solver.
        for (left_index, &left) in inners.iter().enumerate() {
            if left_index == omitted {
                continue;
            }
            let left_mask = possible_json_type_mask(left);
            if left_mask == 0 {
                return true;
            }
            for (right_index, &right) in inners.iter().enumerate().skip(left_index + 1) {
                if right_index == omitted {
                    continue;
                }
                if possible_json_type_mask(right) == 0
                    || schemas_definitely_disjoint_for_partition(left, left_mask, right, context)
                {
                    return true;
                }
            }
        }
        false
    })
}

/// Try the mixed-xor disjoint reduction against a target directly, or against
/// one negated arm of a target union.
/// Prove `oneOf[A, B] <= not T` when one positive arm is contained in the
/// other.  Then the xor is the larger arm with the smaller arm removed.
pub(super) fn comparable_oneof_difference_subset_of_negation(
    branches: &[SchemaNode],
    excluded: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    for (small_idx, large_idx) in [(0_usize, 1_usize), (1, 0)] {
        let small = unwrap_singleton_applicators(&branches[small_idx]);
        let large = unwrap_singleton_applicators(&branches[large_idx]);
        if matches!(small.kind(), SchemaNodeKind::Not(_))
            || matches!(large.kind(), SchemaNodeKind::Not(_))
        {
            continue;
        }
        if !analyze_subschema_with_context(small, large, context, ExplanationMode::VerdictOnly)
            .is_subschema
        {
            continue;
        }

        let excluded = unwrap_singleton_applicators(excluded);
        let mut pieces: Vec<&SchemaNode> = Vec::new();
        match excluded.kind() {
            SchemaNodeKind::AnyOf(children) => pieces.extend(children.iter()),
            _ => pieces.push(excluded),
        }

        let mut ok = true;
        for piece in pieces {
            let piece = unwrap_singleton_applicators(piece);
            let inside_removed =
                analyze_subschema_with_context(piece, small, context, ExplanationMode::VerdictOnly)
                    .is_subschema;
            if inside_removed {
                continue;
            }
            if !schemas_definitely_disjoint_for_negation(piece, large, context) {
                ok = false;
                break;
            }
        }
        if ok {
            return true;
        }
    }
    false
}

pub(super) fn mixed_oneof_disjoint_complement_subset_of_target(
    branches: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    fn inner(
        branches: &[SchemaNode],
        sup: &SchemaNode,
        context: &mut SubschemaCheckContext,
        active: &mut HashSet<NodeId>,
    ) -> bool {
        let sup = unwrap_singleton_applicators(sup);
        if !active.insert(sup.id()) {
            return false;
        }
        let result = match sup.kind() {
            SchemaNodeKind::BoolSchema(true) | SchemaNodeKind::Any => true,
            SchemaNodeKind::Not(excluded) => {
                mixed_oneof_disjoint_complement_subset_of_negation(branches, excluded, context)
            }
            // It is enough to fit one union arm.
            SchemaNodeKind::AnyOf(children) => {
                if mixed_oneof_disjoint_complement_subset_of_union_target(
                    branches, children, context,
                ) {
                    true
                } else {
                    let mut ok = false;
                    for child in children {
                        if inner(branches, child, context, active) {
                            ok = true;
                            break;
                        }
                    }
                    ok
                }
            }
            // To fit an intersection, fit every conjunct.
            SchemaNodeKind::AllOf(children) => {
                let mut ok = true;
                for child in children {
                    if !inner(branches, child, context, active) {
                        ok = false;
                        break;
                    }
                }
                ok
            }
            SchemaNodeKind::OneOf(target_children) => {
                mixed_oneof_disjoint_complement_subset_of_mixed_target(
                    branches,
                    target_children,
                    context,
                )
            }
            _ => false,
        };
        active.remove(&sup.id());
        result
    }

    inner(branches, sup, context, &mut HashSet::new())
}

/// Prove a disjoint mixed xor fits a union containing a negated finite arm.
///
/// For subset `not(A ∪ B)`, a target union with an arm `not D` can only reject
/// values from D that are not accepted by another union arm.  Enumerate a
/// finite over-approximation of D and require each such remaining value to be
/// covered by A or B.
pub(super) fn mixed_oneof_disjoint_complement_subset_of_union_target(
    sub_branches: &[SchemaNode],
    target_children: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if sub_branches.len() != 2 || target_children.is_empty() {
        return false;
    }

    for sub_neg_idx in 0..2 {
        let sub_pos_idx = 1 - sub_neg_idx;
        let SchemaNodeKind::Not(sub_b_raw) = sub_branches[sub_neg_idx].kind() else {
            continue;
        };
        let sub_a = unwrap_singleton_applicators(&sub_branches[sub_pos_idx]);
        let sub_b = unwrap_singleton_applicators(sub_b_raw);
        if !schemas_definitely_disjoint_for_negation(sub_a, sub_b, context) {
            continue;
        }

        for (neg_index, child) in target_children.iter().enumerate() {
            let SchemaNodeKind::Not(d_raw) = unwrap_singleton_applicators(child).kind() else {
                continue;
            };
            let d = unwrap_singleton_applicators(d_raw);
            let Some(values) = finite_schema_value_superset(d) else {
                continue;
            };
            let mut covered = true;
            'values: for value in values {
                for (index, other) in target_children.iter().enumerate() {
                    if index == neg_index {
                        continue;
                    }
                    if context.superset_contains_value(other, &value) {
                        continue 'values;
                    }
                }
                if !(context.superset_contains_value(sub_a, &value)
                    || context.superset_contains_value(sub_b, &value))
                {
                    covered = false;
                    break;
                }
            }
            if covered {
                return true;
            }
        }
    }
    false
}

/// Prove a disjoint mixed xor is contained by a comparable mixed-xor target.
///
/// For disjoint A/B, the subset is `not(A ∪ B)`.  A target `oneOf[C, not D]`
/// with C <= D is `C ∪ not D`, i.e. it rejects only `D \ C`.  If D has a
/// finite over-approximation and every value not definitely in C is covered by
/// A or B, then anything outside A ∪ B must satisfy the target.
pub(super) fn mixed_oneof_disjoint_complement_subset_of_mixed_target(
    sub_branches: &[SchemaNode],
    target_branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if sub_branches.len() != 2 || target_branches.len() != 2 {
        return false;
    }

    for sub_neg_idx in 0..2 {
        let sub_pos_idx = 1 - sub_neg_idx;
        let SchemaNodeKind::Not(sub_b_raw) = sub_branches[sub_neg_idx].kind() else {
            continue;
        };
        let sub_a = unwrap_singleton_applicators(&sub_branches[sub_pos_idx]);
        let sub_b = unwrap_singleton_applicators(sub_b_raw);
        if !schemas_definitely_disjoint_for_negation(sub_a, sub_b, context) {
            continue;
        }

        for tgt_neg_idx in 0..2 {
            let tgt_pos_idx = 1 - tgt_neg_idx;
            let SchemaNodeKind::Not(tgt_d_raw) = target_branches[tgt_neg_idx].kind() else {
                continue;
            };
            let tgt_c = unwrap_singleton_applicators(&target_branches[tgt_pos_idx]);
            let tgt_d = unwrap_singleton_applicators(tgt_d_raw);
            if !analyze_subschema_with_context(tgt_c, tgt_d, context, ExplanationMode::VerdictOnly)
                .is_subschema
            {
                continue;
            }
            let Some(values) = finite_schema_value_superset(tgt_d) else {
                continue;
            };
            let mut covered = true;
            for value in values {
                if context.superset_contains_value(tgt_c, &value) {
                    continue;
                }
                if !(context.superset_contains_value(sub_a, &value)
                    || context.superset_contains_value(sub_b, &value))
                {
                    covered = false;
                    break;
                }
            }
            if covered {
                return true;
            }
        }
    }
    false
}

/// Prove `oneOf[A, not B] <= not T` for the disjoint A/B case.
///
/// If A and B are disjoint, the xor accepts exactly values outside both A and
/// B.  Therefore it is contained by `not T` whenever T is contained by A or B.
pub(super) fn mixed_oneof_disjoint_complement_subset_of_negation(
    branches: &[SchemaNode],
    excluded: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    for neg_idx in 0..2 {
        let pos_idx = 1 - neg_idx;
        let SchemaNodeKind::Not(inner_b) = branches[neg_idx].kind() else {
            continue;
        };
        let a = unwrap_singleton_applicators(&branches[pos_idx]);
        let b = unwrap_singleton_applicators(inner_b);
        if !schemas_definitely_disjoint_for_negation(a, b, context) {
            continue;
        }
        let excluded = unwrap_singleton_applicators(excluded);
        if analyze_subschema_with_context(excluded, a, context, ExplanationMode::VerdictOnly)
            .is_subschema
            || analyze_subschema_with_context(excluded, b, context, ExplanationMode::VerdictOnly)
                .is_subschema
        {
            return true;
        }
    }
    false
}

/// Prove a subset relation for the complement of a two-arm xor where one arm
/// is itself a complement: `not(oneOf[A, not B])`.
///
/// Semantically this is `(A && !B) || (!A && B)`, i.e. the symmetric
/// difference of `A` and `B`, so it is always contained in `A || B`. We first
/// try that general union bound, then fall back to smaller comparable/disjoint
/// normal forms. In each case we discharge the remaining containment(s) with
/// the ordinary prover. This catches generated xor/complement partitions without treating
/// arbitrary negated oneOf as a union.
pub(super) fn negated_oneof_complement_pair_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    for not_index in 0..2 {
        let SchemaNodeKind::Not(inner_b) = branches[not_index].kind() else {
            continue;
        };
        let a = &branches[1 - not_index];
        let b = inner_b;

        // In full generality this negated xor is the symmetric difference
        // `A xor B`, hence it is always contained in `A || B`. If both sides
        // independently imply the target, no relationship between A and B is
        // needed.
        let a_le_sup =
            analyze_subschema_with_context(a, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema;
        let b_le_sup =
            analyze_subschema_with_context(b, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema;
        if a_le_sup && b_le_sup {
            return true;
        }

        // If both sides are finite, evaluate the exact symmetric-difference
        // candidates concretely. Unknown evaluator results fail closed.
        if let (Some(mut a_values), Some(b_values)) = (
            finite_schema_value_superset(a),
            finite_schema_value_superset(b),
        ) {
            for value in b_values {
                if !a_values
                    .iter()
                    .any(|existing| json_values_equal(existing, &value))
                {
                    a_values.push(value);
                }
            }
            let mut all_known = true;
            let mut all_live_in_sup = true;
            for value in &a_values {
                let a_accepts = context.superset_contains_value(a, value);
                let a_rejects = context.schema_definitely_rejects_value(a, value);
                let b_accepts = context.superset_contains_value(b, value);
                let b_rejects = context.schema_definitely_rejects_value(b, value);
                if (!a_accepts && !a_rejects) || (!b_accepts && !b_rejects) {
                    all_known = false;
                    break;
                }
                if a_accepts != b_accepts && !context.superset_contains_value(sup, value) {
                    all_live_in_sup = false;
                    break;
                }
            }
            if all_known && all_live_in_sup {
                return true;
            }
        }

        let a_le_b = analyze_subschema_with_context(a, b, context, ExplanationMode::VerdictOnly)
            .is_subschema;
        if a_le_b
            && analyze_subschema_with_context(b, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
        {
            return true;
        }

        let b_le_a = analyze_subschema_with_context(b, a, context, ExplanationMode::VerdictOnly)
            .is_subschema;
        if b_le_a
            && analyze_subschema_with_context(a, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
        {
            return true;
        }

        let a_mask = possible_json_type_mask(a);
        if a_mask == 0 {
            if analyze_subschema_with_context(b, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
            {
                return true;
            }
        } else if schemas_definitely_disjoint_for_partition(a, a_mask, b, context)
            && analyze_subschema_with_context(a, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
            && analyze_subschema_with_context(b, sup, context, ExplanationMode::VerdictOnly)
                .is_subschema
        {
            return true;
        }
    }
    false
}

/// For `not(oneOf[P, Q])`, prove the xor excludes an infinite comparable gap
/// of a mixed-xor target. If the gap is `Domain \ Kept`, one xor arm covering
/// all of Domain and the other intersecting Domain only inside Kept suffices.
pub(super) fn negated_xor_covers_mixed_comparable_gap(
    branches: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    fn finite_intersection_subset_of(
        finite_side: &SchemaNode,
        domain: &SchemaNode,
        kept: &SchemaNode,
        context: &mut SubschemaCheckContext,
    ) -> bool {
        let Some(values) = finite_schema_value_superset(finite_side) else {
            return false;
        };
        for value in values {
            if context.schema_definitely_rejects_value(finite_side, &value)
                || context.schema_definitely_rejects_value(domain, &value)
            {
                continue;
            }
            // If this value may lie in both sides, it must definitely be kept.
            if !context.superset_contains_value(kept, &value) {
                return false;
            }
        }
        true
    }

    if branches.len() != 2 {
        return false;
    }
    let p = unwrap_singleton_applicators(&branches[0]);
    let q = unwrap_singleton_applicators(&branches[1]);
    let SchemaNodeKind::OneOf(target_branches) = unwrap_singleton_applicators(sup).kind() else {
        return false;
    };
    if target_branches.len() != 2 {
        return false;
    }
    for neg_index in 0..2 {
        let neg_arm = unwrap_singleton_applicators(&target_branches[neg_index]);
        let SchemaNodeKind::Not(d_raw) = neg_arm.kind() else {
            continue;
        };
        let d = unwrap_singleton_applicators(d_raw);
        let c = unwrap_singleton_applicators(&target_branches[1 - neg_index]);
        for (domain, kept) in [(c, d), (d, c)] {
            if !analyze_subschema_with_context(kept, domain, context, ExplanationMode::VerdictOnly)
                .is_subschema
            {
                continue;
            }
            if analyze_subschema_with_context(domain, q, context, ExplanationMode::VerdictOnly)
                .is_subschema
                && finite_intersection_subset_of(p, domain, kept, context)
            {
                return true;
            }
            if analyze_subschema_with_context(domain, p, context, ExplanationMode::VerdictOnly)
                .is_subschema
                && finite_intersection_subset_of(q, domain, kept, context)
            {
                return true;
            }
        }
    }
    false
}

/// General finite-gap check for `not X <= oneOf[C, not D]` when C and D are
/// comparable. The mixed xor rejects exactly the finite difference between
/// the larger and smaller side; prove X definitely accepts every such value.
pub(super) fn negated_schema_excludes_mixed_finite_gap(
    excluded: &SchemaNode,
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    fn gap_rejected_by_negation(
        domain: &SchemaNode,
        kept: &SchemaNode,
        excluded: &SchemaNode,
        context: &mut SubschemaCheckContext,
    ) -> bool {
        let Some(values) = finite_schema_value_superset(domain) else {
            return false;
        };
        for value in values {
            if context.superset_contains_value(kept, &value)
                || context.schema_definitely_rejects_value(domain, &value)
            {
                continue;
            }
            if !context.superset_contains_value(excluded, &value) {
                return false;
            }
        }
        true
    }

    let SchemaNodeKind::OneOf(target_branches) = unwrap_singleton_applicators(sup).kind() else {
        return false;
    };
    if target_branches.len() != 2 {
        return false;
    }
    for neg_index in 0..2 {
        let neg_arm = unwrap_singleton_applicators(&target_branches[neg_index]);
        let SchemaNodeKind::Not(d_raw) = neg_arm.kind() else {
            continue;
        };
        let d = unwrap_singleton_applicators(d_raw);
        let c = unwrap_singleton_applicators(&target_branches[1 - neg_index]);
        if analyze_subschema_with_context(d, c, context, ExplanationMode::VerdictOnly).is_subschema
            && gap_rejected_by_negation(c, d, excluded, context)
        {
            return true;
        }
        if analyze_subschema_with_context(c, d, context, ExplanationMode::VerdictOnly).is_subschema
            && gap_rejected_by_negation(d, c, excluded, context)
        {
            return true;
        }
    }
    false
}

/// Prove `not(anyOf[arms...]) <= oneOf[C, not D]` in the finite remainder
/// case. If D <= C, the target rejects exactly C \ D; every such finite
/// candidate must be covered by one of the excluded union arms.
pub(super) fn negated_union_subset_of_mixed_difference(
    arms: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    fn gap_covered(
        domain: &SchemaNode,
        kept: &SchemaNode,
        arms: &[SchemaNode],
        context: &mut SubschemaCheckContext,
    ) -> bool {
        let Some(values) = finite_schema_value_superset(domain) else {
            return false;
        };
        'values: for value in values {
            if context.superset_contains_value(kept, &value)
                || context.schema_definitely_rejects_value(domain, &value)
            {
                continue;
            }
            for arm in arms {
                if context.superset_contains_value(arm, &value) {
                    continue 'values;
                }
            }
            return false;
        }
        true
    }

    if arms.is_empty() {
        return false;
    }
    let SchemaNodeKind::OneOf(target_branches) = unwrap_singleton_applicators(sup).kind() else {
        return false;
    };
    if target_branches.len() != 2 {
        return false;
    }
    for neg_index in 0..2 {
        let neg_arm = unwrap_singleton_applicators(&target_branches[neg_index]);
        let SchemaNodeKind::Not(d_raw) = neg_arm.kind() else {
            continue;
        };
        let d = unwrap_singleton_applicators(d_raw);
        let c = unwrap_singleton_applicators(&target_branches[1 - neg_index]);
        // If D <= C, the xor rejects C \ D. If C <= D, it rejects D \ C.
        if analyze_subschema_with_context(d, c, context, ExplanationMode::VerdictOnly).is_subschema
            && gap_covered(c, d, arms, context)
        {
            return true;
        }
        if analyze_subschema_with_context(c, d, context, ExplanationMode::VerdictOnly).is_subschema
            && gap_covered(d, c, arms, context)
        {
            return true;
        }
    }
    false
}

/// Handle `not(oneOf[not A, not B]) <= oneOf[C, not D]` for the common
/// finite-difference case.  When A and B are disjoint, the left side is
/// `not(A || B)`.  When D is contained in C, the mixed-xor target is
/// `D || not C`, i.e. it rejects only `C \ D`.  Enumerating a finite
/// over-approximation of C and requiring every possible remainder value to be
/// covered by A or B is a conservative proof of containment.
pub(super) fn negated_complement_pair_subset_of_mixed_difference(
    branches: &[SchemaNode],
    sup: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    let left0 = unwrap_singleton_applicators(&branches[0]);
    let left1 = unwrap_singleton_applicators(&branches[1]);
    // Two shapes reduce to the complement of a disjoint union: a negated xor
    // of disjoint positive arms, and a negated xor of two complemented arms
    // whose inners are disjoint (`xor(!A, !B) == A || B`).
    let mut candidates = vec![(left0, left1)];
    if let (SchemaNodeKind::Not(a_raw), SchemaNodeKind::Not(b_raw)) = (left0.kind(), left1.kind()) {
        candidates.push((
            unwrap_singleton_applicators(a_raw),
            unwrap_singleton_applicators(b_raw),
        ));
    }

    let SchemaNodeKind::OneOf(target_branches) = unwrap_singleton_applicators(sup).kind() else {
        return false;
    };
    if target_branches.len() != 2 {
        return false;
    }

    for (a, b) in candidates {
        if !schemas_definitely_disjoint_for_negation(a, b, context) {
            continue;
        }

        for neg_index in 0..2 {
            let neg_arm = unwrap_singleton_applicators(&target_branches[neg_index]);
            let SchemaNodeKind::Not(d_raw) = neg_arm.kind() else {
                continue;
            };
            let d = unwrap_singleton_applicators(d_raw);
            let c = unwrap_singleton_applicators(&target_branches[1 - neg_index]);

            // Restrict to the simple identity `oneOf[C, not D] == D || not C`.
            if !analyze_subschema_with_context(d, c, context, ExplanationMode::VerdictOnly)
                .is_subschema
            {
                continue;
            }
            let Some(values) = finite_schema_value_superset(c) else {
                continue;
            };

            let mut ok = true;
            for value in values {
                if context.superset_contains_value(d, &value) {
                    continue;
                }
                if context.schema_definitely_rejects_value(c, &value) {
                    continue;
                }
                if !(context.superset_contains_value(a, &value)
                    || context.superset_contains_value(b, &value))
                {
                    ok = false;
                    break;
                }
            }
            if ok {
                return true;
            }
        }
    }
    false
}

/// Prove the generic two-arm identity for a complemented xor: values accepted
/// by both arms, or by neither arm, are accepted by `not(oneOf[A, B])`.
pub(super) fn negated_two_arm_oneof_contains(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }
    let a = &branches[0];
    let b = &branches[1];

    let in_a =
        analyze_subschema_with_context(sub, a, context, ExplanationMode::VerdictOnly).is_subschema;
    let in_b =
        analyze_subschema_with_context(sub, b, context, ExplanationMode::VerdictOnly).is_subschema;
    if in_a && in_b {
        return true;
    }

    // The "neither" side is also safe when we can prove disjointness from both
    // arms.  Keep this as a fallback because disjointness may recurse through
    // finite/value reasoning and is a little more expensive than implication.
    !in_a
        && !in_b
        && schemas_definitely_disjoint_for_negation(sub, a, context)
        && schemas_definitely_disjoint_for_negation(sub, b, context)
}

/// Prove a subset is contained by `not(oneOf[not A, B])` in the simple
/// disjoint case.  If A and B cannot overlap, the complemented xor is exactly
/// `A ∪ B`; therefore any schema known to fit either side is accepted.
pub(super) fn negated_oneof_complement_pair_contains(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    for neg_idx in 0..2 {
        let pos_idx = 1 - neg_idx;
        let SchemaNodeKind::Not(a) = branches[neg_idx].kind() else {
            continue;
        };
        let a = unwrap_singleton_applicators(a);
        let b = &branches[pos_idx];
        if !schemas_definitely_disjoint_for_negation(a, b, context) {
            continue;
        }
        if analyze_subschema_with_context(sub, a, context, ExplanationMode::VerdictOnly)
            .is_subschema
            || analyze_subschema_with_context(sub, b, context, ExplanationMode::VerdictOnly)
                .is_subschema
        {
            return true;
        }
    }
    false
}

/// Return true for the exact degenerate xor `oneOf: [A, A]`.
///
/// This is intentionally syntactic (plus the tiny equivalence fast path) so it
/// cannot turn overlapping-but-not-identical branches into an empty language.
pub(super) fn one_of_pair_is_syntactically_empty(branches: &[SchemaNode]) -> bool {
    if branches.len() != 2 {
        return false;
    }
    (branches[0] == branches[1] || schemas_obviously_equivalent(&branches[0], &branches[1]))
        || (schema_is_locally_empty_for_finite_enumeration(&branches[0])
            && schema_is_locally_empty_for_finite_enumeration(&branches[1]))
}

/// If all but one branch of a oneOf are locally empty, the xor is exactly the
/// remaining branch; if every branch is empty, the xor is empty.  Returns
/// `Some(None)` for the all-empty case and `Some(Some(branch))` for one live
/// branch. Wider live sets are left untouched.
pub(super) fn one_of_trivial_live_branch(branches: &[SchemaNode]) -> Option<Option<&SchemaNode>> {
    if branches.is_empty() {
        return Some(None);
    }
    let mut live: Option<&SchemaNode> = None;
    for branch in branches {
        if schema_is_locally_empty_for_finite_enumeration(branch) {
            continue;
        }
        if live.is_some() {
            return None;
        }
        live = Some(branch);
    }
    Some(live)
}

/// For a two-arm xor with one definitely empty arm, return the live arm.
///
/// Restrict this to local, non-recursive emptiness facts. A false arm has no
/// effect on xor parity, so `oneOf(false, A)` is exactly `A`.
pub(super) fn one_of_pair_single_live_branch(branches: &[SchemaNode]) -> Option<&SchemaNode> {
    if branches.len() != 2 {
        return None;
    }
    let left_empty = schema_is_locally_empty_for_finite_enumeration(&branches[0]);
    let right_empty = schema_is_locally_empty_for_finite_enumeration(&branches[1]);
    match (left_empty, right_empty) {
        (true, false) => Some(&branches[1]),
        (false, true) => Some(&branches[0]),
        // If both are empty the xor is empty; the caller's empty-pair fast path
        // handles syntactically equal false arms, and otherwise falling through
        // remains conservative.
        _ => None,
    }
}

/// Prove the exact two-arm identity `oneOf: [A, not A]` (up to conservative
/// mutual-subset equivalence) is universal.  This catches infinite partitions
/// such as strings vs non-strings without treating arbitrary `oneOf` as a
/// union: with exactly two arms, equivalence of the positive arm and the
/// negated arm's exclusion guarantees every value matches exactly one arm.
pub(super) fn one_of_complement_pair_is_universal(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    if branches.len() != 2 {
        return false;
    }

    for not_index in 0..2 {
        let SchemaNodeKind::Not(excluded) = branches[not_index].kind() else {
            continue;
        };
        let positive = &branches[1 - not_index];
        let positive_implies_excluded = analyze_subschema_with_context(
            positive,
            excluded,
            context,
            ExplanationMode::VerdictOnly,
        )
        .is_subschema;
        if !positive_implies_excluded {
            continue;
        }
        let excluded_implies_positive = analyze_subschema_with_context(
            excluded,
            positive,
            context,
            ExplanationMode::VerdictOnly,
        )
        .is_subschema;
        if excluded_implies_positive {
            return true;
        }
    }
    false
}

/// Prove a `oneOf` is universal when it consists of disjoint positive
/// siblings plus the complement of their union.  This recognizes generated
/// partitions such as `[string, number, not(anyOf[string, number])]` without
/// assuming arbitrary `oneOf` behaves like `anyOf`.
pub(super) fn one_of_complement_union_partition_is_universal(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    for (not_index, branch) in branches.iter().enumerate() {
        let SchemaNodeKind::Not(excluded_union) = branch.kind() else {
            continue;
        };
        let union_children = match excluded_union.kind() {
            SchemaNodeKind::AnyOf(children) | SchemaNodeKind::OneOf(children) => children,
            _ => continue,
        };

        let sibling_indices: Vec<usize> = (0..branches.len())
            .filter(|&index| index != not_index)
            .collect();
        if sibling_indices.is_empty() {
            continue;
        }

        // Outside the excluded union, only the complement branch may match.
        if !sibling_indices.iter().all(|&index| {
            analyze_subschema_with_context(
                &branches[index],
                excluded_union,
                context,
                ExplanationMode::VerdictOnly,
            )
            .is_subschema
        }) {
            continue;
        }

        // Inside the union, at most one positive sibling may match.
        let pairwise_disjoint = sibling_indices
            .iter()
            .enumerate()
            .all(|(pos, &left_index)| {
                sibling_indices[pos + 1..].iter().all(|&right_index| {
                    let left = &branches[left_index];
                    let right = &branches[right_index];
                    let mask = possible_json_type_mask(left);
                    mask == 0
                        || schemas_definitely_disjoint_for_partition(left, mask, right, context)
                })
            });
        if !pairwise_disjoint {
            continue;
        }

        // Every arm of the excluded union must flow into some positive
        // sibling, giving coverage of the inside region.
        if union_children.iter().all(|child| {
            sibling_indices.iter().any(|&index| {
                analyze_subschema_with_context(
                    child,
                    &branches[index],
                    context,
                    ExplanationMode::VerdictOnly,
                )
                .is_subschema
            })
        }) {
            return true;
        }
    }
    false
}

/// Prove a finite complement partition written as `oneOf` is universal.
///
/// For a branch `not B`, require every sibling to be a subset of B, then use a
/// finite upper bound for B to prove each live B-candidate is accepted by
/// exactly one sibling (one definite accept, definite rejection by the rest).
/// Values outside B then satisfy only the complement branch, so the `oneOf`
/// accepts every instance.  All checks are conservative: unsupported concrete
/// evaluation or subset facts simply make the proof fail.
pub(super) fn one_of_finite_complement_partition_is_universal(
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    for (not_index, branch) in branches.iter().enumerate() {
        let SchemaNodeKind::Not(excluded) = branch.kind() else {
            continue;
        };
        let Some(values) = finite_schema_value_superset(excluded) else {
            continue;
        };

        let sibling_indices: Vec<usize> = (0..branches.len())
            .filter(|&index| index != not_index)
            .collect();
        if sibling_indices.is_empty() {
            continue;
        }

        // Keep complement branch exclusive: no sibling may accept a value
        // outside B. Delegate that implication to the conservative subset
        // prover rather than trying to reason from concrete samples.
        if !sibling_indices.iter().all(|&index| {
            analyze_subschema_with_context(
                &branches[index],
                excluded,
                context,
                ExplanationMode::VerdictOnly,
            )
            .is_subschema
        }) {
            continue;
        }

        let partitions_finite_side = values.iter().all(|value| {
            if context.schema_definitely_rejects_value(excluded, value) {
                return true;
            }

            let mut definite_accepts = 0usize;
            for &index in &sibling_indices {
                let sibling = &branches[index];
                if context.superset_contains_value(sibling, value) {
                    definite_accepts += 1;
                    if definite_accepts > 1 {
                        return false;
                    }
                } else if !context.schema_definitely_rejects_value(sibling, value) {
                    // Unknown membership could hide either a coverage gap or
                    // an overlap, so do not claim an exact oneOf partition.
                    return false;
                }
            }
            definite_accepts == 1
        });

        if partitions_finite_side {
            return true;
        }
    }
    false
}

pub(super) const JSON_TYPE_NULL: u8 = 1 << 0;
pub(super) const JSON_TYPE_BOOL: u8 = 1 << 1;
pub(super) const JSON_TYPE_NUMBER: u8 = 1 << 2;
pub(super) const JSON_TYPE_STRING: u8 = 1 << 3;
pub(super) const JSON_TYPE_ARRAY: u8 = 1 << 4;
pub(super) const JSON_TYPE_OBJECT: u8 = 1 << 5;
pub(super) const JSON_TYPE_ALL: u8 = JSON_TYPE_NULL
    | JSON_TYPE_BOOL
    | JSON_TYPE_NUMBER
    | JSON_TYPE_STRING
    | JSON_TYPE_ARRAY
    | JSON_TYPE_OBJECT;

// Whole-type recognizers used by the lightweight union/complement shortcuts below.

// If a negated union explicitly contains whole-type arms for every type except
// a small remainder, then the negation can only produce values in that
// remainder.  When the target accepts each remaining type wholesale, this is a
// sound subset proof.  This is especially useful for parsed type-specific
// assertions without an explicit `type` (e.g. `{ "maximum": 1 }` lowers to a
// union of all non-number types plus the bounded-number arm; negating it
// forces a number).

pub(super) fn complement_u64_count_halfline(range: CountRange<u64>) -> Option<CountRange<u64>> {
    match (range.min(), range.max()) {
        (0, None) => None,
        (min, None) => min
            .checked_sub(1)
            .and_then(|max| CountRange::new(0, Some(max))),
        (0, Some(max)) => max.checked_add(1).map(CountRange::unbounded_from),
        (_, Some(_)) => None,
    }
}

pub(super) fn complement_usize_count_halfline(
    range: CountRange<usize>,
) -> Option<CountRange<usize>> {
    match (range.min(), range.max()) {
        (0, None) => None, // complement is empty; callers handle as vacuous true
        (min, None) => min
            .checked_sub(1)
            .and_then(|max| CountRange::new(0, Some(max))),
        (0, Some(max)) => max.checked_add(1).map(CountRange::unbounded_from),
        (_, Some(_)) => None,
    }
}

/// Recognize negated untyped `minItems`/`maxItems` half-lines.
pub(super) fn negated_untyped_array_count_halfline_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
) -> bool {
    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_STRING,
        JSON_TYPE_OBJECT,
    ] {
        if !branches
            .iter()
            .any(|b| schema_obviously_accepts_json_type(b, bit))
        {
            return false;
        }
    }
    if !array_schema_is_plain_count(sup) {
        return false;
    }
    let SchemaNodeKind::Array {
        item_count: sup_count,
        ..
    } = sup.kind()
    else {
        return false;
    };
    for branch in branches {
        if !array_schema_is_plain_count(branch) {
            continue;
        }
        let SchemaNodeKind::Array { item_count, .. } = branch.kind() else {
            continue;
        };
        if item_count.min() == 0 && item_count.max().is_none() {
            return true; // excluded union is universal for arrays too; complement empty
        }
        if let Some(complement) = complement_u64_count_halfline(*item_count)
            && sup_count.contains_range(complement)
        {
            return true;
        }
    }
    false
}

/// Recognize negated untyped `minProperties`/`maxProperties` half-lines.
pub(super) fn negated_untyped_object_count_halfline_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
) -> bool {
    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_STRING,
        JSON_TYPE_ARRAY,
    ] {
        if !branches
            .iter()
            .any(|b| schema_obviously_accepts_json_type(b, bit))
        {
            return false;
        }
    }
    if !object_schema_is_plain_count(sup) {
        return false;
    }
    let SchemaNodeKind::Object {
        property_count: sup_count,
        ..
    } = sup.kind()
    else {
        return false;
    };
    for branch in branches {
        if !object_schema_is_plain_count(branch) {
            continue;
        }
        let SchemaNodeKind::Object { property_count, .. } = branch.kind() else {
            continue;
        };
        if property_count.min() == 0 && property_count.max().is_none() {
            return true;
        }
        if let Some(complement) = complement_usize_count_halfline(*property_count)
            && sup_count.contains_range(complement)
        {
            return true;
        }
    }
    false
}

/// Recognize the complement of a canonicalized untyped string length half-line.
pub(super) fn negated_untyped_string_length_halfline_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
) -> bool {
    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_ARRAY,
        JSON_TYPE_OBJECT,
    ] {
        if !branches
            .iter()
            .any(|branch| schema_obviously_accepts_json_type(branch, bit))
        {
            return false;
        }
    }

    let SchemaNodeKind::String {
        length: sup_length,
        pattern: None,
        format: None,
        enumeration: None,
    } = sup.kind()
    else {
        return false;
    };

    for branch in branches {
        let SchemaNodeKind::String {
            length,
            pattern: None,
            format: None,
            enumeration: None,
        } = branch.kind()
        else {
            continue;
        };

        let complement = match (length.min(), length.max()) {
            // Excluding strings of length >= min leaves 0..min-1.  min=0 is
            // the universal string branch, whose complement is empty (vacuous).
            (0, None) => return true,
            (min, None) => CountRange::new(0_u64, min.checked_sub(1)),
            // Excluding strings of length <= max leaves max+1..
            (0, Some(max)) => max.checked_add(1).map(CountRange::unbounded_from),
            // A bounded middle interval has a two-sided complement; skip it.
            (_, Some(_)) => None,
        };
        if let Some(complement) = complement
            && sup_length.contains_range(complement)
        {
            return true;
        }
    }
    false
}

/// Recognize the complement of a canonicalized untyped numeric half-line.
///
/// A raw schema like `{ "minimum": 0 }` canonicalizes to an anyOf that accepts
/// every non-number type plus a bounded `type:number` branch. Negating that
/// union leaves only the opposite numeric half-line. We require explicit whole
/// non-number coverage and a plain numeric branch (no enum/multipleOf), so the
/// derived interval is an over-approximation of the negated language even if
/// there are additional numeric branches in the union.
pub(super) fn negated_untyped_numeric_halfline_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
) -> bool {
    // All non-number JSON types must be swallowed by the excluded union.
    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_STRING,
        JSON_TYPE_ARRAY,
        JSON_TYPE_OBJECT,
    ] {
        if !branches
            .iter()
            .any(|branch| schema_obviously_accepts_json_type(branch, bit))
        {
            return false;
        }
    }

    let SchemaNodeKind::Number {
        multiple_of: None,
        enumeration: None,
        ..
    } = sup.kind()
    else {
        return false;
    };
    let Some(sup_interval) = numeric_interval_bound(sup) else {
        return false;
    };

    for branch in branches {
        let SchemaNodeKind::Number {
            bounds,
            multiple_of: None,
            enumeration: None,
        } = branch.kind()
        else {
            continue;
        };
        let excluded = NumericInterval {
            lower: match bounds.lower() {
                NumberBound::Unbounded => None,
                NumberBound::Inclusive(value) => Some(NumericIntervalBound {
                    value,
                    inclusive: true,
                }),
                NumberBound::Exclusive(value) => Some(NumericIntervalBound {
                    value,
                    inclusive: false,
                }),
            },
            upper: match bounds.upper() {
                NumberBound::Unbounded => None,
                NumberBound::Inclusive(value) => Some(NumericIntervalBound {
                    value,
                    inclusive: true,
                }),
                NumberBound::Exclusive(value) => Some(NumericIntervalBound {
                    value,
                    inclusive: false,
                }),
            },
            empty: false,
        };

        // Only a half-line has a single-interval complement.
        let complement = match (excluded.lower, excluded.upper) {
            (Some(lower), None) => NumericInterval {
                lower: None,
                upper: Some(NumericIntervalBound {
                    value: lower.value,
                    inclusive: !lower.inclusive,
                }),
                empty: false,
            },
            (None, Some(upper)) => NumericInterval {
                lower: Some(NumericIntervalBound {
                    value: upper.value,
                    inclusive: !upper.inclusive,
                }),
                upper: None,
                empty: false,
            },
            _ => continue,
        };
        if numeric_interval_contains(sup_interval, complement) {
            return true;
        }
    }
    false
}

pub(super) fn negated_union_type_remainder_subset_of(
    branches: &[SchemaNode],
    sup: &SchemaNode,
) -> bool {
    let mut remainder = JSON_TYPE_ALL;
    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_STRING,
        JSON_TYPE_ARRAY,
        JSON_TYPE_OBJECT,
    ] {
        if branches
            .iter()
            .any(|branch| schema_obviously_accepts_json_type(branch, bit))
        {
            remainder &= !bit;
        }
    }
    if remainder == 0 {
        return true;
    }
    [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_STRING,
        JSON_TYPE_ARRAY,
        JSON_TYPE_OBJECT,
    ]
    .into_iter()
    .filter(|bit| remainder & *bit != 0)
    .all(|bit| schema_obviously_accepts_json_type(sup, bit))
}

/// Prove a schema is covered by an `anyOf` whose branches obviously accept
/// whole JSON value types. This is intentionally much weaker than general
/// union reasoning: it only fires when every possible type of `sub` has a
/// branch that accepts *all* values of that type.
pub(super) fn any_of_obvious_type_cover_contains(
    sub: &SchemaNode,
    branches: &[SchemaNode],
) -> bool {
    let mask = possible_json_type_mask(sub);
    if mask == 0 {
        return true;
    }

    for bit in [
        JSON_TYPE_NULL,
        JSON_TYPE_BOOL,
        JSON_TYPE_NUMBER,
        JSON_TYPE_STRING,
        JSON_TYPE_ARRAY,
        JSON_TYPE_OBJECT,
    ] {
        if mask & bit == 0 {
            continue;
        }
        if !branches
            .iter()
            .any(|branch| schema_obviously_accepts_json_type(branch, bit))
        {
            return false;
        }
    }
    true
}

/// A small syntactic recognizer for schemas that accept every value of one JSON
/// type. Returning false is always conservative; returning true must mean the
/// entire type is admitted by raw JSON Schema validation.
pub(super) fn schema_obviously_accepts_json_type(schema: &SchemaNode, bit: u8) -> bool {
    fn inner(schema: &SchemaNode, bit: u8, active: &mut HashSet<NodeId>) -> bool {
        if !active.insert(schema.id()) {
            return false;
        }
        use SchemaNodeKind::*;
        let result = match schema.kind() {
            BoolSchema(true) | Any => true,
            BoolSchema(false) => false,
            // `null` has exactly one inhabitant, so a const-null schema admits
            // the whole null JSON type. Other const values are only a single
            // member of their type and cannot witness whole-type acceptance.
            Const(value) => bit == JSON_TYPE_NULL && value.is_null(),
            Enum(values) => match bit {
                JSON_TYPE_NULL => values.iter().any(|value| value.is_null()),
                JSON_TYPE_BOOL => {
                    values.iter().any(|value| value == &Value::Bool(false))
                        && values.iter().any(|value| value == &Value::Bool(true))
                }
                _ => false,
            },
            Not(child) if matches!(child.kind(), BoolSchema(false)) => true,
            // If a schema cannot possibly accept a JSON type, its negation
            // accepts that entire type. `possible_json_type_mask` is an upper
            // bound, so a missing bit is a sound whole-type fact.
            Not(child) if possible_json_type_mask(child) & bit == 0 => true,
            AllOf(children) => children.iter().all(|child| inner(child, bit, active)),
            AnyOf(children) => children.iter().any(|child| inner(child, bit, active)),
            String {
                length,
                pattern,
                format,
                enumeration,
            } => {
                bit == JSON_TYPE_STRING
                    && length.min() == 0
                    && length.max().is_none()
                    && pattern.is_none()
                    && format.is_none()
                    && enumeration.is_none()
            }
            Number {
                bounds,
                multiple_of,
                enumeration,
            } => {
                bit == JSON_TYPE_NUMBER
                    && bounds.lower() == NumberBound::Unbounded
                    && bounds.upper() == NumberBound::Unbounded
                    && multiple_of.is_none()
                    && enumeration.is_none()
            }
            Boolean { enumeration } => bit == JSON_TYPE_BOOL && enumeration.is_none(),
            Null { enumeration } => bit == JSON_TYPE_NULL && enumeration.is_none(),
            Object {
                properties,
                pattern_properties,
                required,
                additional,
                property_names,
                property_count,
                dependent_required,
                enumeration,
            } => {
                bit == JSON_TYPE_OBJECT
                    && properties.is_empty()
                    && pattern_properties.is_empty()
                    && required.is_empty()
                    && property_count.min() == 0
                    && property_count.max().is_none()
                    && dependent_required.is_empty()
                    && enumeration.is_none()
                    && schema_is_trivially_universal(additional)
                    && (schema_is_trivially_universal(property_names)
                        || inner(property_names, JSON_TYPE_STRING, active))
            }
            Array {
                prefix_items,
                items,
                item_count,
                contains,
                unique_items,
                enumeration,
            } => {
                bit == JSON_TYPE_ARRAY
                    && prefix_items.is_empty()
                    && item_count.min() == 0
                    && item_count.max().is_none()
                    && contains.is_none()
                    && !*unique_items
                    && enumeration.is_none()
                    && schema_is_trivially_universal(items)
            }
            // Integer schemas do not accept every JSON number (fractional
            // numbers remain), and conditionals/oneOf require more exactness
            // than this helper promises.
            Integer { .. } | OneOf(_) | IfThenElse { .. } | Not(_) => false,
            _ => false,
        };
        active.remove(&schema.id());
        result
    }

    inner(schema, bit, &mut HashSet::new())
}

/// Prove a subset is accepted by a `oneOf` whose alternatives are separated
/// by JSON value type. This intentionally does not try to reason about
/// overlapping numeric ranges or object tags; it only uses an upper bound on
/// the set of JSON types a schema can accept. If that upper bound is disjoint
/// from every non-selected branch, a value that fits the selected branch can
/// match exactly one branch.
pub(super) fn one_of_type_partition_contains(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
    mode: ExplanationMode,
) -> bool {
    let sub_mask = possible_json_type_mask(sub);
    if sub_mask == 0 {
        return true;
    }

    for (index, branch) in branches.iter().enumerate() {
        if !analyze_subschema_with_context(sub, branch, context, mode).is_subschema {
            continue;
        }

        let disjoint_from_others = branches.iter().enumerate().all(|(other_index, other)| {
            other_index == index
                || schemas_definitely_disjoint_for_partition(sub, sub_mask, other, context)
        });
        if disjoint_from_others {
            return true;
        }
    }

    finite_subset_values_fit_oneof_exactly(sub, branches, context)
}

/// Prove a finite-language subset is contained in an anyOf even when its
/// values are split across branches. We only rely on a finite upper bound for
/// the subset and definite positive membership in at least one branch, so
/// unsupported evaluators keep this conservative rather than unsound.
pub(super) fn finite_subset_values_fit_anyof(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    let Some(values) = finite_schema_value_superset(sub) else {
        return false;
    };

    values.into_iter().all(|value| {
        context.schema_definitely_rejects_value(sub, &value)
            || branches
                .iter()
                .any(|branch| context.superset_contains_value(branch, &value))
    })
}

/// Prove a finite-language subset is contained in a oneOf even when its
/// values are split across multiple branches. `finite_schema_value_superset`
/// is an upper bound, so checking every candidate is conservative; for each
/// candidate that might be accepted by `sub`, require one branch that is
/// definitely accepting and all other branches definitely rejecting. This
/// avoids treating overlapping oneOfs as unions while handling common enum /
/// const partitions.
pub(super) fn finite_subset_values_fit_oneof_exactly(
    sub: &SchemaNode,
    branches: &[SchemaNode],
    context: &mut SubschemaCheckContext,
) -> bool {
    let Some(values) = finite_schema_value_superset(sub) else {
        return false;
    };

    values.into_iter().all(|value| {
        if context.schema_definitely_rejects_value(sub, &value) {
            return true;
        }

        let mut accepting = 0usize;
        for branch in branches {
            if context.superset_contains_value(branch, &value) {
                accepting += 1;
                if accepting > 1 {
                    return false;
                }
            } else if !context.schema_definitely_rejects_value(branch, &value) {
                return false;
            }
        }
        accepting == 1
    })
}