phoxal-bus 0.42.2

Phoxal bus ABI floor: the Zenoh-native contract bus client and the API-version / contract-body primitive traits.
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
//! Receiver-owned command leases and producer fencing (#952 sections F and G).
//!
//! A command envelope carries execution membership (structurally, through the
//! bus root), producer identity, a monotonic sequence, and the body. It carries
//! no production timestamp, because a command is a request, not an observation.
//!
//! The **owning service** - never the sender - decides what a command is worth:
//! it rejects a non-increasing sequence from a known producer, stamps its own
//! [`LocalInstant`] observation, renews a lease it owns itself, and applies the
//! result at a logical step. Every leased command has two independent expiry
//! conditions, and neither is distorted to serve the other:
//!
//! - a **host-monotonic silence deadline**, which protects human and network
//!   liveness. At 5x simulation speed it still requires operator presence in
//!   human time.
//! - a **logical hold horizon**, which bounds real or simulated travel. At 5x
//!   simulation speed it still bounds how far the machine may move on one
//!   command.
//!
//! [`ProducerFence`] is the identity half. A fresh process is a fresh producer
//! whose sequence starts at zero, so "did the sequence reset" collapses into
//! "is this a different producer". Once a replacement producer is accepted, the
//! superseded one is fenced: it cannot renew a lease or an actuator permit
//! afterwards, even if it is still live.

use std::collections::HashSet;
use std::time::Duration;

use crate::identity::ProducerId;
use crate::time::{LocalInstant, RobotInstant, TimelineMismatch};

/// Why a command was not accepted.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum LeaseRejection {
    /// The sequence did not increase within the accepted producer's stream, so
    /// this is a replay or a reorder.
    #[error("sequence {observed} does not follow {accepted} for the accepted producer")]
    StaleSequence {
        /// The highest sequence accepted so far.
        accepted: u64,
        /// The sequence just observed.
        observed: u64,
    },
    /// A producer that has already been superseded tried to renew.
    #[error("producer {superseded} was superseded by {accepted}")]
    SupersededProducer {
        /// The producer that tried to renew.
        superseded: ProducerId,
        /// The producer that replaced it.
        accepted: ProducerId,
    },
}

/// What happened to a lease when a command was offered to it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LeaseDecision {
    /// The command was accepted and the lease renewed.
    Renewed,
    /// A replacement producer was accepted; the previous one is now fenced.
    ProducerReplaced {
        /// The producer that was fenced.
        superseded: ProducerId,
    },
    /// The command was rejected and the lease untouched.
    Rejected(LeaseRejection),
}

/// Tracks which producer holds authority over one input, and which ones have
/// lost it for good.
///
/// The replacement policy is "last accepted wins, and the loser stays fenced":
/// a live producer is authoritative until a *different* producer is accepted,
/// at which point the previous one is recorded as superseded and can never
/// renew again. Remembering only the current producer would let A -> B -> late A
/// hand authority back to a process the service already replaced, which is the
/// exact zombie this fence exists to stop.
///
/// "Never" is meant literally, so the fenced set is not bounded. A ring would
/// hand authority back to the oldest zombie once enough replacements pushed it
/// out - and producer identities are opaque, so nothing downstream could tell
/// that had happened. The set grows by one identity per replacement, which is
/// one process restart, not one message.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ProducerFence {
    accepted: Option<(ProducerId, u64)>,
    superseded: HashSet<ProducerId>,
}

impl ProducerFence {
    /// Nothing accepted yet.
    pub fn new() -> Self {
        ProducerFence::default()
    }

    /// The producer currently holding authority, if any.
    pub fn accepted_producer(&self) -> Option<ProducerId> {
        self.accepted.map(|(producer, _)| producer)
    }

