helm-schema-ir 0.0.4

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
//! In-domain helper summaries: a bound helper call evaluates its body's CST
//! through the same fragment interpreter as documents, producing one
//! [`FragmentSummary`] — the body's abstract fragment plus its pathless
//! reads — memoized per bound call in the analysis db.
//!
//! The summary is call-site independent: reads carry only helper-internal
//! guards and helper-body provenance, and fragment sites keep the body's
//! own facts. Call sites add their ambient guards, site provenance, and
//! resource scope when absorbing reads, and [`splice_summary`] rebases the
//! fragment's sites onto the call site.
//!
//! One value projection ([`FragmentSummary::value`]) derives the
//! `AbstractValue` for helper calls in value position (inside expressions):
//!
//! - literal text arms project as string sets,
//! - splices project as `OutputPath` rows whose meta carries the arm's
//!   root-to-leaf conditions, defaultedness, and body provenance,
//! - opaque taint projects as per-path `OutputPath` rows (the influence is
//!   attributable even though the text is not),
//! - mappings/sequences project as dicts/lists; dynamic-key entry values
//!   merge at the parent level (no invented segment),
//! - render-suppressed scalars project no value (their splices surface as
//!   dependency reads instead),
//! - arms merge with the value lattice's merge rules.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::rc::Rc;

use helm_schema_syntax::TemplatedDocument;

use crate::abstract_value::AbstractValue;
use crate::analysis_db::{BoundHelperCallResolution, IrAnalysisDb};
use crate::helper_meta::{HelperOutputMeta, RenderedRow, merge_provenance_sites};
use crate::symbolic_local_state::SymbolicLocalState;
use crate::{ContractProvenance, ValueKind};
use helm_schema_core::{GuardDnf, Predicate};

use super::domain::{AbstractFragment, Guarded, PathCondition, SiteFacts, Splice, StringPart};
use super::eval::{Interpreter, NodeView, ValueRead};

/// One bound helper call's evaluation in the fragment domain.
#[derive(Debug, Default)]
pub(crate) struct FragmentSummary {
    /// Predicate paths severed by index-call narrowing inside the body;
    /// callers absorb their own reads against them.
    pub(crate) suppress_predicate_paths: BTreeSet<String>,
    /// The body's abstract fragment (sites carry helper-body facts).
    pub(crate) root: Guarded<AbstractFragment>,
    /// Pathless reads observed in the body (helper-internal guards only).
    pub(crate) reads: Vec<ValueRead>,
    /// Declared input-type hints observed unconditionally in the body.
    pub(crate) type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Input-type hints observed only under body branch predicates.
    pub(crate) guarded_type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Input-type hints from literal `default`/`coalesce` fallbacks in the
    /// body: they type only the truthy arm of the path.
    pub(crate) fallback_type_hints: BTreeMap<String, BTreeSet<String>>,
    /// Paths consumed as serialized YAML by `fromYaml` in the body.
    pub(crate) parsed_yaml_input_paths: BTreeSet<String>,
    /// Paths serialized with `toYaml` in the helper's projected output.
    pub(crate) yaml_serialized_paths: BTreeSet<String>,
    /// Paths consumed through total stringifications anywhere in the body:
    /// the chart tolerates any input type at them.
    pub(crate) shape_erased_paths: BTreeSet<String>,
    /// Paths carrying a real runtime string contract in the body.
    pub(crate) string_contract_paths: BTreeSet<String>,
    /// The body's per-path range facts after bound-context resolution
    /// (direct iteration identity, JSON-decoded values, destructuring).
    /// Callers need the direct identity to project the runtime domain.
    pub(crate) range_modes: crate::range_modes::RangeModes,
    /// `fail` captures of the body, helper-internal state only.
    pub(crate) fail_conditions: Vec<crate::eval_effect::FailCapture>,
    /// Object-producing value mutations observed in source order.
    pub(crate) member_host_conversions: BTreeSet<crate::eval_effect::MemberHostConversion>,
    /// Chart-level `set … default` normalizations the body applies.
    pub(crate) chart_defaults: BTreeSet<String>,
    /// Root-context fields replaced while the helper executes.
    pub(crate) root_set_mutations: BTreeMap<String, AbstractValue>,
    /// Truth predicates for root-context fields replaced by the helper.
    pub(crate) root_set_predicates: BTreeMap<String, Predicate>,
    /// Joined per-arm value alternatives for root-context fields the helper
    /// set across complete if/else chains.
    pub(crate) root_set_value_dispatches: BTreeMap<String, crate::eval_effect::RootValueDispatch>,
    /// Chart value subtrees supplying defaults to a replaced effective values tree.
    pub(crate) values_default_sources: BTreeSet<crate::ValuesDefaultSource>,
    pub(crate) values_root_overlay_prefixes: BTreeSet<String>,
    /// Helper names through which the values root was replaced.
    pub(crate) values_root_helper_includes: BTreeSet<String>,
    /// Strictly string-consumed paths whose consumers ran before the
    /// body's values-root wrapper rewrite (see the interpreter field).
    pub(crate) pre_rewrite_strict_paths: BTreeSet<String>,
    /// The value projection (see module docs), computed once.
    pub(crate) value: Option<AbstractValue>,
    /// Rendered splice/taint rows flattened from the tree: per-path branch
    /// conditions, defaultedness, encoding, and provenance. Value-position
    /// call sites use these for no-render demotion and for restoring
    /// per-path meta after transfer functions collapse the value shape.
    pub(crate) rendered: Vec<RenderedRow>,
}

