xberg 1.1.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! Pipeline initialization and setup logic.
//!
//! This module handles the initialization of features and processor cache
//! required for pipeline execution.

use crate::Result;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{LazyLock, Mutex, RwLock};

use super::cache::{PROCESSOR_CACHE, ProcessorCache};

/// Records the outcome of the latest built-in post-processor registration pass
/// so callers can learn when registration was incomplete.
static BUILTIN_REGISTRATION_ERROR: RwLock<Option<String>> = RwLock::new(None);
#[cfg(test)]
static FORCED_BUILTIN_REGISTRATION_ERROR: RwLock<Option<String>> = RwLock::new(None);
static BUILTIN_REGISTRATION_REQUIRED: AtomicBool = AtomicBool::new(true);
/// ~keep Odd epochs bracket registry mutation; cache snapshots publish only across one unchanged
/// even epoch, so public clear cannot expose its intentionally empty intermediate registry.
static BUILTIN_REGISTRATION_EPOCH: AtomicU64 = AtomicU64::new(0);
static BUILTIN_REGISTRATION_LOCK: Mutex<()> = Mutex::new(());
/// ~keep A validated snapshot holds a lease through processor execution; lifecycle mutations
/// fail as retryable while a lease is active, so shutdown never overlaps processing or blocks an async worker.
static ACTIVE_PROCESSOR_SNAPSHOTS: AtomicUsize = AtomicUsize::new(0);
static AUTOMATIC_REGISTRATION_SUPPRESSIONS: LazyLock<Mutex<HashSet<String>>> =
    LazyLock::new(|| Mutex::new(HashSet::new()));

#[cfg(test)]
type InitializationHook = Box<dyn FnOnce() + Send>;
#[cfg(test)]
type InitializationRetryHook = Box<dyn FnMut() + Send>;
#[cfg(test)]
std::thread_local! {
    static BEFORE_REGISTRATION_CHECK_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
    static AFTER_FEATURE_INITIALIZATION_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
    static REGISTRATION_RETRY_HOOK: std::cell::RefCell<Option<InitializationRetryHook>> =
        std::cell::RefCell::new(None);
    static BEFORE_PROCESSOR_SNAPSHOT_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
    static BEFORE_BLOCKING_CACHE_INITIALIZATION_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
    static AFTER_PROCESSOR_SNAPSHOT_VALIDATED_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
    static AFTER_REGISTRATION_UPDATE_BEGAN_HOOK: std::cell::RefCell<Option<InitializationHook>> =
        std::cell::RefCell::new(None);
}

const REGISTRATION_UPDATE_BIT: u64 = 1;
const AUTOMATIC_POST_PROCESSOR_NAMES: &[&str] = &[
    "page-classification",
    "chunk-classification",
    "summarization",
    "translation",
    "captioning",
    "qr-codes",
    "ner",
    "redaction",
    "quality-processing",
    "keyword-extraction",
];

struct RegistrationUpdate;
struct ProcessorSnapshotLease;

pub(super) struct ProcessorSnapshot {
    pub(super) early: std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
    pub(super) middle: std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
    pub(super) late: std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
    _lease: ProcessorSnapshotLease,
}

impl RegistrationUpdate {
    fn begin() -> Self {
        BUILTIN_REGISTRATION_EPOCH.fetch_add(1, Ordering::SeqCst);
        Self
    }
}

impl Drop for RegistrationUpdate {
    fn drop(&mut self) {
        BUILTIN_REGISTRATION_EPOCH.fetch_add(1, Ordering::SeqCst);
    }
}

impl ProcessorSnapshotLease {
    fn acquire() -> Self {
        ACTIVE_PROCESSOR_SNAPSHOTS.fetch_add(1, Ordering::SeqCst);
        Self
    }
}

impl Drop for ProcessorSnapshotLease {
    fn drop(&mut self) {
        ACTIVE_PROCESSOR_SNAPSHOTS.fetch_sub(1, Ordering::SeqCst);
    }
}