    /// Offer a sample from `producer` at `sequence`.
    ///
    /// A different producer replaces the current one and fences it. The same
    /// producer must strictly increase its sequence. A producer that was
    /// already superseded is rejected outright, however live it still is.
    pub fn admit(&mut self, producer: ProducerId, sequence: u64) -> LeaseDecision {
        if self.superseded.contains(&producer) {
            return LeaseDecision::Rejected(LeaseRejection::SupersededProducer {
                superseded: producer,
                accepted: self.accepted.map_or(producer, |(accepted, _)| accepted),
            });
        }
        match self.accepted {
            None => {
                self.accepted = Some((producer, sequence));
                LeaseDecision::Renewed
            }
            Some((accepted, last)) if accepted == producer => {
                if sequence > last {
                    self.accepted = Some((producer, sequence));
                    LeaseDecision::Renewed
                } else {
                    LeaseDecision::Rejected(LeaseRejection::StaleSequence {
                        accepted: last,
                        observed: sequence,
                    })
                }
            }
            Some((superseded, _)) => {
                self.fence(superseded);
                self.accepted = Some((producer, sequence));
                LeaseDecision::ProducerReplaced { superseded }
            }
        }
    }

    /// Whether `producer` may still act, i.e. has not been superseded.
    pub fn permits(&self, producer: ProducerId) -> bool {
        !self.superseded.contains(&producer)
            && self
                .accepted
                .is_none_or(|(accepted, _)| accepted == producer)
    }

    /// Forget the *accepted* producer, so the next sample starts a fresh
    /// stream. The superseded set deliberately survives: a world replacement
    /// invalidates derived state, not the fact that a process lost authority.
    pub fn clear(&mut self) {
        self.accepted = None;
    }

    fn fence(&mut self, producer: ProducerId) {
        self.superseded.insert(producer);
    }
}

/// The tracing target every lease decision is emitted on.
///
/// It is deliberately outside the `phoxal_bus` / `phoxal.bus` prefixes the bus
/// log layer filters, so the decision trace reaches the bus and any recorder
/// subscribed to it. Renewals are `DEBUG` and every transition is `INFO`, so an
/// operator running at the default level sees each change of authority and each
/// expiry, and raising the level to `DEBUG` yields the complete per-command
/// trace.
pub const LEASE_TRACE_TARGET: &str = "phoxal.lease";

/// A receiver-owned command lease with two independent expiry conditions.
///
/// The lease holds the last accepted command body plus both deadlines. It is
/// the owning service's own state, renewed only by commands the service itself
/// admitted, and read at a logical step.
///
/// Every decision it takes - renewal, replacement, rejection, and each expiry -
/// is emitted on [`LEASE_TRACE_TARGET`] with the producer, the sender's
/// sequence, this receiver's own observation ordinal, and the logical step the
/// command was applied at. That trace is the acceptance evidence; the lease
/// keeps no private history of its own.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Lease<B> {
    input: &'static str,
    silence: Duration,
    hold: Duration,
    fence: ProducerFence,
    held: Option<Held<B>>,
    observations: u64,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Held<B> {
    body: B,
    observed_at: LocalInstant,
    /// Who sent the held command, what they numbered it, and where it sits in
    /// this receiver's own arrival order. Every later decision about this
    /// command - the step it was applied at, the deadline it died on - reports
    /// the same three, so a trace never has to be joined back to an earlier
    /// row that may itself have been dropped.
    producer: ProducerId,
    sequence: u64,
    observation: u64,
    accepted_at: Option<RobotInstant>,
}

impl<B> Lease<B> {
    /// A lease that expires after `silence` of host-monotonic quiet, or after
    /// `hold` of robot time since the command was applied.
    ///
    /// `input` names the command input this lease owns (`"motion/manual"`,
    /// `"drive/target"`); it labels every decision this lease emits.
    pub fn new(input: &'static str, silence: Duration, hold: Duration) -> Self {
        Lease {
            input,
            silence,
            hold,
            fence: ProducerFence::new(),
            held: None,
            observations: 0,
        }
    }

    /// How many commands this receiver has observed on this input.
    ///
    /// This is the receiver's own arrival order, not anything the sender
    /// supplied, and it is the ordinate of the decision trace.
    pub const fn observations(&self) -> u64 {
        self.observations
    }

