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
/// Integration tests for debug command infrastructure
use bevy_debugger_mcp::brp_messages::{
    DebugCommand, DebugResponse, DebugOverlayType, SessionOperation,
    ValidatedQuery, QueryFilter, QueryCost, EntityData, BrpRequest,
};
use bevy_debugger_mcp::debug_command_processor::{
    DebugCommandRequest, DebugCommandRouter, DebugCommandProcessor,
    EntityInspectionProcessor, DEBUG_COMMAND_TIMEOUT,
};
use bevy_debugger_mcp::error::{Error, Result};
use async_trait::async_trait;
use serde_json::json;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::time::sleep;

/// Mock processor for testing
struct MockDebugProcessor {
    delay_ms: u64,
    should_fail: bool,
}

#[async_trait]
impl DebugCommandProcessor for MockDebugProcessor {
    async fn process(&self, command: DebugCommand) -> Result<DebugResponse> {
        if self.delay_ms > 0 {
            sleep(Duration::from_millis(self.delay_ms)).await;
        }
        
        if self.should_fail {
            return Err(Error::DebugError("Mock processor failed".to_string()));
        }
        
        match command {
            DebugCommand::GetStatus => {
                Ok(DebugResponse::Status {
                    version: "test-1.0.0".to_string(),
                    active_sessions: 1,
                    command_queue_size: 0,
                    performance_overhead_percent: 0.5,
                })
            }
            _ => Ok(DebugResponse::Custom(json!({"mock": "response"})))
        }
    }
    
    async fn validate(&self, _command: &DebugCommand) -> Result<()> {
        if self.should_fail {
            Err(Error::DebugError("Validation failed".to_string()))
        } else {
            Ok(())
        }
    }
    
    fn estimate_processing_time(&self, _command: &DebugCommand) -> Duration {
        Duration::from_millis(self.delay_ms)
    }
    
    fn supports_command(&self, command: &DebugCommand) -> bool {
        matches!(command, DebugCommand::GetStatus | DebugCommand::Custom { .. })
    }
}

#[tokio::test]
async fn test_debug_command_compatibility() {
    // Ensure new debug commands don't break existing BRP commands
    let query_request = BrpRequest::Query {
        filter: None,
        limit: Some(10),
    };
    
    let debug_request = BrpRequest::Debug {
        command: DebugCommand::GetStatus,
        correlation_id: "test-123".to_string(),
        priority: Some(5),
    };
    
    // Both should serialize/deserialize correctly
    let query_json = serde_json::to_string(&query_request).unwrap();
    let debug_json = serde_json::to_string(&debug_request).unwrap();
    
    let _query_parsed: BrpRequest = serde_json::from_str(&query_json).unwrap();
    let _debug_parsed: BrpRequest = serde_json::from_str(&debug_json).unwrap();
}

#[tokio::test]
async fn test_command_timeout() {
    let router = DebugCommandRouter::new();
    
    // Register a slow processor
    let slow_processor = Arc::new(MockDebugProcessor {
        delay_ms: 100,
        should_fail: false,
    });
    
    router.register_processor("mock".to_string(), slow_processor).await;
    
    // Create a command that will timeout
    let mut request = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "timeout-test".to_string(),
        None,
    );
    
    // Set a very short timeout
    request.timeout = Duration::from_millis(50);
    
    // Queue and process
    router.queue_command(request.clone()).await.unwrap();
    
    // Wait for timeout
    sleep(Duration::from_millis(60)).await;
    
    // Should timeout
    let result = router.process_next().await;
    assert!(result.is_some());
    assert!(matches!(result.unwrap(), Err(Error::Timeout(_))));
}

#[tokio::test]
async fn test_concurrent_commands() {
    let router = Arc::new(DebugCommandRouter::new());
    
    // Register processor
    let processor = Arc::new(MockDebugProcessor {
        delay_ms: 10,
        should_fail: false,
    });
    
    router.register_processor("mock".to_string(), processor).await;
    
    // Create 100+ concurrent commands
    let mut handles = Vec::new();
    
    for i in 0..100 {
        let router_clone = router.clone();
        let handle = tokio::spawn(async move {
            let request = DebugCommandRequest::new(
                DebugCommand::GetStatus,
                format!("concurrent-{}", i),
                Some((i % 10) as u8), // Vary priorities
            );
            
            router_clone.queue_command(request).await
        });
        
        handles.push(handle);
    }
    
    // Wait for all commands to be queued
    for handle in handles {
        handle.await.unwrap().unwrap();
    }
    
    // Process all commands
    let mut processed = 0;
    while let Some(result) = router.process_next().await {
        assert!(result.is_ok());
        processed += 1;
        if processed >= 100 {
            break;
        }
    }
    
    assert_eq!(processed, 100);
}

