bevy_debugger_mcp 0.1.8

AI-assisted debugging for Bevy games through Claude Code using Model Context Protocol
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
/// Performance Budget Processor for Debug Command Integration
/// 
/// This processor integrates the performance budget monitoring system with the debug command
/// infrastructure, providing MCP-accessible commands for budget configuration and monitoring.

use crate::brp_messages::{DebugCommand, DebugResponse};
use crate::brp_client::BrpClient;
use crate::debug_command_processor::DebugCommandProcessor;
use crate::performance_budget::{
    PerformanceBudgetMonitor, BudgetConfig, PerformanceMetrics, Platform,
    BudgetViolation, ComplianceReport, BudgetRecommendation
};
use crate::error::{Error, Result};
use async_trait::async_trait;
use chrono::Utc;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tokio::time::interval;
use tracing::{debug, info, warn};

/// Performance budget processor for debug commands
pub struct PerformanceBudgetProcessor {
    /// Budget monitor instance
    monitor: Arc<PerformanceBudgetMonitor>,
    
    /// BRP client for Bevy interaction
    brp_client: Arc<RwLock<BrpClient>>,
    
    /// Background monitoring task handle
    monitoring_handle: Arc<RwLock<Option<tokio::task::JoinHandle<()>>>>,
    
    /// Monitoring state
    monitoring_state: Arc<RwLock<MonitoringState>>,
    
    /// Configuration persistence path
    config_path: Option<String>,
}

/// Monitoring state tracking
#[derive(Debug, Default)]
struct MonitoringState {
    /// Whether continuous monitoring is active
    continuous_monitoring: bool,
    
    /// Last check timestamp
    last_check: Option<Instant>,
    
    /// Recent violations count
    recent_violations: usize,
    
    /// Consecutive violation count
    consecutive_violations: usize,
    
    /// Last platform check
    last_platform_check: Option<Instant>,
}

impl PerformanceBudgetProcessor {
    /// Create a new performance budget processor
    pub fn new(brp_client: Arc<RwLock<BrpClient>>) -> Self {
        let config = BudgetConfig::default();
        let monitor = Arc::new(PerformanceBudgetMonitor::new(config));
        
        Self {
            monitor,
            brp_client,
            monitoring_handle: Arc::new(RwLock::new(None)),
            monitoring_state: Arc::new(RwLock::new(MonitoringState::default())),
            config_path: Some("config/performance_budgets.toml".to_string()),
        }
    }
    
    /// Start continuous budget monitoring
    pub async fn start_continuous_monitoring(&self) -> Result<()> {
        let mut handle_guard = self.monitoring_handle.write().await;
        
        if handle_guard.is_some() {
            return Ok(()); // Already monitoring
        }
        
        // Start the monitor
        self.monitor.start_monitoring().await?;
        
        let monitor = Arc::clone(&self.monitor);
        let brp_client = Arc::clone(&self.brp_client);
        let monitoring_state = Arc::clone(&self.monitoring_state);
        
        let handle = tokio::spawn(async move {
            let mut check_interval = interval(Duration::from_millis(100)); // Check every 100ms
            let mut platform_check_interval = interval(Duration::from_secs(60)); // Platform check every minute
            
            loop {
                tokio::select! {
                    _ = check_interval.tick() => {
                        // Perform budget checks
                        if let Ok(metrics) = Self::collect_metrics(&brp_client).await {
                            if let Ok(violations) = monitor.check_violations(metrics).await {
                                Self::handle_violations(&violations, &monitoring_state).await;
                            }
                        }
                    }
                    _ = platform_check_interval.tick() => {
                        // Update platform detection
                        let platform = monitor.update_platform().await;
                        debug!("Platform updated: {:?}", platform);
                        
                        let mut state = monitoring_state.write().await;
                        state.last_platform_check = Some(Instant::now());
                    }
                }
            }
        });
        
        *handle_guard = Some(handle);
        
        // Update state
        let mut state = self.monitoring_state.write().await;
        state.continuous_monitoring = true;
        
        info!("Continuous performance budget monitoring started");
        Ok(())
    }
    
    /// Stop continuous monitoring
    pub async fn stop_continuous_monitoring(&self) -> Result<()> {
        let mut handle_guard = self.monitoring_handle.write().await;
        
        if let Some(handle) = handle_guard.take() {
            handle.abort();
        }
        
        // Stop the monitor
        self.monitor.stop_monitoring().await?;
        
        // Update state
        let mut state = self.monitoring_state.write().await;
        state.continuous_monitoring = false;
        
        info!("Continuous performance budget monitoring stopped");
        Ok(())
    }
    