    /// The host-monotonic silence deadline this lease enforces.
    pub const fn silence(&self) -> Duration {
        self.silence
    }

    /// The logical hold horizon this lease enforces.
    pub const fn hold(&self) -> Duration {
        self.hold
    }

    /// The producer currently holding this lease.
    pub fn producer(&self) -> Option<ProducerId> {
        self.fence.accepted_producer()
    }

    /// Offer an observed command to the lease.
    ///
    /// `observed_at` is the receiver's own stamp - the one the bus took before
    /// decode - never anything the sender supplied.
    pub fn offer(
        &mut self,
        producer: ProducerId,
        sequence: u64,
        observed_at: LocalInstant,
        body: B,
    ) -> LeaseDecision {
        let decision = self.fence.admit(producer, sequence);
        self.observations = self.observations.saturating_add(1);
        let observation = self.observations;
        match decision {
            LeaseDecision::Renewed => {
                tracing::debug!(
                    target: LEASE_TRACE_TARGET,
                    input = self.input,
                    producer = %producer,
                    sequence,
                    observation,
                    decision = "renewed",
                    "command renewed the lease"
                );
            }
            LeaseDecision::ProducerReplaced { superseded } => {
                tracing::info!(
                    target: LEASE_TRACE_TARGET,
                    input = self.input,
                    producer = %producer,
                    sequence,
                    observation,
                    superseded = %superseded,
                    decision = "producer_replaced",
                    "a replacement producer took the lease; the previous one is fenced"
                );
            }
            LeaseDecision::Rejected(rejection) => {
                tracing::info!(
                    target: LEASE_TRACE_TARGET,
                    input = self.input,
                    producer = %producer,
                    sequence,
                    observation,
                    decision = "rejected",
                    reason = %rejection,
                    "command rejected"
                );
            }
        }
        match decision {
            LeaseDecision::Renewed | LeaseDecision::ProducerReplaced { .. } => {
                self.held = Some(Held {
                    body,
                    observed_at,
                    producer,
                    sequence,
                    observation,
                    // A renewal restarts the logical horizon at the next step
                    // that applies it; until then it has not been applied at
                    // any robot instant.
                    accepted_at: None,
                });
            }
            LeaseDecision::Rejected(_) => {}
        }
        decision
    }

    /// The live command at this step, or `None` if either expiry condition has
    /// been met.
    ///
    /// The first call after a renewal anchors the logical hold horizon at
    /// `now`, so the horizon measures time since the command was *applied*, not
    /// since it was sent - which is what makes it survive a paused simulation
    /// correctly.
    ///
    /// A cross-timeline anchor is not a silently wrong number: it means the
    /// world was replaced under a held command, so the command is dropped.
    ///
    /// Both deadlines are reached *at* the deadline, not after it: a command
    /// whose silence or hold has run exactly to its bound is already expired.
    /// The actuator permit ages the same way, so no boundary sample is live in
    /// one layer and dead in the next.
    pub fn live(&mut self, now: LocalInstant, step: RobotInstant) -> Option<&B> {
        let input = self.input;
        let silence = self.silence;
        let hold = self.hold;
        let held = self.held.as_mut()?;
        let (producer, sequence, observation) = (held.producer, held.sequence, held.observation);
        if now.saturating_duration_since(held.observed_at) >= silence {
            tracing::info!(
                target: LEASE_TRACE_TARGET,
                input,
                producer = %producer,
                sequence,
                observation,
                decision = "expired_silence",
                "the lease expired on host-monotonic silence"
            );
            self.held = None;
            return None;
        }
        let anchor = match held.accepted_at {
            Some(anchor) => anchor,
            None => {
                tracing::debug!(
                    target: LEASE_TRACE_TARGET,
                    input,
                    producer = %producer,
                    sequence,
                    observation,
                    step = %step,
                    decision = "applied",
                    "the held command was applied at this step and anchors the hold horizon"
                );
                *held.accepted_at.insert(step)
            }
        };
        match step.duration_since(anchor) {
            Ok(elapsed) if elapsed < hold => Some(&self.held.as_ref()?.body),
            Ok(_) => {
                tracing::info!(
                    target: LEASE_TRACE_TARGET,
                    input,
                    producer = %producer,
                    sequence,
                    observation,
                    step = %step,
                    decision = "expired_hold",
                    "the lease expired on its logical hold horizon"
                );
                self.held = None;
                None
            }
            Err(TimelineMismatch { .. }) => {
                tracing::info!(
                    target: LEASE_TRACE_TARGET,
                    input,
                    producer = %producer,
                    sequence,
                    observation,
                    step = %step,
                    decision = "timeline_replaced",
                    "the world was replaced under the held command, so it was dropped"
                );
                self.held = None;
                None
            }
        }
    }

