zuoer 0.3.2

Domain-neutral run segmenter: the Silero-derived hysteresis state machine that turns any frame-probability sequence into contiguous runs with mean/peak aggregates — one instance per class for sound-event detection. Over it sits a backend-agnostic voice-activity-detection (VAD) shell: the VadBackend seam and the Speech* names. The model, inference runtime, and audio I/O live in the backend crate.
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
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
use std::collections::VecDeque;

use crate::options::{RunOptions, SampleRate};

/// One contiguous above-threshold run on the stream timeline.
///
/// Domain-neutral: a run is whatever the frame probabilities were scoring
/// — speech for a VAD backend, one sound-event class for a detector like
/// CED. [`SpeechSegment`](crate::SpeechSegment) is an alias of this type.
///
/// # Probability aggregates
///
/// [`mean_probability`](Self::mean_probability) and
/// [`peak_probability`](Self::peak_probability) summarize the frame
/// probabilities the run was built from — the data source for VAD segment
/// confidence and CED event confidence. Both are accumulated in O(1) per
/// frame over the run's **raw model-frame span**:
///
/// - **Padding is excluded.** [`RunOptions::pad`] extends the emitted
///   `[start_sample, end_sample)` on both sides, but padding is a timeline
///   courtesy, not an observation: there are no probabilities out there to
///   average, so the accumulator covers only `active_raw_start ..
///   raw_end`.
/// - **Bridged frames are included.** Frames inside a gap shorter than
///   [`RunOptions::min_gap_duration`] genuinely occurred inside the run,
///   so they stay in the mean and pull it down. That is the correct
///   signal.
/// - **A force-split cuts the accumulator too.** When
///   [`RunOptions::max_run_duration`] force-splits, the emitted run
///   carries only what was accumulated up to the split point, and the
///   continuation run's accumulator restarts from the frame the
///   continuation starts at. Frames in the gap that the split landed on
///   belong to neither run and appear in neither aggregate.
///
/// Both aggregates on a run the [`RunSegmenter`] emits are always finite
/// and inside `[0, 1]`, whatever the probability producer fed in:
/// [`push_probability`](RunSegmenter::push_probability) canonicalizes
/// every frame — `NaN` to `0.0`, everything else clamped — before it
/// reaches either the state machine or the accumulator.
///
/// A [`Run`] built directly with [`new`](Self::new) has zeroed aggregates;
/// only runs the [`RunSegmenter`] emits carry observed values. The
/// [`with_mean_probability`](Self::with_mean_probability) /
/// [`with_peak_probability`](Self::with_peak_probability) setters store
/// what the caller hands them, so the range guarantee above covers
/// segmenter-emitted runs only.
///
/// `Eq` is deliberately not implemented — the aggregates are floats.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Run {
  start_sample: u64,
  end_sample: u64,
  sample_rate: SampleRate,
  mean_probability: f32,
  peak_probability: f32,
}

impl Run {
  /// Create a new run with the given start and end samples and sample
  /// rate, and zeroed probability aggregates.
  ///
  /// Attach observed aggregates with
  /// [`with_mean_probability`](Self::with_mean_probability) /
  /// [`with_peak_probability`](Self::with_peak_probability).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(start_sample: u64, end_sample: u64, sample_rate: SampleRate) -> Self {
    Self {
      start_sample,
      end_sample,
      sample_rate,
      mean_probability: 0.0,
      peak_probability: 0.0,
    }
  }

  /// Set the mean frame probability over this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_mean_probability(mut self, mean_probability: f32) -> Self {
    self.mean_probability = mean_probability;
    self
  }

  /// Set the peak frame probability over this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_peak_probability(mut self, peak_probability: f32) -> Self {
    self.peak_probability = peak_probability;
    self
  }

  /// Returns the start sample of this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn start_sample(&self) -> u64 {
    self.start_sample
  }

  /// Returns the end sample of this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn end_sample(&self) -> u64 {
    self.end_sample
  }

  /// Returns the sample rate of this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn sample_rate(&self) -> SampleRate {
    self.sample_rate
  }

  /// Returns the number of samples in this run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn sample_count(&self) -> u64 {
    self.end_sample.saturating_sub(self.start_sample)
  }

  /// Returns the start time of this run in seconds.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn start_seconds(&self) -> f64 {
    self.start_sample as f64 / self.sample_rate.hz() as f64
  }

  /// Returns the end time of this run in seconds.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn end_seconds(&self) -> f64 {
    self.end_sample as f64 / self.sample_rate.hz() as f64
  }

  /// Returns the mean frame probability over this run's raw model-frame
  /// span — padding excluded, bridged frames included.
  ///
  /// See the [type-level notes](Self#probability-aggregates) for the exact
  /// span the accumulator covers. `0.0` for a run built with
  /// [`new`](Self::new).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn mean_probability(&self) -> f32 {
    self.mean_probability
  }

  /// Returns the highest frame probability over this run's raw
  /// model-frame span — padding excluded, bridged frames included.
  ///
  /// See the [type-level notes](Self#probability-aggregates) for the exact
  /// span the accumulator covers. `0.0` for a run built with
  /// [`new`](Self::new).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn peak_probability(&self) -> f32 {
    self.peak_probability
  }
}

/// O(1) running mean/peak over a set of frame probabilities.
///
/// The sum is kept in `f64` so a long run does not lose the tail of its
/// mean to `f32` rounding. Every observation arrives already canonicalized
/// into `[0, 1]` by [`normalize_probability`] — the accumulator is never
/// handed a `NaN`, an infinity, or an out-of-range value — so `sum`,
/// [`mean`](Self::mean), and [`peak`](Self::peak) are always finite and in
/// range. `peak` still starts at `f32::NEG_INFINITY` so the first
/// observation wins outright instead of tying with a `0.0` seed the run
/// never observed.
#[derive(Debug, Clone, Copy, PartialEq)]
struct Aggregate {
  sum: f64,
  count: u64,
  peak: f32,
}

impl Aggregate {
  const EMPTY: Self = Self {
    sum: 0.0,
    count: 0,
    peak: f32::NEG_INFINITY,
  };

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn observe(&mut self, probability: f32) {
    self.sum += probability as f64;
    self.count += 1;
    if probability > self.peak {
      self.peak = probability;
    }
  }

