whittle 1.0.0

Coordinate-consistent trimming of long-read uBAM and tagged FASTQ
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! Workflow drivers shared by the FASTQ and BAM paths: batching, the parallel driver, run counters and per-read segment filtering.

pub(crate) mod bam;
mod fastq;
#[cfg(feature = "paraseq")]
mod paraseq;
pub(crate) mod reject;

use std::collections::BTreeMap;
use std::io::Write;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex, OnceLock};

use rayon::prelude::*;

pub(crate) use bam::run_bam_to_fastq;
pub use bam::run_raw_bam;

/// Wraps a record stream so that only reads `keep` accepts pass on. A rejected
/// read is counted here as input and as tag-filtered, since no workflow sees
/// it, and handed to `reject`, which writes it to the rejected output when one
/// is open; an evaluation error names the read.
pub(crate) fn filter_by_tags<R, I, K, W, N, J>(
    records: I,
    counters: Arc<Counters>,
    keep: K,
    weight: W,
    name: N,
    reject: J,
) -> Box<dyn Iterator<Item = anyhow::Result<R>> + Send>
where
    R: Send + 'static,
    I: Iterator<Item = anyhow::Result<R>> + Send + 'static,
    K: Fn(&R) -> anyhow::Result<bool> + Send + 'static,
    W: Fn(&R) -> usize + Send + 'static,
    N: Fn(&R) -> String + Send + 'static,
    J: Fn(&R) -> anyhow::Result<()> + Send + 'static,
{
    Box::new(records.filter_map(move |item| {
        let rec = match item {
            Ok(rec) => rec,
            Err(e) => return Some(Err(e)),
        };
        match keep(&rec) {
            Ok(true) => Some(Ok(rec)),
            Ok(false) => {
                counters.input_reads.fetch_add(1, Ordering::Relaxed);
                counters
                    .input_bases
                    .fetch_add(weight(&rec) as u64, Ordering::Relaxed);
                counters.tag_filtered_reads.fetch_add(1, Ordering::Relaxed);
                match reject(&rec) {
                    Ok(()) => None,
                    Err(e) => Some(Err(e.context(format!("read {}", name(&rec))))),
                }
            },
            Err(e) => Some(Err(e.context(format!("read {}", name(&rec))))),
        }
    }))
}
pub(crate) use fastq::{run_fastq, tag_filtered_fastq};
#[cfg(feature = "paraseq")]
pub(crate) use paraseq::{run_fastq_paraseq, selected as paraseq_selected};

use crate::config::Config;
use crate::filter::{DropReason, FilterConfig};

/// Batch sizing for one parallel workflow. `target_weight` bounds the summed
/// record weight (bases) per batch and `max_items` bounds the record count, so a
/// batch is large enough to amortize scheduling and channel costs and small
/// enough to balance unusually long reads across workers. `queue_per_worker`
/// sizes the bounded channel to the writer in batches per render worker.
#[derive(Clone, Copy)]
pub(crate) struct BatchPolicy {
    target_weight: usize,
    max_items: usize,
    queue_per_worker: usize,
}

/// FASTQ batches: owned records rendered into a shared batch buffer.
pub(crate) const FASTQ_BATCH: BatchPolicy = BatchPolicy {
    target_weight: 2 * 1024 * 1024,
    max_items: 128,
    queue_per_worker: 2,
};

/// BAM batches: records that decode to large owned buffers.
pub(crate) const BAM_BATCH: BatchPolicy = BatchPolicy {
    target_weight: 1024 * 1024,
    max_items: 16,
    queue_per_worker: 1,
};

/// Groups an iterator's items into batches bounded by a weight sum and an item
/// count.
pub(crate) struct Batches<I, F> {
    records: I,
    weight: F,
    policy: BatchPolicy,
}

impl<I, F> Batches<I, F> {
    /// Wraps `records`, weighing each item with `weight`, under `policy`.
    pub(crate) fn new(records: I, weight: F, policy: BatchPolicy) -> Self {
        Self {
            records,
            weight,
            policy,
        }
    }
}

impl<I, F, T> Iterator for Batches<I, F>
where
    I: Iterator<Item = T>,
    F: Fn(&T) -> usize,
{
    type Item = Vec<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let mut batch = Vec::with_capacity(self.policy.max_items);
        let mut bases = 0usize;
        while batch.len() < self.policy.max_items && bases < self.policy.target_weight {
            let Some(record) = self.records.next() else {
                break;
            };
            bases = bases.saturating_add((self.weight)(&record));
            batch.push(record);
        }
        (!batch.is_empty()).then_some(batch)
    }
}

/// A sink of rendered text batches: plain bytes, or BGZF blocks the render
/// workers compressed.
pub(crate) trait BatchSink: Write + Send {
    /// The BGZF level when the sink takes blocks compressed on the render
    /// pool; `None` for a sink that takes the rendered bytes as they are.
    fn block_level(&self) -> Option<u8> {
        None
    }
}

impl BatchSink for Vec<u8> {}

impl BatchSink for crate::io::fastq::FastqOut {
    fn block_level(&self) -> Option<u8> {
        crate::io::fastq::FastqOut::block_level(self)
    }
}

