1use crate::validate::types::*;
4use sha2::{Digest, Sha256};
5use std::collections::HashSet;
6
7#[derive(Debug, Clone)]
9pub struct SourceContent {
10 pub url: String,
11 pub index: usize,
12 pub title: String,
13 pub claims: Vec<String>,
14 pub full_text: String,
15 pub confidence: f64,
16}
17
18pub struct CrossSourceVerifier {
20 similarity_threshold: f64,
21 min_sources: usize,
22}
23
24impl Default for CrossSourceVerifier {
25 fn default() -> Self {
26 Self {
27 similarity_threshold: 0.4,
28 min_sources: 2,
29 }
30 }
31}
32
33impl CrossSourceVerifier {
34 pub fn new() -> Self {
35 Self::default()
36 }
37
38 pub fn with_threshold(mut self, t: f64) -> Self {
39 self.similarity_threshold = t;
40 self
41 }
42
43 pub fn verify(&self, sources: &[SourceContent]) -> LayerResult {
45 self.verify_with_query(sources, "")
46 }
47
48 pub fn verify_with_query(&self, sources: &[SourceContent], query: &str) -> LayerResult {
50 let start = std::time::Instant::now();
51 let risk = ValidationRisk::for_query(query);
52
53 if sources.len() < self.min_sources {
54 let high_stakes = risk == ValidationRisk::HighStakes;
55 return LayerResult {
56 layer: ValidationLayerId::V4CrossSource,
57 passed: !high_stakes,
58 score: if high_stakes { 0.15 } else { 0.5 },
59 issues: vec![ValidationIssue {
60 severity: if high_stakes {
61 IssueSeverity::Error
62 } else {
63 IssueSeverity::Info
64 },
65 code: if high_stakes {
66 "V4_INSUFFICIENT_HIGH_STAKES_EVIDENCE".into()
67 } else {
68 "V4_INSUFFICIENT_SOURCES".into()
69 },
70 message: format!(
71 "Need {} independent sources, have {}",
72 self.min_sources,
73 sources.len()
74 ),
75 source_url: None,
76 }],
77 duration_ms: start.elapsed().as_millis() as u64,
78 };
79 }
80
81 let all_claims = self.extract_claims(sources);
82 let clusters = self.cluster_claims(&all_claims);
83 let mut issues = Vec::new();
84 let unique_domains = self.unique_domain_count(sources);
85 if risk == ValidationRisk::HighStakes && unique_domains < self.min_sources {
86 issues.push(ValidationIssue {
87 severity: IssueSeverity::Error,
88 code: "V4_LOW_SOURCE_DIVERSITY".into(),
89 message: format!(
90 "High-stakes query requires {} independent domains, found {}",
91 self.min_sources, unique_domains
92 ),
93 source_url: None,
94 });
95 }
96 let mut scores = Vec::new();
97
98 for cluster in &clusters {
99 let consensus = self.score_consensus(cluster, sources.len());
100 if consensus.consensus_score <= 0.5 && consensus.contradicting_sources > 0 {
101 for c in self.detect_contradictions(cluster) {
102 let mut is_resolved = false;
103 let mut resolution_msg = String::new();
104
105 let d_a = url::Url::parse(&c.claim_a.source_url)
107 .map(|u| u.host_str().unwrap_or("unknown").to_string())
108 .unwrap_or_else(|_| "unknown".to_string());
109 let d_b = url::Url::parse(&c.claim_b.source_url)
110 .map(|u| u.host_str().unwrap_or("unknown").to_string())
111 .unwrap_or_else(|_| "unknown".to_string());
112
113 let crp_contradiction = crate::intelligence::crp::CrpContradiction {
114 claim_a: c.claim_a.text.clone(),
115 source_a_domain: d_a.clone(),
116 source_a_trust: c.claim_a.confidence,
117 source_a_date: None,
118 claim_b: c.claim_b.text.clone(),
119 source_b_domain: d_b.clone(),
120 source_b_trust: c.claim_b.confidence,
121 source_b_date: None,
122 severity: match c.severity {
123 ContradictionSeverity::High => crate::intelligence::crp::Severity::High,
124 ContradictionSeverity::Medium => {
125 crate::intelligence::crp::Severity::Medium
126 }
127 ContradictionSeverity::Low => crate::intelligence::crp::Severity::Low,
128 },
129 };
130
131 let pie = crate::intelligence::pie::PersistentIntelligenceEngine::new().ok();
132 if let Ok(res) =
133 crate::intelligence::crp::resolve(&crp_contradiction, |domain| {
134 if let Some(pie_engine) = &pie {
135 pie_engine.stm.get_trust(domain).unwrap_or(0.5)
136 } else {
137 0.5
138 }
139 })
140 {
141 if res.resolution_type
142 != crate::intelligence::crp::ResolutionType::Unresolved
143 {
144 is_resolved = true;
145 resolution_msg = res.synthesis;
146 }
147 }
148
149 if is_resolved {
150 issues.push(ValidationIssue {
151 severity: IssueSeverity::Info,
152 code: "V4_CONTRADICTION_RESOLVED".into(),
153 message: format!(
154 "{} (CRP Resolved: {})",
155 c.description, resolution_msg
156 ),
157 source_url: Some(c.claim_a.source_url.clone()),
158 });
159 } else {
160 issues.push(ValidationIssue {
161 severity: match c.severity {
162 ContradictionSeverity::High => IssueSeverity::Error,
163 ContradictionSeverity::Medium => IssueSeverity::Warning,
164 ContradictionSeverity::Low => IssueSeverity::Info,
165 },
166 code: "V4_CONTRADICTION".into(),
167 message: c.description.clone(),
168 source_url: Some(c.claim_a.source_url.clone()),
169 });
170 }
171 }
172 }
173 scores.push(consensus.consensus_score);
174 }
175
176 let avg = if scores.is_empty() {
177 1.0
178 } else {
179 scores.iter().sum::<f64>() / scores.len() as f64
180 };
181 let high_ct = issues
182 .iter()
183 .filter(|i| i.severity == IssueSeverity::Error)
184 .count();
185
186 LayerResult {
187 layer: ValidationLayerId::V4CrossSource,
188 passed: high_ct == 0 && avg >= 0.4,
189 score: avg,
190 issues,
191 duration_ms: start.elapsed().as_millis() as u64,
192 }
193 }
194
195 fn unique_domain_count(&self, sources: &[SourceContent]) -> usize {
196 sources
197 .iter()
198 .filter_map(|source| {
199 url::Url::parse(&source.url)
200 .ok()
201 .and_then(|url| url.host_str().map(Self::normalize_host))
202 })
203 .collect::<HashSet<_>>()
204 .len()
205 }
206
207 fn normalize_host(host: &str) -> String {
208 host.strip_prefix("www.")
209 .unwrap_or(host)
210 .to_ascii_lowercase()
211 }
212
213 fn extract_claims(&self, sources: &[SourceContent]) -> Vec<Claim> {
214 sources
215 .iter()
216 .flat_map(|s| {
217 s.claims.iter().enumerate().map(move |(i, text)| Claim {
218 id: format!("{}:{}", &Self::hash_short(&s.url), i),
219 text: text.clone(),
220 normalized: Self::normalize(text),
221 source_url: s.url.clone(),
222 source_index: s.index,
223 confidence: s.confidence,
224 })
225 })
226 .collect()
227 }
228
229 fn normalize(text: &str) -> String {
230 text.to_lowercase()
231 .chars()
232 .filter(|c| c.is_alphanumeric() || c.is_whitespace())
233 .collect::<String>()
234 .split_whitespace()
235 .collect::<Vec<_>>()
236 .join(" ")
237 }
238
239 fn cluster_claims(&self, claims: &[Claim]) -> Vec<Vec<Claim>> {
240 let mut assigned = vec![false; claims.len()];
241 let mut clusters = Vec::new();
242
243 for i in 0..claims.len() {
244 if assigned[i] {
245 continue;
246 }
247 let mut cluster = vec![claims[i].clone()];
248 assigned[i] = true;
249
250 for j in (i + 1)..claims.len() {
251 if assigned[j] || claims[i].source_index == claims[j].source_index {
252 continue;
253 }
254 if Self::jaccard(&claims[i].normalized, &claims[j].normalized)
255 >= self.similarity_threshold
256 {
257 cluster.push(claims[j].clone());
258 assigned[j] = true;
259 }
260 }
261
262 if cluster.len() > 1 {
263 clusters.push(cluster);
264 }
265 }
266
267 clusters
268 }
269
270 fn jaccard(a: &str, b: &str) -> f64 {
271 let a_words: Vec<&str> = a.split_whitespace().collect();
272 let b_words: Vec<&str> = b.split_whitespace().collect();
273 let ba: HashSet<String> = a_words
274 .windows(2)
275 .map(|w| format!("{} {}", w[0], w[1]))
276 .collect();
277 let bb: HashSet<String> = b_words
278 .windows(2)
279 .map(|w| format!("{} {}", w[0], w[1]))
280 .collect();
281 let inter = ba.intersection(&bb).count();
282 let union = ba.union(&bb).count();
283 if union == 0 {
284 if a == b {
287 1.0
288 } else {
289 0.0
290 }
291 } else {
292 inter as f64 / union as f64
293 }
294 }
295
296 fn score_consensus(&self, cluster: &[Claim], total: usize) -> ClaimConsensus {
297 let anchor = &cluster[0];
298 let mut sup = HashSet::new();
299 let mut con = HashSet::new();
300 sup.insert(anchor.source_index);
301
302 for c in &cluster[1..] {
303 if Self::claims_agree(&anchor.normalized, &c.normalized) >= 0.5 {
304 sup.insert(c.source_index);
305 } else {
306 con.insert(c.source_index);
307 }
308 }
309
310 ClaimConsensus {
311 claim: anchor.clone(),
312 supporting_sources: sup.len(),
313 contradicting_sources: con.len(),
314 total_sources: total,
315 consensus_score: sup.len() as f64 / total as f64,
316 }
317 }
318
319 fn claims_agree(a: &str, b: &str) -> f64 {
320 let neg_words = [
322 "not",
323 "no",
324 "never",
325 "neither",
326 "doesnt",
327 "isnt",
328 "cant",
329 "wont",
330 "did not",
331 "has not",
332 "have not",
333 "is not",
334 "are not",
335 "was not",
336 "does not",
337 "will not",
338 "would not",
339 "should not",
340 "could not",
341 ];
342 let a_neg = neg_words.iter().any(|w| a.contains(w));
343 let b_neg = neg_words.iter().any(|w| b.contains(w));
344 if a_neg != b_neg {
345 return 0.2;
346 }
347 Self::jaccard(a, b).min(1.0)
348 }
349
350 fn detect_contradictions(&self, cluster: &[Claim]) -> Vec<Contradiction> {
351 let mut out = Vec::new();
352 for i in 0..cluster.len() {
353 for j in (i + 1)..cluster.len() {
354 if cluster[i].source_index == cluster[j].source_index {
355 continue;
356 }
357 let agr = Self::claims_agree(&cluster[i].normalized, &cluster[j].normalized);
358 if agr < 0.5 {
359 let sev = if agr < 0.2 {
360 ContradictionSeverity::High
361 } else if agr < 0.35 {
362 ContradictionSeverity::Medium
363 } else {
364 ContradictionSeverity::Low
365 };
366 out.push(Contradiction {
367 claim_a: cluster[i].clone(),
368 claim_b: cluster[j].clone(),
369 severity: sev,
370 description: format!(
371 "{sev:?}: \"{}\" vs \"{}\"",
372 cluster[i].text, cluster[j].text
373 ),
374 });
375 }
376 }
377 }
378 out
379 }
380
381 fn hash_short(input: &str) -> String {
382 let mut h = Sha256::new();
383 h.update(input.as_bytes());
384 let bytes = h.finalize();
385 bytes[..4].iter().map(|b| format!("{b:02x}")).collect()
386 }
387}
388
389#[cfg(test)]
390mod tests {
391 use super::*;
392
393 fn src(i: usize, url: &str, claims: &[&str], conf: f64) -> SourceContent {
394 SourceContent {
395 url: url.into(),
396 index: i,
397 title: format!("S{i}"),
398 claims: claims.iter().map(|c| c.to_string()).collect(),
399 full_text: claims.join(". "),
400 confidence: conf,
401 }
402 }
403
404 #[test]
405 fn consistent_sources_pass() {
406 let v = CrossSourceVerifier::new();
407 let r = v.verify(&[
408 src(0, "https://a.com", &["Rust released in 2015"], 0.9),
409 src(1, "https://b.com", &["Rust released in 2015"], 0.8),
410 ]);
411 assert!(r.passed);
412 }
413
414 #[test]
415 fn contradictions_detected() {
416 let v = CrossSourceVerifier::new();
419 let r = v.verify(&[
420 src(0, "https://a.com", &["Rust is a safe language"], 0.9),
421 src(1, "https://b.com", &["Rust is not a safe language"], 0.8),
422 ]);
423 assert!(!r.issues.is_empty());
424 }
425
426 #[test]
427 fn insufficient_sources() {
428 let v = CrossSourceVerifier::new();
429 let r = v.verify(&[src(0, "https://a.com", &["claim"], 0.9)]);
430 assert!(r.passed);
431 assert_eq!(r.score, 0.5);
432 }
433
434 #[test]
435 fn insufficient_high_stakes_sources_fail_closed() {
436 let v = CrossSourceVerifier::new();
437 let r = v.verify_with_query(
438 &[src(
439 0,
440 "https://example.com/medical",
441 &["aspirin helps"],
442 0.9,
443 )],
444 "aspirin dosage for children",
445 );
446 assert!(!r.passed);
447 assert!(r
448 .issues
449 .iter()
450 .any(|i| i.code == "V4_INSUFFICIENT_HIGH_STAKES_EVIDENCE"));
451 }
452
453 #[test]
454 fn low_source_diversity_fails_for_high_stakes_queries() {
455 let v = CrossSourceVerifier::new();
456 let r = v.verify_with_query(
457 &[
458 src(0, "https://example.com/a", &["ETF fees changed"], 0.9),
459 src(1, "https://www.example.com/b", &["ETF fees changed"], 0.8),
460 ],
461 "best ETF for retirement",
462 );
463 assert!(!r.passed);
464 assert!(r.issues.iter().any(|i| i.code == "V4_LOW_SOURCE_DIVERSITY"));
465 }
466
467 #[test]
468 fn jaccard_identical() {
469 assert!(
470 (CrossSourceVerifier::jaccard("the quick brown fox", "the quick brown fox") - 1.0)
471 .abs()
472 < 1e-9
473 );
474 }
475
476 #[test]
477 fn jaccard_disjoint() {
478 assert_eq!(CrossSourceVerifier::jaccard("rust", "python"), 0.0);
480 }
481}