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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::fs;
use tracing::{debug, error, info, warn};

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

/// A checkpoint represents a saved state that can be restored later
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Checkpoint {
    /// Unique identifier for this checkpoint
    pub id: String,
    /// When this checkpoint was created
    pub timestamp: u64,
    /// Human-readable name for this checkpoint
    pub name: String,
    /// Description of what operation this checkpoint represents
    pub description: String,
    /// The operation type being checkpointed
    pub operation_type: String,
    /// Component that created this checkpoint
    pub component: String,
    /// Serialized state data
    pub state_data: serde_json::Value,
    /// Metadata about the checkpoint
    pub metadata: HashMap<String, String>,
    /// Whether this checkpoint can be automatically restored
    pub auto_restorable: bool,
    /// Expiration time (if any)
    pub expires_at: Option<u64>,
}

impl Checkpoint {
    pub fn new(
        name: &str,
        description: &str,
        operation_type: &str,
        component: &str,
        state_data: serde_json::Value,
    ) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            id: uuid::Uuid::new_v4().to_string(),
            timestamp,
            name: name.to_string(),
            description: description.to_string(),
            operation_type: operation_type.to_string(),
            component: component.to_string(),
            state_data,
            metadata: HashMap::new(),
            auto_restorable: true,
            expires_at: None,
        }
    }

    /// Add metadata to the checkpoint
    pub fn with_metadata(mut self, key: &str, value: &str) -> Self {
        self.metadata.insert(key.to_string(), value.to_string());
        self
    }

    /// Set expiration time (in seconds from now)
    pub fn with_expiration(mut self, seconds_from_now: u64) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        self.expires_at = Some(now + seconds_from_now);
        self
    }

    /// Set whether this checkpoint can be automatically restored
    pub fn set_auto_restorable(mut self, auto_restorable: bool) -> Self {
        self.auto_restorable = auto_restorable;
        self
    }

    /// Check if this checkpoint has expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            now > expires_at
        } else {
            false
        }
    }

    /// Get the age of this checkpoint in seconds
    pub fn age_seconds(&self) -> u64 {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now.saturating_sub(self.timestamp)
    }
}

/// Configuration for the checkpoint manager
#[derive(Debug, Clone)]
pub struct CheckpointConfig {
    /// Maximum number of checkpoints to keep in memory
    pub max_checkpoints: usize,
    /// Maximum age of checkpoints in seconds before auto-cleanup
    pub max_age_seconds: u64,
    /// Whether to persist checkpoints to disk
    pub persist_to_disk: bool,
    /// Directory for storing checkpoint files
    pub storage_directory: String,
    /// How often to run cleanup (in seconds)
    pub cleanup_interval_seconds: u64,
    /// Maximum size of state data in bytes
    pub max_state_size_bytes: usize,
}

impl Default for CheckpointConfig {
    fn default() -> Self {
        Self {
            max_checkpoints: 100,
            max_age_seconds: 24 * 60 * 60, // 24 hours
            persist_to_disk: true,
            storage_directory: "./checkpoints".to_string(),
            cleanup_interval_seconds: 60 * 60,      // 1 hour
            max_state_size_bytes: 10 * 1024 * 1024, // 10MB
        }
    }
}

/// Manager for creating and restoring checkpoints
#[derive(Debug)]
pub struct CheckpointManager {
    config: CheckpointConfig,
    checkpoints: std::sync::Arc<std::sync::RwLock<HashMap<String, Checkpoint>>>,
    cleanup_handle: Option<tokio::task::JoinHandle<()>>,
    shutdown_tx: Option<tokio::sync::mpsc::Sender<()>>,
}

impl CheckpointManager {
    pub fn new(config: CheckpointConfig) -> Self {
        Self {
            config,
            checkpoints: std::sync::Arc::new(std::sync::RwLock::new(HashMap::new())),
            cleanup_handle: None,
            shutdown_tx: None,
        }
    }

