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
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
/// Semantic analysis for understanding game state queries in natural language
use regex::Regex;
use serde::{Deserialize, Serialize};
use strsim::jaro_winkler;

use crate::brp_messages::{BrpRequest, ComponentFilter, ComponentValue, FilterOp, QueryFilter};
use crate::error::{Error, Result};

/// Game concepts that can be analyzed semantically
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum GameConcept {
    /// Entities that appear stuck (low velocity but have movement target)
    StuckEntities,
    /// Entities moving faster than normal
    FastMovingObjects,
    /// Entities with overlapping collision bounds
    OverlappingColliders,
    /// Entities that might be memory leaks (inactive but taking resources)
    PotentialMemoryLeaks,
    /// Entities with inconsistent state
    InconsistentState,
    /// Entities experiencing physics violations
    PhysicsViolations,
}

impl GameConcept {
    /// Get human-readable description
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::StuckEntities => "Entities that appear stuck (have target but low velocity)",
            Self::FastMovingObjects => "Entities moving faster than expected",
            Self::OverlappingColliders => "Entities with overlapping collision boundaries",
            Self::PotentialMemoryLeaks => "Inactive entities consuming resources",
            Self::InconsistentState => "Entities with contradictory component values",
            Self::PhysicsViolations => "Entities violating physics constraints",
        }
    }
}

/// Configurable thresholds for semantic analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticThresholds {
    /// Velocity threshold below which entity is considered stuck (units/second)
    pub stuck_velocity_threshold: f32,
    /// Velocity threshold above which entity is considered fast
    pub fast_velocity_threshold: f32,
    /// Distance threshold for overlapping colliders
    pub overlap_distance_threshold: f32,
    /// Time threshold for inactive entities (seconds)
    pub inactive_time_threshold: f32,
    /// Fuzzy matching threshold for component names (0.0-1.0)
    pub fuzzy_match_threshold: f32,
}

impl Default for SemanticThresholds {
    fn default() -> Self {
        Self {
            stuck_velocity_threshold: 0.1,
            fast_velocity_threshold: 50.0,
            overlap_distance_threshold: 0.5,
            inactive_time_threshold: 60.0,
            fuzzy_match_threshold: 0.8,
        }
    }
}

/// Explanation for why an entity matches semantic criteria
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MatchExplanation {
    pub concept: GameConcept,
    pub reason: String,
    pub confidence: f32,
    pub relevant_components: Vec<String>,
}

/// Result of semantic analysis with explanations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticQueryResult {
    pub request: BrpRequest,
    pub explanations: Vec<MatchExplanation>,
    pub suggestions: Vec<String>,
}

/// Builder for constructing complex semantic queries
#[derive(Debug, Clone)]
pub struct SemanticQueryBuilder {
    concepts: Vec<GameConcept>,
    logic: QueryLogic,
    thresholds: SemanticThresholds,
    component_filters: Vec<ComponentFilter>,
}

/// Logic operators for combining semantic queries
#[derive(Debug, Clone, PartialEq)]
pub enum QueryLogic {
    And,
    Or,
}

impl SemanticQueryBuilder {
    /// Create a new semantic query builder
    #[must_use]
    pub fn new() -> Self {
        Self {
            concepts: Vec::new(),
            logic: QueryLogic::And,
            thresholds: SemanticThresholds::default(),
            component_filters: Vec::new(),
        }
    }

    /// Add a game concept to analyze
    #[must_use]
    pub fn with_concept(mut self, concept: GameConcept) -> Self {
        self.concepts.push(concept);
        self
    }

    /// Set query logic (AND/OR)
    #[must_use]
    pub fn with_logic(mut self, logic: QueryLogic) -> Self {
        self.logic = logic;
        self
    }

    /// Set custom thresholds
    #[must_use]
    pub fn with_thresholds(mut self, thresholds: SemanticThresholds) -> Self {
        self.thresholds = thresholds;
        self
    }

    /// Add component filter
    #[must_use]
    pub fn with_component_filter(mut self, filter: ComponentFilter) -> Self {
        self.component_filters.push(filter);
        self
    }

