Skip to main content

omega_cognitive_kernel/
error.rs

1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use alloc::string::String;
5use core::fmt;
6use uuid::Uuid;
7
8/// Error returned by the kernel's public operations.
9///
10/// The type implements `std::error::Error` when the `std` feature is enabled; its `Display`
11/// implementation is also available in `no_std` builds.
12///
13/// # Examples
14///
15/// ```
16/// use omega_cognitive_kernel::OmegaError;
17///
18/// let error = OmegaError::ConstraintViolation("risk limit exceeded".into());
19/// assert_eq!(error.to_string(), "Constraint violation: risk limit exceeded");
20/// assert!(matches!(error, OmegaError::ConstraintViolation(_)));
21/// ```
22#[derive(Debug)]
23pub enum OmegaError {
24    /// A requested operation violated a kernel constraint.
25    ConstraintViolation(String),
26    /// A proposed action or patch was rejected by sandbox validation.
27    SandboxRejection(String),
28    /// Execution was blocked by policy or authorization.
29    ExecutionBlocked(String),
30    /// A memory operation failed.
31    Memory(String),
32    /// Loading or persisting kernel state failed.
33    Persist(String),
34    /// The step was not executed.
35    StepNotExecuted {
36        /// Identifier supplied for the step.
37        step_id: Uuid,
38        /// Why the step was not executed.
39        reason: String,
40    },
41    /// The step may have caused external effects, but its execution result is unknown.
42    StepExecutionOutcomeUnknown {
43        /// Identifier supplied for the step.
44        step_id: Uuid,
45        /// Why the outcome could not be established.
46        reason: String,
47    },
48    /// Step execution completed, but committing its state did not finish.
49    StepCommitIncomplete {
50        /// Identifier supplied for the step.
51        step_id: Uuid,
52        /// Commit stage that did not complete.
53        stage: String,
54    },
55    /// The step identifier was already committed, so the duplicate was not executed.
56    StepAlreadyCommitted {
57        /// Identifier supplied for the duplicate step.
58        step_id: Uuid,
59    },
60    /// The step identifier was previously used with a different goal.
61    StepIdConflict {
62        /// Identifier reused for a different goal.
63        step_id: Uuid,
64    },
65    /// Kernel configuration was invalid or could not be loaded.
66    Config(String),
67    /// An underlying error that does not have a more specific kernel variant.
68    Other(anyhow::Error),
69}
70
71impl fmt::Display for OmegaError {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            Self::ConstraintViolation(value) => write!(f, "Constraint violation: {value}"),
75            Self::SandboxRejection(value) => write!(f, "Sandbox rejection: {value}"),
76            Self::ExecutionBlocked(value) => write!(f, "Execution blocked: {value}"),
77            Self::Memory(value) => write!(f, "Memory error: {value}"),
78            Self::Persist(value) => write!(f, "Persistence error: {value}"),
79            Self::StepNotExecuted { step_id, reason } => {
80                write!(f, "Step {step_id} was not executed: {reason}")
81            }
82            Self::StepExecutionOutcomeUnknown { step_id, reason } => write!(
83                f,
84                "Step {step_id} execution outcome is unknown: {reason}; external effects may have occurred"
85            ),
86            Self::StepCommitIncomplete { step_id, stage } => write!(
87                f,
88                "Step {step_id} executed but commit is incomplete at {stage}; do not retry automatically"
89            ),
90            Self::StepAlreadyCommitted { step_id } => write!(
91                f,
92                "Step {step_id} is already committed; duplicate was not executed"
93            ),
94            Self::StepIdConflict { step_id } => write!(
95                f,
96                "Step ID {step_id} was already used with a different goal"
97            ),
98            Self::Config(value) => write!(f, "Config error: {value}"),
99            Self::Other(error) => error.fmt(f),
100        }
101    }
102}
103
104#[cfg(feature = "std")]
105impl std::error::Error for OmegaError {
106    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
107        match self {
108            Self::Other(error) => error.source(),
109            _ => None,
110        }
111    }
112}
113
114impl From<anyhow::Error> for OmegaError {
115    fn from(error: anyhow::Error) -> Self {
116        Self::Other(error)
117    }
118}
119
120/// Result type used by the kernel's public APIs.
121pub type Result<T> = core::result::Result<T, OmegaError>;