/// Renders directly into a batch buffer and compresses block output on the pool.
pub(crate) fn run_bytes_parallel<R, W, Weight, Render>(
    records: impl Iterator<Item = anyhow::Result<R>> + Send,
    policy: BatchPolicy,
    weight: Weight,
    cfg: &Config,
    writer: &mut W,
    render: Render,
    counters: &Counters,
) -> anyhow::Result<Stats>
where
    R: Send,
    W: BatchSink,
    Weight: Fn(&R) -> usize + Sync,
    Render: Fn(R, &Config, &mut Vec<u8>) -> anyhow::Result<()> + Sync,
{
    let level = writer.block_level();
    run_parallel(
        records,
        policy,
        weight,
        cfg,
        writer,
        render,
        |bytes: Vec<u8>| match level {
            Some(level) => crate::io::fastq::encode_blocks(level, &bytes),
            None => Ok(bytes),
        },
        |writer, bytes: &Vec<u8>| writer.write_all(bytes),
        counters,
    )
}

/// Returns the render-pool size for a run: the settled budget, or the thread
/// count when no budget was settled.
pub(crate) fn render_pool_size(cfg: &Config) -> usize {
    if cfg.render_workers >= 1 {
        cfg.render_workers
    } else {
        cfg.threads.max(1)
    }
}

/// Keeps the first error of a parallel run and raises the shared abort flag.
struct FirstError<E> {
    slot: Mutex<Option<E>>,
}

impl<E> FirstError<E> {
    fn new() -> Self {
        Self {
            slot: Mutex::new(None),
        }
    }

    fn record(&self, error: E, aborted: &AtomicBool) {
        aborted.store(true, Ordering::Relaxed);
        let mut slot = self.slot.lock().unwrap();
        if slot.is_none() {
            *slot = Some(error);
        }
    }

    fn take(&self) -> Option<E> {
        self.slot.lock().unwrap().take()
    }
}

/// Bounds how far batch hand-out runs ahead of the ordered writer: batch `idx`
/// is handed out only once fewer than `size` batches before it are unwritten,
/// so a slow batch holds back at most `size` batches behind it.
struct ReorderWindow {
    /// Batches written so far.
    written: Mutex<usize>,
    /// Signaled when `written` advances or the run aborts.
    advanced: Condvar,
    /// Batches allowed in flight.
    size: usize,
}

impl ReorderWindow {
    fn new(size: usize) -> Self {
        Self {
            written: Mutex::new(0),
            advanced: Condvar::new(),
            size,
        }
    }

    /// Blocks until batch `idx` fits in the window or the run has aborted.
    fn wait_for(&self, idx: usize, aborted: &AtomicBool) {
        let mut written = self.written.lock().unwrap();
        while idx >= *written + self.size && !aborted.load(Ordering::Relaxed) {
            written = self.advanced.wait(written).unwrap();
        }
    }

    /// Records that `written` batches have been written.
    fn advance(&self, written: usize) {
        *self.written.lock().unwrap() = written;
        self.advanced.notify_all();
    }

    /// Wakes every waiter after the abort flag is raised. The lock orders the
    /// flag before a waiter's next check.
    fn release(&self) {
        let _written = self.written.lock().unwrap();
        self.advanced.notify_all();
    }
}

/// A record stream that ends after its first `Err` (which is still delivered)
/// or once the run's abort flag is raised. The source is checked before every
/// poll, so it is never polled again after either event.
struct FuseOnError<'a, I> {
    inner: I,
    done: bool,
    aborted: &'a AtomicBool,
}

impl<I, R> Iterator for FuseOnError<'_, I>
where
    I: Iterator<Item = anyhow::Result<R>>,
{
    type Item = anyhow::Result<R>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done || self.aborted.load(Ordering::Relaxed) {
            return None;
        }
        let item = self.inner.next()?;
        self.done = item.is_err();
        Some(item)
    }
}

