agentics-domain 0.3.0

Domain types and validation models for the Agentics challenge platform.
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
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt;

use serde::{Deserialize, Serialize};

use super::challenge::{MetricDirection, MetricSchemaSpec, MetricVisibility};
use super::hashes::Sha256Digest;
use super::ids::{EvaluationId, EvaluationJobId};
use super::names::{ChallengeName, MetricName, RunName, TargetName};
use crate::storage::StorageKey;

/// Evaluation surface requested for a solution submission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub enum ScoringMode {
    /// Private validation scoring, backed by public challenge data.
    #[serde(rename = "validation")]
    Validation,
    /// Ranking-visible official scoring, backed by private benchmark data.
    #[serde(rename = "official")]
    Official,
}

impl ScoringMode {
    /// Canonical persisted and API value for this mode.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Validation => "validation",
            Self::Official => "official",
        }
    }

    /// Parse canonical persisted values.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "validation" => Some(Self::Validation),
            "official" => Some(Self::Official),
            _ => None,
        }
    }

    /// Argument passed to the evaluator protocol.
    pub fn evaluator_mode_arg(self) -> &'static str {
        match self {
            Self::Validation => "validation",
            Self::Official => "official",
        }
    }
}

impl fmt::Display for ScoringMode {
    /// Format the mode as its stable persisted and wire value.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Controls how much per-case detail a dataset may expose.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ScoreVisibility {
    Full,
    ScoreOnly,
}

/// Per-case evaluator outcome for public validation tests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EvaluatorCaseStatus {
    Passed,
    Failed,
    Error,
}

/// Overall evaluator outcome emitted by `result.json`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EvaluatorRunStatus {
    Passed,
    Failed,
    Error,
}

/// Persistent lifecycle state for an evaluation job/result.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EvaluationStatus {
    Queued,
    Running,
    Completed,
    Failed,
}

impl EvaluationStatus {
    /// Stable database string for an evaluation lifecycle state.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Queued => "queued",
            Self::Running => "running",
            Self::Completed => "completed",
            Self::Failed => "failed",
        }
    }

    /// Parse a stable database string for an evaluation lifecycle state.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "queued" => Some(Self::Queued),
            "running" => Some(Self::Running),
            "completed" => Some(Self::Completed),
            "failed" => Some(Self::Failed),
            _ => None,
        }
    }
}

impl fmt::Display for EvaluationStatus {
    /// Format the evaluation status as its stable persisted and wire value.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Persistent lifecycle state for an evaluation job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EvaluationJobStatus {
    Staged,
    Queued,
    Running,
    Completed,
    Failed,
}

impl EvaluationJobStatus {
    /// Stable database string for an evaluation job lifecycle state.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Staged => "staged",
            Self::Queued => "queued",
            Self::Running => "running",
            Self::Completed => "completed",
            Self::Failed => "failed",
        }
    }

    /// Parse a stable database string for an evaluation job lifecycle state.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "staged" => Some(Self::Staged),
            "queued" => Some(Self::Queued),
            "running" => Some(Self::Running),
            "completed" => Some(Self::Completed),
            "failed" => Some(Self::Failed),
            _ => None,
        }
    }
}

impl fmt::Display for EvaluationJobStatus {
    /// Format the job status as its stable persisted and wire value.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Persistent lifecycle state for a solution submission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum SolutionSubmissionStatus {
    Pending,
    Queued,
    Running,
    Completed,
    Failed,
}

impl SolutionSubmissionStatus {
    /// Stable database string for a solution-submission lifecycle state.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Pending => "pending",
            Self::Queued => "queued",
            Self::Running => "running",
            Self::Completed => "completed",
            Self::Failed => "failed",
        }
    }

    /// Parse a stable database string for a solution-submission lifecycle state.
    pub fn from_storage_value(value: &str) -> Option<Self> {
        match value {
            "pending" => Some(Self::Pending),
            "queued" => Some(Self::Queued),
            "running" => Some(Self::Running),
            "completed" => Some(Self::Completed),
            "failed" => Some(Self::Failed),
            _ => None,
        }
    }
}