/// Evaluate one bound helper body as a fragment. `seen` is the active call
/// chain (this helper included), threaded so nested calls cut cycles.
pub(crate) fn eval_bound_helper_fragment(
    name: &str,
    resolution: &BoundHelperCallResolution,
    db: &IrAnalysisDb,
    seen: &HashSet<String>,
) -> FragmentSummary {
    let Some(body) = db.parsed_helper_body(name) else {
        return FragmentSummary::default();
    };
    let document = TemplatedDocument::parse_with_root(body.source, body.tree.root_node());
    let body_facts = db.helper_body_eval_facts(name, || {
        super::eval::BodyEvalFacts::collect(body.source, db, &body.tree, &document)
    });
    let mut interpreter = Interpreter::with_body_facts(
        body.source,
        Some(body.source_path),
        db,
        &document,
        body_facts,
    );
    interpreter.source_offset = body.body_offset;
    interpreter.inline_files = vec![format!("define:{name}")];
    interpreter.helper_scope = true;
    interpreter.helper_seen = seen.clone();
    interpreter.root_bindings = resolution.bindings.clone();
    interpreter.root_truthy_predicates = resolution.root_truthy_predicates.clone();
    interpreter.root_value_dispatches = resolution.root_value_dispatches.clone();
    interpreter.root_value_dot = resolution.dot.helper.clone();
    interpreter.dot_stack.push(resolution.dot.fragment.clone());
    interpreter.locals = SymbolicLocalState::default();
    let roots: Vec<NodeView<'_>> = document.roots().iter().map(NodeView::plain).collect();
    let contributions = interpreter.eval_node_list(&roots);
    let root = contributions.assemble();
    // Guard reads that are strict ancestors of an index-narrowed path are
    // traversal steps, not conditions on the ancestor (the narrowing severed
    // them); dependency rows and the narrowed paths themselves stay.
    let suppress = interpreter.suppress_predicate_paths;
    let mut reads: Vec<ValueRead> = interpreter
        .reads
        .into_iter()
        .filter(|read| {
            read.dependency
                || suppress.contains(&read.values_path)
                || !suppress.iter().any(|narrowed| {
                    helm_schema_core::values_path_is_descendant(narrowed, &read.values_path)
                })
        })
        .collect();
    let rendered = rendered_rows(&root);
    prune_sibling_conditions(&mut reads, &rendered);
    let mut summary = FragmentSummary {
        value: projected_value(&root),
        rendered,
        root,
        reads,
        suppress_predicate_paths: suppress,
        type_hints: interpreter.type_hints,
        guarded_type_hints: interpreter.guarded_type_hints,
        fallback_type_hints: interpreter.fallback_type_hints,
        parsed_yaml_input_paths: interpreter.parsed_yaml_input_paths,
        yaml_serialized_paths: interpreter.yaml_serialized_paths,
        shape_erased_paths: interpreter.shape_erased_paths,
        string_contract_paths: interpreter.string_contract_paths,
        range_modes: interpreter.range_modes,
        fail_conditions: interpreter.fail_conditions,
        member_host_conversions: interpreter.member_host_conversions,
        chart_defaults: interpreter.chart_defaults_observed,
        root_set_mutations: interpreter.root_set_mutations_observed,
        root_set_predicates: interpreter.root_set_predicates_observed,
        root_set_value_dispatches: interpreter.root_value_dispatches_observed,
        values_default_sources: interpreter.values_default_sources_observed,
        values_root_overlay_prefixes: interpreter.values_root_overlay_prefixes_observed,
        values_root_helper_includes: interpreter.values_root_helper_includes_observed,
        pre_rewrite_strict_paths: interpreter.pre_rewrite_strict_paths,
    };
    // Render-suppressed splices (block-scalar bodies) influence the text
    // without rendering a sink-typed value; value-position consumers see
    // them as dependency reads, matching the demotion the summary walk
    // applied at suppressing slots.
    append_suppressed_reads(&summary.root, &mut Vec::new(), &mut summary.reads);
    summary
}

