bevy_fairy 0.2.2

An agentic debugger for Bevy games: a Bevy plugin that exposes the game's state over BRP, and an MCP server that lets a coding agent read and drive it
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
/*
 * bevy-fairy - Bevy Reflection Inspector
 * Copyright (C) 2025 ladvien
 *
 * Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.
 *
 * Relicensed from GPL-3.0 when this crate was adopted into Ladvien/foundation_vs_slop: a GPL
 * crate in the Bevy ecosystem cannot be adopted, and being adoptable is why it is published.
 */

//! # Bevy Reflection Integration
//!
//! This module integrates Bevy's reflection system for dynamic component inspection,
//! type discovery, and advanced debugging capabilities. It provides runtime introspection
//! of component types using Bevy's TypeRegistry and Reflect trait.
//!
//! ## Key Features
//!
//! - **TypeRegistry Integration**: Discover all registered component types dynamically
//! - **Reflection-based Queries**: Query components using reflection metadata
//! - **Complex Type Handling**: Support for `Option<T>`, `Vec<T>`, `HashMap<K,V>`, etc.
//! - **Custom Inspectors**: Extensible inspector system for complex types
//! - **Reflection-based Diffing**: Compare component states using reflection
//! - **Bevy 0.16 Compatible**: Uses latest Bevy reflection APIs

#[cfg(feature = "bevy-reflection")]
use bevy::prelude::*;
#[cfg(feature = "bevy-reflection")]
use bevy::reflect::*;
#[cfg(feature = "bevy-reflection")]
use ::serde::{Deserialize, Serialize};
#[cfg(not(feature = "bevy-reflection"))]
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn, error};

use crate::server::error::{Error, Result};

fn default_type_id() -> TypeId {
    TypeId::of::<()>()
}
use crate::server::brp_messages::ComponentValue;

/// Reflection-based component inspector for Bevy components
#[derive(Clone)]
pub struct BevyReflectionInspector {
    /// Cache of reflection metadata by type name
    reflection_cache: Arc<RwLock<HashMap<String, ReflectionMetadata>>>,
    /// Custom inspectors for complex types
    custom_inspectors: Arc<RwLock<HashMap<String, Box<dyn CustomInspector + Send + Sync>>>>,
}

/// Metadata about a reflected component type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReflectionMetadata {
    /// Type name as registered in TypeRegistry
    pub type_name: String,
    /// Type ID for efficient lookup
    #[serde(skip, default = "default_type_id")]
    pub type_id: TypeId,
    /// Whether this type supports reflection
    pub is_reflected: bool,
    /// Field information for struct types
    pub fields: Vec<FieldMetadata>,
    /// Type category (struct, enum, tuple, etc.)
    pub type_category: TypeCategory,
    /// Custom type information
    pub type_info: Option<Value>,
    /// Last updated timestamp
    pub last_updated: u64,
}

/// Information about a field in a reflected struct
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldMetadata {
    /// Field name
    pub name: String,
    /// Field type name
    pub type_name: String,
    /// Field index in the struct
    pub index: usize,
    /// Whether field supports reflection
    pub is_reflected: bool,
    /// Custom inspector for this field
    pub inspector_name: Option<String>,
}

/// Category of reflected type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TypeCategory {
    Struct,
    TupleStruct,
    Enum,
    Array,
    List,
    Map,
    Value,
}

/// Result of reflection-based component inspection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReflectionInspectionResult {
    /// Component type name
    pub component_type: String,
    /// Reflection metadata
    pub metadata: ReflectionMetadata,
    /// Inspected field values
    pub field_values: HashMap<String, InspectedValue>,
    /// Any errors encountered during inspection
    pub errors: Vec<String>,
    /// Inspection timestamp
    pub timestamp: u64,
}

