rustev-core 0.7.0

Rustev's pure core: validation, value kinds, exact operators, compiler, calibration binding and selection policy.
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
//! Pure fallback extraction (spec 038, 3.7), without runtime or compiler wiring.
use std::collections::{BTreeMap, BTreeSet};

use rustev_contract::{
    Document,
    definition::{InputDecl, ProvenanceClass, TypeDecl},
    extraction::{
        AbsenceReason as Reason, Distributions, Extract, ExtractionSchema, ExtractionType, Fill,
    },
    extraction_documents::{
        DateFormat, ExtractRules, ExtractionCondition as Condition, ExtractionValue as Expr, Scalar,
    },
    extraction_results::{ExtractionFieldEvidence, ExtractionOrigin},
    judgment::Derivation,
    output::RawOutput,
};
use serde_json::Value as Json;

use crate::extraction_validate::{self, ValidatedField, fallback_derivation};

/// Screening for exact steps has already been applied by the caller. A source
/// withheld only from model steps belongs in `Readable`, not `Unavailable`.
#[derive(Debug, Clone, Copy)]
pub enum Source<'a> {
    Readable {
        bytes: &'a [u8],
        derivation: Derivation,
    },
    /// An `integer`, `bool` or `enum` input value, which only an `equals` or
    /// `one_of` condition reads (spec 070, 3.4). An enum is its option text.
    Exact {
        value: &'a Scalar,
        derivation: Derivation,
    },
    Unavailable,
}

/// A validated field plus sorted, unique input references actually read while
/// evaluating it, including reads by rules that did not produce its value.
#[derive(Debug, Clone, PartialEq)]
pub struct FallbackField {
    pub field: ValidatedField,
    pub sources_read: Vec<String>,
}

/// Only `bind` constructs this, checking document bounds and step compatibility.
#[derive(Debug, Clone)]
pub struct BoundProgram {
    program: ExtractRules,
    schema: ExtractionSchema,
}

/// Check spec 038, 3.7.2 and 3.7.3. Errors are for the compiler to map to
/// `invalid_extraction`. Literal bounds and vocabulary membership are evaluated
/// later by the shared field validator, against resolved vocabularies.
pub fn bind(program: &ExtractRules, step: &Extract) -> Result<BoundProgram, String> {
    bind_in(program, step, &[])
}

/// [`bind`], with the definition's inputs, which spec 070's binding checks
/// read (3.5): a source a version 2 extractor reads is a declared `text`
/// input or record field. Without them no such source binds.
pub fn bind_in(
    program: &ExtractRules,
    step: &Extract,
    inputs: &[InputDecl],
) -> Result<BoundProgram, String> {
    // Reuse document-local checks even for directly constructed Rust values.
    let bytes = serde_json::to_vec(program).map_err(|e| e.to_string())?;
    let program = ExtractRules::parse(&bytes).map_err(|e| e.to_string())?;
    // Spec 070, 3.4.2: a source read only by exact-value conditions need
    // not be projected, because only the core reads it.
    let mut text_read = BTreeSet::new();
    let mut exact_read = BTreeSet::new();
    for r in program.fields.iter().flat_map(|f| &f.rules) {
        condition_reads(&r.when, &mut text_read, &mut exact_read);
        value_sources(&r.then, &mut text_read);
    }
    let mut names = BTreeSet::new();
    for s in &program.sources {
        let core_only =
            exact_read.contains(s.name.as_str()) && !text_read.contains(s.name.as_str());
        if (!core_only && !step.projection.contains(&s.reference))
            || !s.reference.starts_with("input:")
        {
            return Err(format!("source {} is not in the input projection", s.name));
        }
        if !names.insert(s.name.as_str()) {
            return Err(format!("duplicate source {}", s.name));
        }
    }
    let mut fields = BTreeSet::new();
    for f in &program.fields {
        if !fields.insert(&f.field) {
            return Err(format!("duplicate field {}", f.field));
        }
        let declared = step
            .output
            .fields
            .iter()
            .find(|d| d.name == f.field)
            .ok_or_else(|| format!("undeclared field {}", f.field))?;
        let mut literals = Vec::new();
        for rule in &f.rules {
            let mut used = BTreeSet::new();
            condition_sources(&rule.when, &mut used);
            value_sources(&rule.then, &mut used);
            if let Some(s) = used.iter().find(|s| !names.contains(**s)) {
                return Err(format!("undeclared source {s}"));
            }
            extractors_v2(&program, &rule.then, inputs)?;
            exact_conditions(&program, &rule.when, inputs)?;
            vocabulary_values(&rule.then, &declared.ty, step)?;
            if let ExtractionType::List { item, .. } = &declared.ty {
                let Expr::Literal(v) = &rule.then else {
                    return Err(format!("list field {} requires literal items", f.field));
                };
                if !literal_type(v, item) {
                    return Err(format!("wrong list item type for {}", f.field));
                }
                if literals.contains(&v) {
                    return Err(format!("duplicate literal item for {}", f.field));
                }
                literals.push(v);
            } else if !value_type(&rule.then, &declared.ty) {
                return Err(format!("incompatible value for {}", f.field));
            }
        }
    }
    Ok(BoundProgram {
        program,
        schema: step.output.clone(),
    })
}