  /// Fold `other`'s frames into this accumulator. Used when a gap shorter
  /// than `min_gap_duration` is bridged and its frames become part of the
  /// run.
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn absorb(&mut self, other: &Self) {
    self.sum += other.sum;
    self.count += other.count;
    if other.peak > self.peak {
      self.peak = other.peak;
    }
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn mean(&self) -> f32 {
    if self.count == 0 {
      0.0
    } else {
      (self.sum / self.count as f64) as f32
    }
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn peak(&self) -> f32 {
    if self.count == 0 { 0.0 } else { self.peak }
  }
}

/// Canonicalize one raw frame probability into `[0, 1]`.
///
/// `NaN` is mapped to `0.0` rather than clamped: `f32::clamp` returns
/// `NaN` for a `NaN` input, so a bare clamp would let it straight through.
/// See [`RunSegmenter::push_probability`] for the policy this implements,
/// why `0.0` is the mapping that preserves the pre-aggregate behaviour,
/// and the single threshold configuration where it does not.
#[cfg_attr(not(tarpaulin), inline(always))]
const fn normalize_probability(probability: f32) -> f32 {
  if probability.is_nan() {
    0.0
  } else {
    probability.clamp(0.0, 1.0)
  }
}

/// A recorded preferred force-split boundary, with the aggregate of the
/// frames that precede it.
#[derive(Debug, Clone, Copy)]
struct SplitPoint {
  /// Raw end sample the force-split would close the run at.
  end: u64,
  /// Aggregate over `active_raw_start .. end`.
  aggregate: Aggregate,
}

/// The resume point a force-split would restart the next run at, with the
/// aggregate of the frames observed since it.
#[derive(Debug, Clone, Copy)]
struct PendingRun {
  /// Raw start sample of the continuation run.
  start: u64,
  /// Aggregate over `start .. current frame`.
  aggregate: Aggregate,
}

/// Streaming hysteresis state machine that turns a frame-probability
/// sequence into contiguous [`Run`]s.
///
/// Domain-neutral: it consumes probabilities and knows nothing about what
/// produced them. [`SpeechSegmenter`](crate::SpeechSegmenter) is an alias
/// of this type; a sound-event detector drives the same machine per class.
///
/// # Timeline contract
///
/// The segmenter has no clock of its own. It counts frames and multiplies:
/// each [`push_probability`](Self::push_probability) advances the timeline
/// by exactly [`frame_hop`](Self::frame_hop) samples, so the `n`-th
/// probability covers `[n * frame_hop, (n + 1) * frame_hop)` and every
/// sample boundary a [`Run`] carries is a multiple of the hop (before
/// padding). Duration options are converted against
/// [`sample_rate`](Self::sample_rate) once, so a threshold that is not a
/// whole number of hops is compared at sample resolution, not rounded to
/// frames.
///
/// `frame_hop` is the **hop**, not the analysis window: for a producer
/// whose windows overlap (a 400-sample window advanced by 160 samples) it
/// is 160. It defaults to the sample rate's model chunk size (512 samples
/// at 16 kHz — the Silero geometry); a producer with a different hop
/// declares it through [`set_frame_hop`](Self::set_frame_hop) (done
/// automatically by [`detect_speech_with`](crate::detect_speech_with) from
/// [`VadBackend::frame_hop`](crate::VadBackend::frame_hop)) so the same
/// segmentation rules apply at any frame geometry.
///
/// # Emitted runs
///
/// A run opens on the first frame at or above
/// [`start_threshold`](RunOptions::start_threshold) and stays open while
/// probabilities hold at or above
/// [`end_threshold`](RunOptions::end_threshold). A below-threshold gap
/// shorter than [`min_gap_duration`](RunOptions::min_gap_duration) is
/// bridged; a longer one closes the run at the gap's first frame. A closed
/// run is dropped unless its raw span reaches
/// [`min_run_duration`](RunOptions::min_run_duration), and the surviving
/// run is padded by [`pad`](RunOptions::pad) on both sides. Each emitted
/// run carries the mean and peak of the probabilities over its raw span;
/// see [`Run`](Run#probability-aggregates).
#[derive(Debug, Clone)]
pub struct RunSegmenter {
  options: RunOptions,
  // Frame hop: the timeline advances by this many samples per
  // probability. Defaults to `sample_rate().chunk_samples()`; a backend
  // with a different hop overrides it via `set_frame_hop`.
  frame_hop: u64,
  current_sample: u64,
  // Padded start sample used for emitted runs.
  active_start: Option<u64>,
  // Raw model-frame start sample used for upstream-compatible duration checks.
  active_raw_start: Option<u64>,
  tentative_end: Option<u64>,
  // Aggregate over the run's confirmed frames: `active_raw_start ..
  // tentative_end` when a gap is open, `active_raw_start .. now`
  // otherwise.
  aggregate: Aggregate,
  // Aggregate over the frames of the currently open gap (`tentative_end
  // .. now`). Folded into `aggregate` when the gap is bridged; discarded
  // when the gap closes the run, because those frames sit past `raw_end`.
  tentative_aggregate: Aggregate,
  // Start sample of the most recent gap long enough to be a preferred
  // force-split point, with the aggregate of the frames before it.
  max_split: Option<SplitPoint>,
  // First above-threshold frame after `max_split`; used to resume after a
  // force-split at that gap boundary, with the aggregate accumulated
  // since.
  next_run: Option<PendingRun>,
  // Queue of runs closed by recent push_probabilities calls that have not
  // yet been popped by the caller. Drained one run at a time via
  // `pop_pending`.
  //
  // `pub(crate)` so the shell's inline tests can stage a queue directly.
  pub(crate) pending_segments: VecDeque<Run>,
}

impl RunSegmenter {
  /// Create a new `RunSegmenter` with the given options.
  ///
  /// The frame hop defaults to the options' sample-rate model chunk
  /// size (512 samples at 16 kHz). Override it with
  /// [`set_frame_hop`](Self::set_frame_hop) for a producer that declares a
  /// different hop.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn new(options: RunOptions) -> Self {
    let frame_hop = options.sample_rate().chunk_samples() as u64;
    Self {
      options,
      frame_hop,
      current_sample: 0,
      active_start: None,
      active_raw_start: None,
      tentative_end: None,
      aggregate: Aggregate::EMPTY,
      tentative_aggregate: Aggregate::EMPTY,
      max_split: None,
      next_run: None,
      pending_segments: VecDeque::new(),
    }
  }

  /// Returns a reference to the [`RunOptions`] used by this segmenter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn options(&self) -> &RunOptions {
    &self.options
  }

