veridict 0.19.7

Statistical regression gate for A/B and hypothesis testing: compare candidate vs baseline results with win-rate, Elo, SPRT, and bootstrap confidence intervals.
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
//! `verify-run`: checks that a completed run's raw data (a `manifest.toml` of declared
//! expectations plus a `games.jsonl` of per-record observations) is structurally sound *before*
//! any statistics (`compare`/`sprt`) are trusted on it - pairing, ordering, contamination, and
//! opaque-identifier consistency, not a metric or a verdict.
//!
//! Domain-agnostic by the same rule the rest of this crate follows (see `AGENTS.md`): every
//! hash/id field here is an opaque caller-supplied string, compared for equality/no-drift only -
//! this module never opens, hashes, or interprets an actual binary/weight/corpus file, and never
//! assigns domain meaning to a field's contents. `role` is the domain-agnostic stand-in for
//! "color/side reversed within a pair": two records sharing an `id` must declare two different
//! `role` values, whatever the caller's own convention for `role` is.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::Validity;
use crate::error::VeridictError;
use crate::metrics::{FailureBreakdown, tally_status};

/// Versions `Manifest`'s own (non-envelope) shape - distinct from `report::REPORT_SCHEMA_VERSION`
/// (which versions `VerifyRunReport`'s output shape) and from the envelope's own `schema_version`
/// field below (which versions the four-repo envelope contract, echoed but never validated by
/// veridict). Three different documents, three different version numbers.
pub const MANIFEST_SCHEMA_VERSION: u32 = 1;

/// Cap on how many `Violation`s `VerifyRunReport::violations` ever carries - a badly corrupted
/// run (e.g. every pair duplicated) could otherwise produce a report proportional to the whole
/// input. `violation_count` always holds the exact total regardless of this cap; the emitted
/// sample is deterministic (the first `MAX_REPORTED_VIOLATIONS` after `run`'s own sort), not a
/// random subset, so the same input always reports the same sample.
const MAX_REPORTED_VIOLATIONS: usize = 500;

/// A run's declared expectations, read from `manifest.toml`. Every envelope field (see the
/// module doc) is `Option` and opaque - compared for consistency against `games.jsonl`, never
/// computed or interpreted. `#[serde(deny_unknown_fields)]` here (unlike `VerifyRunRecord`)
/// because a typo'd manifest key must not silently skip a check the caller thought they'd
/// enabled - manifest fields are more like a declared expectation than data, and this project's
/// convention is to never silently ignore invalid data.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Manifest {
    /// Required; mismatch vs `MANIFEST_SCHEMA_VERSION` is a hard error (see `parse_manifest`),
    /// not a violation - an unrecognized manifest shape can't be safely checked at all.
    pub manifest_schema_version: u32,

    // Experiment envelope fields (shared four-repo contract) - opaque, never interpreted, only
    // compared or echoed.
    /// Drives `check_experiment_contamination`.
    pub experiment_id: Option<String>,
    pub candidate_id: Option<String>,
    pub baseline_id: Option<String>,
    pub lineage_id: Option<String>,
    /// Drives one independent sub-check of `check_environment_consistency`.
    pub dataset_sha256: Option<String>,
    /// Passthrough only - no per-record counterpart this round.
    pub split_sha256: Option<String>,
    /// Passthrough only - no per-record counterpart this round.
    pub teacher_manifest_sha256: Option<String>,
    /// Drives one independent sub-check of `check_environment_consistency`.
    pub binary_sha256: Option<String>,
    /// Drives one independent sub-check of `check_environment_consistency`.
    pub weight_sha256: Option<String>,
    pub init_seed: Option<i64>,
    pub split_seed: Option<i64>,
    pub shuffle_seed: Option<i64>,
    /// Pure passthrough echo of the envelope's own `validity` slot - deliberately never feeds
    /// this report's own computed `validity` (which is purely `violations.is_empty()`). Whatever
    /// upstream stage populated this is its own claim, not something verify-run adopts as its own.
    pub validity: Option<Validity>,
    /// The ENVELOPE's own version tag (one of the 14 shared fields) - opaque passthrough, never
    /// validated by veridict. Deliberately a different field from `manifest_schema_version`
    /// above: that one is veridict-specific and enforced; this one is the four-repo envelope's
    /// own version, echoed but never interpreted, same as every other envelope field.
    pub schema_version: Option<String>,

    // Non-envelope, verify-run-specific:
    /// Drives one independent sub-check of `check_environment_consistency` - not part of the
    /// original envelope; closes the "no mid-run config change" check (time control, thread
    /// count, etc. - anything that drifts without touching a hashed artifact file).
    pub config_sha256: Option<String>,
    /// Declared order of gated pair `id`s. Drives `check_schedule_order`.
    pub schedule: Option<Vec<String>>,
}