impl FragmentSummary {
    /// The rendered `.Values` paths of the summary that are encoded at their
    /// render site (the sink does not constrain the value's shape).
    pub(crate) fn encoded_paths(&self) -> BTreeSet<String> {
        self.rendered
            .iter()
            .filter(|row| row.encoded)
            .map(|row| row.path.clone())
            .collect()
    }
}

/// Splice a memoized summary fragment at a call site: nodes stamped with a
/// helper-body site keep it when the body declared its own resource
/// (resource-defining helper bodies scope their own rows); otherwise the
/// body site becomes helper provenance on the node and the call site's
/// facts take the site slot.
pub(crate) fn splice_summary(
    summary: &FragmentSummary,
    call_site: Option<&Rc<SiteFacts>>,
) -> Guarded<AbstractFragment> {
    let mut root = summary.root.clone();
    for (_, node) in &mut root.arms {
        rebase_node_sites(node, call_site);
    }
    root
}

fn rebase_site(
    site: &mut Option<Rc<SiteFacts>>,
    provenance: &mut Vec<ContractProvenance>,
    call_site: Option<&Rc<SiteFacts>>,
) {
    match site.as_deref() {
        Some(body_site) if body_site.resource.is_none() => {
            let mut merged: Vec<ContractProvenance> =
                body_site.provenance.iter().cloned().collect();
            merge_provenance_sites(&mut merged, provenance);
            *provenance = merged;
            *site = call_site.cloned();
        }
        Some(_) => {}
        None => *site = call_site.cloned(),
    }
}

fn rebase_node_sites(node: &mut AbstractFragment, call_site: Option<&Rc<SiteFacts>>) {
    match node {
        AbstractFragment::Mapping(mapping) => {
            for entry in &mut mapping.entries {
                for (_, value) in &mut entry.value.arms {
                    rebase_node_sites(value, call_site);
                }
            }
        }
        AbstractFragment::Sequence(sequence) => {
            for item in &mut sequence.items {
                for (_, value) in &mut item.arms {
                    rebase_node_sites(value, call_site);
                }
            }
        }
        AbstractFragment::Scalar(scalar) => {
            for part in &mut scalar.parts {
                match part {
                    StringPart::Text(_) => {}
                    StringPart::Splice(splice) => {
                        rebase_site(
                            &mut splice.meta.site,
                            &mut splice.meta.provenance,
                            call_site,
                        );
                    }
                    StringPart::Taint(taint) => {
                        rebase_site(&mut taint.site, &mut taint.provenance, call_site);
                    }
                }
            }
        }
        AbstractFragment::Splice(splice) => {
            rebase_site(
                &mut splice.meta.site,
                &mut splice.meta.provenance,
                call_site,
            );
        }
        AbstractFragment::Opaque(opaque) => {
            rebase_site(&mut opaque.site, &mut opaque.provenance, call_site);
        }
    }
}