  /// Reconfigure the segmenter for a stream with a different sample rate.
  ///
  /// Changing sample rate starts a new logical timeline, so any
  /// in-flight run state is cleared.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_sample_rate(&mut self, sample_rate: SampleRate) {
    if self.sample_rate() != sample_rate {
      self.options.set_sample_rate(sample_rate);
      // A new rate implies a new default hop; a custom backend hop must
      // be re-applied via `set_frame_hop`.
      self.frame_hop = sample_rate.chunk_samples() as u64;
      self.reset();
    }
  }

  /// Returns the sample rate used by this segmenter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn sample_rate(&self) -> SampleRate {
    self.options.sample_rate()
  }

  /// Returns the frame hop: the number of samples this segmenter's
  /// timeline advances by per probability.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn frame_hop(&self) -> usize {
    self.frame_hop as usize
  }

  /// Set the frame hop.
  ///
  /// The segmenter advances its sample timeline by `frame_hop` for each
  /// probability fed to [`push_probability`](Self::push_probability), so
  /// this must equal the hop of the producer of those probabilities — the
  /// value a [`VadBackend`](crate::VadBackend) reports from
  /// [`frame_hop`](crate::VadBackend::frame_hop).
  /// [`detect_speech_with`](crate::detect_speech_with) applies it
  /// automatically; call it directly only when driving
  /// [`push_probability`](Self::push_probability) with a producer whose
  /// hop differs from the sample rate's model chunk size.
  ///
  /// [`set_sample_rate`](Self::set_sample_rate) resets the hop to the
  /// rate's model chunk size, so apply this after any rate change.
  ///
  /// # Panics
  ///
  /// Panics if `frame_hop` is zero.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_frame_hop(&mut self, frame_hop: usize) {
    assert!(frame_hop != 0, "frame_hop must be non-zero");
    self.frame_hop = frame_hop as u64;
  }

  /// Returns whether the segmenter is currently active (i.e., has an ongoing run).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn is_active(&self) -> bool {
    self.active_start.is_some()
  }

  /// Reset the segmenter's internal state: the in-flight run tracker
  /// (active start, tentative end, probability aggregates, force-split
  /// bookkeeping), the running sample counter, and any runs queued for
  /// [`pop_pending`](Self::pop_pending) drain.
  ///
  /// This resets only the segmenter. A backend driving it keeps its own
  /// per-stream memory (recurrent state, rolling context, un-chunked PCM
  /// tail); reset that separately via
  /// [`VadBackend::reset`](crate::VadBackend::reset) when reusing the pair
  /// for a new logical recording.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn reset(&mut self) {
    self.current_sample = 0;
    self.clear_run_memory();
    self.pending_segments.clear();
  }

  /// Number of runs currently queued for drain via
  /// [`pop_pending`](Self::pop_pending).
  ///
  /// Always `0` after a [`pop_pending`](Self::pop_pending) or
  /// [`finish`](Self::finish) call that returned `None`. Useful for tests
  /// that want to assert the caller has drained everything before tearing
  /// down a stream.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn pending_segment_count(&self) -> usize {
    self.pending_segments.len()
  }

  /// Consume one probability for one model frame.
  ///
  /// Returns `Some(run)` only when a run can be closed with the currently
  /// available evidence.
  ///
  /// This returns any closed run directly and does **not** touch the
  /// pending queue. To feed a run of probabilities and buffer the closed
  /// runs for ordered draining — the shape a streaming backend driver
  /// needs — use [`push_probabilities`](Self::push_probabilities)
  /// followed by [`pop_pending`](Self::pop_pending).
  ///
  /// # Malformed probabilities
  ///
  /// [`VadBackend`](crate::VadBackend) documents `[0, 1]`; this is the one
  /// place that contract is enforced rather than assumed. The value is
  /// canonicalized before anything else reads it, and that single
  /// canonical value drives **both** the hysteresis comparisons and the
  /// run's aggregates, so the state machine and the aggregate can never
  /// disagree about a frame:
  ///
  /// - `NaN` becomes `0.0` — inactive. `f32::clamp` returns `NaN` for a
  ///   `NaN` input, so this mapping is explicit rather than a bare clamp.
  ///   Every comparison against `NaN` is false, so such a frame already
  ///   behaved as below-threshold; `0.0` keeps that segmentation instead
  ///   of letting the frame escape into
  ///   [`mean_probability`](Run::mean_probability) as a `NaN` a consumer
  ///   would read.
  /// - everything else is clamped into range: `f32::INFINITY` and any
  ///   value above `1.0` become `1.0`; `f32::NEG_INFINITY` and any value
  ///   below `0.0` become `0.0`.
  ///
  /// ## Canonicalization changes no behaviour
  ///
  /// There is no configuration in which substituting the canonical value
  /// for the raw one moves a boundary. Every effective threshold is
  /// strictly positive — [`RunOptions`] clamps the start threshold, an
  /// explicit end threshold, and the derived end threshold alike into
  /// [`[MIN_THRESHOLD, 1]`](RunOptions::MIN_THRESHOLD) — so every
  /// comparison in the state machine reads the canonical value exactly as
  /// it read the raw one:
  ///
  /// - an input canonicalizing to `0.0` fails `>= threshold` just as the
  ///   raw value did — `NaN` because every comparison against it is
  ///   false, a negative or `f32::NEG_INFINITY` because it is below a
  ///   floor that is itself above zero;
  /// - an input canonicalizing to `1.0` satisfies `>= threshold` just as
  ///   the raw value did, since no threshold exceeds `1.0`;
  /// - an in-range input is not changed at all.
  ///
  /// The boundary case that would break this — an effective threshold of
  /// exactly `0.0`, where `0.0 >= 0.0` holds but `NaN >= 0.0` does not —
  /// is unreachable by construction; see
  /// [`RunOptions::MIN_THRESHOLD`] for why that configuration is excluded
  /// rather than documented.
  ///
  /// The repair keeps a malformed frame from corrupting a value a
  /// consumer reads; it is not a licence to emit out-of-range
  /// probabilities.
  pub fn push_probability(&mut self, probability: f32) -> Option<Run> {
    let probability = normalize_probability(probability);
    let frame_hop = self.frame_hop;
    let frame_start = self.current_sample;
    self.current_sample = self.current_sample.saturating_add(frame_hop);

    if probability >= self.options.start_threshold() {
      if let Some(tentative_end) = self.tentative_end.take() {
        let gap_samples = frame_start.saturating_sub(tentative_end);
        if gap_samples > self.options.min_gap_at_max_run_samples() {
          // Snapshot BEFORE the bridge fold: the split point closes at
          // `tentative_end`, so it carries only the frames before the gap.
          self.max_split = Some(SplitPoint {
            end: tentative_end,
            aggregate: self.aggregate,
          });
          self.next_run = Some(PendingRun {
            start: frame_start,
            aggregate: Aggregate::EMPTY,
          });
        }
        // The gap is bridged: its frames occurred inside the run, so they
        // stay in the run's aggregate.
        self.aggregate.absorb(&self.tentative_aggregate);
        self.tentative_aggregate = Aggregate::EMPTY;
      }
      if self.active_start.is_none() {
        self.active_start = Some(frame_start.saturating_sub(self.options.pad_samples()));
        self.active_raw_start = Some(frame_start);
        self.aggregate = Aggregate::EMPTY;
        self.aggregate.observe(probability);
        return None;
      }
    }

    let start = self.active_start?;
    let raw_start = self.active_raw_start?;
    if let Some(max_run_samples) = self.options.max_run_samples_for_frame(self.frame_hop)
      && frame_start.saturating_sub(raw_start) > max_run_samples
    {
      return self.split_at_max_duration(frame_start, probability);
    }

    if probability >= self.options.end_threshold() {
      self.observe(probability);
      return None;
    }

    // Gap counter is evaluated against `frame_start` (the start sample of
    // the current frame), not `current_sample` (which is already the *end*
    // of the current frame). This matches upstream Python `silero-vad`'s
    // `sil_dur_now = cur_sample - temp_end` semantics, where `cur_sample`
    // is read BEFORE the model consumes the current window. Without this,
    // the comparator fires one frame early — a 4-frame (128 ms) silence
    // dip would close a segment at default `min_silence_duration_ms =
    // 100`, where Python tolerates it and closes after 5 consecutive
    // low-probability frames. Both sides of that boundary are pinned by
    // the `four_frame_silence_dip_does_not_close_segment_at_default_min_silence`
    // and `five_frame_silence_dip_closes_segment_at_default_min_silence`
    // tests in `detector`.
    //
    // `get_or_insert` runs BEFORE `observe` so the frame at `gap_start`
    // lands in the tentative aggregate: `gap_start` is the run's `raw_end`
    // if this gap closes it, and `raw_end` is exclusive.
    let gap_start = *self.tentative_end.get_or_insert(frame_start);
    self.observe(probability);
    let gap_samples = frame_start.saturating_sub(gap_start);
    if gap_samples > self.options.min_gap_at_max_run_samples() {
      self.max_split = Some(SplitPoint {
        end: gap_start,
        aggregate: self.aggregate,
      });
    }
    if gap_samples < self.options.min_gap_samples() {
      return None;
    }

    let aggregate = self.aggregate;
    self.clear_run_memory();
    self.build_run(start, raw_start, gap_start, &aggregate)
  }

  /// Feed a sequence of frame probabilities and buffer any closed runs for
  /// ordered retrieval.
  ///
  /// Each probability is fed through
  /// [`push_probability`](Self::push_probability) in turn; every run it
  /// closes is appended to the internal pending queue rather than
  /// returned. Drain the queue one run at a time with
  /// [`pop_pending`](Self::pop_pending) (or, at end-of-stream,
  /// [`finish`](Self::finish)).
  ///
  /// This is the sans-I/O seam a streaming backend driver builds on: run
  /// the backend over the incoming PCM to obtain the frame probabilities,
  /// hand them here, then pop the closed runs. Passing an empty slice
  /// buffers nothing — a pure drain point when paired with
  /// [`pop_pending`](Self::pop_pending).
  pub fn push_probabilities(&mut self, probabilities: &[f32]) {
    for &probability in probabilities {
      if let Some(run) = self.push_probability(probability) {
        self.pending_segments.push_back(run);
      }
    }
  }

  /// Pop the next buffered run closed by
  /// [`push_probabilities`](Self::push_probabilities), in order, or
  /// `None` when the queue is empty.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn pop_pending(&mut self) -> Option<Run> {
    self.pending_segments.pop_front()
  }

  /// Compute the trailing open run (if any) without resetting.
  /// Helper for `finish`.
  fn take_trailing(&self) -> Option<Run> {
    let start = self.active_start?;
    let raw_start = self.active_raw_start?;
    let end = self.current_sample;
    if end.saturating_sub(raw_start) < self.options.min_run_samples() {
      None
    } else {
      // The trailing run spans every frame pushed since `raw_start`,
      // including an unclosed gap, so its aggregate includes them too.
      let mut aggregate = self.aggregate;
      aggregate.absorb(&self.tentative_aggregate);
      Some(Self::finished_run(
        start,
        end,
        self.sample_rate(),
        &aggregate,
      ))
    }
  }

  /// Finish the current stream and return the next available run.
  ///
  /// Enqueues the trailing open run (if any) onto the pending queue, then
  /// pops and returns the head of that queue. This preserves the order of
  /// any runs that an earlier
  /// [`push_probabilities`](Self::push_probabilities) queued but the
  /// caller hasn't drained yet (the rare force-split case): they come out
  /// before the trailing run.
  ///
  /// The in-flight run tracker is cleared so
  /// [`is_active`](Self::is_active) is `false` afterwards and a follow-up
  /// `finish()` can't re-emit the same trailing run. The pending queue is
  /// left intact so subsequent [`pop_pending`](Self::pop_pending) calls
  /// drain the rest; call [`Self::reset`] explicitly when starting a new
  /// stream.
  ///
  /// This closes the segmenter's own trailing run only. A backend driver
  /// that also needs to flush the backend's un-chunked PCM tail should
  /// first feed the flushed frame's probability through
  /// [`push_probabilities`](Self::push_probabilities), then call this.
  pub fn finish(&mut self) -> Option<Run> {
    if let Some(trailing) = self.take_trailing() {
      self.pending_segments.push_back(trailing);
    }
    self.clear_run_memory();
    self.pending_segments.pop_front()
  }

  fn split_at_max_duration(&mut self, frame_start: u64, probability: f32) -> Option<Run> {
    let start = self.active_start?;
    let raw_start = self.active_raw_start?;
    let (raw_end, aggregate) = match self.max_split {
      // Split back at the recorded gap: the emitted run carries only the
      // frames before that gap.
      Some(split) => (split.end, split.aggregate),
      // No recorded gap: the run closes at the current frame, so every
      // frame pushed so far belongs to it — including an in-flight gap
      // too short to have been recorded as a split point.
      None => {
        let mut aggregate = self.aggregate;
        aggregate.absorb(&self.tentative_aggregate);
        (frame_start, aggregate)
      }
    };
    let run = self.build_run(start, raw_start, raw_end, &aggregate);

    let next_raw_start = if let Some(next) = self.next_run.filter(|next| next.start >= raw_end) {
      self.active_start = Some(next.start.saturating_sub(self.options.pad_samples()));
      // The continuation restarts from the resume point, carrying the
      // frames observed since it. The frames inside the split gap
      // (`raw_end .. next.start`) belong to neither run.
      self.aggregate = next.aggregate;
      Some(next.start)
    } else if self.max_split.is_none() && probability >= self.options.start_threshold() {
      self.active_start = Some(frame_start.saturating_sub(self.options.pad_samples()));
      self.aggregate = Aggregate::EMPTY;
      Some(frame_start)
    } else {
      self.active_start = None;
      self.aggregate = Aggregate::EMPTY;
      None
    };
    self.active_raw_start = next_raw_start;
    self.clear_split_tracking();
    if next_raw_start.is_some() {
      // The current frame was not part of the emitted run (`raw_end <=
      // frame_start`), so it is the continuation's to observe.
      self.observe(probability);
    }

    run
  }

  /// Route one frame's probability into the accumulator that owns it: the
  /// tentative one while a gap is open, the run's own otherwise. A pending
  /// force-split resume point accumulates in parallel.
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn observe(&mut self, probability: f32) {
    if self.tentative_end.is_some() {
      self.tentative_aggregate.observe(probability);
    } else {
      self.aggregate.observe(probability);
    }
    if let Some(next) = self.next_run.as_mut() {
      next.aggregate.observe(probability);
    }
  }

  fn build_run(
    &self,
    start: u64,
    raw_start: u64,
    raw_end: u64,
    aggregate: &Aggregate,
  ) -> Option<Run> {
    let end_sample = raw_end
      .saturating_add(self.options.pad_samples())
      .min(self.current_sample);
    if raw_end.saturating_sub(raw_start) < self.options.min_run_samples() {
      None
    } else {
      Some(Self::finished_run(
        start,
        end_sample,
        self.sample_rate(),
        aggregate,
      ))
    }
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn finished_run(
    start_sample: u64,
    end_sample: u64,
    sample_rate: SampleRate,
    aggregate: &Aggregate,
  ) -> Run {
    Run::new(start_sample, end_sample, sample_rate)
      .with_mean_probability(aggregate.mean())
      .with_peak_probability(aggregate.peak())
  }

  fn clear_run_memory(&mut self) {
    self.active_start = None;
    self.active_raw_start = None;
    self.aggregate = Aggregate::EMPTY;
    self.clear_split_tracking();
  }

  fn clear_split_tracking(&mut self) {
    self.tentative_end = None;
    self.tentative_aggregate = Aggregate::EMPTY;
    self.max_split = None;
    self.next_run = None;
  }
}

