bevy_agent_runner 0.0.3

Runner API that owns and steps controllable Bevy agent simulation apps.
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
//! Checkpoint ownership, snapshot retention, and reset / direct restore.

use super::*;

/// Computes a snapshot checksum that excludes the pending action queue.
///
/// Pending futures beyond a restore target must NOT affect tick-target state
/// verification: the queue at verify time (cleared + current frame + maybe
/// re-applied futures) differs from the queue at record time. Both the stored
/// expectation and the replay-time actual are therefore computed with the
/// queue stripped (the `action_queue` field and the serialized
/// `AgentActionQueue` resource entry), so verification compares pure
/// simulation state.
pub fn queue_excluded_checksum(snapshot: &Snapshot) -> Result<SnapshotChecksum> {
    let mut stripped = snapshot.clone();
    stripped.action_queue.clear();
    stripped
        .resources
        .retain(|resource| resource.type_id != <AgentActionQueue as SnapshotType>::TYPE_ID);
    checksum_snapshot(&stripped)
}

/// Captures the current world and checksums it excluding the queue (see
/// [`queue_excluded_checksum`]).
pub(super) fn capture_queue_excluded_checksum(world: &mut World) -> Result<SnapshotChecksum> {
    let snapshot = capture_snapshot(world, None)?;
    queue_excluded_checksum(&snapshot)
}

/// Marks an episode boundary by starting a fresh replay log and timeline root.
///
/// Called from `AgentApp::reset` after the `AgentReset` schedule and before the
/// optional initial snapshot. It replaces the `ReplayLog` with a fresh one while
/// preserving the `ReplayRecorder.recording` flag, installs a fresh `Timeline`
/// root, and synchronizes `AgentControlState.timeline_id`/`branch_id` to that
/// new root. It is a no-op for apps that do not install the replay resources, so
/// core-only and snapshot-less configurations remain supported.
fn reset_replay_and_timeline(world: &mut World) -> Result<()> {
    if !world.contains_resource::<Timeline>() {
        return Ok(());
    }
    world.insert_resource(Timeline::default());
    let (timeline_id, branch_id) = {
        let timeline = world.resource::<Timeline>();
        (timeline.timeline_id(), timeline.current_branch())
    };
    if world.contains_resource::<ReplayRecorder>() {
        world.resource_scope(|world, mut recorder: Mut<ReplayRecorder>| {
            recorder
                .reset_episode(
                    world.resource::<EnvironmentMetadata>(),
                    world.resource::<Timeline>(),
                    0,
                )
                .map_err(anyhow::Error::msg)
        })?;
        let episode = world.resource::<ReplayRecorder>().log().manifest.episode_id;
        set_snapshot_episode(world, episode);
    }
    let mut control = world.resource_mut::<AgentControlState>();
    control.timeline_id = timeline_id;
    control.branch_id = branch_id;
    world.insert_resource(ExecutionContext::Live);
    Ok(())
}

impl AgentApp {
    /// Creates a role-aware checkpoint. Callers index it in the replay log
    /// before enforcing retention so the new live reference is protected.
    pub(super) fn create_owned_checkpoint(
        &mut self,
        label: Option<String>,
        role: SnapshotRole,
    ) -> Result<SnapshotCreateResult> {
        create_snapshot_with_role(self.app.world_mut(), label, role)
    }

    pub(super) fn index_checkpoint(
        &mut self,
        branch: BranchId,
        tick: u64,
        id: SnapshotId,
        checksum: SnapshotChecksum,
    ) -> Result<()> {
        if !self.app.world().contains_resource::<ReplayRecorder>() {
            return Ok(());
        }
        let world = self.app.world_mut();
        world.resource_scope(|world, mut recorder: Mut<ReplayRecorder>| {
            recorder
                .index_checkpoint(world.resource::<Timeline>(), branch, tick, id, checksum)
                .map_err(anyhow::Error::msg)
        })?;
        Ok(())
    }

    fn live_snapshot_references(&self) -> BTreeSet<SnapshotId> {
        let mut references = self
            .replay_log()
            .map(collect_replay_references)
            .unwrap_or_default();
        if let Some(timeline) = self.app.world().get_resource::<Timeline>() {
            references.extend(
                timeline
                    .branches()
                    .values()
                    .filter_map(|branch| branch.fork_snapshot),
            );
        }
        references
    }