impl fmt::Display for SolutionSubmissionStatus {
    /// Format the submission status as its stable persisted and wire value.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Aggregate score summary for validation or official datasets.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ScoreSummary {
    /// Challenge-defined finite score summary.
    pub score: f64,
    /// Number of passed cases in the aggregate.
    pub passed: i64,
    /// Total number of cases in the aggregate.
    pub total: i64,
}

/// Public per-case result exposed for validation feedback.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct PublicCaseResult {
    pub case_name: String,
    pub status: EvaluatorCaseStatus,
    pub score: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// Numeric value for one declared metric.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct MetricValue {
    pub metric_name: MetricName,
    pub value: f64,
}

/// Metric values for one evaluator-defined run, case, seed, shard, or scenario.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct RunMetricResult {
    pub run_name: RunName,
    #[serde(default)]
    #[schemars(required)]
    pub metrics: Vec<MetricValue>,
}

/// API DTO for a persisted evaluation.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct EvaluationDto {
    pub id: EvaluationId,
    pub target: TargetName,
    pub status: EvaluationStatus,
    pub eval_type: ScoringMode,
    pub aggregate_metrics: Vec<MetricValue>,
    pub run_metrics: Vec<RunMetricResult>,
    pub public_results: Vec<PublicCaseResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub validation_summary: Option<ScoreSummary>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub official_summary: Option<ScoreSummary>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub runner_log_storage_key: Option<StorageKey>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub started_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finished_at: Option<String>,
}

/// Raw evaluator output read from a runner container's `result.json`.
///
/// Optional fields match the relaxed JSON contract used by the rewrite:
/// absent nullable fields are accepted, but numeric scores and mode-specific
/// summaries are validated before the result is persisted.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct EvaluatorRunResult {
    pub status: EvaluatorRunStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<ScoringMode>,
    #[serde(default)]
    pub aggregate_metrics: Vec<MetricValue>,
    #[serde(default)]
    pub run_metrics: Vec<RunMetricResult>,
    #[serde(default)]
    pub public_results: Vec<PublicCaseResult>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub validation_summary: Option<ScoreSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub official_summary: Option<ScoreSummary>,
    #[serde(default)]
    pub logs: Vec<String>,
}

impl ScoreSummary {
    /// Validate finite score and aggregate case counts for a named summary field.
    pub fn validate(&self, label: &str) -> Result<(), String> {
        validate_finite_number(self.score, &format!("{label}.score"))?;
        if self.passed < 0 {
            return Err(format!("{label}.passed must be >= 0"));
        }
        if self.total < 0 {
            return Err(format!("{label}.total must be >= 0"));
        }
        if self.passed > self.total {
            return Err(format!("{label}.passed cannot be greater than total"));
        }

        Ok(())
    }
}

impl PublicCaseResult {
    /// Validate the public case name and finite challenge-defined score.
    pub fn validate(&self) -> Result<(), String> {
        if self.case_name.trim().is_empty() {
            return Err("public_results.case_name must not be empty".to_string());
        }
        validate_finite_number(self.score, "public_results.score")
    }
}

impl MetricValue {
    /// Validate metric name shape and finite numeric value.
    pub fn validate(&self, field: &str) -> Result<(), String> {
        validate_finite_number(self.value, &format!("{field}.value"))
    }

    /// Find a metric value by name in an evaluator metric payload.
    pub fn find_by_name(metrics: &[Self], metric_name: &MetricName) -> Option<Self> {
        metrics
            .iter()
            .find(|metric| &metric.metric_name == metric_name)
            .cloned()
    }
}

impl RunMetricResult {
    /// Validate one per-run metric record without checking challenge-specific names.
    pub fn validate(&self) -> Result<(), String> {
        let mut metric_names = HashSet::with_capacity(self.metrics.len());
        for metric in &self.metrics {
            metric.validate("run_metrics.metrics")?;
            if !metric_names.insert(metric.metric_name.as_str()) {
                return Err(format!(
                    "run_metrics.metrics contains duplicate metric_name `{}` for run `{}`",
                    metric.metric_name, self.run_name
                ));
            }
        }

        Ok(())
    }
}

