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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Agent Composition and Behaviors
//!
//! Provides a composition-based system for extending agent capabilities
//! through reusable behaviors, avoiding deep inheritance hierarchies.

use crate::{Agent, AgentId, AgentState};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use uuid::Uuid;

/// Unique identifier for behaviors
pub type BehaviorId = Uuid;

/// Behavior execution context
#[derive(Debug, Clone)]
pub struct BehaviorContext {
    /// Agent ID this behavior is executing for
    pub agent_id: AgentId,
    /// Agent current state
    pub agent_state: AgentState,
    /// Execution timestamp
    pub timestamp: u64,
    /// Custom context data
    pub data: HashMap<String, String>,
}

impl BehaviorContext {
    /// Create a new behavior context for an agent
    pub fn new(agent: &Agent) -> Self {
        Self {
            agent_id: agent.id(),
            agent_state: agent.state().clone(),
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as u64,
            data: HashMap::new(),
        }
    }

    /// Add context data
    pub fn with_data(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.data.insert(key.into(), value.into());
        self
    }

    /// Get context data
    pub fn get_data(&self, key: &str) -> Option<&String> {
        self.data.get(key)
    }
}

/// Result of behavior execution
#[derive(Debug, Clone)]
pub enum BehaviorResult {
    /// Behavior executed successfully
    Success,
    /// Behavior execution failed
    Failed(String),
    /// Behavior was skipped (e.g., preconditions not met)
    Skipped(String),
}

impl BehaviorResult {
    pub fn is_success(&self) -> bool {
        matches!(self, BehaviorResult::Success)
    }

    pub fn is_failed(&self) -> bool {
        matches!(self, BehaviorResult::Failed(_))
    }
}

/// Behavior trait for composable agent capabilities
pub trait Behavior: Send + Sync {
    /// Get behavior name
    fn name(&self) -> &str;

    /// Get behavior description
    fn description(&self) -> &str {
        ""
    }

    /// Check if behavior can execute in current context
    fn can_execute(&self, _context: &BehaviorContext) -> bool {
        true
    }

    /// Execute the behavior
    fn execute(&self, context: &BehaviorContext) -> BehaviorResult;

    /// Called when behavior is attached to an agent
    fn on_attach(&self, _agent_id: AgentId) -> BehaviorResult {
        BehaviorResult::Success
    }

    /// Called when behavior is detached from an agent
    fn on_detach(&self, _agent_id: AgentId) -> BehaviorResult {
        BehaviorResult::Success
    }
}

/// A simple behavior defined by a closure
pub struct ClosureBehavior {
    name: String,
    description: String,
    executor: Arc<dyn Fn(&BehaviorContext) -> BehaviorResult + Send + Sync>,
}

impl ClosureBehavior {
    /// Create a new closure-based behavior
    pub fn new<F>(name: impl Into<String>, executor: F) -> Self
    where
        F: Fn(&BehaviorContext) -> BehaviorResult + Send + Sync + 'static,
    {
        Self {
            name: name.into(),
            description: String::new(),
            executor: Arc::new(executor),
        }
    }

    /// Set behavior description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }
}

impl Behavior for ClosureBehavior {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn execute(&self, context: &BehaviorContext) -> BehaviorResult {
        (self.executor)(context)
    }
}

/// Logging behavior - logs agent state changes
pub struct LoggingBehavior {
    name: String,
}

impl LoggingBehavior {
    pub fn new() -> Self {
        Self {
            name: "logging".to_string(),
        }
    }
}

impl Default for LoggingBehavior {
    fn default() -> Self {
        Self::new()
    }
}

impl Behavior for LoggingBehavior {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Logs agent state and activity"
    }

    fn execute(&self, context: &BehaviorContext) -> BehaviorResult {
        println!(
            "[Log] Agent {} in state {:?} at {}",
            context.agent_id, context.agent_state, context.timestamp
        );
        BehaviorResult::Success
    }
}

/// Metrics collection behavior
pub struct MetricsBehavior {
    name: String,
    metrics: Arc<RwLock<HashMap<String, u64>>>,
}

impl MetricsBehavior {
    pub fn new() -> Self {
        Self {
            name: "metrics".to_string(),
            metrics: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Get metric value
    pub fn get_metric(&self, key: &str) -> Option<u64> {
        self.metrics
            .read()
            .expect("Lock poisoned: metrics")
            .get(key)
            .copied()
    }

    /// Get all metrics
    pub fn all_metrics(&self) -> HashMap<String, u64> {
        self.metrics.read().expect("Lock poisoned: metrics").clone()
    }
}

impl Default for MetricsBehavior {
    fn default() -> Self {
        Self::new()
    }
}

impl Behavior for MetricsBehavior {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Collects agent execution metrics"
    }

    fn execute(&self, context: &BehaviorContext) -> BehaviorResult {
        let mut metrics = self.metrics.write().expect("Lock poisoned: metrics");
        let key = format!("state_{:?}", context.agent_state);
        *metrics.entry(key).or_insert(0) += 1;
        *metrics.entry("total_executions".to_string()).or_insert(0) += 1;
        BehaviorResult::Success
    }
}

/// Health check behavior
pub struct HealthCheckBehavior {
    name: String,
    healthy_states: Vec<AgentState>,
}

impl HealthCheckBehavior {
    pub fn new() -> Self {
        Self {
            name: "health_check".to_string(),
            healthy_states: vec![AgentState::Running, AgentState::Created],
        }
    }

