/-! proofs/serve_session.lean Formal contract for the `vetta-infer-gw` `--serve` request lifecycle (docs/ARCHITECTURE.md §6, consumer: strata-ctl/src/engine/supervisor.rs): 1. STOP from any active phase always drains to a DONE (never hangs). 2. `GEN 0` pre-warm reaches DONE without ever entering decode (no tokens). 3. Tokens are emitted only while decoding; a draining request emits none. 4. DONE is exited only to idle (one request cycle = one DONE). Zero `sorryAx`; checked with `lean proofs/serve_session.lean`. -/ namespace Vetta.Proofs.Serve /-- Per-request lifecycle on the engine side. `done` = DONE emitted, awaiting the next request; `stopping` = STOP received, draining in-flight work. -/ inductive ReqState where | idle -- waiting for a command (prefix may be resident) | prefilling -- GEN accepted, processing prompt chunks (PP/RESUME) | decoding -- emitting T tokens | stopping -- STOP received, draining to DONE | done -- DONE emitted for this request deriving DecidableEq, Repr /-- Legal transitions (mirrors `vetta_infer_gw::session`). -/ inductive ReqStep : ReqState → ReqState → Prop where | gen : ReqStep .idle .prefilling | prefillComplete : ReqStep .prefilling .decoding -- max_new > 0 | prewarm : ReqStep .prefilling .done -- max_new == 0: DONE immediately | finish : ReqStep .decoding .done -- stop / length / nonfinite | stopPrefill : ReqStep .prefilling .stopping | stopDecode : ReqStep .decoding .stopping | drain : ReqStep .stopping .done | nextRequest : ReqStep .done .idle inductive Steps : ReqState → ReqState → Prop where | refl (s : ReqState) : Steps s s | tail {s s' s'' : ReqState} : ReqStep s s' → Steps s' s'' → Steps s s'' /-- Theorem 1: STOP is only meaningful on an active request (prefill/decode). -/ theorem stop_only_from_active {s : ReqState} (h : ReqStep s .stopping) : s = .prefilling ∨ s = .decoding := by cases h with | stopPrefill => exact Or.inl rfl | stopDecode => exact Or.inr rfl /-- Theorem 2: STOP in idle is a no-op — it never enters draining. -/ theorem no_stop_in_idle : ¬ ReqStep .idle .stopping := by intro h; cases h /-- Theorem 3: leaving `stopping` can only land in `done` (the drain ack). -/ theorem drain_only_to_done {s : ReqState} (h : ReqStep .stopping s) : s = .done := by cases h; rfl /-- Theorem 4 (STOP guarantee): a draining request always reaches DONE. -/ theorem stopping_reaches_done : Steps .stopping .done := .tail .drain (.refl .done) /-- Theorem 5: a prefilling request always has a path to DONE (pre-warm direct). -/ theorem prefilling_reaches_done : Steps .prefilling .done := .tail .prewarm (.refl .done) /-- Theorem 6: a decoding request always has a path to DONE. -/ theorem decoding_reaches_done : Steps .decoding .done := .tail .finish (.refl .done) /-- Theorem 7 (GEN 0 pre-warm): prefill can reach DONE directly, skipping decode. -/ theorem prewarm_skips_decode : ReqStep .prefilling .done := .prewarm /-- Theorem 8: DONE is exited only to idle — one request cycle yields one DONE. -/ theorem done_only_to_idle {s : ReqState} (h : ReqStep .done s) : s = .idle := by cases h; rfl /-- Token emission is enabled only while decoding. -/ def emitsTokens : ReqState → Bool | .decoding => true | _ => false /-- Theorem 9: tokens are emitted only in `decoding` — never while prefilling, draining, or done. Combined with Theorem 3, a STOP'd request emits no token between STOP and its DONE. -/ theorem tokens_only_decoding {s : ReqState} (h : emitsTokens s = true) : s = .decoding := by cases s <;> simp [emitsTokens] at h ⊢ /-- Theorem 10: idle is re-entered only from done (clean request boundary). -/ theorem idle_only_from_done {s : ReqState} (h : ReqStep s .idle) : s = .done := by cases h; rfl -- Axiom audit (standard core axioms only, 0 sorryAx) #print axioms stop_only_from_active #print axioms no_stop_in_idle #print axioms drain_only_to_done #print axioms stopping_reaches_done #print axioms prefilling_reaches_done #print axioms decoding_reaches_done #print axioms prewarm_skips_decode #print axioms done_only_to_idle #print axioms tokens_only_decoding #print axioms idle_only_from_done end Vetta.Proofs.Serve