impl Manifest {
    /// Whether any manifest-dependent check has something to check against - the three
    /// manifest-independent checks (`check_pair_completeness`/`check_global_index_uniqueness`/
    /// `check_role_consistency`) always run regardless and don't count toward this.
    fn has_anything_to_verify(&self) -> bool {
        self.schedule.is_some()
            || self.experiment_id.is_some()
            || self.dataset_sha256.is_some()
            || self.binary_sha256.is_some()
            || self.weight_sha256.is_some()
            || self.config_sha256.is_some()
    }
}

/// Parses and validates a `manifest.toml`'s text. `path` is used only for the error message (a
/// caller reading from a real file passes its path; a caller reading from an in-memory string
/// for a test can pass any label).
pub fn parse_manifest(path: &str, text: &str) -> Result<Manifest, VeridictError> {
    let manifest: Manifest = toml::from_str(text).map_err(|source| VeridictError::InvalidToml {
        path: path.to_string(),
        source,
    })?;
    if manifest.manifest_schema_version != MANIFEST_SCHEMA_VERSION {
        return Err(VeridictError::UnsupportedManifestSchemaVersion {
            found: manifest.manifest_schema_version,
            supported: MANIFEST_SCHEMA_VERSION,
        });
    }
    Ok(manifest)
}

/// One `games.jsonl` record for `verify-run`. A genuinely separate schema from `input::Record`:
/// verify-run computes no metric (no `baseline`/`candidate`/`result` numeric fields), and needs
/// several fields `Record` has no room for (`global_index`, `role`, `include_in_gate`, the
/// per-record envelope-subset fields). No `#[serde(deny_unknown_fields)]` here (unlike
/// `Manifest`): an unknown key on a data record is harmless extra caller metadata, matching
/// `input::Record`'s existing precedent.
#[derive(Debug, Clone, Deserialize)]
pub struct VerifyRunRecord {
    /// Pairing key. A non-paired record simply omits it.
    pub id: Option<String>,
    /// Caller-declared per-record sequence number. Drives `check_global_index_uniqueness`.
    pub global_index: Option<u64>,
    /// Opaque orientation tag - the domain-agnostic stand-in for "color/side reversed". Must
    /// differ within a pair (see `check_role_consistency`); whatever "role" means is the
    /// caller's business.
    pub role: Option<String>,
    /// `None`/`Some(true)` = gated (counts toward `check_schedule_order`); `Some(false)` =
    /// burn-in, excluded from the gated schedule order.
    pub include_in_gate: Option<bool>,
    pub experiment_id: Option<String>,
    pub dataset_sha256: Option<String>,
    pub binary_sha256: Option<String>,
    pub weight_sha256: Option<String>,
    pub config_sha256: Option<String>,
    /// `ok|timeout|crash|invalid` - reuses `TrialStatus`/`tally_status` verbatim.
    pub baseline_status: Option<String>,
    pub candidate_status: Option<String>,
}

/// One structural defect found in the run. `lines` is plural (unlike
/// `VeridictError::SchemaMismatch`'s single `line`) because most violations here are relational -
/// they implicate two or more records, not one.
#[derive(Debug, Serialize)]
pub struct Violation {
    pub check: &'static str,
    pub id: Option<String>,
    pub lines: Vec<usize>,
    pub field: Option<&'static str>,
    pub detail: String,
}

/// A check that had nothing to check against - structured and machine-readable, not just a
/// prose warning, so a caller can tell "this passed" from "this wasn't evaluated" without
/// parsing text. Reported instead of a silent pass (false reassurance) or a hard failure
/// (unusable until every optional field is emitted).
#[derive(Debug, Serialize)]
pub struct SkippedCheck {
    pub check: &'static str,
    pub field: Option<&'static str>,
    pub reason: String,
}

#[derive(Debug, Serialize)]
pub struct VerifyRunReport {
    pub schema_version: u32,
    /// `Valid` iff `violations` is empty. Reuses the existing `Validity` enum directly rather
    /// than inventing a parallel type - no `Verdict`-shaped three-way concept applies here (a
    /// structural invariant either holds or it doesn't; there's no "inconclusive").
    pub validity: Validity,
    pub reason: String,
    /// Sorted deterministically (see `run`'s doc), then capped at `MAX_REPORTED_VIOLATIONS` -
    /// see `violation_count`/`violations_truncated` for the exact total when this list was cut
    /// short.
    pub violations: Vec<Violation>,
    /// Exact total violation count, even when `violations` above was truncated - `validity`/
    /// `reason` are derived from this, never from `violations.len()`.
    pub violation_count: u64,
    /// `true` iff `violation_count > MAX_REPORTED_VIOLATIONS`, i.e. `violations` above is a
    /// deterministic (first-`MAX_REPORTED_VIOLATIONS`-after-sort) sample, not the full list.
    pub violations_truncated: bool,
    pub checks_skipped: Vec<SkippedCheck>,
    /// Human-readable mirror of `checks_skipped`, same "one computation, two representations"
    /// idiom `report::DataQuality`/`warnings` already use elsewhere.
    pub warnings: Vec<String>,
    pub record_count: u64,
    /// Count of `id`-groups with exactly 2 records, excluding burn-in pairs (either record
    /// marked `include_in_gate: false`) - the formal, gated pair count. Distinct from
    /// `SprtReport::paired_count`, which counts pairs a pentanomial LLR walk actually analyzed.
    pub paired_count: u64,
    pub timeouts: u64,
    pub crashes: u64,
    pub invalid: u64,
    pub failure_breakdown: FailureBreakdown,
    /// Normalized typed representation of the parsed manifest.
    pub manifest: Manifest,
}

