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
//! Actor-based replay system to replace `Arc<RwLock<T>>` heavy replay.rs
//!
//! This module implements the actor model for replay functionality, eliminating
//! the need for shared mutable state through locks.

use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, oneshot};
use tracing::{debug, error, info, warn};

use crate::server::brp_client::BrpClient;
use crate::server::error::{Error, Result};
use crate::server::playback_system::{DirectSync, PlaybackController};
use crate::server::recording_system::{RecordingBuffer, RecordingConfig, RecordingState};
use crate::server::timeline_branching::{
    BranchId, MergeStrategy, Modification, ModificationLayer, TimelineBranchManager,
};

/// Messages that can be sent to the replay actor
#[derive(Debug)]
pub enum ReplayMessage {
    StartRecording {
        config: RecordingConfig,
        respond_to: oneshot::Sender<Result<ReplayStatus>>,
    },
    StopRecording {
        respond_to: oneshot::Sender<Result<ReplayStatus>>,
    },
    GetStatus {
        respond_to: oneshot::Sender<ReplayStatus>,
    },
    AddMarker {
        name: String,
        respond_to: oneshot::Sender<Result<()>>,
    },
    SaveRecording {
        path: PathBuf,
        respond_to: oneshot::Sender<Result<()>>,
    },
    LoadRecording {
        path: PathBuf,
        respond_to: oneshot::Sender<Result<()>>,
    },
    GetStats {
        respond_to: oneshot::Sender<ReplayStats>,
    },
    // Playback controls
    Play {
        respond_to: oneshot::Sender<Result<()>>,
    },
    Pause {
        respond_to: oneshot::Sender<Result<()>>,
    },
    Seek {
        position: f64,
        respond_to: oneshot::Sender<Result<()>>,
    },
    Step {
        frames: u32,
        respond_to: oneshot::Sender<Result<()>>,
    },
    SetSpeed {
        speed: f64,
        respond_to: oneshot::Sender<Result<()>>,
    },
    GetPlaybackStatus {
        respond_to: oneshot::Sender<PlaybackStatus>,
    },
    // Branch management
    CreateBranch {
        name: String,
        description: Option<String>,
        respond_to: oneshot::Sender<Result<BranchId>>,
    },
    ListBranches {
        respond_to: oneshot::Sender<Vec<BranchInfo>>,
    },
    SwitchBranch {
        branch_id: BranchId,
        respond_to: oneshot::Sender<Result<()>>,
    },
    AddModification {
        modification: Modification,
        respond_to: oneshot::Sender<Result<()>>,
    },
    MergeBranch {
        source: BranchId,
        target: BranchId,
        strategy: MergeStrategy,
        respond_to: oneshot::Sender<Result<()>>,
    },
    CompareBranches {
        branch1: BranchId,
        branch2: BranchId,
        respond_to: oneshot::Sender<BranchComparison>,
    },
    DeleteBranch {
        branch_id: BranchId,
        respond_to: oneshot::Sender<Result<()>>,
    },
    GetBranchTree {
        respond_to: oneshot::Sender<BranchTree>,
    },
}

/// Current replay system status
#[derive(Debug, Clone)]
pub struct ReplayStatus {
    pub recording_active: bool,
    pub playback_active: bool,
    pub current_frame: u64,
    pub total_frames: u64,
    pub recording_duration: Duration,
    pub sample_rate: f64,
}

/// Playback-specific status
#[derive(Debug, Clone)]
pub struct PlaybackStatus {
    pub state: PlaybackState,
    pub current_position: f64,
    pub playback_speed: f64,
    pub total_duration: Duration,
    pub loop_enabled: bool,
}

#[derive(Debug, Clone)]
pub enum PlaybackState {
    Stopped,
    Playing,
    Paused,
    Seeking,
}

/// Statistics about recording/playback
#[derive(Debug, Clone)]
pub struct ReplayStats {
    pub total_recordings: usize,
    pub total_frames_recorded: u64,
    pub total_branches: usize,
    pub disk_usage_bytes: u64,
    pub memory_usage_bytes: u64,
    pub performance_metrics: PerformanceMetrics,
}

