helm-schema-gen 0.0.6

Generate an accurate JSON schema for any helm chart
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
use std::collections::{BTreeMap, BTreeSet};

use serde_json::{Map, Value};
use serde_yaml::Value as YamlValue;

use crate::merge::merge_two_schemas;
use crate::schema_node::{JsonSchemaType, SchemaNode, SchemaTypeKeyword, TypedSchemaNode};
use crate::values_yaml::{
    child_value_path, schema_node_from_yaml_value_with_skips, yaml_value_at_segments,
};

#[derive(Clone)]
pub(crate) struct SchemaDocument {
    root: SchemaNode,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CanonicalConstraintOutcome {
    Applied(CanonicalConstraintApplication),
    NotApplicable,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CanonicalConstraintApplication {
    Emitted,
    Redundant,
}

impl SchemaDocument {
    pub(crate) fn new_root_object() -> Self {
        Self {
            root: SchemaNode::closed_object(),
        }
    }

    pub(crate) fn visit_embedded_values(&self, visit: &mut impl FnMut(&Value)) {
        self.root.visit_foreign_values(visit);
    }

    pub(crate) fn insert_path_schema(
        &mut self,
        path_segments: &[String],
        schema: SchemaNode,
    ) -> usize {
        insert_schema_at_parts(&mut self.root, path_segments, schema)
    }

    /// Opens the root `global` property. Helm shares `global` values across
    /// the whole chart tree, so parent and sibling charts consume keys the
    /// analyzed chart never reads; closing the namespace to this chart's
    /// observed members would reject valid umbrella configurations.
    pub(crate) fn open_helm_global_namespace(&mut self) {
        let global = match &mut self.root {
            SchemaNode::Object { properties, .. } => properties.get_mut("global"),
            SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => keywords
                .properties
                .as_mut()
                .and_then(|properties| properties.get_mut("global")),
            _ => None,
        };
        if let Some(global) = global {
            global.open_object();
        }
    }

    pub(crate) fn replace_path_schema(
        &mut self,
        path_segments: &[String],
        schema: SchemaNode,
    ) -> usize {
        replace_schema_at_parts(&mut self.root, path_segments, schema)
    }

    /// Conjoins an unconditional generator constraint at an existing values
    /// slot. A missing direct slot is not created: the closed root may reject
    /// that name, so the caller must retain its root-anchored fallback.
    pub(crate) fn canonicalize_constraint_at_path(
        &mut self,
        path_segments: &[String],
        constraint: &Value,
    ) -> CanonicalConstraintOutcome {
        canonicalize_constraint_at_parts(&mut self.root, path_segments, constraint)
    }

    pub(crate) fn append_conditional(
        &mut self,
        ancestor_segments: &[String],
        condition: SchemaNode,
        then_schema: SchemaNode,
    ) {
        append_conditional_at_parts(&mut self.root, ancestor_segments, condition, then_schema);
    }

    /// Drops the structural `type: object` from a host whose members are
    /// only ever read through the nil-safe grouped form: the caller's
    /// presence-guarded arm carries the object requirement, and the base
    /// must render-match helm's tolerance for an absent or null-deleted
    /// receiver. Only the tree's own `Object` nodes relax — a `Foreign`
    /// base claiming `type: object` was resolved from independent
    /// evidence and keeps it.
    pub(crate) fn relax_host_object_type(&mut self, path_segments: &[String]) {
        relax_host_object_type(&mut self.root, path_segments);
    }

    /// Applies a wildcard member schema to keys that `values.yaml` already
    /// materialized as named properties.
    ///
    /// JSON Schema excludes named `properties` from
    /// `additionalProperties`. A direct range over a member identity owns
    /// both sets, so leaving the declared slots untouched would let their
    /// example shapes override the runtime member domain.
    pub(crate) fn materialize_declared_member_schema(
        &mut self,
        path_segments: &[String],
        declared: &YamlValue,
        member_schema: &Value,
    ) {
        let YamlValue::Mapping(declared) = declared else {
            return;
        };
        let keys = declared
            .keys()
            .filter_map(YamlValue::as_str)
            .collect::<Vec<_>>();
        if keys.is_empty() {
            return;
        }

        let root = std::mem::replace(&mut self.root, SchemaNode::empty());
        let mut root = root.into_value();
        visit_schema_values_at_path_mut(&mut root, path_segments, &mut |schema| {
            materialize_declared_properties(schema, &keys, member_schema);
        });
        self.root = SchemaNode::foreign(root);
    }

    #[tracing::instrument(skip_all)]
    pub(crate) fn merge_missing_values_yaml_defaults_under_roots(
        &mut self,
        values_yaml_doc: &YamlValue,
        root_paths: &[Vec<String>],
        skip_paths: &BTreeSet<Vec<String>>,
    ) -> usize {
        let mut insertions = Vec::new();
        for root_path in root_paths {
            let Some(yaml) = yaml_value_at_segments(values_yaml_doc, root_path) else {
                continue;
            };
            collect_missing_yaml_default_insertions(
                &self.root,
                root_path,
                yaml,
                skip_paths,
                &mut insertions,
            );
        }
        insertions
            .into_iter()
            .map(|(path, schema)| self.insert_path_schema(&path, schema))
            .sum()
    }

    pub(crate) fn into_value(self) -> Value {
        self.root.into_value()
    }
}

fn canonicalize_constraint_at_parts(
    node: &mut SchemaNode,
    path_segments: &[String],
    constraint: &Value,
) -> CanonicalConstraintOutcome {
    let Some((head, tail)) = path_segments.split_first() else {
        let is_object_constraint = constraint.as_object().is_some_and(|object| {
            object.len() == 1 && object.get("type").and_then(Value::as_str) == Some("object")
        });
        if is_object_constraint && let Some(outcome) = canonicalize_object_constraint(node) {
            return outcome;
        }
        if let Some(outcome) = apply_required_entries(node, constraint) {
            return outcome;
        }
        if !is_object_constraint && !is_not_null_constraint(constraint) {
            return CanonicalConstraintOutcome::NotApplicable;
        }
        if node.is_false_schema() || constraint_is_implied_by_node(node, constraint) {
            return CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant);
        }
        let existing = std::mem::replace(node, SchemaNode::empty());
        *node = SchemaNode::all_of(vec![existing, SchemaNode::foreign(constraint.clone())]);
        return CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Emitted);
    };

