omega-cognitive-kernel 1.0.0

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
Documentation
#[allow(unused_imports)]
use crate::prelude::*;

use alloc::string::String;
use core::fmt;
use uuid::Uuid;

/// Error returned by the kernel's public operations.
///
/// The type implements `std::error::Error` when the `std` feature is enabled; its `Display`
/// implementation is also available in `no_std` builds.
///
/// # Examples
///
/// ```
/// use omega_cognitive_kernel::OmegaError;
///
/// let error = OmegaError::ConstraintViolation("risk limit exceeded".into());
/// assert_eq!(error.to_string(), "Constraint violation: risk limit exceeded");
/// assert!(matches!(error, OmegaError::ConstraintViolation(_)));
/// ```
#[derive(Debug)]
pub enum OmegaError {
    /// A requested operation violated a kernel constraint.
    ConstraintViolation(String),
    /// A proposed action or patch was rejected by sandbox validation.
    SandboxRejection(String),
    /// Execution was blocked by policy or authorization.
    ExecutionBlocked(String),
    /// A memory operation failed.
    Memory(String),
    /// Loading or persisting kernel state failed.
    Persist(String),
    /// The step was not executed.
    StepNotExecuted {
        /// Identifier supplied for the step.
        step_id: Uuid,
        /// Why the step was not executed.
        reason: String,
    },
    /// The step may have caused external effects, but its execution result is unknown.
    StepExecutionOutcomeUnknown {
        /// Identifier supplied for the step.
        step_id: Uuid,
        /// Why the outcome could not be established.
        reason: String,
    },
    /// Step execution completed, but committing its state did not finish.
    StepCommitIncomplete {
        /// Identifier supplied for the step.
        step_id: Uuid,
        /// Commit stage that did not complete.
        stage: String,
    },
    /// The step identifier was already committed, so the duplicate was not executed.
    StepAlreadyCommitted {
        /// Identifier supplied for the duplicate step.
        step_id: Uuid,
    },
    /// The step identifier was previously used with a different goal.
    StepIdConflict {
        /// Identifier reused for a different goal.
        step_id: Uuid,
    },
    /// Kernel configuration was invalid or could not be loaded.
    Config(String),
    /// An underlying error that does not have a more specific kernel variant.
    Other(anyhow::Error),
}

impl fmt::Display for OmegaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ConstraintViolation(value) => write!(f, "Constraint violation: {value}"),
            Self::SandboxRejection(value) => write!(f, "Sandbox rejection: {value}"),
            Self::ExecutionBlocked(value) => write!(f, "Execution blocked: {value}"),
            Self::Memory(value) => write!(f, "Memory error: {value}"),
            Self::Persist(value) => write!(f, "Persistence error: {value}"),
            Self::StepNotExecuted { step_id, reason } => {
                write!(f, "Step {step_id} was not executed: {reason}")
            }
            Self::StepExecutionOutcomeUnknown { step_id, reason } => write!(
                f,
                "Step {step_id} execution outcome is unknown: {reason}; external effects may have occurred"
            ),
            Self::StepCommitIncomplete { step_id, stage } => write!(
                f,
                "Step {step_id} executed but commit is incomplete at {stage}; do not retry automatically"
            ),
            Self::StepAlreadyCommitted { step_id } => write!(
                f,
                "Step {step_id} is already committed; duplicate was not executed"
            ),
            Self::StepIdConflict { step_id } => write!(
                f,
                "Step ID {step_id} was already used with a different goal"
            ),
            Self::Config(value) => write!(f, "Config error: {value}"),
            Self::Other(error) => error.fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for OmegaError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Other(error) => error.source(),
            _ => None,
        }
    }
}

impl From<anyhow::Error> for OmegaError {
    fn from(error: anyhow::Error) -> Self {
        Self::Other(error)
    }
}

/// Result type used by the kernel's public APIs.
pub type Result<T> = core::result::Result<T, OmegaError>;