runifold-testkit 0.5.4

Deterministic runtime testing and Agent quality evaluation for Runifold
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
use std::{
    collections::{BTreeMap, BTreeSet},
    fmt,
    future::Future,
    num::NonZeroUsize,
    pin::Pin,
    sync::Arc,
};

use futures_util::{StreamExt, stream};
use runifold_core::RunId;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

/// Owned asynchronous evaluation operation.
pub type EvaluationFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;

/// Evaluation configuration or execution failure.
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum EvaluationError {
    /// A required name or version was empty.
    #[error("{field} must not be empty")]
    EmptyField {
        /// Invalid field name.
        field: &'static str,
    },
    /// A dataset contains no cases.
    #[error("evaluation dataset must contain at least one case")]
    EmptyDataset,
    /// A rule scorer contains no rules.
    #[error("evaluation rule scorer must contain at least one rule")]
    EmptyRules,
    /// A serialized report contradicts its per-case evidence.
    #[error("evaluation report is inconsistent: {message}")]
    InconsistentReport {
        /// Stable inconsistency explanation.
        message: &'static str,
    },
    /// A runner without scorers cannot measure quality.
    #[error("evaluation runner must contain at least one scorer")]
    NoScorers,
    /// A case identifier occurs more than once.
    #[error("duplicate evaluation case id: {case_id}")]
    DuplicateCase {
        /// Duplicated case identifier.
        case_id: String,
    },
    /// A scorer name occurs more than once.
    #[error("duplicate evaluation scorer name: {scorer}")]
    DuplicateScorer {
        /// Duplicated scorer name.
        scorer: String,
    },
    /// A score or threshold is not finite and within zero through one.
    #[error("{field} must be finite and between 0 and 1, got {value}")]
    InvalidRatio {
        /// Invalid field name.
        field: &'static str,
        /// Rejected value.
        value: f64,
    },
    /// A duration or monetary metric was negative or non-finite.
    #[error("{field} must be finite and non-negative, got {value}")]
    InvalidMetric {
        /// Invalid field name.
        field: &'static str,
        /// Rejected value.
        value: f64,
    },
    /// The evaluated target failed to produce an output.
    #[error("evaluation target failed: {message}")]
    Target {
        /// Safe failure explanation.
        message: String,
    },
    /// A scorer could not evaluate one output.
    #[error("evaluation scorer {scorer} failed: {message}")]
    Scorer {
        /// Stable scorer name.
        scorer: String,
        /// Safe failure explanation.
        message: String,
    },
    /// Reports from different dataset identities cannot be compared.
    #[error(
        "evaluation dataset mismatch: baseline {baseline_name}@{baseline_version}, candidate {candidate_name}@{candidate_version}"
    )]
    DatasetMismatch {
        /// Baseline dataset name.
        baseline_name: String,
        /// Baseline dataset version.
        baseline_version: String,
        /// Candidate dataset name.
        candidate_name: String,
        /// Candidate dataset version.
        candidate_version: String,
    },
}

/// Stable case identity within a dataset.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct EvaluationCaseId(String);

impl EvaluationCaseId {
    /// Creates a non-empty case identifier.
    ///
    /// # Errors
    ///
    /// Returns [`EvaluationError::EmptyField`] for an empty identifier.
    pub fn new(value: impl Into<String>) -> Result<Self, EvaluationError> {
        let value = value.into();
        ensure_not_empty("case id", &value)?;
        Ok(Self(value))
    }

    /// Returns the identifier text.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for EvaluationCaseId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// One immutable evaluation input and its optional reference answer.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EvaluationCase {
    id: EvaluationCaseId,
    input: Value,
    expected: Option<Value>,
    tags: BTreeSet<String>,
}

impl EvaluationCase {
    /// Creates one case without a reference answer.
    ///
    /// # Errors
    ///
    /// Returns an error when `id` is empty.
    pub fn new(id: impl Into<String>, input: Value) -> Result<Self, EvaluationError> {
        Ok(Self {
            id: EvaluationCaseId::new(id)?,
            input,
            expected: None,
            tags: BTreeSet::new(),
        })
    }