/// Runs the parallel driver shared by every multithreaded workflow. Records are
/// batched under `policy`, rendered on a rayon pool of `render_pool_size(cfg)`
/// threads, and written by one thread. The record stream is fused on its first
/// `Err` and stops being read once any render or write error is recorded, so a
/// failing run neither re-polls a reader after an I/O error nor processes the
/// rest of the input. With `cfg.ordered` the writer emits batches in input
/// order, and a batch is handed out only while a bounded window of batches
/// separates it from the next one to write; otherwise in completion order.
///
/// `render` appends each record's output to the batch accumulator; `pack`
/// turns the accumulator into the unit the writer takes, on
/// the pool, so a compressing sink has its blocks compressed by the render
/// workers. The read-level counters are updated inside `render` by
/// `process_read_segments`; this driver counts input reads and bases only.
#[allow(clippy::too_many_arguments)]
pub(crate) fn run_parallel<R, T, P, S, Weight, Render, Pack, WriteOne>(
    records: impl Iterator<Item = anyhow::Result<R>> + Send,
    policy: BatchPolicy,
    weight: Weight,
    cfg: &Config,
    sink: &mut S,
    render: Render,
    pack: Pack,
    write_one: WriteOne,
    counters: &Counters,
) -> anyhow::Result<Stats>
where
    R: Send,
    T: Default + Send,
    P: Send,
    S: Send,
    Weight: Fn(&R) -> usize + Sync,
    Render: Fn(R, &Config, &mut T) -> anyhow::Result<()> + Sync,
    Pack: Fn(T) -> std::io::Result<P> + Sync,
    WriteOne: Fn(&mut S, &P) -> std::io::Result<()> + Send,
{
    let render_workers = render_pool_size(cfg);
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(render_workers)
        .build()?;
    let queue = (render_workers * policy.queue_per_worker).max(2);
    let (tx, rx) = std::sync::mpsc::sync_channel::<(usize, P)>(queue);
    let ordered = cfg.ordered;
    let aborted = AtomicBool::new(false);
    let render_err: FirstError<anyhow::Error> = FirstError::new();
    let write_err: FirstError<std::io::Error> = FirstError::new();
    let window = ReorderWindow::new(queue + render_workers);

    let aborted_ref = &aborted;
    let window_ref = &window;
    let records = FuseOnError {
        inner: records,
        done: false,
        aborted: aborted_ref,
    };

    // The writer is a scoped OS thread; rendering runs on the local pool via
    // `pool.install`, so the nested `par_bridge` is bounded by `-t`. The writer
    // keeps draining after an error so a producer blocked on `tx.send` cannot
    // deadlock.
    std::thread::scope(|s| {
        let write_err = &write_err;
        s.spawn(move || {
            let mut next = 0usize;
            let mut pending: BTreeMap<usize, P> = BTreeMap::new();
            let mut errored = false;
            let mut write_batch = |batch: &P| -> bool {
                if let Err(e) = write_one(sink, batch) {
                    write_err.record(e, aborted_ref);
                    window_ref.release();
                    return false;
                }
                true
            };
            for (idx, batch) in rx.iter() {
                if errored {
                    continue;
                }
                if ordered {
                    pending.insert(idx, batch);
                    let written = next;
                    while let Some(batch) = pending.remove(&next) {
                        if !write_batch(&batch) {
                            errored = true;
                            break;
                        }
                        next += 1;
                    }
                    if next > written {
                        window_ref.advance(next);
                    }
                } else if !write_batch(&batch) {
                    errored = true;
                }
            }
        });

        pool.install(|| {
            let weight_of = |rec: &anyhow::Result<R>| rec.as_ref().map_or(0, &weight);
            let mut batches = Batches::new(records, weight_of, policy);
            let mut handed_out = 0usize;
            // Under `ordered`, the next batch is read only once it fits in the
            // reorder window, which bounds the reordered output in memory.
            let batches = std::iter::from_fn(move || {
                if ordered {
                    window_ref.wait_for(handed_out, aborted_ref);
                }
                let batch = batches.next()?;
                handed_out += 1;
                Some((handed_out - 1, batch))
            });
            let render_batch = |(idx, batch): (usize, Vec<anyhow::Result<R>>)| {
                let mut out = T::default();
                let mut input_reads = 0u64;
                let mut input_bases = 0u64;
                for rec in batch {
                    if aborted.load(Ordering::Relaxed) {
                        break;
                    }
                    let rec = match rec {
                        Ok(r) => r,
                        Err(e) => {
                            render_err.record(e, &aborted);
                            break;
                        },
                    };
                    input_reads += 1;
                    input_bases += weight(&rec) as u64;
                    if let Err(e) = render(rec, cfg, &mut out) {
                        render_err.record(e, &aborted);
                        break;
                    }
                }
                if aborted.load(Ordering::Relaxed) {
                    window.release();
                }
                counters
                    .input_reads
                    .fetch_add(input_reads, Ordering::Relaxed);
                counters
                    .input_bases
                    .fetch_add(input_bases, Ordering::Relaxed);
                let packed = match pack(out) {
                    Ok(packed) => packed,
                    Err(e) => {
                        render_err.record(e.into(), &aborted);
                        window.release();
                        return;
                    },
                };
                // Every batch is sent, empty ones included, so the ordered
                // writer can advance past it. A closed channel means the
                // writer is gone; nothing more can be written.
                if tx.send((idx, packed)).is_err() {
                    aborted.store(true, Ordering::Relaxed);
                    window.release();
                }
            };
            batches.par_bridge().for_each(render_batch);
        });
        drop(tx);
    });

    if let Some(e) = render_err.take() {
        return Err(e);
    }
    if let Some(e) = write_err.take() {
        return Err(e.into());
    }
    Ok(counters.snapshot())
}

