mediation 0.2.0

Telecom usage mediation domain library — one-pass batch processing with a conservation law that proves no record was lost, 3GPP CDR local-time-plus-offset arithmetic (quarter-hour offsets, DST-adjusted), integer octet volumes, validated identifiers (IMSI/MSISDN/IMEI/ICCID/SUPI/GPSI/PEI/SUCI, PLMN, TADIG), natural-key deduplication, RFC 1982 wrap-aware sequence-gap detection, partial-record reassembly, cross-node correlation, signed restatement, a numbered validation catalogue, wire codecs for TBCD/BCD, ISDN-AddressString, the PLMN identity and the full User Location Information element, a zero-copy BER reader, usage aggregation, roaming-batch reconciliation, longest-prefix number ranges with portability, conserving tariff-band apportionment, two-question jurisdiction determination and an EU/German regulatory layer. Zero I/O, no async, no clock, exact integer quantities, and no input can panic a parser.
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
//! The release gate: may this batch be invoiced?
//!
//! One question, answered from three inputs — whether the batch balances,
//! whether its sources are complete, and what the rules found.
//!
//! ```rust
//! use mediation::{BatchReconciliation, Disposition, QualityGrade, ValidationReport, score_batch};
//!
//! let counts: BatchReconciliation = vec![Disposition::Billable; 100].into_iter().collect();
//! let report = score_batch(&counts, &ValidationReport::new(), true);
//!
//! assert_eq!(report.grade, QualityGrade::A);
//! assert!(!report.blocks_release());
//! ```
//!
//! # Why completeness is an input rather than a finding
//!
//! A source with an unfilled sequence hole has **unknown** completeness, and
//! that is categorically different from a known fault. Nothing in the batch is
//! wrong; what is wrong is that the batch cannot say what it is missing.
//! Releasing it invoices an amount nobody can prove is right, so it is a hard
//! gate rather than a score that enough good records could outvote.
//!
//! It is per **source**, not per batch. [`crate::pipeline::process`] holds the
//! records of a source with a hole and releases everybody else's, so the
//! question this gate asks is whether anything *released* came from a source
//! that cannot account for itself. Blocking the whole batch instead would stop
//! an operator's billing run on one gateway's lost file.
//!
//! # Why a grade at all
//!
//! The gate is a boolean, but an operator needs to know whether a batch is
//! clean, merely noisy, or deteriorating. The grade is that signal, and it is
//! deliberately coarse: four values that fit in a dashboard column and mean the
//! same thing to everyone reading it.

use std::fmt;
use std::str::FromStr;

use crate::disposition::BatchReconciliation;
use crate::error::ParseError;
use crate::validation::ValidationReport;

/// How clean a batch is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum QualityGrade {
    /// Balanced, complete, nothing found. Release.
    A,
    /// Balanced and complete; a few warnings. Release, logged.
    B,
    /// Balanced and complete; many warnings, or records still held. Release
    /// with a reconciliation note.
    C,
    /// Unbalanced, incomplete, or something blocking. **Do not release.**
    F,
}

impl QualityGrade {
    /// Every grade, cleanest first.
    pub const ALL: &'static [Self] = &[Self::A, Self::B, Self::C, Self::F];

    /// `true` for the one grade that stops a release.
    #[must_use]
    pub const fn blocks_release(self) -> bool {
        matches!(self, Self::F)
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::A => "A",
            Self::B => "B",
            Self::C => "C",
            Self::F => "F",
        }
    }
}

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

impl FromStr for QualityGrade {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_uppercase().as_str() {
            "A" => Ok(Self::A),
            "B" => Ok(Self::B),
            "C" => Ok(Self::C),
            "F" => Ok(Self::F),
            _ => Err(ParseError::one_of("QualityGrade", s, &["A", "B", "C", "F"])),
        }
    }
}

/// Why a batch may not be released.
///
/// Each variant is independently sufficient, and all that apply are reported —
/// fixing one does not imply the batch is releasable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ReleaseBlocker {
    /// The disposition counts do not satisfy the conservation law.
    Unbalanced,
    /// A source has an unfilled sequence hole, so completeness is unknown.
    IncompleteSources,
    /// At least one rule fired at blocking severity.
    BlockingFindings,
}