    pub fn with_healthy_states(mut self, states: Vec<AgentState>) -> Self {
        self.healthy_states = states;
        self
    }
}

impl Default for HealthCheckBehavior {
    fn default() -> Self {
        Self::new()
    }
}

impl Behavior for HealthCheckBehavior {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Checks agent health based on state"
    }

    fn execute(&self, context: &BehaviorContext) -> BehaviorResult {
        if self.healthy_states.contains(&context.agent_state) {
            BehaviorResult::Success
        } else {
            BehaviorResult::Failed(format!("Unhealthy state: {:?}", context.agent_state))
        }
    }
}

/// Behavior composition - manages multiple behaviors for an agent
pub struct BehaviorComposite {
    agent_id: AgentId,
    behaviors: RwLock<HashMap<BehaviorId, Arc<dyn Behavior>>>,
    execution_order: RwLock<Vec<BehaviorId>>,
}

impl BehaviorComposite {
    /// Create a new behavior composite for an agent
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            behaviors: RwLock::new(HashMap::new()),
            execution_order: RwLock::new(Vec::new()),
        }
    }

    /// Attach a behavior
    pub fn attach(&self, behavior: Arc<dyn Behavior>) -> BehaviorId {
        let id = Uuid::new_v4();

        // Call on_attach hook
        behavior.on_attach(self.agent_id);

        // Store behavior
        self.behaviors
            .write()
            .expect("Lock poisoned: behaviors")
            .insert(id, behavior);
        self.execution_order
            .write()
            .expect("Lock poisoned: execution_order")
            .push(id);

        id
    }

    /// Detach a behavior
    pub fn detach(&self, behavior_id: &BehaviorId) -> Option<Arc<dyn Behavior>> {
        let behavior = self
            .behaviors
            .write()
            .expect("Lock poisoned: behaviors")
            .remove(behavior_id)?;

        // Call on_detach hook
        behavior.on_detach(self.agent_id);

        // Remove from execution order
        self.execution_order
            .write()
            .expect("Lock poisoned: execution_order")
            .retain(|id| id != behavior_id);

        Some(behavior)
    }

    /// Get a behavior by ID
    pub fn get(&self, behavior_id: &BehaviorId) -> Option<Arc<dyn Behavior>> {
        self.behaviors
            .read()
            .expect("Lock poisoned: behaviors")
            .get(behavior_id)
            .cloned()
    }

    /// Execute all behaviors in order
    pub fn execute_all(&self, context: &BehaviorContext) -> Vec<(BehaviorId, BehaviorResult)> {
        let order = self
            .execution_order
            .read()
            .expect("Lock poisoned: execution_order");
        let behaviors = self.behaviors.read().expect("Lock poisoned: behaviors");

        order
            .iter()
            .filter_map(|id| {
                let behavior = behaviors.get(id)?;
                if behavior.can_execute(context) {
                    let result = behavior.execute(context);
                    Some((*id, result))
                } else {
                    Some((
                        *id,
                        BehaviorResult::Skipped("Preconditions not met".to_string()),
                    ))
                }
            })
            .collect()
    }

    /// Execute a specific behavior
    pub fn execute(
        &self,
        behavior_id: &BehaviorId,
        context: &BehaviorContext,
    ) -> Option<BehaviorResult> {
        let behaviors = self.behaviors.read().expect("Lock poisoned: behaviors");
        let behavior = behaviors.get(behavior_id)?;

        if behavior.can_execute(context) {
            Some(behavior.execute(context))
        } else {
            Some(BehaviorResult::Skipped("Preconditions not met".to_string()))
        }
    }

    /// Get number of attached behaviors
    pub fn count(&self) -> usize {
        self.behaviors
            .read()
            .expect("Lock poisoned: behaviors")
            .len()
    }

    /// Get all behavior IDs
    pub fn behavior_ids(&self) -> Vec<BehaviorId> {
        self.execution_order
            .read()
            .expect("Lock poisoned: execution_order")
            .clone()
    }

    /// Get behavior names
    pub fn behavior_names(&self) -> Vec<String> {
        let order = self
            .execution_order
            .read()
            .expect("Lock poisoned: execution_order");
        let behaviors = self.behaviors.read().expect("Lock poisoned: behaviors");

        order
            .iter()
            .filter_map(|id| behaviors.get(id).map(|b| b.name().to_string()))
            .collect()
    }
}

/// Registry for managing agent behaviors
pub struct BehaviorRegistry {
    /// Agent ID -> BehaviorComposite
    composites: RwLock<HashMap<AgentId, Arc<BehaviorComposite>>>,
}

