rustev-eval 0.2.0

Rustev's offline evaluation: bounded replay bundles, historical reproduction, candidate comparison, datasets, metrics and calibration fitting.
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
//! Dynamic-question evaluation (spec 019, 3.7): offline reproduction of a
//! retained outcome with zero backend calls, and a dedicated dataset,
//! evaluator configuration and report for one dynamic task family.
//!
//! Nothing here calls a backend or reads a clock: there is no backend
//! parameter anywhere in this module. A changed request, or a retained
//! outcome without a raw response, is `incomparable`, never permission to
//! ask again. Dynamic evidence never becomes a registered-plan replay
//! bundle, and evidence for one dynamic task family, question schema,
//! label order, template or artifact does not transfer to another.
//!
//! Reports state coverage and abstention first, then accepted error and
//! class-appropriate metrics, each with its denominator; an empty
//! denominator is `unknown`, never a pass. Distributions stay uncalibrated:
//! this module fits no calibration and reports none. A report on synthetic
//! data measures mechanics only, and every gate over it is `unknown`.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use rustev_contract::Identified;
use rustev_contract::dynamic::{
    DynamicAnswer, DynamicCapability, DynamicOperation, DynamicOutcome, DynamicOutputKind,
    DynamicQuestion, DynamicRaw, DynamicResult, TaskFamily, TemplateRef,
};
use rustev_contract::eval_report::{DatasetProvenance, Metric, MetricValue, Split};
use rustev_contract::ids::{
    ArtifactId, DatasetId, DynamicCapabilityId, DynamicRequestId, EvaluatorConfigId,
};
use rustev_contract::limits::{RECORD_V1, REPLAY_V1};
use rustev_core::dynamic as core;
use serde::{Deserialize, Serialize};

use crate::dataset::Label;
use crate::doc::eval_document;

pub const DYNAMIC_DATASET: &str = "rustev.dynamic-dataset/1";
pub const DYNAMIC_EVALUATOR_CONFIG: &str = "rustev.dynamic-evaluator-config/1";
pub const DYNAMIC_REPORT: &str = "rustev.dynamic-eval-report/1";

// ---------------------------------------------------------------------------
// Offline reproduction

/// Why a retained outcome cannot be compared (3.7).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum DynamicIncomparable {
    /// The supplied request fails validation.
    RequestInvalid { detail: String },
    /// The canonical request identity differs from the retained one (the
    /// question, label order, context, scope, task family, template,
    /// preprocessing, artifact, privacy or output kind changed).
    RequestChanged,
    /// The capability declaration differs from the one retained.
    CapabilityChanged,
    /// The outcome holds no raw response: nothing was dispatched, the
    /// adapter failed, or no answer arrived.
    RawResponseUnavailable,
}

impl DynamicIncomparable {
    pub fn name(&self) -> &'static str {
        match self {
            DynamicIncomparable::RequestInvalid { .. } => "request_invalid",
            DynamicIncomparable::RequestChanged => "request_changed",
            DynamicIncomparable::CapabilityChanged => "capability_changed",
            DynamicIncomparable::RawResponseUnavailable => "raw_response_unavailable",
        }
    }
}

/// The result of reproducing one retained outcome.
#[derive(Debug, Clone, PartialEq)]
pub enum DynamicReproduction {
    /// Validation, abstention and result are exactly as retained.
    Reproduced,
    /// The same raw response judges differently now.
    Diverged {
        detail: String,
    },
    Incomparable(DynamicIncomparable),
}