fn literal_type(v: &Json, ty: &ExtractionType) -> bool {
    match ty {
        ExtractionType::Bool {} => v.is_boolean(),
        ExtractionType::Integer { .. } => v.is_i64() || v.is_u64(),
        ExtractionType::Decimal(_)
        | ExtractionType::Text { .. }
        | ExtractionType::Date {}
        | ExtractionType::Enum { .. } => v.is_string(),
        ExtractionType::Record { fields } => v.as_object().is_some_and(|obj| {
            obj.iter().all(|(name, value)| {
                fields
                    .iter()
                    .any(|f| f.name == *name && literal_type(value, &f.ty))
            })
        }),
        ExtractionType::List { .. } => false,
    }
}

fn value_type(v: &Expr, ty: &ExtractionType) -> bool {
    match (v, ty) {
        (Expr::Literal(v), ty) => literal_type(v, ty),
        (Expr::IntegerAfter { .. }, ExtractionType::Integer { .. })
        | (Expr::DateIso { .. }, ExtractionType::Date {})
        | (Expr::TokenAfter { .. } | Expr::TokenMatching { .. }, ExtractionType::Text { .. })
        | (Expr::Date { .. }, ExtractionType::Date {})
        | (Expr::VocabularyAfter { .. }, ExtractionType::Text { .. }) => true,
        (Expr::VocabularyAfter { vocabulary, .. }, ExtractionType::Enum { vocabulary: v }) => {
            vocabulary == v
        }
        (Expr::Record(values), ExtractionType::Record { fields }) => values
            .iter()
            .all(|(n, v)| fields.iter().any(|f| f.name == *n && value_type(v, &f.ty))),
        _ => false,
    }
}

/// The declared type of an input reference `input:<name>` or
/// `input:<name>/<field>`.
fn input_type<'a>(inputs: &'a [InputDecl], reference: &str) -> Option<&'a TypeDecl> {
    let path = reference.strip_prefix("input:")?;
    let (name, field) = match path.split_once('/') {
        Some((name, field)) => (name, Some(field)),
        None => (path, None),
    };
    let ty = &inputs.iter().find(|i| i.name == name)?.ty;
    match (ty, field) {
        (ty, None) => Some(ty),
        (TypeDecl::Record { fields, .. }, Some(field)) => {
            fields.iter().find(|f| f.name == field).map(|f| &f.ty)
        }
        _ => None,
    }
}

