mediation 0.3.0

Telecom usage mediation as a pure Rust library: normalise, deduplicate, correlate and reconcile CDRs from mobile and fixed-line networks, with a conservation law that proves no record was lost. Zero I/O, no clock, integer quantities.
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
//! The conservation law: every ingested record reaches exactly one outcome.
//!
//! This is the difference between a mediation layer and a data pipeline. A
//! pipeline moves records; a mediation layer can **prove** it did not lose any.
//!
//! ```text
//! carried_in + ingested == billable + suppressed + duplicate + rejected + held
//! ```
//!
//! [`BatchReconciliation::balances`] is that equation, and a batch that does not
//! satisfy it is not releasable — its completeness is unknown, and billing over
//! unknown completeness invoices an amount nobody can prove is right.
//!
//! # The law spans batches, not just one
//!
//! A [`Disposition::Held`] record comes back. If the batch that re-presents it
//! counts it as newly ingested, the two batches together report it twice; if
//! neither counts it, it vanishes. Both are the conservation law failing at a
//! boundary, which is exactly where nobody looks.
//!
//! So a re-presented record is counted through
//! [`observe_carried`](BatchReconciliation::observe_carried) rather than
//! [`observe`](BatchReconciliation::observe), and [`PeriodLedger`] checks that
//! each batch's `carried_in` equals its predecessor's `held`. Chained that way
//! the law telescopes: across a whole period, everything ingested reached a
//! final outcome or is still explicitly outstanding.
//!
//! ```rust
//! use mediation::{BatchReconciliation, Disposition, SuppressReason};
//!
//! let mut counts = BatchReconciliation::EMPTY;
//! counts.observe(&Disposition::Billable);
//! counts.observe(&Disposition::Billable);
//! counts.observe(&Disposition::Suppressed(SuppressReason::TestIdentity));
//!
//! assert_eq!(counts.ingested(), 3);
//! assert_eq!(counts.released(), 2);
//! assert!(counts.balances());
//! ```
//!
//! # Held is not a sixth kind of failure
//!
//! Four outcomes are final. [`Disposition::Held`] is the one that may return —
//! a partial record whose successor has not arrived, a leg whose counterpart is
//! still in flight, an identity awaiting reference data. It still *counts*, so
//! the batch balances, but it also says the batch is not finished, which is
//! exactly the information a period close needs.
//!
//! # Merging
//!
//! [`BatchReconciliation::checked_add`] exists so a caller can process disjoint
//! key ranges in parallel and recombine. Because the crate reads no clock and
//! performs no I/O, that parallelisation is trivially safe, and the
//! conservation law makes the recombination checkable rather than hopeful.
//!
//! Counts are also the raw material for revenue-assurance reporting, which is
//! why they are a public type rather than a log line.

use std::fmt;

use crate::dedup::{NotKeyable, RecordKey};
use crate::lateness::Lateness;
use crate::party::UnattributableReason;

// ── Reasons ──────────────────────────────────────────────────────────────────

/// Why a record was deliberately not billed.
///
/// Suppression is a decision, not a failure: the record was understood and a
/// policy said not to charge for it. Distinguishing it from rejection is what
/// keeps a revenue-assurance report readable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SuppressReason {
    /// A test SIM, a probe, or an internal test identity.
    TestIdentity,
    /// Zero-rated by policy — sponsored data, an emergency call, a free tier.
    ZeroRated,
    /// Traffic internal to the operator, netted off before billing.
    InternalTraffic,
    /// The record described no usage at all.
    ZeroUsage,
    /// An operator policy not covered by the cases above.
    OperatorPolicy,
}

/// Why a record can never be billed.
///
/// Terminal. A rejected record will not be retried, so anything that *could*
/// succeed later belongs in [`HoldReason`] instead.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RejectReason {
    /// Past the inclusion deadline — see [`crate::lateness`].
    BeyondDeadline,
    /// Timestamped ahead of the evaluation instant: clock skew or corruption.
    FutureTimestamp,
    /// No charged party could be determined.
    Unattributable(UnattributableReason),
    /// The record carries no natural key, so it cannot be deduplicated.
    ///
    /// Billing an undeduplicatable record risks charging for it twice, which is
    /// the more expensive of the two errors.
    NotKeyable(NotKeyable),
    /// The record failed structural validation.
    Malformed,
}

/// Why a record is waiting rather than finished.
///
/// The only non-final outcome. Each variant names something that may still
/// arrive, which is what distinguishes it from a rejection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HoldReason {
    /// A partial record whose siblings have not all arrived.
    AwaitingPartial,
    /// A leg whose counterpart has not arrived.
    AwaitingCorrelation,
    /// An identity that a reference-data load could still resolve.
    AwaitingReferenceData,
    /// The record's source has an unfilled sequence gap.
    ///
    /// Completeness is unknown for the whole source, so releasing any of its
    /// records would invoice an unprovable amount.
    SequenceGap,
}

macro_rules! coded_enum {
    ($ty:ty, $all:expr, $($variant:pat => $code:literal),+ $(,)?) => {
        impl $ty {
            /// Every variant, for exhaustive iteration in tests and mappings.
            pub const ALL: &'static [Self] = $all;

            /// The canonical string form.
            #[must_use]
            pub const fn as_str(self) -> &'static str {
                match self {
                    $($variant => $code,)+
                }
            }
        }

        impl fmt::Display for $ty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(self.as_str())
            }
        }

        impl std::str::FromStr for $ty {
            type Err = crate::error::ParseError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                let t = s.trim().to_ascii_uppercase();
                Self::ALL
                    .iter()
                    .find(|v| v.as_str() == t)
                    .copied()
                    .ok_or_else(|| {
                        crate::error::ParseError::format(
                            stringify!($ty),
                            s,
                            concat!("one of the ", stringify!($ty), " codes"),
                        )
                    })
            }
        }
    };
}

