/-! proofs/bus_lifecycle.lean Formal contracts for the `vetta-infer-kn` microkernel (docs/ARCHITECTURE.md §2, §8): 1. Service lifecycle: STOP/cancellation always drains to a terminal state, never resurrects, never skips the draining phase. 2. Registry two-phase commit: a prepared id is fresh, a failed allocation leaves zero ghost entries, commit only adds the prepared entry. 3. Bus delivery: messages are deliverable only to a `running` service. Zero `sorryAx`; checked with `lean proofs/bus_lifecycle.lean`. -/ namespace Vetta.Proofs.Bus -- ============================================================================ -- 1. Service Lifecycle State Machine -- ============================================================================ /-- Lifecycle of one registered service. `stopping` = STOP issued, draining in-flight work; the service must reach `stopped` via an explicit drain ack. -/ inductive SvcState where | registered -- entry committed, not yet started | running -- accepting bus messages | stopping -- STOP issued, draining (no new work accepted) | stopped -- terminal: resources released deriving DecidableEq, Repr /-- Legal single-service transitions (mirrors `vetta_infer_kn::registry`). -/ inductive SvcStep : SvcState → SvcState → Prop where | start : SvcStep .registered .running | stop : SvcStep .running .stopping | drain : SvcStep .stopping .stopped | cancelReady : SvcStep .registered .stopped -- cancelled before start /-- Reflexive-transitive closure of `SvcStep`. -/ inductive Steps : SvcState → SvcState → Prop where | refl (s : SvcState) : Steps s s | tail {s s' s'' : SvcState} : SvcStep s s' → Steps s' s'' → Steps s s'' /-- Theorem 1: `stopped` is terminal — no transition ever leaves it. -/ theorem stopped_terminal {s : SvcState} : ¬ SvcStep .stopped s := by intro h; cases h /-- Theorem 2: a draining service can never go back to `running`. -/ theorem no_resurrect_stopping : ¬ SvcStep .stopping .running := by intro h; cases h /-- Theorem 3: a stopped service can never go back to `running`. -/ theorem no_resurrect_stopped : ¬ SvcStep .stopped .running := by intro h; cases h /-- Theorem 4: `running` cannot jump straight to `stopped`: the drain phase is mandatory (in-flight work is always observed before release). -/ theorem no_skip_drain : ¬ SvcStep .running .stopped := by intro h; cases h /-- Theorem 5: entering `stopping` is only possible from `running` (STOP is only meaningful on a live service). -/ theorem stop_only_from_running {s : SvcState} (h : SvcStep s .stopping) : s = .running := by cases h; rfl /-- Theorem 6: leaving `stopping` can only land in `stopped` (drain ack). -/ theorem drain_only_to_stopped {s : SvcState} (h : SvcStep .stopping s) : s = .stopped := by cases h; rfl /-- Theorem 7 (progress): a running service always has a path to `stopped`. -/ theorem running_reaches_stopped : Steps .running .stopped := .tail .stop (.tail .drain (.refl .stopped)) /-- Theorem 8 (progress): a registered-but-not-started service can be cancelled to `stopped` without ever running. -/ theorem registered_reaches_stopped : Steps .registered .stopped := .tail .cancelReady (.refl .stopped) /-- Theorem 9 (progress): a draining service always has a path to `stopped`. -/ theorem stopping_reaches_stopped : Steps .stopping .stopped := .tail .drain (.refl .stopped) -- ============================================================================ -- 2. Registry Two-Phase Commit (no ghost entries, fresh ids) -- ============================================================================ /-- Committed registry entries only. A pending (prepared but not committed) entry lives OUTSIDE this structure, so it is unobservable by construction. -/ structure Registry where entries : List (Nat × SvcState) nextId : Nat /-- Well-formedness: every committed id was handed out before `nextId`. -/ def Registry.wf (r : Registry) : Prop := ∀ p ∈ r.entries, p.1 < r.nextId /-- Phase 1 (pure peek): the candidate id for a new service. No mutation. -/ def prepare (r : Registry) : Nat := r.nextId /-- Phase 2: commit the prepared entry. Called ONLY after the service's resources were successfully allocated (mirrors `commit_slot` discipline). -/ def commit (r : Registry) : Registry := { entries := (r.nextId, .registered) :: r.entries, nextId := r.nextId + 1 } /-- Theorem 10: the prepared id is fresh — it never collides with a committed entry. -/ theorem prepare_id_fresh (r : Registry) (hwf : r.wf) : ∀ p ∈ r.entries, p.1 ≠ prepare r := by intro p hp heq have hlt := hwf p hp simp [prepare] at heq omega /-- Theorem 11: committing the prepared entry preserves well-formedness. -/ theorem commit_preserves_wf (r : Registry) (hwf : r.wf) : (commit r).wf := by intro p hp simp [commit] at hp ⊢ rcases hp with ⟨rfl, rfl⟩ | hp · omega · have := hwf p hp omega /-- Theorem 12: commit only ADDS the prepared entry; every pre-existing entry is preserved untouched (a failed allocation that never calls `commit` leaves the registry bit-identical — no half-committed state is observable). -/ theorem commit_only_adds (r : Registry) : (commit r).entries = (r.nextId, .registered) :: r.entries := rfl -- ============================================================================ -- 3. Bus Delivery Rules -- ============================================================================ /-- A bus message is deliverable only to a running service. -/ def deliverable : SvcState → Bool | .running => true | _ => false /-- Theorem 13: deliverability implies `running` — no message ever reaches a registered, draining, or stopped service. -/ theorem deliver_only_running {s : SvcState} (h : deliverable s = true) : s = .running := by cases s <;> simp [deliverable] at h ⊢ /-- Theorem 14: STOP itself is accepted only where delivery is legal, and it is exactly the transition into draining. -/ theorem stop_is_delivery_gated {s s' : SvcState} (h : SvcStep s s') (hs : s' = .stopping) : deliverable s = true := by subst hs cases h rfl -- Axiom audit (all theorems must rely only on standard core axioms, 0 sorryAx) #print axioms stopped_terminal #print axioms no_resurrect_stopping #print axioms no_resurrect_stopped #print axioms no_skip_drain #print axioms stop_only_from_running #print axioms drain_only_to_stopped #print axioms running_reaches_stopped #print axioms registered_reaches_stopped #print axioms stopping_reaches_stopped #print axioms prepare_id_fresh #print axioms commit_preserves_wf #print axioms commit_only_adds #print axioms deliver_only_running #print axioms stop_is_delivery_gated end Vetta.Proofs.Bus