phoxal 0.67.0

Phoxal - production-oriented autonomous robot framework: the one framework library, holding the runtime engine, the api contract tree, the typed bus, the canonical model, and the bundle.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Participant presence built on Zenoh Liveliness.
//!
//! Each token is keyed by execution root, participant id, and producer
//! identity. Observers receive exact per-producer events and can aggregate them
//! by the stable participant id for present/not-present UI state. Because the
//! root is execution-scoped, a previous run's tokens are on different keys
//! entirely and can never be mistaken for the current run's.

use crate::identity::{ParticipantId, ProducerId};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use zenoh::key_expr::OwnedKeyExpr;
use zenoh::sample::SampleKind;

use crate::bus::error::{BusError, KeyProblem, Result};
use crate::bus::metadata::ParticipantSourceIdentity;
use crate::bus::session::{BusHandle, BusOwner};

pub(crate) const PARTICIPANT_LIVELINESS_PREFIX: &str = "liveliness/participants";

/// One stable participant Ready key in the Zenoh Liveliness transport.
#[derive(Clone, Debug, PartialEq, Eq)]
struct ParticipantReadyKey {
    key: OwnedKeyExpr,
    source: ParticipantSourceIdentity,
}

impl ParticipantReadyKey {
    pub(crate) fn for_bus(bus: &BusHandle) -> Result<Self> {
        let participant = bus
            .participant()
            .ok_or_else(|| BusError::invalid_key("", KeyProblem::Empty))?;
        Self::new(bus.root(), participant.as_str(), bus.producer())
    }

    /// Build and validate a producer-qualified participant key below an
    /// existing execution root.
    fn new(root: &str, participant: impl Into<String>, producer: ProducerId) -> Result<Self> {
        validate_root(root)?;
        let participant = participant.into();
        validate_participant(&participant)?;
        let participant = ParticipantId::new(participant.clone())
            .map_err(|_| BusError::invalid_key(participant, KeyProblem::NotOneSegment))?;
        let source = ParticipantSourceIdentity::new(participant, producer);
        let raw = format!(
            "{root}/{PARTICIPANT_LIVELINESS_PREFIX}/{}/{}",
            source.participant, source.producer
        );
        let key = OwnedKeyExpr::new(raw.clone())
            .map_err(|error| BusError::not_a_key_expression(&raw, error))?;
        Ok(Self { key, source })
    }

    /// The complete execution-rooted Zenoh key.
    fn as_str(&self) -> &str {
        self.key.as_str()
    }

    /// Participant id encoded in the key.
    #[cfg(test)]
    fn participant(&self) -> &ParticipantId {
        &self.source.participant
    }

    /// Producer identity encoded in the key.
    #[cfg(test)]
    fn producer(&self) -> ProducerId {
        self.source.producer
    }

    /// The participant/source pair encoded in the key.
    fn source(&self) -> &ParticipantSourceIdentity {
        &self.source
    }

    /// Parse a concrete participant key emitted below `root`.
    fn parse(root: &str, key: &str) -> Option<Self> {
        let suffix = key.strip_prefix(root)?.strip_prefix('/')?;
        let suffix = suffix
            .strip_prefix(PARTICIPANT_LIVELINESS_PREFIX)?
            .strip_prefix('/')?;
        let (participant, producer) = suffix.split_once('/')?;
        if producer.contains('/') {
            return None;
        }
        Self::new(root, participant, ProducerId::parse(producer).ok()?).ok()
    }

    /// Wildcard selector used by an execution-scoped observer.
    fn selector(root: &str) -> Result<OwnedKeyExpr> {
        validate_root(root)?;
        let selector = format!("{root}/{PARTICIPANT_LIVELINESS_PREFIX}/*/*");
        OwnedKeyExpr::new(selector.clone())
            .map_err(|error| BusError::not_a_key_expression(&selector, error))
    }

    /// Selector for one participant's producer-qualified Ready keys.
    fn participant_selector(root: &str, participant: &ParticipantId) -> Result<OwnedKeyExpr> {
        validate_root(root)?;
        validate_participant(participant.as_str())?;
        let selector = format!("{root}/{PARTICIPANT_LIVELINESS_PREFIX}/{participant}/*");
        OwnedKeyExpr::new(selector.clone())
            .map_err(|error| BusError::not_a_key_expression(&selector, error))
    }
}