    match node {
        SchemaNode::Object { properties, .. } => properties
            .get_mut(head)
            .map_or(CanonicalConstraintOutcome::NotApplicable, |child| {
                canonicalize_constraint_at_parts(child, tail, constraint)
            }),
        SchemaNode::Array { items, .. } if head == "*" => items
            .as_deref_mut()
            .map_or(CanonicalConstraintOutcome::NotApplicable, |child| {
                canonicalize_constraint_at_parts(child, tail, constraint)
            }),
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => {
            let child = if head == "*" {
                keywords.items.as_deref_mut()
            } else {
                keywords
                    .properties
                    .as_mut()
                    .and_then(|properties| properties.get_mut(head))
            };
            child.map_or(CanonicalConstraintOutcome::NotApplicable, |child| {
                canonicalize_constraint_at_parts(child, tail, constraint)
            })
        }
        SchemaNode::Empty
        | SchemaNode::Array { .. }
        | SchemaNode::Typed(TypedSchemaNode::Boolean(_))
        | SchemaNode::Foreign(_) => CanonicalConstraintOutcome::NotApplicable,
    }
}

fn canonicalize_object_constraint(node: &mut SchemaNode) -> Option<CanonicalConstraintOutcome> {
    match node {
        SchemaNode::Empty | SchemaNode::Typed(TypedSchemaNode::Boolean(true)) => {
            *node = SchemaNode::type_named("object");
            Some(CanonicalConstraintOutcome::Applied(
                CanonicalConstraintApplication::Emitted,
            ))
        }
        SchemaNode::Object { typed, .. } => {
            let was_typed = *typed;
            *typed = true;
            Some(if was_typed {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant)
            } else {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Emitted)
            })
        }
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => {
            if keywords.schema_type == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object)) {
                return Some(CanonicalConstraintOutcome::Applied(
                    CanonicalConstraintApplication::Redundant,
                ));
            }
            None
        }
        SchemaNode::Typed(TypedSchemaNode::Boolean(false)) => Some(
            CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant),
        ),
        SchemaNode::Array { .. } | SchemaNode::Foreign(_) => None,
    }
}

fn apply_required_entries(
    node: &mut SchemaNode,
    constraint: &Value,
) -> Option<CanonicalConstraintOutcome> {
    let constraint = constraint.as_object()?;
    if constraint.len() != 2 || constraint.get("type").and_then(Value::as_str) != Some("object") {
        return None;
    }
    let required = constraint.get("required")?.as_array()?;
    let required = required
        .iter()
        .map(Value::as_str)
        .collect::<Option<Vec<_>>>()?;

    match node {
        SchemaNode::Object {
            typed,
            properties,
            required: existing,
            ..
        } => {
            if required.iter().any(|name| !properties.contains_key(*name)) {
                return Some(CanonicalConstraintOutcome::NotApplicable);
            }
            let before = existing.len();
            let was_typed = *typed;
            *typed = true;
            existing.extend(required.into_iter().map(str::to_string));
            Some(if was_typed && existing.len() == before {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant)
            } else {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Emitted)
            })
        }
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords))
            if keywords.schema_type == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object)) =>
        {
            let all_slots_exist = keywords.properties.as_ref().is_some_and(|properties| {
                required.iter().all(|name| properties.contains_key(*name))
            });
            if !all_slots_exist {
                return Some(CanonicalConstraintOutcome::NotApplicable);
            }
            if required.is_empty() {
                return Some(CanonicalConstraintOutcome::Applied(
                    CanonicalConstraintApplication::Redundant,
                ));
            }
            let existing = keywords.required.get_or_insert_with(Vec::new);
            let before = existing.len();
            for name in required {
                let name = name.to_string();
                if !existing.contains(&name) {
                    existing.push(name);
                }
            }
            existing.sort();
            Some(if existing.len() == before {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant)
            } else {
                CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Emitted)
            })
        }
        SchemaNode::Typed(TypedSchemaNode::Boolean(false)) => Some(
            CanonicalConstraintOutcome::Applied(CanonicalConstraintApplication::Redundant),
        ),
        SchemaNode::Empty
        | SchemaNode::Array { .. }
        | SchemaNode::Typed(_)
        | SchemaNode::Foreign(_) => Some(CanonicalConstraintOutcome::NotApplicable),
    }
}

fn constraint_is_implied_by_node(node: &SchemaNode, constraint: &Value) -> bool {
    let existing = node.clone().into_value();
    if is_not_null_constraint(constraint) {
        return crate::schema_model::schema_excludes_type(&existing, "null");
    }
    false
}

fn is_not_null_constraint(constraint: &Value) -> bool {
    constraint
        == &serde_json::json!({
            "not": { "type": "null" },
        })
}

fn relax_host_object_type(node: &mut SchemaNode, path_segments: &[String]) {
    let Some((head, tail)) = path_segments.split_first() else {
        match node {
            SchemaNode::Object { typed, .. } => *typed = false,
            SchemaNode::Typed(TypedSchemaNode::Keywords(keywords))
                if keywords.schema_type
                    == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object)) =>
            {
                keywords.schema_type = Some(SchemaTypeKeyword::Multiple(vec![
                    JsonSchemaType::Object,
                    JsonSchemaType::Null,
                ]));
            }
            _ => {}
        }
        return;
    };
    match node {
        SchemaNode::Object { properties, .. } => {
            if let Some(child) = properties.get_mut(head) {
                relax_host_object_type(child, tail);
            }
        }
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => {
            if let Some(child) = keywords
                .properties
                .as_mut()
                .and_then(|properties| properties.get_mut(head))
            {
                relax_host_object_type(child, tail);
            }
        }
        _ => {}
    }
}

pub(crate) fn draft07_root_document(root_schema: Value) -> Value {
    let mut out = Map::new();
    out.insert(
        "$schema".to_string(),
        Value::String("http://json-schema.org/draft-07/schema#".to_string()),
    );

    if let Value::Object(obj) = root_schema {
        for (k, v) in obj {
            out.insert(k, v);
        }
    } else {
        out.insert("type".to_string(), Value::String("object".to_string()));
        out.insert("properties".to_string(), Value::Object(Map::new()));
        out.insert("additionalProperties".to_string(), Value::Bool(false));
    }
    Value::Object(out)
}

