//! Typed control bus — delivery gated on lifecycle state (`proofs/bus_lifecycle.lean`, //! Theorems 13–14). In-process routing for now; the contract allows out-of-process //! or Lyra transports later without changing message semantics. //! //! The bus carries control-plane messages only. Tensor data never crosses it: //! the data plane uses UMA handles granted by the `SlabRing` broker. use vetta_infer_abi::bus::{BusMsg, BusPayload, ServiceId, ServiceState}; use crate::registry::{Registry, RegistryError}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum BusError { Registry(RegistryError), /// Destination exists but is not in a state that accepts this payload. NotDeliverable { dst: ServiceId, state: ServiceState }, } impl std::fmt::Display for BusError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{self:?}") } } impl std::error::Error for BusError {} impl From for BusError { fn from(e: RegistryError) -> Self { Self::Registry(e) } } #[derive(Debug, Default)] pub struct ControlBus { registry: Registry, delivered: Vec, } impl ControlBus { pub fn new() -> Self { Self::default() } pub fn registry(&self) -> &Registry { &self.registry } pub fn registry_mut(&mut self) -> &mut Registry { &mut self.registry } /// Routes one message, enforcing the verified delivery rules: /// - `Stop` is accepted only by a `Running` service and starts its drain; /// - `StopAck` is accepted only from a `Stopping` service and completes it; /// - every other payload is deliverable only to `Running`. pub fn send(&mut self, msg: BusMsg) -> Result<(), BusError> { let state = self.registry.state(msg.dst)?; match &msg.payload { BusPayload::Stop => { if state != ServiceState::Running { return Err(BusError::NotDeliverable { dst: msg.dst, state }); } self.registry.request_stop(msg.dst)?; } BusPayload::StopAck => { if state != ServiceState::Stopping { return Err(BusError::NotDeliverable { dst: msg.dst, state }); } self.registry.drain_done(msg.dst)?; } _ => { if state != ServiceState::Running { return Err(BusError::NotDeliverable { dst: msg.dst, state }); } } } self.delivered.push(msg); Ok(()) } /// In-order log of accepted messages (transport seam: a real transport /// replaces this with per-service queues without changing `send` rules). pub fn delivered(&self) -> &[BusMsg] { &self.delivered } } #[cfg(test)] mod tests { use super::*; use vetta_infer_abi::bus::ServiceKind; fn running_bus() -> (ControlBus, ServiceId) { let mut bus = ControlBus::new(); let id = bus.registry().prepare(); bus.registry_mut().commit(id, ServiceKind::Cache).unwrap(); bus.registry_mut().start(id).unwrap(); (bus, id) } #[test] fn stop_drains_to_stopped_and_blocks_further_delivery() { let (mut bus, id) = running_bus(); let hint = BusMsg { src: ServiceId(0), dst: id, payload: BusPayload::DomainHint("lean4".into()) }; bus.send(hint.clone()).unwrap(); let stop = BusMsg { src: ServiceId(0), dst: id, payload: BusPayload::Stop }; bus.send(stop).unwrap(); assert_eq!(bus.registry().state(id).unwrap(), ServiceState::Stopping); // Draining service accepts no new work assert!(bus.send(hint).is_err()); // Nor a second STOP assert!(bus.send(BusMsg { src: ServiceId(0), dst: id, payload: BusPayload::Stop }).is_err()); let ack = BusMsg { src: id, dst: id, payload: BusPayload::StopAck }; bus.send(ack).unwrap(); assert_eq!(bus.registry().state(id).unwrap(), ServiceState::Stopped); assert!(bus.send(BusMsg { src: id, dst: id, payload: BusPayload::StopAck }).is_err()); assert_eq!(bus.delivered().len(), 3); // hint, stop, ack — rejected msgs never logged } #[test] fn no_delivery_to_registered_or_unknown() { let mut bus = ControlBus::new(); let id = bus.registry().prepare(); bus.registry_mut().commit(id, ServiceKind::Net).unwrap(); let msg = BusMsg { src: ServiceId(0), dst: id, payload: BusPayload::DomainHint("dante".into()) }; assert!(bus.send(msg.clone()).is_err()); // Registered is not deliverable assert!(bus.send(BusMsg { src: ServiceId(0), dst: ServiceId(77), payload: BusPayload::Stop }).is_err()); assert!(bus.delivered().is_empty()); } }