asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Evidence ledger for "galaxy-brain" oracle diagnostics.
//!
//! The evidence ledger explains invariant violations (or their absence) using
//! Bayes factors and log-likelihood contributions. Each invariant gets a
//! structured evidence entry with:
//!
//! - **Bayes factor** (BF): quantifies how much the observed data favours the
//!   "invariant violated" hypothesis vs "invariant holds".
//! - **Log-likelihood contributions**: breaks the evidence into structural,
//!   temporal, and aggregate components.
//! - **Evidence lines**: human-readable `equation + substitution + intuition`
//!   triples that form the "galaxy-brain" explanation.
//!
//! # Bayes Factor Interpretation (Kass & Raftery 1995)
//!
//! | log₁₀(BF) | BF        | Strength      |
//! |-----------|-----------|---------------|
//! | < 0       | < 1       | Against       |
//! | 0 – 0.5  | 1 – 3.2   | Negligible    |
//! | 0.5 – 1.3| 3.2 – 20  | Positive      |
//! | 1.3 – 2.2| 20 – 150  | Strong        |
//! | > 2.2    | > 150     | Very strong   |

use std::fmt::Write as _;

use serde::{Deserialize, Serialize};

use super::{OracleReport, OracleStats};

// ---------------------------------------------------------------------------
// Evidence strength classification
// ---------------------------------------------------------------------------

/// Strength of evidence for a hypothesis, following Kass & Raftery (1995).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvidenceStrength {
    /// BF < 1 — evidence *against* the hypothesis.
    Against,
    /// 1 ≤ BF < 3.2 (log₁₀ BF < 0.5) — barely worth mentioning.
    Negligible,
    /// 3.2 ≤ BF < 20 (0.5 ≤ log₁₀ BF < 1.3) — positive evidence.
    Positive,
    /// 20 ≤ BF < 150 (1.3 ≤ log₁₀ BF < 2.2) — strong evidence.
    Strong,
    /// BF ≥ 150 (log₁₀ BF ≥ 2.2) — very strong evidence.
    VeryStrong,
}

impl EvidenceStrength {
    /// Classifies a log₁₀ Bayes factor into a strength category.
    #[must_use]
    pub fn from_log10_bf(log10_bf: f64) -> Self {
        if log10_bf.is_nan() {
            Self::Negligible
        } else if log10_bf < 0.0 {
            Self::Against
        } else if log10_bf < 0.5 {
            Self::Negligible
        } else if log10_bf < 1.3 {
            Self::Positive
        } else if log10_bf < 2.2 {
            Self::Strong
        } else {
            Self::VeryStrong
        }
    }

    /// Returns a short label for the strength.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Against => "against",
            Self::Negligible => "negligible",
            Self::Positive => "positive",
            Self::Strong => "strong",
            Self::VeryStrong => "very strong",
        }
    }
}

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

// ---------------------------------------------------------------------------
// Bayes factor
// ---------------------------------------------------------------------------

/// Bayes factor for an invariant hypothesis.
///
/// `BF = P(data | H_violation) / P(data | H_holds)`.
///
/// Stored as `log₁₀(BF)` for numerical stability.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BayesFactor {
    /// `log₁₀(BF)`. Positive ⇒ evidence for violation, negative ⇒ evidence for clean.
    pub log10_bf: f64,
    /// The hypothesis being tested.
    pub hypothesis: String,
    /// Classified evidence strength.
    pub strength: EvidenceStrength,
}

impl BayesFactor {
    /// Computes a Bayes factor from explicit log-likelihoods.
    ///
    /// `log10_bf = log₁₀ P(data | H1) − log₁₀ P(data | H0)`
    #[must_use]
    pub fn from_log_likelihoods(
        log10_likelihood_h1: f64,
        log10_likelihood_h0: f64,
        hypothesis: String,
    ) -> Self {
        let log10_bf = log10_likelihood_h1 - log10_likelihood_h0;
        Self {
            log10_bf,
            hypothesis,
            strength: EvidenceStrength::from_log10_bf(log10_bf),
        }
    }

    /// Returns the raw Bayes factor (10^log10_bf), clamped to avoid infinity.
    #[must_use]
    pub fn value(&self) -> f64 {
        10.0_f64.powf(self.log10_bf.clamp(-300.0, 300.0))
    }
}

// ---------------------------------------------------------------------------
// Log-likelihood contributions
// ---------------------------------------------------------------------------