/// Presence or absence of one Liveliness token.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LivelinessStatus {
    /// The token is declared.
    Alive,
    /// The token is not declared, or stopped being declared.
    Lost,
}

/// Whether one exact participant producer currently owns its Ready lease.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParticipantReadyStatus {
    /// The producer-qualified Ready lease is present.
    Ready,
    /// The producer-qualified Ready lease disappeared.
    Lost,
}

impl From<SampleKind> for ParticipantReadyStatus {
    fn from(kind: SampleKind) -> Self {
        match kind {
            SampleKind::Put => ParticipantReadyStatus::Ready,
            SampleKind::Delete => ParticipantReadyStatus::Lost,
        }
    }
}

impl From<SampleKind> for LivelinessStatus {
    fn from(kind: SampleKind) -> Self {
        match kind {
            SampleKind::Put => LivelinessStatus::Alive,
            SampleKind::Delete => LivelinessStatus::Lost,
        }
    }
}

/// A participant Ready change for one exact producer incarnation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParticipantReadyEvent {
    /// The stable participant role and exact process/session incarnation that
    /// declared Ready.
    pub source: ParticipantSourceIdentity,
    /// Whether that exact Ready lease appeared or disappeared.
    pub status: ParticipantReadyStatus,
}

impl ParticipantReadyEvent {
    /// The stable participant role represented by this event.
    pub fn participant(&self) -> &ParticipantId {
        &self.source.participant
    }

    /// The exact producer incarnation represented by this event.
    pub fn producer(&self) -> ProducerId {
        self.source.producer
    }
}

/// Keeps this participant producer's Ready lease alive until it is dropped.
pub struct ParticipantReadyToken {
    _token: zenoh::liveliness::LivelinessToken,
    source: ParticipantSourceIdentity,
}

/// Keeps one infrastructure-owned exact Liveliness key declared until drop.
/// Participant readiness uses [`ParticipantReadyToken`] instead; this token is
/// for execution-scoped authorities such as the supervisor presence lease.
pub struct KeyLivelinessToken {
    _token: zenoh::liveliness::LivelinessToken,
    relative_key: String,
}

impl KeyLivelinessToken {
    /// The exact key below this token's execution root.
    pub fn relative_key(&self) -> &str {
        &self.relative_key
    }
}

impl ParticipantReadyToken {
    /// The participant/source pair represented by this lease.
    pub fn source(&self) -> &ParticipantSourceIdentity {
        &self.source
    }

    /// The stable participant role represented by this lease.
    pub fn participant(&self) -> &ParticipantId {
        &self.source.participant
    }

    /// The exact producer incarnation represented by this lease.
    pub fn producer(&self) -> ProducerId {
        self.source.producer
    }
}

/// Keeps a history-enabled participant Ready observer declared until dropped.
pub struct ParticipantReadyObserver {
    _subscriber: zenoh::pubsub::Subscriber<()>,
}

/// A bounded runner-facing stream of exact participant Ready changes.
///
/// Zenoh invokes the observer callback outside the participant step loop.  The
/// callback therefore only attempts a bounded local enqueue; services drain
/// this value at their ordinary step boundary.  If the queue ever overflows,
/// [`Self::overflowed`] remains true and fixed-source consumers must fail
/// closed rather than act on an incomplete Ready set.
pub struct ParticipantReadyEvents {
    receiver: Mutex<mpsc::Receiver<ParticipantReadyEvent>>,
    _observer: ParticipantReadyObserver,
    overflowed: Arc<AtomicBool>,
}

impl ParticipantReadyEvents {
    /// Drain one Ready event without waiting.
    pub fn try_recv(&self) -> Option<ParticipantReadyEvent> {
        match self.receiver.lock() {
            Ok(mut receiver) => receiver.try_recv().ok(),
            Err(_) => {
                // A poisoned receiver means the exact Ready set can no
                // longer be trusted. Fixed-source consumers observe this as
                // overflow and stop admitting authority.
                self.overflowed.store(true, Ordering::Release);
                None
            }
        }
    }

