Skip to main content

visual_rubric/
deterministic.rs

1//! Deterministic browser evidence used before semantic rubric evaluation.
2
3use serde::{Deserialize, Serialize};
4
5/// Current schema version for browser-side deterministic observations.
6pub const DETERMINISTIC_EVIDENCE_SCHEMA_VERSION: u32 = 1;
7
8/// Evidence collected by the browser without a model call.
9#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
10pub struct DeterministicEvidenceV1 {
11    /// Schema discriminator.
12    pub schema_version: u32,
13    /// Normalized observations found in this capture.
14    #[serde(default)]
15    pub observations: Vec<DeterministicObservationV1>,
16}
17
18/// One deterministic observation from a browser capture.
19#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
20pub struct DeterministicObservationV1 {
21    /// Shared observation class used by the visual-run report.
22    pub kind: String,
23    /// Factual summary of the evidence.
24    pub summary: String,
25}
26
27impl DeterministicEvidenceV1 {
28    /// Validates the browser-side evidence envelope.
29    pub fn validate(&self) -> Result<(), Vec<String>> {
30        let mut issues = Vec::new();
31        if self.schema_version != DETERMINISTIC_EVIDENCE_SCHEMA_VERSION {
32            issues.push(format!(
33                "unsupported deterministic evidence schema_version {}; expected {}",
34                self.schema_version, DETERMINISTIC_EVIDENCE_SCHEMA_VERSION
35            ));
36        }
37        for (index, observation) in self.observations.iter().enumerate() {
38            if observation.kind.trim().is_empty() {
39                issues.push(format!(
40                    "deterministic observation {index} has an empty kind"
41                ));
42            }
43            if observation.summary.trim().is_empty() {
44                issues.push(format!(
45                    "deterministic observation {} has an empty summary",
46                    observation.kind
47                ));
48            }
49        }
50        if issues.is_empty() {
51            Ok(())
52        } else {
53            Err(issues)
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn accepts_a_clear_envelope() {
64        DeterministicEvidenceV1 {
65            schema_version: DETERMINISTIC_EVIDENCE_SCHEMA_VERSION,
66            observations: Vec::new(),
67        }
68        .validate()
69        .expect("clear evidence");
70    }
71
72    #[test]
73    fn rejects_empty_observation_fields() {
74        let issues = DeterministicEvidenceV1 {
75            schema_version: DETERMINISTIC_EVIDENCE_SCHEMA_VERSION,
76            observations: vec![DeterministicObservationV1 {
77                kind: String::new(),
78                summary: String::new(),
79            }],
80        }
81        .validate()
82        .expect_err("empty fields");
83        assert_eq!(issues.len(), 2);
84    }
85}