/// The root-to-leaf condition chain as one predicate branch (flattening
/// `And` compounds into their parts, the shape branch meta always used).
fn condition_branch(conditions: &[PathCondition]) -> BTreeSet<Predicate> {
    let mut branch = BTreeSet::new();
    for condition in conditions {
        match condition {
            Predicate::True => {}
            Predicate::And(parts) => branch.extend(parts.iter().cloned()),
            other => {
                branch.insert(other.clone());
            }
        }
    }
    branch
}

fn splice_row_meta(splice: &Splice, conditions: &[PathCondition]) -> HelperOutputMeta {
    let mut provenance: Vec<ContractProvenance> = splice
        .meta
        .site
        .as_deref()
        .and_then(|site| site.provenance.clone())
        .into_iter()
        .collect();
    merge_provenance_sites(&mut provenance, &splice.meta.provenance);
    let mut meta = HelperOutputMeta {
        defaulted: splice.meta.defaulted,
        shape_erased: splice.meta.shape_erased,
        nil_omitted: splice.meta.nil_omitted,
        string_contract: splice.meta.string_contract,
        json_serialized: splice.meta.json_serialized,
        json_decoded: splice.meta.json_decoded,
        lexical_escapes: splice.meta.lexical_escapes.clone(),
        // Crossing the helper-summary boundary makes the layer facts
        // binding-carried: the caller renders the helper's OUTPUT, whose
        // sibling dispatch arms may rely on the base typing the direct
        // render-site lane moves onto synthesized arms.
        // Crossing the helper-summary boundary makes the layer facts
        // binding-carried: the caller renders the helper's OUTPUT, whose
        // sibling dispatch arms may rely on the base typing the direct
        // render-site lane moves onto synthesized arms.
        merge_layers: splice.meta.merge_layers.clone().map(|mut merge| {
            merge.via_binding = true;
            merge
        }),
        provenance,
        ..HelperOutputMeta::default()
    };
    let branch = condition_branch(conditions);
    if !branch.is_empty() {
        meta.predicates.insert(branch);
    }
    meta
}

fn taint_row_meta(
    site: Option<&SiteFacts>,
    provenance: &[ContractProvenance],
    conditions: &[PathCondition],
) -> HelperOutputMeta {
    let mut merged: Vec<ContractProvenance> = site
        .and_then(|site| site.provenance.clone())
        .into_iter()
        .collect();
    merge_provenance_sites(&mut merged, provenance);
    let mut meta = HelperOutputMeta {
        provenance: merged,
        ..HelperOutputMeta::default()
    };
    let branch = condition_branch(conditions);
    if !branch.is_empty() {
        meta.predicates.insert(branch);
    }
    meta
}

fn scalar_taint_row_meta(
    taint: &super::domain::TaintPart,
    conditions: &[PathCondition],
) -> HelperOutputMeta {
    let mut meta = taint_row_meta(taint.site.as_deref(), &taint.provenance, conditions);
    meta.json_serialized = taint.json_serialized;
    meta
}