pub(crate) fn insert_path_schema_value(
    root_schema: Value,
    path_segments: &[String],
    schema: Value,
) -> (Value, usize) {
    let mut root = SchemaNode::foreign(root_schema);
    let abstentions = insert_schema_at_parts(&mut root, path_segments, SchemaNode::foreign(schema));
    (root.into_value(), abstentions)
}

/// Conjoins one member schema with every array-item and map-value lane.
///
/// This is deliberately distinct from [`insert_path_schema_value`], whose
/// merge operation combines alternative inference evidence. A schema that
/// enriches a fail requirement is a logical conjunction; union-merging it
/// into the requirement can collapse separate `anyOf` arms.
pub(crate) fn conjoin_collection_member_schema_value(
    mut collection_schema: Value,
    member_schema: &Value,
) -> Value {
    conjoin_collection_member_schema(&mut collection_schema, member_schema);
    collection_schema
}

fn conjoin_collection_member_schema(collection_schema: &mut Value, member_schema: &Value) -> bool {
    let Some(object) = collection_schema.as_object_mut() else {
        return false;
    };

    let mut conjoined = false;
    for keyword in ["anyOf", "oneOf"] {
        if let Some(arms) = object.get_mut(keyword).and_then(Value::as_array_mut) {
            for arm in arms {
                conjoined |= conjoin_collection_member_schema(arm, member_schema);
            }
        }
    }

    if let Some(types) = object.get("type").and_then(Value::as_array) {
        let allows_array = types
            .iter()
            .any(|schema_type| schema_type.as_str() == Some("array"));
        let allows_object = types
            .iter()
            .any(|schema_type| schema_type.as_str() == Some("object"));
        if allows_array {
            let existing = object
                .remove("items")
                .unwrap_or_else(|| Value::Object(Map::new()));
            object.insert(
                "items".to_string(),
                conjoin_schema_values(existing, member_schema.clone()),
            );
        }
        if allows_object {
            if let Some(properties) = object.get_mut("properties").and_then(Value::as_object_mut) {
                for property in properties.values_mut() {
                    let existing = std::mem::take(property);
                    *property = conjoin_schema_values(existing, member_schema.clone());
                }
            }
            let existing = object
                .remove("additionalProperties")
                .unwrap_or_else(|| Value::Object(Map::new()));
            object.insert(
                "additionalProperties".to_string(),
                conjoin_schema_values(existing, member_schema.clone()),
            );
        }
        if allows_array || allows_object {
            return true;
        }
    }

    match object.get("type").and_then(Value::as_str) {
        Some("array") => {
            let existing = object
                .remove("items")
                .unwrap_or_else(|| Value::Object(Map::new()));
            object.insert(
                "items".to_string(),
                conjoin_schema_values(existing, member_schema.clone()),
            );
            true
        }
        Some("object") if object.get("additionalProperties") != Some(&Value::Bool(false)) => {
            if let Some(properties) = object.get_mut("properties").and_then(Value::as_object_mut) {
                for property in properties.values_mut() {
                    let existing = std::mem::take(property);
                    *property = conjoin_schema_values(existing, member_schema.clone());
                }
            }
            let existing = object
                .remove("additionalProperties")
                .unwrap_or_else(|| Value::Object(Map::new()));
            object.insert(
                "additionalProperties".to_string(),
                conjoin_schema_values(existing, member_schema.clone()),
            );
            true
        }
        _ => conjoined,
    }
}

fn conjoin_schema_values(existing: Value, incoming: Value) -> Value {
    if existing == incoming || crate::schema_model::is_empty_schema(&incoming) {
        return existing;
    }
    if schemas_equal_ignoring_noop_collection_keywords(&existing, &incoming) {
        return incoming;
    }
    if crate::schema_model::is_empty_schema(&existing) {
        return incoming;
    }
    if let Some(merged) = conjoin_type_only_object_with_carrier(&existing, &incoming)
        .or_else(|| conjoin_type_only_object_with_carrier(&incoming, &existing))
    {
        return merged;
    }
    SchemaNode::all_of(vec![
        SchemaNode::foreign(existing),
        SchemaNode::foreign(incoming),
    ])
    .into_value()
}

fn conjoin_type_only_object_with_carrier(typed: &Value, carrier: &Value) -> Option<Value> {
    let typed = typed.as_object()?;
    if typed.len() != 1 || typed.get("type").and_then(Value::as_str) != Some("object") {
        return None;
    }
    let carrier = carrier.as_object()?;
    if carrier.contains_key("type")
        || !carrier.keys().all(|key| {
            matches!(
                key.as_str(),
                "additionalProperties" | "allOf" | "patternProperties" | "properties" | "required"
            )
        })
    {
        return None;
    }
    let mut merged = carrier.clone();
    merged.insert("type".to_string(), Value::String("object".to_string()));
    Some(Value::Object(merged))
}

fn schemas_equal_ignoring_noop_collection_keywords(left: &Value, right: &Value) -> bool {
    fn strip_noop_collection_keywords(schema: &mut Value) {
        match schema {
            Value::Array(items) => {
                for item in items {
                    strip_noop_collection_keywords(item);
                }
            }
            Value::Object(object) => {
                for value in object.values_mut() {
                    strip_noop_collection_keywords(value);
                }
                for keyword in ["additionalProperties", "items"] {
                    if object
                        .get(keyword)
                        .is_some_and(crate::schema_model::is_empty_schema)
                    {
                        object.remove(keyword);
                    }
                }
            }
            Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
        }
    }

    let mut left = left.clone();
    let mut right = right.clone();
    strip_noop_collection_keywords(&mut left);
    strip_noop_collection_keywords(&mut right);
    left == right
}

fn conditional_entry(condition: Value, then_schema: SchemaNode) -> SchemaNode {
    SchemaNode::foreign(Value::Object(
        [
            ("if".to_string(), condition),
            ("then".to_string(), then_schema.into_value()),
        ]
        .into_iter()
        .collect(),
    ))
}