/// Decomposed log-likelihood contributions from different evidence sources.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogLikelihoodContributions {
    /// Contribution from structural observations (entity counts, topology).
    pub structural: f64,
    /// Contribution from the detection signal (violation present/absent).
    pub detection: f64,
    /// Aggregate (sum of components).
    pub total: f64,
}

// ---------------------------------------------------------------------------
// Evidence line — the "galaxy-brain" unit
// ---------------------------------------------------------------------------

/// A single evidence line: equation + substituted values + one-line intuition.
///
/// # Example
///
/// ```text
/// equation:     BF = P(violation_observed | leak) / P(violation_observed | clean)
/// substitution: BF = 0.998 / 0.001 = 998.0
/// intuition:    Very strong evidence of task leak in region R1 (3 tasks tracked)
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceLine {
    /// The general equation form.
    pub equation: String,
    /// The equation with concrete values substituted.
    pub substitution: String,
    /// One-line human-readable intuition.
    pub intuition: String,
}

// ---------------------------------------------------------------------------
// Per-invariant evidence entry
// ---------------------------------------------------------------------------

/// Evidence entry for a single invariant within the ledger.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceEntry {
    /// Invariant name (e.g., "task_leak").
    pub invariant: String,
    /// Whether the invariant passed.
    pub passed: bool,
    /// Bayes factor for the violation hypothesis.
    pub bayes_factor: BayesFactor,
    /// Decomposed log-likelihood contributions.
    pub log_likelihoods: LogLikelihoodContributions,
    /// Evidence lines (equations + substitutions + intuitions).
    pub evidence_lines: Vec<EvidenceLine>,
}

// ---------------------------------------------------------------------------
// Evidence summary
// ---------------------------------------------------------------------------

/// Aggregate summary across all invariants.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceSummary {
    /// Total invariants examined.
    pub total_invariants: usize,
    /// Number where a violation was detected.
    pub violations_detected: usize,
    /// Invariant with the strongest evidence of violation (if any).
    pub strongest_violation: Option<String>,
    /// Invariant with the strongest evidence of being clean (if any violations exist).
    pub strongest_clean: Option<String>,
    /// Sum of log₁₀ BF across all invariants with violations.
    pub aggregate_log10_bf: f64,
}

// ---------------------------------------------------------------------------
// Evidence ledger
// ---------------------------------------------------------------------------

/// The evidence ledger: structured Bayesian explanation of oracle results.
///
/// Constructed from an [`OracleReport`] via [`EvidenceLedger::from_report`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceLedger {
    /// Per-invariant evidence entries.
    pub entries: Vec<EvidenceEntry>,
    /// Aggregate summary.
    pub summary: EvidenceSummary,
    /// Check time in nanoseconds (inherited from the oracle report).
    pub check_time_nanos: u64,
}

// ---------------------------------------------------------------------------
// Bayes factor computation model
// ---------------------------------------------------------------------------

/// Detection model parameters for computing Bayes factors.
///
/// These encode assumptions about oracle sensitivity.
#[derive(Debug, Clone)]
pub struct DetectionModel {
    /// Per-entity detection probability when the invariant is violated.
    /// Default: 0.9 (oracle detects 90% of single-entity violations).
    pub per_entity_detection_rate: f64,
    /// False-positive rate: probability of observing a violation when the
    /// invariant actually holds.  Default: 0.001.
    pub false_positive_rate: f64,
}

impl Default for DetectionModel {
    fn default() -> Self {
        Self {
            per_entity_detection_rate: 0.9,
            false_positive_rate: 0.001,
        }
    }
}

impl DetectionModel {
    /// Probability of observing a violation given that one exists,
    /// as a function of the number of tracked entities.
    ///
    /// `P(violation_observed | H_violated) = 1 − (1 − p)^n`
    ///
    /// More entities → higher detection probability.
    #[must_use]
    pub fn p_detection_given_violation(&self, entities_tracked: usize) -> f64 {
        let n = f64::from(entities_tracked.max(1).min(u32::MAX as usize) as u32);
        1.0 - (1.0 - self.per_entity_detection_rate).powf(n)
    }

    /// Probability of observing a pass given that the invariant holds.
    ///
    /// `P(pass | H_holds) = 1 − ε`
    #[must_use]
    pub fn p_pass_given_clean(&self) -> f64 {
        1.0 - self.false_positive_rate
    }

