bevy_fairy 0.12.0

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
use serde_json::{json, Value};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

use crate::server::brp_client::{BrpClient, GameName, Unnamed};
use crate::server::error::{Error, Result};
use crate::server::experiment_system::{Action, ActionExecutor};

/// The game process the history was recorded against.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Recorded {
    /// No action has been recorded.
    Nothing,
    /// A game that named itself with the `instance` of `bevy_fairy/status`.
    Game(String),
    /// A game that could not name itself, and why: its history cannot be told from another's, unless
    /// a game that names itself answers later (a process does not gain a plugin).
    Unverifiable(Unnamed),
}

/// What comparing the game that answers now with the one the history was recorded against found.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Standing {
    /// Nothing recorded, or the same game.
    Current,
    /// The history is of another game process: a restart reuses entity ids for other entities.
    Stale { entries: usize },
    /// The history is of a game that named itself, and the game now cannot be asked who it is.
    Unconfirmed(String),
    /// Neither game can name itself, so no comparison is possible. The reason says why.
    Unverified(String),
}

impl Standing {
    /// The reply that refuses an operation this standing forbids, or `None` when it may go ahead.
    /// A stale history refuses `undo` and `redo` but not `execute`, which starts a new one. A
    /// history recorded against a game that named itself refuses every operation, `execute`
    /// included, while that game cannot be asked who it is.
    fn refusal(&self, operation: &str, url: &str, stale_refuses: bool) -> Option<Value> {
        match self {
            Standing::Stale { entries } if stale_refuses => Some(json!({
                "error": "history belongs to another game",
                "message": format!(
                    "The {entries} recorded action(s) were taken against another game process than the one answering at {url} now; \
                     a restarted game gives its entities other ids, so `{operation}` would change the wrong ones. \
                     Nothing was changed in the game, and the stale history was discarded. `execute` starts a new history."
                ),
                "history_discarded": {"entries": entries},
            })),
            Standing::Unconfirmed(why) => Some(json!({
                "error": "cannot tell whether the history belongs to this game",
                "message": format!(
                    "The history was recorded against a game that named itself, and the game at {url} cannot now: {why}. \
                     It may be another game, so `{operation}` was not run. `clear_history` discards the history."
                ),
            })),
            _ => None,
        }
    }

    /// Put what the comparison found into the reply of an operation that went ahead.
    fn annotate(self, reply: &mut Value, url: &str) {
        let Some(fields) = reply.as_object_mut() else { return };
        match self {
            Standing::Stale { entries } => {
                fields.insert(
                    "history_discarded".to_string(),
                    json!({
                        "entries": entries,
                        "reason": format!("they were recorded against another game process than the one answering at {url} now (a restart reuses entity ids)"),
                    }),
                );
            }
            Standing::Unverified(why) => {
                fields.insert(
                    "game_instance_unverified".to_string(),
                    Value::from(format!(
                        "the game answering could not be confirmed as the one that ran the recorded actions: {why}"
                    )),
                );
            }
            Standing::Current | Standing::Unconfirmed(_) => {}
        }
    }
}

/// Global experiment state
pub struct ExperimentState {
    executor: ActionExecutor,
    /// Which game the history in `executor` was recorded against.
    recorded: Recorded,
}

impl ExperimentState {
    /// Create new experiment state
    pub fn new() -> Self {
        Self {
            executor: ActionExecutor::new(),
            recorded: Recorded::Nothing,
        }
    }

    /// Compare the game that answers now (`Err`: why it cannot name itself) with the one the
    /// history was recorded against. Changes nothing.
    fn standing(&self, now: &GameName) -> Standing {
        standing_of(&self.recorded, self.executor.entry_count(), now)
    }

    /// Settle on the game now answering before an operation runs: a stale history is dropped, and
    /// an empty one is recorded against the game the operation is about to act on.
    fn adopt(&mut self, now: &GameName) -> Standing {
        let standing = self.standing(now);
        let empty = self.executor.entry_count() == 0;
        if matches!(standing, Standing::Stale { .. }) {
            self.executor.clear_history();
        }
        if empty || matches!(standing, Standing::Stale { .. }) {
            self.recorded = match now {
                Ok(id) => Recorded::Game(id.clone()),
                Err(unnamed) => Recorded::Unverifiable(unnamed.clone()),
            };
        }
        standing
    }
}