    /// Whether the bounded observer queue dropped an event.
    pub fn overflowed(&self) -> bool {
        self.overflowed.load(Ordering::Acquire)
    }
}

/// Keeps an observation of one exact Liveliness key declared until it is
/// dropped, and carries the state that key was in when it was established.
pub struct KeyLivelinessObserver {
    _subscriber: zenoh::pubsub::Subscriber<()>,
    initial: LivelinessStatus,
}

impl KeyLivelinessObserver {
    /// The token's state at the moment this observation was established.
    ///
    /// A client that attached after the token was declared learns it is there
    /// from this, not from a change event that already happened.
    pub fn initial(&self) -> LivelinessStatus {
        self.initial
    }
}

impl BusOwner {
    /// Declare one exact execution-scoped infrastructure presence lease.
    ///
    /// The key must be a concrete relative key. This cannot mint participant
    /// Ready authority; that producer-qualified contract has its own method.
    #[allow(
        dead_code,
        reason = "the supervisor's own presence key, declared by the one profile that compiles a supervisor"
    )]
    pub async fn declare_liveliness_key(&self, relative_key: &str) -> Result<KeyLivelinessToken> {
        validate_relative_key(relative_key)?;
        if relative_key.starts_with(PARTICIPANT_LIVELINESS_PREFIX) {
            return Err(BusError::invalid_key(
                relative_key,
                KeyProblem::ReservedPrefix,
            ));
        }
        let bus = self.handle();
        let token = bus
            .session()?
            .liveliness()
            .declare_token(bus.full_key(relative_key))
            .await
            .map_err(|error| BusError::Transport(error.to_string()))?;
        Ok(KeyLivelinessToken {
            _token: token,
            relative_key: relative_key.to_string(),
        })
    }

    /// Declare this bus participant's Ready lease. Call only after setup succeeds.
    pub async fn declare_participant_ready(&self) -> Result<ParticipantReadyToken> {
        let bus = self.handle();
        self.declare_ready(ParticipantReadyKey::for_bus(&bus)?)
            .await
    }

    /// Declare a Ready lease for `participant` under this session's producer.
    ///
    /// One process may stand for several participants: the Webots controller is
    /// a single external bus client that simulates every component driver of a
    /// robot, and the supervisor watches presence per participant id. Without
    /// this, that one client could present at most one of the drivers it
    /// actually runs, and every other component would read as absent for the
    /// whole simulated execution. The producer stays this session's, so all the
    /// leases one client declares this way appear and disappear together with
    /// it, which is exactly the truth: they share a process.
    ///
    /// [`Self::declare_liveliness_key`] still refuses the participant prefix.
    /// That method mints infrastructure keys, and readiness is producer
    /// qualified; this is the one door onto another participant's Ready key, and
    /// it goes through the same key builder as a participant's own.
    #[allow(
        dead_code,
        reason = "delegated presence, declared by the one profile that compiles a simulator"
    )]
    pub async fn declare_participant_ready_as(
        &self,
        participant: &ParticipantId,
    ) -> Result<ParticipantReadyToken> {
        let bus = self.handle();
        self.declare_ready(ParticipantReadyKey::new(
            bus.root(),
            participant.as_str(),
            bus.producer(),
        )?)
        .await
    }

    async fn declare_ready(&self, key: ParticipantReadyKey) -> Result<ParticipantReadyToken> {
        let token = self
            .handle()
            .session()?
            .liveliness()
            .declare_token(key.as_str())
            .await
            .map_err(|error| BusError::Transport(error.to_string()))?;
        Ok(ParticipantReadyToken {
            _token: token,
            source: key.source().clone(),
        })
    }
}

impl BusHandle {
    /// Observe all exact participant Ready tokens below this execution root,
    /// delivering changes through a bounded local channel suitable for a
    /// participant step loop.
    pub async fn participant_ready_events(&self) -> Result<ParticipantReadyEvents> {
        self.participant_ready_events_with_selector(ParticipantReadyKey::selector(self.root())?)
            .await
    }