/// Live, thread-shared counters read by the progress ticker and finalized into `Stats`.
#[derive(Default)]
pub struct Counters {
    /// Whether any FASTQ header carries SAM auxiliary fields.
    pub(crate) tagged_fastq: AtomicBool,
    /// Input reads consumed.
    pub input_reads: AtomicU64,
    /// Output segments written (one per surviving segment).
    pub output_reads: AtomicU64,
    /// Compressed bytes pulled from the input source.
    pub bytes_read: AtomicU64,
    /// Sum of SEQ lengths (bases) across every input read, regardless of
    /// whether it survives filtering/trimming.
    pub input_bases: AtomicU64,
    /// Sum of surviving segment lengths (bases) written to output.
    pub output_bases: AtomicU64,
    /// Input reads carrying a known per-base tag (`ip`, `pw`, ...) whose array
    /// length disagrees with the sequence length, or an `sa` coverage array
    /// whose runs do not sum to it. The tag is left untouched and the count is
    /// surfaced as a run-level advisory. Tracked by the BAM paths only.
    pub malformed_tag_reads: AtomicU64,
    /// Input reads whose `MM`/`ML`/`MN` block was malformed (an `MN` that
    /// disagrees with the sequence length, an `ML` whose length disagrees with
    /// `MM`, an `MM` that does not parse to its end or exceeds the available
    /// counting-base occurrences, or a non-`B:C` `ML`) and was
    /// therefore removed from the output record.
    pub malformed_mod_reads: AtomicU64,
    /// Trimmed input reads whose PacBio undo blobs (`ds`, `ls`) were removed,
    /// since they describe the untrimmed read.
    pub undo_tags_dropped_reads: AtomicU64,
    /// Trimmed or split input reads whose fibertools footprint arrays (`ns`,
    /// `nl`, `as`, `al`) were removed, since their positions index the whole
    /// read.
    pub footprint_tags_dropped_reads: AtomicU64,
    /// Input reads whose `bi` barcode tag could not be read as a barcode window
    /// (not a seven-element float array, or an empty, inverted or out-of-range
    /// window) and were left untrimmed by that stage.
    pub barcode_tag_malformed_reads: AtomicU64,
    /// Input reads with a recorded barcode span at which no configured or
    /// named catalog barcode was found; that span was left untrimmed.
    pub barcode_tag_unverified_reads: AtomicU64,
    /// Input reads rejected by `--tag-filter` before any workflow saw them.
    /// Read-level, part of the invariant below.
    pub tag_filtered_reads: AtomicU64,
    /// The `--rejected-output` channel, set before the first record is read when
    /// the flag is given; unset otherwise.
    pub(crate) rejects: OnceLock<reject::Rejects>,
    /// Input reads that produced at least one surviving output segment,
    /// bumped once per input read (not once per segment, unlike
    /// `output_reads`, which a quality-split read can bump several times).
    /// Exists so `snapshot` can check that every input read is accounted for
    /// by exactly one of the three read-level outcomes.
    pub reads_with_output: AtomicU64,
    /// Input reads that produced zero segments: `trim::apply` returned no
    /// intervals for the per-segment filter (an empty read, a read fully
    /// consumed by adapter trimming, or an over-crop). Read-level, paired with
    /// `reads_with_output` and `reads_all_filtered` in the invariant below.
    pub reads_trimmed_to_nothing: AtomicU64,
    /// Input reads that produced at least one segment, but every one was
    /// rejected by post-trim `filter::check`. Read-level, paired with
    /// `reads_with_output` and `reads_trimmed_to_nothing` in the invariant
    /// below.
    pub reads_all_filtered: AtomicU64,
    /// Segments dropped as `TooShort`. This and the five counters below are
    /// segment-level: one bump per segment (not read) that `filter::check`
    /// rejects, by reason, post-trim. A single input read can contribute to
    /// more than one of these (e.g. a quality-split read whose several pieces
    /// are each judged independently). They are not part of the read-level
    /// invariant.
    pub segments_dropped_short: AtomicU64,
    /// Segments dropped as `TooLong`.
    pub segments_dropped_long: AtomicU64,
    /// Segments dropped as `LowQuality`.
    pub segments_dropped_low_qual: AtomicU64,
    /// Segments dropped as `HighQuality`.
    pub segments_dropped_high_qual: AtomicU64,
    /// Segments dropped as `ExpectedErrors`.
    pub segments_dropped_expected_errors: AtomicU64,
    /// Segments dropped as `Gc`.
    pub segments_dropped_gc: AtomicU64,
}

impl Counters {
    /// Whether rejected records are written, so producers can skip rendering them.
    pub(crate) fn wants_rejects(&self) -> bool {
        self.rejects.get().is_some()
    }

    /// Queues a rejected record when `--rejected-output` is set.
    pub(crate) fn reject(&self, item: reject::RejectItem) -> anyhow::Result<()> {
        match self.rejects.get() {
            Some(rejects) => rejects.send(item),
            None => Ok(()),
        }
    }

    /// Bumps the segment-level counter matching a `filter::check` failure
    /// reason. Called once per rejected segment (post-trim), not per read.
    pub fn record_segment_drop(&self, reason: DropReason) {
        let counter = match reason {
            DropReason::TooShort => &self.segments_dropped_short,
            DropReason::TooLong => &self.segments_dropped_long,
            DropReason::LowQuality => &self.segments_dropped_low_qual,
            DropReason::HighQuality => &self.segments_dropped_high_qual,
            DropReason::ExpectedErrors => &self.segments_dropped_expected_errors,
            DropReason::Gc => &self.segments_dropped_gc,
        };
        counter.fetch_add(1, Ordering::Relaxed);
    }

    /// Snapshots every counter into a `Stats` for end-of-run reporting.
    pub fn snapshot(&self) -> Stats {
        let input_reads = self.input_reads.load(Ordering::Relaxed);
        let reads_with_output = self.reads_with_output.load(Ordering::Relaxed);
        let reads_trimmed_to_nothing = self.reads_trimmed_to_nothing.load(Ordering::Relaxed);
        let reads_all_filtered = self.reads_all_filtered.load(Ordering::Relaxed);
        let reads_tag_filtered = self.tag_filtered_reads.load(Ordering::Relaxed);
        let segments_dropped_short = self.segments_dropped_short.load(Ordering::Relaxed);
        let segments_dropped_long = self.segments_dropped_long.load(Ordering::Relaxed);
        let segments_dropped_low_qual = self.segments_dropped_low_qual.load(Ordering::Relaxed);
        let segments_dropped_high_qual = self.segments_dropped_high_qual.load(Ordering::Relaxed);
        let segments_dropped_expected_errors = self
            .segments_dropped_expected_errors
            .load(Ordering::Relaxed);
        let segments_dropped_gc = self.segments_dropped_gc.load(Ordering::Relaxed);

        // Every input read lands in exactly one of the four read-level buckets: it
        // was rejected by the tag filter, produced surviving segments, produced
        // none at all, or produced some and lost them all to `filter::check`.
        // Segment-level drops are excluded, since a read can shed segments and
        // still survive. The assertion catches an early return that skips one.
        debug_assert_eq!(
            reads_with_output + reads_trimmed_to_nothing + reads_all_filtered + reads_tag_filtered,
            input_reads,
            "Every input read must be exactly one of: tag filtered, produced output, \
             trimmed to nothing, or had every segment filtered"
        );

        Stats {
            input_reads,
            output_reads: self.output_reads.load(Ordering::Relaxed),
            malformed_mod_reads: self.malformed_mod_reads.load(Ordering::Relaxed),
            undo_tags_dropped_reads: self.undo_tags_dropped_reads.load(Ordering::Relaxed),
            footprint_tags_dropped_reads: self.footprint_tags_dropped_reads.load(Ordering::Relaxed),
            barcode_tag_malformed_reads: self.barcode_tag_malformed_reads.load(Ordering::Relaxed),
            barcode_tag_unverified_reads: self.barcode_tag_unverified_reads.load(Ordering::Relaxed),
            input_bases: self.input_bases.load(Ordering::Relaxed),
            output_bases: self.output_bases.load(Ordering::Relaxed),
            malformed_tag_reads: self.malformed_tag_reads.load(Ordering::Relaxed),
            reads_with_output,
            reads_trimmed_to_nothing,
            reads_all_filtered,
            reads_tag_filtered,
            segments_dropped_short,
            segments_dropped_long,
            segments_dropped_low_qual,
            segments_dropped_high_qual,
            segments_dropped_expected_errors,
            segments_dropped_gc,
        }
    }
}