/// Whether a condition schema is a pure partition of the value's own TYPE
/// (`{type: object}`, also negated or a combination of such tests). Such a
/// condition deliberately selects one runtime kind out of several the chart
/// handles, so the conditional's carrier must not assert `type: object`
/// itself; a condition that tests members instead presupposes an object
/// host.
fn condition_is_value_type_partition(condition: &Value) -> bool {
    let Value::Object(object) = condition else {
        return false;
    };
    if object.len() != 1 {
        return false;
    }
    if let Some(inner) = object.get("not") {
        return condition_is_value_type_partition(inner);
    }
    if let Some(Value::Array(items)) = object.get("anyOf").or_else(|| object.get("allOf")) {
        return items.iter().all(condition_is_value_type_partition);
    }
    object.contains_key("type")
}

fn append_conditional_at_parts(
    node: &mut SchemaNode,
    path_segments: &[String],
    condition: SchemaNode,
    then_schema: SchemaNode,
) {
    let Some((head, tail)) = path_segments.split_first() else {
        push_conditional_entry(node, condition, then_schema, false);
        return;
    };

    // A `*` segment addresses EACH member value of a ranged map: the
    // conditional lands inside the node's `additionalProperties` member
    // slot, mirroring `insert_map_member_row` — a literal `"*"` property
    // would constrain nothing a chart can render, silently dropping the
    // guarded member contract (velero `schedules.*` rows).
    if head == "*" {
        if node.is_empty_slot() {
            *node = SchemaNode::untyped_member_host();
        }
        if let SchemaNode::Object {
            additional_properties,
            ..
        } = node
        {
            let member = additional_properties.get_or_insert_with(|| Box::new(SchemaNode::empty()));
            if !member.is_false_schema() {
                if tail.is_empty() {
                    push_conditional_entry(member, condition, then_schema, true);
                } else {
                    append_conditional_at_parts(member, tail, condition, then_schema);
                }
                return;
            }
        }
        if let SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) = node {
            let member = keywords
                .additional_properties
                .get_or_insert_with(|| Box::new(SchemaNode::empty()));
            if !member.is_false_schema() {
                if tail.is_empty() {
                    push_conditional_entry(member, condition, then_schema, true);
                } else {
                    append_conditional_at_parts(member, tail, condition, then_schema);
                }
                return;
            }
        }
    }

    if matches!(node, SchemaNode::Typed(_) | SchemaNode::Foreign(_)) && !node.is_empty_slot() {
        let mut fragment = SchemaNode::empty();
        append_conditional_at_parts(&mut fragment, path_segments, condition, then_schema);
        merge_conditional_fragment_into_foreign_slot(node, fragment);
        return;
    }

    // Pure navigation toward the conditional's anchor: a `with` chain skips
    // falsy ancestors, so every carrier level must hold vacuously for them
    // instead of asserting `type: object`.
    if node.is_empty_slot() {
        *node = SchemaNode::untyped_member_host();
    }
    let properties = ensure_object_properties(node);
    let child = properties.entry(head.clone()).or_insert_with(|| {
        if tail.is_empty() {
            SchemaNode::foreign(serde_json::json!({}))
        } else {
            SchemaNode::untyped_member_host()
        }
    });
    if tail.is_empty() {
        push_conditional_entry(child, condition, then_schema, true);
    } else {
        append_conditional_at_parts(child, tail, condition, then_schema);
    }
}

fn merge_conditional_fragment_into_foreign_slot(node: &mut SchemaNode, fragment: SchemaNode) {
    reconcile_node_host_with_branch_schema(node, &fragment);
    open_host_descendants_extended_by_schema(node, &fragment);
    // The fragment is a conditional constraint on the existing slot, not an
    // alternative value lane. A union here lets the old slot bypass every
    // descendant `if`/`then` carried by the fragment.
    node.push_all_of(fragment);
}

fn push_conditional_entry(
    node: &mut SchemaNode,
    condition: SchemaNode,
    then_schema: SchemaNode,
    open_host: bool,
) {
    if open_host {
        reconcile_node_host_with_branch_schema(node, &then_schema);
    }
    open_host_descendants_extended_by_schema(node, &then_schema);
    let condition = condition.into_value();
    let type_partition = condition_is_value_type_partition(&condition);
    // A trivially-true condition (an unconditional requirement, e.g. an
    // unguarded member-access contract) needs no `if`/`then` wrapper.
    let conditional = if crate::schema_model::is_empty_schema(&condition) {
        then_schema
    } else {
        conditional_entry(condition, then_schema)
    };

    if !matches!(
        node,
        SchemaNode::Object { .. } | SchemaNode::Typed(TypedSchemaNode::Keywords(_))
    ) {
        // A type-partition arm deliberately selects one runtime kind out of
        // several the chart handles, so its carrier holds vacuously for the
        // unselected kinds; every other conditional presupposes an object
        // host the same way its member tests do.
        *node = if type_partition {
            SchemaNode::untyped_member_host()
        } else {
            SchemaNode::unknown_object()
        };
    }
    node.push_all_of(conditional);
}

fn open_host_descendants_extended_by_schema(node: &mut SchemaNode, schema: &SchemaNode) {
    let branch_properties = schema.property_entries();
    if branch_properties.is_empty() {
        return;
    }

    for (property, branch_child) in branch_properties {
        let Some(mut host_child) = node.take_property(&property) else {
            continue;
        };
        reconcile_node_host_with_branch_schema(&mut host_child, &branch_child);
        open_host_descendants_extended_by_schema(&mut host_child, &branch_child);
        node.put_property(property, host_child);
    }
}

fn reconcile_node_host_with_branch_schema(host: &mut SchemaNode, branch: &SchemaNode) {
    if branch.opens_unknown_object_fields() {
        host.open_object();
        return;
    }

    if !branch_schema_extends_node_host(host, branch) {
        return;
    }

    if host.is_plain_closed_values_object() {
        add_branch_property_placeholders_to_node(host, branch);
    } else {
        host.open_object();
    }
}

