matrixcode-core 0.4.43

MatrixCode Agent Core - Pure logic, no UI
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
//! Lifecycle Manager
//!
//! Manages the lifecycle of extension services including connection,
//! reconnection, heartbeat monitoring, and graceful shutdown.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{broadcast, mpsc, RwLock};
use tokio::time::{interval, sleep};

use crate::matrixrpc::registry::RegistryService;
use crate::matrixrpc::service::{ExtensionService, ServiceId, ServiceStatus};

/// Lifecycle event
#[derive(Debug, Clone)]
pub enum LifecycleEvent {
    /// Service started
    Started(ServiceId),

    /// Service stopped
    Stopped(ServiceId),

    /// Service status changed
    StatusChanged {
        id: ServiceId,
        old_status: ServiceStatus,
        new_status: ServiceStatus,
    },

    /// Heartbeat received
    Heartbeat(ServiceId),

    /// Heartbeat timeout
    HeartbeatTimeout(ServiceId),

    /// Reconnection attempt
    Reconnecting {
        id: ServiceId,
        attempt: u32,
        max_attempts: u32,
    },

    /// Reconnection succeeded
    Reconnected(ServiceId),

    /// Reconnection failed
    ReconnectFailed(ServiceId),

    /// Service error
    Error {
        id: ServiceId,
        error: String,
    },
}

/// Lifecycle configuration
#[derive(Debug, Clone)]
pub struct LifecycleConfig {
    /// Heartbeat interval in seconds
    pub heartbeat_interval_secs: u64,

    /// Heartbeat timeout in seconds
    pub heartbeat_timeout_secs: u64,

    /// Maximum reconnection attempts
    pub max_reconnect_attempts: u32,

    /// Initial reconnection delay in milliseconds
    pub reconnect_delay_ms: u64,

    /// Maximum reconnection delay in milliseconds
    pub max_reconnect_delay_ms: u64,

    /// Backoff multiplier for reconnection delays
    pub reconnect_backoff_multiplier: f64,

    /// Enable auto-reconnection
    pub auto_reconnect: bool,
}

impl Default for LifecycleConfig {
    fn default() -> Self {
        Self {
            heartbeat_interval_secs: 30,
            heartbeat_timeout_secs: 90,
            max_reconnect_attempts: 5,
            reconnect_delay_ms: 1000,
            max_reconnect_delay_ms: 30000,
            reconnect_backoff_multiplier: 2.0,
            auto_reconnect: true,
        }
    }
}

/// Lifecycle manager error
#[derive(Debug, thiserror::Error)]
pub enum LifecycleError {
    /// Service not found
    #[error("Service '{0}' not found")]
    NotFound(String),

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

    /// Reconnection failed
    #[error("Reconnection failed after {0} attempts")]
    ReconnectFailed(u32),

    /// Invalid state
    #[error("Invalid state: {0}")]
    InvalidState(String),

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

/// Service lifecycle state
#[derive(Debug, Clone)]
struct ServiceLifecycle {
#[allow(dead_code)]
    /// Service ID
    id: ServiceId,
#[allow(dead_code)]

    /// Current status
    status: ServiceStatus,

    /// Reconnection attempts
    reconnect_attempts: u32,

    /// Is auto-reconnect enabled for this service
    auto_reconnect: bool,

    /// Stop signal sender
    stop_tx: Option<mpsc::Sender<()>>,
}

/// Lifecycle Manager
///
/// Manages service lifecycle events including:
/// - Connection initialization
/// - Heartbeat monitoring
/// - Auto-reconnection with backoff
/// - Graceful shutdown
pub struct LifecycleManager {
    /// Registry service reference
    registry: Arc<RegistryService>,

    /// Lifecycle configuration
    config: LifecycleConfig,

    /// Service lifecycle states
    lifecycles: Arc<RwLock<HashMap<ServiceId, ServiceLifecycle>>>,

    /// Event broadcaster
    event_tx: broadcast::Sender<LifecycleEvent>,
}

impl LifecycleManager {
    /// Create a new lifecycle manager
    pub fn new(registry: Arc<RegistryService>) -> Self {
        Self::with_config(registry, LifecycleConfig::default())
    }