    /// Probability of observing a pass given that a violation exists.
    ///
    /// `P(pass | H_violated) = (1 − p)^n`
    #[must_use]
    pub fn p_pass_given_violation(&self, entities_tracked: usize) -> f64 {
        let n = f64::from(entities_tracked.max(1).min(u32::MAX as usize) as u32);
        (1.0 - self.per_entity_detection_rate).powf(n)
    }

    /// Computes a [`BayesFactor`] and evidence lines for an invariant.
    #[must_use]
    pub fn compute_evidence(
        &self,
        invariant: &str,
        passed: bool,
        stats: &OracleStats,
    ) -> (BayesFactor, LogLikelihoodContributions, Vec<EvidenceLine>) {
        let n = stats.entities_tracked;

        if passed {
            // Observed: pass.  BF for "holds" = P(pass|holds) / P(pass|violated)
            let p_h0 = self.p_pass_given_clean();
            let p_h1 = self.p_pass_given_violation(n);
            // The BF for the violation hypothesis should stay below 1 for a clean
            // observation, so structural support must reinforce the clean outcome
            // instead of always pushing toward violation.
            let detection = p_h1.log10() - p_h0.log10();
            let structural = -structural_contribution(stats);
            let total = structural + detection;

            let bf_val = 10.0_f64.powf(total.clamp(-300.0, 300.0));

            let bf = BayesFactor {
                log10_bf: total,
                hypothesis: format!("{invariant} violated"),
                strength: EvidenceStrength::from_log10_bf(total),
            };

            let lines = vec![
                EvidenceLine {
                    equation: "BF_violation = P(pass | violated) / P(pass | holds)".into(),
                    substitution: format!("BF = {p_h1:.6} / {p_h0:.6} = {bf_val:.4}"),
                    intuition: format!(
                        "{} evidence against '{invariant}' violation ({n} entities tracked, oracle saw pass)",
                        bf.strength.label().to_uppercase(),
                    ),
                },
                EvidenceLine {
                    equation: "P(pass | violated) = (1 − p)^n".into(),
                    substitution: format!(
                        "P(pass | violated) = (1 − {:.2})^{n} = {:.6}",
                        self.per_entity_detection_rate, p_h1,
                    ),
                    intuition: format!(
                        "With {n} entities, a real violation would be missed with probability {p_h1:.6}",
                    ),
                },
            ];

            let ll = LogLikelihoodContributions {
                structural,
                detection,
                total,
            };

            (bf, ll, lines)
        } else {
            // Observed: violation.  BF for "violated" = P(violation|violated) / P(violation|holds)
            let p_h1 = self.p_detection_given_violation(n);
            let p_h0 = self.false_positive_rate;
            let log10_h1 = p_h1.log10();
            let log10_h0 = p_h0.log10();

            let structural = structural_contribution(stats);
            let detection = log10_h1 - log10_h0;
            let total = structural + detection;

            let bf_val = 10.0_f64.powf(total.clamp(-300.0, 300.0));

            let bf = BayesFactor {
                log10_bf: total,
                hypothesis: format!("{invariant} violated"),
                strength: EvidenceStrength::from_log10_bf(total),
            };

            let lines = vec![
                EvidenceLine {
                    equation:
                        "BF_violation = P(violation_observed | violated) / P(violation_observed | holds)"
                            .into(),
                    substitution: format!("BF = {p_h1:.6} / {p_h0:.6} = {bf_val:.1}"),
                    intuition: format!(
                        "{} evidence that '{invariant}' is violated ({n} entities tracked, violation observed)",
                        bf.strength.label().to_uppercase(),
                    ),
                },
                EvidenceLine {
                    equation: "P(violation_observed | violated) = 1 − (1 − p)^n".into(),
                    substitution: format!(
                        "P(detected | violated) = 1 − (1 − {:.2})^{n} = {:.6}",
                        self.per_entity_detection_rate, p_h1,
                    ),
                    intuition: format!(
                        "With {n} entities, a real violation would be detected with probability {p_h1:.6}",
                    ),
                },
            ];

            let ll = LogLikelihoodContributions {
                structural,
                detection,
                total,
            };

            (bf, ll, lines)
        }
    }
}

