rsconstruct 0.9.131

Rust based fast build system
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
//! The `[processor]` section: declared processor instances, the per-processor
//! schema lookups (known/checksum/required fields) and the default values
//! applied to each instance before it is deserialized.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use super::{
    FieldProvenance, KnownFields, OUTPUT_DEPENDS_ON_INPUT_NAME, ProcessorDefaults, ProvenanceMap,
    SCAN_CONFIG_FIELDS, STANDARD_EXTRA_FIELDS, ScanDefaultsData, StandardConfig, provenance,
};
use crate::registries::{self as registry, ProcessorPlugin};

/// A single processor instance parsed from the TOML config.
/// `[processor.checker.pylint]` produces one instance with
/// `pname="processor.checker.pylint"`, `instance_name="processor.checker.pylint"`.
/// `[processor.checker.pylint.core]` produces one with the same `pname` and
/// `instance_name="processor.checker.pylint.core"`.
#[derive(Debug, Clone)]
pub struct ProcessorInstance {
    /// Instance name: the pname for a single instance, `pname.<name>` for a named one.
    pub instance_name: String,
    /// The processor's full name, `processor.<type>.<name>`.
    pub pname: String,
    /// The raw TOML config for this instance (deserialized lazily per processor type)
    pub config_toml: toml::Value,
    /// Source of every field in `config_toml` (user TOML, processor default, scan default, …).
    pub provenance: ProvenanceMap,
}

/// The plugin for a processor name (`processor.checker.ruff`); an instance
/// name finds the same plugin.
pub fn find_registry_entry(pname: &str) -> Option<&'static ProcessorPlugin> {
    registry::find_plugin(pname)
}

/// Return all registered processor plugins.
pub fn registry_entries() -> impl Iterator<Item = &'static ProcessorPlugin> {
    registry::all_plugins()
}

/// Check if a name is a registered processor (`processor.<type>.<name>`).
pub fn is_builtin_type(name: &str) -> bool {
    find_registry_entry(name).is_some()
}

/// Seed a provenance map with every top-level key currently present in `value`,
/// marking each as originating from the user's TOML. Defaults applied afterwards
/// will see these entries and not overwrite them.
///
/// Line numbers are filled in later by the `toml_edit` span pass; until then we
/// use line 0 as a sentinel meaning "from user TOML, line unknown".
pub fn seed_user_provenance(value: &toml::Value) -> ProvenanceMap {
    let mut map = ProvenanceMap::new();
    if let Some(table) = value.as_table() {
        for key in table.keys() {
            map.insert(key.clone(), FieldProvenance::UserToml { line: 0 });
        }
    }
    map
}

/// Resolve scan and processor defaults for an instance config in-place.
pub fn resolve_instance_defaults(
    type_name: &str,
    value: &mut toml::Value,
    provenance: &mut ProvenanceMap,
) {
    if find_registry_entry(type_name).is_some() {
        registry::apply_all_defaults(type_name, value, provenance);
    }
}