    /// Create a new lifecycle manager with configuration
    pub fn with_config(registry: Arc<RegistryService>, config: LifecycleConfig) -> Self {
        let (event_tx, _) = broadcast::channel(256);

        Self {
            registry,
            config,
            lifecycles: Arc::new(RwLock::new(HashMap::new())),
            event_tx,
        }
    }

    /// Subscribe to lifecycle events
    pub fn subscribe(&self) -> broadcast::Receiver<LifecycleEvent> {
        self.event_tx.subscribe()
    }

    /// Start managing a service
    pub async fn start_service(&self, service: ExtensionService) -> Result<ServiceId, LifecycleError> {
        let auto_reconnect = service.transport.auto_reconnect;

        // Register the service
        let id = self
            .registry
            .register(service)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Set initial status
        self.registry
            .update_status(&id, ServiceStatus::Starting)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Create lifecycle state
        let (stop_tx, stop_rx) = mpsc::channel(1);
        let lifecycle = ServiceLifecycle {
            id: id.clone(),
            status: ServiceStatus::Starting,
            reconnect_attempts: 0,
            auto_reconnect,
            stop_tx: Some(stop_tx),
        };

        {
            let mut lifecycles = self.lifecycles.write().await;
            lifecycles.insert(id.clone(), lifecycle);
        }

        // Start heartbeat monitor task
        self.spawn_heartbeat_monitor(id.clone(), stop_rx);

        // Emit started event
        let _ = self.event_tx.send(LifecycleEvent::Started(id.clone()));

        // Transition to running
        self.transition_status(&id, ServiceStatus::Running).await?;

        Ok(id)
    }

    /// Stop managing a service
    pub async fn stop_service(&self, id: &ServiceId) -> Result<(), LifecycleError> {
        // Update status first before removing from lifecycles
        self.transition_status(id, ServiceStatus::Stopping).await?;

        // Get lifecycle and remove from tracking
        let lifecycle = {
            let mut lifecycles = self.lifecycles.write().await;
            lifecycles.remove(id).ok_or_else(|| LifecycleError::NotFound(id.to_string()))?
        };

        // Send stop signal
        if let Some(stop_tx) = lifecycle.stop_tx {
            let _ = stop_tx.send(()).await;
        }

        // Unregister from registry
        self.registry
            .unregister(id)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Emit stopped event
        let _ = self.event_tx.send(LifecycleEvent::Stopped(id.clone()));

        Ok(())
    }

    /// Handle heartbeat from a service
    pub async fn handle_heartbeat(&self, id: &ServiceId) -> Result<(), LifecycleError> {
        self.registry
            .heartbeat(id)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Reset reconnection attempts
        {
            let mut lifecycles = self.lifecycles.write().await;
            if let Some(lifecycle) = lifecycles.get_mut(id) {
                lifecycle.reconnect_attempts = 0;
            }
        }

        // Emit heartbeat event
        let _ = self.event_tx.send(LifecycleEvent::Heartbeat(id.clone()));

        Ok(())
    }

    /// Handle service error
    pub async fn handle_error(&self, id: &ServiceId, error: String) -> Result<(), LifecycleError> {
        // Emit error event
        let _ = self.event_tx.send(LifecycleEvent::Error {
            id: id.clone(),
            error,
        });

        // Transition to error status
        self.transition_status(id, ServiceStatus::Error).await?;

        // Attempt reconnection if enabled
        let should_reconnect = {
            let lifecycles = self.lifecycles.read().await;
            lifecycles
                .get(id)
                .map(|l| l.auto_reconnect)
                .unwrap_or(false)
        };

        if should_reconnect {
            self.attempt_reconnect(id).await?;
        }

        Ok(())
    }