fn branch_schema_extends_node_host(host: &SchemaNode, branch: &SchemaNode) -> bool {
    let branch_properties = branch.property_entries();
    if branch_properties.is_empty() {
        return false;
    }

    let host_properties = host
        .property_entries()
        .into_iter()
        .map(|(property, _)| property)
        .collect::<BTreeSet<_>>();
    branch_properties
        .iter()
        .any(|(property, _)| !host_properties.contains(property))
}

fn add_branch_property_placeholders_to_node(host: &mut SchemaNode, branch: &SchemaNode) {
    let branch_properties = branch.property_entries();
    if branch_properties.is_empty() {
        return;
    }

    let properties = ensure_object_properties(host);
    for (property, _) in branch_properties {
        properties.entry(property).or_insert_with(SchemaNode::empty);
    }
}

fn collect_missing_yaml_default_insertions(
    root_schema: &SchemaNode,
    current_path: &[String],
    yaml: &YamlValue,
    skip_paths: &BTreeSet<Vec<String>>,
    insertions: &mut Vec<(Vec<String>, SchemaNode)>,
) {
    if skip_paths.iter().any(|skip_path| {
        skip_path.len() < current_path.len() && current_path.starts_with(skip_path)
    }) {
        return;
    }
    if skip_paths.contains(current_path) {
        if !root_schema.path_exists(current_path) {
            insertions.push((current_path.to_vec(), SchemaNode::empty()));
        }
        return;
    }

    if let YamlValue::Mapping(mapping) = yaml
        && !mapping.is_empty()
        && root_schema.path_exists(current_path)
    {
        for (key, value_yaml) in mapping {
            let Some(key) = key.as_str() else {
                continue;
            };
            let child_path = child_value_path(current_path, key);
            collect_missing_yaml_default_insertions(
                root_schema,
                &child_path,
                value_yaml,
                skip_paths,
                insertions,
            );
        }
        return;
    }

    if !root_schema.path_exists(current_path)
        && let Some(schema) = schema_node_from_yaml_value_with_skips(yaml, current_path, skip_paths)
    {
        insertions.push((current_path.to_vec(), schema));
    }
}

pub(crate) fn apply_values_descriptions(root: &mut Value, descriptions: &BTreeMap<String, String>) {
    for (path, description) in descriptions {
        if description.trim().is_empty() {
            continue;
        }
        let path_segments = crate::split_value_path(path);
        visit_schema_values_at_path_mut(root, &path_segments, &mut |node| {
            set_schema_description(node, description);
        });
    }
}

fn visit_schema_values_at_path_mut(
    node: &mut Value,
    path_segments: &[String],
    visit: &mut impl FnMut(&mut Value),
) -> bool {
    if path_segments.is_empty() {
        visit(node);
        return true;
    }

    let Some(obj) = node.as_object_mut() else {
        return false;
    };

    let mut visited = false;
    for key in ["anyOf", "allOf", "oneOf"] {
        if let Some(Value::Array(variants)) = obj.get_mut(key) {
            for variant in variants {
                visited |= visit_schema_values_at_path_mut(variant, path_segments, visit);
            }
        }
    }
    for key in ["then", "else"] {
        if let Some(child) = obj.get_mut(key) {
            visited |= visit_schema_values_at_path_mut(child, path_segments, visit);
        }
    }

    let Some((head, tail)) = path_segments.split_first() else {
        return visited;
    };
    if head == "*" {
        if let Some(items) = obj.get_mut("items") {
            visited |= visit_schema_values_at_path_mut(items, tail, visit);
        }
        return visited;
    }

    if let Some(child) = obj
        .get_mut("properties")
        .and_then(Value::as_object_mut)
        .and_then(|properties| properties.get_mut(head))
    {
        visited |= visit_schema_values_at_path_mut(child, tail, visit);
    }
    visited
}

fn set_schema_description(node: &mut Value, description: &str) {
    if let Value::Object(obj) = node {
        obj.insert(
            "description".to_string(),
            Value::String(description.to_string()),
        );
    }
}

fn materialize_declared_properties(schema: &mut Value, keys: &[&str], member_schema: &Value) {
    let Some(object) = schema.as_object_mut() else {
        return;
    };
    for keyword in ["anyOf", "allOf", "oneOf"] {
        if let Some(arms) = object.get_mut(keyword).and_then(Value::as_array_mut) {
            for arm in arms {
                materialize_declared_properties(arm, keys, member_schema);
            }
        }
    }

    let object_lane = object.get("type").and_then(Value::as_str) == Some("object")
        || object.contains_key("properties")
        || object.contains_key("additionalProperties");
    if !object_lane {
        return;
    }
    let properties = object
        .entry("properties")
        .or_insert_with(|| Value::Object(Map::new()));
    let Some(properties) = properties.as_object_mut() else {
        return;
    };
    for key in keys {
        properties.insert((*key).to_string(), member_schema.clone());
    }
}

fn new_array_slot() -> SchemaNode {
    SchemaNode::array().items(SchemaNode::foreign(Value::Null))
}

fn ensure_array_items_schema(node: &mut SchemaNode) -> &mut SchemaNode {
    let is_array = matches!(node, SchemaNode::Array { .. })
        || matches!(
            node,
            SchemaNode::Typed(TypedSchemaNode::Keywords(keywords))
                if keywords.is_array_like()
        );
    if !is_array {
        *node = new_array_slot();
    }
    match node {
        SchemaNode::Array { items, .. } => items
            .get_or_insert_with(|| Box::new(SchemaNode::foreign(Value::Null)))
            .as_mut(),
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => keywords
            .items
            .get_or_insert_with(|| Box::new(SchemaNode::foreign(Value::Null)))
            .as_mut(),
        _ => ensure_array_items_schema(node),
    }
}

fn ensure_object_properties(node: &mut SchemaNode) -> &mut BTreeMap<String, SchemaNode> {
    let is_object = matches!(node, SchemaNode::Object { .. })
        || matches!(
            node,
            SchemaNode::Typed(TypedSchemaNode::Keywords(keywords))
            if keywords.schema_type
                == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object))
                || keywords.schema_type.is_none()
                    && (keywords.properties.is_some()
                        || keywords.additional_properties.is_some())
        );
    if !is_object {
        let is_empty_schema = matches!(
            node,
            SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) if keywords.is_empty_schema()
        );
        if is_empty_schema {
            *node = SchemaNode::untyped_member_host();
        } else {
            // Hosting a member under a non-object slot proves keys exist,
            // not that the member set is bounded: the coerced host stays
            // open (the strict root closure is created separately).
            *node = SchemaNode::unknown_object();
        }
    }
    match node {
        SchemaNode::Object { properties, .. } => properties,
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => {
            keywords.properties.get_or_insert_with(BTreeMap::new)
        }
        _ => ensure_object_properties(node),
    }
}

