Skip to main content

qualia_core_db/research/
dark_link.rs

1//! Dark link inference — provenance gaps, concealment patterns, confirmation/refutation.
2
3/// Status of a dark link hypothesis.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum DarkLinkStatus {
6    /// Inferred but not yet confirmed.
7    Inferred,
8    /// Confirmed by evidence.
9    Confirmed,
10    /// Refuted by evidence.
11    Refuted,
12    /// Insufficient evidence.
13    Inconclusive,
14}
15
16/// A dark link — a hypothesised causal or informational connection that is
17/// not directly visible in the available data.
18#[derive(Debug, Clone)]
19pub struct DarkLink {
20    pub id: String,
21    pub source: String,
22    pub target: String,
23    pub link_type: String,
24    pub confidence: f64,
25    pub status: DarkLinkStatus,
26    pub evidence: Vec<String>,
27    pub provenance_gaps: Vec<String>,
28}
29
30impl DarkLink {
31    /// Creates a `source`→`target` link of `link_type`.
32    pub fn new(id: &str, source: &str, target: &str, link_type: &str) -> Self {
33        Self {
34            id: id.to_string(),
35            source: source.to_string(),
36            target: target.to_string(),
37            link_type: link_type.to_string(),
38            confidence: 0.0,
39            status: DarkLinkStatus::Inferred,
40            evidence: Vec::new(),
41            provenance_gaps: Vec::new(),
42        }
43    }
44
45    /// Attaches evidence for the link.
46    pub fn add_evidence(&mut self, evidence: &str) {
47        self.evidence.push(evidence.to_string());
48        self.confidence = (self.confidence + 0.2).min(1.0);
49    }
50
51    /// Marks the link confirmed.
52    pub fn confirm(&mut self) {
53        self.status = DarkLinkStatus::Confirmed;
54        self.confidence = 1.0;
55    }
56
57    /// Marks the link refuted.
58    pub fn refute(&mut self) {
59        self.status = DarkLinkStatus::Refuted;
60        self.confidence = 0.0;
61    }
62
63    /// Records a provenance gap.
64    pub fn add_provenance_gap(&mut self, gap: &str) {
65        self.provenance_gaps.push(gap.to_string());
66    }
67}
68
69/// Detect provenance gaps in a list of items — missing source references,
70/// unexplained temporal gaps, or attribution chains with breaks.
71pub fn detect_provenance_gaps(items: &[(String, String, Option<String>)]) -> Vec<String> {
72    let mut gaps = Vec::new();
73    for (id, content, source) in items {
74        if source.is_none() {
75            gaps.push(format!("{id}: missing source attribution"));
76        }
77        // Check for unexplained temporal gaps (simplified: look for date markers).
78        if !content.contains("date") && !content.contains("year") && !content.contains("time") {
79            gaps.push(format!("{id}: no temporal reference found"));
80        }
81    }
82    gaps
83}
84
85/// Detect concealment patterns — redacted content, missing sections,
86/// or suspiciously uniform language across sources.
87pub fn detect_concealment_patterns(items: &[(String, String)]) -> Vec<String> {
88    let mut patterns = Vec::new();
89    for (id, content) in items {
90        if content.contains("[REDACTED]") || content.contains("...") {
91            patterns.push(format!("{id}: contains redaction markers"));
92        }
93        if content.len() < 50 {
94            patterns.push(format!(
95                "{id}: suspiciously short content ({}) bytes",
96                content.len()
97            ));
98        }
99    }
100
101    // Check for suspiciously uniform language (all items identical).
102    if items.len() > 2 {
103        let first = &items[0].1;
104        let all_same = items.iter().all(|(_, c)| c == first);
105        if all_same {
106            patterns.push("all items have identical content — possible templating".to_string());
107        }
108    }
109
110    patterns
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn dark_link_creation() {
119        let dl = DarkLink::new("dl1", "entity_a", "entity_b", "causal");
120        assert_eq!(dl.source, "entity_a");
121        assert_eq!(dl.status, DarkLinkStatus::Inferred);
122        assert_eq!(dl.confidence, 0.0);
123    }
124
125    #[test]
126    fn dark_link_add_evidence() {
127        let mut dl = DarkLink::new("dl1", "a", "b", "causal");
128        dl.add_evidence("Document X confirms the link");
129        assert!(dl.confidence > 0.0);
130        assert_eq!(dl.evidence.len(), 1);
131    }
132
133    #[test]
134    fn dark_link_confirm_refute() {
135        let mut dl = DarkLink::new("dl1", "a", "b", "causal");
136        dl.confirm();
137        assert_eq!(dl.status, DarkLinkStatus::Confirmed);
138        assert_eq!(dl.confidence, 1.0);
139
140        let mut dl2 = DarkLink::new("dl2", "c", "d", "informational");
141        dl2.refute();
142        assert_eq!(dl2.status, DarkLinkStatus::Refuted);
143        assert_eq!(dl2.confidence, 0.0);
144    }
145
146    #[test]
147    fn detect_provenance_gaps_missing_source() {
148        let items = vec![
149            (
150                "i1".to_string(),
151                "Some content with date".to_string(),
152                Some("src1".to_string()),
153            ),
154            (
155                "i2".to_string(),
156                "Content without date ref".to_string(),
157                None,
158            ),
159        ];
160        let gaps = detect_provenance_gaps(&items);
161        assert!(gaps.iter().any(|g| g.contains("i2: missing source")));
162    }
163
164    #[test]
165    fn detect_concealment_redacted() {
166        let items = vec![(
167            "i1".to_string(),
168            "Some [REDACTED] content here that is long enough".to_string(),
169        )];
170        let patterns = detect_concealment_patterns(&items);
171        assert!(patterns.iter().any(|p| p.contains("redaction")));
172    }
173
174    #[test]
175    fn detect_concealment_uniform() {
176        let items = vec![
177            ("i1".to_string(), "identical".to_string()),
178            ("i2".to_string(), "identical".to_string()),
179            ("i3".to_string(), "identical".to_string()),
180        ];
181        let patterns = detect_concealment_patterns(&items);
182        assert!(patterns.iter().any(|p| p.contains("templating")));
183    }
184}