impl ProcessorConfig {
    /// Collect unique scan directories from all declared instances.
    pub(crate) fn src_dirs(&self) -> Vec<String> {
        let mut dirs: Vec<String> = self
            .instances
            .iter()
            .flat_map(|inst| {
                inst.config_toml
                    .get("src_dirs")
                    .and_then(|v| v.as_array())
                    .into_iter()
                    .flat_map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(std::string::ToString::to_string))
                    })
                    .filter(|d| !d.is_empty())
            })
            .collect();
        dirs.sort();
        dirs.dedup();
        dirs
    }

    /// Return known fields for a processor, or None when the name is not registered.
    pub(crate) fn known_fields_for(type_name: &str) -> Option<Vec<&'static str>> {
        let e = find_registry_entry(type_name)?;
        // Standard fields (minus this processor's omissions) plus the
        // plugin's own FieldSpec names. A spec sharing a standard field's
        // name replaces it, so the dedup keeps one copy.
        let mut fields: Vec<&'static str> = StandardConfig::known_fields()
            .iter()
            .copied()
            .filter(|f| !e.omit_standard_fields.contains(f))
            .collect();
        for spec in e.fields {
            if !fields.contains(&spec.name) {
                fields.push(spec.name);
            }
        }
        Some(fields)
    }

    /// Return checksum-affecting fields for a processor, or None when the name is not registered.
    pub(crate) fn checksum_fields_for(type_name: &str) -> Option<Vec<&'static str>> {
        let e = find_registry_entry(type_name)?;
        // A spec that shadows a standard field owns its checksum membership.
        let shadowed = |f: &&'static str| e.fields.iter().any(|s| s.name == *f);
        let mut fields: Vec<&'static str> = StandardConfig::checksum_fields()
            .iter()
            .copied()
            .filter(|f| !e.omit_standard_fields.contains(f))
            .filter(|f| !shadowed(f))
            .collect();
        for spec in e.fields {
            if spec.affects_output {
                fields.push(spec.name);
            }
        }
        Some(fields)
    }

    /// Return must fields (required non-empty fields) for a processor, or None when the name is not registered.
    pub(crate) fn must_fields_for(type_name: &str) -> Option<Vec<&'static str>> {
        let e = find_registry_entry(type_name)?;
        Some(
            e.fields
                .iter()
                .filter(|s| s.required)
                .map(|s| s.name)
                .collect(),
        )
    }

    /// Return (field, description) pairs for a processor, or None when the name is not registered.
    /// Standard-field descriptions come from `StandardConfig` (minus omissions,
    /// minus shadowed); the plugin's spec docs follow.
    pub(crate) fn field_descriptions_for(
        type_name: &str,
    ) -> Option<Vec<(&'static str, &'static str)>> {
        let e = find_registry_entry(type_name)?;
        let shadowed = |f: &str| e.fields.iter().any(|s| s.name == f);
        let mut descs: Vec<(&'static str, &'static str)> = StandardConfig::field_descriptions()
            .iter()
            .copied()
            .filter(|(f, _)| !e.omit_standard_fields.contains(f) && !shadowed(f))
            .collect();
        for spec in e.fields {
            descs.push((spec.name, spec.doc));
        }
        Some(descs)
    }

    /// Return the default config for a processor type as pretty JSON, or None if unknown.
    pub(crate) fn defconfig_json(type_name: &str) -> Option<String> {
        let entry = find_registry_entry(type_name)?;
        // The defaults machinery is keyed by the full name; the plugin's
        // short `name` finds nothing there and yields an all-empty config.
        (entry.defconfig_json)(&entry.pname())
    }
}

/// Return scan defaults for a builtin processor type. The data lives on the
/// processor's own plugin entry (`ProcessorPlugin.scan_defaults`) — this used
/// to be a 92-arm name-keyed match table here, which every new processor had
/// to remember to extend (and `prettier` once shipped without its arm,
/// silently mis-defaulted).
pub fn scan_defaults_for(type_name: &str) -> Option<ScanDefaultsData> {
    find_registry_entry(type_name)?.scan_defaults
}

/// Return per-processor default values (command, `dep_auto`, batch override).
/// Only needed for processors whose defaults differ from the struct's Default impl.
pub fn processor_defaults_for(type_name: &str) -> Option<ProcessorDefaults> {
    // Data lives on the plugin entry — see `scan_defaults_for`'s note; this
    // was the second of the two hand-synchronized default tables (V6's marp
    // args divergence was between this table and MarpConfig::default()).
    find_registry_entry(type_name)?.defaults
}

