prime-radiant 0.1.0

Universal coherence engine using sheaf Laplacian mathematics for AI safety, hallucination detection, and structural consistency verification in LLMs and distributed systems
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
//! LLM coherence gate for Prime-Radiant.

use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Instant;

use crate::coherence::{CoherenceEngine, CoherenceEnergy};
use crate::execution::ComputeLane;
use crate::governance::PolicyBundle;

use super::config::LlmCoherenceConfig;
use super::error::{Result, RuvLlmIntegrationError};

/// Coherence gate for LLM responses.
///
/// Evaluates LLM outputs against the sheaf graph to detect
/// potential hallucinations and coherence violations.
pub struct LlmCoherenceGate {
    /// Coherence engine (shared reference)
    engine: Arc<CoherenceEngine>,

    /// Policy bundle
    policy: PolicyBundle,

    /// Configuration
    config: LlmCoherenceConfig,
}

impl std::fmt::Debug for LlmCoherenceGate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LlmCoherenceGate")
            .field("policy", &self.policy)
            .field("config", &self.config)
            .finish_non_exhaustive()
    }
}

impl Clone for LlmCoherenceGate {
    fn clone(&self) -> Self {
        Self {
            engine: Arc::clone(&self.engine),
            policy: self.policy.clone(),
            config: self.config.clone(),
        }
    }
}

/// Decision from the LLM coherence gate.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmGateDecision {
    /// Whether the response is allowed
    pub allowed: bool,

    /// Computed coherence energy
    pub energy: f64,

    /// Assigned compute lane
    pub lane: ComputeLane,

    /// Reason for the decision
    pub reason: LlmGateReason,

    /// Coherence analysis details
    pub analysis: CoherenceAnalysis,

    /// Processing time in microseconds
    pub processing_time_us: u64,

    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Reason for the gate decision.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LlmGateReason {
    /// Response is coherent with knowledge graph
    Coherent,

    /// Response energy below threshold
    BelowThreshold {
        /// Computed energy
        energy: f64,
        /// Required threshold
        threshold: f64,
    },

    /// Potential hallucination detected
    HallucinationDetected {
        /// Confidence score
        confidence: f64,
        /// Description of the issue
        description: String,
    },

    /// Semantic inconsistency found
    SemanticInconsistency {
        /// Description of the inconsistency
        description: String,
    },

    /// Citation verification failed
    CitationFailure {
        /// Missing or invalid citations
        citations: Vec<String>,
    },

    /// Escalated to human review
    HumanEscalation {
        /// Reason for escalation
        reason: String,
    },

    /// Response too long
    LengthExceeded {
        /// Actual length
        actual: usize,
        /// Maximum allowed
        maximum: usize,
    },
}

/// Analysis of response coherence.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoherenceAnalysis {
    /// Semantic consistency score (0.0-1.0)
    pub semantic_score: f64,

    /// Factual grounding score (0.0-1.0)
    pub factual_score: f64,

    /// Citation validity score (0.0-1.0)
    pub citation_score: f64,

    /// Hallucination probability (0.0-1.0)
    pub hallucination_prob: f64,

    /// Number of nodes affected
    pub affected_nodes: usize,

    /// Maximum residual in affected subgraph
    pub max_residual: f64,

    /// Total energy in affected subgraph
    pub subgraph_energy: f64,
}

impl Default for CoherenceAnalysis {
    fn default() -> Self {
        Self {
            semantic_score: 1.0,
            factual_score: 1.0,
            citation_score: 1.0,
            hallucination_prob: 0.0,
            affected_nodes: 0,
            max_residual: 0.0,
            subgraph_energy: 0.0,
        }
    }
}

/// Coherence check for a response.
#[derive(Debug, Clone)]
pub struct ResponseCoherence {
    /// Response text
    pub response: String,

    /// Context embedding
    pub context_embedding: Vec<f32>,

    /// Response embedding
    pub response_embedding: Vec<f32>,

    /// Related node IDs in the knowledge graph
    pub related_nodes: Vec<crate::NodeId>,

    /// Session ID (if applicable)
    pub session_id: Option<String>,
}

impl LlmCoherenceGate {
    /// Create a new LLM coherence gate.
    /// Create a new LLM coherence gate with an Arc-wrapped engine.
    pub fn new(
        engine: Arc<CoherenceEngine>,
        policy: PolicyBundle,
        config: LlmCoherenceConfig,
    ) -> Result<Self> {
        Ok(Self {
            engine,
            policy,
            config,
        })
    }

    /// Create a new LLM coherence gate, wrapping the engine in an Arc.
    pub fn from_engine(
        engine: CoherenceEngine,
        policy: PolicyBundle,
        config: LlmCoherenceConfig,
    ) -> Result<Self> {
        Self::new(Arc::new(engine), policy, config)
    }

    /// Evaluate a response for coherence.
    pub fn evaluate(&self, response: &ResponseCoherence) -> Result<LlmGateDecision> {
        let start = Instant::now();

        // Check response length
        if response.response.len() > self.config.max_response_length {
            return Ok(self.create_decision(
                false,
                0.0,
                ComputeLane::Human,
                LlmGateReason::LengthExceeded {
                    actual: response.response.len(),
                    maximum: self.config.max_response_length,
                },
                CoherenceAnalysis::default(),
                start.elapsed().as_micros() as u64,
            ));
        }

        // Compute coherence analysis
        let analysis = self.analyze_coherence(response)?;

        // Determine decision based on analysis
        let (allowed, lane, reason) = self.determine_decision(&analysis);

        Ok(self.create_decision(
            allowed,
            analysis.subgraph_energy,
            lane,
            reason,
            analysis,
            start.elapsed().as_micros() as u64,
        ))
    }