fn registration_update_in_progress(epoch: u64) -> bool {
    epoch & REGISTRATION_UPDATE_BIT != 0
}

fn wait_for_registration_update() {
    let _registration_guard = BUILTIN_REGISTRATION_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
}

fn with_registration_update<T>(update: impl FnOnce() -> Result<T>) -> Result<T> {
    let _registration_guard = BUILTIN_REGISTRATION_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let _registration_update = RegistrationUpdate::begin();
    // Read the lease count BEFORE the hook, not after. The hook is how a test learns this
    // mutation has started, and its only caller then releases the parked pipeline that holds
    // the lease. Loading afterwards let that release win the race under CI contention: the
    // mutation observed a count of 0 and succeeded, failing an assertion that it be rejected.
    // Sampling first fixes the outcome while the lease is still provably held. Production is
    // unaffected -- the hook compiles away outside `cfg(test)`. ~keep
    let registry_in_use = ACTIVE_PROCESSOR_SNAPSHOTS.load(Ordering::SeqCst) != 0;
    #[cfg(test)]
    run_after_registration_update_began_hook();
    if registry_in_use {
        return Err(crate::XbergError::Other(
            "post-processor registry is in use by an active extraction; retry the lifecycle mutation after extraction completes"
                .to_string(),
        ));
    }
    update()
}