/// Apply processor-specific defaults to a config TOML value.
/// Sets command and `dep_auto` if they weren't explicitly provided by the user.
/// Every field that's actually injected is recorded in `provenance` as a processor default.
/// Fields already present in `provenance` (i.e. user-set) are skipped.
pub fn apply_processor_defaults(
    type_name: &str,
    value: &mut toml::Value,
    provenance: &mut ProvenanceMap,
) {
    let Some(defaults) = processor_defaults_for(type_name) else {
        return;
    };
    let Some(table) = value.as_table_mut() else {
        return;
    };
    set_string_default(
        table,
        "command",
        defaults.command,
        provenance,
        FieldProvenance::ProcessorDefault,
    );
    set_string_default(
        table,
        "output_dir",
        defaults.output_dir,
        provenance,
        FieldProvenance::ProcessorDefault,
    );
    set_string_array_default(
        table,
        "dep_auto",
        defaults.dep_auto,
        provenance,
        FieldProvenance::ProcessorDefault,
    );
    set_string_array_default(
        table,
        "formats",
        defaults.formats,
        provenance,
        FieldProvenance::ProcessorDefault,
    );
    set_string_array_default(
        table,
        "args",
        defaults.args,
        provenance,
        FieldProvenance::ProcessorDefault,
    );
    if let Some(batch) = defaults.batch
        && !table.contains_key("batch")
    {
        table.insert("batch".into(), toml::Value::Boolean(batch));
        provenance::record_if_absent(provenance, "batch", FieldProvenance::ProcessorDefault);
    }
    if defaults.output_depends_on_input_name && !table.contains_key(OUTPUT_DEPENDS_ON_INPUT_NAME) {
        table.insert(
            OUTPUT_DEPENDS_ON_INPUT_NAME.into(),
            toml::Value::Boolean(true),
        );
        provenance::record_if_absent(
            provenance,
            OUTPUT_DEPENDS_ON_INPUT_NAME,
            FieldProvenance::ProcessorDefault,
        );
    }
}

fn set_string_default(
    table: &mut toml::map::Map<String, toml::Value>,
    key: &str,
    val: &str,
    provenance: &mut ProvenanceMap,
    source: FieldProvenance,
) {
    if !val.is_empty() && !table.contains_key(key) {
        table.insert(key.into(), toml::Value::String(val.into()));
        provenance::record_if_absent(provenance, key, source);
    }
}

fn set_string_array_default(
    table: &mut toml::map::Map<String, toml::Value>,
    key: &str,
    vals: &[&str],
    provenance: &mut ProvenanceMap,
    source: FieldProvenance,
) {
    if !vals.is_empty() && !table.contains_key(key) {
        let arr: Vec<toml::Value> = vals
            .iter()
            .map(|s| toml::Value::String(s.to_string()))
            .collect();
        table.insert(key.into(), toml::Value::Array(arr));
        provenance::record_if_absent(provenance, key, source);
    }
}

fn set_empty_array_default(
    table: &mut toml::map::Map<String, toml::Value>,
    key: &str,
    provenance: &mut ProvenanceMap,
    source: FieldProvenance,
) {
    if !table.contains_key(key) {
        table.insert(key.into(), toml::Value::Array(Vec::new()));
        provenance::record_if_absent(provenance, key, source);
    }
}

fn set_maybe_empty_array_default(
    table: &mut toml::map::Map<String, toml::Value>,
    key: &str,
    vals: &[&str],
    provenance: &mut ProvenanceMap,
    source: FieldProvenance,
) {
    if !table.contains_key(key) {
        let arr: Vec<toml::Value> = vals
            .iter()
            .map(|s| toml::Value::String(s.to_string()))
            .collect();
        table.insert(key.into(), toml::Value::Array(arr));
        provenance::record_if_absent(provenance, key, source);
    }
}

/// Apply scan defaults to a config TOML value.
/// Sets `src_dirs`, `src_extensions`, and `src_exclude_dirs` if not explicitly provided.
/// Every field that's actually injected is recorded in `provenance` as a scan default.
pub fn apply_scan_defaults(
    type_name: &str,
    value: &mut toml::Value,
    provenance: &mut ProvenanceMap,
) {
    let Some(defaults) = scan_defaults_for(type_name) else {
        return;
    };
    let Some(table) = value.as_table_mut() else {
        return;
    };
    set_maybe_empty_array_default(
        table,
        "src_dirs",
        defaults.src_dirs,
        provenance,
        FieldProvenance::ScanDefault,
    );
    set_maybe_empty_array_default(
        table,
        "src_extensions",
        defaults.src_extensions,
        provenance,
        FieldProvenance::ScanDefault,
    );
    set_maybe_empty_array_default(
        table,
        "src_exclude_dirs",
        defaults.src_exclude_dirs,
        provenance,
        FieldProvenance::ScanDefault,
    );
    set_empty_array_default(
        table,
        "src_exclude_files",
        provenance,
        FieldProvenance::ScanDefault,
    );
    set_empty_array_default(
        table,
        "src_exclude_paths",
        provenance,
        FieldProvenance::ScanDefault,
    );
    set_empty_array_default(table, "src_files", provenance, FieldProvenance::ScanDefault);
}