/// Every `id`-group among `records` that carries a non-`None` `id`, regardless of
/// `include_in_gate` - pairing is a property of `id`, not of gating, so a burn-in record that
/// unexpectedly reuses a gated pair's `id` legitimately trips `check_pair_completeness`/
/// `check_role_consistency`. `BTreeMap`, not `HashMap`: iteration order must be
/// process-independent (see `run`'s doc on why the final violation order is sorted).
fn group_by_id(
    records: &[(usize, VerifyRunRecord)],
) -> BTreeMap<String, Vec<(usize, &VerifyRunRecord)>> {
    let mut groups: BTreeMap<String, Vec<(usize, &VerifyRunRecord)>> = BTreeMap::new();
    for (line, record) in records {
        if let Some(id) = &record.id {
            groups.entry(id.clone()).or_default().push((*line, record));
        }
    }
    groups
}

/// Merges the original request's "#2 pair IDs appear exactly twice" and "#5 no incomplete
/// pairs" - identical mechanism, one check.
fn check_pair_completeness(
    groups: &BTreeMap<String, Vec<(usize, &VerifyRunRecord)>>,
) -> Vec<Violation> {
    groups
        .iter()
        .filter(|(_, records)| records.len() != 2)
        .map(|(id, records)| Violation {
            check: "pair_completeness",
            id: Some(id.clone()),
            lines: records.iter().map(|(line, _)| *line).collect(),
            field: Some("id"),
            detail: format!(
                "id '{id}' appears {} time(s); pairing requires exactly 2 records per id",
                records.len()
            ),
        })
        .collect()
}

/// Merges the original request's "#4 no duplicate global index".
fn check_global_index_uniqueness(
    records: &[(usize, VerifyRunRecord)],
) -> (Vec<Violation>, Option<SkippedCheck>) {
    let mut by_index: BTreeMap<u64, Vec<usize>> = BTreeMap::new();
    for (line, record) in records {
        if let Some(index) = record.global_index {
            by_index.entry(index).or_default().push(*line);
        }
    }
    if by_index.is_empty() {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "global_index_uniqueness",
                field: Some("global_index"),
                reason: "no record declared global_index".to_string(),
            }),
        );
    }
    let violations = by_index
        .into_iter()
        .filter(|(_, lines)| lines.len() > 1)
        .map(|(index, lines)| Violation {
            check: "global_index_uniqueness",
            id: None,
            lines,
            field: Some("global_index"),
            detail: format!("global_index {index} appears on more than one record"),
        })
        .collect();
    (violations, None)
}

/// Merges the original request's "#1 candidate/baseline orientation" and "#3 color/side
/// reversal confirmed" - both are the same underlying `role` field, checked two ways: within a
/// pair (must differ) and across the whole run (only one role-pairing convention should be in
/// use). A pair whose completeness `check_pair_completeness` already flagged (size != 2) is
/// skipped here rather than double-reported for the same underlying defect.
fn check_role_consistency(
    groups: &BTreeMap<String, Vec<(usize, &VerifyRunRecord)>>,
) -> (Vec<Violation>, Option<SkippedCheck>) {
    let mut violations = Vec::new();
    let mut role_pairings: BTreeMap<(String, String), Vec<usize>> = BTreeMap::new();
    let mut any_role_seen = false;

    for (id, records) in groups {
        if records.len() != 2 {
            continue;
        }
        let (line_a, a) = records[0];
        let (line_b, b) = records[1];
        let (Some(role_a), Some(role_b)) = (&a.role, &b.role) else {
            continue;
        };
        any_role_seen = true;
        if role_a == role_b {
            violations.push(Violation {
                check: "role_consistency",
                id: Some(id.clone()),
                lines: vec![line_a, line_b],
                field: Some("role"),
                detail: format!(
                    "pair '{id}' has the same role ('{role_a}') on both records; a valid pair \
                     requires two different roles"
                ),
            });
            continue;
        }
        if a.include_in_gate == Some(false) || b.include_in_gate == Some(false) {
            // Burn-in pairs may legitimately use a different role vocabulary than gated
            // pairs; only gated pairs vote on the run's single "expected" role pairing.
            continue;
        }
        let mut combo = [role_a.clone(), role_b.clone()];
        combo.sort();
        let [first, second] = combo;
        role_pairings
            .entry((first, second))
            .or_default()
            .push(line_a);
    }

    if !any_role_seen {
        return (
            violations,
            Some(SkippedCheck {
                check: "role_consistency",
                field: Some("role"),
                reason: "no record declared role".to_string(),
            }),
        );
    }

    if role_pairings.len() > 1 {
        let combos: Vec<String> = role_pairings
            .keys()
            .map(|(a, b)| format!("{{{a}, {b}}}"))
            .collect();
        violations.push(Violation {
            check: "role_consistency",
            id: None,
            lines: role_pairings.values().flatten().copied().collect(),
            field: Some("role"),
            detail: format!(
                "more than one distinct role pairing is used across the run: {}",
                combos.join(", ")
            ),
        });
    }

    (violations, None)
}