    /// Adds a reference answer for deterministic scorers.
    #[must_use]
    pub fn with_expected(mut self, expected: Value) -> Self {
        self.expected = Some(expected);
        self
    }

    /// Adds a low-cardinality dataset tag.
    ///
    /// # Errors
    ///
    /// Returns an error when the tag is empty.
    pub fn with_tag(mut self, tag: impl Into<String>) -> Result<Self, EvaluationError> {
        let tag = tag.into();
        ensure_not_empty("case tag", &tag)?;
        self.tags.insert(tag);
        Ok(self)
    }

    /// Returns the case identifier.
    pub const fn id(&self) -> &EvaluationCaseId {
        &self.id
    }

    /// Returns the target input.
    pub const fn input(&self) -> &Value {
        &self.input
    }

    /// Returns the optional reference answer.
    pub const fn expected(&self) -> Option<&Value> {
        self.expected.as_ref()
    }

    /// Returns stable case tags.
    pub const fn tags(&self) -> &BTreeSet<String> {
        &self.tags
    }
}

/// Versioned, duplicate-free evaluation dataset.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EvaluationDataset {
    name: String,
    version: String,
    cases: Vec<EvaluationCase>,
}

impl EvaluationDataset {
    /// Creates a non-empty versioned dataset.
    ///
    /// # Errors
    ///
    /// Returns an error for empty fields, no cases, or duplicate case IDs.
    pub fn new(
        name: impl Into<String>,
        version: impl Into<String>,
        cases: Vec<EvaluationCase>,
    ) -> Result<Self, EvaluationError> {
        let name = name.into();
        let version = version.into();
        ensure_not_empty("dataset name", &name)?;
        ensure_not_empty("dataset version", &version)?;
        if cases.is_empty() {
            return Err(EvaluationError::EmptyDataset);
        }
        let mut ids = BTreeSet::new();
        for case in &cases {
            ensure_not_empty("case id", case.id.as_str())?;
            for tag in &case.tags {
                ensure_not_empty("case tag", tag)?;
            }
            if !ids.insert(case.id.clone()) {
                return Err(EvaluationError::DuplicateCase {
                    case_id: case.id.to_string(),
                });
            }
        }
        Ok(Self {
            name,
            version,
            cases,
        })
    }

    /// Returns the dataset name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the dataset version.
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Returns cases in stable dataset order.
    pub fn cases(&self) -> &[EvaluationCase] {
        &self.cases
    }

    /// Validates a dataset loaded from an external artifact.
    ///
    /// # Errors
    ///
    /// Returns the same invariant errors as [`Self::new`].
    pub fn validate(&self) -> Result<(), EvaluationError> {
        Self::new(&self.name, &self.version, self.cases.clone()).map(|_| ())
    }
}

/// Target output plus optional Run/Trace correlation.
#[derive(Clone, Debug)]
pub struct EvaluationOutput {
    value: Value,
    run_id: Option<RunId>,
    metadata: BTreeMap<String, Value>,
    metrics: Option<EvaluationMetrics>,
}

impl EvaluationOutput {
    /// Creates an output without Run correlation.
    pub fn new(value: Value) -> Self {
        Self {
            value,
            run_id: None,
            metadata: BTreeMap::new(),
            metrics: None,
        }
    }

    /// Correlates this output with a Runifold Run and its trace.
    #[must_use]
    pub const fn with_run_id(mut self, run_id: RunId) -> Self {
        self.run_id = Some(run_id);
        self
    }

    /// Adds scorer-visible metadata.
    ///
    /// # Errors
    ///
    /// Returns an error when the metadata key is empty.
    pub fn with_metadata(
        mut self,
        key: impl Into<String>,
        value: Value,
    ) -> Result<Self, EvaluationError> {
        let key = key.into();
        ensure_not_empty("output metadata key", &key)?;
        self.metadata.insert(key, value);
        Ok(self)
    }