/// Returns a trace-level span naming the read, so the segment decisions logged
/// while processing it are attributable without repeating the name on every
/// event.
///
/// Returns a disabled span when trace is off, which costs a level check rather
/// than any formatting, keeping the hot path unaffected at the default level.
pub(crate) fn read_span(name: &[u8]) -> tracing::Span {
    if tracing::enabled!(tracing::Level::TRACE) {
        tracing::trace_span!("read", name = %String::from_utf8_lossy(name))
    } else {
        tracing::Span::none()
    }
}

/// Filters produced segments and updates segment- and read-level counters for
/// all workflows. `seq` and `qual` contain the complete input read and
/// `produced` contains the final ranges from adapter and quality processing
/// in original-coordinate order. Segment numbers index this flattened list
/// before filtering; they do not restart at adapter boundaries. For each survivor,
/// `render` receives `(idx, total, start, end)`; for each rejected segment and
/// for a read that produced none, `reject` receives the `Rejection`. A render
/// error stops processing before the read-level outcome counter is updated.
pub(crate) fn process_read_segments<Rn, Rj>(
    produced: &[(usize, usize)],
    seq: &[u8],
    qual: &[u8],
    filter_cfg: &FilterConfig,
    counters: &Counters,
    mut render: Rn,
    mut reject: Rj,
) -> anyhow::Result<()>
where
    Rn: FnMut(usize, usize, usize, usize) -> anyhow::Result<()>,
    Rj: FnMut(Rejection) -> anyhow::Result<()>,
{
    let total = produced.len();
    let mut survived = 0usize;
    for (idx, &(s, e)) in produced.iter().enumerate() {
        if let Some(reason) = crate::filter::check(&seq[s..e], &qual[s..e], filter_cfg) {
            // The per-segment verdict attributes a missing read to a specific
            // segment and reason, which no run-level counter can do.
            tracing::trace!(
                segment = idx + 1,
                of = total,
                start = s,
                end = e,
                len = e - s,
                reason = reason.label(),
                "Segment dropped"
            );
            counters.record_segment_drop(reason);
            reject(Rejection::Segment {
                idx,
                total,
                start: s,
                end: e,
                reason,
            })?;
            continue;
        }
        tracing::trace!(
            segment = idx + 1,
            of = total,
            start = s,
            end = e,
            len = e - s,
            "Segment kept"
        );
        render(idx, total, s, e)?;
        counters.output_reads.fetch_add(1, Ordering::Relaxed);
        counters
            .output_bases
            .fetch_add((e - s) as u64, Ordering::Relaxed);
        survived += 1;
    }
    if produced.is_empty() {
        tracing::trace!("Read produced no segments");
        counters
            .reads_trimmed_to_nothing
            .fetch_add(1, Ordering::Relaxed);
        reject(Rejection::Whole)?;
    } else if survived == 0 {
        tracing::trace!(produced = total, "Every segment filtered");
        counters.reads_all_filtered.fetch_add(1, Ordering::Relaxed);
    } else {
        counters.reads_with_output.fetch_add(1, Ordering::Relaxed);
    }
    Ok(())
}

/// What `process_read_segments` rejected: one trimmed segment, or the whole
/// read because trimming produced no segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Rejection {
    /// A produced segment that failed a post-trim filter.
    Segment {
        /// 0-based segment index.
        idx: usize,
        /// Number of segments produced from the read.
        total: usize,
        /// First base of the segment, inclusive.
        start: usize,
        /// End of the segment, exclusive.
        end: usize,
        /// The failed filter.
        reason: DropReason,
    },
    /// The read produced no segment.
    Whole,
}

