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.
168 lines
5.8 KiB
Rust
168 lines
5.8 KiB
Rust
//! Service registry & lifecycle — implements `proofs/bus_lifecycle.lean`.
|
|
//!
|
|
//! Two-phase registration (prepare/commit): a failed resource allocation that
|
|
//! never calls `commit` leaves zero ghost entries (Theorem 12). Transitions
|
|
//! mirror `SvcStep`: no resurrection, no skipping the drain phase.
|
|
|
|
use vetta_infer_abi::bus::{ServiceId, ServiceKind, ServiceState};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum RegistryError {
|
|
UnknownService(ServiceId),
|
|
/// `commit` called with an id that is not the currently prepared one.
|
|
NotPrepared { expected: ServiceId, got: ServiceId },
|
|
InvalidTransition { id: ServiceId, state: ServiceState, op: &'static str },
|
|
}
|
|
|
|
impl std::fmt::Display for RegistryError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{self:?}")
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for RegistryError {}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ServiceEntry {
|
|
pub id: ServiceId,
|
|
pub kind: ServiceKind,
|
|
pub state: ServiceState,
|
|
}
|
|
|
|
/// Committed entries only — a prepared-but-uncommitted entry is unobservable.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct Registry {
|
|
entries: Vec<ServiceEntry>,
|
|
next_id: u32,
|
|
}
|
|
|
|
impl Registry {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Phase 1 (pure peek): the candidate id for a new service. No mutation.
|
|
pub fn prepare(&self) -> ServiceId {
|
|
ServiceId(self.next_id)
|
|
}
|
|
|
|
/// Phase 2: commit the prepared entry. Call ONLY after the service's
|
|
/// resources were successfully allocated.
|
|
pub fn commit(&mut self, id: ServiceId, kind: ServiceKind) -> Result<(), RegistryError> {
|
|
let expected = self.prepare();
|
|
if id != expected {
|
|
return Err(RegistryError::NotPrepared { expected, got: id });
|
|
}
|
|
self.entries.push(ServiceEntry { id, kind, state: ServiceState::Registered });
|
|
self.next_id += 1;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn entries(&self) -> &[ServiceEntry] {
|
|
&self.entries
|
|
}
|
|
|
|
pub fn state(&self, id: ServiceId) -> Result<ServiceState, RegistryError> {
|
|
self.entries
|
|
.iter()
|
|
.find(|e| e.id == id)
|
|
.map(|e| e.state)
|
|
.ok_or(RegistryError::UnknownService(id))
|
|
}
|
|
|
|
fn entry_mut(&mut self, id: ServiceId) -> Result<&mut ServiceEntry, RegistryError> {
|
|
self.entries
|
|
.iter_mut()
|
|
.find(|e| e.id == id)
|
|
.ok_or(RegistryError::UnknownService(id))
|
|
}
|
|
|
|
fn transition(
|
|
&mut self,
|
|
id: ServiceId,
|
|
from: ServiceState,
|
|
to: ServiceState,
|
|
op: &'static str,
|
|
) -> Result<(), RegistryError> {
|
|
let e = self.entry_mut(id)?;
|
|
if e.state != from {
|
|
return Err(RegistryError::InvalidTransition { id, state: e.state, op });
|
|
}
|
|
e.state = to;
|
|
Ok(())
|
|
}
|
|
|
|
/// `SvcStep.start`: Registered -> Running.
|
|
pub fn start(&mut self, id: ServiceId) -> Result<(), RegistryError> {
|
|
self.transition(id, ServiceState::Registered, ServiceState::Running, "start")
|
|
}
|
|
|
|
/// `SvcStep.stop`: Running -> Stopping (drain begins, no new work).
|
|
pub fn request_stop(&mut self, id: ServiceId) -> Result<(), RegistryError> {
|
|
self.transition(id, ServiceState::Running, ServiceState::Stopping, "request_stop")
|
|
}
|
|
|
|
/// `SvcStep.drain`: Stopping -> Stopped (drain ack observed).
|
|
pub fn drain_done(&mut self, id: ServiceId) -> Result<(), RegistryError> {
|
|
self.transition(id, ServiceState::Stopping, ServiceState::Stopped, "drain_done")
|
|
}
|
|
|
|
/// `SvcStep.cancelReady`: Registered -> Stopped (cancelled before start).
|
|
pub fn cancel_ready(&mut self, id: ServiceId) -> Result<(), RegistryError> {
|
|
self.transition(id, ServiceState::Registered, ServiceState::Stopped, "cancel_ready")
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn two_phase_commit_no_ghost_entries() {
|
|
let mut r = Registry::new();
|
|
let id = r.prepare();
|
|
assert!(r.entries().is_empty()); // failed alloc before commit: zero ghosts
|
|
assert_eq!(r.prepare(), id); // prepare is pure
|
|
assert!(r.commit(ServiceId(99), ServiceKind::Cache).is_err()); // not prepared
|
|
assert!(r.entries().is_empty());
|
|
r.commit(id, ServiceKind::Cache).unwrap();
|
|
assert_eq!(r.entries().len(), 1);
|
|
assert_eq!(r.state(id).unwrap(), ServiceState::Registered);
|
|
assert_eq!(r.prepare(), ServiceId(1)); // fresh id, never collides
|
|
}
|
|
|
|
#[test]
|
|
fn lifecycle_happy_path_and_cancellation() {
|
|
let mut r = Registry::new();
|
|
let a = r.prepare();
|
|
r.commit(a, ServiceKind::Runtime).unwrap();
|
|
r.start(a).unwrap();
|
|
r.request_stop(a).unwrap();
|
|
assert_eq!(r.state(a).unwrap(), ServiceState::Stopping);
|
|
r.drain_done(a).unwrap();
|
|
assert_eq!(r.state(a).unwrap(), ServiceState::Stopped);
|
|
|
|
let b = r.prepare();
|
|
r.commit(b, ServiceKind::Lab).unwrap();
|
|
r.cancel_ready(b).unwrap(); // cancelled before ever running
|
|
assert_eq!(r.state(b).unwrap(), ServiceState::Stopped);
|
|
}
|
|
|
|
#[test]
|
|
fn illegal_transitions_rejected() {
|
|
let mut r = Registry::new();
|
|
let id = r.prepare();
|
|
r.commit(id, ServiceKind::Spec).unwrap();
|
|
assert!(r.request_stop(id).is_err()); // not running yet
|
|
assert!(r.drain_done(id).is_err()); // not stopping
|
|
r.start(id).unwrap();
|
|
assert!(r.start(id).is_err()); // already running
|
|
assert!(r.drain_done(id).is_err()); // must pass through Stopping
|
|
r.request_stop(id).unwrap();
|
|
assert!(r.start(id).is_err()); // no resurrection while draining
|
|
r.drain_done(id).unwrap();
|
|
assert!(r.start(id).is_err()); // stopped is terminal
|
|
assert!(r.request_stop(id).is_err());
|
|
assert!(r.state(ServiceId(42)).is_err()); // unknown service
|
|
}
|
|
} |