interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / helix / proofs / PhysicsDispatch.lean

1189 lines · 58966 bytes Raw Blame History
1/-
2 SPDX-License-Identifier: Apache-2.0
3 seed/helix/proofs/PhysicsDispatch.lean
4
5 Standalone machine-checked Lean 4 safety proof for Sovereign Intelligence 2.0 Stage 3
6 (Pillar 3: Physics-Typed Computing — `seed/ng/physics/phys_solver.om` <-> `watt_cbf.om`
7 <-> `fabHotSwap`).
8
9 Zero `sorry`, zero warnings, zero Mathlib dependency, standard Lean 4 axioms only.
10
11 Proved properties (exact 1:1 fixed-point mirror of `phys_solver.om` and `watt_cbf.om`):
12 1. Inviolable Hardware `WattCBF` Power & Thermal Safety (`admitted_respects_wattcbf`):
13 Every realization dispatched by `solveDispatch` is proof-verified (`proofMask = 7`),
14 admitted un-clamped by `cbfFilter` (`failClosed = false`), respects the Trap-14 80 W
15 GPU hard ceiling (`pGpu <= min 80000 pol.pGpuBudget`), whole-unit Watt conservation
16 (`pGpu + pCpu + pUnc <= min pol.pBudget 140000`), discrete CBF barrier invariance
17 (`barrierH >= 0` and next-step `tDie' <= tCeil`), and the ΩIR v2 `@power` ceiling.
18 2. Inviolable Declared `@err` Accuracy Bound (`admitted_respects_err_bound`):
19 Every dispatched realization satisfies `0 <= r.errBound <= contract.errBound`, and
20 whenever `contract.precisionTier = 1` (`@exact`), `r.errBound = 0` strictly.
21 3. Hard vs. Soft Deadline Semantics & Optimality (`hard_deadline_fail_closed`,
22 `soft_deadline_slip_never_silent`, `admitted_is_min_latency_among_safe`,
23 `unproved_never_admitted`):
24 - When `@hard_deadline = true` and no safe realization meets `@deadline`, the solver
25 fails closed (`refuse`) and NEVER dispatches an unsafe or late realization.
26 - When `@hard_deadline = false` and no safe realization meets `@deadline`, the solver
27 selects the minimum-latency safe realization inside (1) & (2) AND emits an explicit
28 structured `DispatchAlarm.deadlineSlip` (never silent).
29 - No realization with `proofMask != 7` is ever dispatched.
30 4. Composed Multi-Stage Chain Error Soundness (`chain_error_composition_sound`,
31 `chain_overflow_refused`):
32 For any sequential call chain of proof-bounded stages whose per-stage error bounds
33 sum to `<= outerErr`, the composed end-to-end deviation is bounded by `outerErr`;
34 any chain exceeding `outerErr` is refused.
35 5. Non-Vacuity Negative-Control Witnesses (axiom-free via `decide`):
36 - `neg_control_trap14_90w_refused`: 90 W Trap-14 GPU spike refused by solver.
37 - `neg_control_err_violation_refused`: loosened-precision `@err` violation refused.
38 - `neg_control_hard_deadline_fail_closed`: tight `@hard_deadline` at 84 C fails closed.
39 - `neg_control_soft_deadline_alarm`: same 84 C state with soft deadline emits explicit
40 `deadlineSlip 5400` alarm.
41 - `neg_control_unproved_mask_refused`: missing proof bit (`proofMask = 5`) refused.
42 - `neg_control_chain_err_overflow_refused`: 3-stage error overflow `[40, 35, 30] > 100`
43 refused while `[40, 35, 25] <= 100` is admitted.
44-/
45
46namespace PhysicsDispatch
47
48-- =========================================================================
49-- Section 0: Exact Fixed-Point Mirror of WattCBF (`apps/omega-wattd/watt_cbf.om`)
50-- =========================================================================
51
52def P_GPU_HARD_MAX_MW : Int := 80000
53def P_TOTAL_HARD_MAX_MW : Int := 140000
54def P_UNC_HARD_MAX_MW : Int := 25000
55def T_CEIL_ABS_MAX_MC : Int := 90000
56def FAN_MIN_PCT : Int := 20
57def FAN_MAX_PCT : Int := 100
58
59def THERMAL_SCALE : Int := 1000
60def K_GPU_HEAT : Int := 25
61def K_CPU_HEAT : Int := 5
62def K_DS_COND : Int := 100
63
64def PROOF_REQUIRED : Int := 7
65def TIER_UNSPEC : Int := 0
66def TIER_EXACT : Int := 1
67def TIER_TOL : Int := 2
68
69structure ThermalState where
70 tDie : Int
71 tSink : Int
72 tAmb : Int
73 deriving Repr, DecidableEq
74
75structure WattPolicy where
76 pBudget : Int
77 pGpuBudget : Int
78 tCeil : Int
79 tau : Int
80 deriving Repr, DecidableEq
81
82structure PowerProposal where
83 pGpu : Int
84 pCpu : Int
85 pUnc : Int
86 fanPct : Int
87 deriving Repr, DecidableEq
88
89structure SafeCommand where
90 pGpu : Int
91 pCpu : Int
92 pUnc : Int
93 fanPct : Int
94 failClosed : Bool
95 clamped : Bool
96 deriving Repr, DecidableEq
97
98structure RailTriple where
99 pUnc : Int
100 pCpu : Int
101 pGpu : Int
102 deriving Repr, DecidableEq
103
104def ceilDiv (a b : Int) : Int :=
105 if a ≥ 0 then (a + b - 1) / b else -((-a) / b)
106
107theorem ceilDiv_le_iff (a b k : Int) (hb : b > 0) :
108 ceilDiv a b ≤ k ↔ a ≤ b * k := by
109 unfold ceilDiv
110 split
111 · constructor
112 · intro h
113 have h1 : (a + b - 1) / b * b ≤ k * b := Int.mul_le_mul_of_nonneg_right h (by omega)
114 have h2 := Int.ediv_mul_add_emod (a + b - 1) b
115 have hmod := Int.emod_lt_of_pos (a + b - 1) hb
116 have hcomm : k * b = b * k := Int.mul_comm k b
117 omega
118 · intro h
119 have hcomm : b * k = k * b := Int.mul_comm b k
120 have hle : a + b - 1 ≤ k * b + (b - 1) := by omega
121 have h3 : (k * b + (b - 1)) / b ≤ k := by
122 have hk : k * b + (b - 1) < (k + 1) * b := by
123 have := Int.add_mul k 1 b
124 omega
125 have heq := Int.ediv_mul_add_emod (k * b + (b - 1)) b
126 have hnn := Int.emod_nonneg (k * b + (b - 1)) (by omega : b ≠ 0)
127 by_cases hgt : (k * b + (b - 1)) / b ≤ k
128 · exact hgt
129 · have hge : k + 1 ≤ (k * b + (b - 1)) / b := by omega
130 have hm := Int.mul_le_mul_of_nonneg_right hge (by omega : 0 ≤ b)
131 omega
132 have hmono := Int.ediv_le_ediv hb hle
133 omega
134 · constructor
135 · intro h
136 have hneg : -k ≤ (-a) / b := by omega
137 have h1 : (-k) * b ≤ ((-a) / b) * b := Int.mul_le_mul_of_nonneg_right hneg (by omega)
138 have h2 := Int.ediv_mul_add_emod (-a) b
139 have hnn := Int.emod_nonneg (-a) (by omega : b ≠ 0)
140 have hcomm : k * b = b * k := Int.mul_comm k b
141 have hng : (-k) * b = -(k * b) := Int.neg_mul k b
142 omega
143 · intro h
144 have hng : (-k) * b = -(b * k) := by
145 have hcomm : k * b = b * k := Int.mul_comm k b
146 have hn : (-k) * b = -(k * b) := Int.neg_mul k b
147 omega
148 have hle : (-k) * b ≤ -a := by omega
149 have hdiv := Int.le_ediv_of_mul_le hb hle
150 omega
151
152def barrierH (tCeil tDie tau dT : Int) : Int :=
153 tCeil - tDie - tau * dT
154
155def dieHeat (pGpu pCpu pUnc : Int) : Int :=
156 K_GPU_HEAT * pGpu + K_CPU_HEAT * (pCpu + pUnc)
157
158def dieCoolDs (st : ThermalState) : Int :=
159 K_DS_COND * (st.tDie - st.tSink)
160
161def nextDieDelta (st : ThermalState) (pGpu pCpu pUnc : Int) : Int :=
162 ceilDiv (dieHeat pGpu pCpu pUnc - dieCoolDs st) THERMAL_SCALE
163
164def clampNonNeg (x hi : Int) : Int :=
165 if hi ≤ 0 then 0
166 else if x ≤ 0 then 0
167 else if x ≥ hi then hi
168 else x
169
170def policyValid (pol : WattPolicy) : Bool :=
171 decide (pol.pBudget > 0 ∧ pol.pGpuBudget ≥ 0 ∧ pol.tCeil > 0 ∧
172 pol.tCeil ≤ T_CEIL_ABS_MAX_MC ∧ pol.tau ≥ 0)
173
174def rawHeatBudget (st : ThermalState) (pol : WattPolicy) : Int :=
175 if pol.tau < 0 then 0
176 else THERMAL_SCALE * ((pol.tCeil - st.tDie) / (pol.tau + 1)) + dieCoolDs st
177
178def maxHeatSafe (st : ThermalState) (pol : WattPolicy) : Int :=
179 if st.tDie ≥ pol.tCeil ∨ st.tSink ≥ pol.tCeil ∨ pol.tau < 0 then 0
180 else max 0 (rawHeatBudget st pol)
181
182def mustFailClosed (st : ThermalState) (pol : WattPolicy) : Bool :=
183 !policyValid pol ||
184 decide (st.tDie ≥ pol.tCeil) ||
185 decide (st.tSink ≥ pol.tCeil) ||
186 decide (rawHeatBudget st pol < 0)
187
188def safeFanFloor (headroom : Int) (thermalClamped : Bool) : Int :=
189 if thermalClamped || decide (headroom ≤ 5000) then FAN_MAX_PCT
190 else if decide (headroom ≤ 10000) then 85
191 else if decide (headroom ≤ 18000) then 65
192 else if decide (headroom ≤ 28000) then 45
193 else FAN_MIN_PCT
194
195def powerClampUnc (pol : WattPolicy) (prop : PowerProposal) : Int :=
196 clampNonNeg prop.pUnc (min P_UNC_HARD_MAX_MW (min pol.pBudget P_TOTAL_HARD_MAX_MW))
197
198def powerClampCpu (pol : WattPolicy) (prop : PowerProposal) : Int :=
199 clampNonNeg prop.pCpu (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop)
200
201def powerClampGpu (pol : WattPolicy) (prop : PowerProposal) : Int :=
202 clampNonNeg prop.pGpu
203 (min (min P_GPU_HARD_MAX_MW (min pol.pGpuBudget (min pol.pBudget P_TOTAL_HARD_MAX_MW)))
204 (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop - powerClampCpu pol prop))
205
206def powerClamp (pol : WattPolicy) (prop : PowerProposal) : RailTriple :=
207 { pUnc := powerClampUnc pol prop
208 pCpu := powerClampCpu pol prop
209 pGpu := powerClampGpu pol prop }
210
211def thermalHeatUnc (r : RailTriple) (maxHeat : Int) : Int :=
212 min (K_CPU_HEAT * r.pUnc) maxHeat
213
214def thermalHeatCpu (r : RailTriple) (maxHeat : Int) : Int :=
215 min (K_CPU_HEAT * r.pCpu) (maxHeat - thermalHeatUnc r maxHeat)
216
217def thermalHeatGpu (r : RailTriple) (maxHeat : Int) : Int :=
218 min (K_GPU_HEAT * r.pGpu) (maxHeat - thermalHeatUnc r maxHeat - thermalHeatCpu r maxHeat)
219
220def thermalClamp (r : RailTriple) (maxHeat : Int) : RailTriple :=
221 { pUnc := thermalHeatUnc r maxHeat / K_CPU_HEAT
222 pCpu := thermalHeatCpu r maxHeat / K_CPU_HEAT
223 pGpu := thermalHeatGpu r maxHeat / K_GPU_HEAT }
224
225def cbfFilter (st : ThermalState) (pol : WattPolicy) (prop : PowerProposal) : SafeCommand :=
226 if mustFailClosed st pol then
227 { pGpu := 0
228 pCpu := 0
229 pUnc := 0
230 fanPct := FAN_MAX_PCT
231 failClosed := true
232 clamped := true }
233 else
234 let r1 := powerClamp pol prop
235 let r2 := thermalClamp r1 (maxHeatSafe st pol)
236 let headroom := pol.tCeil - st.tDie
237 let thermClamp := decide (r2.pUnc < r1.pUnc ∨ r2.pCpu < r1.pCpu ∨ r2.pGpu < r1.pGpu)
238 let fanFloor := safeFanFloor headroom thermClamp
239 let fanOut := min FAN_MAX_PCT (max fanFloor (max FAN_MIN_PCT prop.fanPct))
240 let clamped := decide (r2.pGpu ≠ prop.pGpu ∨ r2.pCpu ≠ prop.pCpu ∨
241 r2.pUnc ≠ prop.pUnc ∨ fanOut ≠ prop.fanPct)
242 { pGpu := r2.pGpu
243 pCpu := r2.pCpu
244 pUnc := r2.pUnc
245 fanPct := fanOut
246 failClosed := false
247 clamped := clamped }
248
249-- =========================================================================
250-- Section 1: WattCBF Core Safety Lemmas (Self-Contained)
251-- =========================================================================
252
253private theorem mul_ediv_bounds (x k : Int) (hk : k > 0) (hx : 0 ≤ x) :
254 0 ≤ x / k ∧ k * (x / k) ≤ x := by
255 have h1 : 0 ≤ x / k := Int.ediv_nonneg hx (by omega)
256 have h2 := Int.mul_ediv_add_emod x k
257 have hmod := Int.emod_nonneg x (by omega : k ≠ 0)
258 omega
259
260private theorem min_mul_ediv_le (y m k : Int) (hk : k > 0) :
261 min (k * y) m / k ≤ y := by
262 have hle : min (k * y) m ≤ y * k := by
263 have hcomm : k * y = y * k := Int.mul_comm k y
264 have hm : min (k * y) m ≤ k * y := Int.min_le_left _ _
265 omega
266 exact Int.ediv_le_of_le_mul hk hle
267
268private theorem clampNonNeg_bounds (x hi : Int) :
269 0 ≤ clampNonNeg x hi ∧ (0 ≤ hi → clampNonNeg x hi ≤ hi) ∧
270 (0 ≤ x → clampNonNeg x hi ≤ x) := by
271 unfold clampNonNeg
272 by_cases h1 : hi ≤ 0
273 · simp [h1]
274 · by_cases h2 : x ≤ 0
275 · simp [h1, h2]
276 · by_cases h3 : x ≥ hi
277 · simp [h1, h2, h3]; omega
278 · simp [h1, h2, h3]; omega
279
280private theorem maxHeatSafe_nonneg (st : ThermalState) (pol : WattPolicy) :
281 0 ≤ maxHeatSafe st pol := by
282 unfold maxHeatSafe
283 split <;> omega
284
285private theorem powerClamp_bounds (pol : WattPolicy) (prop : PowerProposal)
286 (hb : 0 ≤ pol.pBudget) (hgb : 0 ≤ pol.pGpuBudget) :
287 let r := powerClamp pol prop
288 0 ≤ r.pUnc ∧ 0 ≤ r.pCpu ∧ 0 ≤ r.pGpu ∧
289 r.pUnc ≤ P_UNC_HARD_MAX_MW ∧
290 r.pGpu ≤ min P_GPU_HARD_MAX_MW pol.pGpuBudget ∧
291 r.pGpu + r.pCpu + r.pUnc ≤ min pol.pBudget P_TOTAL_HARD_MAX_MW := by
292 dsimp only [powerClamp]
293 have hu : 0 ≤ powerClampUnc pol prop ∧
294 (0 ≤ min P_UNC_HARD_MAX_MW (min pol.pBudget P_TOTAL_HARD_MAX_MW) →
295 powerClampUnc pol prop ≤ min P_UNC_HARD_MAX_MW (min pol.pBudget P_TOTAL_HARD_MAX_MW)) := by
296 unfold powerClampUnc
297 have h := clampNonNeg_bounds prop.pUnc (min P_UNC_HARD_MAX_MW (min pol.pBudget P_TOTAL_HARD_MAX_MW))
298 exact ⟨h.1, h.2.1⟩
299 have hc : 0 ≤ powerClampCpu pol prop ∧
300 (0 ≤ min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop →
301 powerClampCpu pol prop ≤ min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop) := by
302 unfold powerClampCpu
303 have h := clampNonNeg_bounds prop.pCpu (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop)
304 exact ⟨h.1, h.2.1⟩
305 have hg : 0 ≤ powerClampGpu pol prop ∧
306 (0 ≤ min (min P_GPU_HARD_MAX_MW (min pol.pGpuBudget (min pol.pBudget P_TOTAL_HARD_MAX_MW)))
307 (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop - powerClampCpu pol prop) →
308 powerClampGpu pol prop ≤
309 min (min P_GPU_HARD_MAX_MW (min pol.pGpuBudget (min pol.pBudget P_TOTAL_HARD_MAX_MW)))
310 (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop - powerClampCpu pol prop)) := by
311 unfold powerClampGpu
312 have h := clampNonNeg_bounds prop.pGpu
313 (min (min P_GPU_HARD_MAX_MW (min pol.pGpuBudget (min pol.pBudget P_TOTAL_HARD_MAX_MW)))
314 (min pol.pBudget P_TOTAL_HARD_MAX_MW - powerClampUnc pol prop - powerClampCpu pol prop))
315 exact ⟨h.1, h.2.1⟩
316 unfold P_GPU_HARD_MAX_MW P_TOTAL_HARD_MAX_MW P_UNC_HARD_MAX_MW at *
317 omega
318
319private theorem thermalClamp_le_input (r : RailTriple) (maxHeat : Int) :
320 let t := thermalClamp r maxHeat
321 t.pUnc ≤ r.pUnc ∧ t.pCpu ≤ r.pCpu ∧ t.pGpu ≤ r.pGpu := by
322 dsimp only [thermalClamp, thermalHeatUnc, thermalHeatCpu, thermalHeatGpu]
323 exact ⟨min_mul_ediv_le r.pUnc maxHeat K_CPU_HEAT (by decide),
324 min_mul_ediv_le r.pCpu _ K_CPU_HEAT (by decide),
325 min_mul_ediv_le r.pGpu _ K_GPU_HEAT (by decide)⟩
326
327private theorem thermalClamp_nonneg_and_heat (r : RailTriple) (maxHeat : Int)
328 (hu : 0 ≤ r.pUnc) (hc : 0 ≤ r.pCpu) (hg : 0 ≤ r.pGpu) (hm : 0 ≤ maxHeat) :
329 let t := thermalClamp r maxHeat
330 0 ≤ t.pUnc ∧ 0 ≤ t.pCpu ∧ 0 ≤ t.pGpu ∧
331 dieHeat t.pGpu t.pCpu t.pUnc ≤ maxHeat := by
332 dsimp only [thermalClamp]
333 have hhu : 0 ≤ thermalHeatUnc r maxHeat := by unfold thermalHeatUnc K_CPU_HEAT; omega
334 have hhc : 0 ≤ thermalHeatCpu r maxHeat := by
335 unfold thermalHeatCpu thermalHeatUnc K_CPU_HEAT; omega
336 have hhg : 0 ≤ thermalHeatGpu r maxHeat := by
337 unfold thermalHeatGpu thermalHeatCpu thermalHeatUnc K_GPU_HEAT K_CPU_HEAT; omega
338 have bu := mul_ediv_bounds (thermalHeatUnc r maxHeat) K_CPU_HEAT (by decide) hhu
339 have bc := mul_ediv_bounds (thermalHeatCpu r maxHeat) K_CPU_HEAT (by decide) hhc
340 have bg := mul_ediv_bounds (thermalHeatGpu r maxHeat) K_GPU_HEAT (by decide) hhg
341 have hsum : thermalHeatUnc r maxHeat + thermalHeatCpu r maxHeat + thermalHeatGpu r maxHeat ≤ maxHeat := by
342 unfold thermalHeatGpu thermalHeatCpu thermalHeatUnc; omega
343 unfold dieHeat K_GPU_HEAT K_CPU_HEAT at *
344 omega
345
346private theorem policyValid_of_not_guard (st : ThermalState) (pol : WattPolicy)
347 (h : ¬ (mustFailClosed st pol = true)) :
348 pol.pBudget > 0 ∧ pol.pGpuBudget ≥ 0 ∧ pol.tCeil > 0 ∧
349 pol.tCeil ≤ T_CEIL_ABS_MAX_MC ∧ pol.tau ≥ 0 ∧
350 st.tDie < pol.tCeil ∧ st.tSink < pol.tCeil ∧
351 0 ≤ rawHeatBudget st pol := by
352 unfold mustFailClosed at h
353 have hv : policyValid pol = true := by
354 cases hpv : policyValid pol <;> simp [hpv] at h ⊢
355 have hdie : ¬ (st.tDie ≥ pol.tCeil) := by intro hge; simp [hge] at h
356 have hsink : ¬ (st.tSink ≥ pol.tCeil) := by intro hge; simp [hge] at h
357 have hraw : ¬ (rawHeatBudget st pol < 0) := by intro hlt; simp [hlt] at h
358 unfold policyValid at hv
359 simp only [decide_eq_true_eq] at hv
360 omega
361
362theorem cbf_admitted_safe
363 (st : ThermalState) (pol : WattPolicy) (prop : PowerProposal)
364 (hadmit : (cbfFilter st pol prop).failClosed = false) :
365 let out := cbfFilter st pol prop
366 let dT := nextDieDelta st out.pGpu out.pCpu out.pUnc
367 out.pGpu ≤ min P_GPU_HARD_MAX_MW pol.pGpuBudget ∧
368 out.pGpu + out.pCpu + out.pUnc ≤ min pol.pBudget P_TOTAL_HARD_MAX_MW ∧
369 barrierH pol.tCeil st.tDie pol.tau dT ≥ 0 ∧
370 st.tDie + dT ≤ pol.tCeil := by
371 dsimp only
372 have hng : ¬ (mustFailClosed st pol = true) := by
373 intro hfc; unfold cbfFilter at hadmit; simp [hfc] at hadmit
374 have hp := policyValid_of_not_guard st pol hng
375 have hok : (cbfFilter st pol prop).pGpu = (thermalClamp (powerClamp pol prop) (maxHeatSafe st pol)).pGpu ∧
376 (cbfFilter st pol prop).pCpu = (thermalClamp (powerClamp pol prop) (maxHeatSafe st pol)).pCpu ∧
377 (cbfFilter st pol prop).pUnc = (thermalClamp (powerClamp pol prop) (maxHeatSafe st pol)).pUnc := by
378 unfold cbfFilter; rw [ite_eq_right hng]; exact ⟨rfl, rfl, rfl⟩
379 have pb := powerClamp_bounds pol prop (by omega) (by omega)
380 have tc := thermalClamp_le_input (powerClamp pol prop) (maxHeatSafe st pol)
381 have th := thermalClamp_nonneg_and_heat (powerClamp pol prop) (maxHeatSafe st pol)
382 pb.1 pb.2.1 pb.2.2.1 (maxHeatSafe_nonneg st pol)
383 have hheat : dieHeat (cbfFilter st pol prop).pGpu (cbfFilter st pol prop).pCpu
384 (cbfFilter st pol prop).pUnc ≤ maxHeatSafe st pol := by
385 rw [hok.1, hok.2.1, hok.2.2]; exact th.2.2.2
386 have hraw : 0 ≤ THERMAL_SCALE * ((pol.tCeil - st.tDie) / (pol.tau + 1)) + dieCoolDs st := by
387 have h1 := hp.2.2.2.2.2.2.2
388 unfold rawHeatBudget at h1
389 have hnt : ¬ (pol.tau < 0) := by omega
390 simp only [hnt, ↓reduceIte] at h1
391 exact h1
392 have hmh_le : maxHeatSafe st pol ≤
393 THERMAL_SCALE * ((pol.tCeil - st.tDie) / (pol.tau + 1)) + dieCoolDs st := by
394 unfold maxHeatSafe rawHeatBudget
395 have hntau : ¬ (pol.tau < 0) := by omega
396 simp only [hntau, ↓reduceIte]
397 split <;> omega
398 have hdTle : nextDieDelta st (cbfFilter st pol prop).pGpu (cbfFilter st pol prop).pCpu
399 (cbfFilter st pol prop).pUnc ≤ (pol.tCeil - st.tDie) / (pol.tau + 1) := by
400 unfold nextDieDelta
401 rw [ceilDiv_le_iff _ THERMAL_SCALE _ (by decide)]
402 omega
403 have hdiv := mul_ediv_bounds (pol.tCeil - st.tDie) (pol.tau + 1) (by omega) (by omega)
404 have htau1 : (pol.tau + 1) * nextDieDelta st (cbfFilter st pol prop).pGpu
405 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc ≤ pol.tCeil - st.tDie := by
406 have hmul := Int.mul_le_mul_of_nonneg_left hdTle (by omega : 0 ≤ pol.tau + 1)
407 omega
408 have hdist : (pol.tau + 1) * nextDieDelta st (cbfFilter st pol prop).pGpu
409 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc =
410 pol.tau * nextDieDelta st (cbfFilter st pol prop).pGpu
411 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc +
412 nextDieDelta st (cbfFilter st pol prop).pGpu
413 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc := by
414 have := Int.add_mul pol.tau 1 (nextDieDelta st (cbfFilter st pol prop).pGpu
415 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc)
416 omega
417 have hdT_bound : nextDieDelta st (cbfFilter st pol prop).pGpu
418 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc ≤ pol.tCeil - st.tDie := by
419 by_cases hpos : 0 ≤ nextDieDelta st (cbfFilter st pol prop).pGpu
420 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc
421 · have : 0 ≤ pol.tau * nextDieDelta st (cbfFilter st pol prop).pGpu
422 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc := Int.mul_nonneg (by omega) hpos
423 omega
424 · omega
425 have htau_bound : pol.tau * nextDieDelta st (cbfFilter st pol prop).pGpu
426 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc ≤ pol.tCeil - st.tDie := by
427 by_cases hpos : 0 ≤ nextDieDelta st (cbfFilter st pol prop).pGpu
428 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc
429 · omega
430 · have : pol.tau * nextDieDelta st (cbfFilter st pol prop).pGpu
431 (cbfFilter st pol prop).pCpu (cbfFilter st pol prop).pUnc ≤ 0 :=
432 Int.mul_nonpos_of_nonneg_of_nonpos (by omega) (by omega)
433 omega
434 unfold barrierH
435 refine ⟨by omega, by omega, by omega, by omega⟩
436
437-- =========================================================================
438-- Section 2: Physics-Typed Dispatch Model (`pareto_bank.om` + `phys_solver.om`)
439-- =========================================================================
440
441structure Realization where
442 semId : Int
443 targetTid : Int
444 isGpuTarget : Bool
445 precisionId : Int
446 estPowerMw : Int
447 estLatencyUs : Int
448 estJoulesUj : Int
449 errBound : Int
450 proofMask : Int
451 deriving Repr, DecidableEq
452
453structure Contract where
454 purityFlag : Int -- 0 or 1
455 precisionTier : Int -- 0=unspec, 1=exact, 2=tol
456 hardDeadline : Bool -- false=soft/none, true=@hard_deadline
457 errBound : Int -- >= 0; 0 unless precisionTier == 2
458 deadlineUs : Int -- >= 0; > 0 when hardDeadline == true
459 powerMw : Int -- >= 0 (0 = none)
460 deriving Repr, DecidableEq
461
462structure BaselineLoad where
463 baseCpuMw : Int
464 baseUncMw : Int
465 fanReqPct : Int
466 deriving Repr, DecidableEq
467
468inductive DispatchAlarm where
469 | none
470 | deadlineSlip (slipUs : Int)
471 deriving Repr, DecidableEq
472
473inductive RefusalReason where
474 | badContract
475 | badBaseline
476 | wattCbfFailClosed
477 | noSafeRealization
478 | hardDeadlineMiss
479 deriving Repr, DecidableEq
480
481inductive DispatchResult where
482 | dispatch (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
483 | refuse (reason : RefusalReason)
484 deriving Repr, DecidableEq
485
486/-- Canonical ΩIR v2 contract validation (exact mirror of `ps_contract_valid` & `IR.md` §16.1). -/
487def contractValid (c : Contract) : Bool :=
488 decide (0 ≤ c.purityFlag ∧ c.purityFlag ≤ 1 ∧
489 0 ≤ c.precisionTier ∧ c.precisionTier ≤ 2 ∧
490 0 ≤ c.errBound ∧ 0 ≤ c.deadlineUs ∧ 0 ≤ c.powerMw ∧
491 (c.precisionTier ≠ TIER_TOL → c.errBound = 0) ∧
492 (c.hardDeadline = true → c.deadlineUs > 0))
493
494/-- Map realization onto physical WattCBF rails (GPU vs CPU + baseline loads). -/
495def realizationProposal (base : BaselineLoad) (r : Realization) : PowerProposal :=
496 if r.isGpuTarget then
497 { pGpu := r.estPowerMw, pCpu := base.baseCpuMw, pUnc := base.baseUncMw, fanPct := base.fanReqPct }
498 else
499 { pGpu := 0, pCpu := base.baseCpuMw + r.estPowerMw, pUnc := base.baseUncMw, fanPct := base.fanReqPct }
500
501/-- Proof gate: exact `proofMask == 7` (`Contract | EGP3 | Antibody`) plus valid physical metrics. -/
502def isProofVerified (r : Realization) : Bool :=
503 decide (r.proofMask = PROOF_REQUIRED ∧ r.estPowerMw ≥ 0 ∧ r.estLatencyUs > 0 ∧
504 r.estJoulesUj ≥ 0 ∧ r.errBound ≥ 0)
505
506/-- Priority 1 (INVIOLABLE): `WattCBF` admits realization un-clamped AND `@power` cap holds. -/
507def isWattCbfSafe (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
508 (c : Contract) (r : Realization) : Bool :=
509 let prop := realizationProposal base r
510 let cmd := cbfFilter st pol prop
511 !cmd.failClosed &&
512 decide (cmd.pGpu = prop.pGpu ∧ cmd.pCpu = prop.pCpu ∧ cmd.pUnc = prop.pUnc) &&
513 decide (c.powerMw = 0 ∨ r.estPowerMw ≤ c.powerMw)
514
515/-- Priority 2 (INVIOLABLE): proven realization error bound never exceeds contract `@err`. -/
516def isErrSafe (c : Contract) (r : Realization) : Bool :=
517 decide (r.errBound ≤ c.errBound)
518
519/-- Full safety admission predicate inside Priorities (1) & (2). -/
520def isSafeCandidate (semId : Int) (st : ThermalState) (pol : WattPolicy)
521 (base : BaselineLoad) (c : Contract) (r : Realization) : Bool :=
522 decide (r.semId = semId) &&
523 isProofVerified r &&
524 isWattCbfSafe st pol base c r &&
525 isErrSafe c r
526
527/-- Strict deterministic preference order inside `(1) & (2)`:
528 minimize latency, tie-breaking by lower error, lower energy (uJ), lower power (mW). -/
529def betterCandidate (a b : Realization) : Bool :=
530 if a.estLatencyUs < b.estLatencyUs then true
531 else if a.estLatencyUs > b.estLatencyUs then false
532 else if a.errBound < b.errBound then true
533 else if a.errBound > b.errBound then false
534 else if a.estJoulesUj < b.estJoulesUj then true
535 else if a.estJoulesUj > b.estJoulesUj then false
536 else decide (a.estPowerMw < b.estPowerMw)
537
538/-- Fold over Pareto bank to pick the minimum-latency safe candidate inside `(1) & (2)`. -/
539def selectFastestLoop (semId : Int) (st : ThermalState) (pol : WattPolicy)
540 (base : BaselineLoad) (c : Contract) (acc : Option Realization) :
541 List Realization → Option Realization
542 | [] => acc
543 | r :: rest =>
544 if isSafeCandidate semId st pol base c r then
545 let acc' := match acc with
546 | none => some r
547 | some cur => if betterCandidate r cur then some r else some cur
548 selectFastestLoop semId st pol base c acc' rest
549 else
550 selectFastestLoop semId st pol base c acc rest
551
552def selectFastestSafe (semId : Int) (st : ThermalState) (pol : WattPolicy)
553 (base : BaselineLoad) (c : Contract) (bank : List Realization) : Option Realization :=
554 selectFastestLoop semId st pol base c none bank
555
556/-- Real-Time Physics Dispatch Solver (exact mirror of `ps_dispatch` in `phys_solver.om`). -/
557def solveDispatch (semId : Int) (st : ThermalState) (pol : WattPolicy)
558 (base : BaselineLoad) (c : Contract) (bank : List Realization) : DispatchResult :=
559 if !contractValid c then
560 .refuse .badContract
561 else if decide (base.baseCpuMw < 0 ∨ base.baseUncMw < 0) then
562 .refuse .badBaseline
563 else if mustFailClosed st pol then
564 .refuse .wattCbfFailClosed
565 else
566 match selectFastestSafe semId st pol base c bank with
567 | none => .refuse .noSafeRealization
568 | some best =>
569 let cmd := cbfFilter st pol (realizationProposal base best)
570 if decide (c.deadlineUs = 0 ∨ best.estLatencyUs ≤ c.deadlineUs) then
571 .dispatch best cmd .none
572 else if c.hardDeadline then
573 .refuse .hardDeadlineMiss
574 else
575 .dispatch best cmd (.deadlineSlip (best.estLatencyUs - c.deadlineUs))
576
577-- =========================================================================
578-- Section 3: Selection Helper Lemmas
579-- =========================================================================
580
581private theorem betterCandidate_lat_le (a b : Realization) (h : betterCandidate a b = true) :
582 a.estLatencyUs ≤ b.estLatencyUs := by
583 unfold betterCandidate at h
584 by_cases h1 : a.estLatencyUs < b.estLatencyUs
585 · omega
586 · simp only [h1, ↓reduceIte] at h
587 by_cases h2 : a.estLatencyUs > b.estLatencyUs
588 · simp [h2] at h
589 · omega
590
591private theorem selectFastestLoop_props
592 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad) (c : Contract)
593 (bank : List Realization) (acc : Option Realization)
594 (hacc : ∀ a, acc = some a → isSafeCandidate semId st pol base c a = true) :
595 ∀ res, selectFastestLoop semId st pol base c acc bank = some res →
596 isSafeCandidate semId st pol base c res = true ∧
597 (∀ a, acc = some a → res.estLatencyUs ≤ a.estLatencyUs) ∧
598 (∀ x, x ∈ bank → isSafeCandidate semId st pol base c x = true →
599 res.estLatencyUs ≤ x.estLatencyUs) := by
600 induction bank generalizing acc with
601 | nil =>
602 intro res hres
603 unfold selectFastestLoop at hres
604 refine ⟨hacc res hres, ?_, ?_⟩
605 · intro a ha; rw [ha] at hres; cases hres; exact Int.le_refl _
606 · intro x hx; cases hx
607 | cons r rest ih =>
608 intro res hres
609 unfold selectFastestLoop at hres
610 by_cases hsafe : isSafeCandidate semId st pol base c r = true
611 · simp only [hsafe, ↓reduceIte] at hres
612 cases acc with
613 | none =>
614 dsimp only at hres
615 have hih := ih (some r) (by intro a ha; cases ha; exact hsafe) res hres
616 refine ⟨hih.1, ?_, ?_⟩
617 · intro a ha; cases ha
618 · intro x hx hxs
619 cases hx with
620 | head => exact hih.2.1 r rfl
621 | tail _ hmem => exact hih.2.2 x hmem hxs
622 | some cur =>
623 dsimp only at hres
624 by_cases hbet : betterCandidate r cur = true
625 · simp only [hbet, ↓reduceIte] at hres
626 have hlat_rc : r.estLatencyUs ≤ cur.estLatencyUs := betterCandidate_lat_le r cur hbet
627 have hih := ih (some r) (by intro a ha; cases ha; exact hsafe) res hres
628 have hres_r : res.estLatencyUs ≤ r.estLatencyUs := hih.2.1 r rfl
629 refine ⟨hih.1, ?_, ?_⟩
630 · intro a ha; cases ha; omega
631 · intro x hx hxs
632 cases hx with
633 | head => exact hres_r
634 | tail _ hmem => exact hih.2.2 x hmem hxs
635 · simp only [hbet, Bool.false_eq_true, ↓reduceIte] at hres
636 have hlat_cr : cur.estLatencyUs ≤ r.estLatencyUs := by
637 unfold betterCandidate at hbet
638 by_cases hlt : r.estLatencyUs < cur.estLatencyUs
639 · simp [hlt] at hbet
640 · omega
641 have hih := ih (some cur) hacc res hres
642 have hres_c : res.estLatencyUs ≤ cur.estLatencyUs := hih.2.1 cur rfl
643 refine ⟨hih.1, ?_, ?_⟩
644 · intro a ha; cases ha; exact hres_c
645 · intro x hx hxs
646 cases hx with
647 | head => omega
648 | tail _ hmem => exact hih.2.2 x hmem hxs
649 · simp only [hsafe, Bool.false_eq_true, ↓reduceIte] at hres
650 have hih := ih acc hacc res hres
651 refine ⟨hih.1, hih.2.1, ?_⟩
652 · intro x hx hxs
653 cases hx with
654 | head => rw [hxs] at hsafe; exact absurd rfl hsafe
655 | tail _ hmem => exact hih.2.2 x hmem hxs
656
657private theorem selectFastestLoop_mem_some
658 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad) (c : Contract)
659 (bank : List Realization) (acc : Option Realization)
660 (hacc_some : acc.isSome = true) :
661 (selectFastestLoop semId st pol base c acc bank).isSome = true := by
662 induction bank generalizing acc with
663 | nil => exact hacc_some
664 | cons r rest ih =>
665 unfold selectFastestLoop
666 by_cases hsafe : isSafeCandidate semId st pol base c r = true
667 · simp only [hsafe, ↓reduceIte]
668 apply ih
669 cases acc <;> simp
670 split <;> rfl
671 · simp only [hsafe, Bool.false_eq_true, ↓reduceIte]
672 exact ih acc hacc_some
673
674private theorem selectFastestSafe_sound
675 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
676 (c : Contract) (bank : List Realization) (res : Realization)
677 (h : selectFastestSafe semId st pol base c bank = some res) :
678 isSafeCandidate semId st pol base c res = true ∧
679 (∀ x, x ∈ bank → isSafeCandidate semId st pol base c x = true →
680 res.estLatencyUs ≤ x.estLatencyUs) := by
681 have hp := selectFastestLoop_props semId st pol base c bank none (by intro a ha; cases ha) res h
682 exact ⟨hp.1, hp.2.2⟩
683
684private theorem solveDispatch_extract
685 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
686 (c : Contract) (bank : List Realization)
687 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
688 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm) :
689 contractValid c = true ∧
690 0 ≤ base.baseCpuMw ∧ 0 ≤ base.baseUncMw ∧
691 mustFailClosed st pol = false ∧
692 selectFastestSafe semId st pol base c bank = some r ∧
693 cmd = cbfFilter st pol (realizationProposal base r) ∧
694 ((c.deadlineUs = 0 ∨ r.estLatencyUs ≤ c.deadlineUs) ∧ alarm = .none ∨
695 (c.deadlineUs ≠ 0 ∧ r.estLatencyUs > c.deadlineUs ∧ c.hardDeadline = false ∧
696 alarm = .deadlineSlip (r.estLatencyUs - c.deadlineUs))) := by
697 unfold solveDispatch at h
698 by_cases hcv : contractValid c = true
699 · simp only [hcv, Bool.not_true, Bool.false_eq_true, ↓reduceIte] at h
700 by_cases hbb : (base.baseCpuMw < 0 ∨ base.baseUncMw < 0)
701 · simp [hbb] at h
702 · simp only [hbb, decide_false, Bool.false_eq_true, ↓reduceIte] at h
703 by_cases hfc : mustFailClosed st pol = true
704 · simp [hfc] at h
705 · have hfcF : mustFailClosed st pol = false := by
706 cases hm : mustFailClosed st pol
707 · rfl
708 · exact absurd hm hfc
709 simp only [hfcF, Bool.false_eq_true, ↓reduceIte] at h
710 cases hsel : selectFastestSafe semId st pol base c bank with
711 | none => simp [hsel] at h
712 | some best =>
713 simp only [hsel] at h
714 by_cases hdl : (c.deadlineUs = 0 ∨ best.estLatencyUs ≤ c.deadlineUs)
715 · simp only [hdl, decide_true, ↓reduceIte] at h
716 cases h
717 refine ⟨hcv, by omega, by omega, hfcF, rfl, rfl, Or.inl ⟨hdl, rfl⟩⟩
718 · simp only [hdl, decide_false, Bool.false_eq_true, ↓reduceIte] at h
719 by_cases hhd : c.hardDeadline = true
720 · simp [hhd] at h
721 · have hhdF : c.hardDeadline = false := by
722 cases hh : c.hardDeadline
723 · rfl
724 · exact absurd hh hhd
725 simp only [hhdF, Bool.false_eq_true, ↓reduceIte] at h
726 cases h
727 refine ⟨hcv, by omega, by omega, hfcF, rfl, rfl,
728 Or.inr ⟨by omega, by omega, hhdF, rfl⟩⟩
729 · have hcvF : contractValid c = false := by
730 cases hc : contractValid c
731 · rfl
732 · exact absurd hc hcv
733 simp [hcvF] at h
734
735-- =========================================================================
736-- Section 4: Main Stage 3 Safety Theorems
737-- =========================================================================
738
739/-- Theorem 1 (Inviolable Hardware WattCBF Safety):
740 Every realization dispatched by `solveDispatch` is proof-verified (`proofMask = 7`),
741 admitted un-clamped by `cbfFilter` (`failClosed = false`), obeys the Trap-14 80 W
742 GPU ceiling, total package Watt conservation (`<= min(P_budget, 140 W)`), discrete
743 thermal CBF barrier `h >= 0`, next-step thermal safety `T_die' <= T_ceil`, and
744 the ΩIR v2 `@power` ceiling. -/
745theorem admitted_respects_wattcbf
746 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
747 (c : Contract) (bank : List Realization)
748 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
749 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm) :
750 let prop := realizationProposal base r
751 let dT := nextDieDelta st prop.pGpu prop.pCpu prop.pUnc
752 r.proofMask = PROOF_REQUIRED ∧
753 cmd.failClosed = false ∧
754 cmd.pGpu = prop.pGpu ∧ cmd.pCpu = prop.pCpu ∧ cmd.pUnc = prop.pUnc ∧
755 prop.pGpu ≤ min P_GPU_HARD_MAX_MW pol.pGpuBudget ∧
756 prop.pGpu + prop.pCpu + prop.pUnc ≤ min pol.pBudget P_TOTAL_HARD_MAX_MW ∧
757 barrierH pol.tCeil st.tDie pol.tau dT ≥ 0 ∧
758 st.tDie + dT ≤ pol.tCeil ∧
759 (c.powerMw = 0 ∨ r.estPowerMw ≤ c.powerMw) := by
760 dsimp only
761 have hex := solveDispatch_extract semId st pol base c bank r cmd alarm h
762 have hsel := selectFastestSafe_sound semId st pol base c bank r hex.2.2.2.2.1
763 have hcand := hsel.1
764 unfold isSafeCandidate at hcand
765 simp only [Bool.and_eq_true, decide_eq_true_eq] at hcand
766 have hproof := hcand.1.1.2
767 unfold isProofVerified at hproof
768 simp only [decide_eq_true_eq] at hproof
769 have hwc := hcand.1.2
770 unfold isWattCbfSafe at hwc
771 simp only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_true_eq] at hwc
772 have hcmd_eq : cmd = cbfFilter st pol (realizationProposal base r) := hex.2.2.2.2.2.1
773 have hsafe := cbf_admitted_safe st pol (realizationProposal base r) hwc.1.1
774 dsimp only at hsafe
775 rw [hcmd_eq]
776 rw [hwc.1.2.1, hwc.1.2.2.1, hwc.1.2.2.2] at hsafe
777 exact ⟨hproof.1, hwc.1.1, hwc.1.2.1, hwc.1.2.2.1, hwc.1.2.2.2,
778 hsafe.1, hsafe.2.1, hsafe.2.2.1, hsafe.2.2.2, hwc.2⟩
779
780/-- Theorem 2 (Inviolable Declared `@err` Accuracy Bound):
781 Every dispatched realization satisfies `0 <= r.errBound <= contract.errBound`, and
782 under `@exact` (`precisionTier = 1`), `r.errBound = 0` strictly. -/
783theorem admitted_respects_err_bound
784 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
785 (c : Contract) (bank : List Realization)
786 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
787 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm) :
788 0 ≤ r.errBound ∧ r.errBound ≤ c.errBound ∧
789 (c.precisionTier = TIER_EXACT → r.errBound = 0) := by
790 have hex := solveDispatch_extract semId st pol base c bank r cmd alarm h
791 have hcv := hex.1
792 unfold contractValid at hcv
793 simp only [decide_eq_true_eq] at hcv
794 have hsel := selectFastestSafe_sound semId st pol base c bank r hex.2.2.2.2.1
795 have hcand := hsel.1
796 unfold isSafeCandidate at hcand
797 simp only [Bool.and_eq_true, decide_eq_true_eq] at hcand
798 have hproof := hcand.1.1.2
799 unfold isProofVerified at hproof
800 simp only [decide_eq_true_eq] at hproof
801 have herr := hcand.2
802 unfold isErrSafe at herr
803 simp only [decide_eq_true_eq] at herr
804 refine ⟨by omega, herr, ?_⟩
805 intro hexact
806 have hzero : c.errBound = 0 := hcv.2.2.2.2.2.2.2.1 (by unfold TIER_EXACT TIER_TOL at *; omega)
807 omega
808
809/-- Theorem 3 (Hard Deadline Fail-Closed):
810 When `contract.hardDeadline = true` and NO safe realization in the bank meets
811 `contract.deadlineUs`, `solveDispatch` ALWAYS returns `.refuse reason` (never dispatches!).
812 Specifically, when contract/baseline/thermal state are valid and safe realizations exist
813 (all missing the deadline), it returns `.refuse .hardDeadlineMiss`. -/
814theorem hard_deadline_fail_closed
815 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
816 (c : Contract) (bank : List Realization)
817 (hhard : c.hardDeadline = true)
818 (hnone : ∀ r, r ∈ bank → isSafeCandidate semId st pol base c r = true →
819 r.estLatencyUs > c.deadlineUs) :
820 ∃ reason, solveDispatch semId st pol base c bank = .refuse reason := by
821 unfold solveDispatch
822 by_cases hcv : contractValid c = true
823 · simp only [hcv, Bool.not_true, Bool.false_eq_true, ↓reduceIte]
824 by_cases hbb : (base.baseCpuMw < 0 ∨ base.baseUncMw < 0)
825 · exact ⟨.badBaseline, by simp [hbb]⟩
826 · simp only [hbb, decide_false, Bool.false_eq_true, ↓reduceIte]
827 by_cases hfc : mustFailClosed st pol = true
828 · exact ⟨.wattCbfFailClosed, by simp [hfc]⟩
829 · have hfcF : mustFailClosed st pol = false := by
830 cases hm : mustFailClosed st pol
831 · rfl
832 · exact absurd hm hfc
833 simp only [hfcF, Bool.false_eq_true, ↓reduceIte]
834 cases hsel : selectFastestSafe semId st pol base c bank with
835 | none => exact ⟨.noSafeRealization, rfl⟩
836 | some best =>
837 have hmem : best ∈ bank := by
838 -- Prove membership directly from selectFastestLoop
839 have hmem_aux : ∀ (xs : List Realization) (acc : Option Realization),
840 (∀ a, acc = some a → a ∈ bank) →
841 (∀ x, x ∈ xs → x ∈ bank) →
842 ∀ res, selectFastestLoop semId st pol base c acc xs = some res → res ∈ bank := by
843 intro xs
844 induction xs with
845 | nil =>
846 intro acc hacc _ res hres
847 exact hacc res hres
848 | cons y ys ih =>
849 intro acc hacc hsub res hres
850 unfold selectFastestLoop at hres
851 by_cases hs : isSafeCandidate semId st pol base c y = true
852 · simp only [hs, ↓reduceIte] at hres
853 apply ih _ _ (fun z hz => hsub z (List.mem_cons_of_mem y hz)) res hres
854 intro a ha
855 cases acc with
856 | none => cases ha; exact hsub y (List.mem_cons_self)
857 | some cur =>
858 dsimp only at ha
859 by_cases hb : betterCandidate y cur = true
860 · simp only [hb, ↓reduceIte] at ha; cases ha; exact hsub y List.mem_cons_self
861 · simp only [hb, Bool.false_eq_true, ↓reduceIte] at ha; exact hacc a ha
862 · simp only [hs, Bool.false_eq_true, ↓reduceIte] at hres
863 exact ih acc hacc (fun z hz => hsub z (List.mem_cons_of_mem y hz)) res hres
864 exact hmem_aux bank none (by intro a ha; cases ha) (fun x hx => hx) best hsel
865 have hsafe := (selectFastestSafe_sound semId st pol base c bank best hsel).1
866 have hlate := hnone best hmem hsafe
867 unfold contractValid at hcv
868 simp only [decide_eq_true_eq] at hcv
869 have hdl_pos : c.deadlineUs > 0 := hcv.2.2.2.2.2.2.2.2 hhard
870 have hcond : ¬ (c.deadlineUs = 0 ∨ best.estLatencyUs ≤ c.deadlineUs) := by omega
871 exact ⟨.hardDeadlineMiss, by simp [hcond, hhard]⟩
872 · have hcvF : contractValid c = false := by
873 cases hc : contractValid c
874 · rfl
875 · exact absurd hc hcv
876 exact ⟨.badContract, by simp [hcvF]⟩
877
878private theorem selectFastestLoop_exists_some
879 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad) (c : Contract)
880 (bank : List Realization) (acc : Option Realization) (r0 : Realization)
881 (hmem : r0 ∈ bank) (hsafe0 : isSafeCandidate semId st pol base c r0 = true) :
882 (selectFastestLoop semId st pol base c acc bank).isSome = true := by
883 induction bank generalizing acc with
884 | nil => cases hmem
885 | cons r rest ih =>
886 unfold selectFastestLoop
887 cases hmem with
888 | head =>
889 simp only [hsafe0, ↓reduceIte]
890 apply selectFastestLoop_mem_some
891 cases acc <;> simp
892 split <;> rfl
893 | tail _ htail =>
894 by_cases hs : isSafeCandidate semId st pol base c r = true
895 · simp only [hs, ↓reduceIte]; exact ih _ htail
896 · simp only [hs, Bool.false_eq_true, ↓reduceIte]; exact ih acc htail
897
898/-- Theorem 3b (Exact Hard-Deadline Refusal Code):
899 Under valid contract, baseline, and WattCBF state, if at least one safe realization exists
900 and EVERY safe realization misses `contract.deadlineUs` with `contract.hardDeadline = true`,
901 `solveDispatch` returns `.refuse .hardDeadlineMiss` specifically. -/
902theorem hard_deadline_miss_exact
903 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
904 (c : Contract) (bank : List Realization)
905 (hcv : contractValid c = true)
906 (hbc : 0 ≤ base.baseCpuMw) (hbu : 0 ≤ base.baseUncMw)
907 (hfc : mustFailClosed st pol = false)
908 (hhard : c.hardDeadline = true)
909 (hex : ∃ r0, r0 ∈ bank ∧ isSafeCandidate semId st pol base c r0 = true)
910 (hnone : ∀ r, r ∈ bank → isSafeCandidate semId st pol base c r = true →
911 r.estLatencyUs > c.deadlineUs) :
912 solveDispatch semId st pol base c bank = .refuse .hardDeadlineMiss := by
913 have ⟨r0, hmem0, hsafe0⟩ := hex
914 have hsome : (selectFastestSafe semId st pol base c bank).isSome = true :=
915 selectFastestLoop_exists_some semId st pol base c bank none r0 hmem0 hsafe0
916 unfold solveDispatch
917 have hbb : ¬ (base.baseCpuMw < 0 ∨ base.baseUncMw < 0) := by omega
918 simp only [hcv, Bool.not_true, Bool.false_eq_true, ↓reduceIte, hbb, decide_false, hfc]
919 cases hsel : selectFastestSafe semId st pol base c bank with
920 | none => rw [hsel] at hsome; simp at hsome
921 | some best =>
922 have hmem : best ∈ bank := by
923 have hmem_aux : ∀ (xs : List Realization) (acc : Option Realization),
924 (∀ a, acc = some a → a ∈ bank) →
925 (∀ x, x ∈ xs → x ∈ bank) →
926 ∀ res, selectFastestLoop semId st pol base c acc xs = some res → res ∈ bank := by
927 intro xs
928 induction xs with
929 | nil => intro acc hacc _ res hres; exact hacc res hres
930 | cons y ys ih =>
931 intro acc hacc hsub res hres
932 unfold selectFastestLoop at hres
933 by_cases hs : isSafeCandidate semId st pol base c y = true
934 · simp only [hs, ↓reduceIte] at hres
935 apply ih _ _ (fun z hz => hsub z (List.mem_cons_of_mem y hz)) res hres
936 intro a ha
937 cases acc with
938 | none => cases ha; exact hsub y List.mem_cons_self
939 | some cur =>
940 dsimp only at ha
941 by_cases hb : betterCandidate y cur = true
942 · simp only [hb, ↓reduceIte] at ha; cases ha; exact hsub y List.mem_cons_self
943 · simp only [hb, Bool.false_eq_true, ↓reduceIte] at ha; exact hacc a ha
944 · simp only [hs, Bool.false_eq_true, ↓reduceIte] at hres
945 exact ih acc hacc (fun z hz => hsub z (List.mem_cons_of_mem y hz)) res hres
946 exact hmem_aux bank none (by intro a ha; cases ha) (fun x hx => hx) best hsel
947 have hsafe := (selectFastestSafe_sound semId st pol base c bank best hsel).1
948 have hlate := hnone best hmem hsafe
949 unfold contractValid at hcv
950 simp only [decide_eq_true_eq] at hcv
951 have hdl_pos : c.deadlineUs > 0 := hcv.2.2.2.2.2.2.2.2 hhard
952 have hcond : ¬ (c.deadlineUs = 0 ∨ best.estLatencyUs ≤ c.deadlineUs) := by omega
953 simp [hcond, hhard]
954
955/-- Theorem 4 (Soft Deadline Slip Is Never Silent):
956 Whenever a dispatched realization exceeds `contract.deadlineUs > 0`, `contract.hardDeadline`
957 MUST be `false` and the solver MUST emit `DispatchAlarm.deadlineSlip (r.estLatencyUs - c.deadlineUs)`. -/
958theorem soft_deadline_slip_never_silent
959 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
960 (c : Contract) (bank : List Realization)
961 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
962 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm)
963 (hdl : c.deadlineUs > 0) (hslip : r.estLatencyUs > c.deadlineUs) :
964 c.hardDeadline = false ∧ alarm = .deadlineSlip (r.estLatencyUs - c.deadlineUs) := by
965 have hex := solveDispatch_extract semId st pol base c bank r cmd alarm h
966 cases hex.2.2.2.2.2.2 with
967 | inl hok => omega
968 | inr hsl => exact ⟨hsl.2.2.1, hsl.2.2.2⟩
969
970/-- Theorem 5 (Minimum-Latency Optimality Inside Priorities 1 & 2):
971 Every dispatched realization achieves the minimum latency among all proof-verified,
972 WattCBF-safe, `@err`-safe candidates in the bank. -/
973theorem admitted_is_min_latency_among_safe
974 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
975 (c : Contract) (bank : List Realization)
976 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
977 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm)
978 (x : Realization) (hx : x ∈ bank) (hxs : isSafeCandidate semId st pol base c x = true) :
979 r.estLatencyUs ≤ x.estLatencyUs := by
980 have hex := solveDispatch_extract semId st pol base c bank r cmd alarm h
981 exact (selectFastestSafe_sound semId st pol base c bank r hex.2.2.2.2.1).2 x hx hxs
982
983/-- Theorem 6 (Proof Gate Non-Bypassability):
984 No unproved realization (`proofMask != 7`) can ever be dispatched. -/
985theorem unproved_never_admitted
986 (semId : Int) (st : ThermalState) (pol : WattPolicy) (base : BaselineLoad)
987 (c : Contract) (bank : List Realization)
988 (r : Realization) (cmd : SafeCommand) (alarm : DispatchAlarm)
989 (h : solveDispatch semId st pol base c bank = .dispatch r cmd alarm) :
990 r.proofMask = PROOF_REQUIRED :=
991 (admitted_respects_wattcbf semId st pol base c bank r cmd alarm h).1
992
993-- =========================================================================
994-- Section 5: Sequential Call-Chain Error-Composition Soundness
995-- =========================================================================
996
997/-- Sum of per-stage proven error bounds across a sequential call chain. -/
998def chainErrSum : List Int → Int
999 | [] => 0
1000 | e :: rest => e + chainErrSum rest
1001
1002/-- Every stage error bound in the chain is non-negative. -/
1003def chainAllNonNeg : List Int → Bool
1004 | [] => true
1005 | e :: rest => decide (0 ≤ e) && chainAllNonNeg rest
1006
1007/-- Chain admission predicate (exact mirror of `ps_chain_add_stage` / `ps_chain_commit`). -/
1008def chainAdmit (stages : List Int) (outerErr : Int) : Bool :=
1009 decide (0 ≤ outerErr) && chainAllNonNeg stages && decide (chainErrSum stages ≤ outerErr)
1010
1011/-- Inductive additive/interval error propagation along a sequential call chain:
1012 if input deviation is bounded by `errIn`, then after stage with proven error `e`,
1013 intermediate deviation is bounded by `errIn + e`. -/
1014inductive ChainPropagates : List Int → Int → Int → Int → Int → Prop where
1015 | nil (ex ap : Int) :
1016 ChainPropagates [] ex ap ex ap
1017 | cons (e : Int) (rest : List Int) (ex0 ap0 ex1 ap1 exN apN : Int)
1018 (hstep : ∀ d : Int, -d ≤ ap0 - ex0 → ap0 - ex0 ≤ d →
1019 -(d + e) ≤ ap1 - ex1 ∧ ap1 - ex1 ≤ d + e)
1020 (htail : ChainPropagates rest ex1 ap1 exN apN) :
1021 ChainPropagates (e :: rest) ex0 ap0 exN apN
1022
1023private theorem chainPropagates_bound
1024 (stages : List Int) (ex0 ap0 exN apN d0 : Int)
1025 (hprop : ChainPropagates stages ex0 ap0 exN apN)
1026 (hinit : -d0 ≤ ap0 - ex0 ∧ ap0 - ex0 ≤ d0) :
1027 -(d0 + chainErrSum stages) ≤ apN - exN ∧ apN - exN ≤ d0 + chainErrSum stages := by
1028 induction hprop generalizing d0 with
1029 | nil ex ap =>
1030 unfold chainErrSum
1031 omega
1032 | cons e rest ex0 ap0 ex1 ap1 exN apN hstep _ ih =>
1033 have h1 := hstep d0 hinit.1 hinit.2
1034 have h2 := ih (d0 + e) h1
1035 unfold chainErrSum
1036 omega
1037
1038/-- Theorem 7 (Chain Error-Composition Soundness):
1039 If every stage in a sequential call chain has non-negative proven error bound and
1040 the sum of stage errors is `<= outerErr` (`chainAdmit stages outerErr = true`), then
1041 starting from exact inputs (`ap0 = ex0`), the end-to-end composed output error is
1042 guaranteed to remain within `[-outerErr, +outerErr]`. -/
1043theorem chain_error_composition_sound
1044 (stages : List Int) (outerErr ex0 ap0 exN apN : Int)
1045 (hadmit : chainAdmit stages outerErr = true)
1046 (hexact_in : ap0 = ex0)
1047 (hprop : ChainPropagates stages ex0 ap0 exN apN) :
1048 chainErrSum stages ≤ outerErr ∧
1049 -outerErr ≤ apN - exN ∧ apN - exN ≤ outerErr := by
1050 unfold chainAdmit at hadmit
1051 simp only [Bool.and_eq_true, decide_eq_true_eq] at hadmit
1052 have hsum : chainErrSum stages ≤ outerErr := hadmit.2
1053 have hb := chainPropagates_bound stages ex0 ap0 exN apN 0 hprop (by omega)
1054 exact ⟨hsum, by omega, by omega⟩
1055
1056/-- Theorem 8 (Chain Overflow Refusal):
1057 Whenever the composed chain error sum strictly exceeds `outerErr`, `chainAdmit` returns `false`. -/
1058theorem chain_overflow_refused (stages : List Int) (outerErr : Int)
1059 (hovf : chainErrSum stages > outerErr) :
1060 chainAdmit stages outerErr = false := by
1061 unfold chainAdmit
1062 have hle : ¬ (chainErrSum stages ≤ outerErr) := by omega
1063 simp [hle]
1064
1065-- =========================================================================
1066-- Section 6: Non-Vacuity Negative-Control Witnesses (Concrete `decide` Proofs)
1067-- =========================================================================
1068
1069/-- Negative Control 1 (Trap-14 90 W Spike Refused):
1070 A 90 W (`90,000 mW`) GPU realization (even with `proofMask = 7`, `err = 0`, `lat = 500 us`)
1071 is REFUSED by `solveDispatch` on its own, and bypassed in favor of a safe 50 W realization
1072 when both are in the bank. -/
1073theorem neg_control_trap14_90w_refused :
1074 let st : ThermalState := { tDie := 76000, tSink := 66000, tAmb := 25000 }
1075 let pol : WattPolicy := { pBudget := 110000, pGpuBudget := 80000, tCeil := 78000, tau := 2 }
1076 let base : BaselineLoad := { baseCpuMw := 15000, baseUncMw := 5000, fanReqPct := 40 }
1077 let ctr : Contract := { purityFlag := 1, precisionTier := 1, hardDeadline := false,
1078 errBound := 0, deadlineUs := 16600, powerMw := 0 }
1079 let r90w : Realization := { semId := 9001, targetTid := 6, isGpuTarget := true,
1080 precisionId := 8, estPowerMw := 90000, estLatencyUs := 500,
1081 estJoulesUj := 45000, errBound := 0, proofMask := 7 }
1082 let r50w : Realization := { semId := 9001, targetTid := 6, isGpuTarget := true,
1083 precisionId := 4, estPowerMw := 50000, estLatencyUs := 3000,
1084 estJoulesUj := 150000, errBound := 0, proofMask := 7 }
1085 solveDispatch 9001 st pol base ctr [r90w] = .refuse .noSafeRealization ∧
1086 (match solveDispatch 9001 st pol base ctr [r90w, r50w] with
1087 | .dispatch chosen _ .none => chosen = r50w
1088 | _ => false) = true := by
1089 decide
1090
1091/-- Negative Control 2 (Loosened-Precision `@err` Violation Refused):
1092 A fast, cool, low-power NVFP4 realization (`err = 100`) is REFUSED under `@tol 10`
1093 (`@err` is NEVER silently broken to meet latency or power). -/
1094theorem neg_control_err_violation_refused :
1095 let st : ThermalState := { tDie := 60000, tSink := 54000, tAmb := 25000 }
1096 let pol : WattPolicy := { pBudget := 110000, pGpuBudget := 80000, tCeil := 85000, tau := 5 }
1097 let base : BaselineLoad := { baseCpuMw := 15000, baseUncMw := 5000, fanReqPct := 40 }
1098 let ctr : Contract := { purityFlag := 1, precisionTier := 2, hardDeadline := false,
1099 errBound := 10, deadlineUs := 16600, powerMw := 0 }
1100 let rNv4 : Realization := { semId := 9002, targetTid := 6, isGpuTarget := true,
1101 precisionId := 5, estPowerMw := 26000, estLatencyUs := 1200,
1102 estJoulesUj := 31200, errBound := 100, proofMask := 7 }
1103 solveDispatch 9002 st pol base ctr [rNv4] = .refuse .noSafeRealization := by
1104 decide
1105
1106/-- Negative Control 3 (Tight `@hard_deadline` Fail-Closed Witness at 84 C):
1107 At `84.0 C` (`T_ceil = 85.0 C`, `tau = 5 s`), GPU kernels & `CPU_X925` exceed WattCBF
1108 headroom; only `CPU_A725` (`5 W`, `22,000 us > 16,600 us`) is safe. With `hardDeadline = true`,
1109 `solveDispatch` MUST fail closed with `.refuse .hardDeadlineMiss`. -/
1110theorem neg_control_hard_deadline_fail_closed :
1111 let st : ThermalState := { tDie := 84000, tSink := 84000, tAmb := 25000 }
1112 let pol : WattPolicy := { pBudget := 110000, pGpuBudget := 80000, tCeil := 85000, tau := 5 }
1113 let base : BaselineLoad := { baseCpuMw := 15000, baseUncMw := 5000, fanReqPct := 40 }
1114 let ctrH : Contract := { purityFlag := 1, precisionTier := 2, hardDeadline := true,
1115 errBound := 100, deadlineUs := 16600, powerMw := 0 }
1116 let rGpu : Realization := { semId := 9003, targetTid := 6, isGpuTarget := true,
1117 precisionId := 5, estPowerMw := 26000, estLatencyUs := 6500,
1118 estJoulesUj := 169000, errBound := 100, proofMask := 7 }
1119 let rX925 : Realization := { semId := 9003, targetTid := 0, isGpuTarget := false,
1120 precisionId := 6, estPowerMw := 18000, estLatencyUs := 13000,
1121 estJoulesUj := 234000, errBound := 0, proofMask := 7 }
1122 let rA725 : Realization := { semId := 9003, targetTid := 1, isGpuTarget := false,
1123 precisionId := 7, estPowerMw := 5000, estLatencyUs := 22000,
1124 estJoulesUj := 110000, errBound := 0, proofMask := 7 }
1125 solveDispatch 9003 st pol base ctrH [rGpu, rX925, rA725] = .refuse .hardDeadlineMiss := by
1126 decide
1127
1128/-- Negative Control 4 (Soft Deadline Explicit `ALARM_DEADLINE_SLIP` Witness at 84 C):
1129 Under the exact same `84.0 C` thermal ceiling with `hardDeadline = false`, `solveDispatch`
1130 selects safe `CPU_A725` AND emits explicit `.deadlineSlip 5400` (never silent). -/
1131theorem neg_control_soft_deadline_alarm :
1132 let st : ThermalState := { tDie := 84000, tSink := 84000, tAmb := 25000 }
1133 let pol : WattPolicy := { pBudget := 110000, pGpuBudget := 80000, tCeil := 85000, tau := 5 }
1134 let base : BaselineLoad := { baseCpuMw := 15000, baseUncMw := 5000, fanReqPct := 40 }
1135 let ctrS : Contract := { purityFlag := 1, precisionTier := 2, hardDeadline := false,
1136 errBound := 100, deadlineUs := 16600, powerMw := 0 }
1137 let rGpu : Realization := { semId := 9003, targetTid := 6, isGpuTarget := true,
1138 precisionId := 5, estPowerMw := 26000, estLatencyUs := 6500,
1139 estJoulesUj := 169000, errBound := 100, proofMask := 7 }
1140 let rX925 : Realization := { semId := 9003, targetTid := 0, isGpuTarget := false,
1141 precisionId := 6, estPowerMw := 18000, estLatencyUs := 13000,
1142 estJoulesUj := 234000, errBound := 0, proofMask := 7 }
1143 let rA725 : Realization := { semId := 9003, targetTid := 1, isGpuTarget := false,
1144 precisionId := 7, estPowerMw := 5000, estLatencyUs := 22000,
1145 estJoulesUj := 110000, errBound := 0, proofMask := 7 }
1146 (match solveDispatch 9003 st pol base ctrS [rGpu, rX925, rA725] with
1147 | .dispatch chosen _ (.deadlineSlip slip) => chosen = rA725 ∧ slip = 5400
1148 | _ => false) = true := by
1149 decide
1150
1151/-- Negative Control 5 (Unproved Realization `proofMask != 7` Refused):
1152 A realization missing the EGP3 proof bit (`proofMask = 5`) is refused even with ideal metrics. -/
1153theorem neg_control_unproved_mask_refused :
1154 let st : ThermalState := { tDie := 60000, tSink := 54000, tAmb := 25000 }
1155 let pol : WattPolicy := { pBudget := 110000, pGpuBudget := 80000, tCeil := 85000, tau := 5 }
1156 let base : BaselineLoad := { baseCpuMw := 15000, baseUncMw := 5000, fanReqPct := 40 }
1157 let ctr : Contract := { purityFlag := 1, precisionTier := 1, hardDeadline := false,
1158 errBound := 0, deadlineUs := 16600, powerMw := 0 }
1159 let rBad : Realization := { semId := 9004, targetTid := 0, isGpuTarget := false,
1160 precisionId := 6, estPowerMw := 10000, estLatencyUs := 1000,
1161 estJoulesUj := 10000, errBound := 0, proofMask := 5 }
1162 solveDispatch 9004 st pol base ctr [rBad] = .refuse .noSafeRealization := by
1163 decide
1164
1165/-- Negative Control 6 (Composed Chain Error Overflow Refused vs Exact Budget Admitted):
1166 Stages `[40, 35, 30]` (sum `105 > 100`) are refused while `[40, 35, 25]` (sum `100 <= 100`) pass. -/
1167theorem neg_control_chain_err_overflow_refused :
1168 chainAdmit [40, 35, 30] 100 = false ∧
1169 chainAdmit [40, 35, 25] 100 = true := by
1170 decide
1171
1172#print axioms cbf_admitted_safe
1173#print axioms admitted_respects_wattcbf
1174#print axioms admitted_respects_err_bound
1175#print axioms hard_deadline_fail_closed
1176#print axioms hard_deadline_miss_exact
1177#print axioms soft_deadline_slip_never_silent
1178#print axioms admitted_is_min_latency_among_safe
1179#print axioms unproved_never_admitted
1180#print axioms chain_error_composition_sound
1181#print axioms chain_overflow_refused
1182#print axioms neg_control_trap14_90w_refused
1183#print axioms neg_control_err_violation_refused
1184#print axioms neg_control_hard_deadline_fail_closed
1185#print axioms neg_control_soft_deadline_alarm
1186#print axioms neg_control_unproved_mask_refused
1187#print axioms neg_control_chain_err_overflow_refused
1188
1189end PhysicsDispatch