/// Value inspected using reflection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InspectedValue {
    /// Field name
    pub name: String,
    /// Raw JSON value
    pub raw_value: Value,
    /// Reflected type information
    pub type_info: String,
    /// Human-readable display value
    pub display_value: String,
    /// Whether this value supports further inspection
    pub inspectable: bool,
    /// Child values for nested structures
    pub children: Option<Vec<InspectedValue>>,
}

/// Trait for custom component inspectors
pub trait CustomInspector: Any + Send + Sync {
    /// Inspect a component value using custom logic
    fn inspect(&self, value: &ComponentValue, type_name: &str) -> Result<InspectedValue>;
    
    /// Get the inspector name
    fn name(&self) -> &str;
    
    /// Get supported type names
    fn supported_types(&self) -> Vec<String>;
}

/// Result of reflection-based diffing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReflectionDiffResult {
    /// Type being compared
    pub type_name: String,
    /// Field-level differences
    pub field_diffs: HashMap<String, FieldDiff>,
    /// Summary statistics
    pub summary: DiffSummary,
    /// Detailed change descriptions
    pub change_descriptions: Vec<String>,
}

/// Difference in a specific field
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldDiff {
    /// Field name
    pub field_name: String,
    /// Field type
    pub field_type: String,
    /// Old value (if available)
    pub old_value: Option<InspectedValue>,
    /// New value (if available)
    pub new_value: Option<InspectedValue>,
    /// Type of change
    pub change_type: ChangeType,
    /// Semantic significance of this change
    pub significance: ChangeSeverity,
}

/// Type of change detected
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChangeType {
    Added,
    Removed,
    Modified,
    TypeChanged,
}

/// Severity of a detected change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChangeSeverity {
    Trivial,   // Small numerical changes, formatting
    Minor,     // Field value changes within expected ranges
    Major,     // Significant state changes
    Critical,  // Type changes, structural modifications
}

/// Summary of diff results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffSummary {
    pub total_fields: usize,
    pub changed_fields: usize,
    pub added_fields: usize,
    pub removed_fields: usize,
    pub critical_changes: usize,
    pub major_changes: usize,
    pub minor_changes: usize,
    pub trivial_changes: usize,
}

impl BevyReflectionInspector {
    /// Create a new reflection inspector
    pub fn new() -> Self {
        let mut inspector = Self {
            reflection_cache: Arc::new(RwLock::new(HashMap::new())),
            custom_inspectors: Arc::new(RwLock::new(HashMap::new())),
        };
        
        // Register default custom inspectors
        inspector.register_default_inspectors();
        inspector
    }

    /// Register default custom inspectors for common Bevy types
    fn register_default_inspectors(&mut self) {
        // Note: Implementation would register inspectors here
        // This is a placeholder for the structure
        debug!("Registering default custom inspectors");
    }

    /// Register a custom inspector for specific types
    pub async fn register_custom_inspector(&self, inspector: Box<dyn CustomInspector + Send + Sync>) {
        let inspector_name = inspector.name().to_string();
        let mut inspectors = self.custom_inspectors.write().await;
        inspectors.insert(inspector_name.clone(), inspector);
        info!("Registered custom inspector: {}", inspector_name);
    }

    /// Discover component types using TypeRegistry (when Bevy feature is enabled)
    #[cfg(feature = "bevy-reflection")]
    pub async fn discover_component_types(&self, type_registry: &TypeRegistry) -> Result<Vec<ReflectionMetadata>> {
        debug!("Discovering component types from TypeRegistry");
        let mut discovered_types = Vec::new();
        
        for registration in type_registry.iter() {
            let type_info = registration.type_info();
            let metadata = self.build_reflection_metadata(registration).await?;
            discovered_types.push(metadata);
        }
        
        info!("Discovered {} reflected component types", discovered_types.len());
        Ok(discovered_types)
    }