#[cfg(test)]
mod tests {
  use std::time::Duration;

  use crate::{RunOptions, SampleRate};

  use super::{Run, RunSegmenter};

  /// The 16 kHz default hop: one frame is 512 samples / 32 ms.
  const HOP: u64 = 512;

  fn collect(segmenter: &mut RunSegmenter, probabilities: &[f32]) -> Vec<Run> {
    let mut runs = Vec::new();
    for probability in probabilities {
      if let Some(run) = segmenter.push_probability(*probability) {
        runs.push(run);
      }
    }
    if let Some(run) = segmenter.finish() {
      runs.push(run);
    }
    runs
  }

  #[track_caller]
  fn assert_close(actual: f32, expected: f32, what: &str) {
    assert!(
      (actual - expected).abs() < 1e-6,
      "{what}: expected {expected}, got {actual}"
    );
  }

  /// Aggregation baseline: a run of exactly one frame reports that frame's
  /// probability as both mean and peak — no off-by-one in the accumulator
  /// and no phantom frame from the gap frame that closed it.
  #[test]
  fn single_frame_run_reports_that_frame_as_mean_and_peak() {
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO)
      .with_min_gap_duration(Duration::ZERO);
    let mut segmenter = RunSegmenter::new(options);

    // One above-threshold frame, then one below-threshold frame which (at
    // `min_gap_duration = 0`) closes the run immediately.
    let runs = collect(&mut segmenter, &[0.8, 0.0]);