#[tokio::test]
async fn test_priority_ordering() {
    let router = DebugCommandRouter::new();
    
    // Register processor
    let processor = Arc::new(MockDebugProcessor {
        delay_ms: 0,
        should_fail: false,
    });
    
    router.register_processor("mock".to_string(), processor).await;
    
    // Queue commands with different priorities
    let low = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "low".to_string(),
        Some(1),
    );
    
    let medium = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "medium".to_string(),
        Some(5),
    );
    
    let high = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "high".to_string(),
        Some(9),
    );
    
    // Queue in reverse priority order
    router.queue_command(low).await.unwrap();
    router.queue_command(medium).await.unwrap();
    router.queue_command(high).await.unwrap();
    
    // Process and check order
    let first = router.process_next().await.unwrap().unwrap();
    assert_eq!(first.0, "high");
    
    let second = router.process_next().await.unwrap().unwrap();
    assert_eq!(second.0, "medium");
    
    let third = router.process_next().await.unwrap().unwrap();
    assert_eq!(third.0, "low");
}

#[tokio::test]
async fn test_response_correlation_ttl() {
    let router = DebugCommandRouter::new();
    
    // Register processor
    let processor = Arc::new(MockDebugProcessor {
        delay_ms: 0,
        should_fail: false,
    });
    
    router.register_processor("mock".to_string(), processor).await;
    
    // Create command with short TTL
    let mut request = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "ttl-test".to_string(),
        None,
    );
    request.response_ttl = Duration::from_millis(100);
    
    // Process command
    router.queue_command(request).await.unwrap();
    let result = router.process_next().await.unwrap().unwrap();
    
    // Response should be available immediately
    let response = router.get_response(&result.0).await;
    assert!(response.is_some());
    
    // Wait for TTL to expire
    sleep(Duration::from_millis(150)).await;
    
    // Clean up expired responses
    router.cleanup_expired_responses().await;
    
    // Response should no longer be available
    let response = router.get_response(&result.0).await;
    assert!(response.is_none());
}

#[tokio::test]
async fn test_metrics_collection() {
    let router = DebugCommandRouter::new();
    
    // Register processors
    let success_processor = Arc::new(MockDebugProcessor {
        delay_ms: 10,
        should_fail: false,
    });
    
    let failure_processor = Arc::new(MockDebugProcessor {
        delay_ms: 5,
        should_fail: true,
    });
    
    router.register_processor("success".to_string(), success_processor).await;
    router.register_processor("failure".to_string(), failure_processor).await;
    
    // Process successful commands
    for i in 0..5 {
        let request = DebugCommandRequest::new(
            DebugCommand::GetStatus,
            format!("success-{}", i),
            None,
        );
        router.queue_command(request).await.unwrap();
        router.process_next().await.unwrap().unwrap();
    }
    
    // Process failing commands
    for i in 0..3 {
        let request = DebugCommandRequest::new(
            DebugCommand::Custom {
                name: "fail".to_string(),
                params: json!({}),
            },
            format!("fail-{}", i),
            None,
        );
        router.queue_command(request).await.unwrap();
        let _ = router.process_next().await;
    }
    
    // Check metrics
    let metrics = router.get_metrics().await;
    assert_eq!(metrics.total_commands, 8);
    assert_eq!(metrics.successful_commands, 5);
    assert_eq!(metrics.failed_commands, 3);
    assert!(metrics.avg_processing_time_us > 0);
    assert!(metrics.is_within_performance_target()); // Should be < 1ms average
}

#[tokio::test]
async fn test_entity_inspection_processor() {
    let processor = EntityInspectionProcessor {};
    
    // Test valid entity inspection
    let command = DebugCommand::InspectEntity {
        entity_id: 123,
        include_metadata: Some(true),
        include_relationships: Some(true),
    };
    
    let response = processor.process(command).await.unwrap();
    
    match response {
        DebugResponse::EntityInspection { entity, metadata, relationships } => {
            assert_eq!(entity.id, 123);
            assert!(metadata.is_some());
            assert!(relationships.is_some());
        }
        _ => panic!("Wrong response type"),
    }
    
    // Test validation
    let invalid_command = DebugCommand::InspectEntity {
        entity_id: 0, // Invalid ID
        include_metadata: None,
        include_relationships: None,
    };
    
    let validation_result = processor.validate(&invalid_command).await;
    assert!(validation_result.is_err());
}