    /// Build reflection metadata from type registration
    #[cfg(feature = "bevy-reflection")]
    async fn build_reflection_metadata(&self, registration: &TypeRegistration) -> Result<ReflectionMetadata> {
        let type_info = registration.type_info();
        let type_name = type_info.type_path().to_string();
        let type_id = registration.type_id();
        
        let (type_category, fields) = match type_info {
            TypeInfo::Struct(struct_info) => {
                let mut field_metadata = Vec::new();
                for (index, field) in struct_info.iter().enumerate() {
                    field_metadata.push(FieldMetadata {
                        name: field.name().to_string(),
                        type_name: field.type_path().to_string(),
                        index,
                        is_reflected: true, // If it's in the registry, it should be reflected
                        inspector_name: self.find_inspector_for_type(field.type_path()).await,
                    });
                }
                (TypeCategory::Struct, field_metadata)
            }
            TypeInfo::TupleStruct(tuple_info) => {
                let mut field_metadata = Vec::new();
                for (index, field) in tuple_info.iter().enumerate() {
                    field_metadata.push(FieldMetadata {
                        name: format!("field_{}", index),
                        type_name: field.type_path().to_string(),
                        index,
                        is_reflected: true,
                        inspector_name: self.find_inspector_for_type(field.type_path()).await,
                    });
                }
                (TypeCategory::TupleStruct, field_metadata)
            }
            TypeInfo::Enum(_) => (TypeCategory::Enum, Vec::new()),
            TypeInfo::Array(_) => (TypeCategory::Array, Vec::new()),
            TypeInfo::List(_) => (TypeCategory::List, Vec::new()),
            TypeInfo::Map(_) => (TypeCategory::Map, Vec::new()),
            TypeInfo::Set(_) => (TypeCategory::Map, Vec::new()),
            TypeInfo::Tuple(_) => (TypeCategory::TupleStruct, Vec::new()),
            TypeInfo::Opaque(_) => (TypeCategory::Value, Vec::new()),
        };

        Ok(ReflectionMetadata {
            type_name: type_name.clone(),
            type_id,
            is_reflected: true,
            fields,
            type_category,
            type_info: Some(self.serialize_type_info(&type_info).await),
            last_updated: chrono::Utc::now().timestamp_micros() as u64,
        })
    }

    /// Serialize type info to JSON for storage
    #[cfg(feature = "bevy-reflection")]
    async fn serialize_type_info(&self, type_info: &TypeInfo) -> Value {
        match type_info {
            TypeInfo::Struct(struct_info) => json!({
                "type": "struct",
                "name": struct_info.type_path(),
                "field_count": struct_info.field_len(),
                "fields": struct_info.iter().map(|field| {
                    json!({
                        "name": field.name(),
                        "type": field.type_path(),
                    })
                }).collect::<Vec<_>>()
            }),
            TypeInfo::Enum(enum_info) => json!({
                "type": "enum",
                "name": enum_info.type_path(),
                "variant_count": enum_info.variant_len(),
            }),
            _ => json!({
                "type": "other",
                "name": type_info.type_path(),
            }),
        }
    }

    /// Find custom inspector for a type
    async fn find_inspector_for_type(&self, type_path: &str) -> Option<String> {
        let inspectors = self.custom_inspectors.read().await;
        for inspector in inspectors.values() {
            if inspector.supported_types().contains(&type_path.to_string()) {
                return Some(inspector.name().to_string());
            }
        }
        None
    }