#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub avg_frame_processing_time: Duration,
    pub max_frame_processing_time: Duration,
    pub dropped_frames: u64,
    pub compression_ratio: f64,
}

/// Information about a timeline branch
#[derive(Debug, Clone)]
pub struct BranchInfo {
    pub id: BranchId,
    pub name: String,
    pub description: Option<String>,
    pub created_at: Instant,
    pub frame_count: u64,
    pub modifications: usize,
}

/// Result of comparing two branches
#[derive(Debug, Clone)]
pub struct BranchComparison {
    pub differences: Vec<BranchDifference>,
    pub similarity_score: f64,
    pub divergence_frame: Option<u64>,
}

#[derive(Debug, Clone)]
pub struct BranchDifference {
    pub frame: u64,
    pub difference_type: DifferenceType,
    pub description: String,
}

#[derive(Debug, Clone)]
pub enum DifferenceType {
    ComponentChange,
    EntityChange,
    SystemChange,
    ResourceChange,
}

/// Tree representation of timeline branches
#[derive(Debug, Clone)]
pub struct BranchTree {
    pub nodes: Vec<BranchNode>,
    pub edges: Vec<BranchEdge>,
}

#[derive(Debug, Clone)]
pub struct BranchNode {
    pub branch_id: BranchId,
    pub name: String,
    pub frame_count: u64,
}

#[derive(Debug, Clone)]
pub struct BranchEdge {
    pub parent: BranchId,
    pub child: BranchId,
    pub merge_point: Option<u64>,
}

/// Internal state of the replay actor
struct ReplayActorState {
    recording_state: RecordingState,
    playback_controller: PlaybackController,
    branch_manager: TimelineBranchManager,
    brp_client: Arc<BrpClient>, // Note: no longer wrapped in RwLock
    current_recording_config: Option<RecordingConfig>,
    playback_status: PlaybackStatus,
    stats: ReplayStats,
}

/// The replay actor that manages all replay functionality
pub struct ReplayActor {
    receiver: mpsc::UnboundedReceiver<ReplayMessage>,
    state: ReplayActorState,
}

impl ReplayActor {
    pub fn new(
        receiver: mpsc::UnboundedReceiver<ReplayMessage>,
        brp_client: Arc<BrpClient>,
    ) -> Self {
        let state = ReplayActorState {
            recording_state: RecordingState::new(RecordingConfig::default()),
            playback_controller: PlaybackController::new(Box::new(DirectSync)),
            branch_manager: TimelineBranchManager::new(),
            brp_client,
            current_recording_config: None,
            playback_status: PlaybackStatus {
                state: PlaybackState::Stopped,
                current_position: 0.0,
                playback_speed: 1.0,
                total_duration: Duration::ZERO,
                loop_enabled: false,
            },
            stats: ReplayStats {
                total_recordings: 0,
                total_frames_recorded: 0,
                total_branches: 0,
                disk_usage_bytes: 0,
                memory_usage_bytes: 0,
                performance_metrics: PerformanceMetrics {
                    avg_frame_processing_time: Duration::ZERO,
                    max_frame_processing_time: Duration::ZERO,
                    dropped_frames: 0,
                    compression_ratio: 1.0,
                },
            },
        };

        Self { receiver, state }
    }

    /// Run the actor's message processing loop
    pub async fn run(mut self) {
        info!("Replay actor started");

        while let Some(message) = self.receiver.recv().await {
            debug!("Processing replay message: {:?}", message);
            self.handle_message(message).await;
        }

        info!("Replay actor stopped");
    }

