Files
vetta-infer/crates/vetta-infer-kn/src/bus.rs
T
adm 7b50f1c07c [ABI][KN][LD][CC][RT][LAB][SP][NT][TM][GW][Proofs][Docs] M0.5: realign workspace to vetta-infer-* microkernel architecture
Dissolve the M0 crates (core/io/proto/telemetry/hip/cli) into the approved
service layout abi/kn/ld/cc/rt/lab/sp/nt/tm/gw (docs/ARCHITECTURE.md).

- kn: Lean-verified control bus + service registry (bus_lifecycle.lean, 14 thms)
  and SlabRing/UMA broker; rt: PrefixState + hybrid checkpoint pool.
- sp: faithful throughput-aware Adaptive-K port of clover_policy.h
  (cooldowns, probes, throughput guards) with scenario parity tests.
- gw: strata-ctl-compatible wire emitters + serve session FSM
  (serve_session.lean, 10 thms); abi::serve accepts image= (Vision delta closed).
- abi: pure contracts, checked JSON/VTC1/GEN parsers, zero-alloc sampler.
- ld/cc: checked safetensors reader + write-once VTC1 writer / O_RDONLY reader.
- tm: lock-free telemetry; nt: LyraRpc; lab shares the rt executor core.
- Docs: ARCHITECTURE.md decision-complete spec (MoE-only scope, .vbundle,
  .vplan->CompiledPlan, wire contract, migration map); AGENTS.md Lean rule
  + tandem/review workflow.

Verified live: cargo test --workspace -D warnings = 46/46 PASS;
lean proofs = 34 theorems (slab_ring_dma 10, bus_lifecycle 14, serve_session 10),
0 sorryAx, standard axioms only; vetta-infer info on Inkling = 1244 tensors,
18.18 GiB, checked bounds.
2026-11-30 00:54:00 +01:00

133 lines
4.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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<RegistryError> for BusError {
fn from(e: RegistryError) -> Self {
Self::Registry(e)
}
}
#[derive(Debug, Default)]
pub struct ControlBus {
registry: Registry,
delivered: Vec<BusMsg>,
}
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());
}
}