    /// Analyze the coherence of a response.
    fn analyze_coherence(&self, response: &ResponseCoherence) -> Result<CoherenceAnalysis> {
        let mut analysis = CoherenceAnalysis::default();

        // Check if we have related nodes to evaluate against
        if response.related_nodes.is_empty() {
            // No related nodes - can't compute coherence, assume coherent
            return Ok(analysis);
        }

        // Compute semantic consistency if enabled
        if self.config.semantic_consistency {
            analysis.semantic_score = self.compute_semantic_score(response);
        }

        // Compute factual grounding if enabled
        if self.config.factual_grounding {
            analysis.factual_score = self.compute_factual_score(response);
        }

        // Compute citation validity if enabled
        if self.config.citation_verification {
            analysis.citation_score = self.compute_citation_score(response);
        }

        // Estimate hallucination probability
        analysis.hallucination_prob = self.estimate_hallucination_prob(&analysis);

        // Compute subgraph metrics
        analysis.affected_nodes = response.related_nodes.len();

        Ok(analysis)
    }

    /// Compute semantic consistency score.
    fn compute_semantic_score(&self, response: &ResponseCoherence) -> f64 {
        // Compute cosine similarity between context and response embeddings
        if response.context_embedding.is_empty() || response.response_embedding.is_empty() {
            return 1.0;
        }

        let dot: f32 = response
            .context_embedding
            .iter()
            .zip(&response.response_embedding)
            .map(|(a, b)| a * b)
            .sum();

        let mag_a: f32 = response.context_embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
        let mag_b: f32 = response.response_embedding.iter().map(|x| x * x).sum::<f32>().sqrt();

        if mag_a == 0.0 || mag_b == 0.0 {
            return 1.0;
        }

        (dot / (mag_a * mag_b)).max(0.0) as f64
    }

    /// Compute factual grounding score.
    fn compute_factual_score(&self, _response: &ResponseCoherence) -> f64 {
        // Placeholder - would require access to knowledge base
        1.0
    }

    /// Compute citation validity score.
    fn compute_citation_score(&self, _response: &ResponseCoherence) -> f64 {
        // Placeholder - would require citation parsing and verification
        1.0
    }

    /// Estimate hallucination probability.
    fn estimate_hallucination_prob(&self, analysis: &CoherenceAnalysis) -> f64 {
        // Combine scores to estimate hallucination probability
        let combined = (analysis.semantic_score + analysis.factual_score + analysis.citation_score) / 3.0;

        // Higher combined score = lower hallucination probability
        (1.0 - combined) * self.config.hallucination_sensitivity
    }

    /// Determine the gate decision based on analysis.
    fn determine_decision(&self, analysis: &CoherenceAnalysis) -> (bool, ComputeLane, LlmGateReason) {
        // Check for hallucination
        if analysis.hallucination_prob > self.config.hallucination_sensitivity {
            return (
                false,
                ComputeLane::Human,
                LlmGateReason::HallucinationDetected {
                    confidence: analysis.hallucination_prob,
                    description: "Response may contain hallucinated content".to_string(),
                },
            );
        }

        // Check semantic consistency
        if analysis.semantic_score < self.config.coherence_threshold {
            return (
                false,
                ComputeLane::Heavy,
                LlmGateReason::SemanticInconsistency {
                    description: format!(
                        "Semantic score {:.2} below threshold {:.2}",
                        analysis.semantic_score, self.config.coherence_threshold
                    ),
                },
            );
        }

        // Determine lane based on energy
        let lane = self.determine_lane(analysis.subgraph_energy);

        (true, lane, LlmGateReason::Coherent)
    }

    /// Determine the compute lane based on energy.
    fn determine_lane(&self, energy: f64) -> ComputeLane {
        if energy < self.config.lane_thresholds.reflex {
            ComputeLane::Reflex
        } else if energy < self.config.lane_thresholds.retrieval {
            ComputeLane::Retrieval
        } else if energy < self.config.lane_thresholds.heavy {
            ComputeLane::Heavy
        } else {
            ComputeLane::Human
        }
    }

    /// Create a gate decision.
    fn create_decision(
        &self,
        allowed: bool,
        energy: f64,
        lane: ComputeLane,
        reason: LlmGateReason,
        analysis: CoherenceAnalysis,
        processing_time_us: u64,
    ) -> LlmGateDecision {
        LlmGateDecision {
            allowed,
            energy,
            lane,
            reason,
            analysis,
            processing_time_us,
            timestamp: chrono::Utc::now(),
        }
    }

    /// Get the configuration.
    pub fn config(&self) -> &LlmCoherenceConfig {
        &self.config
    }

    /// Get the policy bundle.
    pub fn policy(&self) -> &PolicyBundle {
        &self.policy
    }

    /// Get the coherence engine.
    pub fn engine(&self) -> &CoherenceEngine {
        &self.engine
    }
}

impl LlmGateDecision {
    /// Check if the response is allowed.
    pub fn is_allowed(&self) -> bool {
        self.allowed
    }

    /// Check if escalation is required.
    pub fn requires_escalation(&self) -> bool {
        matches!(self.lane, ComputeLane::Human)
    }
}