/// End-of-run counters, snapshotted from `Counters`.
#[derive(Debug, Default, Clone, Copy)]
pub struct Stats {
    /// Input reads consumed.
    pub input_reads: u64,
    /// Output segments written.
    pub output_reads: u64,
    /// Sum of SEQ lengths (bases) across every input read.
    pub input_bases: u64,
    /// Sum of surviving segment lengths (bases) written to output.
    pub output_bases: u64,
    /// Reads carrying a malformed per-base tag, left untouched; see
    /// `Counters::malformed_tag_reads`. Surfaced as a run-level advisory; not
    /// an error.
    pub malformed_tag_reads: u64,
    /// Input reads whose modification block was malformed and removed; see
    /// `Counters::malformed_mod_reads`.
    pub malformed_mod_reads: u64,
    /// Trimmed reads whose `ds`/`ls` undo blobs were removed.
    pub undo_tags_dropped_reads: u64,
    /// Trimmed or split reads whose fibertools footprint arrays were removed.
    pub footprint_tags_dropped_reads: u64,
    /// Reads whose `bi` barcode tag was unusable; see
    /// `Counters::barcode_tag_malformed_reads`.
    pub barcode_tag_malformed_reads: u64,
    /// Reads with an unverified barcode span; see
    /// `Counters::barcode_tag_unverified_reads`.
    pub barcode_tag_unverified_reads: u64,
    /// Read-level: input reads with at least one written segment.
    pub reads_with_output: u64,
    /// Read-level: input reads that produced zero segments at all (empty
    /// read, fully consumed by adapter trimming, or an over-crop).
    /// `trim::apply` returned no intervals, so the per-segment filter loop
    /// never ran.
    pub reads_trimmed_to_nothing: u64,
    /// Read-level: input reads that produced at least one segment, but every
    /// one of them was rejected by post-trim `filter::check`.
    pub reads_all_filtered: u64,
    /// Read-level: input reads rejected by `--tag-filter` before trimming.
    pub reads_tag_filtered: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for being
    /// shorter than `min_length` (including empty segments).
    pub segments_dropped_short: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for exceeding `max_length`.
    pub segments_dropped_long: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for quality below `min_qual`.
    pub segments_dropped_low_qual: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for quality above `max_qual`.
    pub segments_dropped_high_qual: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for
    /// expected errors above `max_expected_errors`.
    pub segments_dropped_expected_errors: u64,
    /// Segment-level: segments dropped by post-trim `filter::check` for GC fraction
    /// outside `[min_gc, max_gc]`.
    pub segments_dropped_gc: u64,
}

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

    #[test]
    fn batches_stop_at_weight_or_record_limit() {
        let by_weight: Vec<Vec<usize>> = Batches::new(
            vec![800_000usize; 5].into_iter(),
            |n: &usize| *n,
            FASTQ_BATCH,
        )
        .collect();
        assert_eq!(by_weight.iter().map(Vec::len).collect::<Vec<_>>(), [3, 2]);

        let bam: Vec<Vec<usize>> =
            Batches::new(vec![1usize; 65].into_iter(), |n: &usize| *n, BAM_BATCH).collect();
        assert_eq!(
            bam.iter().map(Vec::len).collect::<Vec<_>>(),
            [16, 16, 16, 16, 1]
        );
    }

    /// The three-way read-level invariant (`reads_with_output +
    /// reads_trimmed_to_nothing + reads_all_filtered == input_reads`) over three
    /// reads: (a) 2 surviving segments, (b) 2 produced segments both `TooShort`,
    /// and (c) an empty read producing none. (c) differs from (b) in that its
    /// per-segment filter loop never runs.
    #[test]
    fn three_way_read_counters_hold_the_invariant() {
        let counters = Counters::default();

        // (a) a read that splits into 2 surviving segments.
        counters.input_reads.fetch_add(1, Ordering::Relaxed);
        counters.output_reads.fetch_add(2, Ordering::Relaxed);
        counters.reads_with_output.fetch_add(1, Ordering::Relaxed);

        // (b) a read whose 2 produced segments are both filtered `TooShort`:
        // `reads_all_filtered` (segments were produced, but none survived).
        counters.input_reads.fetch_add(1, Ordering::Relaxed);
        counters.record_segment_drop(DropReason::TooShort);
        counters.record_segment_drop(DropReason::TooShort);
        counters.reads_all_filtered.fetch_add(1, Ordering::Relaxed);

        // (c) an empty input read: `trim::apply` produces no segments, so
        // `reads_trimmed_to_nothing` is bumped and no segment-level drop is
        // recorded.
        counters.input_reads.fetch_add(1, Ordering::Relaxed);
        counters
            .reads_trimmed_to_nothing
            .fetch_add(1, Ordering::Relaxed);

        let stats = counters.snapshot();

        assert_eq!(stats.input_reads, 3);
        assert_eq!(stats.output_reads, 2);
        assert_eq!(
            stats.reads_all_filtered, 1,
            "Read b produced segments, but every one was filtered"
        );
        assert_eq!(
            stats.reads_trimmed_to_nothing, 1,
            "Read c produced no segments"
        );
        assert_eq!(counters.reads_with_output.load(Ordering::Relaxed), 1);
        // `reads_with_output + reads_trimmed_to_nothing + reads_all_filtered ==
        // input_reads` holds (also asserted by `snapshot`).
        assert_eq!(
            counters.reads_with_output.load(Ordering::Relaxed)
                + stats.reads_trimmed_to_nothing
                + stats.reads_all_filtered,
            stats.input_reads
        );
        assert_eq!(stats.segments_dropped_short, 2);
        assert_eq!(stats.segments_dropped_long, 0);
        assert_eq!(stats.segments_dropped_low_qual, 0);
        assert_eq!(stats.segments_dropped_high_qual, 0);
        assert_eq!(stats.segments_dropped_gc, 0);
    }

    /// Covers all read-level outcomes and the corresponding render arguments.
    #[test]
    fn process_read_segments_dispatches_and_counts_all_three_outcomes() {
        let filter_cfg = FilterConfig {
            min_length: 3,
            max_length: usize::MAX,
            min_qual: 0.0,
            max_qual: 1000.0,
            max_expected_errors: None,
            min_gc: None,
            max_gc: None,
            qual_mode: crate::qual::QualMode::Mean,
        };

        // Trimmed to nothing: no produced intervals, so `render` is never called,
        // `reads_trimmed_to_nothing` is bumped and no segment-level drop is
        // recorded.
        {
            let counters = Counters::default();
            let mut calls: Vec<(usize, usize, usize, usize)> = Vec::new();
            process_read_segments(
                &[],
                b"",
                b"",
                &filter_cfg,
                &counters,
                |idx, total, s, e| {
                    calls.push((idx, total, s, e));
                    Ok(())
                },
                |_| Ok(()),
            )
            .unwrap();
            assert!(calls.is_empty());
            assert_eq!(counters.reads_trimmed_to_nothing.load(Ordering::Relaxed), 1);
            assert_eq!(counters.reads_all_filtered.load(Ordering::Relaxed), 0);
            assert_eq!(counters.reads_with_output.load(Ordering::Relaxed), 0);
            assert_eq!(counters.segments_dropped_short.load(Ordering::Relaxed), 0);
        }

        // All filtered: one produced segment, too short to pass, so `render` is
        // not called, `reads_all_filtered` is bumped and one segment drop is
        // recorded.
        {
            let counters = Counters::default();
            let seq = b"AA";
            let qual = b"II";
            let mut calls: Vec<(usize, usize, usize, usize)> = Vec::new();
            process_read_segments(
                &[(0, 2)],
                seq,
                qual,
                &filter_cfg,
                &counters,
                |idx, total, s, e| {
                    calls.push((idx, total, s, e));
                    Ok(())
                },
                |_| Ok(()),
            )
            .unwrap();
            assert!(calls.is_empty());
            assert_eq!(counters.reads_trimmed_to_nothing.load(Ordering::Relaxed), 0);
            assert_eq!(counters.reads_all_filtered.load(Ordering::Relaxed), 1);
            assert_eq!(counters.reads_with_output.load(Ordering::Relaxed), 0);
            assert_eq!(counters.segments_dropped_short.load(Ordering::Relaxed), 1);
        }

        // With output: two produced segments, both long enough, so `render` is
        // called once per survivor with `(idx, total, s, e)`.
        {
            let counters = Counters::default();
            let seq = b"AAAAAA";
            let qual = b"IIIIII";
            let mut calls: Vec<(usize, usize, usize, usize)> = Vec::new();
            process_read_segments(
                &[(0, 3), (3, 6)],
                seq,
                qual,
                &filter_cfg,
                &counters,
                |idx, total, s, e| {
                    calls.push((idx, total, s, e));
                    Ok(())
                },
                |_| Ok(()),
            )
            .unwrap();
            assert_eq!(calls, vec![(0, 2, 0, 3), (1, 2, 3, 6)]);
            assert_eq!(counters.reads_trimmed_to_nothing.load(Ordering::Relaxed), 0);
            assert_eq!(counters.reads_all_filtered.load(Ordering::Relaxed), 0);
            assert_eq!(counters.reads_with_output.load(Ordering::Relaxed), 1);
            assert_eq!(counters.output_reads.load(Ordering::Relaxed), 2);
            assert_eq!(counters.output_bases.load(Ordering::Relaxed), 6);
        }
    }

    fn driver_cfg(threads: usize, ordered: bool) -> Config {
        let path = std::path::Path::new("/dev/null");
        let mut cfg = crate::cli::config_for_test_threads(path, path, 0, 0, threads);
        cfg.ordered = ordered;
        cfg
    }

    /// Rendering odd items slowly forces completion order to differ from input
    /// order, so the ordered writer is exercised rather than trivially satisfied.
    fn run_driver(ordered: bool) -> Vec<usize> {
        let cfg = driver_cfg(4, ordered);
        let mut sink: Vec<usize> = Vec::new();
        let counters = Counters::default();
        run_parallel(
            (0..200usize).map(anyhow::Ok),
            BatchPolicy {
                target_weight: 1,
                max_items: 1,
                queue_per_worker: 1,
            },
            |_: &usize| 1,
            &cfg,
            &mut sink,
            |n, _cfg, out: &mut Vec<usize>| {
                if n % 2 == 1 {
                    std::thread::sleep(std::time::Duration::from_micros(200));
                }
                counters.reads_with_output.fetch_add(1, Ordering::Relaxed);
                out.push(n);
                Ok(())
            },
            Ok,
            |sink, batch: &Vec<usize>| {
                sink.extend_from_slice(batch);
                Ok(())
            },
            &counters,
        )
        .unwrap();
        sink
    }

    #[test]
    fn ordered_driver_bounds_read_ahead_behind_a_slow_batch() {
        use std::sync::atomic::AtomicUsize;
        use std::time::Duration;
        let cfg = driver_cfg(4, true);
        let consumed = AtomicUsize::new(0);
        let (ready_tx, ready_rx) = std::sync::mpsc::channel();
        let (release_tx, release_rx) = std::sync::mpsc::channel();
        let release_rx = Mutex::new(release_rx);
        let mut output = Vec::new();
        let counters = Counters::default();
        std::thread::scope(|scope| {
            let consumed_ref = &consumed;
            let observed = scope.spawn(move || {
                for _ in 0..7 {
                    ready_rx.recv_timeout(Duration::from_secs(5)).unwrap();
                }
                std::thread::sleep(Duration::from_millis(20));
                let read_ahead = consumed_ref.load(Ordering::Relaxed);
                release_tx.send(()).unwrap();
                read_ahead
            });
            run_parallel(
                (0..1000usize).map(|n| {
                    consumed.fetch_add(1, Ordering::Relaxed);
                    Ok(n)
                }),
                BatchPolicy {
                    target_weight: 1,
                    max_items: 1,
                    queue_per_worker: 1,
                },
                |_| 1,
                &cfg,
                &mut output,
                |n, _, out: &mut Vec<usize>| {
                    if n == 0 {
                        release_rx
                            .lock()
                            .unwrap()
                            .recv_timeout(Duration::from_secs(5))
                            .unwrap();
                    } else if n < 8 {
                        ready_tx.send(()).unwrap();
                    }
                    counters.reads_with_output.fetch_add(1, Ordering::Relaxed);
                    out.push(n);
                    Ok(())
                },
                Ok,
                |out, batch: &Vec<usize>| {
                    out.extend_from_slice(batch);
                    Ok(())
                },
                &counters,
            )
            .unwrap();
            assert_eq!(observed.join().unwrap(), 8);
        });
        assert_eq!(output, (0..1000).collect::<Vec<_>>());
    }

    #[test]
    fn ordered_driver_propagates_write_errors() {
        let cfg = driver_cfg(4, true);
        let error = run_parallel(
            (0..1000usize).map(Ok),
            BatchPolicy {
                target_weight: 1,
                max_items: 1,
                queue_per_worker: 1,
            },
            |_| 1,
            &cfg,
            &mut (),
            |n, _, out: &mut Vec<usize>| {
                out.push(n);
                Ok(())
            },
            Ok,
            |_, _: &Vec<usize>| Err(std::io::Error::other("sink failed")),
            &Counters::default(),
        )
        .unwrap_err();
        assert_eq!(error.to_string(), "sink failed");
    }

    /// A batch that fails to pack is never written, so the ordered writer
    /// cannot advance past it; the run still ends with the error rather than
    /// waiting on the reorder window.
    #[test]
    fn ordered_driver_ends_on_a_pack_error() {
        let cfg = driver_cfg(4, true);
        let error = run_parallel(
            (0..1000usize).map(Ok),
            BatchPolicy {
                target_weight: 1,
                max_items: 1,
                queue_per_worker: 1,
            },
            |_| 1,
            &cfg,
            &mut Vec::new(),
            |n, _, out: &mut Vec<usize>| {
                out.push(n);
                Ok(())
            },
            |batch: Vec<usize>| {
                if batch == [3] {
                    return Err(std::io::Error::other("pack failed"));
                }
                Ok(batch)
            },
            |out: &mut Vec<usize>, batch: &Vec<usize>| {
                out.extend_from_slice(batch);
                Ok(())
            },
            &Counters::default(),
        )
        .unwrap_err();
        assert_eq!(error.to_string(), "pack failed");
    }

    #[test]
    fn ordered_driver_writes_in_input_order() {
        let out = run_driver(true);
        assert_eq!(out, (0..200).collect::<Vec<_>>());
    }

    #[test]
    fn unordered_driver_writes_every_item_once() {
        let mut out = run_driver(false);
        out.sort_unstable();
        assert_eq!(out, (0..200).collect::<Vec<_>>());
    }

    /// The record stream is fused on its first `Err`: the source is never
    /// polled again, so a reader left in an inconsistent state after an I/O
    /// error cannot panic.
    #[test]
    fn driver_stops_polling_the_source_after_its_first_error() {
        struct PoisonAfterError {
            n: usize,
            yielded_error: bool,
        }
        impl Iterator for PoisonAfterError {
            type Item = anyhow::Result<usize>;
            fn next(&mut self) -> Option<Self::Item> {
                assert!(!self.yielded_error, "Source polled after it returned Err");
                if self.n == 50 {
                    self.yielded_error = true;
                    return Some(Err(anyhow::anyhow!("incomplete stream")));
                }
                self.n += 1;
                Some(Ok(self.n))
            }
        }
        let cfg = driver_cfg(4, false);
        let mut sink: Vec<usize> = Vec::new();
        let counters = Counters::default();
        let res = run_parallel(
            PoisonAfterError {
                n: 0,
                yielded_error: false,
            },
            FASTQ_BATCH,
            |_: &usize| 1,
            &cfg,
            &mut sink,
            |n, _cfg, out: &mut Vec<usize>| {
                out.push(n);
                Ok(())
            },
            Ok,
            |sink, batch: &Vec<usize>| {
                sink.extend_from_slice(batch);
                Ok(())
            },
            &counters,
        );
        assert_eq!(res.unwrap_err().to_string(), "incomplete stream");
    }

    /// A render error stops the run: records after the failing one are not
    /// rendered, so a failing run does not process the rest of its input.
    #[test]
    fn driver_stops_rendering_after_the_first_render_error() {
        use std::sync::atomic::AtomicUsize;

        let cfg = driver_cfg(2, false);
        let mut sink: Vec<usize> = Vec::new();
        let counters = Counters::default();
        let rendered = AtomicUsize::new(0);
        let res = run_parallel(
            (0..100_000usize).map(anyhow::Ok),
            BatchPolicy {
                target_weight: 1,
                max_items: 1,
                queue_per_worker: 1,
            },
            |_: &usize| 1,
            &cfg,
            &mut sink,
            |n, _cfg, out: &mut Vec<usize>| {
                rendered.fetch_add(1, Ordering::Relaxed);
                if n == 10 {
                    anyhow::bail!("record 10 is malformed");
                }
                out.push(n);
                Ok(())
            },
            Ok,
            |sink, batch: &Vec<usize>| {
                sink.extend_from_slice(batch);
                Ok(())
            },
            &counters,
        );
        assert!(res.is_err());
        assert!(
            rendered.load(Ordering::Relaxed) < 1_000,
            "Rendering continued long after the first error: {} records",
            rendered.load(Ordering::Relaxed)
        );
    }
}