/// The decision behind [`ExperimentState::standing`]: `entries` actions are on record, taken
/// against `recorded`, and `now` is the game that answers.
fn standing_of(recorded: &Recorded, entries: usize, now: &GameName) -> Standing {
    match (entries, recorded, now) {
        (0, _, Ok(_)) => Standing::Current,
        (0, _, Err(now)) => Standing::Unverified(now.why.clone()),
        (_, Recorded::Game(then), Ok(id)) if then == id => Standing::Current,
        (_, Recorded::Game(_), Ok(_)) => Standing::Stale { entries },
        (_, Recorded::Game(_), Err(now)) => Standing::Unconfirmed(now.why.clone()),
        // The game that ran the actions answered without naming itself and a game that names itself
        // answers now: a process does not gain a plugin, so it is another one.
        (_, Recorded::Unverifiable(then), Ok(_)) if then.answered_without_a_name => Standing::Stale { entries },
        (_, Recorded::Unverifiable(then), _) => Standing::Unverified(then.why.clone()),
        // Actions on record against no game cannot happen: `adopt` runs before every action.
        (_, Recorded::Nothing, _) => Standing::Unverified("the history records no game".to_string()),
    }
}

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

// Global experiment state
static EXPERIMENT_STATE: std::sync::OnceLock<Arc<RwLock<ExperimentState>>> =
    std::sync::OnceLock::new();

fn get_experiment_state() -> Arc<RwLock<ExperimentState>> {
    EXPERIMENT_STATE
        .get_or_init(|| Arc::new(RwLock::new(ExperimentState::new())))
        .clone()
}

/// Handle experiment tool requests
pub async fn handle(arguments: Value, brp_client: Arc<RwLock<BrpClient>>) -> Result<Value> {
    debug!("Experiment tool called with arguments: {}", arguments);

    // Parse action parameter
    let action_str = arguments
        .get("action")
        .and_then(|a| a.as_str())
        .unwrap_or("execute");

    // Actions that don't require BRP connection
    match action_str {
        "history" => return handle_history(brp_client).await,
        "clear_history" => return handle_clear_history().await,
        _ => {}
    }

    // Check connection for actions that need BRP
    let is_connected = {
        let client = brp_client.read().await;
        client.reachable().await
    };

    if !is_connected {
        warn!("BRP client not connected");
        return Ok(json!({
            "error": "BRP client not connected",
            "message": "Cannot execute experiment - not connected to Bevy game",
            "brp_connected": false
        }));
    }

    match action_str {
        "execute" => handle_execute(arguments, brp_client).await,
        "undo" => handle_undo(brp_client).await,
        "redo" => handle_redo(brp_client).await,
        _ => Ok(json!({
            "error": "Unknown action",
            "message": format!("Unknown action: {}", action_str),
            "available_actions": ["execute", "undo", "redo", "history", "clear_history"]
        })),
    }
}

/// Handle execute action
async fn handle_execute(arguments: Value, brp_client: Arc<RwLock<BrpClient>>) -> Result<Value> {
    // Parse actions array
    let actions_json = arguments
        .get("actions")
        .ok_or_else(|| Error::Validation("Missing 'actions' parameter".to_string()))?;

    let actions: Vec<Action> = if actions_json.is_array() {
        serde_json::from_value(actions_json.clone())
            .map_err(|e| Error::Validation(format!("Failed to parse actions: {e}")))?
    } else {
        // Single action
        vec![serde_json::from_value(actions_json.clone())
            .map_err(|e| Error::Validation(format!("Failed to parse action: {e}")))?]
    };

    if actions.is_empty() {
        return Err(Error::Validation(
            "`execute` needs a non-empty `actions` array, e.g. {\"action\":\"execute\",\"actions\":[...]}; \
             nothing was run"
                .to_string(),
        ));
    }

    info!("Executing {} actions", actions.len());

    let state = get_experiment_state();
    let mut state_guard = state.write().await;
    let mut client = brp_client.write().await;

    let standing = state_guard.adopt(&client.game_instance().await);
    if let Some(refusal) = standing.refusal("execute", client.url(), false) {
        return Ok(refusal);
    }

    let mut results = Vec::new();
    let mut success_count = 0;
    let mut failure_count = 0;

    for action in &actions {
        let result = state_guard
            .executor
            .execute_action(action, &mut client)
            .await?;

        if result.success {
            success_count += 1;
            info!("Action succeeded: {}", result.message);
        } else {
            failure_count += 1;
            warn!("Action failed: {}", result.message);
        }

        results.push(result);
    }

    let mut reply = json!({
        "results": results,
        "summary": {
            "total_actions": actions.len(),
            "successful": success_count,
            "failed": failure_count,
            "success_rate": if actions.is_empty() { 0.0 } else { (success_count as f64) / (actions.len() as f64) }
        },
        "timestamp": chrono::Utc::now().to_rfc3339()
    });
    standing.annotate(&mut reply, client.url());
    Ok(reply)
}