    /// Retention runs after indexing with both log and live graph references.
    pub(super) fn prune_with_live_refs(&mut self) {
        let Some(keep) = self
            .app
            .world()
            .get_resource::<SnapshotPolicy>()
            .map(|policy| policy.keep_last_n_checkpoints)
        else {
            return;
        };
        let references = self.live_snapshot_references();
        prune_checkpoints_with_refs(self.app.world_mut(), keep, &references);
    }

    /// Rejects deletion of pinned snapshots and live replay / graph references.
    pub fn delete_snapshot(&mut self, id: SnapshotId) -> Result<()> {
        let references = self.live_snapshot_references();
        delete_snapshot_checked(self.app.world_mut(), id, &references)
    }

    /// Explicit CheckpointCreated handling: mirrors only the checkpoint
    /// created in the current episode (via `control.last_snapshot_created`,
    /// set by `create_snapshot` inside `AgentTick`) into the replay log.
    /// No scan-all-store behavior: stale cross-episode checkpoints never
    /// contaminate the fresh log. Tagged with (episode, branch, tick).
    ///
    /// Owner-side indexing: after `maybe_take_snapshot` creates an id, it is
    /// immediately indexed into the log and retention is enforced with live
    /// refs (the snapshot crate no longer prunes with replay visibility).
    pub(super) fn sync_auto_checkpoints(&mut self) -> Result<()> {
        // Replay-disabled retention: even without a recorder, snapshot-only
        // retention still applies (empty reference set).
        if !self.app.world().contains_resource::<ReplayRecorder>() {
            if self.app.world().contains_resource::<SnapshotStore>() {
                enforce_retention(self.app.world_mut(), &BTreeSet::new());
            }
            return Ok(());
        }
        if !self.app.world().resource::<ReplayRecorder>().is_recording() {
            self.prune_with_live_refs();
            return Ok(());
        }
        let (branch, created) = match (
            self.app.world().get_resource::<AgentControlState>(),
            self.app.world().get_resource::<ReplayRecorder>(),
        ) {
            (Some(control), Some(_recorder)) => (control.branch_id, control.last_snapshot_created),
            _ => return Ok(()),
        };
        if !self.app.world().contains_resource::<SnapshotStore>() {
            return Ok(());
        }
        let Some(id) = created else {
            return Ok(());
        };
        let known = match self.app.world().get_resource::<ReplayRecorder>() {
            Some(recorder) => recorder
                .log()
                .branch_checkpoints
                .iter()
                .map(|checkpoint| checkpoint.snapshot_id)
                .collect::<BTreeSet<_>>(),
            None => return Ok(()),
        };
        if known.contains(&id) {
            return Ok(());
        }
        let (tick, checksum, episode) = match self
            .app
            .world()
            .get_resource::<SnapshotStore>()
            .and_then(|store| store.get(id))
        {
            Some(snapshot) => (
                snapshot.manifest.tick,
                queue_excluded_checksum(snapshot).unwrap_or_else(|_| snapshot.checksum.clone()),
                snapshot.manifest.episode_id,
            ),
            None => return Ok(()),
        };
        if episode
            != self
                .app
                .world()
                .resource::<ReplayRecorder>()
                .log()
                .manifest
                .episode_id
        {
            return Ok(());
        }
        self.index_checkpoint(branch, tick, id, checksum)?;
        // Owner-side retention: the snapshot creation path no longer prunes
        // with replay visibility, so enforce here with live refs immediately
        // after indexing.
        self.prune_with_live_refs();
        Ok(())
    }