impl EvaluatorRunResult {
    /// Validate platform-owned size limits before result persistence.
    pub fn validate_size_limits(
        &self,
        max_public_results: u64,
        max_result_log_bytes: u64,
    ) -> Result<(), String> {
        let public_result_count = u64::try_from(self.public_results.len())
            .map_err(|_| "public_results count exceeds supported range".to_string())?;
        if public_result_count > max_public_results {
            return Err(format!(
                "public_results contains too many entries: {public_result_count} > {max_public_results}"
            ));
        }

        let mut log_bytes = 0u64;
        for log in &self.logs {
            let len = u64::try_from(log.len())
                .map_err(|_| "result.logs byte length exceeds supported range".to_string())?;
            log_bytes = log_bytes
                .checked_add(len)
                .ok_or_else(|| "result.logs byte length overflow".to_string())?;
            if log_bytes > max_result_log_bytes {
                return Err(format!(
                    "result.logs exceeds byte limit: {log_bytes} > {max_result_log_bytes} bytes"
                ));
            }
        }

        Ok(())
    }

    /// Validate evaluator output against the evaluation mode that was actually run.
    ///
    /// If the evaluator included a `mode`, it must match `mode`.
    pub fn validate_for_mode(&self, mode: ScoringMode) -> Result<(), String> {
        if let Some(result_mode) = self.mode
            && result_mode != mode
        {
            return Err("result mode does not match evaluation job type".to_string());
        }

        validate_metric_values(&self.aggregate_metrics, "aggregate_metrics")?;

        let mut run_names = HashSet::with_capacity(self.run_metrics.len());
        for run in &self.run_metrics {
            run.validate()?;
            if !run_names.insert(run.run_name.as_str()) {
                return Err(format!(
                    "run_metrics contains duplicate run_name `{}`",
                    run.run_name
                ));
            }
        }

        for public_result in &self.public_results {
            public_result.validate()?;
        }

        if let Some(validation) = &self.validation_summary {
            validation.validate("validation_summary")?;
        }
        if let Some(official) = &self.official_summary {
            official.validate("official_summary")?;
        }

        if self.validation_summary.is_none() && self.official_summary.is_none() {
            return Err(
                "validation_summary and official_summary cannot both be absent".to_string(),
            );
        }
        if mode == ScoringMode::Validation && self.validation_summary.is_none() {
            return Err("validation evaluation requires validation_summary".to_string());
        }
        if mode == ScoringMode::Official && self.official_summary.is_none() {
            return Err("official evaluation requires official_summary".to_string());
        }

        Ok(())
    }

    /// Complete metric-derived evaluator fields after schema validation.
    pub fn complete_metric_result(
        &mut self,
        schema: &MetricSchemaSpec,
        mode: ScoringMode,
    ) -> Result<(), String> {
        self.validate_for_metric_schema(schema, mode)
    }

    /// Validate metric names against the challenge's declared metric schema.
    pub fn validate_for_metric_schema(
        &self,
        schema: &MetricSchemaSpec,
        mode: ScoringMode,
    ) -> Result<(), String> {
        let declared = schema
            .metrics
            .iter()
            .map(|metric| (metric.name.as_str(), metric))
            .collect::<std::collections::HashMap<_, _>>();
        if declared.is_empty() {
            return Err("metric schema must declare at least one metric".to_string());
        }

        for metric in &self.aggregate_metrics {
            let Some(definition) = declared.get(metric.metric_name.as_str()) else {
                return Err(format!(
                    "aggregate_metrics references unknown metric `{}`",
                    metric.metric_name
                ));
            };
            validate_metric_visibility(mode, definition.visibility, &metric.metric_name)?;
        }

        for run in &self.run_metrics {
            for metric in &run.metrics {
                let Some(definition) = declared.get(metric.metric_name.as_str()) else {
                    return Err(format!(
                        "run_metrics references unknown metric `{}`",
                        metric.metric_name
                    ));
                };
                validate_metric_visibility(mode, definition.visibility, &metric.metric_name)?;
            }
        }

        if mode == ScoringMode::Official
            && !self
                .aggregate_metrics
                .iter()
                .any(|metric| metric.metric_name == schema.ranking.primary_metric_name)
        {
            return Err(format!(
                "aggregate_metrics missing primary metric `{}`",
                schema.ranking.primary_metric_name
            ));
        }

        Ok(())
    }
}