coded_enum!(
    SuppressReason,
    &[
        Self::TestIdentity,
        Self::ZeroRated,
        Self::InternalTraffic,
        Self::ZeroUsage,
        Self::OperatorPolicy,
    ],
    Self::TestIdentity => "TEST_IDENTITY",
    Self::ZeroRated => "ZERO_RATED",
    Self::InternalTraffic => "INTERNAL_TRAFFIC",
    Self::ZeroUsage => "ZERO_USAGE",
    Self::OperatorPolicy => "OPERATOR_POLICY",
);

coded_enum!(
    HoldReason,
    &[
        Self::AwaitingPartial,
        Self::AwaitingCorrelation,
        Self::AwaitingReferenceData,
        Self::SequenceGap,
    ],
    Self::AwaitingPartial => "AWAITING_PARTIAL",
    Self::AwaitingCorrelation => "AWAITING_CORRELATION",
    Self::AwaitingReferenceData => "AWAITING_REFERENCE_DATA",
    Self::SequenceGap => "SEQUENCE_GAP",
);

impl RejectReason {
    /// The rejection implied by a lateness verdict, if it implies one.
    ///
    /// The bridge between [`crate::lateness`] and this module, so a caller
    /// cannot map [`Lateness::Late`] onto a rejection by mistake — a late
    /// record is still billable.
    #[must_use]
    pub const fn from_lateness(lateness: Lateness) -> Option<Self> {
        match lateness {
            Lateness::BeyondDeadline(_) => Some(Self::BeyondDeadline),
            Lateness::Future(_) => Some(Self::FutureTimestamp),
            Lateness::OnTime | Lateness::Late(_) => None,
        }
    }

    /// A short code naming the rejection.
    #[must_use]
    pub const fn kind(self) -> &'static str {
        match self {
            Self::BeyondDeadline => "BEYOND_DEADLINE",
            Self::FutureTimestamp => "FUTURE_TIMESTAMP",
            Self::Unattributable(_) => "UNATTRIBUTABLE",
            Self::NotKeyable(_) => "NOT_KEYABLE",
            Self::Malformed => "MALFORMED",
        }
    }
}

impl fmt::Display for RejectReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unattributable(reason) => write!(f, "UNATTRIBUTABLE:{reason}"),
            Self::NotKeyable(reason) => write!(f, "NOT_KEYABLE:{reason}"),
            other => f.write_str(other.kind()),
        }
    }
}

// ── Outcome ──────────────────────────────────────────────────────────────────

/// One of the five terms on the right of the conservation law.
///
/// A [`Disposition`] with its reason stripped away. The reason explains *why* a
/// record ended where it did; the outcome is which counter it lands in, and it
/// is the only part a count can be moved between.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Outcome {
    /// Released downstream to be rated.
    Billable,
    /// Understood, and deliberately not billed.
    Suppressed,
    /// Already seen; the surviving copy carries the usage.
    Duplicate,
    /// Cannot be billed, and will not be retried.
    Rejected,
    /// Not finished — something that may still arrive is missing.
    Held,
}

impl Outcome {
    /// Every outcome, in the order the conservation law writes them.
    pub const ALL: &'static [Self] = &[
        Self::Billable,
        Self::Suppressed,
        Self::Duplicate,
        Self::Rejected,
        Self::Held,
    ];

    /// The canonical code of each variant, in [`ALL`](Self::ALL) order.
    pub const CODES: &'static [&'static str] =
        &["BILLABLE", "SUPPRESSED", "DUPLICATE", "REJECTED", "HELD"];

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Billable => "BILLABLE",
            Self::Suppressed => "SUPPRESSED",
            Self::Duplicate => "DUPLICATE",
            Self::Rejected => "REJECTED",
            Self::Held => "HELD",
        }
    }

    /// `true` for the one outcome that reaches a rating engine.
    #[must_use]
    pub const fn is_released(self) -> bool {
        matches!(self, Self::Billable)
    }

    /// `true` for the one outcome that is not final.
    #[must_use]
    pub const fn may_return(self) -> bool {
        matches!(self, Self::Held)
    }
}

impl fmt::Display for Outcome {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Outcome {
    type Err = crate::error::ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .iter()
            .find(|o| o.as_str().eq_ignore_ascii_case(s.trim()))
            .copied()
            .ok_or_else(|| crate::error::ParseError::one_of("Outcome", s, Self::CODES))
    }
}

// ── Disposition ──────────────────────────────────────────────────────────────

/// What became of one ingested record.
///
/// Exhaustive, and deliberately not `#[non_exhaustive]`: a consumer mapping
/// these outcomes onto their own storage codes should get a compile error when
/// a new one appears, not a silent wildcard that files a rejected record as
/// billable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Disposition {
    /// Released downstream to be rated.
    Billable,
    /// Understood, and deliberately not billed.
    Suppressed(SuppressReason),
    /// Already seen; the surviving copy carries the usage.
    Duplicate(RecordKey),
    /// Cannot be billed, and will not be retried.
    Rejected(RejectReason),
    /// Not finished — something that may still arrive is missing.
    Held(HoldReason),
}

impl Disposition {
    /// `true` for the one outcome that reaches a rating engine.
    #[must_use]
    pub const fn is_released(&self) -> bool {
        matches!(self, Self::Billable)
    }

    /// `true` when the record may yet reach a different outcome.
    ///
    /// Only [`Held`](Self::Held). The other four are final, which is what makes
    /// the reconciliation of a closed period meaningful.
    #[must_use]
    pub const fn may_return(&self) -> bool {
        matches!(self, Self::Held(_))
    }