    /// Inspect a component value using reflection
    pub async fn inspect_component(&self, component_type: &str, component_value: &ComponentValue) -> Result<ReflectionInspectionResult> {
        debug!("Inspecting component: {}", component_type);
        
        let metadata = self.get_or_create_metadata(component_type).await?;
        let mut field_values = HashMap::new();
        let mut errors = Vec::new();

        // Try custom inspector first
        if let Some(inspector_name) = &metadata.fields.get(0).and_then(|f| f.inspector_name.as_ref()) {
            let inspectors = self.custom_inspectors.read().await;
            if let Some(inspector) = inspectors.get(inspector_name.as_str()) {
                match inspector.inspect(component_value, component_type) {
                    Ok(inspected_value) => {
                        field_values.insert("root".to_string(), inspected_value);
                    }
                    Err(e) => {
                        errors.push(format!("Custom inspector error: {}", e));
                    }
                }
            }
        }

        // Fallback to generic inspection
        if field_values.is_empty() {
            match self.inspect_value_generic(component_value, component_type).await {
                Ok(inspected_value) => {
                    field_values.insert("root".to_string(), inspected_value);
                }
                Err(e) => {
                    errors.push(format!("Generic inspection error: {}", e));
                }
            }
        }

        Ok(ReflectionInspectionResult {
            component_type: component_type.to_string(),
            metadata,
            field_values,
            errors,
            timestamp: chrono::Utc::now().timestamp_micros() as u64,
        })
    }

    /// Generic value inspection for fallback
    pub async fn inspect_value_generic(&self, value: &ComponentValue, type_name: &str) -> Result<InspectedValue> {
        let inspected = match value {
            Value::Object(obj) => {
                let mut children = Vec::new();
                for (key, val) in obj {
                    let child = Box::pin(self.inspect_value_generic(val, "unknown")).await?;
                    children.push(InspectedValue {
                        name: key.clone(),
                        raw_value: val.clone(),
                        type_info: self.infer_type_from_value(val),
                        display_value: self.format_display_value(val),
                        inspectable: self.is_value_inspectable(val),
                        children: None,
                    });
                }
                
                InspectedValue {
                    name: "root".to_string(),
                    raw_value: value.clone(),
                    type_info: type_name.to_string(),
                    display_value: format!("{} {{ {} fields }}", type_name, obj.len()),
                    inspectable: true,
                    children: Some(children),
                }
            }
            Value::Array(arr) => {
                let mut children = Vec::new();
                for (i, val) in arr.iter().enumerate() {
                    let child = Box::pin(self.inspect_value_generic(val, "unknown")).await?;
                    children.push(InspectedValue {
                        name: format!("[{}]", i),
                        raw_value: val.clone(),
                        type_info: self.infer_type_from_value(val),
                        display_value: self.format_display_value(val),
                        inspectable: self.is_value_inspectable(val),
                        children: None,
                    });
                }
                
                InspectedValue {
                    name: "root".to_string(),
                    raw_value: value.clone(),
                    type_info: format!("Vec<T> (length: {})", arr.len()),
                    display_value: format!("Array[{}]", arr.len()),
                    inspectable: true,
                    children: Some(children),
                }
            }
            _ => {
                InspectedValue {
                    name: "root".to_string(),
                    raw_value: value.clone(),
                    type_info: self.infer_type_from_value(value),
                    display_value: self.format_display_value(value),
                    inspectable: false,
                    children: None,
                }
            }
        };

        Ok(inspected)
    }

    /// Infer type from JSON value
    fn infer_type_from_value(&self, value: &Value) -> String {
        match value {
            Value::Null => "Option<T> (None)".to_string(),
            Value::Bool(_) => "bool".to_string(),
            Value::Number(n) => {
                if n.is_i64() {
                    "i64".to_string()
                } else if n.is_u64() {
                    "u64".to_string()
                } else {
                    "f64".to_string()
                }
            }
            Value::String(_) => "String".to_string(),
            Value::Array(_) => "Vec<T>".to_string(),
            Value::Object(_) => "Struct".to_string(),
        }
    }

    /// Format display value for UI
    fn format_display_value(&self, value: &Value) -> String {
        match value {
            Value::Null => "None".to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Number(n) => n.to_string(),
            Value::String(s) => format!("\"{}\"", s),
            Value::Array(arr) => format!("[{} items]", arr.len()),
            Value::Object(obj) => format!("{{ {} fields }}", obj.len()),
        }
    }