fn project_structured_taint_value(
    value: &AbstractValue,
    outer_meta: &HelperOutputMeta,
) -> AbstractValue {
    match value {
        AbstractValue::ValuesPath(path) => {
            AbstractValue::OutputPath(path.clone(), outer_meta.clone())
        }
        AbstractValue::JsonDecodedPath(path) => {
            let mut meta = outer_meta.clone();
            meta.json_decoded = true;
            AbstractValue::OutputPath(path.clone(), meta)
        }
        AbstractValue::OutputPath(path, inner_meta) => {
            AbstractValue::OutputPath(path.clone(), conjoin_output_meta(inner_meta, outer_meta))
        }
        AbstractValue::Dict(entries) => AbstractValue::Dict(
            entries
                .iter()
                .map(|(key, value)| {
                    (
                        key.clone(),
                        project_structured_taint_value(value, outer_meta),
                    )
                })
                .collect(),
        ),
        AbstractValue::List(items) => AbstractValue::List(
            items
                .iter()
                .map(|item| project_structured_taint_value(item, outer_meta))
                .collect(),
        ),
        AbstractValue::Overlay { entries, fallback } => AbstractValue::Overlay {
            entries: entries
                .iter()
                .map(|(key, value)| {
                    (
                        key.clone(),
                        project_structured_taint_value(value, outer_meta),
                    )
                })
                .collect(),
            fallback: Box::new(project_structured_taint_value(fallback, outer_meta)),
        },
        AbstractValue::Choice(choices) => AbstractValue::Choice(
            choices
                .iter()
                .map(|choice| project_structured_taint_value(choice, outer_meta))
                .collect(),
        ),
        AbstractValue::FirstTruthy(candidates) => AbstractValue::FirstTruthy(
            candidates
                .iter()
                .map(|candidate| project_structured_taint_value(candidate, outer_meta))
                .collect(),
        ),
        AbstractValue::MergedLayers(layers) => AbstractValue::MergedLayers(
            layers
                .iter()
                .map(|layer| project_structured_taint_value(layer, outer_meta))
                .collect(),
        ),
        AbstractValue::Top
        | AbstractValue::Unknown
        | AbstractValue::RangeKey(_)
        | AbstractValue::KeysList(_)
        | AbstractValue::RootContext
        | AbstractValue::StringSet(_)
        | AbstractValue::DerivedBoolean(_)
        | AbstractValue::SplitList { .. }
        | AbstractValue::SplitSegment { .. }
        | AbstractValue::Widened(_) => value.clone(),
    }
}

fn conjoin_output_meta(inner: &HelperOutputMeta, outer: &HelperOutputMeta) -> HelperOutputMeta {
    let inner_branches = if inner.predicates.is_empty() {
        vec![BTreeSet::new()]
    } else {
        inner.predicates.iter().cloned().collect()
    };
    let outer_branches = if outer.predicates.is_empty() {
        vec![BTreeSet::new()]
    } else {
        outer.predicates.iter().cloned().collect()
    };
    let predicates = inner_branches
        .into_iter()
        .flat_map(|inner| {
            outer_branches.iter().map(move |outer| {
                let mut branch = inner.clone();
                branch.extend(outer.iter().cloned());
                branch
            })
        })
        .filter(|branch| !branch.is_empty())
        .collect();
    let mut combined = inner.clone();
    combined.predicates.clear();
    let mut outer = outer.clone();
    outer.predicates.clear();
    combined.merge(&outer);
    combined.predicates = predicates;
    combined
}

/// The documented value projection (module docs). Arms of one guarded value
/// project independently and merge with the lattice's merge rules; splice
/// conditions survive in `OutputPath` meta.
pub(super) fn projected_value(root: &Guarded<AbstractFragment>) -> Option<AbstractValue> {
    let mut conditions = Vec::new();
    let values = project_guarded(root, &mut conditions);
    AbstractValue::merge_all(values)
}

fn project_guarded(
    guarded: &Guarded<AbstractFragment>,
    conditions: &mut Vec<PathCondition>,
) -> Vec<AbstractValue> {
    let mut values = Vec::new();
    for (condition, node) in &guarded.arms {
        let pushed = !condition.is_trivial();
        if pushed {
            conditions.push(condition.clone());
        }
        values.extend(project_node(node, conditions));
        if pushed {
            conditions.pop();
        }
    }
    values
}

