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
//! The endpoint-kind publisher handles.
//!
//! Each handle wraps the same private `Outbox` publish path - encode, build
//! provenance, enqueue - and differs only in the endpoint marker it is bounded
//! by and the robot time that marker permits it to express.

use std::marker::PhantomData;

use crate::bus::abi::{Codec, MessagePack};
use crate::bus::contract::{
    DeliveryFamily, Endpoint, EndpointSemantics, Event, Sample, Setpoint, State, StreamDelivered,
    WorldClock,
};
use crate::bus::error::{BusError, Result};
use crate::bus::handle::stamp::StepStamp;
use crate::bus::runtime_metrics::RuntimeMetricHandle;
use crate::bus::session::{BusHandle, OUTBOUND_CAPACITY};
use crate::bus::time::{CaptureStamp, TimeWindow};
use crate::bus::topic::{Publish, Topic};

/// The shared publish path: encode, build provenance, enqueue. Private, so the
/// only public way to reach it is through an endpoint-kind publisher.
struct Outbox<E> {
    bus: BusHandle,
    key: String,
    family: DeliveryFamily,
    metric: RuntimeMetricHandle,
    _endpoint: PhantomData<fn() -> E>,
}

// Manual (not `#[derive(Clone)]`) so cloning never spuriously requires
// `E: Clone` - every field it actually holds is `Clone` regardless of `E`. All
// real operations take `&self`, so a clone is just a second handle to the same
// publish key on the same session (the runner's `Arc<Self::Api>`
// snapshot-sharing relies on every `Api` field type being cheaply `Clone` this
// way).
impl<E> Clone for Outbox<E> {
    fn clone(&self) -> Self {
        Outbox {
            bus: self.bus.clone(),
            key: self.key.clone(),
            family: self.family,
            metric: self.metric.clone(),
            _endpoint: PhantomData,
        }
    }
}

impl<E: Endpoint> Outbox<E> {
    fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        let topic_key = topic.publish_key()?;
        let family = <E::Semantics as EndpointSemantics>::DELIVERY;
        let metric = bus
            .runtime_metrics()?
            .register_outbound(topic_key, outbound_capacity(family));
        let key = bus.full_key(topic_key);
        Ok(Outbox {
            bus,
            key,
            family,
            metric,
            _endpoint: PhantomData,
        })
    }

    /// Encode `body`, build the [`BusMetadata`](crate::bus::metadata::BusMetadata),
    /// and admit it to the family-specific outbound lane. Returns immediately;
    /// no publisher path blocks the step loop.
    fn emit(&self, produced_at: Option<TimeWindow>, body: E) -> Result<()> {
        let payload = MessagePack::encode(&body)?;
        let metadata = self.bus.metadata(produced_at)?;
        self.bus.enqueue(
            self.key.clone(),
            MessagePack::ID.encoding_string(),
            payload,
            metadata,
            self.family,
            self.metric.clone(),
        )
    }
}

const fn outbound_capacity(family: DeliveryFamily) -> usize {
    match family {
        DeliveryFamily::State | DeliveryFamily::Setpoint => 1,
        DeliveryFamily::Sample | DeliveryFamily::Stream => OUTBOUND_CAPACITY,
        DeliveryFamily::Query => 1,
    }
}

/// Publishes the owner's current state at a logical step.
///
/// The transport keeps only the newest pending state. Replacements are
/// reported by the bus metrics.
pub struct StatePublisher<E: Endpoint<Semantics = State>>(Outbox<E>);

impl<E: Endpoint<Semantics = State>> Clone for StatePublisher<E> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<E: Endpoint<Semantics = State>> StatePublisher<E> {
    #[doc(hidden)]
    pub fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        Ok(Self(Outbox::new(bus, topic)?))
    }
}

/// Publishes a captured sensor observation.
///
/// Samples retain their capture stamp. The bounded ordered lane evicts its
/// oldest item on overflow and reports the loss through bus metrics.
pub struct SamplePublisher<E: Endpoint<Semantics = Sample>>(Outbox<E>);

impl<E: Endpoint<Semantics = Sample>> Clone for SamplePublisher<E> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<E: Endpoint<Semantics = Sample>> SamplePublisher<E> {
    #[doc(hidden)]
    pub fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        Ok(Self(Outbox::new(bus, topic)?))
    }
}