#[cfg(test)]
fn run_before_registration_check_hook() {
    let hook = BEFORE_REGISTRATION_CHECK_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

#[cfg(test)]
fn run_after_feature_initialization_hook() {
    let hook = AFTER_FEATURE_INITIALIZATION_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

#[cfg(test)]
fn run_registration_retry_hook() {
    REGISTRATION_RETRY_HOOK.with(|slot| {
        if let Some(hook) = slot.borrow_mut().as_mut() {
            hook();
        }
    });
}

#[cfg(test)]
fn run_before_processor_snapshot_hook() {
    let hook = BEFORE_PROCESSOR_SNAPSHOT_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

#[cfg(test)]
fn run_before_blocking_cache_initialization_hook() {
    let hook = BEFORE_BLOCKING_CACHE_INITIALIZATION_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

#[cfg(test)]
fn run_after_processor_snapshot_validated_hook() {
    let hook = AFTER_PROCESSOR_SNAPSHOT_VALIDATED_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

#[cfg(test)]
fn run_after_registration_update_began_hook() {
    let hook = AFTER_REGISTRATION_UPDATE_BEGAN_HOOK.with(|slot| slot.borrow_mut().take());
    if let Some(hook) = hook {
        hook();
    }
}

/// ~keep A public registry clear deliberately removes custom and built-in processors, while a
/// named unregister must remain effective. Clear and recovery share the registration mutex and
/// epoch so concurrent cache snapshots reject the intentionally empty intermediate registry.
pub(crate) fn with_builtin_registration_recovery<T>(clear: impl FnOnce() -> Result<T>) -> Result<T> {
    with_registration_update(|| {
        AUTOMATIC_REGISTRATION_SUPPRESSIONS
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clear();
        BUILTIN_REGISTRATION_REQUIRED.store(true, Ordering::Release);
        clear()
    })
}

pub(crate) fn with_post_processor_suppressed<T>(name: &str, remove: impl FnOnce() -> Result<T>) -> Result<T> {
    with_registration_update(|| {
        if AUTOMATIC_POST_PROCESSOR_NAMES.contains(&name) {
            AUTOMATIC_REGISTRATION_SUPPRESSIONS
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .insert(name.to_string());
        }
        remove()
    })
}

pub(crate) fn with_post_processor_enabled<T>(name: &str, register: impl FnOnce() -> Result<T>) -> Result<T> {
    with_registration_update(|| {
        let is_automatic = AUTOMATIC_POST_PROCESSOR_NAMES.contains(&name);
        let was_suppressed = AUTOMATIC_REGISTRATION_SUPPRESSIONS
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .remove(name);
        let result = register();
        if result.is_err() && is_automatic {
            restore_registration_state_after_failure(name, was_suppressed);
        }
        result
    })
}

fn restore_registration_state_after_failure(name: &str, was_suppressed: bool) {
    if was_suppressed {
        AUTOMATIC_REGISTRATION_SUPPRESSIONS
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(name.to_string());
    } else {
        BUILTIN_REGISTRATION_REQUIRED.store(true, Ordering::Release);
    }
}

#[cfg(any(
    feature = "classification",
    feature = "summarization",
    feature = "translation",
    feature = "captioning",
    feature = "qr-codes",
    feature = "ner",
    feature = "redaction",
    feature = "quality",
    feature = "keywords-yake",
    feature = "keywords-rake"
))]
pub(crate) fn automatic_registration_allowed(name: &str) -> bool {
    !AUTOMATIC_REGISTRATION_SUPPRESSIONS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .contains(name)
}

/// The error message from the built-in post-processor registration pass, if any
/// processor failed to register. `None` means either registration has not run
/// yet or every enabled processor registered successfully.
///
/// Called from `pipeline::mod::run_pipeline` (#271), which pushes a
/// `ProcessingWarning` naming this message whenever it is `Some(_)` — the
/// aggregate counterpart to the captioning-only "processor missing" warning at
/// the call site of `run_captioning_prepass`.
pub(crate) fn builtin_registration_error() -> Option<String> {
    #[cfg(test)]
    if let Some(error) = FORCED_BUILTIN_REGISTRATION_ERROR
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clone()
    {
        return Some(error);
    }

    BUILTIN_REGISTRATION_ERROR
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clone()
}

/// Test-only access to `BUILTIN_REGISTRATION_ERROR` for exercising the
/// `builtin_registration_error()` call site inside `pipeline::mod` without
/// racing the real registration pass.
#[cfg(test)]
pub(crate) mod test_support {
    use super::FORCED_BUILTIN_REGISTRATION_ERROR;
    #[cfg(feature = "tokio-runtime")]
    use super::{
        AFTER_PROCESSOR_SNAPSHOT_VALIDATED_HOOK, AFTER_REGISTRATION_UPDATE_BEGAN_HOOK, BEFORE_PROCESSOR_SNAPSHOT_HOOK,
        InitializationHook,
    };

    /// Set (or clear, with `None`) the recorded registration error. The static is
    /// process-global, so callers must restore it (typically to `None`) when done.
    pub(crate) fn set_registration_error(value: Option<String>) {
        let mut slot = FORCED_BUILTIN_REGISTRATION_ERROR
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        *slot = value;
    }

    #[cfg(feature = "tokio-runtime")]
    pub(crate) fn set_before_processor_snapshot_hook(hook: InitializationHook) {
        BEFORE_PROCESSOR_SNAPSHOT_HOOK.with(|slot| *slot.borrow_mut() = Some(hook));
    }

    #[cfg(all(feature = "tokio-runtime", feature = "quality", feature = "summarization"))]
    pub(crate) fn set_before_blocking_cache_initialization_hook(hook: Option<InitializationHook>) {
        super::BEFORE_BLOCKING_CACHE_INITIALIZATION_HOOK.with(|slot| *slot.borrow_mut() = hook);
    }

    #[cfg(feature = "tokio-runtime")]
    pub(crate) fn set_after_processor_snapshot_validated_hook(hook: InitializationHook) {
        AFTER_PROCESSOR_SNAPSHOT_VALIDATED_HOOK.with(|slot| *slot.borrow_mut() = Some(hook));
    }

    #[cfg(feature = "tokio-runtime")]
    pub(crate) fn set_after_registration_update_began_hook(hook: InitializationHook) {
        AFTER_REGISTRATION_UPDATE_BEGAN_HOOK.with(|slot| *slot.borrow_mut() = Some(hook));
    }
}

/// Type alias for processor stages tuple (Early, Middle, Late).
type ProcessorStages = (
    std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
    std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
    std::sync::Arc<Vec<std::sync::Arc<dyn crate::plugins::PostProcessor>>>,
);

/// Initialize feature-specific systems that may be needed during pipeline execution.
pub(super) fn initialize_features() {
    #[cfg(test)]
    run_before_registration_check_hook();

    if !BUILTIN_REGISTRATION_REQUIRED.load(Ordering::Acquire) {
        return;
    }

    let _registration_guard = BUILTIN_REGISTRATION_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    if !BUILTIN_REGISTRATION_REQUIRED.load(Ordering::Acquire) {
        return;
    }

    let _registration_update = RegistrationUpdate::begin();
    let mut failures = Vec::new();
    #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
    record_registration_result(
        &mut failures,
        "keyword-extraction",
        crate::keywords::ensure_initialized(),
    );
    record_registration_result(
        &mut failures,
        "quality-processing",
        register_quality_processor_if_missing(),
    );
    record_registration_result(
        &mut failures,
        "built-in post-processors",
        crate::plugins::processor::builtin::register_builtin(),
    );
    let registration_error = aggregate_registration_error(failures);
    let mut slot = BUILTIN_REGISTRATION_ERROR
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    *slot = registration_error;
    let registration_complete = slot.is_none();
    drop(slot);
    BUILTIN_REGISTRATION_REQUIRED.store(!registration_complete, Ordering::Release);
}

fn record_registration_result(failures: &mut Vec<String>, name: &str, result: Result<()>) {
    if let Err(error) = result {
        tracing::error!(processor = name, %error, "Automatic post-processor registration failed");
        failures.push(format!("{name}: {error}"));
    }
}

fn aggregate_registration_error(failures: Vec<String>) -> Option<String> {
    (!failures.is_empty()).then(|| format!("automatic post-processor registration failed: {}", failures.join("; ")))
}

#[cfg(feature = "quality")]
fn register_quality_processor_if_missing() -> Result<()> {
    crate::plugins::processor::register_post_processor_if_absent(std::sync::Arc::new(crate::text::QualityProcessor))
        .map(|_| ())
}

#[cfg(not(feature = "quality"))]
fn register_quality_processor_if_missing() -> Result<()> {
    Ok(())
}

/// Initialize the processor cache if not already initialized, or rebuild it if
/// the post-processor registry has changed since it was last built.
///
/// #215: the cache used to populate once on the first pipeline run and never
/// again, so a post-processor registered (or removed) after that first run was
/// silently invisible unless a caller happened to know about and call
/// `clear_processor_cache()`. Comparing the registry's live generation against
/// the generation recorded in the cached snapshot makes this self-correcting.
pub(super) fn initialize_processor_cache() -> Result<()> {
    loop {
        initialize_features();
        #[cfg(test)]
        run_after_feature_initialization_hook();
        let registration_epoch = BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst);
        if !registration_update_in_progress(registration_epoch) && try_initialize_processor_cache(registration_epoch)? {
            return Ok(());
        }
        #[cfg(test)]
        run_registration_retry_hook();
        wait_for_registration_update();
    }
}

#[cfg(feature = "tokio-runtime")]
pub(super) async fn initialize_processor_cache_for_async_pipeline() -> Result<ProcessorSnapshot> {
    if let Some(stages) = try_get_processor_snapshot(true) {
        return Ok(stages);
    }
    #[cfg(test)]
    run_before_blocking_cache_initialization_hook();
    let Ok(runtime) = tokio::runtime::Handle::try_current() else {
        return initialize_processor_stages();
    };
    runtime
        .spawn_blocking(initialize_processor_stages)
        .await
        .map_err(|error| crate::XbergError::Other(format!("processor cache task failed to join: {error}")))?
}

#[cfg(not(feature = "tokio-runtime"))]
pub(super) async fn initialize_processor_cache_for_async_pipeline() -> Result<ProcessorSnapshot> {
    if let Some(stages) = try_get_processor_snapshot(true) {
        return Ok(stages);
    }
    #[cfg(test)]
    run_before_blocking_cache_initialization_hook();
    initialize_processor_stages()
}

fn initialize_processor_stages() -> Result<ProcessorSnapshot> {
    loop {
        initialize_processor_cache()?;
        if let Some(stages) = try_get_processor_snapshot(false) {
            return Ok(stages);
        }
        wait_for_registration_update();
    }
}

fn try_initialize_processor_cache(registration_epoch: u64) -> Result<bool> {
    let current_generation = crate::plugins::registry::get_post_processor_registry()
        .read()
        .generation();
    if BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst) != registration_epoch {
        return Ok(false);
    }

    let mut cache_lock = PROCESSOR_CACHE.write();
    let is_stale = cache_lock
        .as_ref()
        .is_some_and(|cache| cache.generation != current_generation);

    if cache_lock.is_none() || is_stale {
        let candidate = ProcessorCache::new(registration_epoch)?;
        if BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst) != registration_epoch {
            return Ok(false);
        }
        *cache_lock = Some(candidate);
    } else if let Some(cache) = cache_lock.as_mut() {
        cache.registration_epoch = registration_epoch;
    }
    Ok(BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst) == registration_epoch)
}