    /// `true` when an operator should look at this record.
    #[must_use]
    pub const fn is_finding(&self) -> bool {
        matches!(self, Self::Rejected(_) | Self::Held(_))
    }

    /// Which term of the conservation law this record lands in.
    ///
    /// The disposition without its reason — what a counter can be keyed on and
    /// what [`BatchReconciliation::reclassify`] moves between.
    #[must_use]
    pub const fn outcome(&self) -> Outcome {
        match self {
            Self::Billable => Outcome::Billable,
            Self::Suppressed(_) => Outcome::Suppressed,
            Self::Duplicate(_) => Outcome::Duplicate,
            Self::Rejected(_) => Outcome::Rejected,
            Self::Held(_) => Outcome::Held,
        }
    }

    /// A short code naming the outcome, for grouping and reporting.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        self.outcome().as_str()
    }
}

impl fmt::Display for Disposition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Billable => f.write_str("BILLABLE"),
            Self::Suppressed(reason) => write!(f, "SUPPRESSED:{reason}"),
            Self::Duplicate(key) => write!(f, "DUPLICATE:{key}"),
            Self::Rejected(reason) => write!(f, "REJECTED:{reason}"),
            Self::Held(reason) => write!(f, "HELD:{reason}"),
        }
    }
}

// ── BatchReconciliation ──────────────────────────────────────────────────────

/// The counting law over one batch.
///
/// Every counter is `u64` and every arithmetic operation that could plausibly
/// overflow is checked: a wrapped count would make an unbalanced batch appear
/// balanced, which is the one failure this type exists to make impossible. The
/// two exceptions are the `+= 1` in [`observe`](Self::observe) and
/// [`observe_carried`](Self::observe_carried), which would need eighteen
/// quintillion records in one batch to wrap and cannot be reached by any input
/// a machine could hold.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BatchReconciliation {
    carried_in: u64,
    ingested: u64,
    billable: u64,
    suppressed: u64,
    duplicate: u64,
    rejected: u64,
    held: u64,
}

impl BatchReconciliation {
    /// Nothing counted yet.
    pub const EMPTY: Self = Self {
        carried_in: 0,
        ingested: 0,
        billable: 0,
        suppressed: 0,
        duplicate: 0,
        rejected: 0,
        held: 0,
    };

    /// Count a **newly arrived** record's outcome.
    ///
    /// Increments `ingested` and exactly one of the outcome counters, which is
    /// what makes [`balances`](Self::balances) hold by construction for any
    /// reconciliation built solely through this method and
    /// [`observe_carried`](Self::observe_carried). A batch that fails the check
    /// was therefore assembled some other way — which is precisely what the
    /// check is for.
    pub const fn observe(&mut self, disposition: &Disposition) {
        self.ingested += 1;
        self.count_outcome(disposition);
    }

    /// Count a record **held by an earlier batch** and re-presented here.
    ///
    /// Increments `carried_in` rather than `ingested`, so the record is not
    /// counted as arriving twice. Getting this wrong is the one way the
    /// conservation law fails at a batch boundary, which is the place nobody
    /// audits.
    pub const fn observe_carried(&mut self, disposition: &Disposition) {
        self.carried_in += 1;
        self.count_outcome(disposition);
    }

    /// Count `count` newly arrived records that all reached `outcome`.
    ///
    /// The bulk form of [`observe`](Self::observe), for a caller who already
    /// holds counts rather than records — a partner's return, a summary row
    /// loaded from storage. `None` on overflow.
    #[must_use]
    pub const fn observe_many(mut self, outcome: Outcome, count: u64) -> Option<Self> {
        self.ingested = match self.ingested.checked_add(count) {
            Some(v) => v,
            None => return None,
        };
        match self.add_outcome(outcome, count) {
            Some(()) => Some(self),
            None => None,
        }
    }

    /// Count `count` records **carried in** that all reached `outcome`.
    ///
    /// `None` on overflow.
    #[must_use]
    pub const fn observe_carried_many(mut self, outcome: Outcome, count: u64) -> Option<Self> {
        self.carried_in = match self.carried_in.checked_add(count) {
            Some(v) => v,
            None => return None,
        };
        match self.add_outcome(outcome, count) {
            Some(()) => Some(self),
            None => None,
        }
    }

    /// How many records reached `outcome`.
    #[must_use]
    pub const fn count_of(&self, outcome: Outcome) -> u64 {
        match outcome {
            Outcome::Billable => self.billable,
            Outcome::Suppressed => self.suppressed,
            Outcome::Duplicate => self.duplicate,
            Outcome::Rejected => self.rejected,
            Outcome::Held => self.held,
        }
    }

    /// Move `count` records from one outcome to another.
    ///
    /// The only supported way to revise a batch after the fact, and the reason
    /// it exists rather than a set of mutable counters: moving records between
    /// outcomes is precisely the operation the conservation law polices, so it
    /// happens through one method that cannot change the totals. `presented`
    /// is unchanged and the batch still balances afterwards.
    ///
    /// `None` when fewer than `count` records are counted under `from`, which
    /// means the caller and the batch disagree about what the batch held.
    ///
    /// ```rust
    /// use mediation::{BatchReconciliation, Disposition, Outcome};
    ///
    /// let mut batch = BatchReconciliation::EMPTY;
    /// batch.observe(&Disposition::Billable);
    /// batch.observe(&Disposition::Billable);
    ///
    /// // A partner returned one of the two.
    /// let revised = batch.reclassify(Outcome::Billable, Outcome::Rejected, 1).unwrap();
    /// assert_eq!(revised.released(), 1);
    /// assert_eq!(revised.rejected(), 1);
    /// assert_eq!(revised.presented(), batch.presented());
    /// assert!(revised.balances());
    /// ```
    #[must_use]
    pub const fn reclassify(mut self, from: Outcome, to: Outcome, count: u64) -> Option<Self> {
        if self.count_of(from) < count {
            return None;
        }
        self.take_outcome(from, count);
        match self.add_outcome(to, count) {
            Some(()) => Some(self),
            None => None,
        }
    }