    /// Observe only the producer-qualified Ready keys for `participant`.
    /// Scoping the selector before the bounded local queue prevents unrelated
    /// participant churn from consuming the receiver's authority evidence.
    pub async fn participant_ready_events_for(
        &self,
        participant: &ParticipantId,
    ) -> Result<ParticipantReadyEvents> {
        self.participant_ready_events_with_selector(ParticipantReadyKey::participant_selector(
            self.root(),
            participant,
        )?)
        .await
    }

    /// Observe one participant's Ready keys directly on the transport
    /// callback. This is for ingress fences that must linearize Ready loss
    /// with a sample before the participant's next step.
    pub async fn observe_participant_ready_for(
        &self,
        participant: &ParticipantId,
        callback: impl Fn(ParticipantReadyEvent) + Send + Sync + 'static,
    ) -> Result<ParticipantReadyObserver> {
        self.observe_participant_ready_selector(
            ParticipantReadyKey::participant_selector(self.root(), participant)?,
            callback,
        )
        .await
    }

    async fn participant_ready_events_with_selector(
        &self,
        selector: OwnedKeyExpr,
    ) -> Result<ParticipantReadyEvents> {
        let (sender, receiver) = mpsc::channel(64);
        let overflowed = Arc::new(AtomicBool::new(false));
        let dropped = Arc::clone(&overflowed);
        let observer = self
            .observe_participant_ready_selector(selector, move |event| {
                if sender.try_send(event).is_err() {
                    dropped.store(true, Ordering::Release);
                }
            })
            .await?;
        Ok(ParticipantReadyEvents {
            receiver: Mutex::new(receiver),
            _observer: observer,
            overflowed,
        })
    }

    /// Observe participant appearance and disappearance, including tokens that
    /// were already live when this observer was declared.
    ///
    /// The callback runs on Zenoh's runtime and must not perform Zenoh network
    /// operations. Sending the event to a local channel or updating local state
    /// is appropriate.
    ///
    /// Callers that render stable participant presence must aggregate the exact
    /// per-producer events and consider the participant present while at least
    /// one producer remains live.
    pub async fn observe_participant_ready(
        &self,
        callback: impl Fn(ParticipantReadyEvent) + Send + Sync + 'static,
    ) -> Result<ParticipantReadyObserver> {
        let root = self.root().to_string();
        let selector = ParticipantReadyKey::selector(&root)?;
        self.observe_participant_ready_selector(selector, callback)
            .await
    }

    /// Observe the exact selector used by a participant-scoped Ready stream.
    async fn observe_participant_ready_selector(
        &self,
        selector: OwnedKeyExpr,
        callback: impl Fn(ParticipantReadyEvent) + Send + Sync + 'static,
    ) -> Result<ParticipantReadyObserver> {
        let root = self.root().to_string();
        let subscriber = self
            .session()?
            .liveliness()
            .declare_subscriber(selector)
            .history(true)
            .callback(move |sample| {
                if let Some(event) =
                    participant_event(&root, sample.key_expr().as_str(), sample.kind())
                {
                    callback(event);
                }
            })
            .await
            .map_err(|error| BusError::Transport(error.to_string()))?;
        Ok(ParticipantReadyObserver {
            _subscriber: subscriber,
        })
    }