/// Processor configuration: a collection of declared processor instances.
/// Each `[processor.TYPE]` or `[processor.TYPE.NAME]` section in rsconstruct.toml
/// creates a `ProcessorInstance`. No instances exist by default — only what's declared.
#[derive(Debug, Default)]
pub struct ProcessorConfig {
    /// All declared processor instances
    pub instances: Vec<ProcessorInstance>,
}

impl Serialize for ProcessorConfig {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        // Rebuild the `[processor]` tree the instances were read from:
        // processor → type → name → (fields | instance → fields).
        let mut root = toml::map::Map::new();
        for inst in &self.instances {
            let Some(parsed) = registry::parse_name(&inst.instance_name) else {
                continue;
            };
            let type_table = root
                .entry(parsed.processor_type.as_str().to_string())
                .or_insert_with(|| toml::Value::Table(toml::map::Map::new()));
            let Some(type_table) = type_table.as_table_mut() else {
                continue;
            };
            match parsed.instance {
                None => {
                    type_table.insert(parsed.short.to_string(), inst.config_toml.clone());
                }
                Some(instance) => {
                    let name_table = type_table
                        .entry(parsed.short.to_string())
                        .or_insert_with(|| toml::Value::Table(toml::map::Map::new()));
                    if let Some(name_table) = name_table.as_table_mut() {
                        name_table.insert(instance.to_string(), inst.config_toml.clone());
                    }
                }
            }
        }
        toml::Value::Table(root).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ProcessorConfig {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> std::result::Result<Self, D::Error> {
        let table = toml::Value::deserialize(deserializer)?;
        Ok(Self::from_toml(&table))
    }
}

/// What a `[processor.TYPE.NAME]` section turned out to be.
///
/// The shape is inferred from the section's contents, so it can be
/// genuinely undecidable — which is why this is an enum rather than a bool.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SectionShape {
    /// Direct config fields: `[processor.checker.pylint]` with `args = [...]`.
    SingleInstance,
    /// Named sub-instances: `[processor.checker.pylint.core]`, `[processor.checker.pylint.tests]`.
    MultiInstance,
    /// Reads as both. Carries the keys that are simultaneously known config
    /// field names and plausible instance names.
    Ambiguous { colliding: Vec<String> },
}

impl ProcessorConfig {
    /// Parse the `[processor]` table from TOML into instances.
    pub(crate) fn from_toml(value: &toml::Value) -> Self {
        let Some(table) = value.as_table() else {
            return Self::default();
        };

        let mut instances = Vec::new();

        // The tree is processor → type → name → (fields | instances). Anything
        // that does not fit is skipped here and reported by
        // `validate_processor_fields_raw`, which runs on the same raw value.
        for (type_key, type_val) in table {
            let Some(type_table) = type_val.as_table() else {
                continue;
            };
            if crate::processor::ProcessorType::parse(type_key).is_none() {
                continue;
            }
            for (short, val) in type_table {
                let Some(sub_table) = val.as_table() else {
                    continue;
                };
                let pname = format!("{}{type_key}.{short}", registry::NAME_PREFIX);
                if !is_builtin_type(&pname) {
                    continue;
                }
                if Self::is_multi_instance(&pname, sub_table) {
                    // Multi-instance: [processor.checker.pylint.core], [processor.checker.pylint.tests]
                    for (name, inst_val) in sub_table {
                        let instance_name = format!("{pname}.{name}");
                        let mut config = inst_val.clone();
                        let mut provenance = seed_user_provenance(&config);
                        resolve_instance_defaults(&pname, &mut config, &mut provenance);
                        instances.push(ProcessorInstance {
                            instance_name,
                            pname: pname.clone(),
                            config_toml: config,
                            provenance,
                        });
                    }
                } else {
                    // Single instance: [processor.checker.pylint]
                    let mut config = val.clone();
                    let mut provenance = seed_user_provenance(&config);
                    resolve_instance_defaults(&pname, &mut config, &mut provenance);
                    instances.push(ProcessorInstance {
                        instance_name: pname.clone(),
                        pname,
                        config_toml: config,
                        provenance,
                    });
                }
            }
        }

        Self { instances }
    }