#[tokio::test]
async fn test_debug_command_serialization() {
    // Test all debug command variants serialize correctly
    let commands = vec![
        DebugCommand::InspectEntity {
            entity_id: 456,
            include_metadata: Some(false),
            include_relationships: None,
        },
        DebugCommand::ProfileSystem {
            system_name: "test_system".to_string(),
            duration_ms: Some(1000),
            track_allocations: Some(true),
        },
        DebugCommand::SetVisualDebug {
            overlay_type: DebugOverlayType::Colliders,
            enabled: true,
            config: Some(json!({"color": "red"})),
        },
        DebugCommand::ExecuteQuery {
            query: ValidatedQuery {
                id: "query-1".to_string(),
                filter: QueryFilter {
                    with: Some(vec!["Transform".to_string()]),
                    without: None,
                    where_clause: None,
                },
                estimated_cost: QueryCost {
                    estimated_entities: 100,
                    estimated_time_us: 500,
                    estimated_memory: 1024,
                },
                hints: vec!["Use index".to_string()],
            },
            offset: Some(0),
            limit: Some(50),
        },
        DebugCommand::ProfileMemory {
            capture_backtraces: Some(false),
            target_systems: Some(vec!["system1".to_string()]),
        },
        DebugCommand::SessionControl {
            operation: SessionOperation::Create,
            session_id: None,
        },
        DebugCommand::GetStatus,
        DebugCommand::Custom {
            name: "custom_command".to_string(),
            params: json!({"key": "value"}),
        },
    ];
    
    for command in commands {
        let serialized = serde_json::to_string(&command).unwrap();
        let deserialized: DebugCommand = serde_json::from_str(&serialized).unwrap();
        
        // Verify round-trip works
        let reserialized = serde_json::to_string(&deserialized).unwrap();
        assert_eq!(serialized, reserialized);
    }
}

#[tokio::test]
async fn test_debug_response_serialization() {
    // Test all debug response variants serialize correctly
    let responses = vec![
        DebugResponse::EntityInspection {
            entity: EntityData {
                id: 789,
                components: HashMap::new(),
            },
            metadata: None,
            relationships: None,
        },
        DebugResponse::Status {
            version: "1.2.3".to_string(),
            active_sessions: 2,
            command_queue_size: 5,
            performance_overhead_percent: 1.5,
        },
        DebugResponse::Custom(json!({"custom": "data"})),
    ];
    
    for response in responses {
        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: DebugResponse = serde_json::from_str(&serialized).unwrap();
        
        // Verify round-trip works
        let reserialized = serde_json::to_string(&deserialized).unwrap();
        assert_eq!(serialized, reserialized);
    }
}

#[tokio::test]
async fn test_performance_requirement() {
    // Test that debug command processing meets <1ms requirement
    let router = DebugCommandRouter::new();
    
    // Register fast processor
    let processor = Arc::new(MockDebugProcessor {
        delay_ms: 0, // No artificial delay
        should_fail: false,
    });
    
    router.register_processor("fast".to_string(), processor).await;
    
    // Measure processing time
    let start = Instant::now();
    
    for _ in 0..100 {
        let request = DebugCommandRequest::new(
            DebugCommand::GetStatus,
            "perf-test".to_string(),
            None,
        );
        
        router.queue_command(request).await.unwrap();
        router.process_next().await.unwrap().unwrap();
    }
    
    let elapsed = start.elapsed();
    let avg_time_us = elapsed.as_micros() / 100;
    
    // Should be less than 1000us (1ms) average
    assert!(avg_time_us < 1000, "Average processing time {}us exceeds 1ms requirement", avg_time_us);
}

#[tokio::test]
async fn test_backward_compatibility() {
    // Ensure debug commands don't break existing tool routing
    use bevy_debugger_mcp::mcp_server::McpServer;
    use bevy_debugger_mcp::config::Config;
    use bevy_debugger_mcp::brp_client::BrpClient;
    use tokio::sync::RwLock;
    
    let 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)));
    let server = McpServer::new(config, brp_client);
    
    // Test that existing tools still work
    let tools = vec![
        "observe",
        "experiment", 
        "screenshot",
        "hypothesis",
        "stress",
        "replay",
        "anomaly",
    ];
    
    for tool in tools {
        // These will fail due to no BRP connection, but shouldn't panic
        let _ = server.handle_tool_call(tool, json!({})).await;
    }
    
    // Test new debug tool
    let debug_args = json!({
        "command": {
            "type": "GetStatus"
        },
        "correlation_id": "test-compat",
        "priority": 5
    });
    
    let result = server.handle_tool_call("debug", debug_args).await;
    // Should at least not panic, even if it returns an error
    assert!(result.is_ok() || result.is_err());
}

#[tokio::test]
async fn test_debug_timeout_is_30_seconds() {
    // Verify the default timeout is 30 seconds as per requirements
    assert_eq!(DEBUG_COMMAND_TIMEOUT, Duration::from_secs(30));
    
    let request = DebugCommandRequest::new(
        DebugCommand::GetStatus,
        "timeout-check".to_string(),
        None,
    );
    
    assert_eq!(request.timeout, Duration::from_secs(30));
}