/// Small structural evidence contribution from event counts.
///
/// More events ⇒ marginally more confident in whatever conclusion was reached.
/// Uses `log₁₀(1 + events / 100)` as a mild bonus (< 0.1 for typical runs).
fn structural_contribution(stats: &OracleStats) -> f64 {
    let events = stats.events_recorded.min(u32::MAX as usize) as u32;
    (1.0 + f64::from(events) / 100.0).log10()
}

// ---------------------------------------------------------------------------
// EvidenceLedger construction
// ---------------------------------------------------------------------------

impl EvidenceLedger {
    /// Constructs an evidence ledger from an oracle report using the default
    /// detection model.
    #[must_use]
    pub fn from_report(report: &OracleReport) -> Self {
        Self::from_report_with_model(report, &DetectionModel::default())
    }

    /// Constructs an evidence ledger from an oracle report using a custom
    /// detection model.
    #[must_use]
    pub fn from_report_with_model(report: &OracleReport, model: &DetectionModel) -> Self {
        let entries: Vec<EvidenceEntry> = report
            .entries
            .iter()
            .map(|entry| {
                let (bf, ll, lines) =
                    model.compute_evidence(&entry.invariant, entry.passed, &entry.stats);
                EvidenceEntry {
                    invariant: entry.invariant.clone(),
                    passed: entry.passed,
                    bayes_factor: bf,
                    log_likelihoods: ll,
                    evidence_lines: lines,
                }
            })
            .collect();

        let summary = Self::compute_summary(&entries);
        Self {
            entries,
            summary,
            check_time_nanos: report.check_time_nanos,
        }
    }