    /// Returns the canonical output value.
    pub const fn value(&self) -> &Value {
        &self.value
    }

    /// Returns the correlated Run identifier.
    pub const fn run_id(&self) -> Option<RunId> {
        self.run_id
    }

    /// Returns scorer-visible metadata.
    pub const fn metadata(&self) -> &BTreeMap<String, Value> {
        &self.metadata
    }

    /// Attaches host-measured latency and optional Candidate usage.
    #[must_use]
    pub const fn with_metrics(mut self, metrics: EvaluationMetrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Returns resource evidence associated with this execution.
    pub const fn metrics(&self) -> Option<&EvaluationMetrics> {
        self.metrics.as_ref()
    }
}

/// Non-sensitive resource evidence for one successful target execution.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EvaluationMetrics {
    /// Host-observed target duration in milliseconds.
    pub duration_ms: f64,
    /// Provider-reported input tokens when available.
    pub input_tokens: Option<u64>,
    /// Provider-reported output tokens when available.
    pub output_tokens: Option<u64>,
    /// Provider- or application-reported cost in US dollars.
    pub cost_usd: Option<f64>,
}

impl EvaluationMetrics {
    /// Creates metrics with host-observed duration and no Provider usage.
    ///
    /// # Errors
    ///
    /// Returns an error when duration is negative or non-finite.
    pub fn new(duration_ms: f64) -> Result<Self, EvaluationError> {
        ensure_non_negative("evaluation duration milliseconds", duration_ms)?;
        Ok(Self {
            duration_ms,
            input_tokens: None,
            output_tokens: None,
            cost_usd: None,
        })
    }

    /// Adds Provider token usage.
    #[must_use]
    pub const fn with_tokens(mut self, input_tokens: u64, output_tokens: u64) -> Self {
        self.input_tokens = Some(input_tokens);
        self.output_tokens = Some(output_tokens);
        self
    }

    /// Adds monetary cost.
    ///
    /// # Errors
    ///
    /// Returns an error when cost is negative or non-finite.
    pub fn with_cost_usd(mut self, cost_usd: f64) -> Result<Self, EvaluationError> {
        ensure_non_negative("evaluation cost USD", cost_usd)?;
        self.cost_usd = Some(cost_usd);
        Ok(self)
    }

    fn validate(&self) -> Result<(), EvaluationError> {
        ensure_non_negative("evaluation duration milliseconds", self.duration_ms)?;
        if self.input_tokens.is_some() != self.output_tokens.is_some() {
            return Err(EvaluationError::InconsistentReport {
                message: "evaluation token metrics must include input and output together",
            });
        }
        if let Some(cost_usd) = self.cost_usd {
            ensure_non_negative("evaluation cost USD", cost_usd)?;
        }
        Ok(())
    }
}

/// Asynchronous system-under-evaluation boundary.
pub trait EvaluationTarget: Send + Sync {
    /// Executes one owned case.
    fn execute(
        &self,
        case: EvaluationCase,
    ) -> EvaluationFuture<Result<EvaluationOutput, EvaluationError>>;
}

impl<F, Fut> EvaluationTarget for F
where
    F: Fn(EvaluationCase) -> Fut + Send + Sync,
    Fut: Future<Output = Result<EvaluationOutput, EvaluationError>> + Send + 'static,
{
    fn execute(
        &self,
        case: EvaluationCase,
    ) -> EvaluationFuture<Result<EvaluationOutput, EvaluationError>> {
        Box::pin(self(case))
    }
}

/// Validated score value and optional evaluator rationale.
#[derive(Clone, Debug)]
pub struct ScoreValue {
    value: f64,
    rationale: Option<String>,
}

impl ScoreValue {
    /// Creates a finite score between zero and one.
    ///
    /// # Errors
    ///
    /// Returns [`EvaluationError::InvalidRatio`] for an invalid value.
    pub fn new(value: f64) -> Result<Self, EvaluationError> {
        ensure_ratio("score", value)?;
        Ok(Self {
            value,
            rationale: None,
        })
    }