fn project_node(
    node: &AbstractFragment,
    conditions: &mut Vec<PathCondition>,
) -> Vec<AbstractValue> {
    match node {
        AbstractFragment::Mapping(mapping) => {
            let mut entries = BTreeMap::new();
            let mut extra = Vec::new();
            for entry in &mapping.entries {
                let child_values = project_guarded(&entry.value, conditions);
                match &entry.key {
                    super::domain::EntryKey::Literal(key) if !key.is_empty() => {
                        if let Some(value) = AbstractValue::merge_all(child_values) {
                            entries.insert(key.clone(), value);
                        }
                    }
                    // Dynamic (and empty) keys attribute at the parent level;
                    // the projection invents no segment for them.
                    _ => extra.extend(child_values),
                }
            }
            let mut values = Vec::new();
            if !entries.is_empty() {
                values.push(AbstractValue::Dict(entries));
            }
            values.extend(extra);
            values
        }
        AbstractFragment::Sequence(sequence) => {
            let items: Vec<AbstractValue> = sequence
                .items
                .iter()
                .filter_map(|item| {
                    let values = project_guarded(item, conditions);
                    AbstractValue::choice(values)
                })
                .collect();
            if items.is_empty() {
                Vec::new()
            } else {
                vec![AbstractValue::List(items)]
            }
        }
        AbstractFragment::Scalar(scalar) => {
            if scalar.suppressed {
                return Vec::new();
            }
            let mut values = Vec::new();
            let mut strings = BTreeSet::new();
            let mut has_non_text = false;
            // A scalar composing literal text AROUND splices renders each
            // spliced path as DERIVED TEXT rather than as the bare value:
            // a structural re-lowering of the projection (the pod-template
            // `fromYaml | toYaml` roundtrip) must keep the partial-text
            // discipline instead of minting full-value preimages
            // (traefik's `--…={{ $value }}` flag items). Splice-only part
            // sets stay bare — contribution-set degradation merges
            // ALTERNATIVE renders into one part list (airflow's
            // nil-aware `revisionHistoryLimit` picker), where each arm
            // still renders its raw value exactly.
            let has_literal_text = scalar.parts.iter().any(|part| {
                matches!(
                    part,
                    StringPart::Text(alternatives)
                        if alternatives.iter().any(|text| !text.is_empty())
                )
            });
            let partial = has_literal_text && scalar.parts.len() > 1;
            for part in &scalar.parts {
                match part {
                    StringPart::Text(alternatives) => strings.extend(alternatives.iter().cloned()),
                    StringPart::Splice(splice) => {
                        has_non_text = true;
                        let mut meta = splice_row_meta(splice, conditions);
                        meta.partial_text |= partial;
                        values.push(AbstractValue::OutputPath(splice.values_path.clone(), meta));
                    }
                    StringPart::Taint(taint) => {
                        has_non_text = true;
                        let mut meta = scalar_taint_row_meta(taint, conditions);
                        meta.partial_text |= partial;
                        if let Some(value) = &taint.structured_value {
                            values.push(project_structured_taint_value(value, &meta));
                        } else {
                            for path in &taint.paths {
                                values.push(AbstractValue::OutputPath(path.clone(), meta.clone()));
                            }
                        }
                    }
                }
            }
            // Literal text forms the value only when the scalar is pure
            // text; text around splices renders but is not the value the
            // call site consumes (the paths are).
            if !has_non_text && !strings.is_empty() {
                values.push(AbstractValue::StringSet(strings));
            }
            values
        }
        AbstractFragment::Splice(splice) => {
            vec![AbstractValue::OutputPath(
                splice.values_path.clone(),
                splice_row_meta(splice, conditions),
            )]
        }
        AbstractFragment::Opaque(opaque) => {
            let meta = taint_row_meta(opaque.site.as_deref(), &opaque.provenance, conditions);
            opaque
                .taint
                .iter()
                .map(|path| AbstractValue::OutputPath(path.clone(), meta.clone()))
                .collect()
        }
    }
}