    /// Determine if a processor section contains named sub-instances (multi-instance)
    /// or direct config fields (single-instance).
    ///
    /// Heuristic: if ALL values are tables AND none of the keys match known config
    /// field names for this processor type, it's multi-instance. An ambiguous
    /// section is treated as single-instance here; `parse_processors` rejects
    /// it outright, so this only affects callers that have already validated.
    fn is_multi_instance(type_name: &str, table: &toml::map::Map<String, toml::Value>) -> bool {
        matches!(
            Self::classify_section(type_name, table),
            SectionShape::MultiInstance
        )
    }

    /// The shape of a `[processor.TYPE.NAME]` section, and whether it is even
    /// decidable.
    ///
    /// Separated from `is_multi_instance` so the ambiguous case can be
    /// reported rather than silently resolved. The heuristic reads a
    /// section's *shape* to guess the user's intent, which means a section
    /// that looks like both is a genuine ambiguity — and resolving it
    /// quietly (as returning a bare bool forced) is how a config could
    /// change meaning under the user without warning.
    pub(super) fn classify_section(
        type_name: &str,
        table: &toml::map::Map<String, toml::Value>,
    ) -> SectionShape {
        if table.is_empty() {
            return SectionShape::SingleInstance;
        }

        let Some(known) = Self::known_fields_for(type_name) else {
            // Not a registered processor: no field list to compare against, so
            // multi-instance is undecidable and the section is taken as one
            // instance. See `multi_instance_requires_known_fields` in the docs.
            return SectionShape::SingleInstance;
        };
        let known_fields: Vec<&str> = known
            .iter()
            .chain(SCAN_CONFIG_FIELDS.iter())
            .chain(STANDARD_EXTRA_FIELDS.iter())
            .copied()
            .collect();

        let field_keys: Vec<&str> = table
            .keys()
            .filter(|k| known_fields.contains(&k.as_str()))
            .map(String::as_str)
            .collect();
        let all_values_are_tables = table.values().all(toml::Value::is_table);

        match (field_keys.is_empty(), all_values_are_tables) {
            // Only sub-tables, no known field names: unambiguously instances.
            (true, true) => SectionShape::MultiInstance,
            // At least one known field name present.
            (false, _) => {
                // A known field whose value is a table, alongside nothing but
                // tables, is the ambiguous case: it reads as both "a config
                // field" and "an instance named after a field".
                if all_values_are_tables {
                    SectionShape::Ambiguous {
                        colliding: field_keys.iter().map(|s| (*s).to_string()).collect(),
                    }
                } else {
                    SectionShape::SingleInstance
                }
            }
            // Scalar values present and no known fields: malformed, but
            // validation reports unknown fields far better than we could.
            (true, false) => SectionShape::SingleInstance,
        }
    }

    /// Resolve scan defaults for all instances.
    pub(crate) fn resolve_scan_defaults(&mut self) {
        for inst in &mut self.instances {
            resolve_instance_defaults(&inst.pname, &mut inst.config_toml, &mut inst.provenance);
        }
    }