impl BehaviorRegistry {
    /// Create a new behavior registry
    pub fn new() -> Self {
        Self {
            composites: RwLock::new(HashMap::new()),
        }
    }

    /// Register an agent (creates behavior composite)
    pub fn register_agent(&self, agent_id: AgentId) -> Arc<BehaviorComposite> {
        let composite = Arc::new(BehaviorComposite::new(agent_id));
        self.composites
            .write()
            .expect("Lock poisoned: composites")
            .insert(agent_id, Arc::clone(&composite));
        composite
    }

    /// Get composite for an agent
    pub fn get_composite(&self, agent_id: &AgentId) -> Option<Arc<BehaviorComposite>> {
        self.composites
            .read()
            .expect("Lock poisoned: composites")
            .get(agent_id)
            .cloned()
    }

    /// Unregister an agent
    pub fn unregister_agent(&self, agent_id: &AgentId) -> Option<Arc<BehaviorComposite>> {
        self.composites
            .write()
            .expect("Lock poisoned: composites")
            .remove(agent_id)
    }

    /// Execute all behaviors for an agent
    pub fn execute_for_agent(
        &self,
        agent_id: &AgentId,
        context: &BehaviorContext,
    ) -> Option<Vec<(BehaviorId, BehaviorResult)>> {
        let composite = self.get_composite(agent_id)?;
        Some(composite.execute_all(context))
    }

    /// Get number of registered agents
    pub fn agent_count(&self) -> usize {
        self.composites
            .read()
            .expect("Lock poisoned: composites")
            .len()
    }
}

impl Default for BehaviorRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_behavior_context() {
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let context = BehaviorContext::new(&agent);

        assert_eq!(context.agent_id, agent.id());
        assert_eq!(context.agent_state, AgentState::Created);
    }

    #[test]
    fn test_closure_behavior() {
        let behavior = ClosureBehavior::new("test", |_| BehaviorResult::Success);

        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let context = BehaviorContext::new(&agent);

        let result = behavior.execute(&context);
        assert!(result.is_success());
    }

    #[test]
    fn test_logging_behavior() {
        let behavior = LoggingBehavior::new();
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let context = BehaviorContext::new(&agent);

        let result = behavior.execute(&context);
        assert!(result.is_success());
    }

    #[test]
    fn test_metrics_behavior() {
        let behavior = MetricsBehavior::new();
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let context = BehaviorContext::new(&agent);

        behavior.execute(&context);
        behavior.execute(&context);

        assert_eq!(behavior.get_metric("total_executions"), Some(2));
    }

    #[test]
    fn test_health_check_behavior() {
        let behavior = HealthCheckBehavior::new();
        let mut agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);

        // Created is healthy
        let context = BehaviorContext::new(&agent);
        let result = behavior.execute(&context);
        assert!(result.is_success());

        // Error is unhealthy
        agent.set_state(AgentState::Error);
        let context = BehaviorContext::new(&agent);
        let result = behavior.execute(&context);
        assert!(result.is_failed());
    }

    #[test]
    fn test_behavior_composite() {
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let composite = BehaviorComposite::new(agent.id());

        // Attach behaviors
        let behavior1 = Arc::new(LoggingBehavior::new());
        let behavior2 = Arc::new(MetricsBehavior::new());

        let id1 = composite.attach(behavior1);
        let _id2 = composite.attach(behavior2);

        assert_eq!(composite.count(), 2);

        // Execute all
        let context = BehaviorContext::new(&agent);
        let results = composite.execute_all(&context);
        assert_eq!(results.len(), 2);

        // Detach
        composite.detach(&id1);
        assert_eq!(composite.count(), 1);
    }

    #[test]
    fn test_behavior_registry() {
        let registry = BehaviorRegistry::new();
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);

        let composite = registry.register_agent(agent.id());
        assert_eq!(registry.agent_count(), 1);

        // Attach behavior
        let behavior = Arc::new(LoggingBehavior::new());
        composite.attach(behavior);

        // Execute
        let context = BehaviorContext::new(&agent);
        let results = registry.execute_for_agent(&agent.id(), &context);
        assert!(results.is_some());
        assert_eq!(results.unwrap().len(), 1);

        // Unregister
        registry.unregister_agent(&agent.id());
        assert_eq!(registry.agent_count(), 0);
    }

    #[test]
    fn test_behavior_names() {
        let agent = Agent::new(vec![0x00, 0x61, 0x73, 0x6d]);
        let composite = BehaviorComposite::new(agent.id());

        composite.attach(Arc::new(LoggingBehavior::new()));
        composite.attach(Arc::new(MetricsBehavior::new()));
        composite.attach(Arc::new(HealthCheckBehavior::new()));

        let names = composite.behavior_names();
        assert_eq!(names.len(), 3);
        assert!(names.contains(&"logging".to_string()));
        assert!(names.contains(&"metrics".to_string()));
        assert!(names.contains(&"health_check".to_string()));
    }
}