Skip to main content

openlogi_device/
inventory.rs

1//! Enumerate connected HID++ receivers and their paired devices.
2
3use std::{
4    collections::{HashMap, HashSet},
5    hash::Hash,
6    sync::Arc,
7    time::Duration,
8};
9
10use futures_concurrency::future::Join as _;
11use hidpp::channel::HidppChannel;
12use openlogi_core::device::DeviceInventory;
13use thiserror::Error;
14use tokio::time::timeout;
15use tracing::{debug, warn};
16
17use crate::ChannelRegistry;
18use crate::backend::{BackendError, HidBackend, NodeId, NodeInfo};
19use crate::channel::route::{DeviceRoute, is_receiver_pid};
20use ledger::NodeLedger;
21
22mod cache;
23mod features;
24pub mod hotplug;
25mod ledger;
26mod mappings;
27pub mod persist;
28mod probe;
29pub mod standalone;
30
31use cache::{CACHE_MISS_GRACE, CacheKey, CacheOutcome, Cached};
32use persist::{ProbeCacheSnapshot, ProbeCacheStore};
33use probe::{NodeProbe, probe_one};
34
35/// How long to wait for device-arrival event bursts before assuming the
36/// receiver has finished reporting. MX Master 4 (and other devices that may
37/// be asleep) need a generous window to wake and respond to the arrival
38/// ping; we err on the side of waiting.
39const ARRIVAL_DRAIN: Duration = Duration::from_millis(1500);
40
41/// Maximum number of pairing slots a Bolt receiver supports. We iterate this
42/// range to surface paired-but-offline devices that won't fire arrival events.
43const MAX_BOLT_SLOTS: u8 = 6;
44
45/// Upper bound on probing one HID node. `hidpp`'s request/response has no
46/// timeout of its own, so without this a single unresponsive (e.g. asleep)
47/// device wedges the whole enumeration — and the GUI runs `enumerate` on a
48/// polling watcher, so a permanent hang would stall every later refresh.
49///
50/// A timed-out node is skipped and re-probed on the next watcher tick (~2 s),
51/// and the first probe usually wakes the device so the retry succeeds fast.
52/// Slots are probed concurrently on both receiver paths, so a receiver's worst
53/// case is the 1.5 s arrival drain plus a single slot's [`BOLT_SLOT_PROBE`] /
54/// [`UNIFYING_SLOT_PROBE`] — not their sum — plus, on Bolt only, the
55/// sequential pairing-register pass that precedes the slot walk. This stays
56/// comfortably above that, so awake devices never trip it.
57///
58/// Sized for the Bluetooth-direct feature walk, the long pole: a ~35-entry
59/// table over a link that drops individual reports, which `hidpp::device`
60/// re-asks for per entry. At 6 s one lost report consumed the whole budget and
61/// the walk was abandoned mid-table, surfacing as a mouse that never appeared.
62const PROBE_BUDGET: Duration = Duration::from_secs(25);
63
64/// Probe budget for receiver nodes (Bolt/Unifying/Lightspeed dongles).
65///
66/// The 25 s [`PROBE_BUDGET`] is sized for Bluetooth-direct feature walks that
67/// receivers never perform. Keeping the receiver budget tighter matters
68/// because a full-budget timeout is also the detection path for a channel
69/// whose input-report delivery died (observed on macOS with concurrent opens
70/// of the same node: requests keep being written and answered, but the
71/// replies are delivered only to the other open handle). Until the channel is
72/// replaced every write on it stalls — DPI, SmartShift, ring haptics — so
73/// this budget bounds that outage.
74///
75/// It must still fit a receiver probe's real worst case, which is NOT the
76/// millisecond register reads but a paired device's full HID++ 2.0 feature
77/// walk: 1.5 s arrival drain + the sequential pairing-register pass + one
78/// slot's [`BOLT_SLOT_PROBE`] (10 s). 6 s proved too tight — a legitimate
79/// deep walk tripped the dead-delivery eviction, the surfaced-empty inventory
80/// tore down capture plans, and a pinned stale channel Arc then deadlocked
81/// recovery (dead buttons until restart). 13 s clears the honest worst case.
82const RECEIVER_PROBE_BUDGET: Duration = Duration::from_secs(13);
83
84/// Per-slot budget for the HID++ 2.0 feature walk on a Unifying paired device.
85///
86/// Unifying wireless round-trips are slower than Bolt BTLE: some devices (e.g.
87/// K540) take ~3 s for the version ping to return. Running multiple slow slots
88/// concurrently can still consume the full PROBE_BUDGET and get cancelled
89/// mid-walk — the probe returns nothing rather than partial features.  A
90/// per-slot cap ensures each slot's feature walk is bounded independently of
91/// how many other slots are being probed at the same time.  A timed-out slot
92/// still surfaces in the inventory (kind + wpid from the arrival event) — it
93/// just lacks capabilities / battery until the next tick.
94const UNIFYING_SLOT_PROBE: Duration = Duration::from_millis(3500);
95
96/// Per-slot budget for the HID++ 2.0 feature walk on a Bolt paired device.
97///
98/// Bounds a single device that stops answering its feature-walk reads (seen on
99/// a recent macOS IOHID stack with a new MX Master 4) so it falls back to its
100/// cached / identity-only data instead of pinning its slot future forever
101/// (#218). Slots walk *concurrently* (mirroring the Unifying path), so this
102/// budget covers the slowest single slot rather than dividing [`PROBE_BUDGET`]
103/// across the slot count. A healthy walk is not always fast either: a
104/// feature-rich device enumerates a large table one round-trip per feature
105/// (the MX Master 4's 45 features take ~1–1.6 s over Bolt even awake), and on
106/// high-latency USB paths (a Bolt receiver behind a KVM's USB emulation) it
107/// takes several seconds — the previous 3 s cap starved every slot there, so a
108/// newly paired device could never acquire model info at all. 10 s is generous
109/// headroom for degraded-but-alive paths while still fitting [`PROBE_BUDGET`]
110/// after the 1.5 s arrival drain and Bolt's sequential pairing-register pass.
111const BOLT_SLOT_PROBE: Duration = Duration::from_secs(10);
112
113/// Errors raised while enumerating HID++ devices.
114#[derive(Debug, Error)]
115pub enum InventoryError {
116    /// Underlying HID backend error.
117    #[error("HID transport error")]
118    Hid(#[from] BackendError),
119    /// More than one indistinguishable standalone raw-HID node was found.
120    #[error("multiple indistinguishable standalone raw HID devices found")]
121    AmbiguousRawDevice,
122}
123
124/// Stateful device enumerator: holds the per-device probe cache so the polling
125/// watcher reuses immutable data across ticks instead of re-handshaking every
126/// device every ~2s. One-shot callers use the [`enumerate`] free function, which
127/// runs against a fresh (empty) cache.
128pub struct Enumerator {
129    /// The HID stack this enumerator walks. `openlogi-hid` supplies this
130    /// host's; tests and other hosts supply their own.
131    backend: Arc<dyn HidBackend>,
132    cache: HashMap<CacheKey, Cached>,
133    /// Consecutive ticks each cached device has been missing, for grace-period
134    /// eviction.
135    misses: HashMap<CacheKey, u8>,
136    /// Open HID++ channels reused across ticks, keyed by OS node id. Opening (and
137    /// tearing down) a device every ~2s tick is the churn issue #99 is about —
138    /// each open also leaks an `io_service_t` in async-hid's macOS backend — so a
139    /// steadily-connected node is opened once here and reused until it
140    /// disconnects.
141    channels: ChannelCache<NodeId, CachedChannel>,
142    /// Per-node last-good inventory + consecutive-failure counts: replays a
143    /// node's snapshot through transient probe failures and decides when its
144    /// cached channel must be dropped and reopened (see [`crate::inventory::ledger`]).
145    ledger: NodeLedger<NodeId>,
146    /// Optional publication sink used by the persistent Agent watcher. One-shot
147    /// callers keep this `None` and retain the route-opening library behavior.
148    registry: Option<ChannelRegistry>,
149    tick: u64,
150    /// Where the immutable probe cache is kept across restarts, `None` for a
151    /// memory-only enumerator (one-shot CLI calls, tests).
152    store: Option<Arc<dyn ProbeCacheStore>>,
153    /// Whether the persistable cache content changed since the last save —
154    /// fresh full probes and evictions, not per-tick battery refreshes.
155    cache_dirty: bool,
156}
157
158/// An open channel to a receiver / direct-device HID node, held across
159/// `enumerate` ticks. Evicting it (on disconnect, or when the `Enumerator`
160/// drops) closes the device and joins the channel's read thread via
161/// [`HidppChannel`]'s `Drop`.
162struct CachedChannel {
163    info: NodeInfo,
164    channel: Arc<HidppChannel>,
165}
166
167struct PreparedNodes {
168    active: Vec<(NodeInfo, Arc<HidppChannel>)>,
169    open_failures: Vec<NodeId>,
170    retiring: Vec<NodeId>,
171}
172
173/// Disjoint active and retiring channels, generic so ownership transitions can
174/// be tested without constructing a platform HID node.
175struct ChannelCache<Node, Channel> {
176    active: HashMap<Node, Channel>,
177    retiring: HashMap<Node, Channel>,
178}
179
180impl<Node, Channel> Default for ChannelCache<Node, Channel> {
181    fn default() -> Self {
182        Self {
183            active: HashMap::new(),
184            retiring: HashMap::new(),
185        }
186    }
187}
188
189impl<Node: Eq + Hash + Clone, Channel> ChannelCache<Node, Channel> {
190    fn get(&self, node: &Node) -> Option<&Channel> {
191        self.active.get(node)
192    }
193
194    fn insert(&mut self, node: Node, channel: Channel) {
195        debug_assert!(!self.retiring.contains_key(&node));
196        self.active.insert(node, channel);
197    }
198
199    fn retire_node(&mut self, node: &Node) -> Option<&Channel> {
200        let channel = self.active.remove(node)?;
201        // Overwrite rather than keep an older retirement. Holding a node in
202        // both maps is a bug `insert` only debug-asserts against, and the
203        // caller uses what comes back to release *this* channel's cache pin —
204        // handed the stale one, it would clear the wrong pointer and leave the
205        // real pin in place, which is what blocks a node from reopening.
206        self.retiring.insert(node.clone(), channel);
207        self.retiring.get(node)
208    }
209
210    /// Whether this node may be opened during the current tick. A quiescent
211    /// retirement is dropped here, but opening remains deferred to a later tick.
212    fn prepare_open(&mut self, node: &Node, is_quiescent: impl FnOnce(&Channel) -> bool) -> bool {
213        let Some(channel) = self.retiring.get(node) else {
214            return true;
215        };
216        if is_quiescent(channel) {
217            self.retiring.remove(node);
218        }
219        false
220    }
221
222    fn retire_absent(&mut self, seen: &HashSet<Node>, mut on_retire: impl FnMut(&Channel)) {
223        let absent = self
224            .active
225            .keys()
226            .filter(|node| !seen.contains(*node))
227            .cloned()
228            .collect::<Vec<_>>();
229        for node in absent {
230            if let Some(channel) = self.retire_node(&node) {
231                on_retire(channel);
232            }
233        }
234    }
235
236    fn reap_absent(&mut self, seen: &HashSet<Node>, is_quiescent: impl Fn(&Channel) -> bool) {
237        self.retiring
238            .retain(|node, channel| seen.contains(node) || !is_quiescent(channel));
239    }
240
241    #[cfg(test)]
242    fn is_retiring(&self, node: &Node) -> bool {
243        self.retiring.contains_key(node)
244    }
245}
246
247fn routes_for_inventories(inventories: &[DeviceInventory]) -> Vec<DeviceRoute> {
248    inventories
249        .iter()
250        .flat_map(|inventory| {
251            inventory
252                .paired
253                .iter()
254                .filter_map(|paired| DeviceRoute::device_route_for(inventory, paired.slot))
255        })
256        .collect()
257}
258
259fn settle_unhealthy_node<Node: Eq + Hash + Clone>(
260    ledger: &mut NodeLedger<Node>,
261    node: &Node,
262    all_complete: &mut bool,
263    all_healthy: &mut bool,
264) -> Option<DeviceInventory> {
265    *all_complete = false;
266    *all_healthy = false;
267    ledger.settle(node, false, None).inventory
268}
269
270/// Enumerate all Logitech HID++ receivers visible to the current process and
271/// the devices paired to each.
272///
273/// Combines two data sources per receiver:
274///
275/// - `trigger_device_arrival` events — the only path to a device's wireless
276///   PID in hidpp 0.2 (the `wpid` field on `BoltDevicePairingInformation` is
277///   private). Only online, responsive devices show up here.
278/// - `get_device_pairing_information` polled per slot — covers paired-but-
279///   offline devices (sleeping mice, devices on a different host) that the
280///   arrival ping doesn't wake. No wpid for these.
281///
282/// We merge the two so an MX Master that's been asleep still shows up with
283/// its codename and kind even before you click it.
284pub async fn enumerate(
285    backend: Arc<dyn HidBackend>,
286) -> Result<Vec<DeviceInventory>, InventoryError> {
287    // The polling [`Enumerator`] keeps a per-node ledger across ticks, so a
288    // transient probe miss replays the node's last good inventory. A one-shot
289    // caller (CLI `list` / `diag`) builds a fresh `Enumerator` whose ledger is
290    // empty, so a miss has nothing to replay and would surface as an empty or
291    // partial list — the two isolated runs in #218 read 3 devices and 0. Retry a
292    // few times instead, reusing the same enumerator so its ledger accumulates a
293    // snapshot a later attempt can replay and the opened channel stays warm.
294    // #226's 5 s request timeout inside `HidppChannel::send` makes a dead probe
295    // fail fast, so a short bounded retry is cheap. Some transports can answer
296    // while still yielding a short device set (for example, a Unifying arrival
297    // event landing just after the drain window). When every node answered this
298    // cycle but that healthy pass is still short, two identical inventories mean
299    // the expected stable Unifying offline drain has settled. A failed/timed-out
300    // probe must keep using the full retry budget so the next attempt can reopen
301    // the channel and recover.
302    let mut enumerator = Enumerator::with_backend(backend);
303    let mut previous_inventories: Option<Vec<DeviceInventory>> = None;
304    let mut attempt = 1u8;
305    loop {
306        let (inventories, all_complete, all_healthy) =
307            enumerator.enumerate_reporting_completeness().await?;
308        if one_shot_should_stop(
309            previous_inventories.as_deref(),
310            &inventories,
311            all_complete,
312            all_healthy,
313            attempt,
314        ) {
315            return Ok(inventories);
316        }
317        debug!(
318            attempt,
319            all_complete,
320            all_healthy,
321            "one-shot enumerate inventory incomplete or still changing — retrying"
322        );
323        // Only a healthy pass is valid evidence for the unchanged-inventory
324        // stop, so the equality check below only ever compares two consecutive
325        // healthy snapshots. A failed/timed-out probe (replayed last-good or
326        // partial live result) is cleared so it can't count as one of the two
327        // "stable" reads and short-circuit a later healthy-but-short pass.
328        previous_inventories = if all_healthy { Some(inventories) } else { None };
329        tokio::time::sleep(ONESHOT_RETRY_DELAY).await;
330        attempt += 1;
331    }
332}
333
334/// Stop the one-shot retry loop when the snapshot is complete, when a healthy
335/// but short pass has stabilized (the expected Unifying offline-drain case), or
336/// when the explicit attempt cap is reached. An unchanged inventory from a
337/// failed probe is not stable evidence; it must keep retrying until the cap.
338fn one_shot_should_stop(
339    previous: Option<&[DeviceInventory]>,
340    current: &[DeviceInventory],
341    all_complete: bool,
342    all_healthy: bool,
343    attempt: u8,
344) -> bool {
345    all_complete
346        || (all_healthy && previous.is_some_and(|previous| previous == current))
347        || attempt >= ONESHOT_ATTEMPTS
348}
349
350/// Attempts a one-shot [`enumerate`] makes before returning whatever it last
351/// read, when an inventory keeps coming back incomplete or changing.
352const ONESHOT_ATTEMPTS: u8 = 4;
353
354/// Delay between one-shot [`enumerate`] retries. A first probe usually wakes an
355/// asleep device, so a short pause lets the next attempt read it cleanly.
356const ONESHOT_RETRY_DELAY: Duration = Duration::from_millis(300);
357
358/// Nodes that remain valid for this tick: everything the OS enumerated plus
359/// cached channels whose open transport still reports a live connection.
360fn retained_nodes<K>(
361    enumerated: &HashSet<K>,
362    cached_channels: impl IntoIterator<Item = (K, bool)>,
363) -> HashSet<K>
364where
365    K: Clone + Eq + Hash,
366{
367    let mut retained = enumerated.clone();
368    retained.extend(
369        cached_channels
370            .into_iter()
371            .filter_map(|(node, connected)| connected.then_some(node)),
372    );
373    retained
374}
375
376/// Add cached channels omitted by this OS enumeration while their open
377/// transport still reports a live connection.
378fn append_live_cached_channels(
379    nodes: &mut HashSet<NodeId>,
380    channels: &ChannelCache<NodeId, CachedChannel>,
381    active: &mut Vec<(NodeInfo, Arc<HidppChannel>)>,
382) {
383    let retained = retained_nodes(
384        nodes,
385        channels
386            .active
387            .iter()
388            .map(|(node, open)| (node.clone(), open.channel.is_connected())),
389    );
390    for node in retained.difference(nodes) {
391        if let Some(open) = channels.get(node) {
392            debug!(
393                ?node,
394                name = %open.info.name,
395                "OS enumeration omitted a live HID node; probing cached channel"
396            );
397            active.push((open.info.clone(), Arc::clone(&open.channel)));
398        }
399    }
400    *nodes = retained;
401}
402
403impl Enumerator {
404    /// An enumerator that walks `backend` — this host's HID stack, a scripted
405    /// device tree in tests, or another host's.
406    #[must_use]
407    pub fn with_backend(backend: Arc<dyn HidBackend>) -> Self {
408        Self {
409            backend,
410            cache: HashMap::new(),
411            misses: HashMap::new(),
412            channels: ChannelCache::default(),
413            ledger: NodeLedger::default(),
414            registry: None,
415            tick: 0,
416            store: None,
417            cache_dirty: false,
418        }
419    }
420
421    /// Publish this enumerator's already-open channels into `registry` after
422    /// each settled inventory tick.
423    #[must_use]
424    pub fn with_registry(mut self, registry: ChannelRegistry) -> Self {
425        self.registry = Some(registry);
426        self
427    }
428
429    /// Warm-start this enumerator's immutable probe cache from `store`, and
430    /// write it back there whenever its persistable content changes.
431    ///
432    /// A modifier rather than a constructor: persistence is orthogonal to the
433    /// channel registry and the backend, so an enumerator can carry all three.
434    #[must_use]
435    pub fn with_probe_cache(mut self, store: Arc<dyn ProbeCacheStore>) -> Self {
436        let cache = store.load().into_entries();
437        if !cache.is_empty() {
438            debug!(entries = cache.len(), "probe cache warm-started");
439        }
440        self.cache.extend(cache);
441        self.store = Some(store);
442        self
443    }
444
445    async fn prepare_nodes(
446        &mut self,
447        backend: &dyn HidBackend,
448        candidates: Vec<NodeInfo>,
449    ) -> PreparedNodes {
450        let mut active = Vec::new();
451        let mut seen_nodes = HashSet::new();
452        let mut open_failures = Vec::new();
453        let mut retiring = Vec::new();
454        for info in candidates {
455            let node = info.id.clone();
456            seen_nodes.insert(node.clone());
457            if !self
458                .channels
459                .prepare_open(&node, |cached| Arc::strong_count(&cached.channel) == 1)
460            {
461                debug!("node still retiring — waiting for its channel's remaining users to drop");
462                retiring.push(node);
463                continue;
464            }
465            if let Some(open) = self.channels.get(&node) {
466                active.push((open.info.clone(), Arc::clone(&open.channel)));
467                continue;
468            }
469            match backend.open_hidpp(&info).await {
470                Ok(Some(channel)) => {
471                    self.channels.insert(
472                        node,
473                        CachedChannel {
474                            info: info.clone(),
475                            channel: Arc::clone(&channel),
476                        },
477                    );
478                    active.push((info, channel));
479                }
480                Ok(None) => {}
481                Err(e) => {
482                    warn!(error = ?e, "failed to open HID++ channel — retrying next tick");
483                    open_failures.push(node);
484                }
485            }
486        }
487
488        // IOHIDManager can temporarily omit a Bluetooth device's vendor HID++
489        // collection while its already-open handle and ordinary mouse link are
490        // still live. Keep probing that cached channel instead of turning one
491        // incomplete OS snapshot into an offline device and stopping capture.
492        append_live_cached_channels(&mut seen_nodes, &self.channels, &mut active);
493
494        if let Some(registry) = &self.registry {
495            registry.retain_nodes(&seen_nodes);
496        }
497        self.channels.retire_absent(&seen_nodes, |cached| {
498            crate::write::clear_haptic_feature_cache_for(&cached.channel);
499        });
500        self.channels.reap_absent(&seen_nodes, |cached| {
501            Arc::strong_count(&cached.channel) == 1
502        });
503        self.ledger.retain_nodes(&seen_nodes);
504
505        PreparedNodes {
506            active,
507            open_failures,
508            retiring,
509        }
510    }
511
512    /// Write the cache through to its store when the persistable content
513    /// changed this tick. Best-effort: a failed write is logged and retried on
514    /// the next dirty tick.
515    fn flush_cache(&mut self) {
516        if !self.cache_dirty {
517            return;
518        }
519        let Some(store) = &self.store else {
520            return;
521        };
522        match store.save(&ProbeCacheSnapshot::of(&self.cache)) {
523            Ok(()) => self.cache_dirty = false,
524            Err(e) => warn!(error = %e, "failed to persist probe cache"),
525        }
526    }
527
528    /// One enumeration pass, reusing the cache from prior passes. Probes every
529    /// HID candidate concurrently (so one asleep node that burns the whole
530    /// `PROBE_BUDGET` can't stall the others), reusing each device's cached
531    /// immutable data when it's present and fresh.
532    ///
533    /// A node the OS still lists but whose probe fails (receiver registers
534    /// unanswered, probe timeout, open failure) is **not** reported as absent:
535    /// its last completed inventory is replayed for a bounded grace and its
536    /// channel is reopened, so a transient HID++ glitch can't masquerade as
537    /// "no devices" (#218) — see the node ledger.
538    pub async fn enumerate(&mut self) -> Result<Vec<DeviceInventory>, InventoryError> {
539        self.enumerate_reporting_completeness()
540            .await
541            .map(|(inv, _, _)| inv)
542    }
543
544    /// [`Self::enumerate`] plus whether every probed node produced a complete
545    /// enough snapshot for the one-shot caller to stop early, and whether every
546    /// probed node answered this cycle. Completeness is separate from per-node
547    /// health: a node can answer cleanly enough for the ledger to accept its
548    /// live inventory while still reporting a known count/list shortfall that
549    /// the one-shot retry should give one more chance to settle. Only healthy
550    /// shortfalls can use the unchanged-inventory early stop; failed probes must
551    /// run through the retry budget so a later attempt can recover.
552    async fn enumerate_reporting_completeness(
553        &mut self,
554    ) -> Result<(Vec<DeviceInventory>, bool, bool), InventoryError> {
555        self.tick = self.tick.wrapping_add(1);
556        let tick = self.tick;
557        let backend = Arc::clone(&self.backend);
558        let candidates = backend.enumerate_hidpp().await?;
559        debug!(count = candidates.len(), "HID++ candidate interfaces");
560
561        // Reuse an open channel per node, opening only when no active or
562        // retiring connection owns that OS node.
563        let PreparedNodes {
564            active,
565            open_failures,
566            retiring: retiring_nodes,
567        } = self.prepare_nodes(&*backend, candidates).await;
568
569        // Probe each open channel concurrently, sharing `&cache` read-only;
570        // updates are collected and applied afterwards (no `RefCell`).
571        let results = {
572            let cache = &self.cache;
573            active
574                .into_iter()
575                .map(|(info, channel)| async move {
576                    let node = info.id.clone();
577                    // Receivers answer register reads over local USB in
578                    // milliseconds; only direct (esp. Bluetooth) devices need
579                    // the long feature-walk budget. A tight receiver budget
580                    // bounds the outage when its channel's input-report
581                    // delivery dies (writes accepted, replies never seen —
582                    // observed on macOS with concurrent opens of one node).
583                    let receiver = is_receiver_pid(info.product_id);
584                    let budget = if receiver {
585                        RECEIVER_PROBE_BUDGET
586                    } else {
587                        PROBE_BUDGET
588                    };
589                    let probe =
590                        timeout(budget, probe_one(info, Arc::clone(&channel), cache, tick)).await;
591                    (node, channel, probe, budget, receiver)
592                })
593                .collect::<Vec<_>>()
594                .join()
595                .await
596        };
597
598        let mut inventories = Vec::new();
599        let mut outcomes = Vec::new();
600        // Aggregates for the one-shot retry. `all_complete` can stop
601        // immediately; `all_healthy` gates the unchanged-inventory shortcut so
602        // failed probes keep retrying. The ledger's own per-node replay is
603        // governed by `probe.healthy`.
604        let mut all_complete = true;
605        let mut all_healthy = true;
606        for (node, channel, result, budget, receiver) in results {
607            let probe = if let Ok(probe) = result {
608                probe
609            } else {
610                // The probe burned the whole budget — an asleep direct device,
611                // or a channel whose input-report delivery died (writes
612                // accepted, replies never seen). Either way: "couldn't
613                // check", not "nothing there".
614                warn!(
615                    ?budget,
616                    receiver, "device probe timed out — treating as a failed probe"
617                );
618                NodeProbe::failed()
619            };
620            all_complete &= probe.complete;
621            all_healthy &= probe.healthy;
622            outcomes.extend(probe.outcomes);
623            let settled = self.ledger.settle(&node, probe.healthy, probe.inventory);
624            // Every node waits for the ledger's consecutive-failure threshold,
625            // receivers included. One full-budget timeout is not evidence of
626            // dead delivery: [`RECEIVER_PROBE_BUDGET`] leaves barely a second
627            // over its own documented worst case, so a legitimate deep walk
628            // plus a single lost reply (5 s `SEND_RESPONSE_TIMEOUT`) already
629            // exceeds it. Evicting on that unpublishes *every* device behind
630            // the receiver — a Bolt publishes all six slots under one node —
631            // and tears down each one's capture plan. A channel whose delivery
632            // really is dead times out again on the next tick and is replaced
633            // then, with the ledger replaying its last-good inventory
634            // meanwhile, so nothing disappears from the GUI in between.
635            if settled.evict_channel {
636                if let Some(registry) = &self.registry {
637                    registry.remove_node(&node);
638                }
639                if let Some(cached) = self.channels.retire_node(&node) {
640                    // Release the haptic cache's pin on this channel NOW —
641                    // waiting for the next haptic route-miss deadlocks when
642                    // capture dies with it (see clear_haptic_feature_cache_for).
643                    crate::write::clear_haptic_feature_cache_for(&cached.channel);
644                    warn!("node probe keeps failing — retiring its channel before reopen");
645                }
646            } else if let Some(registry) = &self.registry {
647                let routes = settled
648                    .inventory
649                    .as_ref()
650                    .map_or_else(Vec::new, |inventory| {
651                        routes_for_inventories(std::slice::from_ref(inventory))
652                    });
653                if routes.is_empty() {
654                    registry.remove_node(&node);
655                } else {
656                    registry.replace_node(node.clone(), routes, channel);
657                }
658            }
659            inventories.extend(settled.inventory);
660        }
661        // A listed node whose old connection is still retiring is an unhealthy
662        // probe, not a disconnect: preserve the ledger's normal replay grace.
663        for node in retiring_nodes {
664            inventories.extend(settle_unhealthy_node(
665                &mut self.ledger,
666                &node,
667                &mut all_complete,
668                &mut all_healthy,
669            ));
670        }
671        // Nodes that wouldn't open this tick still replay their last snapshot
672        // (they have no cached channel to evict).
673        for node in open_failures {
674            inventories.extend(settle_unhealthy_node(
675                &mut self.ledger,
676                &node,
677                &mut all_complete,
678                &mut all_healthy,
679            ));
680        }
681
682        let seen_keys = self.apply_outcomes(outcomes);
683        self.evict_unseen(&seen_keys);
684        self.flush_cache();
685        Ok((inventories, all_complete, all_healthy))
686    }
687
688    /// Fold this tick's probe outcomes into the cache, returning the keys seen
689    /// so [`Self::evict_unseen`] can age out the rest.
690    fn apply_outcomes(&mut self, outcomes: Vec<CacheOutcome>) -> HashSet<CacheKey> {
691        let mut seen_keys = HashSet::new();
692        for outcome in outcomes {
693            match outcome {
694                CacheOutcome::Fresh(key, cached) => {
695                    seen_keys.insert(key.clone());
696                    // A completed full probe of a persistable device is worth
697                    // writing through; battery `Update`s are not (they would
698                    // rewrite the file every tick for a value that is re-read
699                    // live anyway), and neither are keys `persist::save`
700                    // filters out — dirtying on those would rewrite an
701                    // unchanged file on every refresh of a direct-only system.
702                    self.cache_dirty |= persist::is_persistable(&key);
703                    self.cache.insert(key, cached);
704                }
705                CacheOutcome::Update(key, cached) => {
706                    seen_keys.insert(key.clone());
707                    self.cache.insert(key, cached);
708                }
709                CacheOutcome::Seen(key) => {
710                    seen_keys.insert(key);
711                }
712                CacheOutcome::Unkeyed => {}
713            }
714        }
715        seen_keys
716    }
717
718    /// Drop cache entries for devices not seen this tick, after a short grace so
719    /// a transient receiver timeout doesn't discard a still-present device.
720    fn evict_unseen(&mut self, seen_keys: &HashSet<CacheKey>) {
721        for key in seen_keys {
722            self.misses.remove(key);
723        }
724        let missing: Vec<CacheKey> = self
725            .cache
726            .keys()
727            .filter(|k| !seen_keys.contains(*k))
728            .cloned()
729            .collect();
730        for key in missing {
731            let misses = self.misses.entry(key.clone()).or_insert(0);
732            *misses += 1;
733            if *misses > CACHE_MISS_GRACE {
734                self.cache.remove(&key);
735                self.misses.remove(&key);
736                self.cache_dirty |= persist::is_persistable(&key);
737            }
738        }
739    }
740}
741
742#[cfg(test)]
743mod tests;