/// Handle undo action
async fn handle_undo(brp_client: Arc<RwLock<BrpClient>>) -> Result<Value> {
    info!("Performing undo");

    let state = get_experiment_state();
    let mut state_guard = state.write().await;
    let mut client = brp_client.write().await;

    let standing = state_guard.adopt(&client.game_instance().await);
    if let Some(refusal) = standing.refusal("undo", client.url(), true) {
        return Ok(refusal);
    }
    let result = state_guard.executor.undo(&mut client).await?;

    let mut reply = json!({
        "result": result,
        "timestamp": chrono::Utc::now().to_rfc3339()
    });
    standing.annotate(&mut reply, client.url());
    Ok(reply)
}

/// Handle redo action
async fn handle_redo(brp_client: Arc<RwLock<BrpClient>>) -> Result<Value> {
    info!("Performing redo");

    let state = get_experiment_state();
    let mut state_guard = state.write().await;
    let mut client = brp_client.write().await;

    let standing = state_guard.adopt(&client.game_instance().await);
    if let Some(refusal) = standing.refusal("redo", client.url(), true) {
        return Ok(refusal);
    }
    let result = state_guard.executor.redo(&mut client).await?;

    let mut reply = json!({
        "result": result,
        "timestamp": chrono::Utc::now().to_rfc3339()
    });
    standing.annotate(&mut reply, client.url());
    Ok(reply)
}

/// Handle history request. It says which game the history was recorded against and whether the
/// game answering now is that one: `stale` is `true` or `false` when the game can be asked, and
/// `null` when it cannot, with the reason in `game_instance_unverified` while actions are on
/// record. `history` and `count` are the actions `undo` can step back over; `redoable` counts the
/// ones `redo` can step forward over, which a stale check also covers. Reading changes nothing.
async fn handle_history(brp_client: Arc<RwLock<BrpClient>>) -> Result<Value> {
    let state = get_experiment_state();
    let state_guard = state.read().await;

    let now = {
        let mut client = brp_client.write().await;
        client.game_instance().await
    };
    let standing = state_guard.standing(&now);
    let history = state_guard.executor.get_history();

    let mut reply = json!({
        "history": history,
        "count": history.len(),
        "redoable": state_guard.executor.entry_count() - history.len(),
        "recorded_against": match &state_guard.recorded {
            Recorded::Game(id) => Value::from(id.as_str()),
            Recorded::Nothing | Recorded::Unverifiable(_) => Value::Null,
        },
        "stale": match &standing {
            Standing::Stale { .. } => Value::Bool(true),
            Standing::Current => Value::Bool(false),
            Standing::Unconfirmed(_) | Standing::Unverified(_) => Value::Null,
        },
        "timestamp": chrono::Utc::now().to_rfc3339()
    });
    if let (Standing::Unconfirmed(why) | Standing::Unverified(why), Some(fields)) = (&standing, reply.as_object_mut()) {
        if state_guard.executor.entry_count() > 0 {
            fields.insert("game_instance_unverified".to_string(), Value::from(why.as_str()));
        }
    }
    Ok(reply)
}

