hana_rigging 0.1.0

Device identity, presence, availability, and recovery policy for Bevy providers
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
//! Turning reconciled state changes into the events in `crate::events`.
//!
//! Everything here runs at the end of `crate::RiggingSystems::Reconcile`, after
//! `crate::reconcile::project_device_entities` has written this pass's conclusions onto entities.
//! The mirrors are written only when a value differs, so Bevy's own change detection is the
//! once-per-change gate: a settled frame leaves every mirror untouched and this stage emits
//! nothing.
//!
//! `crate::RoleState` is the one axis emitted elsewhere. `crate::apply` writes it a full system set
//! later and can move one role `Applying → Waiting → Applying` inside a single frame, which a
//! mirror-derived event would arrive too late to see and would collapse into one arrival.

use bevy::ecs::change_detection::DetectChanges;
use bevy::ecs::change_detection::Ref;
use bevy::ecs::observer::On;
use bevy::ecs::system::Commands;
use bevy::ecs::system::Query;
use bevy::ecs::system::Res;
use bevy::ecs::system::ResMut;
use bevy::prelude::Added;
use bevy::prelude::Entity;
use bevy::prelude::Resource;

use crate::Bindings;
use crate::Claim;
use crate::ClaimChanged;
use crate::ConfiguredDeviceConnectionChanged;
use crate::DeviceArrived;
use crate::DeviceDeparted;
use crate::DeviceKey;
use crate::DeviceResolution;
use crate::DeviceStateLookup;
use crate::Devices;
use crate::DiscoveryControl;
use crate::DiscoveryFinished;
use crate::DiscoveryProgressChanged;
use crate::IdentityChanged;
use crate::IdentityVerdict;
use crate::Presence;
use crate::PresenceChanged;
use crate::ReapplyConfiguration;
use crate::RecoveryPolicy;
use crate::RecoveryPolicyChanged;
use crate::RetireRole;
use crate::RoleAvailable;
use crate::RoleAwaiting;
use crate::RoleKey;
use crate::SchemeName;
use crate::StartupDiscoveryChanged;
use crate::UnregisteredSchemeReported;
use crate::WaitingWork;
use crate::devices::DepartureAnnouncements;
use crate::discovery::DiscoveryTransition;
use crate::discovery::DiscoveryTransitionJournal;
use crate::registration::Reporters;

/// Which schemes and role-availability edges have already reached a consumer.
///
/// Kept because neither fact is mirrored onto an entity, so neither has Bevy change detection
/// behind it. `crate::Devices::unregistered_schemes` is a retained set that keeps naming a rejected
/// scheme for the life of the process, and a role's availability is derived from a resource lookup
/// rather than from a component, so both would restate themselves on every frame without a record
/// of what was already said.
#[derive(Debug, Default, Resource)]
pub(crate) struct AnnouncedEdges {
    /// Schemes a `crate::UnregisteredSchemeReported` has already been emitted for.
    schemes:   Vec<SchemeName>,
    /// Roles currently reported as waiting for hardware, so the closing `crate::RoleAvailable`
    /// edge fires once and the opening `crate::RoleAwaiting` edge does not repeat while the
    /// wait lasts.
    awaiting:  Vec<RoleKey>,
    /// Roles already reported as resolved, so a role that never went absent is not re-announced
    /// every frame its device stays put.
    available: Vec<RoleKey>,
}