    /// Start the checkpoint manager with automatic cleanup
    pub async fn start(&mut self) -> Result<()> {
        // Create storage directory if it doesn't exist
        if self.config.persist_to_disk {
            fs::create_dir_all(&self.config.storage_directory).await?;
            info!(
                "Checkpoint storage directory: {}",
                self.config.storage_directory
            );
        }

        // Load existing checkpoints from disk
        if self.config.persist_to_disk {
            if let Err(e) = self.load_checkpoints_from_disk().await {
                error!("Failed to load checkpoints from disk: {}", e);
            }
        }

        // Start cleanup task
        let (shutdown_tx, mut shutdown_rx) = tokio::sync::mpsc::channel(1);
        self.shutdown_tx = Some(shutdown_tx);

        let checkpoints = self.checkpoints.clone();
        let config = self.config.clone();

        let handle = tokio::spawn(async move {
            let mut interval =
                tokio::time::interval(Duration::from_secs(config.cleanup_interval_seconds));

            loop {
                tokio::select! {
                    _ = interval.tick() => {
                        Self::cleanup_expired_checkpoints(&checkpoints, &config).await;
                    }
                    _ = shutdown_rx.recv() => {
                        info!("Checkpoint manager cleanup shutting down");
                        break;
                    }
                }
            }
        });

        self.cleanup_handle = Some(handle);
        info!("Checkpoint manager started");
        Ok(())
    }

    /// Create a new checkpoint
    pub async fn create_checkpoint(&self, checkpoint: Checkpoint) -> Result<String> {
        // Validate state size
        let state_size = serde_json::to_vec(&checkpoint.state_data)?.len();
        if state_size > self.config.max_state_size_bytes {
            return Err(Error::Validation(format!(
                "Checkpoint state too large: {} bytes (max: {})",
                state_size, self.config.max_state_size_bytes
            )));
        }

        let checkpoint_id = checkpoint.id.clone();

        // Store in memory
        {
            let mut checkpoints = self.checkpoints.write().unwrap();

            // Remove oldest checkpoints if we're at the limit
            while checkpoints.len() >= self.config.max_checkpoints {
                if let Some((oldest_id, _)) = checkpoints
                    .iter()
                    .min_by_key(|(_, cp)| cp.timestamp)
                    .map(|(id, cp)| (id.clone(), cp.clone()))
                {
                    checkpoints.remove(&oldest_id);
                    warn!("Removed oldest checkpoint to make room: {}", oldest_id);
                }
            }

            checkpoints.insert(checkpoint_id.clone(), checkpoint.clone());
        }

        // Persist to disk if configured
        if self.config.persist_to_disk {
            if let Err(e) = self.save_checkpoint_to_disk(&checkpoint).await {
                error!("Failed to save checkpoint to disk: {}", e);
            }
        }

        info!(
            "Created checkpoint: {} ({})",
            checkpoint.name, checkpoint_id
        );
        Ok(checkpoint_id)
    }

    /// Restore a checkpoint by ID
    pub async fn restore_checkpoint(&self, checkpoint_id: &str) -> Result<Checkpoint> {
        let checkpoint = {
            let checkpoints = self.checkpoints.read().unwrap();
            checkpoints.get(checkpoint_id).cloned()
        };

        match checkpoint {
            Some(cp) => {
                if cp.is_expired() {
                    return Err(Error::Validation(format!(
                        "Checkpoint {checkpoint_id} has expired"
                    )));
                }

                info!("Restoring checkpoint: {} ({})", cp.name, checkpoint_id);
                Ok(cp)
            }
            None => {
                // Try loading from disk if not in memory
                if self.config.persist_to_disk {
                    if let Ok(cp) = self.load_checkpoint_from_disk(checkpoint_id).await {
                        if !cp.is_expired() {
                            // Add back to memory
                            let mut checkpoints = self.checkpoints.write().unwrap();
                            checkpoints.insert(checkpoint_id.to_string(), cp.clone());

                            info!(
                                "Restored checkpoint from disk: {} ({})",
                                cp.name, checkpoint_id
                            );
                            return Ok(cp);
                        }
                    }
                }

                Err(Error::Validation(format!(
                    "Checkpoint not found: {checkpoint_id}"
                )))
            }
        }
    }

