use crate::error::{CoherenceError, ComputationError, GovernanceError, SubstrateError};
use crate::types::{EdgeId, NodeId, WitnessId};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum RuvllmIntegrationError {
#[error("Context conversion failed: {0}")]
ContextConversionFailed(String),
#[error("Response conversion failed: {0}")]
ResponseConversionFailed(String),
#[error("Embedding dimension mismatch: expected {expected}, got {actual}")]
EmbeddingDimensionMismatch {
expected: usize,
actual: usize,
},
#[error("Failed to extract claims from response: {0}")]
ClaimExtractionFailed(String),
#[error("Failed to detect semantic relations: {0}")]
SemanticRelationFailed(String),
#[error("Coherence validation timed out after {timeout_ms}ms")]
ValidationTimeout {
timeout_ms: u64,
},
#[error("Failed to create generation witness: {0}")]
WitnessCreationFailed(String),
#[error("Witness chain integrity violation: {0}")]
WitnessChainIntegrity(String),
#[error("Failed to link witnesses: inference={inference_id}, coherence={coherence_id}")]
WitnessLinkFailed {
inference_id: String,
coherence_id: WitnessId,
},
#[error("Hash chain computation failed: {0}")]
HashChainFailed(String),
#[error("Pattern not found: {0}")]
PatternNotFound(String),
#[error("Failed to extract embeddings from pattern: {0}")]
EmbeddingExtractionFailed(String),
#[error("Restriction map training failed: {0}")]
RestrictionMapTrainingFailed(String),
#[error("Failed to process verdict: {0}")]
VerdictProcessingFailed(String),
#[error("Pattern consolidation failed: {0}")]
ConsolidationFailed(String),
#[error("Memory entry conversion to node failed: {0}")]
MemoryConversionFailed(String),
#[error("Memory type not supported for coherence tracking: {0}")]
UnsupportedMemoryType(String),
#[error("Failed to find related memories: {0}")]
RelatedMemorySearchFailed(String),
#[error("Memory coherence check failed: node={node_id}")]
MemoryCoherenceCheckFailed {
node_id: NodeId,
},
#[error("Circular memory reference detected: {0}")]
CircularMemoryReference(String),
#[error("Confidence computation failed: {0}")]
ConfidenceComputationFailed(String),
#[error("Invalid energy scale: {scale} (must be positive)")]
InvalidEnergyScale {
scale: f32,
},
#[error("Confidence threshold out of range: {threshold} (must be 0.0-1.0)")]
InvalidConfidenceThreshold {
threshold: f32,
},
#[error("Energy breakdown unavailable for confidence explanation")]
EnergyBreakdownUnavailable,
#[error("Coherence error: {0}")]
Coherence(#[from] CoherenceError),
#[error("Substrate error: {0}")]
Substrate(#[from] SubstrateError),
#[error("Governance error: {0}")]
Governance(#[from] GovernanceError),
#[error("Computation error: {0}")]
Computation(#[from] ComputationError),
#[error("Internal error: {0}")]
Internal(String),
#[error("Configuration error: {0}")]
Config(String),
}
pub type RuvllmIntegrationResult<T> = std::result::Result<T, RuvllmIntegrationError>;
pub type RuvLlmIntegrationError = RuvllmIntegrationError;
pub type Result<T> = RuvllmIntegrationResult<T>;
impl RuvllmIntegrationError {
pub fn context_conversion(msg: impl Into<String>) -> Self {
Self::ContextConversionFailed(msg.into())
}
pub fn response_conversion(msg: impl Into<String>) -> Self {
Self::ResponseConversionFailed(msg.into())
}
pub fn witness_creation(msg: impl Into<String>) -> Self {
Self::WitnessCreationFailed(msg.into())
}
pub fn pattern_not_found(pattern_id: impl Into<String>) -> Self {
Self::PatternNotFound(pattern_id.into())
}
pub fn restriction_training(msg: impl Into<String>) -> Self {
Self::RestrictionMapTrainingFailed(msg.into())
}
pub fn memory_conversion(msg: impl Into<String>) -> Self {
Self::MemoryConversionFailed(msg.into())
}
pub fn confidence(msg: impl Into<String>) -> Self {
Self::ConfidenceComputationFailed(msg.into())
}
pub fn internal(msg: impl Into<String>) -> Self {
Self::Internal(msg.into())
}
pub fn config(msg: impl Into<String>) -> Self {
Self::Config(msg.into())
}
pub fn is_validation_error(&self) -> bool {
matches!(
self,
Self::ContextConversionFailed(_)
| Self::ResponseConversionFailed(_)
| Self::EmbeddingDimensionMismatch { .. }
| Self::ClaimExtractionFailed(_)
| Self::SemanticRelationFailed(_)
| Self::ValidationTimeout { .. }
)
}
pub fn is_witness_error(&self) -> bool {
matches!(
self,
Self::WitnessCreationFailed(_)
| Self::WitnessChainIntegrity(_)
| Self::WitnessLinkFailed { .. }
| Self::HashChainFailed(_)
)
}
pub fn is_pattern_error(&self) -> bool {
matches!(
self,
Self::PatternNotFound(_)
| Self::EmbeddingExtractionFailed(_)
| Self::RestrictionMapTrainingFailed(_)
| Self::VerdictProcessingFailed(_)
| Self::ConsolidationFailed(_)
)
}
pub fn is_memory_error(&self) -> bool {
matches!(
self,
Self::MemoryConversionFailed(_)
| Self::UnsupportedMemoryType(_)
| Self::RelatedMemorySearchFailed(_)
| Self::MemoryCoherenceCheckFailed { .. }
| Self::CircularMemoryReference(_)
)
}
pub fn is_confidence_error(&self) -> bool {
matches!(
self,
Self::ConfidenceComputationFailed(_)
| Self::InvalidEnergyScale { .. }
| Self::InvalidConfidenceThreshold { .. }
| Self::EnergyBreakdownUnavailable
)
}
pub fn adr_reference(&self) -> Option<&'static str> {
if self.is_validation_error() {
Some(super::adr_references::COHERENCE_VALIDATOR)
} else if self.is_witness_error() {
Some(super::adr_references::UNIFIED_WITNESS)
} else if self.is_pattern_error() {
Some(super::adr_references::PATTERN_BRIDGE)
} else if self.is_memory_error() {
Some(super::adr_references::MEMORY_AS_NODES)
} else if self.is_confidence_error() {
Some(super::adr_references::CONFIDENCE_FROM_ENERGY)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = RuvllmIntegrationError::context_conversion("invalid format");
assert!(err.is_validation_error());
assert_eq!(
err.adr_reference(),
Some(super::super::adr_references::COHERENCE_VALIDATOR)
);
}
#[test]
fn test_witness_error() {
let err = RuvllmIntegrationError::witness_creation("chain broken");
assert!(err.is_witness_error());
assert!(!err.is_validation_error());
}
#[test]
fn test_pattern_error() {
let err = RuvllmIntegrationError::pattern_not_found("pattern-123");
assert!(err.is_pattern_error());
}
#[test]
fn test_memory_error() {
let err = RuvllmIntegrationError::memory_conversion("embedding missing");
assert!(err.is_memory_error());
}
#[test]
fn test_confidence_error() {
let err = RuvllmIntegrationError::InvalidEnergyScale { scale: -1.0 };
assert!(err.is_confidence_error());
}
#[test]
fn test_error_display() {
let err = RuvllmIntegrationError::EmbeddingDimensionMismatch {
expected: 768,
actual: 512,
};
let msg = err.to_string();
assert!(msg.contains("768"));
assert!(msg.contains("512"));
}
}