    /// Collect current performance metrics
    async fn collect_metrics(brp_client: &Arc<RwLock<BrpClient>>) -> Result<PerformanceMetrics> {
        // In a real implementation, this would query actual metrics from Bevy
        // For now, we'll simulate metrics collection
        
        // This would normally query BRP for actual metrics
        // Example queries would include:
        // - Frame time from diagnostics
        // - Memory usage from system info
        // - Entity count from world stats
        // - System execution times from profiler
        
        // Simulated metrics for now
        Ok(PerformanceMetrics {
            frame_time_ms: 16.0 + (rand::random::<f32>() * 5.0),
            memory_mb: 450.0 + (rand::random::<f32>() * 100.0),
            system_times: HashMap::new(),
            cpu_percent: 60.0 + (rand::random::<f32>() * 30.0),
            gpu_time_ms: 14.0 + (rand::random::<f32>() * 6.0),
            entity_count: 8000 + (rand::random::<f32>() * 4000.0) as usize,
            draw_calls: 800 + (rand::random::<f32>() * 400.0) as usize,
            network_bandwidth_kbps: 500.0 + (rand::random::<f32>() * 500.0),
            timestamp: Utc::now(),
        })
    }
    
    /// Handle detected violations
    async fn handle_violations(
        violations: &[BudgetViolation],
        monitoring_state: &Arc<RwLock<MonitoringState>>,
    ) {
        if violations.is_empty() {
            // Reset consecutive violations if no violations
            let mut state = monitoring_state.write().await;
            state.consecutive_violations = 0;
            return;
        }
        
        let mut state = monitoring_state.write().await;
        state.recent_violations += violations.len();
        state.consecutive_violations += 1;
        state.last_check = Some(Instant::now());
        
        // Log violations based on severity
        for violation in violations {
            match violation.severity {
                crate::performance_budget::ViolationSeverity::Critical => {
                    warn!("CRITICAL budget violation: {:?} - {:.1}% over budget", 
                        violation.metric, violation.violation_percent);
                }
                crate::performance_budget::ViolationSeverity::Major => {
                    warn!("Major budget violation: {:?} - {:.1}% over budget", 
                        violation.metric, violation.violation_percent);
                }
                _ => {
                    debug!("Budget violation: {:?} - {:.1}% over budget", 
                        violation.metric, violation.violation_percent);
                }
            }
        }
    }
    
    /// Load configuration from file
    async fn load_config(&self) -> Result<BudgetConfig> {
        if let Some(ref path) = self.config_path {
            // In a real implementation, this would load from TOML file
            // For now, return default config
            Ok(BudgetConfig::default())
        } else {
            Ok(BudgetConfig::default())
        }
    }
    
    /// Save configuration to file
    async fn save_config(&self, config: &BudgetConfig) -> Result<()> {
        if let Some(ref path) = self.config_path {
            // In a real implementation, this would save to TOML file
            info!("Configuration saved to {}", path);
            Ok(())
        } else {
            Ok(())
        }
    }
}