/// Flatten the tree's rendered splice/taint rows into per-path claims.
fn rendered_rows(root: &Guarded<AbstractFragment>) -> Vec<RenderedRow> {
    let mut rows = Vec::new();
    let mut conditions = Vec::new();
    collect_rendered(root, &mut conditions, false, &mut rows);
    rows
}

fn collect_rendered(
    guarded: &Guarded<AbstractFragment>,
    conditions: &mut Vec<PathCondition>,
    suppressed: bool,
    rows: &mut Vec<RenderedRow>,
) {
    for (condition, node) in &guarded.arms {
        let pushed = !condition.is_trivial();
        if pushed {
            conditions.push(condition.clone());
        }
        collect_rendered_node(node, conditions, suppressed, rows);
        if pushed {
            conditions.pop();
        }
    }
}

fn push_rendered_row(
    rows: &mut Vec<RenderedRow>,
    path: &str,
    kind: ValueKind,
    encoded: bool,
    meta: HelperOutputMeta,
) {
    if path.trim().is_empty() {
        return;
    }
    if let Some(existing) = rows
        .iter_mut()
        .find(|row| row.path == path && row.kind == kind && row.encoded == encoded)
    {
        existing.meta.merge(&meta);
        return;
    }
    rows.push(RenderedRow {
        path: path.to_string(),
        kind,
        encoded,
        meta,
    });
}

fn collect_rendered_node(
    node: &AbstractFragment,
    conditions: &mut Vec<PathCondition>,
    suppressed: bool,
    rows: &mut Vec<RenderedRow>,
) {
    match node {
        AbstractFragment::Mapping(mapping) => {
            for entry in &mapping.entries {
                collect_rendered(&entry.value, conditions, suppressed, rows);
            }
        }
        AbstractFragment::Sequence(sequence) => {
            for item in &sequence.items {
                collect_rendered(item, conditions, suppressed, rows);
            }
        }
        AbstractFragment::Scalar(scalar) => {
            let suppressed = suppressed || scalar.suppressed;
            if suppressed {
                return;
            }
            for part in &scalar.parts {
                match part {
                    StringPart::Text(_) => {}
                    StringPart::Splice(splice) => push_rendered_row(
                        rows,
                        &splice.values_path,
                        splice.kind,
                        splice.meta.encoded,
                        splice_row_meta(splice, conditions),
                    ),
                    StringPart::Taint(taint) => {
                        let meta = scalar_taint_row_meta(taint, conditions);
                        for path in &taint.paths {
                            push_rendered_row(
                                rows,
                                path,
                                ValueKind::PartialScalar,
                                false,
                                meta.clone(),
                            );
                        }
                    }
                }
            }
        }
        AbstractFragment::Splice(splice) => {
            if !suppressed {
                push_rendered_row(
                    rows,
                    &splice.values_path,
                    splice.kind,
                    splice.meta.encoded,
                    splice_row_meta(splice, conditions),
                );
            }
        }
        AbstractFragment::Opaque(opaque) => {
            if suppressed {
                return;
            }
            let meta = taint_row_meta(opaque.site.as_deref(), &opaque.provenance, conditions);
            for path in &opaque.taint {
                push_rendered_row(rows, path, opaque.kind, false, meta.clone());
            }
        }
    }
}