    /// Check if value can be further inspected
    fn is_value_inspectable(&self, value: &Value) -> bool {
        matches!(value, Value::Object(_) | Value::Array(_))
    }

    /// Get or create metadata for a component type
    async fn get_or_create_metadata(&self, component_type: &str) -> Result<ReflectionMetadata> {
        let mut cache = self.reflection_cache.write().await;
        
        if let Some(metadata) = cache.get(component_type) {
            return Ok(metadata.clone());
        }

        // Create basic metadata for unknown types
        let metadata = ReflectionMetadata {
            type_name: component_type.to_string(),
            type_id: TypeId::of::<()>(), // Placeholder
            is_reflected: false,
            fields: Vec::new(),
            type_category: TypeCategory::Value,
            type_info: None,
            last_updated: chrono::Utc::now().timestamp_micros() as u64,
        };

        cache.insert(component_type.to_string(), metadata.clone());
        Ok(metadata)
    }

    /// The fields of a component value, keyed by name — the unit `diff_components` compares.
    ///
    /// A component's fields *are* its JSON object keys; a component that is not an object (a newtype
    /// over a scalar, say) has exactly one implicit field. That is a definition, not a fallback: both
    /// shapes produce one map with the same meaning, and the caller has no second code path.
    async fn field_map(
        &self,
        value: &ComponentValue,
    ) -> Result<HashMap<String, InspectedValue>> {
        let mut fields = HashMap::new();
        match value {
            Value::Object(obj) => {
                for (name, field_value) in obj {
                    fields.insert(
                        name.clone(),
                        self.inspect_value_generic(field_value, name).await?,
                    );
                }
            }
            other => {
                fields.insert(
                    "root".to_string(),
                    self.inspect_value_generic(other, "root").await?,
                );
            }
        }
        Ok(fields)
    }

    /// Perform reflection-based diffing of component states
    pub async fn diff_components(
        &self,
        component_type: &str,
        old_value: &ComponentValue,
        new_value: &ComponentValue,
    ) -> Result<ReflectionDiffResult> {
        debug!("Diffing components of type: {}", component_type);

        // **Diff by the component's actual fields.**
        //
        // This used to read `inspect_component(..).field_values`, which inserts exactly one entry —
        // `"root"` — holding the whole component (see `inspect_component`). Both sides therefore always
        // had the same single key, so the `(None, Some(_))` / `(Some(_), None)` arms below were
        // unreachable and `added_fields`/`removed_fields` could never be anything but zero. A diff that
        // structurally cannot report an added field is worse than no diff, because the summary looks
        // authoritative.
        let old_fields = self.field_map(old_value).await?;
        let new_fields = self.field_map(new_value).await?;

        let mut field_diffs = HashMap::new();
        let mut summary = DiffSummary {
            total_fields: 0,
            changed_fields: 0,
            added_fields: 0,
            removed_fields: 0,
            critical_changes: 0,
            major_changes: 0,
            minor_changes: 0,
            trivial_changes: 0,
        };

        // Compare field values
        let all_field_names: std::collections::HashSet<String> = old_fields
            .keys()
            .chain(new_fields.keys())
            .cloned()
            .collect();

        summary.total_fields = all_field_names.len();

        for field_name in all_field_names {
            let old_field = old_fields.get(&field_name);
            let new_field = new_fields.get(&field_name);

            let (change_type, severity) = match (old_field, new_field) {
                (None, Some(_)) => (ChangeType::Added, ChangeSeverity::Minor),
                (Some(_), None) => (ChangeType::Removed, ChangeSeverity::Major),
                (Some(old), Some(new)) => {
                    if old.type_info != new.type_info {
                        (ChangeType::TypeChanged, ChangeSeverity::Critical)
                    } else if old.raw_value != new.raw_value {
                        let severity = self.assess_change_severity(&old.raw_value, &new.raw_value);
                        (ChangeType::Modified, severity)
                    } else {
                        continue; // No change
                    }
                }
                (None, None) => continue, // Should not happen
            };

            // Update summary counters
            match change_type {
                ChangeType::Added => summary.added_fields += 1,
                ChangeType::Removed => summary.removed_fields += 1,
                _ => {}
            }

            match severity {
                ChangeSeverity::Trivial => summary.trivial_changes += 1,
                ChangeSeverity::Minor => summary.minor_changes += 1,
                ChangeSeverity::Major => summary.major_changes += 1,
                ChangeSeverity::Critical => summary.critical_changes += 1,
            }

            summary.changed_fields += 1;

            let field_diff = FieldDiff {
                field_name: field_name.clone(),
                field_type: old_field
                    .or(new_field)
                    .map(|f| f.type_info.clone())
                    .unwrap_or_else(|| "unknown".to_string()),
                old_value: old_field.cloned(),
                new_value: new_field.cloned(),
                change_type,
                significance: severity,
            };

            field_diffs.insert(field_name, field_diff);
        }

        let change_descriptions = self.generate_change_descriptions(&field_diffs);

        Ok(ReflectionDiffResult {
            type_name: component_type.to_string(),
            field_diffs,
            summary,
            change_descriptions,
        })
    }