fn try_get_processor_snapshot(require_complete_registration: bool) -> Option<ProcessorSnapshot> {
    if require_complete_registration && BUILTIN_REGISTRATION_REQUIRED.load(Ordering::Acquire) {
        return None;
    }
    let registration_epoch = BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst);
    if registration_update_in_progress(registration_epoch) {
        return None;
    }
    #[cfg(test)]
    run_before_processor_snapshot_hook();
    let lease = ProcessorSnapshotLease::acquire();
    let stages = PROCESSOR_CACHE
        .try_read()?
        .as_ref()
        .and_then(|cache| (cache.registration_epoch == registration_epoch).then(|| cached_processor_stages(cache)))?;
    if BUILTIN_REGISTRATION_EPOCH.load(Ordering::SeqCst) != registration_epoch {
        return None;
    }
    #[cfg(test)]
    run_after_processor_snapshot_validated_hook();
    Some(ProcessorSnapshot {
        early: stages.0,
        middle: stages.1,
        late: stages.2,
        _lease: lease,
    })
}

fn cached_processor_stages(cache: &ProcessorCache) -> ProcessorStages {
    (
        std::sync::Arc::clone(&cache.early),
        std::sync::Arc::clone(&cache.middle),
        std::sync::Arc::clone(&cache.late),
    )
}