/// The summary lane's sibling-condition rule for pathless reads: a
/// truthiness condition about a *different* summary source describes that
/// sibling's branch, not this read's, and drops unless the paths are
/// related. A read defaulted on its own path additionally drops its own
/// negation when a positive condition remains (the default supplies the
/// value on the negative side).
fn prune_sibling_conditions(reads: &mut Vec<ValueRead>, rendered: &[RenderedRow]) {
    let mut sources: BTreeSet<String> = reads.iter().map(|read| read.values_path.clone()).collect();
    sources.extend(rendered.iter().map(|row| row.path.clone()));
    if sources.len() < 2 {
        return;
    }
    let unrelated_sibling = |predicate_path: &str, read_path: &str| {
        predicate_path != read_path
            && sources.contains(predicate_path)
            && !crate::helper_meta::values_paths_are_related(predicate_path, read_path)
    };
    let mut pruned: Vec<ValueRead> = Vec::new();
    for mut read in reads.drain(..) {
        let conjunctions = read.condition.disjuncts().iter().map(|conjunction| {
            let has_truthy_sibling = conjunction.iter().any(|predicate| {
                matches!(predicate, Predicate::Guard(crate::Guard::Truthy { path }) if unrelated_sibling(path, &read.values_path))
            });
            let defaulted = conjunction.iter().any(|predicate| {
                matches!(predicate, Predicate::Guard(crate::Guard::Default { path }) if path == &read.values_path)
            });
            let has_self_truthy = conjunction.iter().any(|predicate| {
                matches!(predicate, Predicate::Guard(crate::Guard::Truthy { path }) if path == &read.values_path)
            });
            let mut predicates = conjunction
                .iter()
                .filter(|predicate| {
                    !matches!(predicate, Predicate::Guard(crate::Guard::Truthy { path }) if unrelated_sibling(path, &read.values_path))
                })
                .cloned()
                .collect::<Vec<_>>();
            if defaulted && (has_self_truthy || has_truthy_sibling) {
                predicates.retain(|predicate| {
                    !matches!(predicate, Predicate::Not(inner) if matches!(inner.as_ref(), Predicate::Guard(crate::Guard::Truthy { path }) if path == &read.values_path))
                });
            }
            predicates
        });
        read.condition = GuardDnf::from_disjunction(conjunctions);
        if !pruned.contains(&read) {
            pruned.push(read);
        }
    }
    *reads = pruned;
}

/// Dependency reads for splices inside render-suppressed scalars.
fn append_suppressed_reads(
    guarded: &Guarded<AbstractFragment>,
    conditions: &mut Vec<PathCondition>,
    reads: &mut Vec<ValueRead>,
) {
    for (condition, node) in &guarded.arms {
        let pushed = !condition.is_trivial();
        if pushed {
            conditions.push(condition.clone());
        }
        append_suppressed_node_reads(node, conditions, reads);
        if pushed {
            conditions.pop();
        }
    }
}

fn append_suppressed_node_reads(
    node: &AbstractFragment,
    conditions: &mut Vec<PathCondition>,
    reads: &mut Vec<ValueRead>,
) {
    match node {
        AbstractFragment::Mapping(mapping) => {
            for entry in &mapping.entries {
                append_suppressed_reads(&entry.value, conditions, reads);
            }
        }
        AbstractFragment::Sequence(sequence) => {
            for item in &sequence.items {
                append_suppressed_reads(item, conditions, reads);
            }
        }
        AbstractFragment::Scalar(scalar) if scalar.suppressed => {
            for part in &scalar.parts {
                let (paths, site, provenance): (
                    Vec<&String>,
                    Option<&SiteFacts>,
                    &[ContractProvenance],
                ) = match part {
                    StringPart::Text(_) => continue,
                    StringPart::Splice(splice) => (
                        vec![&splice.values_path],
                        splice.meta.site.as_deref(),
                        &splice.meta.provenance,
                    ),
                    StringPart::Taint(taint) => (
                        taint.paths.iter().collect(),
                        taint.site.as_deref(),
                        &taint.provenance,
                    ),
                };
                let meta = taint_row_meta(site, provenance, conditions);
                for path in paths {
                    if path.trim().is_empty() {
                        continue;
                    }
                    let read = ValueRead {
                        values_path: path.clone(),
                        kind: ValueKind::Scalar,
                        condition: GuardDnf::from_conjunction(conditions.iter().cloned()),
                        resource: None,
                        provenance: meta.provenance.clone(),
                        dependency: true,
                    };
                    if !reads.contains(&read) {
                        reads.push(read);
                    }
                }
            }
        }
        AbstractFragment::Scalar(_) | AbstractFragment::Splice(_) | AbstractFragment::Opaque(_) => {
        }
    }
}