    /// Honors `SnapshotPolicy::checkpoint_on_terminal`: snapshots terminal
    /// steps into the replay log on the current branch.
    pub(super) fn maybe_terminal_checkpoint(&mut self) -> Result<()> {
        let terminal = match self.app.world().get_resource::<LastStepResponse>() {
            Some(last) => last
                .0
                .as_ref()
                .is_some_and(|response| response.done || response.truncated),
            None => false,
        };
        if !terminal || !self.has_snapshot_support() {
            return Ok(());
        }
        let enabled = self
            .app
            .world()
            .get_resource::<SnapshotPolicy>()
            .map(|policy| policy.checkpoint_on_terminal)
            .unwrap_or(false);
        if !enabled {
            return Ok(());
        }
        let branch = match self.app.world().get_resource::<AgentControlState>() {
            Some(control) => control.branch_id,
            None => return Ok(()),
        };
        // Avoid double-snapshotting when the interval policy already captured
        // this tick.
        let tick = self.current_tick();
        let already = self
            .app
            .world()
            .resource::<AgentControlState>()
            .last_snapshot_created
            .and_then(|id| self.app.world().resource::<SnapshotStore>().get(id))
            .is_some_and(|snapshot| snapshot.manifest.tick == tick);
        if already {
            return Ok(());
        }
        let result =
            self.create_owned_checkpoint(Some(format!("terminal-{tick}")), SnapshotRole::Periodic)?;
        let checksum = self
            .app
            .world()
            .get_resource::<SnapshotStore>()
            .and_then(|store| store.get(result.snapshot_id))
            .and_then(|snapshot| queue_excluded_checksum(snapshot).ok())
            .unwrap_or_else(|| result.checksum.clone());
        if self
            .app
            .world()
            .get_resource::<ReplayRecorder>()
            .is_some_and(ReplayRecorder::is_recording)
        {
            self.index_checkpoint(branch, result.tick, result.snapshot_id, checksum)?;
        }
        self.prune_with_live_refs();
        Ok(())
    }

    /// Ensures the latest observation response reports a snapshot created on
    /// the same tick: automatic checkpoints run after the observation system
    /// inside `AgentFinalize`, so the stored response is patched with the current
    /// `last_snapshot_created` marker when set.
    pub(super) fn patch_snapshot_created(&mut self) {
        let snapshot = self
            .app
            .world()
            .get_resource::<AgentControlState>()
            .and_then(|control| control.last_snapshot_created);
        if let Some(snapshot) = snapshot
            && let Some(mut last) = self.app.world_mut().get_resource_mut::<LastStepResponse>()
            && let Some(response) = last.0.as_mut()
        {
            response.info.snapshot_created = Some(snapshot);
        }
    }

    pub(super) fn has_snapshot_support(&self) -> bool {
        self.app.world().contains_resource::<SnapshotStore>()
    }

    pub(super) fn reset_impl(&mut self, options: ResetOptions) -> Result<Observation> {
        self.validate_observation_mode(&options.observation_mode)?;
        self.ensure_started();
        let tick_before = self.current_tick();
        let result = self.apply_reset(options);
        match result {
            Ok(observation) => {
                self.reset_once = true;
                self.app.world_mut().remove_resource::<FaultState>();
                Ok(observation)
            }
            Err(error) => {
                self.reset_once = false;
                Err(self.mutation_failed("reset", tick_before, error))
            }
        }
    }

    fn apply_reset(&mut self, options: ResetOptions) -> Result<Observation> {
        {
            let world = self.app.world_mut();
            world.resource_mut::<ObservationConfig>().mode = options.observation_mode;
            if let Some(seed) = options.seed {
                world.insert_resource(DeterministicRng::seeded(seed));
            }
        }

        self.app.world_mut().run_schedule(AgentReset);
        if let Some(error) = self
            .app
            .world()
            .resource::<bevy_agent_core::AgentTickFailure>()
            .error()
        {
            return Err(anyhow!("reset observation failed: {error}"));
        }
        reset_replay_and_timeline(self.app.world_mut())?;

        let mut response = self.last_response()?;
        {
            let control = self.app.world().resource::<AgentControlState>();
            response.info.timeline_id = control.timeline_id;
            response.info.branch_id = control.branch_id;
        }
        if options.create_initial_snapshot && self.has_snapshot_support() {
            let snapshot = self.create_owned_checkpoint(
                Some(format!("reset-{}", response.tick)),
                SnapshotRole::Initial,
            )?;
            let branch = self
                .app
                .world()
                .get_resource::<AgentControlState>()
                .map(|control| control.branch_id);
            let checksum = self
                .app
                .world()
                .get_resource::<SnapshotStore>()
                .and_then(|store| store.get(snapshot.snapshot_id))
                .and_then(|stored| queue_excluded_checksum(stored).ok())
                .unwrap_or_else(|| snapshot.checksum.clone());
            if self.app.world().contains_resource::<ReplayRecorder>() {
                let world = self.app.world_mut();
                world.resource_scope(|world, mut recorder: Mut<ReplayRecorder>| {
                    recorder
                        .set_baseline(
                            snapshot.snapshot_id,
                            snapshot.tick,
                            world.resource::<Timeline>(),
                        )
                        .map_err(anyhow::Error::msg)
                })?;
                self.index_checkpoint(
                    branch.expect("core control exists"),
                    snapshot.tick,
                    snapshot.snapshot_id,
                    checksum,
                )?;
            }
            response.info.snapshot_created = Some(snapshot.snapshot_id);
        }
        self.app.world_mut().resource_mut::<LastStepResponse>().0 = Some(response.clone());
        self.prune_with_live_refs();
        Ok(response.observation)
    }