/// Spec 070, 3.5.1 and 3.5.4: a version 2 extractor reads a projected
/// `text` source, and a date names a declared month table exactly when its
/// format has month names.
fn extractors_v2(program: &ExtractRules, v: &Expr, inputs: &[InputDecl]) -> Result<(), String> {
    let source = match v {
        Expr::Record(vs) => {
            return vs
                .values()
                .try_for_each(|v| extractors_v2(program, v, inputs));
        }
        Expr::Literal(_) | Expr::IntegerAfter { .. } | Expr::DateIso { .. } => return Ok(()),
        Expr::Date {
            source,
            format,
            months,
            ..
        } => {
            let named = matches!(format, DateFormat::DmyName | DateFormat::MdyName);
            match months {
                None if named => return Err(format!("date format {format:?} requires months")),
                Some(_) if !named => {
                    return Err(format!("date format {format:?} takes no months"));
                }
                Some(t)
                    if !program
                        .month_tables
                        .as_ref()
                        .is_some_and(|tables| tables.contains_key(t)) =>
                {
                    return Err(format!("undeclared month table {t}"));
                }
                _ => {}
            }
            source
        }
        Expr::TokenAfter { source, .. }
        | Expr::TokenMatching { source, .. }
        | Expr::VocabularyAfter { source, .. } => source,
    };
    let reference = program
        .sources
        .iter()
        .find(|s| s.name == *source)
        .map(|s| s.reference.as_str())
        .unwrap_or_default();
    if !matches!(input_type(inputs, reference), Some(TypeDecl::Text { .. })) {
        return Err(format!(
            "source {source} of a version 2 extractor is not text"
        ));
    }
    Ok(())
}

fn condition_sources<'a>(c: &'a Condition, out: &mut BTreeSet<&'a str>) {
    let mut exact = BTreeSet::new();
    condition_reads(c, out, &mut exact);
    out.extend(exact);
}

/// The sources text conditions read, and those exact-value conditions read.
fn condition_reads<'a>(
    c: &'a Condition,
    text: &mut BTreeSet<&'a str>,
    exact: &mut BTreeSet<&'a str>,
) {
    match c {
        Condition::Always => {}
        Condition::ContainsAny { source, .. } | Condition::ContainsAll { source, .. } => {
            text.insert(source);
        }
        Condition::Equals { source, .. } | Condition::OneOf { source, .. } => {
            exact.insert(source);
        }
        Condition::All(cs) | Condition::Any(cs) => {
            for c in cs {
                condition_reads(c, text, exact);
            }
        }
        Condition::Not(c) => condition_reads(c, text, exact),
    }
}

/// Spec 070, 3.4.2 and 3.5.2: an exact-value condition reads a non-nullable
/// `text`, `integer`, `bool` or `enum` input or record field whose accepted
/// provenance excludes `model-derived`, and every value is of its type (an
/// enum value one of its options).
fn exact_conditions(
    program: &ExtractRules,
    c: &Condition,
    inputs: &[InputDecl],
) -> Result<(), String> {
    let (source, values) = match c {
        Condition::Always | Condition::ContainsAny { .. } | Condition::ContainsAll { .. } => {
            return Ok(());
        }
        Condition::All(cs) | Condition::Any(cs) => {
            return cs
                .iter()
                .try_for_each(|c| exact_conditions(program, c, inputs));
        }
        Condition::Not(c) => return exact_conditions(program, c, inputs),
        Condition::Equals { source, value } => (source, std::slice::from_ref(value)),
        Condition::OneOf { source, values } => (source, values.as_slice()),
    };
    let reference = program
        .sources
        .iter()
        .find(|s| s.name == *source)
        .map(|s| s.reference.as_str())
        .unwrap_or_default();
    let input = reference
        .strip_prefix("input:")
        .map(|p| p.split('/').next().unwrap_or(p))
        .and_then(|name| inputs.iter().find(|i| i.name == name));
    if input.is_none_or(|i| i.provenance.contains(&ProvenanceClass::ModelDerived)) {
        return Err(format!(
            "condition source {source} is not an input that excludes model-derived"
        ));
    }
    let fits = |v: &Scalar| match (input_type(inputs, reference), v) {
        (Some(TypeDecl::Text { .. }), Scalar::Text(_))
        | (Some(TypeDecl::Integer), Scalar::Integer(_))
        | (Some(TypeDecl::Bool), Scalar::Bool(_)) => true,
        (Some(TypeDecl::Enum { options }), Scalar::Text(t)) => options.contains(t),
        _ => false,
    };
    if let Some(v) = values.iter().find(|v| !fits(v)) {
        return Err(format!(
            "condition value {v:?} does not fit the type of source {source}"
        ));
    }
    Ok(())
}

