symbi-runtime 1.0.0

Agent Runtime System for the Symbi platform
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Error types and recovery strategies for the Agent Runtime System

use std::time::Duration;
use thiserror::Error;

use super::{AgentId, MessageId, PolicyId, RequestId};

/// Main runtime error type
#[derive(Error, Debug, Clone)]
pub enum RuntimeError {
    #[error("Configuration error: {0}")]
    Configuration(#[from] ConfigError),

    #[error("Resource error: {0}")]
    Resource(#[from] ResourceError),

    #[error("Security error: {0}")]
    Security(#[from] SecurityError),

    #[error("Communication error: {0}")]
    Communication(#[from] CommunicationError),

    #[error("Policy error: {0}")]
    Policy(#[from] PolicyError),

    #[error("Sandbox error: {0}")]
    Sandbox(#[from] SandboxError),

    #[error("Scheduler error: {0}")]
    Scheduler(#[from] SchedulerError),

    #[error("Lifecycle error: {0}")]
    Lifecycle(#[from] LifecycleError),

    #[error("Audit error: {0}")]
    Audit(#[from] AuditError),

    #[error("Error handler error: {0}")]
    ErrorHandler(#[from] ErrorHandlerError),

    #[error("Internal error: {0}")]
    Internal(String),
}

/// Configuration-related errors
#[derive(Error, Debug, Clone)]
pub enum ConfigError {
    #[error("Invalid configuration: {0}")]
    Invalid(String),

    #[error("Missing required field: {0}")]
    MissingField(String),

    #[error("Configuration file not found: {0}")]
    FileNotFound(String),

    #[error("Failed to parse configuration: {0}")]
    ParseError(String),
}

/// Resource management errors
#[derive(Error, Debug, Clone)]
pub enum ResourceError {
    #[error("Insufficient resources: {0}")]
    Insufficient(String),

    #[error("Resource allocation failed for agent {agent_id}: {reason}")]
    AllocationFailed { agent_id: AgentId, reason: String },

    #[error("Resource limit exceeded: {0}")]
    LimitExceeded(String),

    #[error("Resource not found: {0}")]
    NotFound(String),

    #[error("Resource monitoring failed: {0}")]
    MonitoringFailed(String),

    #[error("Agent not found: {agent_id}")]
    AgentNotFound { agent_id: AgentId },

    #[error("System is shutting down")]
    ShuttingDown,

    #[error("Allocation already exists for agent: {agent_id}")]
    AllocationExists { agent_id: AgentId },

    #[error("Insufficient resources for requirements: {requirements:?}")]
    InsufficientResources { requirements: String },

    #[error("Policy error: {0}")]
    PolicyError(String),

    #[error("Policy violation: {reason}")]
    PolicyViolation { reason: String },

    #[error("Resource allocation queued: {reason}")]
    AllocationQueued { reason: String },

    #[error("Escalation required: {reason}")]
    EscalationRequired { reason: String },
}

/// Security-related errors
#[derive(Error, Debug, Clone)]
pub enum SecurityError {
    #[error("Authentication failed: {0}")]
    AuthenticationFailed(String),

    #[error("Authorization denied: {0}")]
    AuthorizationDenied(String),

    #[error("Encryption failed: {0}")]
    EncryptionFailed(String),

    #[error("Signature verification failed: {0}")]
    SignatureVerificationFailed(String),

    #[error("Policy violation: {0}")]
    PolicyViolation(String),

    #[error("Sandbox breach detected: {0}")]
    SandboxBreach(String),

    #[error("Key management error: {0}")]
    KeyManagement(String),
}

/// Communication system errors
#[derive(Error, Debug, Clone)]
pub enum CommunicationError {
    #[error("Message delivery failed for message {message_id}: {reason}")]
    DeliveryFailed {
        message_id: MessageId,
        reason: String,
    },

    #[error("Connection failed: {0}")]
    ConnectionFailed(String),

    #[error("Message timeout: {0}")]
    Timeout(String),

    #[error("Invalid message format: {0}")]
    InvalidFormat(String),

    #[error("Rate limit exceeded: {0}")]
    RateLimitExceeded(String),

    #[error("Channel not found: {0}")]
    ChannelNotFound(String),

    #[error("Message too large: {size} bytes, max allowed: {max_size} bytes")]
    MessageTooLarge { size: usize, max_size: usize },

    #[error("Communication system is shutting down")]
    ShuttingDown,

    #[error("Event processing failed: {reason}")]
    EventProcessingFailed { reason: String },

    #[error("Agent not registered: {agent_id}")]
    AgentNotRegistered { agent_id: AgentId },

    #[error("Message not found: {message_id}")]
    MessageNotFound { message_id: MessageId },

    #[error("Request timeout: request {request_id} timed out after {timeout:?}")]
    RequestTimeout {
        request_id: RequestId,
        timeout: Duration,
    },

    #[error("Request cancelled: {request_id}")]
    RequestCancelled { request_id: RequestId },
}

/// Policy enforcement errors
#[derive(Error, Debug, Clone)]
pub enum PolicyError {
    #[error("Policy not found: {policy_id}")]
    NotFound { policy_id: PolicyId },

    #[error("Policy not found: {id}")]
    PolicyNotFound { id: PolicyId },

    #[error("Policy evaluation failed: {0}")]
    EvaluationFailed(String),

    #[error("Policy compilation failed: {0}")]
    CompilationFailed(String),

    #[error("Policy conflict detected: {0}")]
    Conflict(String),

    #[error("Policy engine unavailable: {0}")]
    EngineUnavailable(String),

    #[error("Invalid policy: {reason}")]
    InvalidPolicy { reason: String },
}

/// Sandbox orchestration errors
#[derive(Error, Debug, Clone)]
pub enum SandboxError {
    #[error("Sandbox creation failed: {0}")]
    CreationFailed(String),

    #[error("Sandbox execution failed: {0}")]
    ExecutionFailed(String),

    #[error("Sandbox not found: {0}")]
    NotFound(String),

    #[error("Sandbox not found: {id}")]
    SandboxNotFound { id: String },

    #[error("Snapshot not found: {id}")]
    SnapshotNotFound { id: String },

    #[error("Sandbox termination failed: {0}")]
    TerminationFailed(String),

    #[error("Sandbox monitoring failed: {0}")]
    MonitoringFailed(String),

    #[error("Unsupported security tier: {0}")]
    UnsupportedTier(String),
}

/// Scheduler errors
#[derive(Error, Debug, Clone)]
pub enum SchedulerError {
    #[error("Agent scheduling failed for {agent_id}: {reason}")]
    SchedulingFailed { agent_id: AgentId, reason: String },

    #[error("Agent not found: {agent_id}")]
    AgentNotFound { agent_id: AgentId },

    #[error("Scheduler overloaded: {0}")]
    Overloaded(String),

    #[error("Invalid priority: {0}")]
    InvalidPriority(String),

    #[error("Scheduler shutdown in progress")]
    ShuttingDown,

    #[error("Serialization failed: {0}")]
    SerializationFailed(String),
}

impl From<serde_json::Error> for SchedulerError {
    fn from(error: serde_json::Error) -> Self {
        SchedulerError::SerializationFailed(format!("JSON serialization error: {}", error))
    }
}

/// Lifecycle management errors
#[derive(Error, Debug, Clone)]
pub enum LifecycleError {
    #[error("Agent initialization failed for {agent_id}: {reason}")]
    InitializationFailed { agent_id: AgentId, reason: String },

    #[error("Agent execution failed for {agent_id}: {reason}")]
    ExecutionFailed { agent_id: AgentId, reason: String },

    #[error("Agent termination failed for {agent_id}: {reason}")]
    TerminationFailed { agent_id: AgentId, reason: String },

    #[error("Invalid state transition from {from} to {to}")]
    InvalidStateTransition { from: String, to: String },

    #[error("DSL parsing failed: {0}")]
    DslParsingFailed(String),

    #[error("Agent not found: {agent_id}")]
    AgentNotFound { agent_id: AgentId },

    #[error("Event processing failed: {reason}")]
    EventProcessingFailed { reason: String },

    #[error("System is shutting down")]
    ShuttingDown,

    #[error("Resource exhausted: {reason}")]
    ResourceExhausted { reason: String },
}

/// Audit trail errors
#[derive(Error, Debug, Clone)]
pub enum AuditError {
    #[error("Audit logging failed: {0}")]
    LoggingFailed(String),

    #[error("Audit verification failed: {0}")]
    VerificationFailed(String),

    #[error("Audit query failed: {0}")]
    QueryFailed(String),

    #[error("Audit storage full: {0}")]
    StorageFull(String),

    #[error("Audit trail corrupted: {0}")]
    Corrupted(String),

    #[error("Record not found: {id}")]
    RecordNotFound { id: String },

    #[error("Export failed: {reason}")]
    ExportFailed { reason: String },

    #[error("Unsupported format: {format}")]
    UnsupportedFormat { format: String },
}

/// Error handler errors
#[derive(Error, Debug, Clone)]
pub enum ErrorHandlerError {
    #[error("Configuration error: {reason}")]
    ConfigurationError { reason: String },

    #[error("Event processing failed: {reason}")]
    EventProcessingFailed { reason: String },

    #[error("Error handler is shutting down")]
    ShuttingDown,
}

/// Recovery strategies for different types of errors
#[derive(Debug, Clone)]
pub enum RecoveryStrategy {
    /// Retry the operation with exponential backoff
    Retry {
        max_attempts: u32,
        backoff: Duration,
    },
    /// Restart the agent, optionally preserving state
    Restart { preserve_state: bool },
    /// Failover to a backup agent
    Failover { backup_agent: Option<AgentId> },
    /// Terminate the agent with cleanup
    Terminate { cleanup: bool },
    /// Manual intervention required
    Manual { reason: String },
    /// No recovery possible
    None,
}

impl Default for RecoveryStrategy {
    fn default() -> Self {
        RecoveryStrategy::Retry {
            max_attempts: 3,
            backoff: Duration::from_secs(1),
        }
    }
}

/// Error recovery configuration
#[derive(Debug, Clone)]
pub struct ErrorRecoveryConfig {
    pub default_strategy: RecoveryStrategy,
    pub max_recovery_attempts: u32,
    pub recovery_timeout: Duration,
    pub escalation_threshold: u32,
}

impl Default for ErrorRecoveryConfig {
    fn default() -> Self {
        Self {
            default_strategy: RecoveryStrategy::default(),
            max_recovery_attempts: 5,
            recovery_timeout: Duration::from_secs(300), // 5 minutes
            escalation_threshold: 10,
        }
    }
}

/// Error context for better debugging and recovery
#[derive(Debug, Clone)]
pub struct ErrorContext {
    pub agent_id: Option<AgentId>,
    pub operation: String,
    pub timestamp: std::time::SystemTime,
    pub recovery_attempts: u32,
    pub additional_info: std::collections::HashMap<String, String>,
}

impl ErrorContext {
    pub fn new(operation: String) -> Self {
        Self {
            agent_id: None,
            operation,
            timestamp: std::time::SystemTime::now(),
            recovery_attempts: 0,
            additional_info: std::collections::HashMap::new(),
        }
    }

    pub fn with_agent(mut self, agent_id: AgentId) -> Self {
        self.agent_id = Some(agent_id);
        self
    }

    pub fn with_info(mut self, key: String, value: String) -> Self {
        self.additional_info.insert(key, value);
        self
    }

    pub fn increment_attempts(&mut self) {
        self.recovery_attempts += 1;
    }
}

/// Result type with error context
pub type RuntimeResult<T> = Result<T, RuntimeError>;

/// Trait for error recovery
pub trait ErrorRecovery {
    fn get_recovery_strategy(&self, error: &RuntimeError) -> RecoveryStrategy;
    fn should_retry(&self, error: &RuntimeError, attempts: u32) -> bool;
    fn escalate_error(&self, error: &RuntimeError, context: &ErrorContext);
}