    /// Build the semantic query
    pub fn build(self) -> Result<SemanticQueryResult> {
        if self.concepts.is_empty() {
            return Err(Error::Brp(
                "No concepts specified for semantic query".to_string(),
            ));
        }

        let mut with_components = Vec::new();
        let mut where_clauses = self.component_filters.clone();
        let mut explanations = Vec::new();

        for concept in &self.concepts {
            let (components, filters, explanation) = self.build_concept_query(concept)?;
            with_components.extend(components);
            where_clauses.extend(filters);
            explanations.push(explanation);
        }

        // Remove duplicates
        with_components.sort();
        with_components.dedup();

        let request = BrpRequest::Query {
            filter: Some(QueryFilter {
                with: if with_components.is_empty() {
                    None
                } else {
                    Some(with_components)
                },
                without: None,
                where_clause: if where_clauses.is_empty() {
                    None
                } else {
                    Some(where_clauses)
                },
            }),
            limit: Some(100), // Reasonable default for semantic queries
        };

        let suggestions = self.generate_suggestions();

        Ok(SemanticQueryResult {
            request,
            explanations,
            suggestions,
        })
    }

    fn build_concept_query(
        &self,
        concept: &GameConcept,
    ) -> Result<(Vec<String>, Vec<ComponentFilter>, MatchExplanation)> {
        match concept {
            GameConcept::StuckEntities => {
                let components = vec!["Transform".to_string(), "Velocity".to_string()];
                let filters = vec![
                    ComponentFilter {
                        component: "Velocity".to_string(),
                        field: Some("linear.x".to_string()),
                        op: FilterOp::LessThan,
                        value: serde_json::json!(self.thresholds.stuck_velocity_threshold),
                    },
                    ComponentFilter {
                        component: "Velocity".to_string(),
                        field: Some("linear.y".to_string()),
                        op: FilterOp::LessThan,
                        value: serde_json::json!(self.thresholds.stuck_velocity_threshold),
                    },
                ];
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: format!(
                        "Entities with velocity magnitude below {} units/second",
                        self.thresholds.stuck_velocity_threshold
                    ),
                    confidence: 0.9,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
            GameConcept::FastMovingObjects => {
                let components = vec!["Velocity".to_string()];
                let filters = vec![ComponentFilter {
                    component: "Velocity".to_string(),
                    field: Some("linear.x".to_string()),
                    op: FilterOp::GreaterThan,
                    value: serde_json::json!(self.thresholds.fast_velocity_threshold),
                }];
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: format!(
                        "Entities with velocity above {} units/second",
                        self.thresholds.fast_velocity_threshold
                    ),
                    confidence: 0.85,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
            GameConcept::OverlappingColliders => {
                let components = vec!["Transform".to_string(), "Collider".to_string()];
                let filters = Vec::new(); // Complex spatial analysis would require post-processing
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: "Entities with colliders that may be overlapping (requires spatial analysis)".to_string(),
                    confidence: 0.7,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
            GameConcept::PotentialMemoryLeaks => {
                let components = vec!["Transform".to_string()];
                let filters = Vec::new(); // Would require analysis of entity lifecycle
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: "Entities that may be consuming resources without active purpose"
                        .to_string(),
                    confidence: 0.6,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
            GameConcept::InconsistentState => {
                let components = vec!["Health".to_string(), "Alive".to_string()];
                let filters = vec![
                    ComponentFilter {
                        component: "Health".to_string(),
                        field: Some("current".to_string()),
                        op: FilterOp::LessThanOrEqual,
                        value: serde_json::json!(0.0),
                    },
                    ComponentFilter {
                        component: "Alive".to_string(),
                        field: None,
                        op: FilterOp::Equal,
                        value: ComponentValue::Bool(true),
                    },
                ];
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: "Entities marked as alive but with zero or negative health".to_string(),
                    confidence: 0.95,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
            GameConcept::PhysicsViolations => {
                let components = vec!["Transform".to_string(), "RigidBody".to_string()];
                let filters = Vec::new(); // Would require physics constraint validation
                let explanation = MatchExplanation {
                    concept: concept.clone(),
                    reason: "Entities potentially violating physics constraints".to_string(),
                    confidence: 0.75,
                    relevant_components: components.clone(),
                };
                Ok((components, filters, explanation))
            }
        }
    }

    fn generate_suggestions(&self) -> Vec<String> {
        let mut suggestions = Vec::new();

        if self.concepts.contains(&GameConcept::StuckEntities) {
            suggestions.push("Try adjusting stuck_velocity_threshold in config".to_string());
            suggestions.push("Look for entities with NavigationTarget component".to_string());
        }

        if self.concepts.contains(&GameConcept::FastMovingObjects) {
            suggestions.push("Check for runaway physics simulations".to_string());
            suggestions.push("Verify velocity damping is applied correctly".to_string());
        }

        if self.concepts.contains(&GameConcept::OverlappingColliders) {
            suggestions.push("Use spatial partitioning for collision detection".to_string());
            suggestions.push("Check collision layer configuration".to_string());
        }

        suggestions
    }
}

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

/// Semantic analyzer for understanding natural language queries about game state
pub struct SemanticAnalyzer {
    thresholds: SemanticThresholds,
    patterns: Vec<SemanticPattern>,
}

struct SemanticPattern {
    pattern: Regex,
    concepts: Vec<GameConcept>,
    builder_fn: fn(&SemanticAnalyzer, &regex::Captures) -> Result<SemanticQueryBuilder>,
}

impl SemanticAnalyzer {
    /// Create a new semantic analyzer with default thresholds
    #[must_use]
    pub fn new() -> Self {
        Self::with_thresholds(SemanticThresholds::default())
    }

    /// Create a semantic analyzer with custom thresholds
    #[must_use]
    pub fn with_thresholds(thresholds: SemanticThresholds) -> Self {
        let patterns = vec![
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+stuck\s+entities?").unwrap(),
                concepts: vec![GameConcept::StuckEntities],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::StuckEntities)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            SemanticPattern {
                pattern: Regex::new(r"(?i)(?:show|find)\s+fast\s+moving\s+(?:objects?|entities?)")
                    .unwrap(),
                concepts: vec![GameConcept::FastMovingObjects],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::FastMovingObjects)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+overlapping\s+colliders?").unwrap(),
                concepts: vec![GameConcept::OverlappingColliders],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::OverlappingColliders)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+(?:memory\s+leaks?|leaked\s+entities?)").unwrap(),
                concepts: vec![GameConcept::PotentialMemoryLeaks],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::PotentialMemoryLeaks)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+(?:inconsistent|invalid)\s+(?:state|entities?)")
                    .unwrap(),
                concepts: vec![GameConcept::InconsistentState],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::InconsistentState)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+physics\s+violations?").unwrap(),
                concepts: vec![GameConcept::PhysicsViolations],
                builder_fn: |analyzer, _| {
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::PhysicsViolations)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
            // Compound queries
            SemanticPattern {
                pattern: Regex::new(r"(?i)find\s+stuck\s+(?:and|or)\s+fast\s+entities?").unwrap(),
                concepts: vec![GameConcept::StuckEntities, GameConcept::FastMovingObjects],
                builder_fn: |analyzer, caps| {
                    let logic = if caps[0].to_lowercase().contains("and") {
                        QueryLogic::And
                    } else {
                        QueryLogic::Or
                    };
                    Ok(SemanticQueryBuilder::new()
                        .with_concept(GameConcept::StuckEntities)
                        .with_concept(GameConcept::FastMovingObjects)
                        .with_logic(logic)
                        .with_thresholds(analyzer.thresholds.clone()))
                },
            },
        ];

        Self {
            thresholds,
            patterns,
        }
    }

    /// Analyze a semantic query and return structured query result
    ///
    /// # Errors
    /// Returns error if query cannot be parsed or analyzed
    pub fn analyze(&self, query: &str) -> Result<SemanticQueryResult> {
        let query = query.trim();

        if query.is_empty() {
            return Err(Error::Brp("Empty semantic query".to_string()));
        }

        // Try to match against known semantic patterns
        for pattern in &self.patterns {
            if let Some(captures) = pattern.pattern.captures(query) {
                let builder = (pattern.builder_fn)(self, &captures)?;
                return builder.build();
            }
        }

        // Try fuzzy matching for component names
        if let Some(suggestion) = self.suggest_component_query(query) {
            return Err(Error::Brp(format!(
                "Unrecognized semantic query: '{query}'. Did you mean: '{suggestion}'?"
            )));
        }

        Err(Error::Brp(format!(
            "Unrecognized semantic query: '{}'. Available concepts: {}",
            query,
            self.available_concepts().join(", ")
        )))
    }

    fn suggest_component_query(&self, query: &str) -> Option<String> {
        let known_concepts = [
            "stuck entities",
            "fast moving objects",
            "overlapping colliders",
            "memory leaks",
            "inconsistent state",
            "physics violations",
        ];

        let mut best_match = None;
        let mut best_score = 0.0;

        for concept in &known_concepts {
            let score = jaro_winkler(query, concept);
            if score > f64::from(self.thresholds.fuzzy_match_threshold) && score > best_score {
                best_score = score;
                best_match = Some(*concept);
            }
        }

        best_match.map(|s| format!("find {s}"))
    }

    fn available_concepts(&self) -> Vec<String> {
        vec![
            "stuck entities".to_string(),
            "fast moving objects".to_string(),
            "overlapping colliders".to_string(),
            "memory leaks".to_string(),
            "inconsistent state".to_string(),
            "physics violations".to_string(),
        ]
    }

    /// Get current thresholds
    #[must_use]
    pub fn thresholds(&self) -> &SemanticThresholds {
        &self.thresholds
    }

    /// Update thresholds
    pub fn set_thresholds(&mut self, thresholds: SemanticThresholds) {
        self.thresholds = thresholds;
    }
}

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

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