/// Sends newest-actionable intent to the endpoint owner.
///
/// Setpoints carry no robot timestamp. A newer pending value replaces the
/// older value and the replacement is reported by bus metrics.
pub struct SetpointPublisher<E: Endpoint<Semantics = Setpoint>>(Outbox<E>);

impl<E: Endpoint<Semantics = Setpoint>> Clone for SetpointPublisher<E> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<E: Endpoint<Semantics = Setpoint>> SetpointPublisher<E> {
    #[doc(hidden)]
    pub fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        Ok(Self(Outbox::new(bus, topic)?))
    }
}

/// Publishes ordered stream chunks in the endpoint's declared direction.
///
/// Stream chunks carry no robot timestamp. Saturation is returned to the
/// caller, and receivers expose gaps or terminal failure.
pub struct StreamPublisher<E: Endpoint>(Outbox<E>)
where
    E::Semantics: StreamDelivered;

impl<E: Endpoint> Clone for StreamPublisher<E>
where
    E::Semantics: StreamDelivered,
{
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<E: Endpoint> StreamPublisher<E>
where
    E::Semantics: StreamDelivered,
{
    #[doc(hidden)]
    pub fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        Ok(Self(Outbox::new(bus, topic)?))
    }
}

/// Publishes the framework's own world-clock contract at a logical step.
///
/// A near-twin of [`StatePublisher`] - same step-stamped publish path - kept
/// as its own type rather than folded into `StatePublisher` precisely so
/// `StatePublisher`'s bound can stay the exact
/// [`State`] semantic. The world clock's semantic is
/// [`WorldClock`], a sibling rather than a subtype, which is what makes the
/// ordinary state publisher reject it at compile time.
///
/// Crate-private, like the authority that stamps it: publishing the world
/// clock is world ownership, and the only holder is [`crate::simulator`]'s
/// world time, which the external world adapter drives.
#[allow(
    dead_code,
    reason = "compiled in every profile because a domain module never asks which profile it is in; its only consumer is a module one profile declares"
)]
pub(crate) struct WorldClockPublisher<B: Endpoint<Semantics = WorldClock>>(Outbox<B>);

impl<B: Endpoint<Semantics = WorldClock>> Clone for WorldClockPublisher<B> {
    fn clone(&self) -> Self {
        WorldClockPublisher(self.0.clone())
    }
}

impl<E: Endpoint<Semantics = State>> StatePublisher<E> {
    /// Publish `body` as the state this step produced.
    pub fn publish(&self, step: &impl StepStamp, body: E) -> Result<()> {
        self.0.emit(Some(TimeWindow::exact(step.instant())), body)
    }
}

#[allow(
    dead_code,
    reason = "compiled in every profile because a domain module never asks which profile it is in; its only consumer is a module one profile declares"
)]
impl<B: Endpoint<Semantics = WorldClock>> WorldClockPublisher<B> {
    /// Build the world-clock publisher over a topic.
    ///
    /// [`crate::simulator`]'s world time is its only caller.
    pub(crate) fn mint(bus: BusHandle, topic: &Topic<Publish<B>>) -> Result<Self> {
        Ok(WorldClockPublisher(Outbox::new(bus, topic)?))
    }

    /// Publish `body` as the state this step produced.
    pub fn publish(&self, step: &impl StepStamp, body: B) -> Result<()> {
        self.0.emit(Some(TimeWindow::exact(step.instant())), body)
    }
}

impl<E: Endpoint<Semantics = Sample>> SamplePublisher<E> {
    /// Publish `body` as captured at `stamp`.
    pub fn publish(&self, stamp: CaptureStamp, body: E) -> Result<()> {
        self.0.emit(stamp.into_window(), body)
    }
}

impl<E: Endpoint<Semantics = Setpoint>> SetpointPublisher<E> {
    /// Send `body` to the contract's owning service.
    pub fn send(&self, body: E) -> Result<()> {
        self.0.emit(None, body)
    }
}

impl<E: Endpoint> StreamPublisher<E>
where
    E::Semantics: StreamDelivered,
{
    /// Send one ordered stream chunk without blocking the step loop.
    pub fn send(&self, body: E) -> Result<()> {
        self.0.emit(None, body).map_err(|error| match error {
            BusError::Saturated { topic, .. } => BusError::WouldBlock { topic },
            error => error,
        })
    }
}