    fn compute_summary(entries: &[EvidenceEntry]) -> EvidenceSummary {
        let total_invariants = entries.len();
        let violations_detected = entries.iter().filter(|e| !e.passed).count();

        let strongest_violation = entries
            .iter()
            .filter(|e| !e.passed)
            .max_by(|a, b| {
                a.bayes_factor
                    .log10_bf
                    .partial_cmp(&b.bayes_factor.log10_bf)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|e| e.invariant.clone());

        let strongest_clean = entries
            .iter()
            .filter(|e| e.passed)
            .min_by(|a, b| {
                a.bayes_factor
                    .log10_bf
                    .partial_cmp(&b.bayes_factor.log10_bf)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|e| e.invariant.clone());

        let aggregate_log10_bf: f64 = entries
            .iter()
            .filter(|e| !e.passed)
            .map(|e| e.bayes_factor.log10_bf)
            .sum();

        EvidenceSummary {
            total_invariants,
            violations_detected,
            strongest_violation,
            strongest_clean,
            aggregate_log10_bf,
        }
    }

    /// Returns entries with violations, sorted by descending evidence strength.
    #[must_use]
    pub fn violations_by_strength(&self) -> Vec<&EvidenceEntry> {
        let mut v: Vec<_> = self.entries.iter().filter(|e| !e.passed).collect();
        v.sort_by(|a, b| {
            b.bayes_factor
                .log10_bf
                .partial_cmp(&a.bayes_factor.log10_bf)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        v
    }

    /// Returns entries for clean invariants, sorted by ascending log₁₀ BF
    /// (most confident first, i.e. most negative).
    #[must_use]
    pub fn clean_by_confidence(&self) -> Vec<&EvidenceEntry> {
        let mut v: Vec<_> = self.entries.iter().filter(|e| e.passed).collect();
        v.sort_by(|a, b| {
            a.bayes_factor
                .log10_bf
                .partial_cmp(&b.bayes_factor.log10_bf)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        v
    }

    /// Renders the ledger as structured text (the "galaxy-brain" output).
    #[must_use]
    pub fn to_text(&self) -> String {
        let mut out = String::new();

        let _ = writeln!(
            &mut out,
            "╔══════════════════════════════════════════════════╗"
        );
        let _ = writeln!(
            &mut out,
            "║          EVIDENCE LEDGER — ORACLE DIAGNOSTICS    ║"
        );
        let _ = writeln!(
            &mut out,
            "╚══════════════════════════════════════════════════╝"
        );
        let _ = writeln!(&mut out);
        let _ = writeln!(
            &mut out,
            "  Invariants examined: {}",
            self.summary.total_invariants
        );
        let _ = writeln!(
            &mut out,
            "  Violations detected: {}",
            self.summary.violations_detected
        );
        if let Some(ref s) = self.summary.strongest_violation {
            let _ = writeln!(&mut out, "  Strongest violation: {s}");
        }
        let _ = writeln!(
            &mut out,
            "  Aggregate log₁₀(BF): {:.3}",
            self.summary.aggregate_log10_bf
        );
        let _ = writeln!(&mut out, "  Check time: {}ns", self.check_time_nanos);
        let _ = writeln!(&mut out);

        // Violations first.
        let violations = self.violations_by_strength();
        if !violations.is_empty() {
            let _ = writeln!(
                &mut out,
                "── VIOLATIONS ──────────────────────────────────────"
            );
            for entry in violations {
                write_entry(&mut out, entry);
            }
        }

        // Clean invariants.
        let clean = self.clean_by_confidence();
        if !clean.is_empty() {
            let _ = writeln!(
                &mut out,
                "── CLEAN INVARIANTS ────────────────────────────────"
            );
            for entry in clean {
                write_entry(&mut out, entry);
            }
        }

        out
    }

    /// Serializes the ledger to a JSON value.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap_or_default()
    }
}

fn write_entry(out: &mut String, entry: &EvidenceEntry) {
    let status = if entry.passed { "PASS" } else { "FAIL" };
    let _ = writeln!(out);
    let _ = writeln!(
        out,
        "  [{status}] {inv}  (BF = {bf:.2}, strength = {strength})",
        inv = entry.invariant,
        bf = entry.bayes_factor.value(),
        strength = entry.bayes_factor.strength,
    );
    let _ = writeln!(
        out,
        "        log₁₀(BF) = {:.4}  [structural={:.4}, detection={:.4}]",
        entry.log_likelihoods.total,
        entry.log_likelihoods.structural,
        entry.log_likelihoods.detection,
    );

    for (i, line) in entry.evidence_lines.iter().enumerate() {
        let _ = writeln!(out, "        ({}) {}", i + 1, line.equation);
        let _ = writeln!(out, "{}", line.substitution);
        let _ = writeln!(out, "{}", line.intuition);
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lab::oracle::{OracleEntryReport, OracleReport};

    fn make_clean_report() -> OracleReport {
        OracleReport {
            entries: vec![
                OracleEntryReport {
                    invariant: "task_leak".into(),
                    passed: true,
                    violation: None,
                    stats: OracleStats {
                        entities_tracked: 5,
                        events_recorded: 10,
                    },
                },
                OracleEntryReport {
                    invariant: "obligation_leak".into(),
                    passed: true,
                    violation: None,
                    stats: OracleStats {
                        entities_tracked: 3,
                        events_recorded: 6,
                    },
                },
            ],
            total: 2,
            passed: 2,
            failed: 0,
            check_time_nanos: 42,
        }
    }

    fn make_violation_report() -> OracleReport {
        OracleReport {
            entries: vec![
                OracleEntryReport {
                    invariant: "task_leak".into(),
                    passed: false,
                    violation: Some("leaked 2 tasks".into()),
                    stats: OracleStats {
                        entities_tracked: 5,
                        events_recorded: 10,
                    },
                },
                OracleEntryReport {
                    invariant: "quiescence".into(),
                    passed: true,
                    violation: None,
                    stats: OracleStats {
                        entities_tracked: 2,
                        events_recorded: 4,
                    },
                },
                OracleEntryReport {
                    invariant: "obligation_leak".into(),
                    passed: false,
                    violation: Some("leaked 1 obligation".into()),
                    stats: OracleStats {
                        entities_tracked: 1,
                        events_recorded: 2,
                    },
                },
            ],
            total: 3,
            passed: 1,
            failed: 2,
            check_time_nanos: 100,
        }
    }

    // -- EvidenceStrength classification --

    #[test]
    fn strength_from_log10_bf() {
        assert_eq!(
            EvidenceStrength::from_log10_bf(-1.0),
            EvidenceStrength::Against
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(0.0),
            EvidenceStrength::Negligible
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(0.49),
            EvidenceStrength::Negligible
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(0.5),
            EvidenceStrength::Positive
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(1.29),
            EvidenceStrength::Positive
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(1.3),
            EvidenceStrength::Strong
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(2.19),
            EvidenceStrength::Strong
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(2.2),
            EvidenceStrength::VeryStrong
        );
        assert_eq!(
            EvidenceStrength::from_log10_bf(5.0),
            EvidenceStrength::VeryStrong
        );
    }

    #[test]
    fn strength_labels() {
        assert_eq!(EvidenceStrength::Against.label(), "against");
        assert_eq!(EvidenceStrength::Negligible.label(), "negligible");
        assert_eq!(EvidenceStrength::Positive.label(), "positive");
        assert_eq!(EvidenceStrength::Strong.label(), "strong");
        assert_eq!(EvidenceStrength::VeryStrong.label(), "very strong");
    }

    #[test]
    fn strength_display() {
        assert_eq!(format!("{}", EvidenceStrength::Strong), "strong");
    }

    // -- BayesFactor --

    #[test]
    fn bayes_factor_from_log_likelihoods() {
        let bf = BayesFactor::from_log_likelihoods(-0.5, -3.0, "test".into());
        assert!((bf.log10_bf - 2.5).abs() < 1e-10);
        assert_eq!(bf.strength, EvidenceStrength::VeryStrong);
    }

    #[test]
    fn bayes_factor_value() {
        let bf = BayesFactor::from_log_likelihoods(0.0, -2.0, "test".into());
        // log10_bf = 2.0, so value = 100.0
        assert!((bf.value() - 100.0).abs() < 1e-6);
    }

    #[test]
    fn bayes_factor_value_clamped() {
        // Extreme value should not produce infinity.
        let bf = BayesFactor {
            log10_bf: 1000.0,
            hypothesis: "extreme".into(),
            strength: EvidenceStrength::VeryStrong,
        };
        assert!(bf.value().is_finite());
    }

    // -- DetectionModel --

    #[test]
    fn detection_model_default() {
        let m = DetectionModel::default();
        assert!((m.per_entity_detection_rate - 0.9).abs() < 1e-10);
        assert!((m.false_positive_rate - 0.001).abs() < 1e-10);
    }

    #[test]
    fn detection_model_p_detection_single_entity() {
        let m = DetectionModel::default();
        let p = m.p_detection_given_violation(1);
        assert!((p - 0.9).abs() < 1e-10);
    }

    #[test]
    fn detection_model_p_detection_multiple_entities() {
        let m = DetectionModel::default();
        // With 2 entities: 1 - (1 - 0.9)^2 = 1 - 0.01 = 0.99
        let p = m.p_detection_given_violation(2);
        assert!((p - 0.99).abs() < 1e-10);
    }

    #[test]
    fn detection_model_p_detection_zero_entities_uses_one() {
        let m = DetectionModel::default();
        let p = m.p_detection_given_violation(0);
        assert!(
            (p - 0.9).abs() < 1e-10,
            "zero entities should be treated as 1"
        );
    }

    #[test]
    fn detection_model_p_pass_given_clean() {
        let m = DetectionModel::default();
        assert!((m.p_pass_given_clean() - 0.999).abs() < 1e-10);
    }

    #[test]
    fn detection_model_p_pass_given_violation() {
        let m = DetectionModel::default();
        // (1 - 0.9)^1 = 0.1
        assert!((m.p_pass_given_violation(1) - 0.1).abs() < 1e-10);
        // (1 - 0.9)^2 = 0.01
        assert!((m.p_pass_given_violation(2) - 0.01).abs() < 1e-10);
    }

    // -- structural_contribution --

    #[test]
    fn structural_contribution_zero_events() {
        let s = structural_contribution(&OracleStats {
            entities_tracked: 0,
            events_recorded: 0,
        });
        assert!((s - 0.0_f64.log10()).abs() < 1e-10 || (s - (1.0_f64).log10()).abs() < 1e-10);
        // log10(1 + 0/100) = log10(1) = 0
        assert!(s.abs() < 1e-10);
    }

    #[test]
    fn structural_contribution_increases_with_events() {
        let s1 = structural_contribution(&OracleStats {
            entities_tracked: 0,
            events_recorded: 10,
        });
        let s2 = structural_contribution(&OracleStats {
            entities_tracked: 0,
            events_recorded: 100,
        });
        assert!(s2 > s1);
    }

    #[test]
    fn clean_entry_stays_against_violation_even_with_many_events() {
        let report = OracleReport {
            entries: vec![OracleEntryReport {
                invariant: "task_leak".into(),
                passed: true,
                violation: None,
                stats: OracleStats {
                    entities_tracked: 1,
                    events_recorded: 1_000_000,
                },
            }],
            total: 1,
            passed: 1,
            failed: 0,
            check_time_nanos: 7,
        };

        let ledger = EvidenceLedger::from_report(&report);
        let entry = &ledger.entries[0];

        assert!(
            entry.bayes_factor.log10_bf < 0.0,
            "clean pass must remain evidence against violation even with large event counts"
        );
        assert!(
            entry.log_likelihoods.structural < 0.0,
            "clean pass should record structural support in the clean direction"
        );
        assert_eq!(entry.bayes_factor.strength, EvidenceStrength::Against);
        assert!(
            entry.evidence_lines[0].intuition.contains("against"),
            "clean intuition should explicitly describe evidence against violation"
        );
    }

    // -- EvidenceLedger from clean report --

    #[test]
    fn ledger_from_clean_report() {
        let report = make_clean_report();
        let ledger = EvidenceLedger::from_report(&report);

        assert_eq!(ledger.entries.len(), 2);
        assert_eq!(ledger.summary.total_invariants, 2);
        assert_eq!(ledger.summary.violations_detected, 0);
        assert!(ledger.summary.strongest_violation.is_none());
        assert!((ledger.summary.aggregate_log10_bf).abs() < 1e-10);
        assert_eq!(ledger.check_time_nanos, 42);
    }

    #[test]
    fn ledger_clean_entries_have_negative_bf() {
        let report = make_clean_report();
        let ledger = EvidenceLedger::from_report(&report);

        for entry in &ledger.entries {
            assert!(entry.passed);
            // BF for "violation" should be < 1 (log10 < 0) since oracle passed.
            assert!(
                entry.bayes_factor.log10_bf < 0.0,
                "clean entry '{inv}' should have BF < 1, got log10_bf={bf}",
                inv = entry.invariant,
                bf = entry.bayes_factor.log10_bf,
            );
            assert_eq!(entry.bayes_factor.strength, EvidenceStrength::Against);
        }
    }

    #[test]
    fn ledger_clean_evidence_lines() {
        let report = make_clean_report();
        let ledger = EvidenceLedger::from_report(&report);

        for entry in &ledger.entries {
            assert!(
                !entry.evidence_lines.is_empty(),
                "entry should have evidence lines"
            );
            // Check first line references the equation.
            assert!(
                entry.evidence_lines[0]
                    .equation
                    .contains("P(pass | violated)")
            );
        }
    }

    // -- EvidenceLedger from violation report --

    #[test]
    fn ledger_from_violation_report() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        assert_eq!(ledger.entries.len(), 3);
        assert_eq!(ledger.summary.total_invariants, 3);
        assert_eq!(ledger.summary.violations_detected, 2);
        assert!(ledger.summary.strongest_violation.is_some());
        assert!(ledger.summary.aggregate_log10_bf > 0.0);
    }

    #[test]
    fn ledger_violation_entries_have_positive_bf() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        for entry in ledger.entries.iter().filter(|e| !e.passed) {
            assert!(
                entry.bayes_factor.log10_bf > 0.0,
                "violation entry '{inv}' should have BF > 1, got log10_bf={bf}",
                inv = entry.invariant,
                bf = entry.bayes_factor.log10_bf,
            );
        }
    }

    #[test]
    fn ledger_violation_evidence_lines() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        let task_entry = ledger
            .entries
            .iter()
            .find(|e| e.invariant == "task_leak")
            .unwrap();
        assert!(!task_entry.passed);
        assert!(
            task_entry.evidence_lines[0]
                .equation
                .contains("P(violation_observed | violated)")
        );
    }

    // -- violations_by_strength --

    #[test]
    fn violations_by_strength_ordering() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);
        let violations = ledger.violations_by_strength();

        assert_eq!(violations.len(), 2);
        // Stronger evidence (more entities) should come first.
        assert!(violations[0].bayes_factor.log10_bf >= violations[1].bayes_factor.log10_bf);
    }

    // -- clean_by_confidence --

    #[test]
    fn clean_by_confidence_ordering() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);
        let clean = ledger.clean_by_confidence();