/// The original request's "#6 permutation order match" plus the set-membership half of "#9 no
/// burn-in/other-run contamination" (a full ordered-sequence-equality check already implies set
/// equality, so a separate membership check would be redundant).
fn check_schedule_order(
    manifest: &Manifest,
    records: &[(usize, VerifyRunRecord)],
) -> (Vec<Violation>, Option<SkippedCheck>) {
    let Some(schedule) = &manifest.schedule else {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "schedule_order",
                field: Some("schedule"),
                reason: "manifest.schedule not declared".to_string(),
            }),
        );
    };

    let mut seen = std::collections::HashSet::new();
    let mut actual = Vec::new();
    for (_, record) in records {
        if record.include_in_gate == Some(false) {
            continue;
        }
        if let Some(id) = &record.id
            && seen.insert(id.clone())
        {
            actual.push(id.clone());
        }
    }

    if &actual == schedule {
        return (Vec::new(), None);
    }
    let mismatch_at = actual
        .iter()
        .zip(schedule.iter())
        .position(|(a, e)| a != e)
        .unwrap_or_else(|| actual.len().min(schedule.len()));
    let describe = |ids: &[String], at: usize| {
        ids.get(at)
            .map(|id| format!("'{id}'"))
            .unwrap_or_else(|| "<end of list>".to_string())
    };
    (
        vec![Violation {
            check: "schedule_order",
            id: None,
            lines: Vec::new(),
            field: Some("schedule"),
            detail: format!(
                "actual gated pair order diverges from manifest.schedule at position \
                 {mismatch_at} (expected {}, got {}); full expected order: {schedule:?}, full \
                 actual order: {actual:?}",
                describe(schedule, mismatch_at),
                describe(&actual, mismatch_at),
            ),
        }],
        None,
    )
}

/// The other half of "#9 no burn-in/other-run contamination": a record whose declared
/// `experiment_id` disagrees with the manifest's.
fn check_experiment_contamination(
    manifest: &Manifest,
    records: &[(usize, VerifyRunRecord)],
) -> (Vec<Violation>, Option<SkippedCheck>) {
    let Some(expected) = &manifest.experiment_id else {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "experiment_contamination",
                field: Some("experiment_id"),
                reason: "manifest.experiment_id not declared".to_string(),
            }),
        );
    };
    if !records
        .iter()
        .any(|(_, record)| record.experiment_id.is_some())
    {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "experiment_contamination",
                field: Some("experiment_id"),
                reason: "no record declared experiment_id".to_string(),
            }),
        );
    }
    let violations = records
        .iter()
        .filter_map(|(line, record)| {
            let actual = record.experiment_id.as_ref()?;
            (actual != expected).then(|| Violation {
                check: "experiment_contamination",
                id: record.id.clone(),
                lines: vec![*line],
                field: Some("experiment_id"),
                detail: format!(
                    "record's experiment_id '{actual}' does not match manifest's '{expected}'"
                ),
            })
        })
        .collect();
    (violations, None)
}

/// One opaque hash/identifier field checked for drift between the manifest's declared value and
/// every record that repeats it - the mechanism shared by all four sub-checks in
/// `check_environment_consistency`.
fn check_one_hash_field(
    field: &'static str,
    expected: &Option<String>,
    records: &[(usize, VerifyRunRecord)],
    get: impl Fn(&VerifyRunRecord) -> &Option<String>,
) -> (Vec<Violation>, Option<SkippedCheck>) {
    let Some(expected) = expected else {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "environment_consistency",
                field: Some(field),
                reason: format!("manifest.{field} not declared"),
            }),
        );
    };
    if !records.iter().any(|(_, record)| get(record).is_some()) {
        return (
            Vec::new(),
            Some(SkippedCheck {
                check: "environment_consistency",
                field: Some(field),
                reason: format!("no record declared {field}"),
            }),
        );
    }
    let violations = records
        .iter()
        .filter_map(|(line, record)| {
            let actual = get(record).as_ref()?;
            (actual != expected).then(|| Violation {
                check: "environment_consistency",
                id: record.id.clone(),
                lines: vec![*line],
                field: Some(field),
                detail: format!(
                    "record's {field} '{actual}' does not match manifest's declared '{expected}'"
                ),
            })
        })
        .collect();
    (violations, None)
}

/// Merges the original request's "#7 binary/weight/corpus hash consistency" and "#8 no mid-run
/// config change" - `config_sha256` (not part of the original envelope) closes the config-drift
/// gap the hash fields alone can't: engine configuration (time control, thread count, search
/// depth) that never touches a hashed artifact file, exactly the kind of drift that motivated
/// this feature.
type HashFieldGetter = fn(&VerifyRunRecord) -> &Option<String>;