    /// Assess the severity of a value change
    pub fn assess_change_severity(&self, old_value: &Value, new_value: &Value) -> ChangeSeverity {
        match (old_value, new_value) {
            (Value::Number(old_num), Value::Number(new_num)) => {
                // Assess numerical changes
                if let (Some(old_f), Some(new_f)) = (old_num.as_f64(), new_num.as_f64()) {
                    let diff = (old_f - new_f).abs();
                    let relative_change = diff / old_f.abs().max(1.0);
                    
                    if relative_change < 0.001 {
                        ChangeSeverity::Trivial
                    } else if relative_change < 0.1 {
                        ChangeSeverity::Minor
                    } else {
                        ChangeSeverity::Major
                    }
                } else {
                    ChangeSeverity::Minor
                }
            }
            (Value::String(old_s), Value::String(new_s)) => {
                if old_s.len() != new_s.len() || old_s != new_s {
                    ChangeSeverity::Minor
                } else {
                    ChangeSeverity::Trivial
                }
            }
            (Value::Bool(_), Value::Bool(_)) => ChangeSeverity::Minor,
            (Value::Array(old_arr), Value::Array(new_arr)) => {
                if old_arr.len() != new_arr.len() {
                    ChangeSeverity::Major
                } else {
                    ChangeSeverity::Minor
                }
            }
            (Value::Object(old_obj), Value::Object(new_obj)) => {
                if old_obj.len() != new_obj.len() {
                    ChangeSeverity::Major
                } else {
                    ChangeSeverity::Minor
                }
            }
            _ => ChangeSeverity::Major, // Type mismatch
        }
    }

    /// Generate human-readable change descriptions
    fn generate_change_descriptions(&self, field_diffs: &HashMap<String, FieldDiff>) -> Vec<String> {
        let mut descriptions = Vec::new();

        for (field_name, diff) in field_diffs {
            let description = match &diff.change_type {
                ChangeType::Added => {
                    format!("Added field '{}' with value: {}", 
                        field_name, 
                        diff.new_value.as_ref()
                            .map(|v| v.display_value.clone())
                            .unwrap_or_else(|| "unknown".to_string())
                    )
                }
                ChangeType::Removed => {
                    format!("Removed field '{}' (was: {})", 
                        field_name, 
                        diff.old_value.as_ref()
                            .map(|v| v.display_value.clone())
                            .unwrap_or_else(|| "unknown".to_string())
                    )
                }
                ChangeType::Modified => {
                    format!("Modified field '{}': {} -> {}", 
                        field_name,
                        diff.old_value.as_ref()
                            .map(|v| v.display_value.clone())
                            .unwrap_or_else(|| "unknown".to_string()),
                        diff.new_value.as_ref()
                            .map(|v| v.display_value.clone())
                            .unwrap_or_else(|| "unknown".to_string())
                    )
                }
                ChangeType::TypeChanged => {
                    format!("Type changed for field '{}': {} -> {}", 
                        field_name,
                        diff.old_value.as_ref()
                            .map(|v| v.type_info.clone())
                            .unwrap_or_else(|| "unknown".to_string()),
                        diff.new_value.as_ref()
                            .map(|v| v.type_info.clone())
                            .unwrap_or_else(|| "unknown".to_string())
                    )
                }
            };
            descriptions.push(description);
        }

        descriptions
    }