    /// Get all available checkpoints
    pub async fn list_checkpoints(&self) -> Vec<Checkpoint> {
        let checkpoints = self.checkpoints.read().unwrap();
        checkpoints.values().cloned().collect()
    }

    /// Get checkpoints by operation type
    pub async fn list_checkpoints_by_operation(&self, operation_type: &str) -> Vec<Checkpoint> {
        let checkpoints = self.checkpoints.read().unwrap();
        checkpoints
            .values()
            .filter(|cp| cp.operation_type == operation_type)
            .cloned()
            .collect()
    }

    /// Get checkpoints by component
    pub async fn list_checkpoints_by_component(&self, component: &str) -> Vec<Checkpoint> {
        let checkpoints = self.checkpoints.read().unwrap();
        checkpoints
            .values()
            .filter(|cp| cp.component == component)
            .cloned()
            .collect()
    }

    /// Delete a checkpoint
    pub async fn delete_checkpoint(&self, checkpoint_id: &str) -> Result<()> {
        // Remove from memory
        let removed = {
            let mut checkpoints = self.checkpoints.write().unwrap();
            checkpoints.remove(checkpoint_id).is_some()
        };

        // Remove from disk if configured
        if self.config.persist_to_disk {
            let file_path = self.get_checkpoint_file_path(checkpoint_id);
            if fs::metadata(&file_path).await.is_ok() {
                fs::remove_file(&file_path).await?;
            }
        }

        if removed {
            info!("Deleted checkpoint: {}", checkpoint_id);
            Ok(())
        } else {
            Err(Error::Validation(format!(
                "Checkpoint not found: {checkpoint_id}"
            )))
        }
    }

    /// Get checkpoint statistics
    pub async fn get_statistics(&self) -> CheckpointStats {
        let checkpoints = self.checkpoints.read().unwrap();

        let mut stats = CheckpointStats {
            total_count: checkpoints.len(),
            by_operation_type: HashMap::new(),
            by_component: HashMap::new(),
            expired_count: 0,
            auto_restorable_count: 0,
            oldest_timestamp: None,
            newest_timestamp: None,
        };

        for checkpoint in checkpoints.values() {
            // Count by operation type
            *stats
                .by_operation_type
                .entry(checkpoint.operation_type.clone())
                .or_insert(0) += 1;

            // Count by component
            *stats
                .by_component
                .entry(checkpoint.component.clone())
                .or_insert(0) += 1;

            // Count expired and auto-restorable
            if checkpoint.is_expired() {
                stats.expired_count += 1;
            }
            if checkpoint.auto_restorable {
                stats.auto_restorable_count += 1;
            }

            // Track timestamps
            if stats.oldest_timestamp.is_none()
                || Some(checkpoint.timestamp) < stats.oldest_timestamp
            {
                stats.oldest_timestamp = Some(checkpoint.timestamp);
            }
            if stats.newest_timestamp.is_none()
                || Some(checkpoint.timestamp) > stats.newest_timestamp
            {
                stats.newest_timestamp = Some(checkpoint.timestamp);
            }
        }

        stats
    }

    async fn cleanup_expired_checkpoints(
        checkpoints: &std::sync::Arc<std::sync::RwLock<HashMap<String, Checkpoint>>>,
        config: &CheckpointConfig,
    ) {
        let max_age = config.max_age_seconds;
        let mut to_remove = Vec::new();

        {
            let checkpoints_guard = checkpoints.read().unwrap();
            for (id, checkpoint) in checkpoints_guard.iter() {
                if checkpoint.is_expired() || checkpoint.age_seconds() > max_age {
                    to_remove.push(id.clone());
                }
            }
        }

        if !to_remove.is_empty() {
            let mut checkpoints_guard = checkpoints.write().unwrap();
            for id in &to_remove {
                checkpoints_guard.remove(id);
            }
            info!("Cleaned up {} expired checkpoints", to_remove.len());
        }
    }