    const fn add_outcome(&mut self, outcome: Outcome, count: u64) -> Option<()> {
        let slot = match outcome {
            Outcome::Billable => &mut self.billable,
            Outcome::Suppressed => &mut self.suppressed,
            Outcome::Duplicate => &mut self.duplicate,
            Outcome::Rejected => &mut self.rejected,
            Outcome::Held => &mut self.held,
        };
        match slot.checked_add(count) {
            Some(v) => {
                *slot = v;
                Some(())
            }
            None => None,
        }
    }

    /// Subtract from a counter the caller has already checked is large enough.
    const fn take_outcome(&mut self, outcome: Outcome, count: u64) {
        let slot = match outcome {
            Outcome::Billable => &mut self.billable,
            Outcome::Suppressed => &mut self.suppressed,
            Outcome::Duplicate => &mut self.duplicate,
            Outcome::Rejected => &mut self.rejected,
            Outcome::Held => &mut self.held,
        };
        *slot -= count;
    }

    const fn count_outcome(&mut self, disposition: &Disposition) {
        // Infallible by construction: one record adds one to one counter, and a
        // batch that could overflow a `u64` of records would have had to ingest
        // 18 quintillion of them.
        let _ = self.add_outcome(disposition.outcome(), 1);
    }

    /// How many records arrived for the first time.
    #[must_use]
    pub const fn ingested(&self) -> u64 {
        self.ingested
    }

    /// How many records an earlier batch held and this one re-presented.
    #[must_use]
    pub const fn carried_in(&self) -> u64 {
        self.carried_in
    }

    /// Everything this batch had to account for: `carried_in + ingested`.
    ///
    /// `None` on overflow, which is itself an imbalance.
    #[must_use]
    pub const fn presented(&self) -> Option<u64> {
        self.carried_in.checked_add(self.ingested)
    }

    /// How many were released downstream.
    #[must_use]
    pub const fn released(&self) -> u64 {
        self.billable
    }

    /// How many were understood and deliberately not billed.
    #[must_use]
    pub const fn suppressed(&self) -> u64 {
        self.suppressed
    }

    /// How many were recognised as retransmissions.
    #[must_use]
    pub const fn duplicate(&self) -> u64 {
        self.duplicate
    }

    /// How many can never be billed.
    #[must_use]
    pub const fn rejected(&self) -> u64 {
        self.rejected
    }

    /// How many are waiting for something that may still arrive.
    #[must_use]
    pub const fn held(&self) -> u64 {
        self.held
    }

    /// Whether every ingested record reached exactly one outcome.
    #[must_use]
    pub const fn balances(&self) -> bool {
        // Written as nested matches rather than a combinator chain so the whole
        // check stays `const`. An overflowing sum returns `false`: it cannot
        // equal an ingested count that did not itself overflow, so an
        // unbalanced batch never reads as balanced by luck.
        macro_rules! accumulate {
            ($acc:expr, $field:ident) => {
                match $acc.checked_add(self.$field) {
                    Some(value) => value,
                    None => return false,
                }
            };
        }
        let total = self.billable;
        let total = accumulate!(total, suppressed);
        let total = accumulate!(total, duplicate);
        let total = accumulate!(total, rejected);
        let total = accumulate!(total, held);
        match self.presented() {
            Some(presented) => total == presented,
            None => false,
        }
    }

    /// `true` when the batch balances and nothing is still waiting.
    ///
    /// The precondition for closing a period over this batch: a held record may
    /// still become billable, so a period closed while records are held will
    /// need reopening.
    #[must_use]
    pub const fn is_final(&self) -> bool {
        self.balances() && self.held == 0
    }

    /// Combine two reconciliations, `None` on overflow.
    ///
    /// For recombining work done over disjoint key ranges.
    #[must_use]
    pub const fn checked_add(self, other: Self) -> Option<Self> {
        macro_rules! add {
            ($field:ident) => {
                match self.$field.checked_add(other.$field) {
                    Some(v) => v,
                    None => return None,
                }
            };
        }
        Some(Self {
            carried_in: add!(carried_in),
            ingested: add!(ingested),
            billable: add!(billable),
            suppressed: add!(suppressed),
            duplicate: add!(duplicate),
            rejected: add!(rejected),
            held: add!(held),
        })
    }
}

impl fmt::Display for BatchReconciliation {
    /// One line, in the order of the conservation law, so an imbalance is
    /// visible by inspection.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "carried_in={} + ingested={} = billable={} + suppressed={} + duplicate={} + rejected={} + held={} [{}]",
            self.carried_in,
            self.ingested,
            self.billable,
            self.suppressed,
            self.duplicate,
            self.rejected,
            self.held,
            if self.balances() {
                "balanced"
            } else {
                "UNBALANCED"
            }
        )
    }
}

impl FromIterator<Disposition> for BatchReconciliation {
    fn from_iter<I: IntoIterator<Item = Disposition>>(iter: I) -> Self {
        let mut counts = Self::EMPTY;
        for disposition in iter {
            counts.observe(&disposition);
        }
        counts
    }
}

impl<'a> FromIterator<&'a Disposition> for BatchReconciliation {
    fn from_iter<I: IntoIterator<Item = &'a Disposition>>(iter: I) -> Self {
        let mut counts = Self::EMPTY;
        for disposition in iter {
            counts.observe(disposition);
        }
        counts
    }
}

// ── PeriodLedger ─────────────────────────────────────────────────────────────

