Skip to main content

clawft_kernel/
error.rs

1//! Kernel error types.
2//!
3//! All kernel operations return [`KernelError`] for typed error
4//! handling. The error variants cover process table operations,
5//! service lifecycle, IPC, and boot sequence failures.
6
7use crate::process::ProcessState;
8
9/// Kernel-level errors.
10#[non_exhaustive]
11#[derive(Debug, thiserror::Error)]
12pub enum KernelError {
13    /// Process not found in the process table.
14    #[error("process not found: PID {pid}")]
15    ProcessNotFound {
16        /// The PID that was looked up.
17        pid: u64,
18    },
19
20    /// Invalid process state transition.
21    #[error("invalid state transition for PID {pid}: {from} -> {to}")]
22    InvalidStateTransition {
23        /// The affected PID.
24        pid: u64,
25        /// Current state.
26        from: ProcessState,
27        /// Requested state.
28        to: ProcessState,
29    },
30
31    /// Process table has reached maximum capacity.
32    #[error("process table full (max: {max})")]
33    ProcessTableFull {
34        /// Maximum number of processes allowed.
35        max: u32,
36    },
37
38    /// Service-related error.
39    #[error("service error: {0}")]
40    Service(String),
41
42    /// Boot sequence error.
43    #[error("boot error: {0}")]
44    Boot(String),
45
46    /// IPC / messaging error.
47    #[error("ipc error: {0}")]
48    Ipc(String),
49
50    /// Kernel is in wrong state for requested operation.
51    #[error("kernel state error: expected {expected}, got {actual}")]
52    WrongState {
53        /// Expected state.
54        expected: String,
55        /// Actual state.
56        actual: String,
57    },
58
59    /// Capability check denied an action.
60    #[error("capability denied for PID {pid}: cannot {action} -- {reason}")]
61    CapabilityDenied {
62        /// The PID of the process that was denied.
63        pid: u64,
64        /// The action that was attempted.
65        action: String,
66        /// Why the action was denied.
67        reason: String,
68    },
69
70    /// Resource limit exceeded.
71    #[error("resource limit exceeded for PID {pid}: {resource} ({current} > {limit})")]
72    ResourceLimitExceeded {
73        /// The PID of the process.
74        pid: u64,
75        /// Name of the resource (memory, cpu_time, etc.).
76        resource: String,
77        /// Current usage value.
78        current: u64,
79        /// Configured limit.
80        limit: u64,
81    },
82
83    /// Agent spawn failed.
84    #[error("spawn failed for agent '{agent_id}': {reason}")]
85    SpawnFailed {
86        /// The agent that was being spawned.
87        agent_id: String,
88        /// Why the spawn failed.
89        reason: String,
90    },
91
92    /// Spawn backend not available (defined but not yet implemented).
93    #[error("backend not available: {backend} ({reason})")]
94    BackendNotAvailable {
95        /// The backend that was requested.
96        backend: String,
97        /// Why the backend is not available.
98        reason: String,
99    },
100
101    /// Operation timed out.
102    #[error("timeout: {operation} after {duration_ms}ms")]
103    Timeout {
104        /// Description of the operation that timed out.
105        operation: String,
106        /// How long we waited before timing out.
107        duration_ms: u64,
108    },
109
110    /// Configuration error.
111    #[error("config error: {0}")]
112    Config(String),
113
114    /// Disk quota exceeded (K1-G4).
115    #[cfg(feature = "os-patterns")]
116    #[error("disk quota exceeded for agent '{agent_id}': {current_bytes} > {limit_bytes} bytes")]
117    QuotaExceeded {
118        /// Agent that exceeded its quota.
119        agent_id: String,
120        /// Current disk usage.
121        current_bytes: u64,
122        /// Configured limit.
123        limit_bytes: u64,
124    },
125
126    /// Mesh networking error.
127    #[error("mesh error: {0}")]
128    Mesh(String),
129
130    /// Wraps a generic error from downstream crates.
131    #[error(transparent)]
132    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
133}
134
135/// Convenience alias for kernel results.
136pub type KernelResult<T> = Result<T, KernelError>;