    async fn save_checkpoint_to_disk(&self, checkpoint: &Checkpoint) -> Result<()> {
        let file_path = self.get_checkpoint_file_path(&checkpoint.id);
        let data = serde_json::to_string_pretty(checkpoint)?;
        fs::write(&file_path, data).await?;
        debug!("Saved checkpoint to disk: {}", file_path.display());
        Ok(())
    }

    async fn load_checkpoint_from_disk(&self, checkpoint_id: &str) -> Result<Checkpoint> {
        let file_path = self.get_checkpoint_file_path(checkpoint_id);
        let data = fs::read_to_string(&file_path).await?;
        let checkpoint: Checkpoint = serde_json::from_str(&data)?;
        Ok(checkpoint)
    }

    async fn load_checkpoints_from_disk(&self) -> Result<()> {
        let storage_dir = Path::new(&self.config.storage_directory);
        if !storage_dir.exists() {
            return Ok(());
        }

        let mut entries = fs::read_dir(storage_dir).await?;
        let mut loaded_count = 0;

        while let Some(entry) = entries.next_entry().await? {
            if let Some(file_name) = entry.file_name().to_str() {
                if file_name.ends_with(".json") {
                    let checkpoint_id = file_name.trim_end_matches(".json");

                    match self.load_checkpoint_from_disk(checkpoint_id).await {
                        Ok(checkpoint) => {
                            if !checkpoint.is_expired() {
                                let mut checkpoints = self.checkpoints.write().unwrap();
                                checkpoints.insert(checkpoint.id.clone(), checkpoint);
                                loaded_count += 1;
                            } else {
                                // Remove expired checkpoint file
                                let _ = fs::remove_file(entry.path()).await;
                            }
                        }
                        Err(e) => {
                            warn!("Failed to load checkpoint {}: {}", checkpoint_id, e);
                        }
                    }
                }
            }
        }

        info!("Loaded {} checkpoints from disk", loaded_count);
        Ok(())
    }

    fn get_checkpoint_file_path(&self, checkpoint_id: &str) -> std::path::PathBuf {
        // Sanitize checkpoint ID to prevent path traversal
        let sanitized_id = checkpoint_id
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_')
            .collect::<String>();

        if sanitized_id.is_empty() {
            panic!("Invalid checkpoint ID: {checkpoint_id}");
        }

        std::path::Path::new(&self.config.storage_directory).join(format!("{sanitized_id}.json"))
    }

    /// Shutdown the checkpoint manager
    pub async fn shutdown(&mut self) -> Result<()> {
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(()).await;
        }

        if let Some(handle) = self.cleanup_handle.take() {
            handle.abort();
        }

        // Save all checkpoints to disk if configured
        if self.config.persist_to_disk {
            let checkpoints = self.checkpoints.read().unwrap();
            for checkpoint in checkpoints.values() {
                if let Err(e) = self.save_checkpoint_to_disk(checkpoint).await {
                    error!("Failed to save checkpoint during shutdown: {}", e);
                }
            }
        }

        info!("Checkpoint manager shutdown complete");
        Ok(())
    }
}

/// Statistics about checkpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointStats {
    pub total_count: usize,
    pub by_operation_type: HashMap<String, usize>,
    pub by_component: HashMap<String, usize>,
    pub expired_count: usize,
    pub auto_restorable_count: usize,
    pub oldest_timestamp: Option<u64>,
    pub newest_timestamp: Option<u64>,
}

impl Drop for CheckpointManager {
    fn drop(&mut self) {
        if self.cleanup_handle.is_some() {
            warn!("CheckpointManager dropped without proper shutdown");
        }
    }
}