/// Publishes a discrete event produced by the owner at a logical step.
///
/// Events use the bounded ordered stream lane. Saturation is returned as
/// `WouldBlock`, and receivers expose producer gaps or terminal evidence.
pub struct EventPublisher<E: Endpoint<Semantics = Event>>(Outbox<E>);

impl<E: Endpoint<Semantics = Event>> Clone for EventPublisher<E> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<E: Endpoint<Semantics = Event>> EventPublisher<E> {
    /// Build an event publisher over a generated endpoint topic.
    #[doc(hidden)]
    pub fn new(bus: BusHandle, topic: &Topic<Publish<E>>) -> Result<Self> {
        Ok(Self(Outbox::new(bus, topic)?))
    }

    /// Publish the event produced at this logical step.
    pub fn publish(&self, step: &impl StepStamp, body: E) -> Result<()> {
        self.0
            .emit(Some(TimeWindow::exact(step.instant())), body)
            .map_err(|error| match error {
                BusError::Saturated { topic, .. } => BusError::WouldBlock { topic },
                error => error,
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bus::contract::TestFamily;
    use crate::bus::error::BusError;
    use crate::bus::handle::subscriber::SetpointReceiver;
    use crate::bus::runtime_metrics::RuntimeBufferKind;
    use crate::bus::session::{BusOwner, OUTBOUND_CAPACITY, OUTBOUND_MAX_BYTES};
    use crate::bus::test_support::{TARGET_TOPIC, Target, bound, participant_config, step};
    use crate::bus::time::CaptureStamp;
    use serial_test::serial;

    const STREAM_TOPIC: &str = "yTEST/stream/chunk";
    const STATE_TOPIC: &str = "yTEST/stream/state";
    const SAMPLE_TOPIC: &str = "yTEST/stream/sample";
    const SETPOINT_TOPIC: &str = "yTEST/stream/setpoint";

    /// One stand-in endpoint per delivery family, so the admission rules below
    /// are exercised on four genuinely different lanes.
    macro_rules! stand_in {
        ($name:ident ( $body:ty ), $semantics:ty) => {
            #[derive(phoxal_macros::DescribeWire, Debug, serde::Serialize, serde::Deserialize)]
            struct $name($body);

            impl crate::bus::contract::sealed::Endpoint for $name {}

            impl Endpoint for $name {
                type Family = TestFamily;
                type Semantics = $semantics;
            }
        };
    }

    stand_in!(StreamChunk(Vec<u8>), crate::bus::Stream<crate::bus::Out>);
    stand_in!(StateChunk(u16), State);
    stand_in!(SampleChunk(u16), Sample);
    stand_in!(SetpointChunk(u16), Setpoint);

    /// A publish after close is a real loss, and the caller has to be able to
    /// see it: silently succeeding would let a participant believe it had
    /// reported state it never sent.
    #[serial]
    #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
    async fn publishing_on_a_closed_bus_reports_the_loss() {
        let (owner, bus) = BusOwner::open(participant_config("closed")).await.unwrap();
        let topic = bound::<Target>(TARGET_TOPIC).owner();
        let publisher = StatePublisher::<Target>::new(bus.clone(), &topic).unwrap();
        owner.close().await;

        let error = publisher
            .publish(
                &step(1, 100),
                Target {
                    linear_x_mps: 0.9,
                    angular_z_radps: -0.1,
                },
            )
            .unwrap_err();
        assert!(matches!(error, BusError::Closed));
    }

    #[serial]
    #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
    async fn stream_saturation_is_reported_as_would_block() {
        let (owner, bus) = BusOwner::open(participant_config("stream-would-block"))
            .await
            .unwrap();
        let topic = bound::<StreamChunk>(STREAM_TOPIC).owner();
        let publisher = StreamPublisher::<StreamChunk>::new(bus.clone(), &topic).unwrap();
        let error = publisher
            .send(StreamChunk(vec![0; OUTBOUND_MAX_BYTES + 1]))
            .expect_err("an oversized stream chunk must not be accepted");
        assert!(matches!(error, BusError::WouldBlock { .. }));
        owner.close().await;
    }

    #[serial]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn publisher_admission_enforces_each_delivery_family_and_records_evidence() {
        let (owner, bus) = BusOwner::open(participant_config("semantic-admission"))
            .await
            .unwrap();
        let state = StatePublisher::<StateChunk>::new(
            bus.clone(),
            &bound::<StateChunk>(STATE_TOPIC).owner(),
        )
        .unwrap();
        let setpoint = SetpointPublisher::<SetpointChunk>::new(
            bus.clone(),
            &bound::<SetpointChunk>(SETPOINT_TOPIC).client(),
        )
        .unwrap();
        let setpoint_receiver =
            SetpointReceiver::new(&bus, &bound::<SetpointChunk>(SETPOINT_TOPIC).owner())
                .await
                .unwrap();
        let sample = SamplePublisher::<SampleChunk>::new(
            bus.clone(),
            &bound::<SampleChunk>(SAMPLE_TOPIC).owner(),
        )
        .unwrap();
        let pause = bus
            .test_pause_outbound_drain()
            .await
            .expect("test drain can be held before admission");
        let stream = StreamPublisher::<StreamChunk>::new(
            bus.clone(),
            &bound::<StreamChunk>(STREAM_TOPIC).owner(),
        )
        .unwrap();

        for value in 0..3 {
            state
                .publish(&step(1, value), StateChunk(value as u16))
                .unwrap();
        }
        for value in 0..3 {
            setpoint.send(SetpointChunk(value)).unwrap();
        }
        for value in 0..=OUTBOUND_CAPACITY {
            sample
                .publish(CaptureStamp::Untranslated, SampleChunk(value as u16))
                .unwrap();
        }
        for value in 0..OUTBOUND_CAPACITY {
            stream.send(StreamChunk(vec![value as u8])).unwrap();
        }
        assert!(matches!(
            stream.send(StreamChunk(vec![0xff])).unwrap_err(),
            BusError::WouldBlock { .. }
        ));

        let full_stream_key = bus.full_key(STREAM_TOPIC);
        let positions: Vec<_> = bus
            .test_queued_stream_metadata(&full_stream_key)
            .into_iter()
            .map(|metadata| {
                metadata
                    .stream_position
                    .expect("an accepted stream has a position")
                    .sequence
            })
            .collect();
        assert_eq!(positions, (0..OUTBOUND_CAPACITY as u64).collect::<Vec<_>>());

        let rows = bus.take_runtime_metrics().unwrap();
        let row = |topic: &str| {
            rows.iter()
                .find(|row| {
                    row.key.buffer_kind == RuntimeBufferKind::Outbound
                        && row.key.topic.ends_with(topic)
                })
                .expect("publisher metric row")
        };
        assert_eq!(row(STATE_TOPIC).count, 3);
        assert_eq!(row(STATE_TOPIC).latest_overwrites, 2);
        assert_eq!(row(STATE_TOPIC).high_water_depth, 1);
        assert_eq!(row(SETPOINT_TOPIC).count, 3);
        assert_eq!(row(SETPOINT_TOPIC).latest_overwrites, 2);
        assert_eq!(row(SETPOINT_TOPIC).high_water_depth, 1);
        assert_eq!(row(SAMPLE_TOPIC).count, OUTBOUND_CAPACITY as u64 + 1);
        assert_eq!(row(SAMPLE_TOPIC).bounded_evictions, 1);
        assert_eq!(row(SAMPLE_TOPIC).high_water_depth, OUTBOUND_CAPACITY as u64);
        assert_eq!(row(STREAM_TOPIC).count, OUTBOUND_CAPACITY as u64);
        assert_eq!(row(STREAM_TOPIC).drops, 1);
        assert_eq!(
            bus.health()
                .outbound_drops
                .load(std::sync::atomic::Ordering::Relaxed),
            2,
            "one sample eviction and one refused stream are both live evidence"
        );

        drop(pause);
        let delivered =
            tokio::time::timeout(std::time::Duration::from_secs(2), setpoint_receiver.recv())
                .await
                .expect("the drain must deliver the coalesced setpoint")
                .expect("setpoint receive remains healthy");
        assert_eq!(delivered.body.0, 2);
        owner
            .close_until(tokio::time::Instant::now() + std::time::Duration::from_secs(10))
            .await;
    }
}