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
#[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>;