    /// Rewrite output paths in processor configs:
    /// 1. For named instances (e.g., `pylint.core`), remap default `out/{type_name}`
    ///    to `out/{instance_name}` so each instance gets its own output directory.
    /// 2. If the global `output_dir` is not "out", replace the `out/` prefix with the
    ///    global value (e.g., `build/` → `build/marp`).
    ///
    /// When a field was originally a `ProcessorDefault` and gets rewritten here, its
    /// provenance is upgraded to `OutputDirDefault` so `config show` can explain why
    /// the value differs from the processor's own default.
    pub(crate) fn apply_output_dir_defaults(&mut self, global_output_dir: &str) {
        for inst in &mut self.instances {
            let type_default_prefix = format!("out/{}", inst.pname);
            let instance_prefix = format!("{}/{}", global_output_dir, inst.instance_name);

            for field in &["output_dir", "output"] {
                // A user-set or CLI-set value is explicitly chosen — never
                // rewrite it (previously only the provenance label was
                // guarded while the value itself was still rewritten).
                if matches!(
                    inst.provenance.get(*field),
                    Some(
                        FieldProvenance::UserToml { .. }
                            | FieldProvenance::LocalToml { .. }
                            | FieldProvenance::CliOverride
                    ),
                ) {
                    continue;
                }
                let Some(val) = inst
                    .config_toml
                    .get(field)
                    .and_then(|v| v.as_str())
                    .map(std::string::ToString::to_string)
                else {
                    continue;
                };
                // Prefix matches must respect path boundaries: `out/marp`
                // must not match `out/marpdeck/...`.
                let type_rest = val
                    .strip_prefix(&type_default_prefix)
                    .filter(|r| r.is_empty() || r.starts_with('/'));
                let new_val = if inst.instance_name != inst.pname
                    && let Some(rest) = type_rest
                {
                    // Named instance: remap out/{type} → {global}/{instance}
                    format!("{instance_prefix}{rest}")
                } else if global_output_dir != "out"
                    && let Some(rest) = val.strip_prefix("out/")
                {
                    // Global output dir override: remap out/ → {global}/
                    format!("{global_output_dir}/{rest}")
                } else {
                    continue;
                };
                if let Some(table) = inst.config_toml.as_table_mut() {
                    table.insert(field.to_string(), toml::Value::String(new_val));
                }
                inst.provenance
                    .insert((*field).to_string(), FieldProvenance::OutputDirDefault);
            }
        }
    }

    /// Get the first instance of a given type (for single-instance access).
    /// Returns None if no instance of that type is declared.
    pub(crate) fn first_instance_of_type(&self, type_name: &str) -> Option<&ProcessorInstance> {
        self.instances.iter().find(|i| i.pname == type_name)
    }

    /// Deserialize the config for `type_name`, whether or not the user
    /// declared the instance.
    ///
    /// A declared instance already has registry defaults baked into its
    /// `config_toml` by `Config::load`. An undeclared one has nothing, and
    /// `C::default()` leaves scan fields unresolved — reaching a scan
    /// accessor then panics with "scan fields not resolved". So the
    /// undeclared case is routed through the same `apply_all_defaults` the
    /// declared case used, instead of each caller hand-resolving.
    pub(crate) fn instance_config_or_default<C: serde::de::DeserializeOwned>(
        &self,
        type_name: &str,
    ) -> Result<C> {
        if let Some(inst) = self.first_instance_of_type(type_name) {
            return inst
                .config_toml
                .clone()
                .try_into()
                .with_context(|| format!("Failed to parse [{type_name}] config"));
        }
        let mut value = toml::Value::Table(toml::map::Map::new());
        let mut provenance = ProvenanceMap::new();
        crate::registries::processor::apply_all_defaults(type_name, &mut value, &mut provenance);
        value
            .try_into()
            .with_context(|| format!("Failed to build default config for processor '{type_name}'"))
    }

    /// Get a typed config value from an instance's TOML config.
    /// Returns the default if the instance is not declared or the field is missing.
    pub(crate) fn instance_field_str(&self, type_name: &str, field: &str) -> Option<String> {
        self.first_instance_of_type(type_name)
            .and_then(|inst| inst.config_toml.get(field))
            .and_then(|v| v.as_str())
            .map(std::string::ToString::to_string)
    }
}