    /// Returns the normalized score.
    pub const fn value(&self) -> f64 {
        self.value
    }

    /// Returns the optional evaluator rationale.
    pub fn rationale(&self) -> Option<&str> {
        self.rationale.as_deref()
    }

    /// Adds an evaluator rationale.
    #[must_use]
    pub fn with_rationale(mut self, rationale: impl Into<String>) -> Self {
        self.rationale = Some(rationale.into());
        self
    }
}

/// Asynchronous scorer boundary.
pub trait EvaluationScorer: Send + Sync {
    /// Stable score name.
    fn name(&self) -> &str;

    /// Per-case passing threshold.
    fn threshold(&self) -> f64;

    /// Scores one target output.
    fn score(
        &self,
        case: EvaluationCase,
        output: EvaluationOutput,
    ) -> EvaluationFuture<Result<ScoreValue, EvaluationError>>;
}

/// Closure-backed asynchronous scorer.
pub struct FnScorer<F> {
    name: String,
    threshold: f64,
    scorer: F,
}

impl<F> FnScorer<F> {
    /// Creates a scorer with a stable name and per-case threshold.
    ///
    /// # Errors
    ///
    /// Returns an error for an empty name or invalid threshold.
    pub fn new(
        name: impl Into<String>,
        threshold: f64,
        scorer: F,
    ) -> Result<Self, EvaluationError> {
        let name = name.into();
        ensure_not_empty("scorer name", &name)?;
        ensure_ratio("score threshold", threshold)?;
        Ok(Self {
            name,
            threshold,
            scorer,
        })
    }
}

impl<F, Fut> EvaluationScorer for FnScorer<F>
where
    F: Fn(EvaluationCase, EvaluationOutput) -> Fut + Send + Sync,
    Fut: Future<Output = Result<ScoreValue, EvaluationError>> + Send + 'static,
{
    fn name(&self) -> &str {
        &self.name
    }

    fn threshold(&self) -> f64 {
        self.threshold
    }

    fn score(
        &self,
        case: EvaluationCase,
        output: EvaluationOutput,
    ) -> EvaluationFuture<Result<ScoreValue, EvaluationError>> {
        Box::pin((self.scorer)(case, output))
    }
}

/// Deterministic JSON equality scorer.
#[derive(Clone, Copy, Debug, Default)]
pub struct JsonExactMatchScorer;

impl EvaluationScorer for JsonExactMatchScorer {
    fn name(&self) -> &'static str {
        "json_exact_match"
    }

    fn threshold(&self) -> f64 {
        1.0
    }

    fn score(
        &self,
        case: EvaluationCase,
        output: EvaluationOutput,
    ) -> EvaluationFuture<Result<ScoreValue, EvaluationError>> {
        Box::pin(async move {
            let expected = case.expected.ok_or_else(|| EvaluationError::Scorer {
                scorer: "json_exact_match".into(),
                message: "case has no reference answer".into(),
            })?;
            ScoreValue::new(if expected == output.value { 1.0 } else { 0.0 })
        })
    }
}

/// One persisted per-case score.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EvaluationScore {
    /// Stable scorer name.
    pub name: String,
    /// Normalized score from zero through one.
    pub value: f64,
    /// Per-case passing threshold.
    pub threshold: f64,
    /// Whether this score meets its threshold.
    pub passed: bool,
    /// Optional evaluator explanation.
    pub rationale: Option<String>,
}

mod regression;
mod runner;
#[cfg(test)]
mod tests;
mod validation;

pub use regression::{MetricRegression, RegressionComparison, RegressionPolicy};
pub use runner::EvaluationRunner;
use validation::{
    ensure_close, ensure_non_negative, ensure_not_empty, ensure_ratio, validate_case_metrics,
};

/// Evaluation failure stage.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EvaluationFailureStage {
    /// The target did not produce an output.
    Target,
    /// One scorer failed.
    Scorer,
}