    /// Attempt to reconnect a service
    async fn attempt_reconnect(&self, id: &ServiceId) -> Result<(), LifecycleError> {
        let (max_attempts, _delay_ms, backoff) = {
            let lifecycles = self.lifecycles.read().await;
            let lifecycle = lifecycles.get(id).ok_or_else(|| LifecycleError::NotFound(id.to_string()))?;
            (self.config.max_reconnect_attempts, self.config.reconnect_delay_ms, lifecycle.reconnect_attempts)
        };

        // Check max attempts
        if backoff >= max_attempts {
            let _ = self.event_tx.send(LifecycleEvent::ReconnectFailed(id.clone()));
            return Err(LifecycleError::ReconnectFailed(max_attempts));
        }

        // Update status and increment attempts
        {
            let mut lifecycles = self.lifecycles.write().await;
            if let Some(lifecycle) = lifecycles.get_mut(id) {
                lifecycle.reconnect_attempts += 1;
                lifecycle.status = ServiceStatus::Reconnecting;
            }
        }

        // Emit reconnecting event
        let _ = self.event_tx.send(LifecycleEvent::Reconnecting {
            id: id.clone(),
            attempt: backoff + 1,
            max_attempts: max_attempts,
        });

        self.registry
            .update_status(id, ServiceStatus::Reconnecting)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Calculate backoff delay
        let delay = self.calculate_reconnect_delay(backoff);

        // Wait for backoff
        sleep(Duration::from_millis(delay)).await;

        // Here you would attempt actual reconnection
        // This is a placeholder - actual implementation depends on transport

        Ok(())
    }

    /// Calculate reconnection delay with exponential backoff
    fn calculate_reconnect_delay(&self, attempt: u32) -> u64 {
        let base = self.config.reconnect_delay_ms as f64;
        let multiplier = self.config.reconnect_backoff_multiplier.powi(attempt as i32);
        let delay = base * multiplier;

        // Add jitter (10%)
        let jitter = delay * 0.1 * (rand_jitter() - 0.5) * 2.0;

        let final_delay = (delay + jitter) as u64;
        final_delay.min(self.config.max_reconnect_delay_ms)
    }

    /// Transition service status
    async fn transition_status(
        &self,
        id: &ServiceId,
        new_status: ServiceStatus,
    ) -> Result<(), LifecycleError> {
        let old_status = {
            let lifecycles = self.lifecycles.read().await;
            lifecycles
                .get(id)
                .map(|l| l.status)
                .ok_or_else(|| LifecycleError::NotFound(id.to_string()))?
        };

        if old_status == new_status {
            return Ok(());
        }

        // Update lifecycle state
        {
            let mut lifecycles = self.lifecycles.write().await;
            if let Some(lifecycle) = lifecycles.get_mut(id) {
                lifecycle.status = new_status;
            }
        }

        // Update registry
        self.registry
            .update_status(id, new_status)
            .await
            .map_err(|e| LifecycleError::Internal(e.to_string()))?;

        // Emit status changed event
        let _ = self.event_tx.send(LifecycleEvent::StatusChanged {
            id: id.clone(),
            old_status,
            new_status,
        });

        Ok(())
    }

    /// Spawn heartbeat monitor task
    fn spawn_heartbeat_monitor(&self, id: ServiceId, mut stop_rx: mpsc::Receiver<()>) {
        let registry = self.registry.clone();
        let event_tx = self.event_tx.clone();
        let timeout_secs = self.config.heartbeat_timeout_secs;

        tokio::spawn(async move {
            let mut check_interval = interval(Duration::from_secs(timeout_secs / 3));

            loop {
                tokio::select! {
                    _ = stop_rx.recv() => {
                        // Stop signal received
                        break;
                    }
                    _ = check_interval.tick() => {
                        // Check if service is healthy
                        if let Some(service) = registry.get(&id).await {
                            if !service.is_healthy(timeout_secs) {
                                if service.status == ServiceStatus::Running {
                                    // Emit heartbeat timeout event
                                    let _ = event_tx.send(LifecycleEvent::HeartbeatTimeout(id.clone()));

                                    // Update status to reconnecting
                                    let _ = registry.update_status(&id, ServiceStatus::Reconnecting).await;
                                }
                            }
                        } else {
                            // Service no longer exists
                            break;
                        }
                    }
                }
            }
        });
    }