fn check_environment_consistency(
    manifest: &Manifest,
    records: &[(usize, VerifyRunRecord)],
) -> (Vec<Violation>, Vec<SkippedCheck>) {
    let fields: [(&'static str, &Option<String>, HashFieldGetter); 4] = [
        ("dataset_sha256", &manifest.dataset_sha256, |r| {
            &r.dataset_sha256
        }),
        ("binary_sha256", &manifest.binary_sha256, |r| {
            &r.binary_sha256
        }),
        ("weight_sha256", &manifest.weight_sha256, |r| {
            &r.weight_sha256
        }),
        ("config_sha256", &manifest.config_sha256, |r| {
            &r.config_sha256
        }),
    ];
    let mut violations = Vec::new();
    let mut skipped = Vec::new();
    for (field, expected, get) in fields {
        let (v, s) = check_one_hash_field(field, expected, records, get);
        violations.extend(v);
        skipped.extend(s);
    }
    (violations, skipped)
}

/// The original request's "#10 invalid/timeout/protocol-error counts" - `protocol error` maps
/// onto the existing `invalid` status rather than a new domain-specific status variant; an
/// unrecognized status string is still the existing hard `VeridictError::UnrecognizedStatus`.
fn tally_failures(records: &[(usize, VerifyRunRecord)]) -> Result<FailureBreakdown, VeridictError> {
    let mut failures = FailureBreakdown::default();
    for (line, record) in records {
        if let Some(status) = record.baseline_status.as_deref() {
            tally_status(status, *line, "baseline_status", &mut failures.baseline)?;
        }
        if let Some(status) = record.candidate_status.as_deref() {
            tally_status(status, *line, "candidate_status", &mut failures.candidate)?;
        }
    }
    Ok(failures)
}

/// Runs every check unconditionally over the whole buffered record set, collecting *all*
/// violations rather than failing fast on the first one - the same rationale
/// `FailureBreakdown`/`DataQuality` already establish: the point of this command is diagnosing
/// everything wrong with a run in one report, not being told about one defect and having to
/// re-run to find the next.
///
/// `records` is buffered whole (not streamed) deliberately: every check here is inherently
/// cross-record (pairing needs every record sharing an `id` seen together, schedule-order needs
/// the complete first-occurrence sequence, global-index dedup needs every value at once), so
/// streaming genuinely isn't practical the way it is for `compare`/`sprt`'s incremental tallies.
///
/// Violation order is sorted by `(first line, check, field, id)` before the report is built -
/// `BTreeMap`-based grouping alone isn't sufficient, since concatenating 6 independently-ordered
/// check outputs still needs a final total order. This determinism is load-bearing for golden
/// fixture tests: any process-order-dependent ordering would make them intermittently flaky.
pub fn run(
    manifest: &Manifest,
    records: Vec<(usize, VerifyRunRecord)>,
) -> Result<VerifyRunReport, VeridictError> {
    if records.is_empty() {
        return Err(VeridictError::EmptyInput);
    }
    if !manifest.has_anything_to_verify() {
        return Err(VeridictError::ManifestDeclaresNothingToVerify);
    }

    let groups = group_by_id(&records);

    let mut violations = check_pair_completeness(&groups);
    let mut checks_skipped = Vec::new();

    let (v, s) = check_global_index_uniqueness(&records);
    violations.extend(v);
    checks_skipped.extend(s);

    let (v, s) = check_role_consistency(&groups);
    violations.extend(v);
    checks_skipped.extend(s);

    let (v, s) = check_schedule_order(manifest, &records);
    violations.extend(v);
    checks_skipped.extend(s);

    let (v, s) = check_experiment_contamination(manifest, &records);
    violations.extend(v);
    checks_skipped.extend(s);

    let (v, s) = check_environment_consistency(manifest, &records);
    violations.extend(v);
    checks_skipped.extend(s);

    violations.sort_by(|a, b| {
        let key = |v: &Violation| {
            (
                v.lines.first().copied().unwrap_or(usize::MAX),
                v.check,
                v.field.unwrap_or(""),
                v.id.clone().unwrap_or_default(),
            )
        };
        key(a).cmp(&key(b))
    });

    let failure_breakdown = tally_failures(&records)?;
    // Excludes burn-in pairs (either record marked `include_in_gate: false`) - this is the
    // *formal* (gated) pair count, the same convention `check_role_consistency` uses to keep
    // burn-in's own role vocabulary from voting on the gated run's pairing convention.
    let paired_count = groups
        .values()
        .filter(|g| {
            g.len() == 2
                && g[0].1.include_in_gate != Some(false)
                && g[1].1.include_in_gate != Some(false)
        })
        .count() as u64;
    let warnings = checks_skipped
        .iter()
        .map(|s| format!("{}: {}", s.check, s.reason))
        .collect();

    let validity = if violations.is_empty() {
        Validity::Valid
    } else {
        Validity::Invalid
    };
    let violation_count = violations.len() as u64;
    let violations_truncated = violations.len() > MAX_REPORTED_VIOLATIONS;
    let reason = if violations.is_empty() {
        "No structural violations found.".to_string()
    } else {
        format!(
            "{violation_count} structural violation(s) found; see `violations`.{}",
            if violations_truncated {
                format!(" (showing first {MAX_REPORTED_VIOLATIONS})")
            } else {
                String::new()
            }
        )
    };
    violations.truncate(MAX_REPORTED_VIOLATIONS);

    Ok(VerifyRunReport {
        schema_version: crate::report::REPORT_SCHEMA_VERSION,
        validity,
        reason,
        violations,
        violation_count,
        violations_truncated,
        checks_skipped,
        warnings,
        record_count: records.len() as u64,
        paired_count,
        timeouts: failure_breakdown.baseline.timeout + failure_breakdown.candidate.timeout,
        crashes: failure_breakdown.baseline.crash + failure_breakdown.candidate.crash,
        invalid: failure_breakdown.baseline.invalid + failure_breakdown.candidate.invalid,
        failure_breakdown,
        manifest: manifest.clone(),
    })
}

impl VerifyRunReport {
    pub fn to_json_pretty(&self) -> String {
        serde_json::to_string_pretty(self).expect(
            "VerifyRunReport contains only finite fields and strings; serialization cannot fail",
        )
    }

    pub fn to_markdown(&self) -> String {
        let b = &self.failure_breakdown.baseline;
        let c = &self.failure_breakdown.candidate;
        let mut violations = String::new();
        for v in &self.violations {
            violations.push_str(&format!(
                "- [{}] {} (lines: {:?})\n",
                v.check, v.detail, v.lines
            ));
        }
        let mut skipped = String::new();
        for s in &self.checks_skipped {
            skipped.push_str(&format!("- {}: {}\n", s.check, s.reason));
        }
        format!(
            "# Veridict Verify-Run Report\n\n\
             Validity: {validity}\n\n\
             {reason}\n\n\
             Violations ({violation_count}{truncated_note}):\n{violations}\n\
             Checks skipped ({skipped_count}):\n{skipped}\n\
             Records: {record_count}, paired: {paired_count}\n\
             Status counts:\n\
             - timeout: {timeouts} (baseline={b_timeout}, candidate={c_timeout})\n\
             - crash: {crashes} (baseline={b_crash}, candidate={c_crash})\n\
             - invalid: {invalid} (baseline={b_invalid}, candidate={c_invalid})\n",
            validity = crate::report::serde_str(&self.validity),
            reason = self.reason,
            violation_count = self.violation_count,
            truncated_note = if self.violations_truncated {
                format!(", showing first {}", self.violations.len())
            } else {
                String::new()
            },
            skipped_count = self.checks_skipped.len(),
            record_count = self.record_count,
            paired_count = self.paired_count,
            timeouts = self.timeouts,
            crashes = self.crashes,
            invalid = self.invalid,
            b_timeout = b.timeout,
            c_timeout = c.timeout,
            b_crash = b.crash,
            c_crash = c.crash,
            b_invalid = b.invalid,
            c_invalid = c.invalid,
        )
    }
}

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

    fn empty_manifest() -> Manifest {
        Manifest {
            manifest_schema_version: MANIFEST_SCHEMA_VERSION,
            experiment_id: None,
            candidate_id: None,
            baseline_id: None,
            lineage_id: None,
            dataset_sha256: None,
            split_sha256: None,
            teacher_manifest_sha256: None,
            binary_sha256: None,
            weight_sha256: None,
            init_seed: None,
            split_seed: None,
            shuffle_seed: None,
            validity: None,
            schema_version: None,
            config_sha256: None,
            schedule: None,
        }
    }

    fn record(id: &str, role: &str) -> VerifyRunRecord {
        VerifyRunRecord {
            id: Some(id.to_string()),
            global_index: None,
            role: Some(role.to_string()),
            include_in_gate: None,
            experiment_id: None,
            dataset_sha256: None,
            binary_sha256: None,
            weight_sha256: None,
            config_sha256: None,
            baseline_status: None,
            candidate_status: None,
        }
    }

    fn clean_pairs(n: usize) -> Vec<(usize, VerifyRunRecord)> {
        (0..n)
            .flat_map(|i| {
                let id = format!("op{i}");
                [(i * 2 + 1, record(&id, "a")), (i * 2 + 2, record(&id, "b"))]
            })
            .collect()
    }

    // --- parse_manifest ---

    #[test]
    fn parse_manifest_reads_a_minimal_valid_manifest() {
        let manifest = parse_manifest("m.toml", "manifest_schema_version = 1\n").unwrap();
        assert_eq!(manifest.manifest_schema_version, 1);
        assert_eq!(manifest.experiment_id, None);
    }

    #[test]
    fn parse_manifest_rejects_malformed_toml() {
        assert!(matches!(
            parse_manifest("m.toml", "not = [valid\n"),
            Err(VeridictError::InvalidToml { .. })
        ));
    }

    #[test]
    fn parse_manifest_rejects_unsupported_schema_version() {
        assert!(matches!(
            parse_manifest("m.toml", "manifest_schema_version = 999\n"),
            Err(VeridictError::UnsupportedManifestSchemaVersion {
                found: 999,
                supported: 1
            })
        ));
    }

    #[test]
    fn parse_manifest_rejects_unknown_fields() {
        assert!(matches!(
            parse_manifest(
                "m.toml",
                "manifest_schema_version = 1\ntypo_field = \"oops\"\n"
            ),
            Err(VeridictError::InvalidToml { .. })
        ));
    }

    // --- run: happy path ---

    #[test]
    fn clean_run_with_matching_schedule_is_valid() {
        let mut manifest = empty_manifest();
        manifest.schedule = Some(vec!["op0".to_string(), "op1".to_string()]);
        let records = clean_pairs(2);
        let report = run(&manifest, records).unwrap();
        assert_eq!(report.validity, Validity::Valid);
        assert!(report.violations.is_empty());
        assert_eq!(report.paired_count, 2);
        assert_eq!(report.record_count, 4);
    }

    // --- run: guard errors ---

    #[test]
    fn empty_records_is_an_error() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        assert!(matches!(
            run(&manifest, Vec::new()),
            Err(VeridictError::EmptyInput)
        ));
    }

    #[test]
    fn manifest_declaring_nothing_is_an_error() {
        let manifest = empty_manifest();
        let records = clean_pairs(1);
        assert!(matches!(
            run(&manifest, records),
            Err(VeridictError::ManifestDeclaresNothingToVerify)
        ));
    }

    // --- check_pair_completeness ---

    #[test]
    fn incomplete_pair_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = clean_pairs(2);
        records.push((100, record("lonely", "a")));
        let report = run(&manifest, records).unwrap();
        assert_eq!(report.validity, Validity::Invalid);
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "pair_completeness" && v.id.as_deref() == Some("lonely"))
        );
    }

    #[test]
    fn tripled_id_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = clean_pairs(1);
        records.push((100, record("op0", "a")));
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "pair_completeness" && v.lines.len() == 3)
        );
    }

    // --- check_role_consistency ---

    #[test]
    fn same_role_on_both_pair_members_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let records = vec![(1, record("op0", "a")), (2, record("op0", "a"))];
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "role_consistency" && v.id.as_deref() == Some("op0"))
        );
    }

    #[test]
    fn inconsistent_role_pairing_across_run_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = clean_pairs(3); // all pairs use {a, b}
        records.push((100, record("odd", "x")));
        records.push((101, record("odd", "y"))); // a different {x, y} pairing
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "role_consistency"
                    && v.detail.contains("more than one distinct role pairing"))
        );
    }

    #[test]
    fn burn_in_pair_with_a_different_role_vocabulary_is_not_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = clean_pairs(3); // all gated pairs use {a, b}
        records.push((
            100,
            VerifyRunRecord {
                include_in_gate: Some(false),
                ..record("burn_in", "x")
            },
        ));
        records.push((
            101,
            VerifyRunRecord {
                include_in_gate: Some(false),
                ..record("burn_in", "y")
            },
        ));
        let report = run(&manifest, records).unwrap();
        assert!(
            !report
                .violations
                .iter()
                .any(|v| v.check == "role_consistency")
        );
    }

    #[test]
    fn role_consistency_is_skipped_when_no_record_declares_role() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let records: Vec<_> = (0..2)
            .flat_map(|i| {
                let id = format!("op{i}");
                [
                    (
                        i * 2 + 1,
                        VerifyRunRecord {
                            role: None,
                            ..record(&id, "unused")
                        },
                    ),
                    (
                        i * 2 + 2,
                        VerifyRunRecord {
                            role: None,
                            ..record(&id, "unused")
                        },
                    ),
                ]
            })
            .collect();
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .checks_skipped
                .iter()
                .any(|s| s.check == "role_consistency")
        );
        assert!(
            !report
                .violations
                .iter()
                .any(|v| v.check == "role_consistency")
        );
    }

    // --- check_global_index_uniqueness ---

    #[test]
    fn duplicate_global_index_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let records = vec![
            (
                1,
                VerifyRunRecord {
                    global_index: Some(7),
                    ..record("op0", "a")
                },
            ),
            (
                2,
                VerifyRunRecord {
                    global_index: Some(7),
                    ..record("op0", "b")
                },
            ),
        ];
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "global_index_uniqueness")
        );
    }

    #[test]
    fn violations_beyond_the_cap_are_counted_but_not_all_emitted() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        // 600 distinct duplicated global_index values - one violation each, well past
        // MAX_REPORTED_VIOLATIONS (500).
        let records: Vec<_> = (0..600u64)
            .flat_map(|i| {
                let id = format!("op{i}");
                [
                    (
                        (i * 2 + 1) as usize,
                        VerifyRunRecord {
                            global_index: Some(i),
                            ..record(&id, "a")
                        },
                    ),
                    (
                        (i * 2 + 2) as usize,
                        VerifyRunRecord {
                            global_index: Some(i),
                            ..record(&id, "b")
                        },
                    ),
                ]
            })
            .collect();
        let report = run(&manifest, records).unwrap();
        assert_eq!(report.validity, Validity::Invalid);
        assert_eq!(report.violation_count, 600);
        assert!(report.violations_truncated);
        assert_eq!(report.violations.len(), 500);
        assert!(report.reason.contains("600"));
    }

    // --- check_schedule_order ---

    #[test]
    fn schedule_mismatch_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.schedule = Some(vec!["op1".to_string(), "op0".to_string()]); // reversed
        let records = clean_pairs(2);
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "schedule_order")
        );
    }

    #[test]
    fn burn_in_records_are_excluded_from_schedule_order() {
        let mut manifest = empty_manifest();
        manifest.schedule = Some(vec!["op0".to_string()]);
        let mut records = clean_pairs(1);
        records.push((
            100,
            VerifyRunRecord {
                include_in_gate: Some(false),
                ..record("burn_in", "a")
            },
        ));
        records.push((
            101,
            VerifyRunRecord {
                include_in_gate: Some(false),
                ..record("burn_in", "b")
            },
        ));
        let report = run(&manifest, records).unwrap();
        assert!(
            !report
                .violations
                .iter()
                .any(|v| v.check == "schedule_order")
        );
        // The burn-in pair still counts toward record_count/pair-completeness checking, but not
        // toward the formal (gated) paired_count.
        assert_eq!(report.paired_count, 1);
        assert_eq!(report.record_count, 4);
    }

    // --- check_experiment_contamination ---

    #[test]
    fn mismatched_experiment_id_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = clean_pairs(1);
        records.push((
            100,
            VerifyRunRecord {
                experiment_id: Some("exp2".to_string()),
                ..record("contaminant", "a")
            },
        ));
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "experiment_contamination")
        );
    }

    #[test]
    fn declared_experiment_id_without_record_values_is_reported_as_skipped() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let report = run(&manifest, clean_pairs(1)).unwrap();
        assert!(report.checks_skipped.iter().any(|s| {
            s.check == "experiment_contamination"
                && s.field == Some("experiment_id")
                && s.reason.contains("no record declared")
        }));
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("experiment_contamination"))
        );
    }

    // --- check_environment_consistency ---

    #[test]
    fn hash_drift_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        manifest.binary_sha256 = Some("abc123".to_string());
        let mut records = clean_pairs(1);
        records.push((
            100,
            VerifyRunRecord {
                binary_sha256: Some("different".to_string()),
                ..record("op1", "a")
            },
        ));
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "environment_consistency" && v.field == Some("binary_sha256"))
        );
    }

    #[test]
    fn config_drift_is_a_violation() {
        let mut manifest = empty_manifest();
        manifest.config_sha256 = Some("cfg1".to_string());
        let mut records = clean_pairs(1);
        records.push((
            100,
            VerifyRunRecord {
                config_sha256: Some("cfg2".to_string()),
                ..record("op1", "a")
            },
        ));
        let report = run(&manifest, records).unwrap();
        assert!(
            report
                .violations
                .iter()
                .any(|v| v.check == "environment_consistency" && v.field == Some("config_sha256"))
        );
    }

    #[test]
    fn environment_consistency_reports_a_skip_per_undeclared_field() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let report = run(&manifest, clean_pairs(1)).unwrap();
        let skipped_fields: Vec<_> = report
            .checks_skipped
            .iter()
            .filter(|s| s.check == "environment_consistency")
            .filter_map(|s| s.field)
            .collect();
        assert_eq!(skipped_fields.len(), 4); // dataset/binary/weight/config all undeclared
    }

    #[test]
    fn declared_hash_without_record_values_is_reported_as_skipped() {
        let mut manifest = empty_manifest();
        manifest.binary_sha256 = Some("expected".to_string());
        let report = run(&manifest, clean_pairs(1)).unwrap();
        assert!(report.checks_skipped.iter().any(|s| {
            s.check == "environment_consistency"
                && s.field == Some("binary_sha256")
                && s.reason.contains("no record declared")
        }));
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("binary_sha256"))
        );
    }

    // --- ordering determinism ---

    #[test]
    fn violations_are_sorted_deterministically() {
        let mut manifest = empty_manifest();
        manifest.experiment_id = Some("exp1".to_string());
        let mut records = vec![(1, record("z_lonely", "a")), (2, record("a_lonely", "a"))];
        records.truncate(2);
        // Two independent lone-id violations at different lines - assert first-line order.
        let report1 = run(&manifest, records.clone()).unwrap();
        let report2 = run(&manifest, records).unwrap();
        let lines1: Vec<_> = report1.violations.iter().map(|v| v.lines.clone()).collect();
        let lines2: Vec<_> = report2.violations.iter().map(|v| v.lines.clone()).collect();
        assert_eq!(lines1, lines2);
        assert_eq!(report1.violations[0].lines, vec![1]);
    }
}