#[async_trait]
impl DebugCommandProcessor for PerformanceBudgetProcessor {
    async fn process(&self, command: DebugCommand) -> Result<DebugResponse> {
        match command {
            DebugCommand::StartBudgetMonitoring => {
                debug!("Starting performance budget monitoring");
                self.start_continuous_monitoring().await?;
                
                Ok(DebugResponse::Success {
                    message: "Performance budget monitoring started".to_string(),
                    data: None,
                })
            }
            
            DebugCommand::StopBudgetMonitoring => {
                debug!("Stopping performance budget monitoring");
                self.stop_continuous_monitoring().await?;
                
                Ok(DebugResponse::Success {
                    message: "Performance budget monitoring stopped".to_string(),
                    data: None,
                })
            }
            
            DebugCommand::SetPerformanceBudget { config } => {
                debug!("Setting performance budget configuration");
                
                // Parse the configuration
                let budget_config: BudgetConfig = serde_json::from_value(config)?;
                
                // Update the monitor
                self.monitor.update_config(budget_config.clone()).await?;
                
                // Save to file
                self.save_config(&budget_config).await?;
                
                Ok(DebugResponse::Success {
                    message: "Performance budget configuration updated".to_string(),
                    data: Some(serde_json::to_value(budget_config)?),
                })
            }
            
            DebugCommand::GetPerformanceBudget => {
                debug!("Getting performance budget configuration");
                let config = self.monitor.get_config().await;
                
                Ok(DebugResponse::Success {
                    message: "Current performance budget configuration".to_string(),
                    data: Some(serde_json::to_value(config)?),
                })
            }
            
            DebugCommand::CheckBudgetViolations => {
                debug!("Checking for budget violations");
                
                // Collect current metrics
                let metrics = Self::collect_metrics(&self.brp_client).await?;
                
                // Check for violations
                let violations = self.monitor.check_violations(metrics).await?;
                
                Ok(DebugResponse::Success {
                    message: format!("Found {} budget violations", violations.len()),
                    data: Some(serde_json::to_value(violations)?),
                })
            }
            
            DebugCommand::GetBudgetViolationHistory { limit } => {
                debug!("Getting budget violation history");
                let history = self.monitor.get_violation_history(limit).await;
                
                Ok(DebugResponse::Success {
                    message: format!("Retrieved {} violations from history", history.len()),
                    data: Some(serde_json::to_value(history)?),
                })
            }
            
            DebugCommand::GenerateComplianceReport { duration_seconds } => {
                debug!("Generating compliance report");
                let duration = Duration::from_secs(duration_seconds.unwrap_or(3600));
                
                match self.monitor.generate_compliance_report(duration).await {
                    Ok(report) => {
                        Ok(DebugResponse::Success {
                            message: format!(
                                "Compliance report generated: {:.1}% overall compliance",
                                report.overall_compliance_percent
                            ),
                            data: Some(serde_json::to_value(report)?),
                        })
                    }
                    Err(e) => {
                        Ok(DebugResponse::Success {
                            message: format!("Could not generate report: {}", e),
                            data: None,
                        })
                    }
                }
            }
            
            DebugCommand::GetBudgetRecommendations => {
                debug!("Getting budget recommendations");
                
                // Generate a 1-hour compliance report to get recommendations
                let duration = Duration::from_secs(3600);
                
                match self.monitor.generate_compliance_report(duration).await {
                    Ok(report) => {
                        Ok(DebugResponse::Success {
                            message: format!("Generated {} budget recommendations", 
                                report.recommendations.len()),
                            data: Some(serde_json::to_value(report.recommendations)?),
                        })
                    }
                    Err(_) => {
                        // No data yet, return empty recommendations
                        Ok(DebugResponse::Success {
                            message: "No recommendations available (insufficient data)".to_string(),
                            data: Some(serde_json::json!([])),
                        })
                    }
                }
            }
            
            DebugCommand::ClearBudgetHistory => {
                debug!("Clearing budget violation history");
                self.monitor.clear_violation_history().await;
                
                Ok(DebugResponse::Success {
                    message: "Budget violation history cleared".to_string(),
                    data: None,
                })
            }
            
            DebugCommand::GetBudgetStatistics => {
                debug!("Getting budget monitoring statistics");
                let stats = self.monitor.get_statistics().await;
                
                // Add processor-specific stats
                let state = self.monitoring_state.read().await;
                let mut all_stats = stats;
                all_stats.insert("continuous_monitoring".to_string(), 
                    serde_json::json!(state.continuous_monitoring));
                all_stats.insert("recent_violations".to_string(), 
                    serde_json::json!(state.recent_violations));
                all_stats.insert("consecutive_violations".to_string(), 
                    serde_json::json!(state.consecutive_violations));
                
                Ok(DebugResponse::Success {
                    message: "Budget monitoring statistics".to_string(),
                    data: Some(serde_json::json!(all_stats)),
                })
            }
            
            _ => Err(Error::DebugError(
                format!("Unsupported command for PerformanceBudgetProcessor: {:?}", command)
            )),
        }
    }
    
    fn supports_command(&self, command: &DebugCommand) -> bool {
        matches!(command,
            DebugCommand::StartBudgetMonitoring |
            DebugCommand::StopBudgetMonitoring |
            DebugCommand::SetPerformanceBudget { .. } |
            DebugCommand::GetPerformanceBudget |
            DebugCommand::CheckBudgetViolations |
            DebugCommand::GetBudgetViolationHistory { .. } |
            DebugCommand::ClearBudgetHistory |
            DebugCommand::GenerateComplianceReport { .. } |
            DebugCommand::GetBudgetRecommendations |
            DebugCommand::GetBudgetStatistics
        )
    }