impl ReleaseBlocker {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[
        Self::Unbalanced,
        Self::IncompleteSources,
        Self::BlockingFindings,
    ];

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unbalanced => "UNBALANCED",
            Self::IncompleteSources => "INCOMPLETE_SOURCES",
            Self::BlockingFindings => "BLOCKING_FINDINGS",
        }
    }
}

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

/// What the gate decided, and why.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QualityReport {
    /// How clean the batch is.
    pub grade: QualityGrade,
    /// Every reason the batch may not be released, in a stable order.
    pub blockers: Vec<ReleaseBlocker>,
    /// How many findings fired at blocking severity.
    pub blocking_findings: usize,
    /// How many findings fired at warning severity.
    pub warnings: usize,
    /// How many records are waiting for something that may still arrive.
    pub held: u64,
}

impl QualityReport {
    /// `true` when the batch may not be invoiced.
    ///
    /// The one call a release path needs to make.
    #[must_use]
    pub const fn blocks_release(&self) -> bool {
        !self.blockers.is_empty()
    }

    /// `true` when the batch is releasable **and** nothing is still in flight.
    ///
    /// The precondition for closing a period: 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.blockers.is_empty() && self.held == 0
    }
}

impl fmt::Display for QualityReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "grade {} ({} blocking, {} warning, {} held)",
            self.grade, self.blocking_findings, self.warnings, self.held
        )?;
        if !self.blockers.is_empty() {
            write!(f, " — blocked by ")?;
            for (position, blocker) in self.blockers.iter().enumerate() {
                if position > 0 {
                    f.write_str(", ")?;
                }
                write!(f, "{blocker}")?;
            }
        }
        Ok(())
    }
}

/// Grade a batch and decide whether it may be released.
///
/// `sources_complete` is the caller's answer to *"is every source whose records
/// this batch **releases** complete?"* — the crate cannot know how many sources
/// a batch drew on, and guessing would be the one mistake that lets an
/// incomplete batch through.
///
/// Note *releases*, not *touches*. [`crate::pipeline::process`] holds every
/// record from a source with an unfilled hole
/// ([`HoldReason::SequenceGap`](crate::HoldReason::SequenceGap)), so what it
/// releases is complete by construction and it passes `true` here. A caller
/// assembling their own batch, and releasing records from a source they cannot
/// prove complete, passes `false` — and the gate stops them.
///
/// The grade is derived, not configurable: the boundaries encode when an
/// operator should look, and a per-deployment threshold would make two teams'
/// "grade B" mean different things.
#[must_use]
pub fn score_batch(
    counts: &BatchReconciliation,
    findings: &ValidationReport,
    sources_complete: bool,
) -> QualityReport {
    let blocking_findings = findings.blocking_count();
    let warnings = findings.len() - blocking_findings;

    let mut blockers = Vec::new();
    if !counts.balances() {
        blockers.push(ReleaseBlocker::Unbalanced);
    }
    if !sources_complete {
        blockers.push(ReleaseBlocker::IncompleteSources);
    }
    if blocking_findings > 0 {
        blockers.push(ReleaseBlocker::BlockingFindings);
    }

    let grade = if blockers.is_empty() {
        grade_clean_batch(counts, warnings)
    } else {
        QualityGrade::F
    };

    QualityReport {
        grade,
        blockers,
        blocking_findings,
        warnings,
        held: counts.held(),
    }
}