    /// Observe one exact Liveliness key below this session's root.
    ///
    /// `relative_key` is a concrete key path below the execution root, for
    /// example `supervisor/identity`; the root is this session's, so an
    /// observer can never accidentally watch another execution's token.
    ///
    /// The returned observer carries the token's current state
    /// ([`KeyLivelinessObserver::initial`]) and `callback` receives every
    /// change after that. Statuses are levels, not edges: the subscriber is
    /// declared before the state is read, so a token that appears during that
    /// window is reported twice - once through the callback and once as the
    /// initial state - and a consumer must be indifferent to that.
    ///
    /// Reading that initial state is a query, and a query can fail. A failed
    /// reply is surfaced as [`BusError::Transport`] rather than reported as
    /// [`LivelinessStatus::Lost`]: "the query broke" and "the token is not
    /// there" lead a client to opposite conclusions, so they are never the same
    /// value. No reply at all does mean absent, and stays `Lost`.
    ///
    /// The callback runs on Zenoh's runtime and must not perform Zenoh network
    /// operations.
    pub async fn observe_liveliness_key(
        &self,
        relative_key: &str,
        callback: impl Fn(LivelinessStatus) + Send + Sync + 'static,
    ) -> Result<KeyLivelinessObserver> {
        validate_relative_key(relative_key)?;
        let raw = self.full_key(relative_key);
        let key = OwnedKeyExpr::new(raw.clone())
            .map_err(|error| BusError::not_a_key_expression(&raw, error))?;

        // The initial liveliness query may wait for replies. Keep one
        // admission lease across declaration, query, and receive so close
        // cannot race the long-lived receive path.
        let session = self.session()?;

        // Declared before the state is read, so a token lost in between is
        // reported by the subscriber rather than missed by both.
        let subscriber = session
            .liveliness()
            .declare_subscriber(key.clone())
            .callback(move |sample| callback(LivelinessStatus::from(sample.kind())))
            .await
            .map_err(|error| BusError::Transport(error.to_string()))?;

        let replies = session
            .liveliness()
            .get(key)
            .await
            .map_err(|error| BusError::Transport(error.to_string()))?;
        let mut initial = LivelinessStatus::Lost;
        while let Ok(reply) = replies.recv_async().await {
            match reply.result() {
                Ok(_) => initial = LivelinessStatus::Alive,
                // An error reply is a failed query, not an answer of "absent".
                // Folding it into `Lost` would report a token as gone on the
                // strength of the query having broken, which is exactly the
                // reading a client uses to decide the robot is not there.
                // Silence - the channel closing with no reply at all - genuinely
                // does mean absent and stays `Lost`.
                Err(error) => {
                    return Err(BusError::Transport(format!(
                        "the liveliness query for '{raw}' failed: {error:?}"
                    )));
                }
            }
        }

        Ok(KeyLivelinessObserver {
            _subscriber: subscriber,
            initial,
        })
    }
}

/// A liveliness key must be a concrete path below the execution root: a
/// wildcard would silently widen the observation to whatever else exists.
fn validate_relative_key(relative_key: &str) -> Result<()> {
    validate_concrete_path(relative_key)
}

fn participant_event(root: &str, key: &str, kind: SampleKind) -> Option<ParticipantReadyEvent> {
    let key = ParticipantReadyKey::parse(root, key)?;
    Some(ParticipantReadyEvent {
        source: key.source().clone(),
        status: ParticipantReadyStatus::from(kind),
    })
}

/// A participant id becomes exactly one key segment, so it may carry neither a
/// separator nor a wildcard. The typed `ParticipantId` constructor applies its
/// stricter topology grammar after this transport-specific diagnosis.
fn validate_participant(participant: &str) -> Result<()> {
    if participant.is_empty() {
        return Err(BusError::invalid_key(participant, KeyProblem::Empty));
    }
    if participant.contains('/') {
        return Err(BusError::invalid_key(
            participant,
            KeyProblem::NotOneSegment,
        ));
    }
    if participant.contains('*') {
        return Err(BusError::invalid_key(participant, KeyProblem::Wildcard));
    }
    Ok(())
}

fn validate_root(root: &str) -> Result<()> {
    validate_concrete_path(root)
}