/// Handle clear history
async fn handle_clear_history() -> Result<Value> {
    info!("Clearing action history");

    let state = get_experiment_state();
    let mut state_guard = state.write().await;

    state_guard.executor.clear_history();
    state_guard.recorded = Recorded::Nothing;

    Ok(json!({
        "message": "History cleared",
        "timestamp": chrono::Utc::now().to_rfc3339()
    }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server::config::Config;

    #[tokio::test]
    async fn test_handle_without_connection() {
        let config = Config { mcp_port: 3000, ..Config::default() };
        let brp_client = Arc::new(RwLock::new(crate::server::brp_client::BrpClient::new(&config)));

        let args = json!({
            "action": "execute",
            "actions": []
        });

        let result = handle(args, brp_client).await.unwrap();
        assert_eq!(result.get("error").unwrap(), "BRP client not connected");
    }

    #[tokio::test]
    async fn test_handle_unknown_action() {
        let config = Config { mcp_port: 3000, ..Config::default() };
        let brp_client = Arc::new(RwLock::new(crate::server::brp_client::BrpClient::new(&config)));

        let args = json!({
            "action": "unknown"
        });

        let result = handle(args, brp_client).await.unwrap();
        assert!(result.get("error").is_some());
    }

    #[tokio::test]
    async fn test_handle_history() {
        let config = Config { mcp_port: 3000, ..Config::default() };
        let brp_client = Arc::new(RwLock::new(crate::server::brp_client::BrpClient::new(&config)));

        let args = json!({
            "action": "history"
        });

        let result = handle(args, brp_client).await.unwrap();
        assert!(result.get("history").is_some());
        assert!(result.get("count").is_some());
    }

    #[tokio::test]
    async fn test_handle_clear_history() {
        let config = Config { mcp_port: 3000, ..Config::default() };
        let brp_client = Arc::new(RwLock::new(crate::server::brp_client::BrpClient::new(&config)));

        let args = json!({
            "action": "clear_history"
        });

        let result = handle(args, brp_client).await.unwrap();
        assert_eq!(result.get("message").unwrap(), "History cleared");
    }

    fn game(id: &str) -> Recorded {
        Recorded::Game(id.to_string())
    }

    fn unnamed(why: &str, answered_without_a_name: bool) -> Unnamed {
        Unnamed { why: why.to_string(), answered_without_a_name }
    }

    #[test]
    fn a_history_is_current_only_for_the_game_it_was_recorded_against() {
        let same = Ok("a".to_string());
        let other = Ok("b".to_string());
        assert_eq!(standing_of(&game("a"), 3, &same), Standing::Current);
        assert_eq!(standing_of(&game("a"), 3, &other), Standing::Stale { entries: 3 });
    }

    #[test]
    fn a_game_that_cannot_be_asked_leaves_a_named_history_unconfirmed_not_current() {
        let silent = Err(unnamed("no answer", false));
        assert_eq!(standing_of(&game("a"), 1, &silent), Standing::Unconfirmed("no answer".to_string()));
    }

    #[test]
    fn a_history_recorded_without_a_name_is_unverified_while_no_game_names_itself() {
        for answered in [true, false] {
            let recorded = Recorded::Unverifiable(unnamed("no plugin", answered));
            assert_eq!(
                standing_of(&recorded, 2, &Err(unnamed("still none", true))),
                Standing::Unverified("no plugin".to_string()),
                "answered: {answered}"
            );
        }
    }

    /// A process does not gain a plugin: the game that answered with no `instance` cannot be the
    /// one that names itself now.
    #[test]
    fn a_history_recorded_against_a_game_that_answered_without_a_name_is_stale_for_one_that_names_itself() {
        let recorded = Recorded::Unverifiable(unnamed("no plugin", true));
        assert_eq!(standing_of(&recorded, 2, &Ok("a".to_string())), Standing::Stale { entries: 2 });
    }

    /// A game that was not asked successfully says nothing about itself, so a game that names
    /// itself later may be the same one.
    #[test]
    fn a_history_recorded_while_the_game_could_not_be_asked_stays_unverified_for_one_that_names_itself() {
        let recorded = Recorded::Unverifiable(unnamed("no answer", false));
        assert_eq!(standing_of(&recorded, 2, &Ok("a".to_string())), Standing::Unverified("no answer".to_string()));
    }

    #[test]
    fn an_empty_history_has_nothing_to_be_stale_and_says_when_the_game_cannot_name_itself() {
        assert_eq!(standing_of(&game("a"), 0, &Ok("b".to_string())), Standing::Current);
        assert_eq!(standing_of(&Recorded::Nothing, 0, &Ok("b".to_string())), Standing::Current);
        assert_eq!(
            standing_of(&Recorded::Nothing, 0, &Err(unnamed("no plugin", true))),
            Standing::Unverified("no plugin".to_string())
        );
    }
}