mielin-cells 0.1.0-rc.1

Agent SDK providing agent lifecycle management, policy execution, and inter-agent communication
Documentation
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
425
426
427
428
429
430
431
432
433
//! Migration validation types

use super::audit::MigrationAuditLog;
use super::delta::DeltaSnapshot;
use super::recovery::RollbackInfo;
use crate::migration::functions::simple_checksum;
use crate::{Agent, CellError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Requirements for an agent to be migrated
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AgentRequirements {
    /// Required architecture
    pub architecture: Option<String>,
    /// Required memory
    pub required_memory: u64,
    /// Required storage
    pub required_storage: u64,
    /// Required WASM features
    pub required_wasm_features: Vec<String>,
    /// Current state size
    pub state_size: usize,
    /// Agent version
    pub version: String,
}

/// Target node capabilities for compatibility checking
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NodeCapabilities {
    /// Supported architectures
    pub architectures: Vec<String>,
    /// Available memory in bytes
    pub available_memory: u64,
    /// Available storage in bytes
    pub available_storage: u64,
    /// Supported WASM features
    pub wasm_features: Vec<String>,
    /// Maximum agent state size
    pub max_state_size: usize,
    /// Node version string
    pub version: String,
    /// Minimum compatible version
    pub min_compatible_version: String,
}

impl NodeCapabilities {
    /// Create capabilities with default values
    pub fn new() -> Self {
        Self {
            architectures: vec!["x86_64".to_string(), "aarch64".to_string()],
            available_memory: 8 * 1024 * 1024 * 1024,
            available_storage: 100 * 1024 * 1024 * 1024,
            wasm_features: vec!["bulk-memory".to_string(), "simd".to_string()],
            max_state_size: 1024 * 1024 * 1024,
            version: "0.1.0".to_string(),
            min_compatible_version: "0.1.0".to_string(),
        }
    }
}

/// Compatibility check result
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompatibilityStatus {
    /// Fully compatible
    Compatible,
    /// Compatible with warnings
    CompatibleWithWarnings(Vec<String>),
    /// Incompatible
    Incompatible(String),
}

impl CompatibilityStatus {
    /// Check if migration can proceed
    pub fn can_proceed(&self) -> bool {
        !matches!(self, Self::Incompatible(_))
    }

    /// Get warnings if any
    pub fn warnings(&self) -> Vec<String> {
        match self {
            Self::CompatibleWithWarnings(warnings) => warnings.clone(),
            _ => vec![],
        }
    }
}

/// Result of post-migration verification
#[derive(Debug, Clone)]
pub struct VerificationResult {
    /// Whether verification passed
    pub passed: bool,
    /// State checksum matches
    pub checksum_valid: bool,
    /// State size matches
    pub size_valid: bool,
    /// Agent is responsive
    pub agent_responsive: bool,
    /// Verification details
    pub details: Vec<String>,
}

impl VerificationResult {
    /// Create a successful verification result
    pub fn success() -> Self {
        Self {
            passed: true,
            checksum_valid: true,
            size_valid: true,
            agent_responsive: true,
            details: vec!["All verification checks passed".to_string()],
        }
    }

    /// Create a failed verification result
    pub fn failure(reason: &str) -> Self {
        Self {
            passed: false,
            checksum_valid: false,
            size_valid: false,
            agent_responsive: false,
            details: vec![reason.to_string()],
        }
    }
}

/// Pre-migration compatibility validator
#[derive(Debug, Clone)]
pub struct CompatibilityValidator;

impl CompatibilityValidator {
    /// Check if target node can accept the agent
    pub fn check_compatibility(
        requirements: &AgentRequirements,
        capabilities: &NodeCapabilities,
    ) -> CompatibilityStatus {
        let mut warnings = Vec::new();
        if let Some(ref arch) = requirements.architecture {
            if !capabilities.architectures.contains(arch) {
                return CompatibilityStatus::Incompatible(format!(
                    "Target does not support architecture: {}",
                    arch
                ));
            }
        }
        if requirements.required_memory > capabilities.available_memory {
            return CompatibilityStatus::Incompatible(format!(
                "Insufficient memory: required {} bytes, available {} bytes",
                requirements.required_memory, capabilities.available_memory
            ));
        }
        let memory_headroom = capabilities
            .available_memory
            .saturating_sub(requirements.required_memory);
        if memory_headroom < capabilities.available_memory / 5 {
            warnings.push(format!(
                "Low memory headroom: only {} bytes available after migration",
                memory_headroom
            ));
        }
        if requirements.required_storage > capabilities.available_storage {
            return CompatibilityStatus::Incompatible(format!(
                "Insufficient storage: required {} bytes, available {} bytes",
                requirements.required_storage, capabilities.available_storage
            ));
        }
        if requirements.state_size > capabilities.max_state_size {
            return CompatibilityStatus::Incompatible(format!(
                "Agent state too large: {} bytes exceeds maximum {} bytes",
                requirements.state_size, capabilities.max_state_size
            ));
        }
        for feature in &requirements.required_wasm_features {
            if !capabilities.wasm_features.contains(feature) {
                return CompatibilityStatus::Incompatible(format!(
                    "Target does not support WASM feature: {}",
                    feature
                ));
            }
        }
        if !Self::is_version_compatible(&requirements.version, &capabilities.min_compatible_version)
        {
            return CompatibilityStatus::Incompatible(format!(
                "Version incompatible: agent version {} not compatible with target minimum {}",
                requirements.version, capabilities.min_compatible_version
            ));
        }
        if warnings.is_empty() {
            CompatibilityStatus::Compatible
        } else {
            CompatibilityStatus::CompatibleWithWarnings(warnings)
        }
    }

    /// Check if two versions are compatible (simple semver check)
    fn is_version_compatible(agent_version: &str, min_version: &str) -> bool {
        let parse_version = |v: &str| -> (u32, u32, u32) {
            let parts: Vec<&str> = v.split('.').collect();
            let major = parts.first().and_then(|s| s.parse().ok()).unwrap_or(0);
            let minor = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
            let patch = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
            (major, minor, patch)
        };
        let agent = parse_version(agent_version);
        let min = parse_version(min_version);
        agent.0 == min.0 && (agent.1 > min.1 || (agent.1 == min.1 && agent.2 >= min.2))
    }
}

/// Post-migration state verifier
#[derive(Debug, Clone)]
pub struct StateVerifier;

impl StateVerifier {
    /// Verify that the migrated state matches the original
    pub fn verify_state(
        original_state: &[u8],
        migrated_state: &[u8],
        original_checksum: u32,
    ) -> VerificationResult {
        let mut result = VerificationResult {
            passed: true,
            checksum_valid: false,
            size_valid: false,
            agent_responsive: true,
            details: Vec::new(),
        };
        if original_state.len() == migrated_state.len() {
            result.size_valid = true;
            result
                .details
                .push(format!("Size verified: {} bytes", original_state.len()));
        } else {
            result.passed = false;
            result.details.push(format!(
                "Size mismatch: original {} bytes, migrated {} bytes",
                original_state.len(),
                migrated_state.len()
            ));
        }
        let migrated_checksum = simple_checksum(migrated_state);
        if migrated_checksum == original_checksum {
            result.checksum_valid = true;
            result.details.push("Checksum verified".to_string());
        } else {
            result.passed = false;
            result.details.push(format!(
                "Checksum mismatch: expected {}, got {}",
                original_checksum, migrated_checksum
            ));
        }
        result
    }

    /// Verify a delta snapshot can be applied correctly
    pub fn verify_delta(
        base_state: &[u8],
        delta: &DeltaSnapshot,
        expected_checksum: u32,
    ) -> VerificationResult {
        let mut result = VerificationResult {
            passed: true,
            checksum_valid: false,
            size_valid: false,
            agent_responsive: true,
            details: Vec::new(),
        };
        if delta.checksum == expected_checksum {
            result.checksum_valid = true;
            result.details.push("Delta checksum verified".to_string());
        } else {
            result.passed = false;
            result.checksum_valid = false;
            result.details.push(format!(
                "Delta checksum mismatch: expected {}, got {}",
                expected_checksum, delta.checksum
            ));
        }
        if delta.total_size == base_state.len() {
            result.size_valid = true;
            result
                .details
                .push("Delta size compatible with base state".to_string());
        } else {
            result.passed = false;
            result.size_valid = false;
            result.details.push(format!(
                "Delta size mismatch: delta expects {} bytes, base has {} bytes",
                delta.total_size,
                base_state.len()
            ));
        }
        result
    }
}

/// Coordinated migration validator that ties everything together
#[derive(Debug)]
pub struct MigrationValidator {
    /// Audit log
    audit_log: MigrationAuditLog,
    /// Active rollback info (keyed by migration_id)
    rollback_info: HashMap<[u8; 16], RollbackInfo>,
    /// Maximum rollback info age in seconds
    rollback_max_age_secs: u64,
}

impl MigrationValidator {
    /// Create a new migration validator
    pub fn new() -> Self {
        Self {
            audit_log: MigrationAuditLog::new(),
            rollback_info: HashMap::new(),
            rollback_max_age_secs: 3600,
        }
    }

    /// Set maximum rollback info age
    pub fn set_rollback_max_age(&mut self, secs: u64) {
        self.rollback_max_age_secs = secs;
    }

    /// Pre-migration validation
    pub fn validate_pre_migration(
        &mut self,
        migration_id: [u8; 16],
        agent: &Agent,
        requirements: &AgentRequirements,
        target_capabilities: &NodeCapabilities,
    ) -> Result<CompatibilityStatus, CellError> {
        let status = CompatibilityValidator::check_compatibility(requirements, target_capabilities);
        self.audit_log
            .log_validation(migration_id, *agent.id().as_bytes(), &status);
        Ok(status)
    }

    /// Capture rollback info before migration
    pub fn capture_rollback_info(
        &mut self,
        migration_id: [u8; 16],
        agent: &Agent,
        state: &[u8],
    ) -> Result<(), CellError> {
        let rollback = RollbackInfo::capture(agent, state)?;
        self.rollback_info.insert(migration_id, rollback);
        self.audit_log.log(super::audit::AuditEntry::new(
            migration_id,
            *agent.id().as_bytes(),
            super::audit::AuditEventType::SnapshotCaptured,
            format!("Rollback info captured: {} bytes", state.len()),
            true,
        ));
        Ok(())
    }

    /// Verify post-migration state
    pub fn verify_post_migration(
        &mut self,
        migration_id: [u8; 16],
        agent_id: [u8; 16],
        original_state: &[u8],
        migrated_state: &[u8],
    ) -> VerificationResult {
        let original_checksum = simple_checksum(original_state);
        let result = StateVerifier::verify_state(original_state, migrated_state, original_checksum);
        self.audit_log
            .log_verification(migration_id, agent_id, &result);
        result
    }

    /// Execute rollback for a failed migration
    pub fn execute_rollback(&mut self, migration_id: [u8; 16]) -> Result<RollbackInfo, CellError> {
        if let Some(info) = self.rollback_info.get(&migration_id) {
            self.audit_log.log_rollback(
                migration_id,
                info.agent_id,
                true,
                true,
                "Rollback initiated".to_string(),
            );
        }
        let info = self.rollback_info.remove(&migration_id).ok_or_else(|| {
            CellError::InvalidState("No rollback info available for migration".to_string())
        })?;
        if !info.is_valid(self.rollback_max_age_secs) {
            self.audit_log.log_rollback(
                migration_id,
                info.agent_id,
                false,
                false,
                format!("Rollback info expired: {} seconds old", info.age_secs()),
            );
            return Err(CellError::InvalidState(
                "Rollback info has expired".to_string(),
            ));
        }
        self.audit_log.log_rollback(
            migration_id,
            info.agent_id,
            false,
            true,
            format!(
                "Rollback completed: restored {} bytes",
                info.original_state.len()
            ),
        );
        Ok(info)
    }

    /// Complete a migration (remove rollback info)
    pub fn complete_migration(&mut self, migration_id: [u8; 16], success: bool, details: String) {
        if let Some(info) = self.rollback_info.remove(&migration_id) {
            self.audit_log
                .log_completion(migration_id, info.agent_id, success, details);
        }
    }

    /// Get the audit log
    pub fn audit_log(&self) -> &MigrationAuditLog {
        &self.audit_log
    }

    /// Get mutable access to audit log
    pub fn audit_log_mut(&mut self) -> &mut MigrationAuditLog {
        &mut self.audit_log
    }

    /// Cleanup expired rollback info
    pub fn cleanup_expired(&mut self) {
        self.rollback_info
            .retain(|_, info| info.is_valid(self.rollback_max_age_secs));
    }
}