    #[test]
    fn test_semantic_query_builder() {
        let builder = SemanticQueryBuilder::new()
            .with_concept(GameConcept::StuckEntities)
            .with_logic(QueryLogic::And);

        let result = builder.build().unwrap();
        assert_eq!(result.explanations.len(), 1);
        assert_eq!(result.explanations[0].concept, GameConcept::StuckEntities);
    }

    #[test]
    fn test_semantic_analyzer_stuck_entities() {
        let analyzer = SemanticAnalyzer::new();
        let result = analyzer.analyze("find stuck entities").unwrap();

        assert_eq!(result.explanations.len(), 1);
        assert_eq!(result.explanations[0].concept, GameConcept::StuckEntities);
        assert!(!result.suggestions.is_empty());
    }

    #[test]
    fn test_semantic_analyzer_fast_moving() {
        let analyzer = SemanticAnalyzer::new();
        let result = analyzer.analyze("show fast moving objects").unwrap();

        assert_eq!(result.explanations.len(), 1);
        assert_eq!(
            result.explanations[0].concept,
            GameConcept::FastMovingObjects
        );
    }

    #[test]
    fn test_compound_query() {
        let analyzer = SemanticAnalyzer::new();
        let result = analyzer.analyze("find stuck and fast entities").unwrap();

        assert_eq!(result.explanations.len(), 2);
    }

    #[test]
    fn test_fuzzy_matching() {
        let analyzer = SemanticAnalyzer::new();
        let result = analyzer.analyze("find stuk entities"); // Typo

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("stuck entities"));
    }

    #[test]
    fn test_custom_thresholds() {
        let custom_thresholds = SemanticThresholds {
            stuck_velocity_threshold: 0.05,
            fast_velocity_threshold: 100.0,
            ..Default::default()
        };

        let analyzer = SemanticAnalyzer::with_thresholds(custom_thresholds.clone());
        let result = analyzer.analyze("find stuck entities").unwrap();

        assert!(result.explanations[0].reason.contains("0.05"));
    }

    #[test]
    fn test_game_concepts() {
        assert_eq!(
            GameConcept::StuckEntities.description(),
            "Entities that appear stuck (have target but low velocity)"
        );
        assert_eq!(
            GameConcept::FastMovingObjects.description(),
            "Entities moving faster than expected"
        );
    }
}