    /// Drop any held command and forget the accepted producer.
    pub fn clear(&mut self) {
        self.held = None;
        self.fence.clear();
    }
}

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

    const SILENCE: Duration = Duration::from_millis(150);
    const HOLD: Duration = Duration::from_millis(500);

    fn step(line: TimelineId, ticks: u64) -> RobotInstant {
        RobotInstant::new(line, ticks)
    }

    fn ms(value: u64) -> u64 {
        value * 1_000_000
    }

    #[test]
    fn a_sequence_that_does_not_increase_is_rejected_without_touching_the_lease() {
        let producer = ProducerId::mint();
        let start = LocalInstant::from_boot_ns(1_000);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);

        assert_eq!(
            lease.offer(producer, 1, start, "go"),
            LeaseDecision::Renewed
        );
        assert_eq!(
            lease.offer(producer, 1, start, "replay"),
            LeaseDecision::Rejected(LeaseRejection::StaleSequence {
                accepted: 1,
                observed: 1
            })
        );
        assert_eq!(
            lease.offer(producer, 0, start, "reorder"),
            LeaseDecision::Rejected(LeaseRejection::StaleSequence {
                accepted: 1,
                observed: 0
            })
        );

        let line = TimelineId::mint();
        assert_eq!(lease.live(start, step(line, 0)), Some(&"go"));
    }

    #[test]
    fn a_fresh_producer_starting_at_zero_supersedes_and_fences_the_previous_one() {
        let first = ProducerId::mint();
        let second = ProducerId::mint();
        let start = LocalInstant::from_boot_ns(1_000);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);

        lease.offer(first, 9, start, "old");
        // A restarted publisher is a fresh producer whose sequence restarts at
        // zero; that must not look like a replay.
        assert_eq!(
            lease.offer(second, 0, start, "new"),
            LeaseDecision::ProducerReplaced { superseded: first }
        );
        assert!(!lease.fence.permits(first));
        assert!(lease.fence.permits(second));

        // The superseded producer cannot renew afterwards, however live it
        // still is: A -> B -> late A must not hand authority back to A.
        assert_eq!(
            lease.offer(first, 10, start, "zombie"),
            LeaseDecision::Rejected(LeaseRejection::SupersededProducer {
                superseded: first,
                accepted: second,
            })
        );
        assert_eq!(lease.producer(), Some(second));
        assert_eq!(
            lease.live(start, step(TimelineId::mint(), 0)),
            Some(&"new"),
            "the zombie must not have replaced the held command either"
        );
    }

    /// A world replacement discards derived state, but it does not rehabilitate
    /// a process that already lost authority.
    #[test]
    fn clearing_a_lease_does_not_unfence_a_superseded_producer() {
        let first = ProducerId::mint();
        let second = ProducerId::mint();
        let start = LocalInstant::from_boot_ns(0);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);

        lease.offer(first, 1, start, "old");
        lease.offer(second, 0, start, "new");
        lease.clear();

        assert!(matches!(
            lease.offer(first, 2, start, "zombie"),
            LeaseDecision::Rejected(LeaseRejection::SupersededProducer { .. })
        ));
        assert_eq!(
            lease.offer(second, 1, start, "live"),
            LeaseDecision::Renewed
        );
    }

    #[test]
    fn a_superseded_producer_cannot_renew_an_actuator_permit() {
        let first = ProducerId::mint();
        let second = ProducerId::mint();
        let mut fence = ProducerFence::new();
        fence.admit(first, 1);
        fence.admit(second, 0);
        assert_eq!(fence.accepted_producer(), Some(second));
        assert!(
            !fence.permits(first),
            "the fenced authority must not be able to hold a permit open"
        );
        assert!(matches!(
            fence.admit(first, 2),
            LeaseDecision::Rejected(LeaseRejection::SupersededProducer { .. })
        ));
        assert_eq!(
            fence.accepted_producer(),
            Some(second),
            "a rejected zombie must not become the accepted producer"
        );
    }

    /// A ring of fenced producers would hand authority back to the oldest
    /// zombie once enough replacements pushed it out, and producer identities
    /// are opaque, so nothing downstream could notice. "Never renews again"
    /// has to survive an arbitrary number of restarts.
    #[test]
    fn a_superseded_producer_stays_fenced_after_many_replacements() {
        let first = ProducerId::mint();
        let mut fence = ProducerFence::new();
        fence.admit(first, 1);
        for _ in 0..64 {
            fence.admit(ProducerId::mint(), 0);
        }
        assert!(matches!(
            fence.admit(first, 2),
            LeaseDecision::Rejected(LeaseRejection::SupersededProducer { .. })
        ));
        assert!(!fence.permits(first));
    }

    #[test]
    fn host_silence_expires_the_lease_independently_of_logical_time() {
        let producer = ProducerId::mint();
        let line = TimelineId::mint();
        let start = LocalInstant::from_boot_ns(0);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);
        lease.offer(producer, 1, start, "go");

        // Logical time is frozen (a paused or slowed simulation), yet host
        // silence still expires the command: operator presence is a human-time
        // requirement.
        let quiet = start.saturating_add(SILENCE);
        assert_eq!(lease.live(quiet, step(line, 0)), None);
        assert_eq!(
            lease.live(start, step(line, 0)),
            None,
            "an expired lease stays expired until a new command renews it"
        );
    }

    #[test]
    fn the_logical_horizon_expires_the_lease_independently_of_host_time() {
        let producer = ProducerId::mint();
        let line = TimelineId::mint();
        let start = LocalInstant::from_boot_ns(0);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);
        lease.offer(producer, 1, start, "go");

        // Host time is still (an accelerated simulation), yet simulated travel
        // is bounded.
        assert_eq!(lease.live(start, step(line, 0)), Some(&"go"));
        assert_eq!(lease.live(start, step(line, ms(499))), Some(&"go"));
        // The horizon is reached *at* the bound, matching how the actuator
        // permit ages: no sample is live in one layer and dead in the next.
        assert_eq!(lease.live(start, step(line, ms(500))), None);
    }

    #[test]
    fn a_lease_that_expired_while_paused_does_not_apply_on_the_first_resumed_step() {
        let producer = ProducerId::mint();
        let line = TimelineId::mint();
        let start = LocalInstant::from_boot_ns(0);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);
        lease.offer(producer, 1, start, "go");
        assert_eq!(lease.live(start, step(line, 0)), Some(&"go"));

        // The world is paused: no steps happen, but host time keeps running
        // past the silence deadline. The first resumed arbitration step must
        // not apply the retained command.
        let resumed = start.saturating_add(SILENCE);
        assert_eq!(lease.live(resumed, step(line, 1)), None);
    }

    #[test]
    fn a_replacement_timeline_drops_a_held_command_rather_than_comparing_across_worlds() {
        let producer = ProducerId::mint();
        let first_line = TimelineId::mint();
        let second_line = TimelineId::mint();
        let start = LocalInstant::from_boot_ns(0);
        let mut lease = Lease::new("test/input", SILENCE, HOLD);
        lease.offer(producer, 1, start, "go");
        assert_eq!(lease.live(start, step(first_line, 0)), Some(&"go"));
        assert_eq!(lease.live(start, step(second_line, 0)), None);
    }
}