fn merge_into_schema_slot(slot: &mut SchemaNode, schema: SchemaNode) {
    if slot.is_empty_slot() {
        *slot = schema;
        return;
    }

    // A TYPELESS member-host carrier (`{"additionalProperties": …}` with no
    // type of its own) merged into an object-typed slot must not degrade
    // the slot into a union whose typeless alternative matches scalars
    // (jenkins' additionalAgents member-map contract): conjoin the
    // carrier's member slot into the object instead.
    if let SchemaNode::Typed(TypedSchemaNode::Keywords(carrier)) = &schema
        && carrier.schema_type.is_none()
        && carrier.any_of.is_none()
        && carrier.one_of.is_none()
        && carrier.additional_properties.is_some()
        && carrier.properties.is_none()
        && carrier.required.is_none()
        && carrier.items.is_none()
        && carrier.all_of.is_none()
        && carrier.not.is_none()
        && carrier.if_schema.is_none()
        && carrier.then_schema.is_none()
        && carrier.else_schema.is_none()
        && carrier.min_properties.is_none()
        && carrier.max_properties.is_none()
        && carrier.min_items.is_none()
        && carrier
            .extra_keywords
            .keys()
            .all(|key| key == "description")
        && let SchemaNode::Typed(TypedSchemaNode::Keywords(object)) = slot
        && object.schema_type == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object))
        && object
            .additional_properties
            .as_deref()
            .is_none_or(|additional| !additional.is_false_schema())
    {
        let carrier_member = carrier
            .additional_properties
            .as_deref()
            .cloned()
            .unwrap_or_else(SchemaNode::empty);
        let member = match object.additional_properties.take().map(|value| *value) {
            None | Some(SchemaNode::Typed(TypedSchemaNode::Boolean(true))) => carrier_member,
            Some(existing) => SchemaNode::from_value(merge_two_schemas(
                existing.into_value(),
                carrier_member.into_value(),
            )),
        };
        object.additional_properties = Some(Box::new(member));
        return;
    }
    if schema.opens_unknown_object_fields()
        || (schema.is_exact_empty_object() && slot.has_object_descendants())
    {
        slot.open_object();
    }

    let slot_opens_unknown_object_fields = slot.opens_unknown_object_fields();
    let existing = std::mem::replace(slot, SchemaNode::empty()).into_value();
    *slot = SchemaNode::foreign(merge_two_schemas(existing, schema.into_value()));
    if slot_opens_unknown_object_fields {
        slot.open_object();
    }
}

fn replace_schema_at_parts(
    node: &mut SchemaNode,
    path_segments: &[String],
    leaf: SchemaNode,
) -> usize {
    if replace_existing_schema_at_parts(node, path_segments, &leaf) {
        return 0;
    }
    insert_schema_at_parts(node, path_segments, leaf)
}

fn replace_existing_schema_at_parts(
    node: &mut SchemaNode,
    path_segments: &[String],
    leaf: &SchemaNode,
) -> bool {
    let Some((head, tail)) = path_segments.split_first() else {
        *node = leaf.clone();
        return true;
    };
    match node {
        SchemaNode::Object { properties, .. } => properties
            .get_mut(head)
            .is_some_and(|child| replace_existing_schema_at_parts(child, tail, leaf)),
        SchemaNode::Array { items, .. } if head == "*" => items
            .as_deref_mut()
            .is_some_and(|child| replace_existing_schema_at_parts(child, tail, leaf)),
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) => {
            let mut replaced = false;
            for schemas in [
                &mut keywords.any_of,
                &mut keywords.all_of,
                &mut keywords.one_of,
            ]
            .into_iter()
            .flatten()
            {
                for schema in schemas {
                    replaced |= replace_existing_schema_at_parts(schema, path_segments, leaf);
                }
            }
            for schema in [&mut keywords.then_schema, &mut keywords.else_schema]
                .into_iter()
                .filter_map(Option::as_deref_mut)
            {
                replaced |= replace_existing_schema_at_parts(schema, path_segments, leaf);
            }
            let child = if head == "*" {
                keywords.items.as_deref_mut()
            } else {
                keywords
                    .properties
                    .as_mut()
                    .and_then(|properties| properties.get_mut(head))
            };
            replaced
                | child.is_some_and(|child| replace_existing_schema_at_parts(child, tail, leaf))
        }
        _ => false,
    }
}

/// Host a `*` member row inside an object-shaped node's
/// `additionalProperties` (a ranged MAP's per-value rows). Returns false
/// when the node is not an open object-shaped schema, leaving list
/// handling to the caller.
fn insert_map_member_row(
    node: &mut SchemaNode,
    path_segments: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) -> bool {
    let Some((_, tail)) = path_segments.split_first() else {
        return false;
    };
    match node {
        SchemaNode::Object {
            additional_properties,
            ..
        } => {
            let member = additional_properties.get_or_insert_with(|| Box::new(SchemaNode::empty()));
            if member.is_false_schema() {
                return false;
            }
            insert_map_member_schema(member.as_mut(), tail, leaf, ambiguous_union_abstentions);
            true
        }
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords))
            if keywords.schema_type == Some(SchemaTypeKeyword::Single(JsonSchemaType::Object))
                && keywords
                    .additional_properties
                    .as_deref()
                    .is_none_or(|additional| !additional.is_false_schema()) =>
        {
            let member = keywords
                .additional_properties
                .get_or_insert_with(|| Box::new(SchemaNode::empty()));
            insert_map_member_schema(member, tail, leaf, ambiguous_union_abstentions);
            true
        }
        // A union base (the runtime iterable domain) hosts the member row
        // in EVERY collection arm: `range` iterates arrays and maps alike,
        // so the member constrains array items and map values, while
        // scalar, null, and CLOSED-object arms (the exact-empty off state)
        // stay untouched. Array arms merge into their `items`
        // directly so repeated member rows fold instead of wrapping.
        SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) if keywords.any_of.is_some() => {
            let Some(arms) = keywords.any_of.as_mut() else {
                return false;
            };
            let mut hosted = false;
            for arm in arms {
                if arm.is_array_like() {
                    let items = ensure_array_items_schema(arm);
                    insert_map_member_schema(items, tail, leaf, ambiguous_union_abstentions);
                    hosted = true;
                } else {
                    hosted |= insert_map_member_row(
                        arm,
                        path_segments,
                        leaf,
                        ambiguous_union_abstentions,
                    );
                }
            }
            hosted
        }
        _ => false,
    }
}