    assert_eq!(runs.len(), 1);
    assert_eq!(runs[0].start_sample(), 0);
    assert_eq!(runs[0].end_sample(), HOP, "one frame wide");
    assert_close(runs[0].mean_probability(), 0.8, "mean");
    assert_close(runs[0].peak_probability(), 0.8, "peak");
  }

  /// Rule 2: frames bridged by `min_gap_duration` are inside the run, so
  /// they stay in the mean and pull it down. Rule 1's other half: the
  /// frames of the gap that finally CLOSES the run sit past `raw_end` and
  /// must not.
  ///
  /// Eight frames span the run — `0.6, 1.0`, four bridged zeros, `0.6,
  /// 0.6` — so the mean is `2.8 / 8 = 0.35` while the peak stays `1.0`.
  ///
  /// Mutations: dropping the bridged frames gives `2.8 / 4 = 0.7`;
  /// including the five closing gap frames gives `2.8 / 13 ≈ 0.215`;
  /// pinning `peak` to the first observation gives `0.6`. All red.
  #[test]
  fn bridged_gap_frames_stay_in_the_mean() {
    // Defaults: start 0.5 / end 0.35, `min_gap_duration = 100 ms` (1600
    // samples), so a four-frame dip (max gap 1536) is bridged and a
    // five-frame dip (2048) closes.
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO);
    let mut segmenter = RunSegmenter::new(options);

    let mut probabilities = vec![0.6, 1.0];
    probabilities.extend([0.0; 4]);
    probabilities.extend([0.6, 0.6]);
    probabilities.extend([0.0; 5]);

    let runs = collect(&mut segmenter, &probabilities);

    assert_eq!(runs.len(), 1);
    assert_eq!(runs[0].start_sample(), 0);
    assert_eq!(
      runs[0].end_sample(),
      8 * HOP,
      "the run spans the eight frames the aggregate covers"
    );
    assert_close(runs[0].mean_probability(), 0.35, "mean");
    assert_close(runs[0].peak_probability(), 1.0, "peak");
  }

  /// Rule 1: `pad` extends the emitted timeline but contributes no
  /// observations. Here the run is padded by one whole frame on each side,
  /// so it spans four frames of timeline over two frames of evidence — and
  /// the two frames the padding covers are both `0.0`.
  ///
  /// Mutation: folding the closing gap frame (the one the trailing pad
  /// covers) into the aggregate gives `2 / 3 ≈ 0.667`. Red. The leading
  /// pad has no mutation to make: no accumulator exists before the run
  /// opens, so those frames are unreachable by construction.
  #[test]
  fn padding_is_outside_the_aggregated_span() {
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_min_gap_duration(Duration::ZERO)
      .with_pad(Duration::from_millis(32));
    let mut segmenter = RunSegmenter::new(options);

    let runs = collect(&mut segmenter, &[0.0, 1.0, 1.0, 0.0]);

    assert_eq!(runs.len(), 1);
    assert_eq!(runs[0].start_sample(), 0, "raw start 512 padded back to 0");
    assert_eq!(runs[0].end_sample(), 4 * HOP, "raw end 1536 padded to 2048");
    assert_eq!(runs[0].sample_count(), 4 * HOP);
    assert_close(runs[0].mean_probability(), 1.0, "mean");
    assert_close(runs[0].peak_probability(), 1.0, "peak");
  }

  /// A run closed by `finish` spans every frame pushed since it opened,
  /// including an unclosed sub-threshold gap — so those frames ARE in its
  /// aggregate. The span and the accumulator must agree: three frames of
  /// timeline, three observations, mean `2 / 3`.
  ///
  /// Mutation: discarding the tentative aggregate in `take_trailing`
  /// gives `1.0`. Red.
  #[test]
  fn trailing_run_aggregates_its_unclosed_gap() {
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO);
    let mut segmenter = RunSegmenter::new(options);

    let runs = collect(&mut segmenter, &[1.0, 1.0, 0.0]);

    assert_eq!(runs.len(), 1);
    assert_eq!(runs[0].end_sample(), 3 * HOP);
    assert_close(runs[0].mean_probability(), 2.0 / 3.0, "mean");
    assert_close(runs[0].peak_probability(), 1.0, "peak");
  }

  /// Rule 3, no recorded gap: the force-split closes at the current frame,
  /// so the emitted run carries the five frames before it and the
  /// continuation's accumulator restarts at the split.
  ///
  /// Boundaries are the ones `force_splits_long_speech_when_max_duration_is_reached`
  /// already pins (2560 / 4096); this adds the aggregates.
  ///
  /// Mutation: not restarting the accumulator gives the continuation
  /// `(5·0.6 + 3·1.0) / 8 = 0.75` instead of `1.0`. Red.
  #[test]
  fn force_split_at_the_current_frame_restarts_the_aggregate() {
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO)
      .with_max_run_duration(Duration::from_millis(160));
    let mut segmenter = RunSegmenter::new(options);

    let mut probabilities = vec![0.6; 5];
    probabilities.extend([1.0; 3]);

    let runs = collect(&mut segmenter, &probabilities);

    assert_eq!(runs.len(), 2);
    assert_eq!(runs[0].end_sample(), 5 * HOP);
    assert_close(runs[0].mean_probability(), 0.6, "split run mean");
    assert_close(runs[0].peak_probability(), 0.6, "split run peak");

    assert_eq!(runs[1].start_sample(), 5 * HOP);
    assert_close(runs[1].mean_probability(), 1.0, "continuation mean");
    assert_close(runs[1].peak_probability(), 1.0, "continuation peak");
  }

  /// Rule 3, recorded gap: the force-split lands back at a gap the
  /// segmenter recorded earlier, so the emitted run carries only the four
  /// frames BEFORE that gap (mean `0.9`) even though the gap had already
  /// been bridged into the live accumulator; the continuation restarts at
  /// the resume point (mean `0.8`). The four gap frames the split landed
  /// on belong to neither run and appear in neither aggregate.
  ///
  /// Boundaries are the ones `prefers_recorded_silence_when_splitting_long_speech`
  /// already pins (2048 / 4096 / 6144); this adds the aggregates.
  ///
  /// Mutations: emitting the live (post-bridge) accumulator instead of the
  /// snapshot gives `3.6 / 8 = 0.45` for the first run; not restarting the
  /// continuation's accumulator gives `(3.6 + 3.2) / 12 ≈ 0.567` for the
  /// second. Both red.
  #[test]
  fn force_split_at_a_recorded_gap_carries_the_pre_gap_aggregate() {
    let options = RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO)
      .with_min_gap_duration(Duration::from_millis(300))
      .with_min_gap_at_max_run(Duration::from_millis(64))
      .with_max_run_duration(Duration::from_millis(256));
    let mut segmenter = RunSegmenter::new(options);

    let mut probabilities = vec![0.9; 4];
    probabilities.extend([0.0; 4]);
    probabilities.extend([0.8; 4]);

    let runs = collect(&mut segmenter, &probabilities);

    assert_eq!(runs.len(), 2);
    assert_eq!(runs[0].start_sample(), 0);
    assert_eq!(runs[0].end_sample(), 4 * HOP);
    assert_close(runs[0].mean_probability(), 0.9, "pre-gap run mean");
    assert_close(runs[0].peak_probability(), 0.9, "pre-gap run peak");

    assert_eq!(runs[1].start_sample(), 8 * HOP);
    assert_eq!(runs[1].end_sample(), 12 * HOP);
    assert_close(runs[1].mean_probability(), 0.8, "continuation mean");
    assert_close(runs[1].peak_probability(), 0.8, "continuation peak");
  }

  /// A hand-built `Run` carries zeroed aggregates until they are set:
  /// only runs the segmenter emits carry observed values.
  #[test]
  fn run_constructor_zeroes_the_aggregates() {
    let run = Run::new(0, 1_000, SampleRate::Rate16k);
    assert_close(run.mean_probability(), 0.0, "mean");
    assert_close(run.peak_probability(), 0.0, "peak");

    let annotated = run.with_mean_probability(0.5).with_peak_probability(0.9);
    assert_close(annotated.mean_probability(), 0.5, "mean");
    assert_close(annotated.peak_probability(), 0.9, "peak");
    assert_eq!(annotated.start_sample(), run.start_sample());
    assert_eq!(annotated.end_sample(), run.end_sample());
  }

  /// Malformed inputs whose canonical value is `0.0`: `NaN` (mapped, not
  /// clamped) and everything at or below the bottom of the range.
  const LOW_INVALID: [(f32, &str); 3] = [
    (f32::NAN, "NaN"),
    (f32::NEG_INFINITY, "-inf"),
    (-1.0, "finite -1.0"),
  ];

  /// Malformed inputs whose canonical value is `1.0`: everything at or
  /// above the top of the range.
  const HIGH_INVALID: [(f32, &str); 2] = [(f32::INFINITY, "+inf"), (2.0, "finite 2.0")];

  /// Every emitted run's aggregates must be finite and inside `[0, 1]`,
  /// whatever the producer fed in. This is the invariant the
  /// `push_probability` normalization exists to hold.
  #[track_caller]
  fn assert_in_range(run: &Run, what: &str) {
    let mean = run.mean_probability();
    let peak = run.peak_probability();
    assert!(
      mean.is_finite() && (0.0..=1.0).contains(&mean),
      "{what}: mean {mean} is not a finite probability in [0, 1]"
    );
    assert!(
      peak.is_finite() && (0.0..=1.0).contains(&peak),
      "{what}: peak {peak} is not a finite probability in [0, 1]"
    );
  }

  #[track_caller]
  fn assert_all_in_range(runs: &[Run], what: &str) {
    for (index, run) in runs.iter().enumerate() {
      assert_in_range(run, &format!("{what} run {index}"));
    }
  }

  fn open_options() -> RunOptions {
    RunOptions::default()
      .with_min_run_duration(Duration::ZERO)
      .with_pad(Duration::ZERO)
  }

  /// Close path 1 (a gap longer than `min_gap_duration` closes the run),
  /// above-range input. `+inf` / `2.0` land on an active frame and both
  /// canonicalize to `1.0`, so the three-frame run means `2.2 / 3` and
  /// peaks at exactly `1.0`.
  ///
  /// Mutation (drop the normalization): `+inf` gives an infinite mean and
  /// peak; `2.0` gives mean `1.0667` and peak `2.0`. Red.
  #[test]
  fn normal_close_clamps_above_range_probabilities() {
    for (invalid, name) in HIGH_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());
      let mut probabilities = vec![0.6, invalid, 0.6];
      probabilities.extend([0.0; 5]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].start_sample(), 0, "{name}");
      assert_eq!(runs[0].end_sample(), 3 * HOP, "{name}");
      assert_close(
        runs[0].mean_probability(),
        2.2 / 3.0,
        &format!("{name} mean"),
      );
      assert_close(runs[0].peak_probability(), 1.0, &format!("{name} peak"));
    }
  }

  /// Close path 1, below-range input. `NaN` / `-inf` / `-1.0` all
  /// canonicalize to `0.0`, so the malformed frame opens a one-frame gap
  /// that the next frame bridges: three observations, mean `1.2 / 3`,
  /// peak `0.6`.
  ///
  /// Mutation: `NaN` gives a `NaN` mean, `-inf` an infinite one, and
  /// `-1.0` mean `0.0667`. Red.
  #[test]
  fn normal_close_maps_below_range_probabilities_to_silence() {
    for (invalid, name) in LOW_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());
      let mut probabilities = vec![0.6, invalid, 0.6];
      probabilities.extend([0.0; 5]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].start_sample(), 0, "{name}");
      assert_eq!(runs[0].end_sample(), 3 * HOP, "{name}");
      assert_close(runs[0].mean_probability(), 0.4, &format!("{name} mean"));
      assert_close(runs[0].peak_probability(), 0.6, &format!("{name} peak"));
    }
  }

  /// Close path 2 (a bridged gap), below-range input inside the gap. The
  /// bridged frames are part of the run, so the malformed one reaches the
  /// aggregate as `0.0`: seven observations — `0.6, 0.6`, three bridged
  /// zeros, `0.6, 0.6` — mean `2.4 / 7`, peak `0.6`.
  ///
  /// Mutation: `NaN` / `-inf` poison the mean; `-1.0` gives `0.2`. Red.
  #[test]
  fn bridged_gap_maps_below_range_probabilities_to_silence() {
    for (invalid, name) in LOW_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());
      let mut probabilities = vec![0.6, 0.6, 0.0, invalid, 0.0, 0.6, 0.6];
      probabilities.extend([0.0; 5]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].start_sample(), 0, "{name}");
      assert_eq!(runs[0].end_sample(), 7 * HOP, "{name}");
      assert_close(
        runs[0].mean_probability(),
        2.4 / 7.0,
        &format!("{name} mean"),
      );
      assert_close(runs[0].peak_probability(), 0.6, &format!("{name} peak"));
    }
  }

  /// Close path 2, above-range input on the frame that BRIDGES the gap.
  /// The malformed frame is what re-opens the run, and it is aggregated as
  /// `1.0`: eight observations, mean `3.4 / 8`, peak `1.0`.
  ///
  /// Mutation: `+inf` poisons mean and peak; `2.0` gives mean `0.55` and
  /// peak `2.0`. Red.
  #[test]
  fn bridged_gap_clamps_the_bridging_frame() {
    for (invalid, name) in HIGH_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());
      let mut probabilities = vec![0.6, 0.6, 0.0, invalid, 0.0, 0.0, 0.6, 0.6];
      probabilities.extend([0.0; 5]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].start_sample(), 0, "{name}");
      assert_eq!(runs[0].end_sample(), 8 * HOP, "{name}");
      assert_close(runs[0].mean_probability(), 0.425, &format!("{name} mean"));
      assert_close(runs[0].peak_probability(), 1.0, &format!("{name} peak"));
    }
  }

  /// Close path 3 (a `max_run_duration` force-split), above-range input in
  /// the split-off run. Five observations before the split, mean
  /// `3.4 / 5`, peak `1.0`; the continuation is clean.
  ///
  /// Mutation: `+inf` poisons the split run; `2.0` gives mean `0.88` and
  /// peak `2.0`. Red.
  #[test]
  fn force_split_clamps_above_range_probabilities() {
    for (invalid, name) in HIGH_INVALID {
      let options = open_options().with_max_run_duration(Duration::from_millis(160));
      let mut segmenter = RunSegmenter::new(options);
      let mut probabilities = vec![0.6, invalid, 0.6, 0.6, 0.6];
      probabilities.extend([0.8; 3]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 2, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].end_sample(), 5 * HOP, "{name}");
      assert_close(
        runs[0].mean_probability(),
        0.68,
        &format!("{name} split mean"),
      );
      assert_close(
        runs[0].peak_probability(),
        1.0,
        &format!("{name} split peak"),
      );
      assert_eq!(runs[1].start_sample(), 5 * HOP, "{name}");
      assert_close(
        runs[1].mean_probability(),
        0.8,
        &format!("{name} continuation mean"),
      );
    }
  }

  /// Close path 3, below-range input bridged into the split-off run: five
  /// observations, mean `2.4 / 5`, peak `0.6`.
  ///
  /// Mutation: `NaN` / `-inf` poison the split run's mean; `-1.0` gives
  /// `0.28`. Red.
  #[test]
  fn force_split_maps_below_range_probabilities_to_silence() {
    for (invalid, name) in LOW_INVALID {
      let options = open_options().with_max_run_duration(Duration::from_millis(160));
      let mut segmenter = RunSegmenter::new(options);
      let mut probabilities = vec![0.6, invalid, 0.6, 0.6, 0.6];
      probabilities.extend([0.8; 3]);

      let runs = collect(&mut segmenter, &probabilities);

      assert_eq!(runs.len(), 2, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].end_sample(), 5 * HOP, "{name}");
      assert_close(
        runs[0].mean_probability(),
        0.48,
        &format!("{name} split mean"),
      );
      assert_close(
        runs[0].peak_probability(),
        0.6,
        &format!("{name} split peak"),
      );
      assert_eq!(runs[1].start_sample(), 5 * HOP, "{name}");
      assert_close(
        runs[1].mean_probability(),
        0.8,
        &format!("{name} continuation mean"),
      );
    }
  }

  /// Close path 4 (`finish` flushes the trailing run), below-range input
  /// in the unclosed trailing gap — which `take_trailing` folds in, so the
  /// malformed frame reaches the aggregate as `0.0`: mean `1.2 / 3`.
  ///
  /// Mutation: `NaN` / `-inf` poison the mean; `-1.0` gives `0.0667`. Red.
  #[test]
  fn finish_maps_below_range_probabilities_to_silence() {
    for (invalid, name) in LOW_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());

      let runs = collect(&mut segmenter, &[0.6, 0.6, invalid]);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].end_sample(), 3 * HOP, "{name}");
      assert_close(runs[0].mean_probability(), 0.4, &format!("{name} mean"));
      assert_close(runs[0].peak_probability(), 0.6, &format!("{name} peak"));
    }
  }

  /// Close path 4, above-range input inside the trailing run.
  ///
  /// Mutation: `+inf` poisons mean and peak; `2.0` gives mean `1.0667`
  /// and peak `2.0`. Red.
  #[test]
  fn finish_clamps_above_range_probabilities() {
    for (invalid, name) in HIGH_INVALID {
      let mut segmenter = RunSegmenter::new(open_options());

      let runs = collect(&mut segmenter, &[0.6, invalid, 0.6]);

      assert_eq!(runs.len(), 1, "{name}");
      assert_all_in_range(&runs, name);
      assert_eq!(runs[0].end_sample(), 3 * HOP, "{name}");
      assert_close(
        runs[0].mean_probability(),
        2.2 / 3.0,
        &format!("{name} mean"),
      );
      assert_close(runs[0].peak_probability(), 1.0, &format!("{name} peak"));
    }
  }

  fn boundaries(options: RunOptions, probabilities: &[f32]) -> Vec<(u64, u64)> {
    let mut segmenter = RunSegmenter::new(options);
    collect(&mut segmenter, probabilities)
      .iter()
      .map(|run| (run.start_sample(), run.end_sample()))
      .collect()
  }

  /// The `NaN` -> `0.0` mapping is behaviour-preserving for the state
  /// machine: both effective thresholds are strictly positive in every
  /// configuration `RunOptions` can hold, and a `NaN` compared against a
  /// positive threshold is false, as is `0.0` — so the identical branch
  /// is taken on every comparison in `push_probability` and
  /// `split_at_max_duration`.
  ///
  /// This test is the evidence, and it is the one test here that must stay
  /// GREEN with the normalization removed: it asserts that substituting
  /// `NaN` for a `0.0` frame moves no boundary, mid-run (bridged), inside
  /// a closing gap, and on the frame a force-split decides its
  /// continuation from.
  #[test]
  fn nan_frames_do_not_move_segment_boundaries() {
    // A NaN mid-run (bridged), plus a NaN inside the gap that closes the
    // first run.
    let mut with_nan = vec![0.6, 0.6, f32::NAN, 0.6, 0.6];
    with_nan.extend([0.0, f32::NAN, 0.0, 0.0, 0.0]);
    with_nan.extend([0.6, 0.6]);
    with_nan.extend([0.0; 5]);
    let with_zero: Vec<f32> = with_nan
      .iter()
      .map(|p| if p.is_nan() { 0.0 } else { *p })
      .collect();

    let nan_boundaries = boundaries(open_options(), &with_nan);
    assert_eq!(
      nan_boundaries,
      boundaries(open_options(), &with_zero),
      "a NaN frame must segment exactly like the 0.0 frame it stands for"
    );
    assert_eq!(
      nan_boundaries,
      vec![(0, 5 * HOP), (10 * HOP, 12 * HOP)],
      "and both must match the pinned boundaries"
    );

    // A NaN on the frame that a force-split reads to decide whether to
    // open a continuation run (`probability >= start_threshold`).
    let split_options = open_options().with_max_run_duration(Duration::from_millis(160));
    let mut with_nan = vec![0.6; 5];
    with_nan.push(f32::NAN);
    with_nan.extend([0.6; 3]);
    let with_zero: Vec<f32> = with_nan
      .iter()
      .map(|p| if p.is_nan() { 0.0 } else { *p })
      .collect();

    let nan_boundaries = boundaries(split_options.clone(), &with_nan);
    assert_eq!(
      nan_boundaries,
      boundaries(split_options, &with_zero),
      "a NaN at the split decision frame must segment exactly like 0.0"
    );
    assert_eq!(
      nan_boundaries,
      vec![(0, 5 * HOP), (6 * HOP, 9 * HOP)],
      "and both must match the pinned boundaries"
    );
  }

  /// A malformed frame must not be able to hold a run open. The frame
  /// that satisfies `probability >= start_threshold` CLEARS the tentative
  /// gap and restarts it on the same frame, so if a frame canonicalized
  /// to `0.0` could satisfy that comparison, repeated malformed frames
  /// would pin the gap age at zero and extend the run for as long as they
  /// kept arriving.
  ///
  /// The threshold floor forecloses it: `0.0` is below every permitted
  /// threshold, so each malformed frame lands IN the gap and the gap
  /// grows until it closes the run.
  ///
  /// Mutation (drop the floor, so `with_start_threshold(0.0)` stores
  /// `0.0`): the gap resets on every frame, no run ever closes while
  /// frames arrive, and the stream ends as one run spanning everything
  /// pushed. Red.
  #[test]
  fn repeated_malformed_frames_cannot_hold_a_run_open() {
    for (invalid, name) in LOW_INVALID {
      // The most permissive configuration the floor permits.
      let mut segmenter = RunSegmenter::new(open_options().with_start_threshold(0.0));

      let mut probabilities = vec![0.6, 0.6];
      probabilities.extend([invalid; 64]);

      let mut closed = None;
      for (index, probability) in probabilities.iter().enumerate() {
        if let Some(run) = segmenter.push_probability(*probability) {
          closed = Some((index, run));
          break;
        }
      }

      let (index, run) = closed.unwrap_or_else(|| {
        panic!("{name}: the run must close while the malformed frames are still arriving")
      });
      assert_eq!(
        index, 6,
        "{name}: closes on the frame the gap outgrows min_gap_duration"
      );
      assert_eq!(run.start_sample(), 0, "{name}");
      assert_eq!(
        run.end_sample(),
        2 * HOP,
        "{name}: the run ends at the first malformed frame"
      );
      assert_in_range(&run, name);
      assert!(!segmenter.is_active(), "{name}: no run is left open");

      for probability in &probabilities[index + 1..] {
        assert!(
          segmenter.push_probability(*probability).is_none(),
          "{name}: a malformed frame must not open a run"
        );
      }
      assert!(segmenter.finish().is_none(), "{name}: nothing trails");
    }
  }

  /// Canonicalization moves neither a boundary nor an aggregate at the
  /// LOWEST thresholds `RunOptions` permits — the configuration that
  /// would be the exception if a threshold could reach `0.0`. Every input
  /// canonicalizing to `0.0` segments exactly like the `0.0` frame it
  /// stands for.
  ///
  /// Mutations: (a) drop the threshold floor -> `0.0` is stored, the
  /// malformed frames satisfy the start comparison, and the stream
  /// collapses to a single run spanning everything pushed; (b) drop the
  /// canonicalization -> `NaN` and `-inf` poison the first run's mean and
  /// `-1.0` drags it to `0.28`. Both red.
  #[test]
  fn canonicalization_is_behaviour_preserving_at_the_lowest_thresholds() {
    for (invalid, name) in LOW_INVALID {
      // `with_*_threshold(0.0)` stores `RunOptions::MIN_THRESHOLD`.
      let options = || {
        open_options()
          .with_start_threshold(0.0)
          .with_end_threshold(0.0)
      };

      let mut with_zero = vec![0.6, 0.6, 0.0, 0.6, 0.6];
      with_zero.extend([0.0; 5]);
      with_zero.extend([0.6, 0.6]);
      with_zero.extend([0.0; 5]);
      let mut with_invalid = with_zero.clone();
      with_invalid[2] = invalid;
      with_invalid[6] = invalid;

      let invalid_boundaries = boundaries(options(), &with_invalid);
      assert_eq!(
        invalid_boundaries,
        boundaries(options(), &with_zero),
        "{name}: must segment exactly like the 0.0 frames it stands for"
      );
      assert_eq!(
        invalid_boundaries,
        vec![(0, 5 * HOP), (10 * HOP, 12 * HOP)],
        "{name}: and both must match the pinned boundaries"
      );

      let mut segmenter = RunSegmenter::new(options());
      let runs = collect(&mut segmenter, &with_invalid);
      assert_all_in_range(&runs, name);
      assert_close(
        runs[0].mean_probability(),
        0.48,
        &format!("{name} first mean"),
      );
      assert_close(
        runs[0].peak_probability(),
        0.6,
        &format!("{name} first peak"),
      );
      assert_close(
        runs[1].mean_probability(),
        0.6,
        &format!("{name} second mean"),
      );
    }
  }

  /// The same at the force-split continuation decision, which re-reads
  /// the current frame through `probability >= start_threshold`. A frame
  /// canonicalized to `0.0` cannot satisfy it, so the continuation opens
  /// at the next genuinely above-threshold frame — exactly where the raw
  /// value would have opened it.
  ///
  /// Mutation (drop the threshold floor): at a stored `0.0` start
  /// threshold the malformed frame satisfies the comparison, so the
  /// continuation opens one frame EARLIER (`5 * HOP` rather than
  /// `6 * HOP`) and carries the canonicalized frame in its mean
  /// (`0.45` rather than `0.6`). Red.
  #[test]
  fn canonicalization_moves_no_force_split_boundary_at_the_lowest_thresholds() {
    for (invalid, name) in LOW_INVALID {
      let options = || {
        open_options()
          .with_start_threshold(0.0)
          .with_max_run_duration(Duration::from_millis(160))
      };

      let mut with_zero = vec![0.6; 5];
      with_zero.push(0.0);
      with_zero.extend([0.6; 3]);
      let mut with_invalid = with_zero.clone();
      with_invalid[5] = invalid;

      let invalid_boundaries = boundaries(options(), &with_invalid);
      assert_eq!(
        invalid_boundaries,
        boundaries(options(), &with_zero),
        "{name}: the split decision must read the canonical value as 0.0"
      );
      assert_eq!(
        invalid_boundaries,
        vec![(0, 5 * HOP), (6 * HOP, 9 * HOP)],
        "{name}: and both must match the pinned boundaries"
      );

      let mut segmenter = RunSegmenter::new(options());
      let runs = collect(&mut segmenter, &with_invalid);
      assert_all_in_range(&runs, name);
      assert_close(
        runs[1].mean_probability(),
        0.6,
        &format!("{name} continuation mean"),
      );
    }
  }
}