/// Spec 070, 3.5.5: `vocabulary_after` names a vocabulary the step declares;
/// its field type is checked with the other values.
fn vocabulary_values(v: &Expr, ty: &ExtractionType, step: &Extract) -> Result<(), String> {
    match (v, ty) {
        (Expr::VocabularyAfter { vocabulary, .. }, _) => {
            if !step.vocabularies.contains_key(vocabulary) {
                return Err(format!("undeclared vocabulary {vocabulary}"));
            }
            Ok(())
        }
        (Expr::Record(vs), ExtractionType::Record { fields }) => {
            vs.iter()
                .try_for_each(|(n, v)| match fields.iter().find(|f| f.name == *n) {
                    Some(f) => vocabulary_values(v, &f.ty, step),
                    None => Ok(()),
                })
        }
        _ => Ok(()),
    }
}

/// The paths of the extract fields a `vocabulary_after` rule can fill, as
/// `(field, subfield)`: their values are graded `hint` (spec 070, 3.8.2).
pub fn hint_paths(program: &ExtractRules) -> BTreeSet<(String, Option<String>)> {
    let mut out = BTreeSet::new();
    for f in &program.fields {
        for r in &f.rules {
            match &r.then {
                Expr::VocabularyAfter { .. } => {
                    out.insert((f.field.clone(), None));
                }
                Expr::Record(vs) => {
                    for (n, v) in vs {
                        if matches!(v, Expr::VocabularyAfter { .. }) {
                            out.insert((f.field.clone(), Some(n.clone())));
                        }
                    }
                }
                _ => {}
            }
        }
    }
    out
}

fn value_sources<'a>(v: &'a Expr, out: &mut BTreeSet<&'a str>) {
    match v {
        Expr::Literal(_) => {}
        Expr::IntegerAfter { source, .. }
        | Expr::DateIso { source, .. }
        | Expr::TokenAfter { source, .. }
        | Expr::TokenMatching { source, .. }
        | Expr::Date { source, .. }
        | Expr::VocabularyAfter { source, .. } => {
            out.insert(source);
        }
        Expr::Record(vs) => {
            for v in vs.values() {
                value_sources(v, out);
            }
        }
    }
}

struct Reads<'a> {
    months: &'a BTreeMap<String, Vec<Vec<String>>>,
    vocabularies: &'a BTreeMap<String, Vec<String>>,
    /// A `vocabulary_after` rule was skipped because its vocabulary was
    /// unavailable (spec 070, 3.3.5).
    vocabulary_unavailable: bool,
    sources: &'a BTreeMap<String, Source<'a>>,
    references: &'a BTreeMap<&'a str, &'a str>,
    max_bytes: u64,
    read: BTreeMap<String, Derivation>,
}
impl<'a> Reads<'a> {
    fn get(&mut self, source: &str) -> Result<&'a [u8], Reason> {
        let reference = self
            .references
            .get(source)
            .ok_or(Reason::SourceUnavailable)?;
        match self.sources.get(*reference) {
            Some(Source::Readable { bytes, derivation })
                if bytes.len() as u64 <= self.max_bytes =>
            {
                self.read.insert((*reference).into(), *derivation);
                Ok(bytes)
            }
            _ => Err(Reason::SourceUnavailable),
        }
    }

    /// An exact-value condition's read: the text bytes or the scalar value.
    fn exact(&mut self, source: &str) -> Result<Exact<'a>, Reason> {
        let reference = self
            .references
            .get(source)
            .ok_or(Reason::SourceUnavailable)?;
        match self.sources.get(*reference) {
            Some(Source::Exact { value, derivation }) => {
                self.read.insert((*reference).into(), *derivation);
                Ok(Exact::Value(value))
            }
            Some(Source::Readable { .. }) => self.get(source).map(Exact::Text),
            _ => Err(Reason::SourceUnavailable),
        }
    }
}