    /// Get cached reflection metadata for all known types
    pub async fn get_all_reflection_metadata(&self) -> HashMap<String, ReflectionMetadata> {
        self.reflection_cache.read().await.clone()
    }

    /// Clear the reflection cache
    pub async fn clear_cache(&self) {
        let mut cache = self.reflection_cache.write().await;
        cache.clear();
        info!("Cleared reflection cache");
    }

    /// Get statistics about the reflection system
    pub async fn get_reflection_stats(&self) -> Value {
        let cache = self.reflection_cache.read().await;
        let inspectors = self.custom_inspectors.read().await;

        json!({
            "cached_types": cache.len(),
            "custom_inspectors": inspectors.len(),
            "reflected_types": cache.values().filter(|m| m.is_reflected).count(),
            "total_fields": cache.values().map(|m| m.fields.len()).sum::<usize>(),
        })
    }
}

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

/// Transform component inspector with 3D math understanding
pub struct TransformInspector;

impl CustomInspector for TransformInspector {
    fn inspect(&self, value: &ComponentValue, _type_name: &str) -> Result<InspectedValue> {
        if let Value::Object(obj) = value {
            let mut children = Vec::new();

            // Extract translation
            if let Some(translation) = obj.get("translation") {
                children.push(InspectedValue {
                    name: "translation".to_string(),
                    raw_value: translation.clone(),
                    type_info: "Vec3".to_string(),
                    display_value: self.format_vec3(translation),
                    inspectable: true,
                    children: None,
                });
            }

            // Extract rotation
            if let Some(rotation) = obj.get("rotation") {
                children.push(InspectedValue {
                    name: "rotation".to_string(),
                    raw_value: rotation.clone(),
                    type_info: "Quat".to_string(),
                    display_value: self.format_quat(rotation),
                    inspectable: true,
                    children: None,
                });
            }

            // Extract scale
            if let Some(scale) = obj.get("scale") {
                children.push(InspectedValue {
                    name: "scale".to_string(),
                    raw_value: scale.clone(),
                    type_info: "Vec3".to_string(),
                    display_value: self.format_vec3(scale),
                    inspectable: true,
                    children: None,
                });
            }

            Ok(InspectedValue {
                name: "Transform".to_string(),
                raw_value: value.clone(),
                type_info: "bevy_transform::components::transform::Transform".to_string(),
                display_value: "Transform Component".to_string(),
                inspectable: true,
                children: Some(children),
            })
        } else {
            Err(Error::DebugError("Transform component should be an object".to_string()))
        }
    }

    fn name(&self) -> &str {
        "TransformInspector"
    }

    fn supported_types(&self) -> Vec<String> {
        vec!["bevy_transform::components::transform::Transform".to_string()]
    }
}