    async fn validate(&self, command: &DebugCommand) -> Result<()> {
        match command {
            DebugCommand::SetPerformanceBudget { config } => {
                // Validate configuration structure
                let _: BudgetConfig = serde_json::from_value(config.clone())
                    .map_err(|e| Error::Validation(format!("Invalid budget config: {}", e)))?;
                Ok(())
            }
            
            DebugCommand::GenerateComplianceReport { duration_seconds } => {
                if let Some(duration) = duration_seconds {
                    if *duration == 0 {
                        return Err(Error::Validation("Duration must be greater than 0".to_string()));
                    }
                    if *duration > 86400 * 30 {
                        return Err(Error::Validation("Duration cannot exceed 30 days".to_string()));
                    }
                }
                Ok(())
            }
            
            DebugCommand::GetBudgetViolationHistory { limit } => {
                if let Some(limit) = limit {
                    if *limit == 0 {
                        return Err(Error::Validation("Limit must be greater than 0".to_string()));
                    }
                    if *limit > 1000 {
                        return Err(Error::Validation("Limit cannot exceed 1000".to_string()));
                    }
                }
                Ok(())
            }
            
            _ => Ok(()),
        }
    }
    
    fn estimate_processing_time(&self, command: &DebugCommand) -> Duration {
        match command {
            DebugCommand::StartBudgetMonitoring | 
            DebugCommand::StopBudgetMonitoring => Duration::from_millis(50),
            
            DebugCommand::CheckBudgetViolations => Duration::from_millis(100),
            
            DebugCommand::GenerateComplianceReport { .. } => Duration::from_millis(500),
            
            DebugCommand::GetBudgetRecommendations => Duration::from_millis(300),
            
            _ => Duration::from_millis(20),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    async fn create_test_processor() -> PerformanceBudgetProcessor {
        let config = crate::config::Config {
            bevy_brp_host: "localhost".to_string(),
            bevy_brp_port: 15702,
            mcp_port: 3000,
        };
        let brp_client = Arc::new(RwLock::new(BrpClient::new(&config)));
        PerformanceBudgetProcessor::new(brp_client)
    }
    
    #[tokio::test]
    async fn test_processor_creation() {
        let processor = create_test_processor().await;
        
        // Should start with monitoring inactive
        let state = processor.monitoring_state.read().await;
        assert!(!state.continuous_monitoring);
    }
    
    #[tokio::test]
    async fn test_start_stop_monitoring() {
        let processor = create_test_processor().await;
        
        // Start monitoring
        let result = processor.process(DebugCommand::StartBudgetMonitoring).await;
        assert!(result.is_ok());
        
        // Check state
        {
            let state = processor.monitoring_state.read().await;
            assert!(state.continuous_monitoring);
        }
        
        // Stop monitoring
        let result = processor.process(DebugCommand::StopBudgetMonitoring).await;
        assert!(result.is_ok());
        
        // Check state
        {
            let state = processor.monitoring_state.read().await;
            assert!(!state.continuous_monitoring);
        }
    }
    
    #[tokio::test]
    async fn test_budget_configuration() {
        let processor = create_test_processor().await;
        
        // Set a custom budget
        let config = serde_json::json!({
            "frame_time_ms": 20.0,
            "memory_mb": 600.0,
            "cpu_percent": 75.0,
            "auto_adjust": true,
            "violation_threshold": 5
        });
        
        let result = processor.process(DebugCommand::SetPerformanceBudget { config }).await;
        assert!(result.is_ok());
        
        // Get the configuration
        let result = processor.process(DebugCommand::GetPerformanceBudget).await;
        assert!(result.is_ok());
        
        match result.unwrap() {
            DebugResponse::Success { data: Some(data), .. } => {
                let config: BudgetConfig = serde_json::from_value(data).unwrap();
                assert_eq!(config.frame_time_ms, Some(20.0));
                assert_eq!(config.memory_mb, Some(600.0));
                assert_eq!(config.cpu_percent, Some(75.0));
                assert!(config.auto_adjust);
            }
            _ => panic!("Expected Success response with data"),
        }
    }
}