fn insert_map_member_schema(
    member: &mut SchemaNode,
    tail: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) {
    if tail.is_empty() {
        merge_into_schema_slot(member, leaf.clone());
    } else {
        insert_schema_at_parts_tracking(member, tail, leaf.clone(), ambiguous_union_abstentions);
    }
}

fn insert_schema_at_parts(
    node: &mut SchemaNode,
    path_segments: &[String],
    leaf: SchemaNode,
) -> usize {
    let mut ambiguous_union_abstentions = 0;
    insert_schema_at_parts_tracking(node, path_segments, leaf, &mut ambiguous_union_abstentions);
    ambiguous_union_abstentions
}

fn insert_schema_at_parts_tracking(
    node: &mut SchemaNode,
    path_segments: &[String],
    leaf: SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) {
    let Some((head, tail)) = path_segments.split_first() else {
        return;
    };

    // `range` iterates maps as well as lists, so a member row's `*` segment
    // means "each member value" of whatever container the node already is:
    // an object-shaped node hosts it under `additionalProperties` instead
    // of growing an array alternative.
    if head == "*" && insert_map_member_row(node, path_segments, &leaf, ambiguous_union_abstentions)
    {
        return;
    }

    if insert_into_nonempty_schema_slot(
        node,
        path_segments,
        head,
        tail,
        &leaf,
        ambiguous_union_abstentions,
    ) {
        return;
    }

    if head == "*" {
        if node.is_empty_slot() {
            // A bare `*` member row proves members exist, not which
            // collection lane hosts them: `range` iterates arrays and maps
            // alike, so a slot with no other shape evidence opens both lanes
            // instead of inventing an array-only shape.
            *node = SchemaNode::foreign(serde_json::json!({
                "anyOf": [
                    { "items": {}, "type": "array" },
                    { "additionalProperties": {}, "type": "object" },
                ]
            }));
            let hosted =
                insert_map_member_row(node, path_segments, &leaf, ambiguous_union_abstentions);
            debug_assert!(hosted, "two-lane member seed must host the row");
            return;
        }
        if !node.is_array_like() {
            let existing = std::mem::replace(node, SchemaNode::empty());
            let mut array_variant = new_array_slot();
            insert_schema_at_parts_tracking(
                &mut array_variant,
                path_segments,
                leaf,
                ambiguous_union_abstentions,
            );
            *node = SchemaNode::any_of(vec![existing, array_variant]);
            return;
        }
        let items = ensure_array_items_schema(node);
        if tail.is_empty() {
            merge_into_schema_slot(items, leaf);
        } else {
            insert_schema_at_parts_tracking(items, tail, leaf, ambiguous_union_abstentions);
        }
        return;
    }

    if !tail.is_empty() {
        node.clear_exact_empty_constraint_for_descendant();
    }
    if tail.is_empty() {
        let properties = ensure_object_properties(node);
        merge_into_schema_slot(
            properties
                .entry(head.clone())
                .or_insert_with(SchemaNode::empty),
            leaf,
        );
        return;
    }

    let key = head.clone();
    let next_is_array = tail.first().is_some_and(|segment| segment == "*");
    let properties = ensure_object_properties(node);
    let child = properties.entry(key).or_insert_with(|| {
        if next_is_array {
            // Left empty so the `*` handling above seeds both collection
            // lanes for the member row instead of an array-only slot.
            SchemaNode::empty()
        } else {
            // An ancestor object materialized only because a DESCENDANT is
            // referenced: member reads prove keys exist, they do not bound
            // the member set, so the interior node stays open (the strict
            // root closure is created separately).
            SchemaNode::unknown_object()
        }
    });
    if !next_is_array {
        child.clear_exact_empty_constraint_for_descendant();
    }
    insert_schema_at_parts_tracking(child, tail, leaf, ambiguous_union_abstentions);
}

fn insert_into_nonempty_schema_slot(
    node: &mut SchemaNode,
    path_segments: &[String],
    head: &str,
    tail: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) -> bool {
    if !matches!(node, SchemaNode::Typed(_)) || node.is_empty_slot() {
        return false;
    }
    // Descend through properties the resolved value already declares, so
    // the descendant merges at the deepest existing node and that node's
    // own openness decides the outcome (a top-level merge of a nested
    // carrier would let the carrier's materialized closure shut an open
    // map one level down).
    if !tail.is_empty()
        && head != "*"
        && let SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) = node
        && let Some(child) = keywords
            .properties
            .as_mut()
            .and_then(|properties| properties.get_mut(head))
    {
        insert_schema_at_parts_tracking(child, tail, leaf.clone(), ambiguous_union_abstentions);
        // Re-enter through the parent's ingestion boundary so later passes
        // do not mistake a newly materialized legacy host for a tree-owned one.
        let child_value = std::mem::replace(child, SchemaNode::empty()).into_value();
        *child = SchemaNode::from_value(child_value);
        return true;
    }
    if insert_into_canonical_object_conjunct(node, head, tail, leaf, ambiguous_union_abstentions) {
        return true;
    }
    // A union base (an off-state arm plus one open object arm, e.g. a
    // declared-empty serialized map) hosts descendants in its open arm:
    // merging a carrier at the union level would replace the open arm
    // with the carrier's materialized closure.
    if head != "*"
        && let SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) = node
        && let Some(arms) = keywords.any_of.as_mut()
    {
        let mut open_arms = arms
            .iter_mut()
            .filter(|arm| arm.opens_unknown_object_fields());
        if let (Some(arm), None) = (open_arms.next(), open_arms.next()) {
            insert_schema_at_parts_tracking(
                arm,
                path_segments,
                leaf.clone(),
                ambiguous_union_abstentions,
            );
            return true;
        }
    }
    let mut fragment = SchemaNode::empty();
    insert_schema_at_parts_tracking(
        &mut fragment,
        path_segments,
        leaf.clone(),
        ambiguous_union_abstentions,
    );
    merge_into_schema_slot(node, fragment);
    true
}