enum Exact<'a> {
    Text(&'a [u8]),
    Value(&'a Scalar),
}

/// Spec 070, 3.4.3: bytes with no folding, integers and booleans by value.
fn equal(read: &Exact<'_>, v: &Scalar) -> bool {
    match (read, v) {
        (Exact::Text(bytes), Scalar::Text(t)) => *bytes == t.as_bytes(),
        (Exact::Value(value), v) => *value == v,
        _ => false,
    }
}

// Spec 005, 3.4.4: the backend uses to_ascii_lowercase; its private
// implementation cannot be imported into core. Other bytes remain unchanged.
fn occurrence(text: &[u8], term: &str) -> Option<usize> {
    text.windows(term.len())
        .position(|w| w.eq_ignore_ascii_case(term.as_bytes()))
}
fn condition(c: &Condition, reads: &mut Reads<'_>) -> Result<bool, Reason> {
    Ok(match c {
        Condition::Always => true,
        Condition::ContainsAny { source, terms } => {
            let text = reads.get(source)?;
            terms.iter().any(|t| occurrence(text, t).is_some())
        }
        Condition::ContainsAll { source, terms } => {
            let text = reads.get(source)?;
            terms.iter().all(|t| occurrence(text, t).is_some())
        }
        Condition::All(cs) => {
            for c in cs {
                if !condition(c, reads)? {
                    return Ok(false);
                }
            }
            true
        }
        Condition::Any(cs) => {
            for c in cs {
                if condition(c, reads)? {
                    return Ok(true);
                }
            }
            false
        }
        Condition::Not(c) => !condition(c, reads)?,
        // An unreadable source ends the field's evaluation: never false
        // (spec 070, 3.4.5).
        Condition::Equals { source, value } => equal(&reads.exact(source)?, value),
        Condition::OneOf { source, values } => {
            let read = reads.exact(source)?;
            values.iter().any(|v| equal(&read, v))
        }
    })
}
fn value(v: &Expr, reads: &mut Reads<'_>) -> Result<Option<Json>, Reason> {
    Ok(match v {
        Expr::Literal(v) => Some(v.clone()),
        Expr::IntegerAfter {
            source,
            terms,
            max_digits,
        } => {
            let text = reads.get(source)?;
            // Earliest byte offset wins; equal offsets keep declared term order.
            let Some((i, len)) = crate::extraction_match::first_term(text, terms) else {
                return Ok(None);
            };
            let tail = &text[i + len..];
            let spaces = tail.iter().take_while(|b| **b == b' ').count();
            let digits = &tail[spaces..];
            let n = digits.iter().take_while(|b| b.is_ascii_digit()).count();
            if n == 0 || n > usize::from(*max_digits) {
                return Ok(None);
            }
            Some(Json::from(
                digits[..n]
                    .iter()
                    .fold(0i64, |v, b| v * 10 + i64::from(b - b'0')),
            ))
        }
        Expr::DateIso { source, occurrence } => {
            let text = reads.get(source)?;
            text.windows(10)
                .filter(|w| {
                    w.iter().enumerate().all(|(i, b)| {
                        if i == 4 || i == 7 {
                            *b == b'-'
                        } else {
                            b.is_ascii_digit()
                        }
                    })
                })
                .nth(usize::from(*occurrence) - 1)
                .map(|w| Json::String(w.iter().map(|b| char::from(*b)).collect()))
        }
        Expr::Record(fields) => {
            let mut obj = serde_json::Map::new();
            for (name, v) in fields {
                let Some(v) = value(v, reads)? else {
                    return Ok(None);
                };
                obj.insert(name.clone(), v);
            }
            Some(Json::Object(obj))
        }
        Expr::TokenAfter {
            source,
            terms,
            charset,
            min_len,
            max_len,
        } => crate::extraction_match::token_after(
            reads.get(source)?,
            terms,
            charset,
            *min_len,
            *max_len,
        )
        .map(Json::String),
        Expr::TokenMatching {
            source,
            shape,
            occurrence,
        } => crate::extraction_match::token_matching(
            reads.get(source)?,
            shape,
            occurrence.unwrap_or(1),
        )
        .map(Json::String),
        Expr::Date {
            source,
            format,
            months,
            occurrence,
        } => {
            let text = reads.get(source)?;
            let table = months
                .as_ref()
                .and_then(|t| reads.months.get(t))
                .map_or(&[][..], Vec::as_slice);
            crate::extraction_match::date(text, *format, table, *occurrence).map(Json::String)
        }
        Expr::VocabularyAfter {
            source,
            terms,
            vocabulary,
        } => {
            let text = reads.get(source)?;
            let Some(options) = reads.vocabularies.get(vocabulary) else {
                reads.vocabulary_unavailable = true;
                return Ok(None);
            };
            crate::extraction_match::word_after(text, terms)
                .filter(|w| options.iter().any(|o| o == w))
                .map(Json::String)
        }
    })
}

impl BoundProgram {
    /// Sources are keyed by input reference, vocabularies by declared name.
    /// Boolean conditions short-circuit in declared order; only reached reads
    /// contribute lineage. An unavailable reached source stops that field.
    pub fn evaluate(
        &self,
        sources: &BTreeMap<String, Source<'_>>,
        vocabularies: &BTreeMap<String, Vec<String>>,
        max_projection_bytes: u64,
    ) -> Vec<FallbackField> {
        let months = self.program.month_tables.clone().unwrap_or_default();
        let references = self
            .program
            .sources
            .iter()
            .map(|s| (s.name.as_str(), s.reference.as_str()))
            .collect();
        self.schema
            .fields
            .iter()
            .map(|field| {
                let mut reads = Reads {
                    months: &months,
                    vocabularies,
                    vocabulary_unavailable: false,
                    sources,
                    references: &references,
                    max_bytes: max_projection_bytes,
                    read: BTreeMap::new(),
                };
                let result = (|| {
                    let mut items = Vec::new();
                    if let Some(f) = self.program.fields.iter().find(|f| f.field == field.name) {
                        for rule in &f.rules {
                            if condition(&rule.when, &mut reads)?
                                && let Some(v) = value(&rule.then, &mut reads)?
                            {
                                if matches!(field.ty, ExtractionType::List { .. }) {
                                    items.push(v);
                                } else {
                                    return Ok(v);
                                }
                            }
                        }
                    }
                    if items.is_empty() {
                        Err(if reads.vocabulary_unavailable {
                            Reason::VocabularyUnavailable
                        } else {
                            Reason::FallbackNoMatch
                        })
                    } else {
                        Ok(Json::Array(items))
                    }
                })();
                let validated = result
                    .and_then(|v| {
                        let schema = ExtractionSchema {
                            fields: vec![field.clone()],
                        };
                        let raw = RawOutput::TypedRecord {
                            fields: BTreeMap::from([(field.name.clone(), v)]),
                            logprobs: vec![],
                            provider_reported: BTreeMap::new(),
                        };
                        let mut validated = extraction_validate::validate(
                            &schema,
                            vocabularies,
                            &raw,
                            Distributions::None,
                            false,
                        )
                        .map_err(|_| Reason::FallbackInvalid)?
                        .remove(0);
                        // A failing fallback item invalidates the fallback field; never
                        // silently salvage a partially invalid deterministic result.
                        if validated.value.is_none() || !validated.evidence.rejected.is_empty() {
                            return Err(Reason::FallbackInvalid);
                        }
                        validated.evidence.origin = ExtractionOrigin::Fallback;
                        validated.evidence.derivation =
                            Some(fallback_derivation(reads.read.values().copied()));
                        Ok(validated)
                    })
                    .unwrap_or_else(|reason| ValidatedField {
                        value: None,
                        evidence: ExtractionFieldEvidence::new(
                            field.name.clone(),
                            &field.ty,
                            ExtractionOrigin::Absent { reason },
                            None,
                            None,
                            None,
                            vec![],
                        ),
                    });
                FallbackField {
                    field: validated,
                    sources_read: reads.read.into_keys().collect(),
                }
            })
            .collect()
    }
}

/// Merge by field name, preserving model order and every present model field.
/// `request_produced_output` distinguishes an accepted record with absent fields
/// from a request failure. The caller supplies model entries for the full schema.
/// Returns source lineage only for fallback entries actually selected by the fill.
pub fn merge(
    model: &mut [ValidatedField],
    fallback: &[FallbackField],
    fill: Fill,
    request_produced_output: bool,
) -> BTreeMap<String, Vec<String>> {
    let mut reads = BTreeMap::new();
    if fill == Fill::RequestFailure && request_produced_output {
        return reads;
    }
    for field in model {
        if field.value.is_none()
            && let Some(f) = fallback
                .iter()
                .find(|f| f.field.evidence.name == field.evidence.name)
        {
            *field = f.field.clone();
            reads.insert(field.evidence.name.clone(), f.sources_read.clone());
        }
    }
    reads
}