/// Reproduce a retained outcome from the request, the capability and the
/// retained raw response, with zero backend calls (3.7).
pub fn reproduce(
    retained: &DynamicOutcome,
    question: &DynamicQuestion,
    capability: &DynamicCapability,
) -> DynamicReproduction {
    let q = match core::validate(
        question.clone(),
        std::slice::from_ref(&question.task_family),
    ) {
        Ok(q) => q,
        Err(e) => {
            return DynamicReproduction::Incomparable(DynamicIncomparable::RequestInvalid {
                detail: e.to_string().chars().take(200).collect(),
            });
        }
    };
    let Ok(identity) = core::request_identity(&q, capability) else {
        return DynamicReproduction::Incomparable(DynamicIncomparable::RequestChanged);
    };
    if retained.request.as_ref() != Some(&identity) {
        return DynamicReproduction::Incomparable(DynamicIncomparable::RequestChanged);
    }
    if retained.capability != capability.id().ok() {
        return DynamicReproduction::Incomparable(DynamicIncomparable::CapabilityChanged);
    }
    let DynamicRaw::Output(raw) = &retained.raw else {
        return DynamicReproduction::Incomparable(DynamicIncomparable::RawResponseUnavailable);
    };
    let Some(attempt) = &retained.attempt else {
        return DynamicReproduction::Incomparable(DynamicIncomparable::RawResponseUnavailable);
    };
    let judged = core::judge(&q, capability, &retained.served, raw);
    let result = match judged.result {
        Ok(answer) => DynamicResult::Proposal(core::proposal(
            &q,
            identity,
            capability,
            &attempt.attempt_id,
            answer,
        )),
        Err(u) => DynamicResult::Unresolved(u),
    };
    let mut differs = vec![];
    if judged.validation != retained.validation {
        differs.push("validation");
    }
    if judged.abstention != retained.abstention {
        differs.push("abstention");
    }
    if result != retained.result {
        differs.push("result");
    }
    if differs.is_empty() {
        DynamicReproduction::Reproduced
    } else {
        DynamicReproduction::Diverged {
            detail: format!("{} differ", differs.join(", ")),
        }
    }
}

// ---------------------------------------------------------------------------
// Dataset

/// The exact question and label schema a dataset and a configuration bind:
/// results never transfer to another question, label order or output kind.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct QuestionSchema {
    pub operation: DynamicOperation,
    pub question: String,
    pub labels: Vec<String>,
    pub output: DynamicOutputKind,
}

impl QuestionSchema {
    fn matches(&self, q: &DynamicQuestion) -> bool {
        self.operation == q.operation
            && self.question == q.question
            && self.labels == q.labels
            && self.output == q.output
    }
}

/// A dynamic dataset, `rustev.dynamic-dataset/1` (3.7).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DynamicDataset {
    pub schema: String,
    pub name: String,
    pub task_family: TaskFamily,
    pub question_schema: QuestionSchema,
    /// The context sources cases may draw from, by name.
    pub context_sources: Vec<String>,
    pub artifact: ArtifactId,
    pub template: TemplateRef,
    /// Synthetic provenance prevents every quality claim (R-04, R-37).
    pub provenance: DatasetProvenance,
    pub license: String,
    /// How the labels were assigned.
    pub labeling: String,
    /// What was excluded, and why.
    pub exclusions: Vec<String>,
    pub limitations: String,
    pub cases: Vec<DynamicCase>,
}

eval_document!(DynamicDataset, DYNAMIC_DATASET, REPLAY_V1, DatasetId);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DynamicCase {
    pub id: String,
    /// Opaque identity of where the case came from.
    pub source: String,
    pub split: Split,
    pub question: DynamicQuestion,
    /// The expected label or level, or an explicit statement there is none.
    pub label: Label,
    /// Instruction, role, tool, URL or schema-like text placed in a
    /// caller-controlled field on purpose.
    pub adversarial: bool,
    pub subgroups: Vec<String>,
}

/// Why a dataset was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DynamicDatasetError {
    Schema,
    Empty,
    /// Name, license, labeling or limitations are empty, or real provenance
    /// lacks a field.
    Provenance,
    DuplicateCase {
        case: String,
    },
    /// The same source or request id appears in more than one split.
    Leakage {
        case: String,
        identity: String,
    },
    /// No injection-adversarial case exists.
    NoAdversarialCase,
    /// A case's request is invalid or does not match the dataset's task
    /// family, question schema or artifact.
    Case {
        case: String,
        detail: String,
    },
}

impl fmt::Display for DynamicDatasetError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

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