/// Compare two aggregate metric payloads using a challenge's ranking schema.
///
/// Returns `Ordering::Less` when `a_metrics` should rank before `b_metrics`.
pub fn compare_metric_payloads_by_ranking(
    schema: &MetricSchemaSpec,
    a_metrics: &[MetricValue],
    b_metrics: &[MetricValue],
) -> Ordering {
    let Some(primary) = schema.primary_metric() else {
        return Ordering::Equal;
    };
    let primary_order = compare_metric_by_direction(
        primary.direction,
        metric_value_by_name(a_metrics, &schema.ranking.primary_metric_name),
        metric_value_by_name(b_metrics, &schema.ranking.primary_metric_name),
    );
    if primary_order != Ordering::Equal {
        return primary_order;
    }

    for metric_name in &schema.ranking.tie_breaker_metric_names {
        let Some(definition) = schema.metric(metric_name) else {
            continue;
        };
        let ordering = compare_metric_by_direction(
            definition.direction,
            metric_value_by_name(a_metrics, metric_name),
            metric_value_by_name(b_metrics, metric_name),
        );
        if ordering != Ordering::Equal {
            return ordering;
        }
    }

    Ordering::Equal
}

/// Return one metric value by name from an aggregate metric payload.
pub fn metric_value_by_name(metrics: &[MetricValue], metric_name: &MetricName) -> Option<f64> {
    metrics
        .iter()
        .find(|metric| &metric.metric_name == metric_name)
        .map(|metric| metric.value)
}

/// Compare optional metric values according to the declared direction.
fn compare_metric_by_direction(
    direction: MetricDirection,
    a: Option<f64>,
    b: Option<f64>,
) -> Ordering {
    match (a, b) {
        (Some(a), Some(b)) => match direction {
            MetricDirection::Maximize => compare_f64_desc(a, b),
            MetricDirection::Minimize => compare_f64_asc(a, b),
        },
        (Some(_), None) => Ordering::Less,
        (None, Some(_)) => Ordering::Greater,
        (None, None) => Ordering::Equal,
    }
}

/// Compare finite values in descending order.
fn compare_f64_desc(a: f64, b: f64) -> Ordering {
    b.partial_cmp(&a).unwrap_or(Ordering::Equal)
}

/// Compare finite values in ascending order.
fn compare_f64_asc(a: f64, b: f64) -> Ordering {
    a.partial_cmp(&b).unwrap_or(Ordering::Equal)
}

/// Validates finite number invariants for this contract.
fn validate_finite_number(value: f64, field: &str) -> Result<(), String> {
    if !value.is_finite() {
        return Err(format!("{field} must be finite"));
    }

    Ok(())
}

/// Validates metric values invariants for this contract.
fn validate_metric_values(metrics: &[MetricValue], field: &str) -> Result<(), String> {
    let mut metric_names = HashSet::with_capacity(metrics.len());
    for metric in metrics {
        metric.validate(field)?;
        if !metric_names.insert(metric.metric_name.as_str()) {
            return Err(format!(
                "{field} contains duplicate metric_name `{}`",
                metric.metric_name
            ));
        }
    }

    Ok(())
}

