pmat 2.93.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! Service lifecycle management per SPECIFICATION.md Section 2.2
//!
//! This module provides lifecycle management for services including
//! initialization, health checks, graceful shutdown, and restart capabilities.

use super::service_base::{Service, ServiceMetrics};
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::{interval, Duration};
use tracing::{error, info, warn};

/// Service lifecycle states
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ServiceState {
    /// Service is not yet initialized
    Uninitialized,
    /// Service is starting up
    Starting,
    /// Service is running and healthy
    Running,
    /// Service is degraded but operational
    Degraded,
    /// Service is stopping
    Stopping,
    /// Service has stopped
    Stopped,
    /// Service has failed
    Failed,
}

/// Health status for a service
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthStatus {
    pub state: ServiceState,
    pub message: String,
    pub last_check: std::time::SystemTime,
    pub uptime_seconds: u64,
    pub metrics: ServiceMetrics,
}

/// Lifecycle events that can occur
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LifecycleEvent {
    Started,
    Stopped,
    HealthCheckPassed,
    HealthCheckFailed(String),
    StateChanged(ServiceState),
    Error(String),
}

/// Trait for services with lifecycle management
#[async_trait]
pub trait ManagedService: Service {
    /// Initialize the service
    async fn initialize(&self) -> Result<()> {
        Ok(())
    }

    /// Perform health check
    async fn health_check(&self) -> Result<HealthStatus>;

    /// Gracefully shutdown the service
    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    /// Handle lifecycle event
    fn handle_event(&mut self, event: LifecycleEvent) {
        if let LifecycleEvent::Error(msg) = event {
            error!("Service error: {}", msg);
        }
    }
}

/// Wrapper that adds lifecycle management to any service
pub struct LifecycleWrapper<S: Service> {
    service: Arc<RwLock<S>>,
    state: Arc<RwLock<ServiceState>>,
    running: Arc<AtomicBool>,
    start_time: std::time::SystemTime,
    health_check_interval: Duration,
    metrics: Arc<RwLock<ServiceMetrics>>,
}

impl<S: Service> LifecycleWrapper<S> {
    /// Create a new lifecycle wrapper for a service
    pub fn new(service: S, health_check_interval: Duration) -> Self {
        Self {
            service: Arc::new(RwLock::new(service)),
            state: Arc::new(RwLock::new(ServiceState::Uninitialized)),
            running: Arc::new(AtomicBool::new(false)),
            start_time: std::time::SystemTime::now(),
            health_check_interval,
            metrics: Arc::new(RwLock::new(ServiceMetrics::default())),
        }
    }

    /// Start the service with lifecycle management
    pub async fn start(&self) -> Result<()> {
        let mut state = self.state.write().await;
        *state = ServiceState::Starting;
        drop(state);

        info!("Starting service lifecycle");

        // Mark as running
        self.running.store(true, Ordering::Relaxed);

        // Update state to running
        let mut state = self.state.write().await;
        *state = ServiceState::Running;
        drop(state);

        // Start health check loop
        self.start_health_check_loop().await;

        info!("Service lifecycle started");
        Ok(())
    }

    /// Stop the service gracefully
    pub async fn stop(&self) -> Result<()> {
        let mut state = self.state.write().await;
        *state = ServiceState::Stopping;
        drop(state);

        info!("Stopping service lifecycle");

        // Signal to stop
        self.running.store(false, Ordering::Relaxed);

        // Wait a bit for graceful shutdown
        tokio::time::sleep(Duration::from_secs(1)).await;

        // Update state
        let mut state = self.state.write().await;
        *state = ServiceState::Stopped;
        drop(state);

        info!("Service lifecycle stopped");
        Ok(())
    }

    /// Get current service state
    pub async fn get_state(&self) -> ServiceState {
        *self.state.read().await
    }

    /// Get health status
    pub async fn get_health(&self) -> HealthStatus {
        let state = *self.state.read().await;
        let metrics = self.metrics.read().await.clone();
        let uptime = self.start_time.elapsed().unwrap_or_default().as_secs();

        HealthStatus {
            state,
            message: format!("Service in {state:?} state"),
            last_check: std::time::SystemTime::now(),
            uptime_seconds: uptime,
            metrics,
        }
    }

    /// Start the health check loop
    async fn start_health_check_loop(&self) {
        let running = self.running.clone();
        let state = self.state.clone();
        let metrics = self.metrics.clone();
        let interval_duration = self.health_check_interval;

        tokio::spawn(async move {
            let mut ticker = interval(interval_duration);

            while running.load(Ordering::Relaxed) {
                ticker.tick().await;

                // Perform health check
                let current_state = *state.read().await;

                // Simple health check based on metrics
                let current_metrics = metrics.read().await.clone();
                let success_rate = current_metrics.success_rate();

                let new_state = if success_rate < 0.5 && current_metrics.request_count > 10 {
                    ServiceState::Failed
                } else if success_rate < 0.8 && current_metrics.request_count > 10 {
                    ServiceState::Degraded
                } else if current_state == ServiceState::Running
                    || current_state == ServiceState::Degraded
                {
                    ServiceState::Running
                } else {
                    current_state
                };

                if new_state != current_state {
                    let mut state_guard = state.write().await;
                    *state_guard = new_state;
                    info!(
                        "Service state changed: {:?} -> {:?}",
                        current_state, new_state
                    );
                }
            }
        });
    }
}

