pmat 3.15.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
impl LoadMonitor {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(cpu_threshold: f64, memory_threshold: f64) -> Self {
        Self {
            cpu_threshold,
            memory_threshold,
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Get load factor.
    pub fn get_load_factor(&self) -> f64 {
        // Simplified - would use actual system metrics
        let cpu_usage = self.get_cpu_usage();
        let memory_usage = self.get_memory_usage();

        let cpu_factor = cpu_usage / self.cpu_threshold;
        let memory_factor = memory_usage / self.memory_threshold;

        cpu_factor.max(memory_factor).min(1.0)
    }

    fn get_cpu_usage(&self) -> f64 {
        // Placeholder - would use sysinfo or similar
        0.5
    }

    fn get_memory_usage(&self) -> f64 {
        // Placeholder - would use sysinfo or similar
        0.4
    }
}

impl AdaptiveRateController {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(base_rate: u32, min_rate: u32, max_rate: u32) -> Self {
        let rate_limiter = RateLimiter::new(base_rate * 10, base_rate);

        Self {
            _base_rate: base_rate,
            current_rate: AtomicU32::new(base_rate),
            min_rate,
            max_rate,
            rate_limiter: Arc::new(parking_lot::RwLock::new(rate_limiter)),
            load_monitor: Arc::new(LoadMonitor::new(0.8, 0.9)),
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub async fn adapt_rate(&self) {
        let load_factor = self.load_monitor.get_load_factor();

        // Adjust rate based on load
        let target_rate = if load_factor < 0.5 {
            // Low load - increase rate
            (self.current_rate.load(Ordering::Relaxed) as f64 * 1.1) as u32
        } else if load_factor > 0.8 {
            // High load - decrease rate
            (self.current_rate.load(Ordering::Relaxed) as f64 * 0.9) as u32
        } else {
            // Moderate load - maintain rate
            self.current_rate.load(Ordering::Relaxed)
        };

        let new_rate = target_rate.max(self.min_rate).min(self.max_rate);
        self.current_rate.store(new_rate, Ordering::Relaxed);

        // Update rate limiter
        *self.rate_limiter.write() = RateLimiter::new(new_rate * 10, new_rate);
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Try acquire.
    pub fn try_acquire(&self) -> bool {
        self.rate_limiter.read().try_acquire(1)
    }

    pub async fn acquire(&self) {
        loop {
            {
                let guard = self.rate_limiter.read();
                if guard.try_acquire(1) {
                    return;
                }
            }
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Get current rate.
    pub fn get_current_rate(&self) -> u32 {
        self.current_rate.load(Ordering::Relaxed)
    }
}