/// Get processors from the cache, organized by stage.
#[cfg(test)]
pub(super) fn get_processors_from_cache() -> Result<ProcessorStages> {
    let cache_lock = PROCESSOR_CACHE.read();
    let cache = cache_lock
        .as_ref()
        .ok_or_else(|| crate::XbergError::Other("Processor cache not initialized".to_string()))?;
    Ok(cached_processor_stages(cache))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(all(feature = "quality", feature = "summarization"))]
    fn cached_processor_names() -> Vec<String> {
        let (early, middle, late) = get_processors_from_cache().unwrap();
        early
            .iter()
            .chain(middle.iter())
            .chain(late.iter())
            .map(|processor| processor.name().to_string())
            .collect()
    }

    #[test]
    #[serial_test::serial]
    #[cfg(all(feature = "quality", feature = "summarization"))]
    fn cache_snapshot_rejects_clear_started_after_feature_fast_path() {
        use crate::plugins::registry::test_support::PostProcessorRegistryGuard;
        use std::sync::mpsc;

        let _guard = PostProcessorRegistryGuard::acquire();
        initialize_features();
        initialize_processor_cache().unwrap();
        assert!(!BUILTIN_REGISTRATION_REQUIRED.load(Ordering::Acquire));
        let registration_epoch = BUILTIN_REGISTRATION_EPOCH.load(Ordering::Acquire);
        assert!(!registration_update_in_progress(registration_epoch));

        let (cleared_sender, cleared_receiver) = mpsc::channel();
        let (release_sender, release_receiver) = mpsc::channel();
        let clear_thread = std::thread::spawn(move || {
            with_builtin_registration_recovery(|| {
                let result = crate::plugins::registry::get_post_processor_registry()
                    .write()
                    .shutdown_all();
                cleared_sender.send(()).unwrap();
                release_receiver.recv().unwrap();
                result
            })
        });
        cleared_receiver.recv().unwrap();

        let accepted_stale_epoch = try_initialize_processor_cache(registration_epoch).unwrap();
        let cached_during_clear = cached_processor_names();
        release_sender.send(()).unwrap();
        clear_thread.join().unwrap().unwrap();

        assert!(!accepted_stale_epoch);
        assert!(cached_during_clear.iter().any(|name| name == "quality-processing"));

        initialize_processor_cache().unwrap();
        let names = cached_processor_names();
        assert!(names.iter().any(|name| name == "quality-processing"));
        assert!(names.iter().any(|name| name == "summarization"));
    }

    #[test]
    #[serial_test::serial]
    #[cfg(all(feature = "quality", feature = "summarization"))]
    fn cache_initialization_waits_without_polling_during_registry_update() {
        use crate::plugins::registry::test_support::PostProcessorRegistryGuard;
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::{Arc, mpsc};
        use std::time::Duration;

        let _guard = PostProcessorRegistryGuard::acquire();
        initialize_processor_cache().unwrap();
        let (fast_path_sender, fast_path_receiver) = mpsc::channel();
        let (resume_sender, resume_receiver) = mpsc::channel();
        let retry_count = Arc::new(AtomicUsize::new(0));
        let thread_retry_count = Arc::clone(&retry_count);
        let initialize_thread = std::thread::spawn(move || {
            AFTER_FEATURE_INITIALIZATION_HOOK.with(|slot| {
                *slot.borrow_mut() = Some(Box::new(move || {
                    fast_path_sender.send(()).unwrap();
                    resume_receiver.recv().unwrap();
                }));
            });
            REGISTRATION_RETRY_HOOK.with(|slot| {
                *slot.borrow_mut() = Some(Box::new(move || {
                    thread_retry_count.fetch_add(1, Ordering::Relaxed);
                }));
            });
            initialize_processor_cache()
        });
        fast_path_receiver.recv().unwrap();

        let (update_started_sender, update_started_receiver) = mpsc::channel();
        let (update_release_sender, update_release_receiver) = mpsc::channel();
        let update_thread = std::thread::spawn(move || {
            with_post_processor_suppressed("unregistered-test-processor", || {
                update_started_sender.send(()).unwrap();
                update_release_receiver.recv().unwrap();
                Ok::<(), crate::XbergError>(())
            })
        });
        update_started_receiver.recv().unwrap();
        resume_sender.send(()).unwrap();

        std::thread::sleep(Duration::from_millis(25));
        assert_eq!(retry_count.load(Ordering::Relaxed), 1);

        update_release_sender.send(()).unwrap();
        update_thread.join().unwrap().unwrap();
        initialize_thread.join().unwrap().unwrap();
    }

    #[tokio::test(flavor = "current_thread")]
    #[serial_test::serial]
    #[cfg(all(feature = "tokio-runtime", feature = "quality", feature = "summarization"))]
    async fn stable_cache_handoff_skips_blocking_initialization() {
        use crate::plugins::registry::test_support::PostProcessorRegistryGuard;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let _guard = PostProcessorRegistryGuard::acquire();
        initialize_processor_cache().unwrap();
        let blocking_initializations = Arc::new(AtomicUsize::new(0));
        let hook_count = Arc::clone(&blocking_initializations);
        test_support::set_before_blocking_cache_initialization_hook(Some(Box::new(move || {
            hook_count.fetch_add(1, Ordering::SeqCst);
        })));

        // `try_get_processor_snapshot` probes `PROCESSOR_CACHE` with a non-blocking `try_read`,
        // which yields `None` while any other thread holds the write side -- and dozens of
        // non-`#[serial]` tests in this binary run real extractions that take it. A single
        // contended probe therefore diverts to the blocking path for reasons unrelated to the
        // behaviour under test. Retrying keeps the assertion meaningful: a genuinely broken fast
        // path fails every attempt, so this still fails outright rather than being weakened into
        // a check no defect could trip. ~keep
        const HANDOFF_ATTEMPTS: usize = 50;
        const HANDOFF_RETRY_DELAY: std::time::Duration = std::time::Duration::from_millis(20);
        let mut warm_snapshot = None;
        for _ in 0..HANDOFF_ATTEMPTS {
            blocking_initializations.store(0, Ordering::SeqCst);
            let candidate = initialize_processor_cache_for_async_pipeline().await.unwrap();
            if blocking_initializations.load(Ordering::SeqCst) == 0 {
                warm_snapshot = Some(candidate);
                break;
            }
            tokio::time::sleep(HANDOFF_RETRY_DELAY).await;
        }
        let snapshot =
            warm_snapshot.expect("the warm-cache fast path never won an uncontended probe across HANDOFF_ATTEMPTS");
        test_support::set_before_blocking_cache_initialization_hook(None);
        let names = snapshot
            .early
            .iter()
            .chain(snapshot.middle.iter())
            .chain(snapshot.late.iter())
            .map(|processor| processor.name())
            .collect::<Vec<_>>();

        assert!(names.contains(&"quality-processing"));
        assert!(names.contains(&"summarization"));
    }

    #[test]
    #[serial_test::serial]
    #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
    fn keyword_recovery_runs_after_a_concurrent_clear() {
        use crate::plugins::registry::test_support::PostProcessorRegistryGuard;
        use std::sync::mpsc;

        let _guard = PostProcessorRegistryGuard::acquire();
        initialize_processor_cache().unwrap();
        let (reached_sender, reached_receiver) = mpsc::channel();
        let (release_sender, release_receiver) = mpsc::channel();
        let initialize_thread = std::thread::spawn(move || {
            BEFORE_REGISTRATION_CHECK_HOOK.with(|slot| {
                *slot.borrow_mut() = Some(Box::new(move || {
                    reached_sender.send(()).unwrap();
                    release_receiver.recv().unwrap();
                }));
            });
            initialize_processor_cache()
        });
        reached_receiver.recv().unwrap();
        let clear_result = crate::plugins::clear_post_processors();
        release_sender.send(()).unwrap();
        initialize_thread.join().unwrap().unwrap();
        clear_result.unwrap();

        let names = crate::plugins::registry::get_post_processor_registry().read().list();
        assert!(
            names
                .iter()
                .any(|name| name == crate::keywords::processor::KEYWORD_PROCESSOR_NAME)
        );
    }

    #[test]
    fn registration_error_includes_quality_failure() {
        let mut failures = Vec::new();
        record_registration_result(
            &mut failures,
            "quality-processing",
            Err(crate::XbergError::Other("quality init failed".to_string())),
        );

        assert_eq!(
            aggregate_registration_error(failures).as_deref(),
            Some("automatic post-processor registration failed: quality-processing: quality init failed")
        );
    }

    /// #271: `builtin_registration_error` must round-trip whatever the
    /// registration pass recorded, so pipeline code has somewhere to look
    /// instead of the failure being dropped by `let _ = ...`.
    #[test]
    fn builtin_registration_error_reports_a_recorded_failure() {
        {
            let mut slot = BUILTIN_REGISTRATION_ERROR
                .write()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            *slot = Some("1 of 3 built-in post-processor(s) failed to register: ner: boom".to_string());
        }
        assert_eq!(
            builtin_registration_error(),
            Some("1 of 3 built-in post-processor(s) failed to register: ner: boom".to_string())
        );

        // Reset: this static is process-global and shared with other tests in this binary.
        let mut slot = BUILTIN_REGISTRATION_ERROR
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        *slot = None;
    }

    /// #215: a post-processor registered after the cache was already populated
    /// must become visible on the *next* extraction, not stay invisible until
    /// something remembers to call `clear_processor_cache()`.
    ///
    /// `#[serial]`: this test clears the global post-processor registry (via
    /// `PostProcessorRegistryGuard`) and mutates the process-global
    /// `PROCESSOR_CACHE` directly — there is no local/injectable variant of either,
    /// so any other test that runs the real pipeline concurrently and expects the
    /// built-in post-processors (or a previously cached, non-empty processor set)
    /// to be present would race this one. Serializing against the crate's other
    /// `#[serial]` tests (see `core::extractor::mod::test_concurrent_extractions_different_mimes`)
    /// is the same pattern used there for the same reason.
    ///
    /// The assertions are deliberately about **containment of this test's own
    /// processor**, never about the registry being empty or about an exact count.
    /// `#[serial]` only excludes the crate's other `#[serial]` tests, while *any*
    /// non-serial test that runs a real extraction calls `initialize_features()` and
    /// registers the built-in post-processors into the same global registry. An
    /// "assert the cache starts empty" check therefore failed roughly one run in four
    /// — a property of the test harness, not of the code under test. Emptiness was
    /// only ever scaffolding; the #215 invariant is that a *late* registration becomes
    /// visible, which containment states exactly and races nothing.
    #[serial_test::serial]
    #[test]
    fn processor_cache_rebuilds_when_registry_changes_after_first_use() {
        use crate::plugins::registry::test_support::PostProcessorRegistryGuard;
        use crate::plugins::{Plugin, PostProcessor, ProcessingStage};
        use crate::types::ExtractedDocument;
        use async_trait::async_trait;
        use std::sync::Arc;

        let _guard = PostProcessorRegistryGuard::acquire();

        #[derive(Debug)]
        struct LateAddedProcessor;

        impl Plugin for LateAddedProcessor {
            fn name(&self) -> &str {
                "late-added-215"
            }
            fn version(&self) -> String {
                "1.0.0".to_string()
            }
            fn initialize(&self) -> Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> Result<()> {
                Ok(())
            }
        }

        #[async_trait]
        impl PostProcessor for LateAddedProcessor {
            async fn process(
                &self,
                _result: &mut ExtractedDocument,
                _config: &crate::core::config::ExtractionConfig,
            ) -> Result<()> {
                Ok(())
            }
            fn processing_stage(&self) -> ProcessingStage {
                ProcessingStage::Middle
            }
        }

        *PROCESSOR_CACHE.write() = None;

        let has_late_added =
            |processors: &[std::sync::Arc<dyn PostProcessor>]| processors.iter().any(|p| p.name() == "late-added-215");

        // Populate the cache, exactly as the first pipeline run of a process would.
        initialize_processor_cache().unwrap();
        let (_, middle, _) = get_processors_from_cache().unwrap();
        assert!(
            !has_late_added(&middle),
            "the processor under test must not be in the cache before it is registered"
        );

        // Register a processor *after* the cache already holds a snapshot.
        crate::plugins::register_post_processor(Arc::new(LateAddedProcessor)).unwrap();

        // Without the #215 fix, `initialize_processor_cache` is a no-op once the
        // cache is `Some(_)`, so the newly registered processor would never appear.
        initialize_processor_cache().unwrap();
        let (_, middle, _) = get_processors_from_cache().unwrap();
        assert!(
            has_late_added(&middle),
            "the cache must pick up the post-registration processor"
        );

        *PROCESSOR_CACHE.write() = None;
    }
}