#[async_trait]
impl<S> Service for LifecycleWrapper<S>
where
    S: Service + Send + Sync,
    S::Input: Send + Sync + 'static,
    S::Output: Send + Sync + 'static,
    S::Error: Send + Sync + 'static,
{
    type Input = S::Input;
    type Output = S::Output;
    type Error = S::Error;

    async fn process(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
        let start = std::time::Instant::now();

        // Check if service is in a runnable state
        let current_state = *self.state.read().await;
        if current_state != ServiceState::Running && current_state != ServiceState::Degraded {
            warn!("Service called while in {:?} state", current_state);
        }

        // Process through wrapped service
        let service = self.service.read().await;
        let result = service.process(input).await;

        // Update metrics
        let mut metrics = self.metrics.write().await;
        metrics.record_request(start.elapsed(), result.is_ok());

        result
    }
}

/// Type alias for managed service trait objects
type ManagedServiceObject = dyn ManagedService<Input = serde_json::Value, Output = serde_json::Value, Error = anyhow::Error>
    + Send
    + Sync;

/// Service supervisor that manages multiple services
pub struct ServiceSupervisor {
    services: Arc<RwLock<Vec<Arc<ManagedServiceObject>>>>,
    running: Arc<AtomicBool>,
}

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

impl ServiceSupervisor {
    /// Create a new service supervisor
    #[must_use] 
    pub fn new() -> Self {
        Self {
            services: Arc::new(RwLock::new(Vec::new())),
            running: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Register a managed service
    pub async fn register<S>(&self, service: S)
    where
        S: ManagedService<
                Input = serde_json::Value,
                Output = serde_json::Value,
                Error = anyhow::Error,
            > + Send
            + Sync
            + 'static,
    {
        let mut services = self.services.write().await;
        services.push(Arc::new(service) as Arc<ManagedServiceObject>);
    }

    /// Start all services
    pub async fn start_all(&self) -> Result<()> {
        info!("Starting service supervisor");
        self.running.store(true, Ordering::Relaxed);

        let services = self.services.read().await;
        for service in services.iter() {
            if let Err(e) = service.initialize().await {
                error!("Failed to initialize service: {}", e);
            }
        }

        // Start monitoring loop
        self.start_monitoring().await;

        Ok(())
    }

    /// Stop all services
    pub async fn stop_all(&self) -> Result<()> {
        info!("Stopping service supervisor");
        self.running.store(false, Ordering::Relaxed);

        let services = self.services.read().await;
        for service in services.iter() {
            if let Err(e) = service.shutdown().await {
                error!("Failed to shutdown service: {}", e);
            }
        }

        Ok(())
    }

    /// Start monitoring loop for all services
    async fn start_monitoring(&self) {
        let services = self.services.clone();
        let running = self.running.clone();

        tokio::spawn(async move {
            let mut ticker = interval(Duration::from_secs(30));

            while running.load(Ordering::Relaxed) {
                ticker.tick().await;

                let services = services.read().await;
                for service in services.iter() {
                    match service.health_check().await {
                        Ok(status) => {
                            if status.state == ServiceState::Failed {
                                warn!("Service health check failed: {}", status.message);
                            }
                        }
                        Err(e) => {
                            error!("Health check error: {}", e);
                        }
                    }
                }
            }
        });
    }

    /// Get health status of all services
    pub async fn get_all_health(&self) -> Vec<HealthStatus> {
        let services = self.services.read().await;
        let mut statuses = Vec::new();

        for service in services.iter() {
            if let Ok(status) = service.health_check().await {
                statuses.push(status);
            }
        }

        statuses
    }
}

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

    // Mock service for testing
    struct TestService {
        fail_count: std::sync::atomic::AtomicU32,
    }

    #[async_trait]
    impl Service for TestService {
        type Input = String;
        type Output = String;
        type Error = Error;

        async fn process(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
            let count = self.fail_count.fetch_add(1, Ordering::Relaxed);
            if count < 2 {
                Err(anyhow::anyhow!("Simulated failure"))
            } else {
                Ok(format!("Processed: {}", input))
            }
        }
    }

    #[tokio::test]
    async fn test_lifecycle_wrapper() {
        let service = TestService {
            fail_count: std::sync::atomic::AtomicU32::new(0),
        };

        let wrapper = LifecycleWrapper::new(service, Duration::from_millis(100));

        // Start the service
        wrapper.start().await.unwrap();

        // Check initial state
        assert_eq!(wrapper.get_state().await, ServiceState::Running);

        // Process some requests (first two will fail)
        let _ = wrapper.process("test1".to_string()).await;
        let _ = wrapper.process("test2".to_string()).await;
        let result = wrapper.process("test3".to_string()).await.unwrap();

        assert_eq!(result, "Processed: test3");

        // Stop the service
        wrapper.stop().await.unwrap();
        assert_eq!(wrapper.get_state().await, ServiceState::Stopped);
    }

    #[tokio::test]
    async fn test_health_status() {
        let service = TestService {
            fail_count: std::sync::atomic::AtomicU32::new(0),
        };

        let wrapper = LifecycleWrapper::new(service, Duration::from_millis(100));
        wrapper.start().await.unwrap();

        let health = wrapper.get_health().await;
        assert_eq!(health.state, ServiceState::Running);
        assert!(health.uptime_seconds < 3600); // Should be less than an hour for test

        wrapper.stop().await.unwrap();
    }
}

#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}