/// Safe per-case execution or scorer failure.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EvaluationFailure {
    /// Failure stage.
    pub stage: EvaluationFailureStage,
    /// Scorer name for scorer failures.
    pub scorer: Option<String>,
    /// Safe operator-facing explanation.
    pub message: String,
}

/// Output-free per-case evaluation result.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EvaluationCaseResult {
    /// Stable case identity.
    pub case_id: EvaluationCaseId,
    /// Run/Trace correlation when the target supplied one.
    pub run_id: Option<RunId>,
    /// Host latency and optional Provider usage for successful execution.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metrics: Option<EvaluationMetrics>,
    /// Successful scores sorted by scorer name.
    pub scores: Vec<EvaluationScore>,
    /// Target and scorer failures.
    pub failures: Vec<EvaluationFailure>,
}

/// Aggregate score statistics.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EvaluationScoreSummary {
    /// Stable scorer name.
    pub name: String,
    /// Cases that produced this score.
    pub scored_cases: usize,
    /// Total cases in the dataset.
    pub total_cases: usize,
    /// Mean over successfully scored cases.
    pub mean: f64,
    /// Passing cases divided by all dataset cases.
    pub pass_rate: f64,
}

/// Deterministic candidate evaluation report.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EvaluationReport {
    /// Dataset name.
    pub dataset_name: String,
    /// Dataset version.
    pub dataset_version: String,
    /// Candidate model, prompt, Agent, or application version.
    pub candidate_version: String,
    /// Target executions that produced output.
    pub execution_success_rate: f64,
    /// Case results in dataset order.
    pub cases: Vec<EvaluationCaseResult>,
    /// Score summaries sorted by scorer name.
    pub summaries: Vec<EvaluationScoreSummary>,
}