fn insert_into_canonical_object_conjunct(
    node: &mut SchemaNode,
    head: &str,
    tail: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) -> bool {
    // A canonical object constraint may leave the slot's type inside
    // `allOf`. Descendant default evidence still belongs to that same
    // object lane; union-merging a new carrier would let either side
    // bypass the other.
    insert_into_typed_canonical_object_conjunct(node, head, tail, leaf, ambiguous_union_abstentions)
}

fn insert_into_typed_canonical_object_conjunct(
    node: &mut SchemaNode,
    head: &str,
    tail: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) -> bool {
    if insert_into_typed_canonical_mixed_object_lane(
        node,
        head,
        tail,
        leaf,
        ambiguous_union_abstentions,
    ) {
        return true;
    }
    let value = node.clone().into_value();
    let mut path_segments = Vec::with_capacity(tail.len() + 1);
    path_segments.push(head.to_string());
    path_segments.extend_from_slice(tail);
    if multi_arm_object_union_has_equivalent_descendant(&value, &path_segments) == Some(false) {
        *ambiguous_union_abstentions += 1;
        return true;
    }
    if !schema_only_allows_object(&value) {
        return false;
    }
    let SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) = node else {
        return false;
    };
    let properties = keywords.properties.get_or_insert_with(BTreeMap::new);
    let next_is_array = tail.first().is_some_and(|segment| segment == "*");
    let child = properties.entry(head.to_string()).or_insert_with(|| {
        if tail.is_empty() || next_is_array {
            SchemaNode::empty()
        } else {
            SchemaNode::unknown_object()
        }
    });
    if tail.is_empty() {
        merge_into_schema_slot(child, leaf.clone());
    } else {
        child.clear_exact_empty_constraint_for_descendant();
        insert_schema_at_parts_tracking(child, tail, leaf.clone(), ambiguous_union_abstentions);
    }
    true
}

fn insert_into_typed_canonical_mixed_object_lane(
    node: &mut SchemaNode,
    head: &str,
    tail: &[String],
    leaf: &SchemaNode,
    ambiguous_union_abstentions: &mut usize,
) -> bool {
    let SchemaNode::Typed(TypedSchemaNode::Keywords(keywords)) = node else {
        return false;
    };
    let Some(conjuncts) = keywords.all_of.as_mut() else {
        return false;
    };
    let [first, second] = conjuncts.as_slice() else {
        return false;
    };
    let base_index = if is_not_null_constraint(&first.clone().into_value()) {
        1
    } else if is_not_null_constraint(&second.clone().into_value()) {
        0
    } else {
        return false;
    };
    let Some(SchemaNode::Typed(TypedSchemaNode::Keywords(base))) = conjuncts.get(base_index) else {
        return false;
    };
    let Some(SchemaTypeKeyword::Multiple(types)) = &base.schema_type else {
        return false;
    };
    if !types.contains(&JsonSchemaType::Object)
        || !types.iter().any(|schema_type| {
            !matches!(schema_type, JsonSchemaType::Null | JsonSchemaType::Object)
        })
    {
        return false;
    }
    let types = types.clone();
    let mut base_keywords = (**base).clone();
    base_keywords.schema_type = None;
    let mut path_segments = Vec::with_capacity(tail.len() + 1);
    path_segments.push(head.to_string());
    path_segments.extend_from_slice(tail);
    let mut arms = Vec::with_capacity(types.len());
    for schema_type in types {
        let mut arm_keywords = base_keywords.clone();
        arm_keywords.schema_type = Some(SchemaTypeKeyword::Single(schema_type));
        let mut arm = SchemaNode::Typed(TypedSchemaNode::Keywords(Box::new(arm_keywords)));
        if schema_type == JsonSchemaType::Object {
            insert_schema_at_parts_tracking(
                &mut arm,
                &path_segments,
                leaf.clone(),
                ambiguous_union_abstentions,
            );
        }
        arms.push(arm);
    }
    let Some(base) = conjuncts.get_mut(base_index) else {
        return false;
    };
    *base = SchemaNode::any_of(arms);
    true
}

fn schema_only_allows_object(schema: &Value) -> bool {
    crate::schema_model::schema_allows_type(schema, "object")
        && ["array", "boolean", "integer", "null", "number", "string"]
            .into_iter()
            .all(|schema_type| crate::schema_model::schema_excludes_type(schema, schema_type))
}

fn multi_arm_object_union_has_equivalent_descendant(
    schema: &Value,
    path_segments: &[String],
) -> Option<bool> {
    let object = schema.as_object()?;
    for keyword in ["anyOf", "oneOf"] {
        let Some(arms) = object.get(keyword).and_then(Value::as_array) else {
            continue;
        };
        if arms.len() <= 1 || !arms.iter().all(schema_only_allows_object) {
            continue;
        }
        let Some(first) = arms
            .first()
            .and_then(|arm| schema_descendant_at_path(arm, path_segments))
        else {
            return Some(false);
        };
        return Some(arms.iter().skip(1).all(|arm| {
            schema_descendant_at_path(arm, path_segments)
                .is_some_and(|descendant| descendant == first)
        }));
    }
    let arms = object.get("allOf").and_then(Value::as_array)?;
    let mut verdicts = arms
        .iter()
        .filter_map(|arm| multi_arm_object_union_has_equivalent_descendant(arm, path_segments));
    let first = verdicts.next()?;
    Some(first && verdicts.all(|verdict| verdict == first))
}

fn schema_descendant_at_path<'a>(
    mut schema: &'a Value,
    path_segments: &[String],
) -> Option<&'a Value> {
    for segment in path_segments {
        if segment == "*" {
            return None;
        }
        schema = schema.get("properties")?.get(segment)?;
    }
    Some(schema)
}