        assert_eq!(clean.len(), 1);
        assert_eq!(clean[0].invariant, "quiescence");
    }

    // -- text output --

    #[test]
    fn ledger_to_text_contains_header() {
        let report = make_clean_report();
        let ledger = EvidenceLedger::from_report(&report);
        let text = ledger.to_text();

        assert!(text.contains("EVIDENCE LEDGER"));
        assert!(text.contains("Invariants examined: 2"));
        assert!(text.contains("Violations detected: 0"));
        assert!(text.contains("CLEAN INVARIANTS"));
    }

    #[test]
    fn ledger_to_text_violations_section() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);
        let text = ledger.to_text();

        assert!(text.contains("VIOLATIONS"));
        assert!(text.contains("[FAIL] task_leak"));
        assert!(text.contains("[FAIL] obligation_leak"));
        assert!(text.contains("[PASS] quiescence"));
    }

    #[test]
    fn ledger_to_text_evidence_lines() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);
        let text = ledger.to_text();

        assert!(text.contains("BF ="));
        assert!(text.contains("log₁₀(BF)"));
    }

    // -- JSON serialization --

    #[test]
    fn ledger_json_roundtrip() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);
        let json = serde_json::to_string(&ledger).unwrap();
        let deserialized: EvidenceLedger = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.entries.len(), ledger.entries.len());
        assert_eq!(
            deserialized.summary.violations_detected,
            ledger.summary.violations_detected
        );
        assert_eq!(deserialized.check_time_nanos, ledger.check_time_nanos);
    }

    #[test]
    fn ledger_to_json_structure() {
        let report = make_clean_report();
        let ledger = EvidenceLedger::from_report(&report);
        let json = ledger.to_json();

        assert!(json["entries"].is_array());
        assert!(json["summary"].is_object());
        assert_eq!(json["summary"]["total_invariants"], 2);
        assert_eq!(json["check_time_nanos"], 42);
    }

    // -- custom detection model --

    #[test]
    fn custom_detection_model() {
        let model = DetectionModel {
            per_entity_detection_rate: 0.5,
            false_positive_rate: 0.01,
        };
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report_with_model(&report, &model);

        // With lower detection rate, BF for violations should be smaller.
        let default_ledger = EvidenceLedger::from_report(&report);
        for (custom, default) in ledger
            .entries
            .iter()
            .zip(default_ledger.entries.iter())
            .filter(|(_, d)| !d.passed)
        {
            assert!(
                custom.bayes_factor.log10_bf < default.bayes_factor.log10_bf,
                "lower detection rate should produce weaker evidence"
            );
        }
    }

    // -- log-likelihood contributions --

    #[test]
    fn log_likelihood_components_sum_to_total() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        for entry in &ledger.entries {
            let expected_total = entry.log_likelihoods.structural + entry.log_likelihoods.detection;
            assert!(
                (entry.log_likelihoods.total - expected_total).abs() < 1e-10,
                "total should equal structural + detection"
            );
        }
    }

    // -- integration with OracleSuite --

    #[test]
    fn ledger_from_oracle_suite() {
        let mut suite = super::super::OracleSuite::new();
        let report = suite.report(crate::types::Time::ZERO);
        let ledger = EvidenceLedger::from_report(&report);

        // 24 core oracles; 4 more with messaging-fabric feature.
        #[cfg(not(feature = "messaging-fabric"))]
        assert_eq!(ledger.entries.len(), 24);
        #[cfg(feature = "messaging-fabric")]
        assert_eq!(ledger.entries.len(), 28);
        assert_eq!(ledger.summary.violations_detected, 0);
        // All should show evidence against violation.
        for entry in &ledger.entries {
            assert!(entry.passed);
            assert_eq!(entry.bayes_factor.strength, EvidenceStrength::Against);
        }
    }

    // -- EvidenceEntry struct --

    #[test]
    fn evidence_entry_fields() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        let task_entry = &ledger.entries[0];
        assert_eq!(task_entry.invariant, "task_leak");
        assert!(!task_entry.passed);
        assert!(!task_entry.evidence_lines.is_empty());
        assert!(task_entry.bayes_factor.log10_bf.is_finite());
        assert!(task_entry.log_likelihoods.total.is_finite());
    }

    // -- EvidenceSummary --

    #[test]
    fn evidence_summary_strongest_violation() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        // task_leak has 5 entities, obligation_leak has 1 — task_leak should be strongest.
        assert_eq!(
            ledger.summary.strongest_violation.as_deref(),
            Some("task_leak")
        );
    }

    #[test]
    fn evidence_summary_strongest_clean() {
        let report = make_violation_report();
        let ledger = EvidenceLedger::from_report(&report);

        assert_eq!(
            ledger.summary.strongest_clean.as_deref(),
            Some("quiescence")
        );
    }
}