/// Emit one event per device-entity state axis that moved this pass.
///
/// `Ref::is_changed` is true on the tick a component is inserted as well as on a later write, so a
/// device's first reported reachability is an edge like any other and does not need a separate
/// first-value path.
pub(crate) fn announce_device_changes(
    mut commands: Commands,
    arrived: Query<(Entity, &DeviceKey), Added<DeviceKey>>,
    presences: Query<(Entity, Ref<'_, Presence>)>,
    claims: Query<(Entity, Ref<'_, Claim>)>,
    verdicts: Query<(Entity, Ref<'_, IdentityVerdict>)>,
) {
    for (device, key) in &arrived {
        commands.trigger(DeviceArrived {
            device,
            key: key.clone(),
        });
    }
    for (device, presence) in &presences {
        if presence.is_changed() {
            commands.trigger(PresenceChanged {
                device,
                presence: *presence,
            });
        }
    }
    for (device, claim) in &claims {
        if claim.is_changed() {
            commands.trigger(ClaimChanged {
                device,
                claim: (*claim).clone(),
            });
        }
    }
    for (device, verdict) in &verdicts {
        if verdict.is_changed() {
            commands.trigger(IdentityChanged {
                device,
                verdict: (*verdict).clone(),
            });
        }
    }
}

/// Emit one event per binding-entity state axis that moved this pass.
///
/// Only `crate::RecoveryPolicy` is mirror-derived here. `crate::RoleState` is announced from
/// `crate::RiggingSystems::Apply` for the reason this module's own documentation gives.
pub(crate) fn announce_binding_changes(
    mut commands: Commands,
    recoveries: Query<(Entity, &RoleKey, Ref<'_, RecoveryPolicy>)>,
) {
    for (binding, role, recovery) in &recoveries {
        if recovery.is_changed() {
            commands.trigger(RecoveryPolicyChanged {
                binding,
                role: role.clone(),
                recovery: *recovery,
            });
        }
    }
}

/// Emit the departure, connection, and rejected-scheme facts that have no mirrored component.
///
/// A departure that retired its key despawned the device entity before this runs, which is exactly
/// why `crate::DeviceDeparted` is global and carries the durable key: there is nothing left to
/// address or to read the key back from.
pub(crate) fn announce_reconciled_facts(
    mut commands: Commands,
    mut departure_announcements: ResMut<DepartureAnnouncements>,
    devices: Res<Devices>,
    mut announced_edges: ResMut<AnnouncedEdges>,
) {
    for departed_device in departure_announcements.departed.drain(..) {
        commands.trigger(DeviceDeparted {
            key:       departed_device.key,
            departure: departed_device.departure,
        });
    }
    for connection_change in departure_announcements.connections.drain(..) {
        commands.trigger(ConfiguredDeviceConnectionChanged {
            key:        connection_change.key,
            connection: connection_change.connection,
        });
    }
    for scheme in devices.unregistered_schemes() {
        if !announced_edges.schemes.contains(scheme) {
            announced_edges.schemes.push(scheme.clone());
            commands.trigger(UnregisteredSchemeReported {
                scheme: scheme.clone(),
            });
        }
    }
}

/// Whether a role's endpoint currently has a usable unit behind it.
///
/// `Devices::resolve` answers only whether the key is still mapped, and it stays mapped for a unit
/// the scan still names while reporting it `Presence::Absent` or `Presence::Unreachable` — the
/// departure a reporter produces when it can still enumerate the unit it lost. Presence is the
/// question the availability edges are defined on, so resolution alone leaves a departed unit
/// indistinguishable from a live one.
fn endpoint_has_live_device(devices: &Devices, device: &DeviceKey) -> bool {
    let DeviceResolution::Resolved(device_id) = devices.resolve(device) else {
        return false;
    };
    matches!(
        devices.state(device_id),
        DeviceStateLookup::Retained(reconciled_device_state)
            if reconciled_device_state.presence == Presence::Present
    )
}

/// Emit the interval in which a registered role has no live device behind its endpoint.
///
/// Both events are global because during that interval there may be no device entity at all, and a
/// role registered this frame may not have had its binding entity spawned yet.
pub(crate) fn announce_role_availability(
    mut commands: Commands,
    bindings: Res<Bindings>,
    devices: Res<Devices>,
    mut announced_edges: ResMut<AnnouncedEdges>,
) {
    let mut awaiting = Vec::new();
    let mut available = Vec::new();
    for role in bindings.registered_roles() {
        let Ok(binding) = bindings.binding(role) else {
            continue;
        };
        if endpoint_has_live_device(&devices, &binding.endpoint.device) {
            available.push(role.clone());
        } else {
            awaiting.push(role.clone());
        }
    }

    for role in &awaiting {
        if !announced_edges.awaiting.contains(role) {
            commands.trigger(RoleAwaiting { role: role.clone() });
        }
    }
    for role in &available {
        if !announced_edges.available.contains(role) {
            commands.trigger(RoleAvailable { role: role.clone() });
        }
    }
    announced_edges.awaiting = awaiting;
    announced_edges.available = available;
}

/// Arm the reporter whose coverage can settle a role's wait for hardware.
///
/// A registered binding is a standing claim on hardware, so the kernel — not each caller — asks
/// for the scan that resolves it: on the opening `crate::RoleAwaiting` edge, the first registered
/// reporter whose `crate::ReporterCoverage` establishes absence for the endpoint's
/// `crate::DeviceKey` is enabled through `DiscoveryControl::enable`, which also requests exactly
/// one run — the only verb that reaches a `crate::ReporterActivation::Disabled` reporter, since
/// `DiscoveryControl::mark_dirty` stays out of the due list while disabled. The edge fires once
/// per awaiting interval, so an already-enabled reporter gets a single extra scan on a device
/// departure and a settled wait requests nothing. A key no registered reporter covers arms
/// nothing: no scan's omission of that key would prove anything.
pub(crate) fn on_role_awaiting(
    role_awaiting: On<RoleAwaiting>,
    bindings: Res<Bindings>,
    reporters: Res<Reporters>,
    mut discovery_control: ResMut<DiscoveryControl>,
) {
    let Ok(binding) = bindings.binding(&role_awaiting.role) else {
        return;
    };
    let Some(covering_reporter) = reporters
        .registered_reporters()
        .find(|registered_reporter| {
            registered_reporter
                .coverage
                .establishes_absence_for(&binding.endpoint.device)
        })
        .map(|registered_reporter| registered_reporter.reporter)
    else {
        return;
    };
    drop(discovery_control.enable(covering_reporter));
}

/// Retire a role because application code asked for it, rather than because a device left.
///
/// Global rather than entity-targeted so a role registered and retired inside one frame — which
/// never had a binding entity — can still be retired. A role that was never bound is not an error
/// here: the request and the outcome agree.
pub(crate) fn on_retire_role(retire_role: On<RetireRole>, mut bindings: ResMut<Bindings>) {
    drop(bindings.retire(&retire_role.role));
}

/// Answer an application's request to re-apply a role's saved configuration.
///
/// Honoured only for `crate::RecoveryPolicy::ReapplyOnRequest`. `crate::RecoveryPolicy::Retain`
/// promises the kernel remembers and reports but never touches the device, so honouring a request
/// would break that promise through the front door; `crate::RecoveryPolicy::Forget` dropped the
/// saved value at the departure and has nothing left to re-apply. Both leave the role's owed
/// application request exactly where it was.
pub(crate) fn on_reapply_configuration(
    reapply_configuration: On<ReapplyConfiguration>,
    roles: Query<&RoleKey>,
    mut bindings: ResMut<Bindings>,
) {
    let Ok(role) = roles.get(reapply_configuration.binding).cloned() else {
        return;
    };
    let Ok(binding) = bindings.binding(&role) else {
        return;
    };
    if binding.recovery != RecoveryPolicy::ReapplyOnRequest
        || bindings.waiting_work(&role) != WaitingWork::ApplicationRequestOwed
    {
        return;
    }
    bindings.request_reapply(&role);
}

/// Emit every discovery transition the scheduler recorded this frame, and empty the journal.
///
/// Ordered after the four systems above so a consumer that watches both sees this frame's device
/// conclusions before the discovery bookkeeping that produced them. Draining here is what keeps the
/// journal bounded — it is a record of one frame's transitions, never a history.
///
/// `crate::DiscoveryProgressChanged` carries the reporter's own report and its batch counts from
/// one recorded transition rather than from two reads of the retained status, so the per-reporter
/// and the aggregate view cannot disagree about the same batch.
pub(crate) fn announce_discovery_transitions(
    mut commands: Commands,
    mut discovery_transition_journal: ResMut<DiscoveryTransitionJournal>,
) {
    for discovery_transition in discovery_transition_journal.drain() {
        match discovery_transition {
            DiscoveryTransition::Progressed {
                batch,
                reporter,
                progress,
                completed,
                total,
                running,
                queued,
            } => {
                commands.trigger(DiscoveryProgressChanged {
                    batch,
                    reporter,
                    progress,
                    completed,
                    total,
                    running,
                    queued,
                });
            },
            DiscoveryTransition::Finished {
                batch,
                reporter,
                outcome,
            } => {
                commands.trigger(DiscoveryFinished {
                    batch,
                    reporter,
                    outcome,
                });
            },
            DiscoveryTransition::StartupChanged { startup } => {
                commands.trigger(StartupDiscoveryChanged { state: startup });
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::sync::Arc;
    use std::sync::atomic::AtomicUsize;
    use std::sync::atomic::Ordering;
    use std::time::Duration;

    use bevy::app::App;

    use crate::AuthoritativeReporterCoverage;
    use crate::Binding;
    use crate::Bindings;
    use crate::CoveredDeviceIdentitySpace;
    use crate::DeviceEndpoint;
    use crate::DeviceIdSource;
    use crate::DeviceKey;
    use crate::DeviceKind;
    use crate::DeviceReporter;
    use crate::DeviceScan;
    use crate::DiscoveryCadence;
    use crate::DiscoveryControl;
    use crate::DiscoveryWork;
    use crate::EndpointId;
    use crate::LastKnownGoodConfiguration;
    use crate::MainThreadDiscoveryJob;
    use crate::OnAbort;
    use crate::OnSessionLoss;
    use crate::RecoveryPolicy;
    use crate::ReporterActivation;
    use crate::ReporterCoverage;
    use crate::ReporterId;
    use crate::ReporterRegistration;
    use crate::RequestedConfiguration;
    use crate::RetryOn;
    use crate::RiggingAppExt;
    use crate::RiggingPlugin;
    use crate::RoleKey;
    use crate::RoleState;
    use crate::binding::ApplyDeadline;
    use crate::registration::DriverId;
    use crate::scheme::AuthoredId;

    /// Long enough that a periodic reporter never becomes due on its own inside a test.
    const NEVER_DUE_UNAIDED: Duration = Duration::from_hours(1);

    struct CountingReporter {
        scans: Arc<AtomicUsize>,
    }

    impl DeviceReporter for CountingReporter {
        fn discover(&mut self) -> DiscoveryWork {
            self.scans.fetch_add(1, Ordering::Relaxed);
            DiscoveryWork::Immediate(MainThreadDiscoveryJob::new(|_| {
                DeviceScan::Complete(Vec::new())
            }))
        }
    }

    fn app_with_disabled_reporter_covering(
        kind: DeviceKind,
    ) -> (App, ReporterId, Arc<AtomicUsize>) {
        let mut app = App::new();
        app.add_plugins(RiggingPlugin);
        let scans = Arc::new(AtomicUsize::new(0));
        let reporter = app.add_device_reporter(
            CountingReporter {
                scans: Arc::clone(&scans),
            },
            ReporterRegistration::optional(
                DiscoveryCadence::Periodic {
                    interval: NEVER_DUE_UNAIDED,
                },
                ReporterActivation::Disabled,
                ReporterCoverage::EstablishesAbsence(AuthoritativeReporterCoverage::one(
                    CoveredDeviceIdentitySpace::AllKeysOfKind { kind },
                )),
            ),
        );

        (app, reporter, scans)
    }

    fn unresolved_camera_binding(role: &str) -> Result<Binding, Box<dyn Error>> {
        Ok(Binding {
            role:            RoleKey::new(role)?,
            endpoint:        DeviceEndpoint {
                device: DeviceKey {
                    kind: DeviceKind::Camera,
                    id:   DeviceIdSource::Authored {
                        value: AuthoredId::new(role)?,
                    },
                },
                id:     EndpointId::Whole,
            },
            driver:          DriverId(0),
            recovery:        RecoveryPolicy::Forget,
            retry:           RetryOn::NewRevision,
            on_abort:        OnAbort::default(),
            on_loss:         OnSessionLoss::default(),
            state:           RoleState::default(),
            requested:       RequestedConfiguration::new(()),
            last_known_good: LastKnownGoodConfiguration::default(),
            apply_deadline:  ApplyDeadline::ProcessDefault,
        })
    }

    #[test]
    fn a_role_awaiting_edge_enables_and_runs_the_covering_disabled_reporter()
    -> Result<(), Box<dyn Error>> {
        let (mut app, reporter, scans) = app_with_disabled_reporter_covering(DeviceKind::Camera);
        app.world_mut()
            .resource_mut::<Bindings>()
            .register(unresolved_camera_binding("camera-tool")?)?;

        app.update();
        assert_eq!(
            app.world()
                .resource::<DiscoveryControl>()
                .activation(reporter),
            ReporterActivation::Enabled,
            "the RoleAwaiting edge for an unresolved covered claim must enable the reporter"
        );

        app.update();
        assert_eq!(
            scans.load(Ordering::Relaxed),
            1,
            "the enable must carry one requested run, collected on the next frame"
        );

        Ok(())
    }

    #[test]
    fn a_claim_no_reporter_covers_arms_nothing() -> Result<(), Box<dyn Error>> {
        let (mut app, reporter, scans) = app_with_disabled_reporter_covering(DeviceKind::Display);
        app.world_mut()
            .resource_mut::<Bindings>()
            .register(unresolved_camera_binding("camera-tool")?)?;

        app.update();
        app.update();
        assert_eq!(
            app.world()
                .resource::<DiscoveryControl>()
                .activation(reporter),
            ReporterActivation::Disabled,
            "a display-only reporter proves nothing about a camera key and must stay disabled"
        );
        assert_eq!(scans.load(Ordering::Relaxed), 0);

        Ok(())
    }
}