/// Validates metric visibility invariants for this contract.
fn validate_metric_visibility(
    mode: ScoringMode,
    visibility: MetricVisibility,
    metric_name: &MetricName,
) -> Result<(), String> {
    if mode == ScoringMode::Validation && visibility == MetricVisibility::Official {
        return Err(format!(
            "validation results cannot include official-only metric `{metric_name}`"
        ));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use crate::models::challenge::{
        MetricDefinitionSpec, MetricDirection, MetricSchemaSpec, MetricVisibility, RankingSpec,
    };
    use crate::models::names::{MetricName, RunName};

    use super::{
        EvaluatorCaseStatus, EvaluatorRunResult, EvaluatorRunStatus, MetricValue, RunMetricResult,
        ScoreSummary, ScoringMode,
    };

    /// Handles metric name for this module.
    fn metric_name(value: &str) -> MetricName {
        MetricName::try_new(value.to_string()).expect("test metric name is valid")
    }

    /// Handles run name for this module.
    fn run_name(value: &str) -> RunName {
        RunName::try_new(value.to_string()).expect("test run name is valid")
    }

    /// Handles valid validation result for this module.
    fn valid_validation_result() -> EvaluatorRunResult {
        EvaluatorRunResult {
            status: EvaluatorRunStatus::Passed,
            mode: Some(ScoringMode::Validation),
            aggregate_metrics: vec![],
            run_metrics: vec![],
            public_results: vec![],
            validation_summary: Some(ScoreSummary {
                score: 1.0,
                passed: 1,
                total: 1,
            }),
            official_summary: None,
            logs: vec![],
        }
    }

    /// Verifies that evaluator mode mismatch is rejected.
    #[test]
    fn evaluator_mode_mismatch_is_rejected() {
        let mut result = valid_validation_result();
        result.mode = Some(ScoringMode::Official);
        result.official_summary = Some(ScoreSummary {
            score: 1.0,
            passed: 1,
            total: 1,
        });

        assert!(result.validate_for_mode(ScoringMode::Validation).is_err());
    }

    /// Verifies that evaluator mode can be absent.
    #[test]
    fn evaluator_mode_can_be_absent() {
        let mut result = valid_validation_result();
        result.mode = None;

        assert!(result.validate_for_mode(ScoringMode::Validation).is_ok());
    }

    /// Verifies that evaluator output can rely on declared aggregate metrics.
    #[test]
    fn evaluator_output_with_declared_metrics_is_valid() {
        let mut result = valid_validation_result();
        result
            .complete_metric_result(&MetricSchemaSpec::default(), ScoringMode::Validation)
            .unwrap();

        assert!(result.aggregate_metrics.is_empty());
    }

    /// Verifies minimized primary metrics rank smaller values first.
    #[test]
    fn minimized_primary_metric_ranks_smaller_values_first() {
        let schema = MetricSchemaSpec {
            metrics: vec![MetricDefinitionSpec {
                name: metric_name("latency_ms"),
                label: "Latency".to_string(),
                unit: Some("ms".to_string()),
                direction: MetricDirection::Minimize,
                visibility: MetricVisibility::Public,
                metric_description: None,
            }],
            ranking: RankingSpec {
                primary_metric_name: metric_name("latency_ms"),
                tie_breaker_metric_names: vec![],
            },
        };
        let faster = vec![MetricValue {
            metric_name: metric_name("latency_ms"),
            value: 7.0,
        }];
        let slower = vec![MetricValue {
            metric_name: metric_name("latency_ms"),
            value: 42.0,
        }];

        assert_eq!(
            super::compare_metric_payloads_by_ranking(&schema, &faster, &slower),
            std::cmp::Ordering::Less
        );
    }

    /// Verifies maximized primary metrics rank larger values first.
    #[test]
    fn maximized_primary_metric_ranks_larger_values_first() {
        let schema = MetricSchemaSpec::default();
        let better = vec![MetricValue {
            metric_name: metric_name("score"),
            value: 42.0,
        }];
        let worse = vec![MetricValue {
            metric_name: metric_name("score"),
            value: 7.0,
        }];

        assert_eq!(
            super::compare_metric_payloads_by_ranking(&schema, &better, &worse),
            std::cmp::Ordering::Less
        );
    }

    /// Verifies declared tie-breakers are applied after equal primary metrics.
    #[test]
    fn ranking_uses_declared_tie_breakers() {
        let schema = MetricSchemaSpec {
            metrics: vec![
                MetricDefinitionSpec {
                    name: metric_name("score"),
                    label: "Score".to_string(),
                    unit: None,
                    direction: MetricDirection::Maximize,
                    visibility: MetricVisibility::Public,
                    metric_description: None,
                },
                MetricDefinitionSpec {
                    name: metric_name("passed_cases"),
                    label: "Passed Cases".to_string(),
                    unit: Some("cases".to_string()),
                    direction: MetricDirection::Maximize,
                    visibility: MetricVisibility::Public,
                    metric_description: None,
                },
            ],
            ranking: RankingSpec {
                primary_metric_name: metric_name("score"),
                tie_breaker_metric_names: vec![metric_name("passed_cases")],
            },
        };
        let better = vec![
            MetricValue {
                metric_name: metric_name("score"),
                value: 1.0,
            },
            MetricValue {
                metric_name: metric_name("passed_cases"),
                value: 3.0,
            },
        ];
        let worse = vec![
            MetricValue {
                metric_name: metric_name("score"),
                value: 1.0,
            },
            MetricValue {
                metric_name: metric_name("passed_cases"),
                value: 1.0,
            },
        ];

        assert_eq!(
            super::compare_metric_payloads_by_ranking(&schema, &better, &worse),
            std::cmp::Ordering::Less
        );
    }

    /// Verifies evaluator outputs cannot keep the removed rank_score field.
    #[test]
    fn evaluator_result_rejects_rank_score_field() {
        let raw = serde_json::json!({
            "status": "passed",
            "mode": "validation",
            "rank_score": 1.0,
            "validation_summary": { "score": 1.0, "passed": 1, "total": 1 }
        });

        let error = serde_json::from_value::<EvaluatorRunResult>(raw)
            .expect_err("rank_score should be rejected as an unknown field");
        assert!(error.to_string().contains("rank_score"));
    }

    /// Verifies that unknown aggregate metric is rejected.
    #[test]
    fn unknown_aggregate_metric_is_rejected() {
        let mut result = valid_validation_result();
        result.aggregate_metrics = vec![MetricValue {
            metric_name: metric_name("unknown"),
            value: 1.0,
        }];

        assert!(
            result
                .complete_metric_result(&MetricSchemaSpec::default(), ScoringMode::Validation)
                .is_err()
        );
    }

    /// Verifies that non finite metric value is rejected.
    #[test]
    fn non_finite_metric_value_is_rejected() {
        let mut result = valid_validation_result();
        result.aggregate_metrics = vec![MetricValue {
            metric_name: metric_name("score"),
            value: f64::NAN,
        }];

        assert!(result.validate_for_mode(ScoringMode::Validation).is_err());
    }

    /// Verifies that per run metrics are validated.
    #[test]
    fn per_run_metrics_are_validated() {
        let mut result = valid_validation_result();
        result.aggregate_metrics = vec![MetricValue {
            metric_name: metric_name("score"),
            value: 1.0,
        }];
        result.run_metrics = vec![RunMetricResult {
            run_name: run_name("case-1"),
            metrics: vec![MetricValue {
                metric_name: metric_name("score"),
                value: 1.0,
            }],
        }];

        assert!(
            result
                .complete_metric_result(&MetricSchemaSpec::default(), ScoringMode::Validation)
                .is_ok()
        );
    }

    /// Verifies that validation result rejects official only metrics.
    #[test]
    fn validation_result_rejects_official_only_metrics() {
        let schema = MetricSchemaSpec {
            metrics: vec![MetricDefinitionSpec {
                name: metric_name("private_quality"),
                label: "Private Quality".to_string(),
                unit: None,
                direction: MetricDirection::Maximize,
                visibility: MetricVisibility::Official,
                metric_description: None,
            }],
            ranking: RankingSpec {
                primary_metric_name: metric_name("private_quality"),
                tie_breaker_metric_names: vec![],
            },
        };
        let mut result = valid_validation_result();
        result.aggregate_metrics = vec![MetricValue {
            metric_name: metric_name("private_quality"),
            value: 0.9,
        }];

        assert!(
            result
                .complete_metric_result(&schema, ScoringMode::Validation)
                .is_err()
        );
    }

    /// Verifies completed official output must include the declared primary metric.
    #[test]
    fn official_result_requires_primary_metric() {
        let mut result = valid_validation_result();
        result.mode = Some(ScoringMode::Official);
        result.validation_summary = None;
        result.official_summary = Some(ScoreSummary {
            score: 3.25,
            passed: 1,
            total: 1,
        });

        let error = result
            .complete_metric_result(&MetricSchemaSpec::default(), ScoringMode::Official)
            .expect_err("official result should require primary aggregate metric");
        assert!(error.contains("aggregate_metrics missing primary metric"));
    }

    /// Verifies summary and public case scores accept arbitrary finite values.
    #[test]
    fn summary_and_public_case_scores_accept_arbitrary_finite_values() {
        let summary = ScoreSummary {
            score: 42.0,
            passed: 1,
            total: 1,
        };
        assert!(summary.validate("validation_summary").is_ok());

        let public_case = super::PublicCaseResult {
            case_name: "case-1".to_string(),
            status: EvaluatorCaseStatus::Passed,
            score: -7.5,
            message: None,
        };
        assert!(public_case.validate().is_ok());

        let invalid_summary = ScoreSummary {
            score: f64::INFINITY,
            passed: 1,
            total: 1,
        };
        assert!(invalid_summary.validate("validation_summary").is_err());

        let invalid_public_case = super::PublicCaseResult {
            case_name: "case-2".to_string(),
            status: EvaluatorCaseStatus::Passed,
            score: f64::NAN,
            message: None,
        };
        assert!(invalid_public_case.validate().is_err());
    }

    /// Verifies platform size limits reject result payload expansion.
    #[test]
    fn evaluator_result_size_limits_are_enforced() {
        let mut result = valid_validation_result();
        result.public_results = vec![
            super::PublicCaseResult {
                case_name: "case-1".to_string(),
                status: EvaluatorCaseStatus::Passed,
                score: 1.0,
                message: None,
            },
            super::PublicCaseResult {
                case_name: "case-2".to_string(),
                status: EvaluatorCaseStatus::Passed,
                score: 1.0,
                message: None,
            },
        ];

        let public_result_error = result
            .validate_size_limits(1, 1024)
            .expect_err("public result count should be capped");
        assert!(public_result_error.contains("public_results"));

        let mut result = valid_validation_result();
        result.logs = vec!["abcd".to_string(), "efgh".to_string()];

        let log_error = result
            .validate_size_limits(1024, 7)
            .expect_err("embedded result logs should be capped");
        assert!(log_error.contains("result.logs"));
    }
}

/// Minimal job DTO returned when a solution submission queues an evaluation.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct EvaluationJobDto {
    pub id: EvaluationJobId,
    pub target: TargetName,
    pub status: EvaluationJobStatus,
}

/// Immutable metadata measured from the submitted solution artifact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct SolutionArtifactMetadata {
    /// Compressed ZIP object size in bytes.
    pub artifact_zip_bytes: u64,
    /// Sum of expanded regular-file entry sizes in bytes.
    pub artifact_uncompressed_bytes: u64,
    /// Number of validated archive entries.
    pub artifact_file_count: u64,
    /// SHA-256 digest of the exact submitted ZIP bytes.
    pub artifact_sha256: Sha256Digest,
}

/// Runner payload persisted on an evaluation job.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct EvaluationJobPayload {
    pub artifact_key: StorageKey,
    pub bundle_key: StorageKey,
    pub public_bundle_key: StorageKey,
    pub challenge_name: ChallengeName,
    pub target: TargetName,
}