/// The grade for a batch that is releasable, from how noisy it is.
///
/// Thresholds are proportions rather than counts, so a batch of a hundred and a
/// batch of a hundred million grade the same way for the same defect rate.
fn grade_clean_batch(counts: &BatchReconciliation, warnings: usize) -> QualityGrade {
    // Everything the batch had to account for, carried-in records included. A
    // batch that only re-presents what a predecessor held has `ingested == 0`
    // and is still a batch: grading it against `ingested` alone would hand an
    // A to a re-presentation full of warnings, and a period whose held records
    // keep failing is exactly the case a grade is meant to surface.
    let presented = counts.presented().unwrap_or(0);
    if presented == 0 {
        // An empty batch is trivially clean. It is also not evidence of
        // anything, which the caller sees in `presented`.
        return QualityGrade::A;
    }

    let warnings = warnings as u64;
    if warnings == 0 && counts.held() == 0 {
        return QualityGrade::A;
    }
    // Below one percent noise, and under a tenth held, is a working batch.
    if warnings.saturating_mul(100) <= presented && counts.held().saturating_mul(10) <= presented {
        return QualityGrade::B;
    }
    QualityGrade::C
}

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

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

    impl Serialize for QualityGrade {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(self.as_str())
        }
    }

    impl<'de> Deserialize<'de> for QualityGrade {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct V;
            impl Visitor<'_> for V {
                type Value = QualityGrade;
                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("a quality grade: \"A\", \"B\", \"C\" or \"F\"")
                }
                fn visit_str<E: de::Error>(self, v: &str) -> Result<QualityGrade, E> {
                    v.parse().map_err(de::Error::custom)
                }
            }
            deserializer.deserialize_str(V)
        }
    }

    impl Serialize for ReleaseBlocker {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(self.as_str())
        }
    }

    impl<'de> Deserialize<'de> for ReleaseBlocker {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct V;
            impl Visitor<'_> for V {
                type Value = ReleaseBlocker;
                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("a release blocker code")
                }
                fn visit_str<E: de::Error>(self, v: &str) -> Result<ReleaseBlocker, E> {
                    ReleaseBlocker::ALL
                        .iter()
                        .find(|b| b.as_str() == v)
                        .copied()
                        .ok_or_else(|| de::Error::custom(format!("unknown blocker {v:?}")))
                }
            }
            deserializer.deserialize_str(V)
        }
    }

    /// Field names are part of the public representation and covered by semver.
    #[derive(Serialize, Deserialize)]
    #[serde(rename = "QualityReport")]
    struct Repr {
        grade: QualityGrade,
        blockers: Vec<ReleaseBlocker>,
        blocking_findings: usize,
        warnings: usize,
        held: u64,
    }

    impl Serialize for QualityReport {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            Repr {
                grade: self.grade,
                blockers: self.blockers.clone(),
                blocking_findings: self.blocking_findings,
                warnings: self.warnings,
                held: self.held,
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for QualityReport {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = Repr::deserialize(deserializer)?;
            Ok(QualityReport {
                grade: r.grade,
                blockers: r.blockers,
                blocking_findings: r.blocking_findings,
                warnings: r.warnings,
                held: r.held,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dedup::{NotKeyable, RecordKey};
    use crate::disposition::{Disposition, HoldReason, RejectReason, SuppressReason};
    use crate::validation::{Finding, Rule};

    fn counts(dispositions: Vec<Disposition>) -> BatchReconciliation {
        dispositions.into_iter().collect()
    }

    fn clean(n: usize) -> BatchReconciliation {
        counts(vec![Disposition::Billable; n])
    }

    fn warnings(n: usize) -> ValidationReport {
        (0..n)
            .map(|i| Finding::new(Rule::ZeroUsage, i, "no quantity"))
            .collect()
    }

    #[test]
    fn a_clean_complete_batch_grades_a_and_releases() {
        let report = score_batch(&clean(100), &ValidationReport::new(), true);
        assert_eq!(report.grade, QualityGrade::A);
        assert!(!report.blocks_release());
        assert!(report.is_final());
        assert!(report.blockers.is_empty());
    }

    /// The hard gate: a source with an unfilled hole has unknown completeness,
    /// which no number of good records can outvote.
    #[test]
    fn an_incomplete_source_blocks_however_clean_the_records_are() {
        let report = score_batch(&clean(1_000_000), &ValidationReport::new(), false);
        assert_eq!(report.grade, QualityGrade::F);
        assert!(report.blocks_release());
        assert_eq!(report.blockers, vec![ReleaseBlocker::IncompleteSources]);
    }

    /// An imbalance cannot be built through the counting API — `observe`
    /// increments `ingested` and exactly one outcome — so the only way one
    /// arrives is from storage. That is itself the property worth having, and
    /// it is why this test needs the `serde` feature to construct its input.
    #[cfg(feature = "serde")]
    #[test]
    fn an_unbalanced_batch_blocks() {
        let broken: BatchReconciliation = serde_json::from_str(
            r#"{"ingested":10,"billable":3,"suppressed":0,"duplicate":0,"rejected":0,"held":0}"#,
        )
        .unwrap();
        let report = score_batch(&broken, &ValidationReport::new(), true);
        assert_eq!(report.grade, QualityGrade::F);
        assert!(report.blockers.contains(&ReleaseBlocker::Unbalanced));
    }

    #[test]
    fn a_blocking_finding_blocks() {
        let findings: ValidationReport =
            vec![Finding::not_keyable(0, NotKeyable::NoCorrelatorPresent)]
                .into_iter()
                .collect();
        let report = score_batch(&clean(10), &findings, true);
        assert_eq!(report.grade, QualityGrade::F);
        assert_eq!(report.blocking_findings, 1);
        assert_eq!(report.warnings, 0);
        assert_eq!(report.blockers, vec![ReleaseBlocker::BlockingFindings]);
    }

    /// Every reason is reported, so fixing one does not imply the batch is
    /// releasable.
    #[cfg(feature = "serde")]
    #[test]
    fn all_blockers_are_reported_not_just_the_first() {
        let broken: BatchReconciliation = serde_json::from_str(
            r#"{"ingested":10,"billable":3,"suppressed":0,"duplicate":0,"rejected":0,"held":0}"#,
        )
        .unwrap();
        let findings: ValidationReport = vec![Finding::new(
            crate::validation::Rule::ClosedBeforeOpened,
            0,
            "",
        )]
        .into_iter()
        .collect();

        let report = score_batch(&broken, &findings, false);
        assert_eq!(report.blockers, ReleaseBlocker::ALL.to_vec());
        assert!(report.to_string().contains("UNBALANCED"), "{report}");
    }

    /// Grades are proportional, so scale does not change the verdict for the
    /// same defect rate.
    #[test]
    fn grading_is_by_proportion_not_by_count() {
        // One warning in a hundred is one percent — still a working batch.
        assert_eq!(
            score_batch(&clean(100), &warnings(1), true).grade,
            QualityGrade::B
        );
        // Ten thousand warnings in a million is the same rate, same grade.
        assert_eq!(
            score_batch(&clean(1_000_000), &warnings(10_000), true).grade,
            QualityGrade::B
        );
        // Two percent is noisy enough to look at.
        assert_eq!(
            score_batch(&clean(100), &warnings(2), true).grade,
            QualityGrade::C
        );
    }

    /// Held records do not block, but they do stop a period closing.
    #[test]
    fn held_records_grade_down_without_blocking() {
        let mut dispositions = vec![Disposition::Billable; 95];
        dispositions.extend(vec![Disposition::Held(HoldReason::AwaitingPartial); 5]);
        let report = score_batch(&counts(dispositions), &ValidationReport::new(), true);

        assert!(!report.blocks_release(), "held is not a fault");
        assert!(!report.is_final(), "…but the period cannot close");
        assert_eq!(report.grade, QualityGrade::B);
        assert_eq!(report.held, 5);

        // A batch that is half held is deteriorating.
        let mut heavy = vec![Disposition::Billable; 50];
        heavy.extend(vec![Disposition::Held(HoldReason::SequenceGap); 50]);
        assert_eq!(
            score_batch(&counts(heavy), &ValidationReport::new(), true).grade,
            QualityGrade::C
        );
    }

    /// Suppressions and rejections are understood outcomes, not noise, so they
    /// do not by themselves grade a batch down.
    #[test]
    fn understood_outcomes_do_not_degrade_the_grade() {
        let batch = counts(vec![
            Disposition::Billable,
            Disposition::Suppressed(SuppressReason::TestIdentity),
            Disposition::Rejected(RejectReason::Malformed),
            Disposition::Duplicate(RecordKey::RecordingEntity {
                entity: "msc-01".parse().unwrap(),
                local_sequence: 1,
            }),
        ]);
        let report = score_batch(&batch, &ValidationReport::new(), true);
        assert_eq!(report.grade, QualityGrade::A);
        assert!(report.is_final());
    }

    #[test]
    fn an_empty_batch_is_clean_and_says_nothing_else() {
        let report = score_batch(&BatchReconciliation::EMPTY, &ValidationReport::new(), true);
        assert_eq!(report.grade, QualityGrade::A);
        assert!(report.is_final());
    }

    #[test]
    fn grades_round_trip_and_only_f_blocks() {
        for grade in QualityGrade::ALL {
            assert_eq!(grade.as_str().parse::<QualityGrade>().unwrap(), *grade);
            assert_eq!(grade.blocks_release(), *grade == QualityGrade::F);
        }
        assert!("Z".parse::<QualityGrade>().is_err());
        // Grades order from cleanest to worst.
        assert!(QualityGrade::A < QualityGrade::B);
        assert!(QualityGrade::C < QualityGrade::F);
    }
}