impl TransformInspector {
    fn format_vec3(&self, value: &Value) -> String {
        if let Value::Object(obj) = value {
            let x = obj.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0);
            let y = obj.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0);
            let z = obj.get("z").and_then(|v| v.as_f64()).unwrap_or(0.0);
            format!("({:.2}, {:.2}, {:.2})", x, y, z)
        } else {
            "Invalid Vec3".to_string()
        }
    }

    fn format_quat(&self, value: &Value) -> String {
        if let Value::Object(obj) = value {
            let x = obj.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0);
            let y = obj.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0);
            let z = obj.get("z").and_then(|v| v.as_f64()).unwrap_or(0.0);
            let w = obj.get("w").and_then(|v| v.as_f64()).unwrap_or(1.0);
            
            // Convert to euler angles for better readability
            let euler = self.quat_to_euler(x, y, z, w);
            format!("Quat(x:{:.2}, y:{:.2}, z:{:.2}, w:{:.2}) ≈ ({:.1}°, {:.1}°, {:.1}°)", 
                x, y, z, w, euler.0, euler.1, euler.2)
        } else {
            "Invalid Quat".to_string()
        }
    }

    fn quat_to_euler(&self, x: f64, y: f64, z: f64, w: f64) -> (f64, f64, f64) {
        // Simple quaternion to euler conversion (yaw, pitch, roll)
        let yaw = (2.0 * (w * y + z * x)).atan2(1.0 - 2.0 * (y * y + z * z));
        let pitch = (2.0 * (w * z - x * y)).asin();
        let roll = (2.0 * (w * x + y * z)).atan2(1.0 - 2.0 * (x * x + z * z));
        
        (yaw.to_degrees(), pitch.to_degrees(), roll.to_degrees())
    }
}

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

    #[tokio::test]
    async fn test_reflection_inspector_creation() {
        let inspector = BevyReflectionInspector::new();
        let stats = inspector.get_reflection_stats().await;
        
        assert!(stats.get("cached_types").is_some());
        assert!(stats.get("custom_inspectors").is_some());
    }

    #[tokio::test]
    async fn test_generic_inspection() {
        let inspector = BevyReflectionInspector::new();
        
        let test_value = json!({
            "x": 1.0,
            "y": 2.0,
            "z": 3.0
        });
        
        let result = inspector.inspect_value_generic(&test_value, "TestType").await.unwrap();
        assert_eq!(result.type_info, "TestType");
        assert!(result.inspectable);
        assert!(result.children.is_some());
    }

    #[tokio::test]
    async fn test_transform_inspector() {
        let inspector = TransformInspector;
        
        let transform_value = json!({
            "translation": {"x": 1.0, "y": 2.0, "z": 3.0},
            "rotation": {"x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0},
            "scale": {"x": 1.0, "y": 1.0, "z": 1.0}
        });
        
        let result = inspector.inspect(&transform_value, "Transform").unwrap();
        assert_eq!(result.name, "Transform");
        assert!(result.children.is_some());
        assert_eq!(result.children.as_ref().unwrap().len(), 3);
    }

    #[tokio::test]
    async fn test_diff_components() {
        let inspector = BevyReflectionInspector::new();
        
        let old_value = json!({"x": 1.0, "y": 2.0});
        let new_value = json!({"x": 1.5, "y": 2.0, "z": 3.0});
        
        let diff_result = inspector.diff_components("TestComponent", &old_value, &new_value).await.unwrap();
        
        assert_eq!(diff_result.type_name, "TestComponent");
        assert!(diff_result.summary.changed_fields > 0);
        assert!(diff_result.summary.added_fields > 0);
    }

    #[test]
    fn test_change_severity_assessment() {
        let inspector = BevyReflectionInspector::new();
        
        // Test numerical change severity
        let old_num = json!(10.0);
        let new_num_trivial = json!(10.001);
        let new_num_minor = json!(10.5);
        let new_num_major = json!(15.0);
        
        assert!(matches!(inspector.assess_change_severity(&old_num, &new_num_trivial), ChangeSeverity::Trivial));
        assert!(matches!(inspector.assess_change_severity(&old_num, &new_num_minor), ChangeSeverity::Minor));
        assert!(matches!(inspector.assess_change_severity(&old_num, &new_num_major), ChangeSeverity::Major));
    }
}