/// Why a chain of batches does not add up.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChainBreak {
    /// A batch does not satisfy the conservation law on its own.
    Unbalanced {
        /// Which batch, counting from zero.
        batch: usize,
    },
    /// The period's running totals overflowed when this batch was added.
    ///
    /// Distinct from [`Unbalanced`](Self::Unbalanced): every batch may be
    /// internally consistent and the *period* still exceed what a `u64` can
    /// count. Reporting it as an imbalance would send an operator looking for a
    /// lost record that does not exist.
    TotalsOverflow {
        /// Which batch, counting from zero.
        batch: usize,
    },
    /// A batch's `carried_in` does not match what was outstanding before it.
    ///
    /// The failure mode this whole type exists to catch: held records counted
    /// twice across the boundary, or dropped at it.
    CarryMismatch {
        /// Which batch, counting from zero.
        batch: usize,
        /// What the previous batch left outstanding.
        expected: u64,
        /// What this batch claimed to carry in.
        found: u64,
    },
}

impl fmt::Display for ChainBreak {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unbalanced { batch } => write!(f, "batch {batch} does not balance"),
            Self::TotalsOverflow { batch } => {
                write!(f, "period totals overflowed at batch {batch}")
            }
            Self::CarryMismatch {
                batch,
                expected,
                found,
            } => write!(
                f,
                "batch {batch} carried in {found}, but {expected} were outstanding"
            ),
        }
    }
}

impl std::error::Error for ChainBreak {}

/// A sequence of batches, checked so the conservation law telescopes.
///
/// Each batch must balance on its own **and** carry in exactly what its
/// predecessor left held. Chained that way, one equation covers a whole billing
/// period: everything ever ingested has reached a final outcome, or is still
/// explicitly outstanding and named as such.
///
/// ```rust
/// use mediation::{BatchReconciliation, Disposition, HoldReason, PeriodLedger};
///
/// let mut first = BatchReconciliation::EMPTY;
/// first.observe(&Disposition::Billable);
/// first.observe(&Disposition::Held(HoldReason::AwaitingPartial));
///
/// // The second batch re-presents the held record; the successor arrived.
/// let mut second = BatchReconciliation::EMPTY;
/// second.observe_carried(&Disposition::Billable);
/// second.observe(&Disposition::Billable);
///
/// let mut ledger = PeriodLedger::new();
/// ledger.append(first).unwrap();
/// ledger.append(second).unwrap();
///
/// assert_eq!(ledger.outstanding(), 0);
/// assert_eq!(ledger.totals().released(), 3);
/// assert!(ledger.is_closable());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PeriodLedger {
    totals: BatchReconciliation,
    batches: usize,
    outstanding: u64,
    breaks: Vec<ChainBreak>,
}

impl PeriodLedger {
    /// An empty period.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            totals: BatchReconciliation::EMPTY,
            batches: 0,
            outstanding: 0,
            breaks: Vec::new(),
        }
    }

    /// Add the next batch of the period.
    ///
    /// # Errors
    ///
    /// [`ChainBreak`] when the batch does not balance, or when its `carried_in`
    /// disagrees with what was outstanding. The break is **also** recorded, so
    /// a caller that chooses to continue past one still sees it in
    /// [`breaks`](Self::breaks) — a period with a hole in the middle must not
    /// look clean because the caller kept going.
    pub fn append(&mut self, batch: BatchReconciliation) -> Result<(), ChainBreak> {
        let index = self.batches;
        self.batches += 1;

        let mut first_break = None;
        if !batch.balances() {
            let broken = ChainBreak::Unbalanced { batch: index };
            self.breaks.push(broken);
            first_break = Some(broken);
        }
        if batch.carried_in() != self.outstanding {
            let broken = ChainBreak::CarryMismatch {
                batch: index,
                expected: self.outstanding,
                found: batch.carried_in(),
            };
            self.breaks.push(broken);
            first_break = first_break.or(Some(broken));
        }

        self.outstanding = batch.held();
        match self.totals.checked_add(batch) {
            Some(totals) => self.totals = totals,
            None => {
                // The running totals are left untouched: a wrapped total would
                // be a plausible-looking wrong number, and the break says the
                // period can no longer be summed rather than that a record was
                // lost.
                let broken = ChainBreak::TotalsOverflow { batch: index };
                self.breaks.push(broken);
                first_break = first_break.or(Some(broken));
            }
        }

        match first_break {
            Some(broken) => Err(broken),
            None => Ok(()),
        }
    }

    /// How many records the most recent batch left held.
    #[must_use]
    pub const fn outstanding(&self) -> u64 {
        self.outstanding
    }

    /// How many batches the period holds.
    #[must_use]
    pub const fn batches(&self) -> usize {
        self.batches
    }

    /// Every break found, in the order they were found.
    #[must_use]
    pub fn breaks(&self) -> &[ChainBreak] {
        &self.breaks
    }

    /// The summed counts across the period.
    ///
    /// `carried_in` here is the sum of every batch's carry, which is a measure
    /// of how much work crossed a boundary rather than of anything owed, and
    /// `held` is the sum of every batch's holds, which counts a record once per
    /// batch it waited in. Neither is the period's own arithmetic — for that,
    /// ask [`period_totals`](Self::period_totals).
    #[must_use]
    pub const fn totals(&self) -> BatchReconciliation {
        self.totals
    }

    /// The period's own arithmetic, with the carries telescoped away.
    ///
    /// The batch law counts a held record again in every batch that
    /// re-presents it, which is right per batch and wrong for a period: a
    /// record held through four batches would be counted four times over.
    /// [`PeriodTotals`] states the law that survives the chaining —
    ///
    /// ```text
    /// ingested == billable + suppressed + duplicate + rejected + outstanding
    /// ```
    ///
    /// — where `outstanding` is what the **last** batch left held rather than
    /// the sum of every batch's holds. That is the equation a period close
    /// needs, and it is the one the chaining exists to make true.
    #[must_use]
    pub const fn period_totals(&self) -> PeriodTotals {
        PeriodTotals {
            ingested: self.totals.ingested(),
            billable: self.totals.released(),
            suppressed: self.totals.suppressed(),
            duplicate: self.totals.duplicate(),
            rejected: self.totals.rejected(),
            outstanding: self.outstanding,
        }
    }

    /// `true` when the period may be closed.
    ///
    /// Every batch balanced, every carry matched, and nothing is still held.
    /// A period closed while records are outstanding will need reopening.
    #[must_use]
    pub fn is_closable(&self) -> bool {
        self.breaks.is_empty() && self.outstanding == 0
    }
}