    /// Stop all services
    pub async fn stop_all(&self) {
        let ids: Vec<ServiceId> = {
            let lifecycles = self.lifecycles.read().await;
            lifecycles.keys().cloned().collect()
        };

        for id in ids {
            let _ = self.stop_service(&id).await;
        }
    }

    /// Get service status
    pub async fn get_status(&self, id: &ServiceId) -> Option<ServiceStatus> {
        let lifecycles = self.lifecycles.read().await;
        lifecycles.get(id).map(|l| l.status)
    }

    /// Check if service is healthy
    pub async fn is_healthy(&self, id: &ServiceId) -> bool {
        let lifecycles = self.lifecycles.read().await;
        lifecycles
            .get(id)
            .map(|l| l.status == ServiceStatus::Running)
            .unwrap_or(false)
    }

    /// Get managed service count
    pub async fn count(&self) -> usize {
        self.lifecycles.read().await.len()
    }

    /// Trigger health check for all services
    pub async fn health_check(&self) -> Vec<ServiceId> {
        self.registry.health_check().await
    }
}

/// Simple random jitter generator
fn rand_jitter() -> f64 {
    // Use a simple deterministic jitter based on time
    // In production, use proper random number generator
    use std::time::{SystemTime, UNIX_EPOCH};
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .subsec_nanos();
    nanos as f64 / u32::MAX as f64
}

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

    #[tokio::test]
    async fn test_lifecycle_manager_creation() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry);
        assert_eq!(manager.count().await, 0);
    }

    #[tokio::test]
    async fn test_start_stop_service() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry);

        let service = ExtensionService::new("test", "1.0.0");
        let id = manager.start_service(service).await.unwrap();

        assert_eq!(manager.count().await, 1);
        assert_eq!(manager.get_status(&id).await, Some(ServiceStatus::Running));

        manager.stop_service(&id).await.unwrap();
        assert_eq!(manager.count().await, 0);
    }

    #[tokio::test]
    async fn test_handle_heartbeat() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry);

        let service = ExtensionService::new("test", "1.0.0");
        let id = manager.start_service(service).await.unwrap();

        let result = manager.handle_heartbeat(&id).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_error() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry.clone());

        let service = ExtensionService::new("test", "1.0.0");
        let id = manager.start_service(service).await.unwrap();

        // Disable auto-reconnect for this test
        {
            let mut lifecycles = manager.lifecycles.write().await;
            if let Some(l) = lifecycles.get_mut(&id) {
                l.auto_reconnect = false;
            }
        }

        manager
            .handle_error(&id, "Test error".to_string())
            .await
            .unwrap();

        assert_eq!(manager.get_status(&id).await, Some(ServiceStatus::Error));
    }

    #[tokio::test]
    async fn test_lifecycle_events() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry);

        let mut event_rx = manager.subscribe();

        let service = ExtensionService::new("test", "1.0.0");
        let id = manager.start_service(service).await.unwrap();

        // Should receive Started and StatusChanged events
        let event1 = event_rx.try_recv();
        let event2 = event_rx.try_recv();

        assert!(event1.is_ok() || event2.is_ok());
    }

    #[tokio::test]
    async fn test_lifecycle_config() {
        let registry = Arc::new(RegistryService::new());
        let config = LifecycleConfig {
            heartbeat_interval_secs: 10,
            heartbeat_timeout_secs: 30,
            max_reconnect_attempts: 3,
            ..Default::default()
        };
        let manager = LifecycleManager::with_config(registry, config);

        assert_eq!(manager.config.heartbeat_interval_secs, 10);
        assert_eq!(manager.config.heartbeat_timeout_secs, 30);
        assert_eq!(manager.config.max_reconnect_attempts, 3);
    }

    #[test]
    fn test_calculate_reconnect_delay() {
        let registry = Arc::new(RegistryService::new());
        let manager = LifecycleManager::new(registry);

        let delay0 = manager.calculate_reconnect_delay(0);
        let delay1 = manager.calculate_reconnect_delay(1);
        let delay2 = manager.calculate_reconnect_delay(2);

        // Delay should increase with backoff
        assert!(delay1 > delay0);
        assert!(delay2 > delay1);
    }
}