fn validate_concrete_path(path: &str) -> Result<()> {
    if path.is_empty() || path.split('/').any(str::is_empty) {
        return Err(BusError::invalid_key(path, KeyProblem::Empty));
    }
    if path.contains('*') {
        return Err(BusError::invalid_key(path, KeyProblem::Wildcard));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    const ROOT: &str = "phoxal/ffffffffffffffffffffffffffffffff";

    /// A distinct deterministic test producer. Production sessions mint their
    /// producer through the bus owner, while tests name theirs explicitly.
    fn producer(value: u128) -> ProducerId {
        ProducerId::try_from((1_u128 << 124) | value).expect("a test producer is canonical")
    }

    #[test]
    fn key_builder_owns_validation_and_round_trips_identity() {
        let producer = producer(1);
        let key = ParticipantReadyKey::new(ROOT, "drive", producer).unwrap();
        assert_eq!(
            key.as_str(),
            format!("{ROOT}/liveliness/participants/drive/{producer}")
        );
        assert_eq!(
            ParticipantReadyKey::parse(ROOT, key.as_str()),
            Some(key.clone())
        );
        assert_eq!(key.participant().as_str(), "drive");
        assert_eq!(key.producer(), producer);
        assert!(ParticipantReadyKey::new(ROOT, "bad/id", producer).is_err());
        assert!(ParticipantReadyKey::new("phoxal/*", "drive", producer).is_err());
        assert!(
            ParticipantReadyKey::parse(
                ROOT,
                &format!("{ROOT}/liveliness/participants/drive/not-a-producer")
            )
            .is_none()
        );
    }

    #[test]
    fn participant_event_maps_sample_kinds() {
        let producer = producer(2);
        let key = format!("{ROOT}/liveliness/participants/drive/{producer}");
        let alive = participant_event(ROOT, &key, SampleKind::Put).unwrap();
        let lost = participant_event(ROOT, &key, SampleKind::Delete).unwrap();

        assert_eq!(alive.participant().as_str(), "drive");
        assert_eq!(alive.producer(), producer);
        assert_eq!(alive.status, ParticipantReadyStatus::Ready);
        assert_eq!(lost.status, ParticipantReadyStatus::Lost);
        assert!(participant_event(ROOT, "other/robot/key", SampleKind::Put).is_none());
    }

    #[test]
    fn observer_selector_covers_exactly_the_emitted_identity_segments() {
        let root = ROOT;
        let selector = ParticipantReadyKey::selector(root).unwrap();
        let key = ParticipantReadyKey::new(root, "drive", producer(3)).unwrap();
        assert_eq!(
            selector.as_str(),
            format!("{ROOT}/liveliness/participants/*/*")
        );
        assert!(selector.includes(&key.key));
    }

    /// The lease a client declares for a participant it stands in for is the
    /// ordinary participant Ready key: same shape, same producer as the
    /// client's own lease, and inside the execution-wide observer's selector.
    /// An observer therefore cannot tell a simulated driver from one running in
    /// its own process, which is the whole point.
    #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
    async fn a_stand_in_lease_is_an_ordinary_participant_ready_key() {
        use crate::bus::session::BusConfig;
        use crate::identity::ExecutionId;

        let controller = ParticipantId::new("webots").unwrap();
        let driver = ParticipantId::new("front_left_drive").unwrap();
        let (owner, bus) = BusOwner::open(BusConfig::for_participant(
            ExecutionId::mint(),
            controller.clone(),
            Vec::new(),
        ))
        .await
        .unwrap();

        let stand_in = owner.declare_participant_ready_as(&driver).await.unwrap();
        assert_eq!(stand_in.participant(), &driver);
        assert_eq!(stand_in.producer(), bus.producer());

        let expected =
            ParticipantReadyKey::new(bus.root(), driver.as_str(), bus.producer()).unwrap();
        assert_eq!(
            expected.as_str(),
            format!(
                "{}/{PARTICIPANT_LIVELINESS_PREFIX}/{driver}/{}",
                bus.root(),
                bus.producer()
            )
        );
        assert!(
            ParticipantReadyKey::selector(bus.root())
                .unwrap()
                .includes(&expected.key)
        );

        let own = owner.declare_participant_ready().await.unwrap();
        assert_eq!(own.producer(), stand_in.producer());
        assert_eq!(own.participant(), &controller);
        owner.close().await;
    }

    #[test]
    fn participant_scoped_selector_excludes_unrelated_ready_churn() {
        let root = ROOT;
        let expected = ParticipantId::new("safety").unwrap();
        let selected = ParticipantReadyKey::participant_selector(root, &expected).unwrap();
        let safety = ParticipantReadyKey::new(root, "safety", producer(4)).unwrap();
        let navigation = ParticipantReadyKey::new(root, "navigation", producer(5)).unwrap();
        assert_eq!(
            selected.as_str(),
            format!("{root}/liveliness/participants/safety/*")
        );
        assert!(selected.includes(&safety.key));
        assert!(!selected.includes(&navigation.key));
    }
}