/// The conservation law over a whole period, with the batch carries
/// telescoped away.
///
/// Produced by [`PeriodLedger::period_totals`]. Every record ever ingested
/// reached a **final** outcome, or is still explicitly outstanding:
///
/// ```text
/// ingested == billable + suppressed + duplicate + rejected + outstanding
/// ```
///
/// A separate type from [`BatchReconciliation`] because it states a different
/// equation over a different set of counters — there is no `carried_in` here,
/// and `outstanding` is the last batch's holds rather than every batch's. One
/// type carrying both meanings would let a caller read the wrong law off the
/// right numbers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct PeriodTotals {
    /// How many records arrived for the first time across the period.
    pub ingested: u64,
    /// How many were released downstream.
    pub billable: u64,
    /// How many were understood and deliberately not billed.
    pub suppressed: u64,
    /// How many were recognised as retransmissions.
    pub duplicate: u64,
    /// How many can never be billed.
    pub rejected: u64,
    /// How many the last batch left waiting.
    pub outstanding: u64,
}

impl PeriodTotals {
    /// How many reached a final outcome.
    ///
    /// `None` on overflow, which is itself an imbalance.
    #[must_use]
    pub const fn finalised(&self) -> Option<u64> {
        let Some(total) = self.billable.checked_add(self.suppressed) else {
            return None;
        };
        let Some(total) = total.checked_add(self.duplicate) else {
            return None;
        };
        total.checked_add(self.rejected)
    }

    /// Whether the period's law holds.
    ///
    /// An overflowing sum reads as `false`: it cannot equal an ingested count
    /// that did not itself overflow, so an unbalanced period never reads as
    /// balanced by luck.
    #[must_use]
    pub const fn balances(&self) -> bool {
        let Some(finalised) = self.finalised() else {
            return false;
        };
        match finalised.checked_add(self.outstanding) {
            Some(total) => total == self.ingested,
            None => false,
        }
    }

    /// `true` when the period balances and nothing is still waiting.
    #[must_use]
    pub const fn is_closable(&self) -> bool {
        self.balances() && self.outstanding == 0
    }
}

impl fmt::Display for PeriodTotals {
    /// One line, in the order of the law, so an imbalance is visible by
    /// inspection.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "ingested={} = billable={} + suppressed={} + duplicate={} + rejected={} + outstanding={} [{}]",
            self.ingested,
            self.billable,
            self.suppressed,
            self.duplicate,
            self.rejected,
            self.outstanding,
            if self.balances() {
                "balanced"
            } else {
                "UNBALANCED"
            }
        )
    }
}

impl fmt::Display for PeriodLedger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} batch(es), {} outstanding, {} break(s) [{}]",
            self.batches,
            self.outstanding,
            self.breaks.len(),
            if self.is_closable() {
                "closable"
            } else {
                "OPEN"
            }
        )
    }
}

// ── Serde ────────────────────────────────────────────────────────────────────

#[cfg(feature = "serde")]
mod serde_impl {
    use super::{BatchReconciliation, HoldReason, Outcome, SuppressReason};
    use serde::de::{self, Visitor};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::fmt;

