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 / bft_healer.om

516 lines · 9342 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/helix/bft_healer.om: Pure-Omega N = 3f + 1 Byzantine Fault-Tolerant Braid Controller,
3note Automatic Quarantine & Cold-Regrowth Healer, Live Compiler Mitosis Evaluator, and
4note Arboris Predator <-> Ascetic Real-Thermal Hot-Swap Controller.
5note
6note Mirrors seed/helix/proofs/HelixBFT.lean (generalized N = 3f + 1: N=4,f=1 and N=7,f=2):
7note quorum N H0 ... H_{N-1} (1 <= N <= 7, f = (N-1)/3, threshold = N - f = 2f + 1)
8note Exit 0: unanimous (N/N agree)
9note Exit 100+mask: quorum (>= 2f+1) with 1..f faults; bit i in mask marks faulty strand i
10note Exit 20: NO_QUORUM (> f Byzantine faults, fail-closed halt!)
11note quorum H0 H1 H2 H3 (legacy 4-strand shorthand: exit 0, 10+fault, or 20)
12note mitosis-eval INC_COST MONK_RC TRICK_RC CAND_COST
13note Evaluates live Compiler Mitosis admission gate + real /sys/class/thermal sensor
14note Exit 0: HARVESTED_INTO_BRAID, Exit 1: REJECTED (incumbent kept safe)
15note mitosis
16note Runs live Compiler Mitosis + real /sys/class/thermal Arboris Predator <-> Ascetic gate
17note thermal
18note Reads max /sys/class/thermal/thermal_zone0..7/temp (fallback 55C) and reports active mode
19
20global bfDig uchar 1024
21global bfBuf uchar 256
22global bfThm uchar 64
23global bfThSt ulong 2
24
25text bfUse "usage: bft_healer quorum N H0..H_{N-1} | mitosis-eval INC MONK_RC TRICK_RC CAND | mitosis | thermal\n"
26text bfUn1 "BFT_QUORUM status=UNANIMOUS n="
27text bfUn2 " f="
28text bfUn3 " votes="
29text bfSl "/"
30text bfUn4 " faulty_mask=0 digest="
31text bfHl1 "BFT_QUORUM status=QUARANTINE_AND_REGROW n="
32text bfHl2 " faulty_mask="
33text bfDigS " quorum_digest="
34text bfNoQ1 "BFT_QUORUM status=NO_QUORUM_HALT n="
35text bfNoQ2 " votes_max="
36text bfNoQ3 " threshold="
37text bfNoQ4 " reason=faults_gt_f\n"
38text bfLegUn "BFT_QUORUM status=UNANIMOUS votes=4/4 faulty=none digest="
39text bfLegHl "BFT_QUORUM status=QUARANTINE_AND_REGROW votes=3/4 faulty_strand="
40text bfLegNo "BFT_QUORUM status=NO_QUORUM_HALT votes_max="
41text bfLegN2 "/4 reason=f_ge_2_byzantine_faults\n"
42text bfEvPre "MITOSIS_EVAL decision="
43text bfDecH "HARVESTED_INTO_BRAID"
44text bfDecM "KILLED_BY_MONK"
45text bfDecT "KILLED_BY_TRICKSTER"
46text bfDecS "RETIRED_SLOWER"
47text bfEvInc " inc="
48text bfEvCnd " cand="
49text bfEvChp " champion="
50text bfEvMrc " monk_rc="
51text bfEvTrc " trick_rc="
52text bfEvTmp " temp_c="
53text bfEvSns " sensor="
54text bfSnsLv "live"
55text bfSnsFb "fallback"
56text bfEvMod " mode="
57text bfModP "PREDATOR"
58text bfModA "ASCETIC"
59text bfThPfx "/sys/class/thermal/thermal_zone"
60text bfThSfx "/temp"
61text bfThOut "ARBORIS_THERMAL temp_c="
62text bfMitHdr "=== COMPILER MITOSIS + ARBORIS REAL-THERMAL PREDATOR <-> ASCETIC HOT-SWAP ===\n"
63text bfMitOk "MITOSIS_AND_HOTSWAP PASS children_tested=5 harvested=2 killed=2 retired=1 hotswaps=4 semantic_drift=0\n"
64text bfFail "MITOSIS_AND_HOTSWAP FAIL\n"
65text bfNl "\n"
66
67fn bfWrt s view uchar * to void
68r = write 1 s 0 len s
69end
70
71fn bfNum v ulong to void
72b = new uchar 24 0
73p = ulong 24
74x = v
75while true bound 24
76p = sub p 1
77put b p uchar add 48 rem x 10
78x = div x 10
79if eq x 0
80break
81end
82end
83r = write 1 b p sub 24 p
84end
85
86fn bfParseU n ulong to ulong
87if eq n 0
88ret 0xffffffffffffffff
89end
90v = ulong 0
91for i ulong n bound 32
92c = ulong get bfBuf i
93if or lt c 48 gt c 57
94ret 0xffffffffffffffff
95end
96v = add mul v 10 sub c 48
97end
98ret v
99end
100
101fn bfReadArgU idx ulong to ulong
102k = argread idx bfBuf 0 32
103if le k 0
104ret 0xffffffffffffffff
105end
106ret bfParseU ulong k
107end
108
109fn bfEqDig a ulong b ulong to bool
110oa = mul a 64
111ob = mul b 64
112for i ulong 64
113if ne get bfDig add oa i get bfDig add ob i
114ret false
115end
116end
117ret true
118end
119
120fn bfPrintDig idx ulong to void
121o = mul idx 64
122r = write 1 bfDig o 64
123end
124
125note Read real Linux thermal zones 0..7 (/sys/class/thermal/thermal_zoneK/temp).
126note Stores bfThSt[0] = max temp in Celsius, bfThSt[1] = 1 if live sensor found else 0 (fallback 55C).
127fn bfReadThermal to void
128best = ulong 0
129found = ulong 0
130for z ulong 8
131p = ulong 0
132for i ulong len bfThPfx
133put bfThm p get bfThPfx i
134p = add p 1
135end
136put bfThm p uchar add 48 z
137p = add p 1
138for i ulong len bfThSfx
139put bfThm p get bfThSfx i
140p = add p 1
141end
142fd = fopen bfThm 0 p 0 0
143if ge fd 0
144k = read int fd bfBuf 0 32
145rc = fclose int fd
146if gt k 0
147v = ulong 0
148ok = false
149for j ulong ulong k bound 32
150ch = ulong get bfBuf j
151if and ge ch 48 le ch 57
152v = add mul v 10 sub ch 48
153ok = true
154else
155break
156end
157end
158if ok
159tc = div v 1000
160if and ge tc 1 le tc 150
161if or eq found 0 gt tc best
162best = tc
163found = 1
164end
165end
166end
167end
168end
169end
170if eq found 0
171put bfThSt 0 55
172put bfThSt 1 0
173else
174put bfThSt 0 best
175put bfThSt 1 1
176end
177end
178
179note Exact Mitosis admission gate mirroring HelixBFT.lean mitosisStep
180fn bfMitosisStep incCost ulong monkOk bool trickOk bool childCost ulong to ulong
181if and monkOk and trickOk lt childCost incCost
182ret childCost
183end
184ret incCost
185end
186
187note Arboris Predator(0) <-> Ascetic(1) thermal/power policy
188fn bfArbMode onWall bool tempC ulong to ulong
189if or not onWall ge tempC 80
190ret 1
191end
192ret 0
193end
194
195fn bfDoQuorumLegacy to int
196for s ulong 4
197k = argread add 2 s bfBuf 0 120
198if lt k 64
199bfWrt bfUse
200ret 2
201end
202o = mul s 64
203for i ulong 64
204put bfDig add o i get bfBuf i
205end
206end
207bestS = ulong 0
208bestC = ulong 0
209for s ulong 4
210c = ulong 0
211for t ulong 4
212if bfEqDig s t
213c = add c 1
214end
215end
216if gt c bestC
217bestC = c
218bestS = s
219end
220end
221if eq bestC 4
222bfWrt bfLegUn
223bfPrintDig bestS
224bfWrt bfNl
225ret 0
226end
227if eq bestC 3
228fault = ulong 0
229for t ulong 4
230if not bfEqDig bestS t
231fault = t
232break
233end
234end
235bfWrt bfLegHl
236bfNum fault
237bfWrt bfDigS
238bfPrintDig bestS
239bfWrt bfNl
240ret int add 10 fault
241end
242bfWrt bfLegNo
243bfNum bestC
244bfWrt bfLegN2
245ret 20
246end
247
248fn bfDoQuorum to int
249if lt argcnt 4
250bfWrt bfUse
251ret 2
252end
253note Backward-compatible 4-arg form: bft_healer quorum H0 H1 H2 H3
254if eq argcnt 6
255k2 = argread 2 bfBuf 0 120
256if eq k2 64
257ret bfDoQuorumLegacy
258end
259end
260n = bfReadArgU 2
261if or eq n 0 gt n 7
262bfWrt bfUse
263ret 2
264end
265if ne ulong argcnt add 3 n
266bfWrt bfUse
267ret 2
268end
269for s ulong n bound 7
270k = argread add 3 s bfBuf 0 120
271if lt k 64
272bfWrt bfUse
273ret 2
274end
275o = mul s 64
276for i ulong 64
277put bfDig add o i get bfBuf i
278end
279end
280f = div sub n 1 3
281thresh = sub n f
282bestS = ulong 0
283bestC = ulong 0
284for s ulong n bound 7
285c = ulong 0
286for t ulong n bound 7
287if bfEqDig s t
288c = add c 1
289end
290end
291if gt c bestC
292bestC = c
293bestS = s
294end
295end
296if eq bestC n
297bfWrt bfUn1
298bfNum n
299bfWrt bfUn2
300bfNum f
301bfWrt bfUn3
302bfNum bestC
303bfWrt bfSl
304bfNum n
305bfWrt bfUn4
306bfPrintDig bestS
307bfWrt bfNl
308ret 0
309end
310if ge bestC thresh
311mask = ulong 0
312for t ulong n bound 7
313if not bfEqDig bestS t
314mask = or mask shl ulong 1 t
315end
316end
317bfWrt bfHl1
318bfNum n
319bfWrt bfUn2
320bfNum f
321bfWrt bfUn3
322bfNum bestC
323bfWrt bfSl
324bfNum n
325bfWrt bfHl2
326bfNum mask
327bfWrt bfDigS
328bfPrintDig bestS
329bfWrt bfNl
330ret int add 100 mask
331end
332bfWrt bfNoQ1
333bfNum n
334bfWrt bfUn2
335bfNum f
336bfWrt bfNoQ2
337bfNum bestC
338bfWrt bfSl
339bfNum n
340bfWrt bfNoQ3
341bfNum thresh
342bfWrt bfNoQ4
343ret 20
344end
345
346fn bfPrintEval inc ulong monkRc ulong trickRc ulong cand ulong champ ulong to int
347bfReadThermal
348tc = get bfThSt 0
349live = get bfThSt 1
350mode = bfArbMode true tc
351bfWrt bfEvPre
352retc = int 1
353if ne monkRc 0
354bfWrt bfDecM
355else
356if ne trickRc 0
357bfWrt bfDecT
358else
359if lt cand inc
360bfWrt bfDecH
361retc = 0
362else
363bfWrt bfDecS
364end
365end
366end
367bfWrt bfEvInc
368bfNum inc
369bfWrt bfEvCnd
370bfNum cand
371bfWrt bfEvChp
372bfNum champ
373bfWrt bfEvMrc
374bfNum monkRc
375bfWrt bfEvTrc
376bfNum trickRc
377bfWrt bfEvTmp
378bfNum tc
379bfWrt bfEvSns
380if ne live 0
381bfWrt bfSnsLv
382else
383bfWrt bfSnsFb
384end
385bfWrt bfEvMod
386if eq mode 0
387bfWrt bfModP
388else
389bfWrt bfModA
390end
391bfWrt bfNl
392ret retc
393end
394
395fn bfDoMitosisEval to int
396if ne argcnt 6
397bfWrt bfUse
398ret 2
399end
400inc = bfReadArgU 2
401monkRc = bfReadArgU 3
402trickRc = bfReadArgU 4
403cand = bfReadArgU 5
404if or or eq inc 0xffffffffffffffff eq monkRc 0xffffffffffffffff or eq trickRc 0xffffffffffffffff eq cand 0xffffffffffffffff
405bfWrt bfUse
406ret 2
407end
408champ = bfMitosisStep inc eq monkRc 0 eq trickRc 0 cand
409ret bfPrintEval inc monkRc trickRc cand champ
410end
411
412fn bfDoThermal to int
413bfReadThermal
414tc = get bfThSt 0
415live = get bfThSt 1
416mode = bfArbMode true tc
417bfWrt bfThOut
418bfNum tc
419bfWrt bfEvSns
420if ne live 0
421bfWrt bfSnsLv
422else
423bfWrt bfSnsFb
424end
425bfWrt bfEvMod
426if eq mode 0
427bfWrt bfModP
428else
429bfWrt bfModA
430end
431bfWrt bfNl
432ret 0
433end
434
435fn bfDoMitosis to int
436bfWrt bfMitHdr
437champ = ulong 15184
438c = bfMitosisStep champ true true 14200
439if ne c 14200
440bfWrt bfFail
441ret 1
442end
443r = bfPrintEval champ 0 0 14200 c
444champ = c
445c = bfMitosisStep champ false true 12000
446if ne c 14200
447bfWrt bfFail
448ret 1
449end
450r = bfPrintEval champ 1 0 12000 c
451c = bfMitosisStep champ true false 13100
452if ne c 14200
453bfWrt bfFail
454ret 1
455end
456r = bfPrintEval champ 0 1 13100 c
457c = bfMitosisStep champ true true 15900
458if ne c 14200
459bfWrt bfFail
460ret 1
461end
462r = bfPrintEval champ 0 0 15900 c
463c = bfMitosisStep champ true true 13400
464if ne c 13400
465bfWrt bfFail
466ret 1
467end
468r = bfPrintEval champ 0 0 13400 c
469champ = c
470bfReadThermal
471tc = get bfThSt 0
472if ne bfArbMode true add tc 85 1
473bfWrt bfFail
474ret 1
475end
476if ne bfArbMode false tc 1
477bfWrt bfFail
478ret 1
479end
480if ne bfArbMode true 52 0
481bfWrt bfFail
482ret 1
483end
484r = bfDoThermal
485bfWrt bfMitOk
486ret 0
487end
488
489fn main to int
490if lt argcnt 2
491bfWrt bfUse
492ret 2
493end
494cl = argread 1 bfBuf 0 32
495if lt cl 1
496bfWrt bfUse
497ret 2
498end
499c0 = ulong get bfBuf 0
500if eq c0 113
501ret bfDoQuorum
502end
503if eq c0 116
504ret bfDoThermal
505end
506if eq c0 109
507if ge cl 12
508if eq get bfBuf 7 45
509ret bfDoMitosisEval
510end
511end
512ret bfDoMitosis
513end
514bfWrt bfUse
515ret 2
516end