    pub(super) fn snapshot_impl(&mut self) -> Result<SnapshotCreateResult> {
        self.ensure_no_fault()?;
        self.ensure_started();
        self.ensure_reset()?;
        if !self.has_snapshot_support() {
            return Err(anyhow!("AgentSnapshotPlugin is not installed"));
        }

        let tick = self.current_tick();
        if let Some(recorder) = self.app.world().get_resource::<ReplayRecorder>() {
            let branch = self.app.world().resource::<AgentControlState>().branch_id;
            let timeline = self.app.world().resource::<Timeline>();
            let info = timeline
                .branches()
                .get(&branch)
                .ok_or_else(|| anyhow!("active branch is missing"))?;
            if tick < info.fork_tick || tick < recorder.log().initial_tick {
                return Err(anyhow!(
                    "cannot checkpoint before active branch recording interval; create a branch at tick {tick} first"
                ));
            }
        }
        if let Some(recorder) = self.app.world().get_resource::<ReplayRecorder>() {
            recorder
                .validate_checkpoint_capacity(
                    self.app.world().resource::<Timeline>(),
                    self.app.world().resource::<AgentControlState>().branch_id,
                    tick,
                )
                .map_err(anyhow::Error::msg)?;
        }
        let result =
            self.create_owned_checkpoint(Some(format!("manual-{tick}")), SnapshotRole::Manual)?;
        // Tag with the current branch to preserve same-tick isolation;
        // checkpoints remain isolated between branches at the same tick.
        let branch = self
            .app
            .world()
            .get_resource::<AgentControlState>()
            .map(|control| control.branch_id)
            .or_else(|| {
                self.app
                    .world()
                    .get_resource::<Timeline>()
                    .map(|timeline| timeline.current_branch())
            });
        let checksum = self
            .app
            .world()
            .get_resource::<SnapshotStore>()
            .and_then(|store| store.get(result.snapshot_id))
            .and_then(|snapshot| queue_excluded_checksum(snapshot).ok())
            .unwrap_or_else(|| result.checksum.clone());
        if let Some(branch) = branch {
            self.index_checkpoint(branch, result.tick, result.snapshot_id, checksum)?;
        }
        self.prune_with_live_refs();
        Ok(result)
    }

    pub(super) fn restore_impl(&mut self, snapshot: SnapshotId) -> Result<()> {
        self.ensure_no_fault()?;
        let stored = self
            .app
            .world()
            .get_resource::<SnapshotStore>()
            .ok_or_else(|| anyhow!("AgentSnapshotPlugin is not installed"))?
            .get(snapshot)
            .ok_or_else(|| anyhow!("snapshot {snapshot:?} not found"))?;
        validate_snapshot_full(self.app.world(), stored)?;
        self.ensure_started();
        self.history_transaction(
            &format!("restore snapshot {snapshot:?}"),
            transaction::TransactionKind::World,
            |agent| agent.apply_stored_snapshot(snapshot),
        )
    }

    /// Applies a checkpoint inside an existing outer navigation transaction.
    pub(super) fn apply_stored_snapshot(&mut self, id: SnapshotId) -> Result<()> {
        let snapshot = self
            .app
            .world()
            .resource::<SnapshotStore>()
            .get(id)
            .cloned()
            .ok_or_else(|| anyhow!("snapshot {id:?} not found"))?;
        restore_snapshot_backup(self.app.world_mut(), &snapshot)?;
        let restored_tick = self.current_tick();
        let mut control = self.app.world_mut().resource_mut::<AgentControlState>();
        control.frame = restored_tick;
        control.last_snapshot_created = None;
        self.reset_once = true;
        collect_observation(self.app.world_mut())?;
        Ok(())
    }
}