    async fn handle_message(&mut self, message: ReplayMessage) {
        match message {
            ReplayMessage::StartRecording { config, respond_to } => {
                let result = self.start_recording(config).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::StopRecording { respond_to } => {
                let result = self.stop_recording().await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::GetStatus { respond_to } => {
                let status = self.get_status();
                let _ = respond_to.send(status);
            }
            ReplayMessage::AddMarker { name, respond_to } => {
                let result = self.add_marker(name).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::SaveRecording { path, respond_to } => {
                let result = self.save_recording(path).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::LoadRecording { path, respond_to } => {
                let result = self.load_recording(path).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::GetStats { respond_to } => {
                let stats = self.get_stats();
                let _ = respond_to.send(stats);
            }
            ReplayMessage::Play { respond_to } => {
                let result = self.play().await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::Pause { respond_to } => {
                let result = self.pause().await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::Seek { position, respond_to } => {
                let result = self.seek(position).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::Step { frames, respond_to } => {
                let result = self.step(frames).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::SetSpeed { speed, respond_to } => {
                let result = self.set_speed(speed).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::GetPlaybackStatus { respond_to } => {
                let status = self.get_playback_status();
                let _ = respond_to.send(status);
            }
            ReplayMessage::CreateBranch { name, description, respond_to } => {
                let result = self.create_branch(name, description).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::ListBranches { respond_to } => {
                let branches = self.list_branches();
                let _ = respond_to.send(branches);
            }
            ReplayMessage::SwitchBranch { branch_id, respond_to } => {
                let result = self.switch_branch(branch_id).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::AddModification { modification, respond_to } => {
                let result = self.add_modification(modification).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::MergeBranch { source, target, strategy, respond_to } => {
                let result = self.merge_branch(source, target, strategy).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::CompareBranches { branch1, branch2, respond_to } => {
                let comparison = self.compare_branches(branch1, branch2);
                let _ = respond_to.send(comparison);
            }
            ReplayMessage::DeleteBranch { branch_id, respond_to } => {
                let result = self.delete_branch(branch_id).await;
                let _ = respond_to.send(result);
            }
            ReplayMessage::GetBranchTree { respond_to } => {
                let tree = self.get_branch_tree();
                let _ = respond_to.send(tree);
            }
        }
    }

    // Implementation methods (simplified for brevity)
    async fn start_recording(&mut self, config: RecordingConfig) -> Result<ReplayStatus> {
        if !self.state.brp_client.is_connected() {
            return Err(Error::Connection("BRP client not connected".to_string()));
        }

        self.state.current_recording_config = Some(config.clone());
        self.state.recording_state = RecordingState::new(config.clone());
        
        info!("Recording started with config: {:?}", config);
        Ok(self.get_status())
    }

    async fn stop_recording(&mut self) -> Result<ReplayStatus> {
        self.state.current_recording_config = None;
        info!("Recording stopped");
        Ok(self.get_status())
    }

    fn get_status(&self) -> ReplayStatus {
        ReplayStatus {
            recording_active: self.state.current_recording_config.is_some(),
            playback_active: matches!(self.state.playback_status.state, PlaybackState::Playing),
            current_frame: 0,
            total_frames: 0,
            recording_duration: Duration::ZERO,
            sample_rate: self.state.current_recording_config
                .as_ref()
                .map(|c| c.sample_rate as f64)
                .unwrap_or(60.0),
        }
    }

    async fn add_marker(&mut self, _name: String) -> Result<()> {
        // Implementation would add marker to recording
        Ok(())
    }

    async fn save_recording(&mut self, _path: PathBuf) -> Result<()> {
        // Implementation would save recording to disk
        Ok(())
    }

    async fn load_recording(&mut self, _path: PathBuf) -> Result<()> {
        // Implementation would load recording from disk
        Ok(())
    }

    fn get_stats(&self) -> ReplayStats {
        self.state.stats.clone()
    }

    async fn play(&mut self) -> Result<()> {
        self.state.playback_status.state = PlaybackState::Playing;
        Ok(())
    }

    async fn pause(&mut self) -> Result<()> {
        self.state.playback_status.state = PlaybackState::Paused;
        Ok(())
    }

    async fn seek(&mut self, position: f64) -> Result<()> {
        self.state.playback_status.current_position = position;
        Ok(())
    }

    async fn step(&mut self, _frames: u32) -> Result<()> {
        // Implementation would step through frames
        Ok(())
    }

    async fn set_speed(&mut self, speed: f64) -> Result<()> {
        self.state.playback_status.playback_speed = speed;
        Ok(())
    }

    fn get_playback_status(&self) -> PlaybackStatus {
        self.state.playback_status.clone()
    }

    async fn create_branch(&mut self, name: String, description: Option<String>) -> Result<BranchId> {
        // Implementation would create new branch
        Ok(BranchId::new())
    }

    fn list_branches(&self) -> Vec<BranchInfo> {
        // Implementation would return list of branches
        Vec::new()
    }

    async fn switch_branch(&mut self, _branch_id: BranchId) -> Result<()> {
        // Implementation would switch to different branch
        Ok(())
    }

    async fn add_modification(&mut self, _modification: Modification) -> Result<()> {
        // Implementation would add modification to current branch
        Ok(())
    }

    async fn merge_branch(&mut self, _source: BranchId, _target: BranchId, _strategy: MergeStrategy) -> Result<()> {
        // Implementation would merge branches
        Ok(())
    }

    fn compare_branches(&self, _branch1: BranchId, _branch2: BranchId) -> BranchComparison {
        // Implementation would compare branches
        BranchComparison {
            differences: Vec::new(),
            similarity_score: 1.0,
            divergence_frame: None,
        }
    }

    async fn delete_branch(&mut self, _branch_id: BranchId) -> Result<()> {
        // Implementation would delete branch
        Ok(())
    }

    fn get_branch_tree(&self) -> BranchTree {
        // Implementation would return branch tree structure
        BranchTree {
            nodes: Vec::new(),
            edges: Vec::new(),
        }
    }
}

/// Handle to communicate with the replay actor
#[derive(Clone)]
pub struct ReplayActorHandle {
    sender: mpsc::UnboundedSender<ReplayMessage>,
}

impl ReplayActorHandle {
    pub fn new(sender: mpsc::UnboundedSender<ReplayMessage>) -> Self {
        Self { sender }
    }

    pub async fn start_recording(&self, config: RecordingConfig) -> Result<ReplayStatus> {
        let (respond_to, response) = oneshot::channel();
        let message = ReplayMessage::StartRecording { config, respond_to };
        
        self.sender.send(message).map_err(|_| Error::Internal("Replay actor disconnected".to_string()))?;
        response.await.map_err(|_| Error::Internal("Response channel closed".to_string()))?
    }

    pub async fn stop_recording(&self) -> Result<ReplayStatus> {
        let (respond_to, response) = oneshot::channel();
        let message = ReplayMessage::StopRecording { respond_to };
        
        self.sender.send(message).map_err(|_| Error::Internal("Replay actor disconnected".to_string()))?;
        response.await.map_err(|_| Error::Internal("Response channel closed".to_string()))?
    }

    pub async fn get_status(&self) -> Result<ReplayStatus> {
        let (respond_to, response) = oneshot::channel();
        let message = ReplayMessage::GetStatus { respond_to };
        
        self.sender.send(message).map_err(|_| Error::Internal("Replay actor disconnected".to_string()))?;
        Ok(response.await.map_err(|_| Error::Internal("Response channel closed".to_string()))?)
    }

    // Add similar methods for all other operations...
}

/// Static handle to the global replay actor
static REPLAY_ACTOR_HANDLE: std::sync::OnceLock<ReplayActorHandle> = std::sync::OnceLock::new();

/// Initialize the replay actor system
pub async fn initialize_replay_actor(brp_client: Arc<BrpClient>) -> Result<()> {
    let (sender, receiver) = mpsc::unbounded_channel();
    let handle = ReplayActorHandle::new(sender);
    
    // Store the handle globally
    REPLAY_ACTOR_HANDLE.set(handle).map_err(|_| Error::Internal("Replay actor already initialized".to_string()))?;
    
    // Spawn the actor
    let actor = ReplayActor::new(receiver, brp_client);
    tokio::spawn(actor.run());
    
    info!("Replay actor initialized successfully");
    Ok(())
}

/// Get the global replay actor handle
pub fn get_replay_actor_handle() -> Result<&'static ReplayActorHandle> {
    REPLAY_ACTOR_HANDLE.get().ok_or_else(|| Error::Internal("Replay actor not initialized".to_string()))
}