impl DynamicDataset {
    /// Validate the dataset (3.7): its bindings, provenance, unique cases,
    /// no leakage across splits, at least one adversarial case, and every
    /// case's request valid for the bound task family, question schema,
    /// artifact and context sources.
    pub fn check(&self) -> Result<(), DynamicDatasetError> {
        if self.schema != DYNAMIC_DATASET {
            return Err(DynamicDatasetError::Schema);
        }
        if self.cases.is_empty() {
            return Err(DynamicDatasetError::Empty);
        }
        if [&self.name, &self.license, &self.labeling, &self.limitations]
            .iter()
            .any(|s| s.trim().is_empty())
        {
            return Err(DynamicDatasetError::Provenance);
        }
        if let DatasetProvenance::Real {
            source,
            license,
            labeling,
            limitations,
        } = &self.provenance
            && [source, license, labeling, limitations]
                .iter()
                .any(|s| s.trim().is_empty())
        {
            return Err(DynamicDatasetError::Provenance);
        }
        if !self.cases.iter().any(|c| c.adversarial) {
            return Err(DynamicDatasetError::NoAdversarialCase);
        }
        let mut ids = BTreeSet::new();
        let mut split_of: BTreeMap<String, Split> = BTreeMap::new();
        for c in &self.cases {
            if !ids.insert(&c.id) {
                return Err(DynamicDatasetError::DuplicateCase { case: c.id.clone() });
            }
            for identity in [
                format!("source:{}", c.source),
                format!("request:{}", c.question.request_id),
            ] {
                match split_of.get(&identity) {
                    Some(s) if *s != c.split => {
                        return Err(DynamicDatasetError::Leakage {
                            case: c.id.clone(),
                            identity,
                        });
                    }
                    _ => {
                        split_of.insert(identity, c.split);
                    }
                }
            }
            let case = |detail: String| DynamicDatasetError::Case {
                case: c.id.clone(),
                detail,
            };
            core::validate(c.question.clone(), std::slice::from_ref(&self.task_family))
                .map_err(|e| case(e.to_string()))?;
            if !self.question_schema.matches(&c.question) {
                return Err(case("the request differs from the question schema".into()));
            }
            if c.question.artifact != self.artifact {
                return Err(case("the request requires another artifact".into()));
            }
            if let Some(f) = c
                .question
                .context
                .iter()
                .find(|f| !self.context_sources.contains(&f.source))
            {
                return Err(case(format!("undeclared context source {:?}", f.source)));
            }
            if let Label::Value(v) = &c.label
                && !self.question_schema.labels.contains(v)
            {
                return Err(case(format!("label {v:?} is not declared")));
            }
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Evaluator configuration

/// A dynamic evaluator configuration, `rustev.dynamic-evaluator-config/1`
/// (3.7): what a report's numbers mean anything under.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DynamicEvaluatorConfig {
    pub schema: String,
    pub task_family: TaskFamily,
    pub question_schema: QuestionSchema,
    pub artifact: ArtifactId,
    pub template: TemplateRef,
    /// What a label means, in words.
    pub label_interpretation: String,
    /// Gates over the report's metrics. Each is reported `unknown`: a
    /// quality gate needs separately governed real labeled evidence (spec
    /// 023), which no dynamic dataset binds yet.
    pub gates: Vec<DynamicGate>,
}

eval_document!(
    DynamicEvaluatorConfig,
    DYNAMIC_EVALUATOR_CONFIG,
    REPLAY_V1,
    EvaluatorConfigId
);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DynamicGate {
    pub metric: String,
    pub direction: GateDirection,
    pub threshold: rustev_contract::decimal::Decimal,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GateDirection {
    AtLeast,
    AtMost,
}

// ---------------------------------------------------------------------------
// Report

/// A dynamic evaluation report, `rustev.dynamic-eval-report/1` (3.7).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DynamicEvalReport {
    pub schema: String,
    pub dataset: DatasetId,
    pub evaluator_config: EvaluatorConfigId,
    pub task_family: TaskFamily,
    pub question_schema: QuestionSchema,
    pub artifact: ArtifactId,
    pub template: TemplateRef,
    pub capability: DynamicCapabilityId,
    pub split: Split,
    pub provenance: DatasetProvenance,
    /// Distributions are uncalibrated; no calibration artifact is bound.
    pub calibration: CalibrationStatus,
    pub claim: QualityClaim,
    /// Coverage first, then abstention by reason, then accepted error, then
    /// class-appropriate metrics; the same again over adversarial cases.
    pub metrics: Vec<Metric>,
    /// Cases not compared, by case id, with the reason.
    pub incomparable: Vec<IncomparableCase>,
    pub gates: Vec<GateResult>,
    pub limitations: Vec<String>,
}

eval_document!(DynamicEvalReport, DYNAMIC_REPORT, RECORD_V1);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CalibrationStatus {
    Uncalibrated,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum QualityClaim {
    /// Synthetic provenance: the report shows mechanics, never quality.
    MechanicsOnly,
    /// Real provenance: measured on the named data only, uncalibrated, and
    /// transferring to no other task family, schema, template or artifact.
    MeasuredOnNamedData,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IncomparableCase {
    pub case: String,
    pub reason: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GateResult {
    pub metric: String,
    pub status: GateStatus,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum GateStatus {
    Unknown { reason: String },
}

/// Why a report could not be produced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DynamicEvalError {
    Dataset(DynamicDatasetError),
    /// The configuration binds another task family, question schema,
    /// artifact or template than the dataset, or the capability another
    /// artifact or template.
    Binding(String),
    Identity(String),
}

impl fmt::Display for DynamicEvalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

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

/// One compared case.
struct Compared<'a> {
    case: &'a DynamicCase,
    outcome: &'a DynamicOutcome,
}

fn measured(num: f64, n: u64) -> MetricValue {
    if n == 0 || !num.is_finite() {
        MetricValue::Unknown {
            reason: "empty denominator".into(),
        }
    } else {
        MetricValue::Measured {
            value: num / n as f64,
            n,
        }
    }
}

fn metric(name: impl Into<String>, value: MetricValue) -> Metric {
    Metric {
        name: name.into(),
        value,
    }
}

/// Masses by label or level, in declared order.
type Masses<'a> = Vec<(&'a str, f64)>;

/// The top label or level of an accepted answer, and its masses if any.
fn answered(o: &DynamicOutcome) -> Option<(&str, Option<Masses<'_>>)> {
    let DynamicResult::Proposal(p) = &o.result else {
        return None;
    };
    Some(match &p.answer {
        DynamicAnswer::Label { label } => (label.as_str(), None),
        DynamicAnswer::UncalibratedDistribution { label, masses }
        | DynamicAnswer::OrdinalDistribution {
            level: label,
            masses,
        } => (
            label.as_str(),
            Some(masses.iter().map(|m| (m.label.as_str(), m.mass)).collect()),
        ),
    })
}

fn cohort_metrics(prefix: &str, cases: &[Compared<'_>], schema: &QuestionSchema) -> Vec<Metric> {
    let n = cases.len() as u64;
    let accepted: Vec<&Compared<'_>> = cases
        .iter()
        .filter(|c| answered(c.outcome).is_some())
        .collect();
    let mut out = vec![metric(
        format!("{prefix}coverage"),
        measured(accepted.len() as f64, n),
    )];
    // Abstention by reason, over every compared case.
    let mut reasons: BTreeMap<&'static str, u64> = BTreeMap::new();
    for c in cases {
        if let DynamicResult::Unresolved(u) = &c.outcome.result {
            *reasons.entry(u.name()).or_default() += 1;
        }
    }
    let total: u64 = reasons.values().sum();
    out.push(metric(
        format!("{prefix}abstention"),
        measured(total as f64, n),
    ));
    for (reason, k) in &reasons {
        out.push(metric(
            format!("{prefix}abstention.{reason}"),
            measured(*k as f64, n),
        ));
    }
    // Accepted error: among accepted cases with a label.
    let labeled: Vec<(&str, &str, Option<Masses<'_>>)> = accepted
        .iter()
        .filter_map(|c| {
            let label = c.case.label.value()?;
            let (top, masses) = answered(c.outcome)?;
            Some((label, top, masses))
        })
        .collect();
    let wrong = labeled.iter().filter(|(l, t, _)| l != t).count();
    out.push(metric(
        format!("{prefix}accepted_error"),
        measured(wrong as f64, labeled.len() as u64),
    ));
    // Class-appropriate metrics over accepted, labeled cases.
    match schema.output {
        DynamicOutputKind::Label => {}
        DynamicOutputKind::Distribution => {
            // Scored as uncalibrated mass, never as calibrated probability.
            let (mut brier, mut log_loss, mut infinite) = (0.0, 0.0, false);
            for (label, _, masses) in &labeled {
                let masses = masses.as_deref().unwrap_or_default();
                for (l, m) in masses {
                    let y = if l == label { 1.0 } else { 0.0 };
                    brier += (m - y) * (m - y);
                }
                let p = masses
                    .iter()
                    .find(|(l, _)| l == label)
                    .map(|(_, m)| *m)
                    .unwrap_or(0.0);
                if p > 0.0 {
                    log_loss -= rustev_core::math::ln(p);
                } else {
                    infinite = true;
                }
            }
            let k = labeled.len() as u64;
            out.push(metric(
                format!("{prefix}uncalibrated_brier"),
                measured(brier, k),
            ));
            out.push(metric(
                format!("{prefix}uncalibrated_log_loss"),
                if infinite {
                    MetricValue::Unknown {
                        reason: "infinite loss: zero mass on a labeled class".into(),
                    }
                } else {
                    measured(log_loss, k)
                },
            ));
        }
        DynamicOutputKind::OrdinalDistribution => {
            // Level distance by index: an ordinal summary, not an interval
            // score.
            let index = |l: &str| schema.labels.iter().position(|x| x == l);
            let mut dist = 0.0;
            for (label, top, _) in &labeled {
                if let (Some(a), Some(b)) = (index(label), index(top)) {
                    dist += a.abs_diff(b) as f64;
                }
            }
            out.push(metric(
                format!("{prefix}mean_level_distance"),
                measured(dist, labeled.len() as u64),
            ));
        }
    }
    out
}

/// Evaluate retained outcomes of one split against a dynamic dataset (3.7).
/// `outcomes` maps case ids to retained outcomes; a missing outcome or one
/// that does not reproduce is listed as incomparable and left out of every
/// denominator. Nothing is dispatched.
pub fn evaluate(
    dataset: &DynamicDataset,
    config: &DynamicEvaluatorConfig,
    capability: &DynamicCapability,
    split: Split,
    outcomes: &BTreeMap<String, DynamicOutcome>,
) -> Result<DynamicEvalReport, DynamicEvalError> {
    dataset.check().map_err(DynamicEvalError::Dataset)?;
    if config.schema != DYNAMIC_EVALUATOR_CONFIG {
        return Err(DynamicEvalError::Binding(
            "not a dynamic evaluator config".into(),
        ));
    }
    if config.task_family != dataset.task_family
        || config.question_schema != dataset.question_schema
        || config.artifact != dataset.artifact
        || config.template != dataset.template
    {
        return Err(DynamicEvalError::Binding(
            "the configuration binds another task family, question schema, artifact or template"
                .into(),
        ));
    }
    if capability.artifact != dataset.artifact || capability.template != dataset.template {
        return Err(DynamicEvalError::Binding(
            "the capability declares another artifact or template".into(),
        ));
    }
    let id =
        |e: rustev_contract::canonical::CanonicalError| DynamicEvalError::Identity(e.to_string());
    let mut compared = vec![];
    let mut incomparable = vec![];
    for case in dataset.cases.iter().filter(|c| c.split == split) {
        let Some(outcome) = outcomes.get(&case.id) else {
            incomparable.push(IncomparableCase {
                case: case.id.clone(),
                reason: "outcome_missing".into(),
            });
            continue;
        };
        // An outcome with a raw response must reproduce; one without (a
        // refusal, a failure, a deadline) is compared by its retained
        // result only when its request identity still matches.
        let reason = match reproduce(outcome, &case.question, capability) {
            DynamicReproduction::Reproduced => None,
            DynamicReproduction::Diverged { .. } => Some("diverged".to_string()),
            DynamicReproduction::Incomparable(DynamicIncomparable::RawResponseUnavailable) => {
                match retained_identity_matches(outcome, &case.question, capability) {
                    true => None,
                    false => Some(DynamicIncomparable::RequestChanged.name().to_string()),
                }
            }
            DynamicReproduction::Incomparable(i) => Some(i.name().to_string()),
        };
        match reason {
            None => compared.push(Compared { case, outcome }),
            Some(reason) => incomparable.push(IncomparableCase {
                case: case.id.clone(),
                reason,
            }),
        }
    }
    let mut metrics = cohort_metrics("", &compared, &dataset.question_schema);
    let adversarial: Vec<Compared<'_>> = compared
        .iter()
        .filter(|c| c.case.adversarial)
        .map(|c| Compared {
            case: c.case,
            outcome: c.outcome,
        })
        .collect();
    metrics.extend(cohort_metrics(
        "adversarial.",
        &adversarial,
        &dataset.question_schema,
    ));
    let synthetic = matches!(dataset.provenance, DatasetProvenance::Synthetic { .. });
    let gates = config
        .gates
        .iter()
        .map(|g| GateResult {
            metric: g.metric.clone(),
            status: gate(&metrics, g, synthetic),
        })
        .collect();
    let mut limitations = vec![
        "distributions are uncalibrated masses, not calibrated probabilities".to_string(),
        "results do not transfer to another task family, question, label order, template, \
         artifact, population or registered-plan task"
            .to_string(),
        dataset.limitations.clone(),
    ];
    if synthetic {
        limitations.insert(
            0,
            "synthetic provenance: evidence of mechanics only, never of semantic quality".into(),
        );
    }
    if !core::qualifiable(capability) {
        limitations.push(
            "the capability declares hidden output-affecting state: not eligible for qualification"
                .into(),
        );
    }
    Ok(DynamicEvalReport {
        schema: DYNAMIC_REPORT.into(),
        dataset: dataset.id().map_err(id)?,
        evaluator_config: config.id().map_err(id)?,
        task_family: dataset.task_family.clone(),
        question_schema: dataset.question_schema.clone(),
        artifact: dataset.artifact.clone(),
        template: dataset.template.clone(),
        capability: capability.id().map_err(id)?,
        split,
        provenance: dataset.provenance.clone(),
        calibration: CalibrationStatus::Uncalibrated,
        claim: if synthetic {
            QualityClaim::MechanicsOnly
        } else {
            QualityClaim::MeasuredOnNamedData
        },
        metrics,
        incomparable,
        gates,
        limitations,
    })
}

fn retained_identity_matches(
    outcome: &DynamicOutcome,
    question: &DynamicQuestion,
    capability: &DynamicCapability,
) -> bool {
    let Ok(q) = core::validate(
        question.clone(),
        std::slice::from_ref(&question.task_family),
    ) else {
        return false;
    };
    let identity: Option<DynamicRequestId> = core::request_identity(&q, capability).ok();
    identity.is_some() && outcome.request == identity && outcome.capability == capability.id().ok()
}

/// Every gate is `unknown` here: over synthetic data a gate would claim
/// quality from mechanics (R-04), and over real data it needs separately
/// governed admission of that data (spec 023), which no dynamic dataset
/// binds yet.
fn gate(metrics: &[Metric], g: &DynamicGate, synthetic: bool) -> GateStatus {
    let known = metrics
        .iter()
        .any(|m| m.name == g.metric && matches!(m.value, MetricValue::Measured { .. }));
    GateStatus::Unknown {
        reason: if synthetic {
            "synthetic provenance: a quality gate needs separately governed real labeled evidence"
        } else if known {
            "no separately governed admission of this real data is bound (spec 023)"
        } else {
            "the metric is unknown or absent"
        }
        .into(),
    }
}