    macro_rules! string_serde {
        ($ty:ty, $expecting:literal) => {
            impl Serialize for $ty {
                fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                    serializer.serialize_str(self.as_str())
                }
            }

            impl<'de> Deserialize<'de> for $ty {
                fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                    struct V;
                    impl Visitor<'_> for V {
                        type Value = $ty;
                        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                            f.write_str($expecting)
                        }
                        fn visit_str<E: de::Error>(self, v: &str) -> Result<$ty, E> {
                            v.parse().map_err(de::Error::custom)
                        }
                    }
                    deserializer.deserialize_str(V)
                }
            }
        };
    }

    string_serde!(SuppressReason, "a suppression reason code");
    string_serde!(HoldReason, "a hold reason code");
    string_serde!(Outcome, "an outcome code such as \"BILLABLE\"");

    /// Field names are part of the public representation and covered by semver.
    #[derive(Serialize, Deserialize)]
    #[serde(rename = "BatchReconciliation")]
    struct Repr {
        #[serde(default)]
        carried_in: u64,
        ingested: u64,
        billable: u64,
        suppressed: u64,
        duplicate: u64,
        rejected: u64,
        held: u64,
    }

    impl Serialize for BatchReconciliation {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            Repr {
                carried_in: self.carried_in(),
                ingested: self.ingested(),
                billable: self.released(),
                suppressed: self.suppressed(),
                duplicate: self.duplicate(),
                rejected: self.rejected(),
                held: self.held(),
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for BatchReconciliation {
        /// Deserialises **without** requiring the law to hold.
        ///
        /// A stored reconciliation that does not balance is exactly the thing a
        /// caller needs to read back and investigate; refusing to decode it
        /// would hide the evidence.
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = Repr::deserialize(deserializer)?;
            Ok(BatchReconciliation {
                carried_in: r.carried_in,
                ingested: r.ingested,
                billable: r.billable,
                suppressed: r.suppressed,
                duplicate: r.duplicate,
                rejected: r.rejected,
                held: r.held,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lateness::Lateness;
    use time::Duration;

    fn key() -> RecordKey {
        RecordKey::RecordingEntity {
            entity: "msc-01".parse().unwrap(),
            local_sequence: 1,
        }
    }

    #[test]
    fn every_outcome_counts_exactly_once() {
        let dispositions = [
            Disposition::Billable,
            Disposition::Billable,
            Disposition::Suppressed(SuppressReason::TestIdentity),
            Disposition::Duplicate(key()),
            Disposition::Rejected(RejectReason::BeyondDeadline),
            Disposition::Held(HoldReason::AwaitingPartial),
        ];
        let counts: BatchReconciliation = dispositions.iter().collect();

        assert_eq!(counts.ingested(), 6);
        assert_eq!(counts.released(), 2);
        assert_eq!(counts.suppressed(), 1);
        assert_eq!(counts.duplicate(), 1);
        assert_eq!(counts.rejected(), 1);
        assert_eq!(counts.held(), 1);
        assert!(counts.balances());
    }

    /// A batch with something still in flight balances, and is still not final:
    /// a period closed over it would need reopening.
    #[test]
    fn holding_a_record_balances_but_does_not_finish_the_batch() {
        let counts: BatchReconciliation = vec![
            Disposition::Billable,
            Disposition::Held(HoldReason::AwaitingCorrelation),
        ]
        .into_iter()
        .collect();

        assert!(counts.balances());
        assert!(!counts.is_final());

        let settled: BatchReconciliation = vec![Disposition::Billable].into_iter().collect();
        assert!(settled.is_final());
    }

    /// The law cannot be broken through the public counting path, which is the
    /// point — an unbalanced value must have been assembled some other way.
    #[test]
    fn counting_through_observe_always_balances() {
        let mut counts = BatchReconciliation::EMPTY;
        for reason in SuppressReason::ALL {
            counts.observe(&Disposition::Suppressed(*reason));
        }
        for reason in HoldReason::ALL {
            counts.observe(&Disposition::Held(*reason));
        }
        counts.observe(&Disposition::Billable);
        counts.observe(&Disposition::Duplicate(key()));
        counts.observe(&Disposition::Rejected(RejectReason::Malformed));
        assert!(counts.balances());
        assert_eq!(
            counts.ingested(),
            (SuppressReason::ALL.len() + HoldReason::ALL.len() + 3) as u64
        );
    }

    #[test]
    fn a_deserialised_imbalance_is_readable_and_reported() {
        let broken = BatchReconciliation {
            carried_in: 0,
            ingested: 10,
            billable: 3,
            suppressed: 0,
            duplicate: 0,
            rejected: 0,
            held: 0,
        };
        assert!(!broken.balances());
        assert!(!broken.is_final());
        assert!(broken.to_string().contains("UNBALANCED"), "{broken}");
    }

    /// The boundary the ledger exists to guard: a held record re-presented as
    /// newly ingested is counted twice across two batches that each balance.
    #[test]
    fn a_carried_record_is_not_counted_as_a_new_arrival() {
        let mut first = BatchReconciliation::EMPTY;
        first.observe(&Disposition::Billable);
        first.observe(&Disposition::Held(HoldReason::AwaitingPartial));
        assert_eq!(first.held(), 1);

        // Correct: the re-presented record is carried, not ingested.
        let mut correct = BatchReconciliation::EMPTY;
        correct.observe_carried(&Disposition::Billable);
        assert_eq!(correct.carried_in(), 1);
        assert_eq!(correct.ingested(), 0);
        assert!(correct.balances());

        let mut ledger = PeriodLedger::new();
        ledger.append(first).unwrap();
        ledger.append(correct).unwrap();

        assert!(ledger.is_closable());
        assert_eq!(
            ledger.totals().ingested(),
            2,
            "two records arrived, in the first batch"
        );
        assert_eq!(
            ledger.totals().carried_in(),
            1,
            "one of them crossed a boundary rather than arriving twice"
        );
        assert_eq!(ledger.totals().released(), 2);
    }

    /// The same record counted as a new arrival balances *within* its batch and
    /// still breaks the period.
    #[test]
    fn counting_a_carried_record_as_new_breaks_the_chain() {
        let mut first = BatchReconciliation::EMPTY;
        first.observe(&Disposition::Held(HoldReason::AwaitingPartial));

        let mut wrong = BatchReconciliation::EMPTY;
        wrong.observe(&Disposition::Billable);
        assert!(wrong.balances(), "the batch is internally consistent");

        let mut ledger = PeriodLedger::new();
        ledger.append(first).unwrap();
        assert_eq!(
            ledger.append(wrong),
            Err(ChainBreak::CarryMismatch {
                batch: 1,
                expected: 1,
                found: 0,
            })
        );
        assert!(!ledger.is_closable());
    }

    /// Dropping a held record is the mirror failure and is caught the same way.
    #[test]
    fn dropping_a_held_record_breaks_the_chain() {
        let mut first = BatchReconciliation::EMPTY;
        for _ in 0..3 {
            first.observe(&Disposition::Held(HoldReason::AwaitingCorrelation));
        }

        let mut second = BatchReconciliation::EMPTY;
        second.observe_carried(&Disposition::Billable);
        second.observe_carried(&Disposition::Billable);

        let mut ledger = PeriodLedger::new();
        ledger.append(first).unwrap();
        assert!(matches!(
            ledger.append(second),
            Err(ChainBreak::CarryMismatch { found: 2, .. })
        ));
    }

    /// A period stays open while anything is held, however clean the batches.
    #[test]
    fn a_period_with_outstanding_records_is_not_closable() {
        let mut batch = BatchReconciliation::EMPTY;
        batch.observe(&Disposition::Billable);
        batch.observe(&Disposition::Held(HoldReason::SequenceGap));

        let mut ledger = PeriodLedger::new();
        ledger.append(batch).unwrap();
        assert_eq!(ledger.outstanding(), 1);
        assert!(!ledger.is_closable());
        assert!(ledger.breaks().is_empty(), "outstanding is not a break");
        assert!(ledger.to_string().contains("OPEN"), "{ledger}");
    }

    /// A break in the middle is remembered even if the caller keeps going.
    #[test]
    fn a_break_survives_a_caller_that_continues() {
        let mut first = BatchReconciliation::EMPTY;
        first.observe(&Disposition::Held(HoldReason::AwaitingPartial));

        let mut wrong = BatchReconciliation::EMPTY;
        wrong.observe(&Disposition::Billable);

        let mut clean = BatchReconciliation::EMPTY;
        clean.observe(&Disposition::Billable);

        let mut ledger = PeriodLedger::new();
        ledger.append(first).unwrap();
        let _ = ledger.append(wrong);
        ledger.append(clean).unwrap();

        assert_eq!(ledger.breaks().len(), 1);
        assert!(!ledger.is_closable(), "a past break is never forgiven");
    }

    /// A period can exceed what a `u64` counts while every batch in it is
    /// internally consistent — that is an overflow, not a lost record.
    #[test]
    fn a_totals_overflow_is_not_reported_as_an_imbalance() {
        let mut huge = BatchReconciliation::EMPTY;
        for _ in 0..2 {
            huge.observe(&Disposition::Billable);
        }
        // Force the running totals past what they can hold.
        let mut period = PeriodLedger::new();
        period.totals = BatchReconciliation {
            carried_in: 0,
            ingested: u64::MAX,
            billable: u64::MAX,
            suppressed: 0,
            duplicate: 0,
            rejected: 0,
            held: 0,
        };

        assert_eq!(
            period.append(huge),
            Err(ChainBreak::TotalsOverflow { batch: 0 })
        );
        assert!(!period.is_closable());
        assert!(period.to_string().contains("break"), "{period}");
    }

    #[test]
    fn an_empty_period_is_closable_and_says_nothing_else() {
        let ledger = PeriodLedger::new();
        assert!(ledger.is_closable());
        assert_eq!(ledger.batches(), 0);
        assert_eq!(ledger.outstanding(), 0);
    }

    #[test]
    fn merging_recombines_parallel_work() {
        let left: BatchReconciliation = vec![Disposition::Billable].into_iter().collect();
        let right: BatchReconciliation = vec![
            Disposition::Billable,
            Disposition::Rejected(RejectReason::Malformed),
        ]
        .into_iter()
        .collect();

        let merged = left.checked_add(right).unwrap();
        assert_eq!(merged.ingested(), 3);
        assert_eq!(merged.released(), 2);
        assert!(merged.balances());
    }

    /// A wrapped count would make an unbalanced batch look balanced, so every
    /// addition is checked.
    #[test]
    fn merging_is_checked() {
        let huge = BatchReconciliation {
            carried_in: 0,
            ingested: u64::MAX,
            billable: u64::MAX,
            suppressed: 0,
            duplicate: 0,
            rejected: 0,
            held: 0,
        };
        let one: BatchReconciliation = vec![Disposition::Billable].into_iter().collect();
        assert_eq!(huge.checked_add(one), None);
    }

    /// The bridge from lateness to rejection: a late record is still billable,
    /// and only the two terminal verdicts map across.
    #[test]
    fn only_terminal_lateness_verdicts_become_rejections() {
        assert_eq!(RejectReason::from_lateness(Lateness::OnTime), None);
        assert_eq!(
            RejectReason::from_lateness(Lateness::Late(Duration::days(1))),
            None
        );
        assert_eq!(
            RejectReason::from_lateness(Lateness::BeyondDeadline(Duration::days(40))),
            Some(RejectReason::BeyondDeadline)
        );
        assert_eq!(
            RejectReason::from_lateness(Lateness::Future(Duration::hours(1))),
            Some(RejectReason::FutureTimestamp)
        );
    }

    #[test]
    fn dispositions_report_their_own_finality() {
        assert!(Disposition::Billable.is_released());
        assert!(!Disposition::Billable.may_return());
        assert!(!Disposition::Billable.is_finding());

        let held = Disposition::Held(HoldReason::SequenceGap);
        assert!(!held.is_released());
        assert!(held.may_return());
        assert!(held.is_finding());

        let rejected = Disposition::Rejected(RejectReason::Malformed);
        assert!(!rejected.may_return(), "a rejection is terminal");
        assert!(rejected.is_finding());
    }

    #[test]
    fn reason_codes_round_trip() {
        for reason in SuppressReason::ALL {
            assert_eq!(reason.as_str().parse::<SuppressReason>().unwrap(), *reason);
        }
        for reason in HoldReason::ALL {
            assert_eq!(reason.as_str().parse::<HoldReason>().unwrap(), *reason);
        }
        assert!("NOPE".parse::<SuppressReason>().is_err());
    }

    #[test]
    fn dispositions_render_legibly() {
        assert_eq!(
            Disposition::Suppressed(SuppressReason::ZeroRated).to_string(),
            "SUPPRESSED:ZERO_RATED"
        );
        assert_eq!(
            Disposition::Rejected(RejectReason::NotKeyable(NotKeyable::NoCorrelatorPresent))
                .to_string(),
            "REJECTED:NOT_KEYABLE:NO_CORRELATOR_PRESENT"
        );
        assert_eq!(
            Disposition::Held(HoldReason::SequenceGap).to_string(),
            "HELD:SEQUENCE_GAP"
        );
    }
}