Skip to main content

domainforge_core/semantic_pack/
review.rs

1use serde::{Deserialize, Serialize};
2
3use super::schema::ReviewRecord;
4
5/// Parse a JSONL review manifest into review records.
6pub fn parse_review_manifest(jsonl: &str) -> Result<Vec<ReviewRecord>, ReviewError> {
7    let mut records = Vec::new();
8    for (i, line) in jsonl.lines().enumerate() {
9        let line = line.trim();
10        if line.is_empty() {
11            continue;
12        }
13        let record: ReviewRecord =
14            serde_json::from_str(line).map_err(|e| ReviewError::ParseError {
15                line: i + 1,
16                message: e.to_string(),
17            })?;
18        records.push(record);
19    }
20    Ok(records)
21}
22
23/// Validate that all active concepts in the pack have matching review decisions.
24pub fn validate_review_coverage(
25    concept_ids: &[String],
26    records: &[ReviewRecord],
27) -> Vec<ReviewGap> {
28    let reviewed: std::collections::HashSet<&str> = records
29        .iter()
30        .filter(|r| {
31            matches!(
32                r.decision,
33                super::schema::ReviewDecision::Approve
34                    | super::schema::ReviewDecision::MinorAmendmentNoSemanticChange
35            )
36        })
37        .map(|r| r.subject_id.as_str())
38        .collect();
39
40    let mut gaps = Vec::new();
41    for id in concept_ids {
42        if !reviewed.contains(id.as_str()) {
43            gaps.push(ReviewGap {
44                subject_id: id.clone(),
45                reason: "no_matching_approve_decision".to_string(),
46            });
47        }
48    }
49    gaps
50}
51
52/// Validate definition hashes match review manifest.
53pub fn validate_definition_hashes(
54    concept_id: &str,
55    current_hash: &str,
56    records: &[ReviewRecord],
57) -> DefinitionHashResult {
58    let mut matching: Vec<&ReviewRecord> = records
59        .iter()
60        .filter(|r| r.subject_id == concept_id)
61        .collect();
62    matching.sort_by(|a, b| a.decision_id.cmp(&b.decision_id));
63
64    if matching.is_empty() {
65        return DefinitionHashResult::NoReview;
66    }
67
68    let latest = matching.last().unwrap();
69
70    if latest.definition_hash == current_hash {
71        return DefinitionHashResult::Match;
72    }
73
74    // Check for minor amendment
75    if matches!(
76        latest.decision,
77        super::schema::ReviewDecision::MinorAmendmentNoSemanticChange
78    ) {
79        if let (Some(prev), Some(new)) = (
80            &latest.previous_definition_hash,
81            &latest.new_definition_hash,
82        ) {
83            if prev == &latest.definition_hash && new == current_hash {
84                return DefinitionHashResult::MinorAmendment;
85            }
86        }
87    }
88
89    DefinitionHashResult::Mismatch {
90        reviewed_hash: latest.definition_hash.clone(),
91        current_hash: current_hash.to_string(),
92    }
93}
94
95/// Compute hash of the review manifest for deterministic tracking.
96pub fn compute_review_manifest_hash(records: &[ReviewRecord]) -> String {
97    let mut sorted = records.to_vec();
98    sorted.sort_by(|a, b| a.decision_id.cmp(&b.decision_id));
99    let json = serde_json::to_string(&sorted)
100        .expect("failed to serialize review records for manifest hash");
101    super::canonical_json::compute_sha256(json.as_bytes())
102}
103
104#[derive(Debug, Clone)]
105pub struct ReviewGap {
106    pub subject_id: String,
107    pub reason: String,
108}
109
110#[derive(Debug, Clone)]
111pub enum DefinitionHashResult {
112    Match,
113    MinorAmendment,
114    NoReview,
115    Mismatch {
116        reviewed_hash: String,
117        current_hash: String,
118    },
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub enum ReviewError {
123    ParseError { line: usize, message: String },
124    IoError { message: String },
125}