impl EvaluationReport {
    /// Serializes this output-free report as stable, pretty JSON.
    ///
    /// # Errors
    ///
    /// Returns a JSON error only if serialization unexpectedly fails.
    pub fn to_json_pretty(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Compares this candidate with a baseline of the same dataset identity.
    ///
    /// # Errors
    ///
    /// Returns [`EvaluationError::DatasetMismatch`] when dataset identities
    /// differ.
    pub fn compare(
        &self,
        baseline: &Self,
        policy: &RegressionPolicy,
    ) -> Result<RegressionComparison, EvaluationError> {
        self.validate()?;
        baseline.validate()?;
        policy.validate()?;
        if self.dataset_name != baseline.dataset_name
            || self.dataset_version != baseline.dataset_version
        {
            return Err(EvaluationError::DatasetMismatch {
                baseline_name: baseline.dataset_name.clone(),
                baseline_version: baseline.dataset_version.clone(),
                candidate_name: self.dataset_name.clone(),
                candidate_version: self.dataset_version.clone(),
            });
        }
        let metrics = baseline
            .summaries
            .iter()
            .map(|baseline_summary| {
                let candidate = self
                    .summaries
                    .iter()
                    .find(|summary| summary.name == baseline_summary.name);
                let candidate_mean = candidate.map_or(0.0, |summary| summary.mean);
                let candidate_pass_rate = candidate.map_or(0.0, |summary| summary.pass_rate);
                let mean_delta = candidate_mean - baseline_summary.mean;
                let pass_rate_delta = candidate_pass_rate - baseline_summary.pass_rate;
                MetricRegression {
                    name: baseline_summary.name.clone(),
                    baseline_mean: baseline_summary.mean,
                    candidate_mean,
                    mean_delta,
                    baseline_pass_rate: baseline_summary.pass_rate,
                    candidate_pass_rate,
                    pass_rate_delta,
                    passed: mean_delta >= -policy.max_mean_drop
                        && pass_rate_delta >= -policy.max_pass_rate_drop,
                }
            })
            .collect::<Vec<_>>();
        let execution_success_rate_delta =
            self.execution_success_rate - baseline.execution_success_rate;
        let passed = execution_success_rate_delta >= -policy.max_execution_success_drop
            && metrics.iter().all(|metric| metric.passed);
        Ok(RegressionComparison {
            baseline_version: baseline.candidate_version.clone(),
            candidate_version: self.candidate_version.clone(),
            execution_success_rate_delta,
            metrics,
            passed,
        })
    }

    /// Validates a report loaded from an external artifact.
    ///
    /// # Errors
    ///
    /// Returns an error for empty identities, invalid ratios, or duplicate
    /// score summaries.
    pub fn validate(&self) -> Result<(), EvaluationError> {
        ensure_not_empty("dataset name", &self.dataset_name)?;
        ensure_not_empty("dataset version", &self.dataset_version)?;
        ensure_not_empty("candidate version", &self.candidate_version)?;
        ensure_ratio("execution success rate", self.execution_success_rate)?;
        if self.cases.is_empty() {
            return Err(EvaluationError::InconsistentReport {
                message: "report contains no cases",
            });
        }
        let total_cases = self.cases.iter().fold(0.0, |total, _| total + 1.0);
        let mut successful_cases = 0.0;
        let mut case_ids = BTreeSet::new();
        let mut aggregate = BTreeMap::<&str, (usize, f64, f64, f64)>::new();
        for case in &self.cases {
            ensure_not_empty("report case id", case.case_id.as_str())?;
            if !case_ids.insert(case.case_id.as_str()) {
                return Err(EvaluationError::InconsistentReport {
                    message: "report contains duplicate case IDs",
                });
            }
            if !case
                .failures
                .iter()
                .any(|failure| failure.stage == EvaluationFailureStage::Target)
            {
                successful_cases += 1.0;
            }
            validate_case_metrics(case)?;
            let mut score_names = BTreeSet::new();
            for score in &case.scores {
                ensure_not_empty("report score name", &score.name)?;
                ensure_ratio("report score", score.value)?;
                ensure_ratio("report score threshold", score.threshold)?;
                if score.passed != (score.value >= score.threshold) {
                    return Err(EvaluationError::InconsistentReport {
                        message: "stored score decision contradicts its threshold",
                    });
                }
                if !score_names.insert(score.name.as_str()) {
                    return Err(EvaluationError::InconsistentReport {
                        message: "one case contains duplicate score names",
                    });
                }
                let entry = aggregate.entry(&score.name).or_default();
                entry.0 += 1;
                entry.1 += score.value;
                entry.2 += 1.0;
                entry.3 += if score.passed { 1.0 } else { 0.0 };
            }
        }
        ensure_close(
            self.execution_success_rate,
            successful_cases / total_cases,
            "execution success rate contradicts case failures",
        )?;
        let mut names = BTreeSet::new();
        for summary in &self.summaries {
            ensure_not_empty("score summary name", &summary.name)?;
            ensure_ratio("score mean", summary.mean)?;
            ensure_ratio("score pass rate", summary.pass_rate)?;
            if !names.insert(summary.name.as_str()) {
                return Err(EvaluationError::DuplicateScorer {
                    scorer: summary.name.clone(),
                });
            }
            let Some((scored_cases, total, scored_cases_ratio, passed)) =
                aggregate.get(summary.name.as_str())
            else {
                return Err(EvaluationError::InconsistentReport {
                    message: "score summary has no per-case evidence",
                });
            };
            if summary.scored_cases != *scored_cases || summary.total_cases != self.cases.len() {
                return Err(EvaluationError::InconsistentReport {
                    message: "score summary case counts are inconsistent",
                });
            }
            ensure_close(
                summary.mean,
                total / scored_cases_ratio,
                "score summary mean contradicts case scores",
            )?;
            ensure_close(
                summary.pass_rate,
                passed / total_cases,
                "score summary pass rate contradicts case scores",
            )?;
        }
        if names.len() != aggregate.len() {
            return Err(EvaluationError::InconsistentReport {
                message: "per-case score is missing its summary",
            });
        }
        Ok(())
    }
}