interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / om / eval / rollback.om

313 lines · 7145 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/rsi/om/eval/rollback.om
3note Pure OM-2 conversion of spark-rsi-mv/src/isolation/{rollback,root_of_trust,manifest}.rs.
4note Isolation rollback gate with SHA-256 root-of-trust checkpoint sealing,
5note automatic rollback on evaluator or canary failure, and hash-chained audit trail.
6note Compose after evaluator.om, om/tls/sha2.om, and canary.om.
7
8const RB_MAX_AUDIT ulong 32
9const RB_HASH_BYTES ulong 32
10const RB_AUDIT_HASH_TOTAL ulong 1024
11
12note Rollback gate decision codes
13const RB_COMMITTED ulong 0
14const RB_ROLLED_BACK_EVAL ulong 1
15const RB_ROLLED_BACK_CANARY ulong 2
16const RB_DENY_UNINIT ulong 10
17const RB_DENY_NO_STAGED ulong 11
18const RB_DENY_CORRUPT_ROT ulong 12
19const RB_DENY_ZERO_DIGEST ulong 13
20const RB_DENY_AUDIT_CORRUPT ulong 14
21
22text rb_rot_dom "RSI-ROT-CHECKPOINT-V1"
23text rb_aud_dom "RSI-ROLLBACK-AUDIT-V1"
24
25note Active baseline & staging metadata:
26note [0]=init(0/1), [1]=active_id, [2]=active_gen, [3]=staged_valid(0/1), [4]=staged_id,
27note [5]=commit_cnt, [6]=rollback_cnt, [7]=audit_cnt
28global rb_meta ulong 8
29
30note Active baseline digest and root-of-trust seal, plus staged candidate digest
31global rb_active_dig uchar 32
32global rb_active_rot uchar 32
33global rb_staged_dig uchar 32
34
35note Hash-chained rollback/commit audit trail (0..31)
36global rb_aud_seq ulong 32
37global rb_aud_cand ulong 32
38global rb_aud_code ulong 32
39global rb_aud_prev uchar 1024
40global rb_aud_hash uchar 1024
41
42fn rb_is_zero_hash h view uchar 32 to bool
43acc = uchar 0
44for i ulong 32
45acc = or acc get h i
46end
47ret eq acc uchar 0
48end
49
50fn rb_hash_eq a view uchar 32 b view uchar 32 to bool
51diff = uchar 0
52for i ulong 32
53diff = or diff xor get a i get b i
54end
55ret eq diff uchar 0
56end
57
58fn rb_put_u64 buf edit uchar * off ulong v ulong to void
59for i ulong 8
60sh = mul i 8
61put buf add off i uchar and shr v sh ulong 255
62end
63end
64
65note Compute root-of-trust seal over (active_id, active_gen, active_dig).
66fn rb_compute_rot aid ulong agen ulong dig view uchar 32 out_rot edit uchar 32 to void
67ctx = new ulong 40 0
68sha256_init ctx
69sha256_update ctx rb_rot_dom
70hdr = new uchar 16 0
71rb_put_u64 hdr 0 aid
72rb_put_u64 hdr 8 agen
73sha256_update ctx hdr
74sha256_update ctx dig
75sha256_final ctx out_rot
76end
77
78note Verify active baseline root-of-trust integrity.
79fn rb_verify_rot to bool
80if eq get rb_meta 0 0
81ret false
82end
83dig = new uchar 32 0
84cur = new uchar 32 0
85for i ulong 32
86put dig i get rb_active_dig i
87put cur i get rb_active_rot i
88end
89if rb_is_zero_hash dig
90ret false
91end
92calc = new uchar 32 0
93rb_compute_rot get rb_meta 1 get rb_meta 2 dig calc
94ret rb_hash_eq calc cur
95end
96
97note Initialize isolation rollback gate with a trusted baseline checkpoint.
98fn rb_init base_id ulong base_dig view uchar 32 to ulong
99for i ulong 8
100put rb_meta i 0
101end
102for i ulong 32
103put rb_active_dig i uchar 0
104put rb_active_rot i uchar 0
105put rb_staged_dig i uchar 0
106put rb_aud_seq i 0
107put rb_aud_cand i 0
108put rb_aud_code i 0
109end
110for i ulong RB_AUDIT_HASH_TOTAL
111put rb_aud_prev i uchar 0
112put rb_aud_hash i uchar 0
113end
114if rb_is_zero_hash base_dig
115ret RB_DENY_ZERO_DIGEST
116end
117for i ulong 32
118put rb_active_dig i get base_dig i
119end
120rot = new uchar 32 0
121rb_compute_rot base_id 1 base_dig rot
122for i ulong 32
123put rb_active_rot i get rot i
124end
125put rb_meta 0 1
126put rb_meta 1 base_id
127put rb_meta 2 1
128ret RB_COMMITTED
129end
130
131fn rb_active_id to ulong
132ret get rb_meta 1
133end
134
135fn rb_active_gen to ulong
136ret get rb_meta 2
137end
138
139fn rb_commit_count to ulong
140ret get rb_meta 5
141end
142
143fn rb_rollback_count to ulong
144ret get rb_meta 6
145end
146
147fn rb_audit_count to ulong
148ret get rb_meta 7
149end
150
151fn rb_has_staged to bool
152ret ne get rb_meta 3 0
153end
154
155note Stage an isolated candidate artifact for evaluation and canary gating.
156fn rb_stage cand_id ulong cand_dig view uchar 32 to ulong
157if eq get rb_meta 0 0
158ret RB_DENY_UNINIT
159end
160if not rb_verify_rot
161ret RB_DENY_CORRUPT_ROT
162end
163if rb_is_zero_hash cand_dig
164ret RB_DENY_ZERO_DIGEST
165end
166put rb_meta 3 1
167put rb_meta 4 cand_id
168for i ulong 32
169put rb_staged_dig i get cand_dig i
170end
171ret RB_COMMITTED
172end
173
174fn rb_compute_audit_hash idx ulong out_h edit uchar 32 to void
175ctx = new ulong 40 0
176sha256_init ctx
177sha256_update ctx rb_aud_dom
178hdr = new uchar 24 0
179rb_put_u64 hdr 0 get rb_aud_seq idx
180rb_put_u64 hdr 8 get rb_aud_cand idx
181rb_put_u64 hdr 16 get rb_aud_code idx
182sha256_update ctx hdr
183prev = new uchar 32 0
184pbase = mul idx RB_HASH_BYTES
185for i ulong 32
186put prev i get rb_aud_prev add pbase i
187end
188sha256_update ctx prev
189sha256_final ctx out_h
190end
191
192fn rb_append_audit cand_id ulong code ulong to void
193cnt = get rb_meta 7
194if ge cnt RB_MAX_AUDIT
195ret
196end
197put rb_aud_seq cnt add cnt 1
198put rb_aud_cand cnt cand_id
199put rb_aud_code cnt code
200pbase = mul cnt RB_HASH_BYTES
201if eq cnt 0
202for i ulong 32
203put rb_aud_prev add pbase i uchar 0
204end
205else
206prev_base = mul sub cnt 1 RB_HASH_BYTES
207for i ulong 32
208put rb_aud_prev add pbase i get rb_aud_hash add prev_base i
209end
210end
211dig = new uchar 32 0
212rb_compute_audit_hash cnt dig
213for i ulong 32
214put rb_aud_hash add pbase i get dig i
215end
216put rb_meta 7 add cnt 1
217end
218
219fn rb_clear_staged to void
220put rb_meta 3 0
221put rb_meta 4 0
222for i ulong 32
223put rb_staged_dig i uchar 0
224end
225end
226
227note Execute isolation rollback gate decision: commit only if ROT valid, eval PASS, and canary PASS.
228note Otherwise discard staged candidate, keep baseline checkpoint intact, and record rollback audit.
229fn rb_gate_decide eval_verdict ulong canary_verdict ulong to ulong
230if eq get rb_meta 0 0
231ret RB_DENY_UNINIT
232end
233if not rb_verify_rot
234rb_clear_staged
235ret RB_DENY_CORRUPT_ROT
236end
237if eq get rb_meta 3 0
238ret RB_DENY_NO_STAGED
239end
240cid = get rb_meta 4
241if ne eval_verdict EV_PASS
242rb_clear_staged
243put rb_meta 6 add get rb_meta 6 1
244rb_append_audit cid RB_ROLLED_BACK_EVAL
245ret RB_ROLLED_BACK_EVAL
246end
247if ne canary_verdict CNR_PASS
248rb_clear_staged
249put rb_meta 6 add get rb_meta 6 1
250rb_append_audit cid RB_ROLLED_BACK_CANARY
251ret RB_ROLLED_BACK_CANARY
252end
253note Both evaluator and canary passed: promote staged candidate to active baseline with new ROT seal.
254new_gen = add get rb_meta 2 1
255dig = new uchar 32 0
256for i ulong 32
257b = get rb_staged_dig i
258put dig i b
259put rb_active_dig i b
260end
261rot = new uchar 32 0
262rb_compute_rot cid new_gen dig rot
263for i ulong 32
264put rb_active_rot i get rot i
265end
266put rb_meta 1 cid
267put rb_meta 2 new_gen
268put rb_meta 5 add get rb_meta 5 1
269rb_clear_staged
270rb_append_audit cid RB_COMMITTED
271ret RB_COMMITTED
272end
273
274fn rb_verify_audit_entry idx ulong to bool
275prev = new uchar 32 0
276exp_prev = new uchar 32 0
277calc = new uchar 32 0
278stored = new uchar 32 0
279if ne get rb_aud_seq idx add idx 1
280ret false
281end
282pbase = mul idx RB_HASH_BYTES
283for j ulong 32
284put prev j get rb_aud_prev add pbase j
285put stored j get rb_aud_hash add pbase j
286end
287if eq idx 0
288if not rb_is_zero_hash prev
289ret false
290end
291else
292pb = mul sub idx 1 RB_HASH_BYTES
293for j ulong 32
294put exp_prev j get rb_aud_hash add pb j
295end
296if not rb_hash_eq prev exp_prev
297ret false
298end
299end
300rb_compute_audit_hash idx calc
301ret rb_hash_eq calc stored
302end
303
304note Verify entire SHA-256 hash-chained rollback audit trail.
305fn rb_verify_audit_chain to ulong
306cnt = get rb_meta 7
307for i ulong cnt bound 32
308if not rb_verify_audit_entry i
309ret RB_DENY_AUDIT_CORRUPT
310end
311end
312ret RB_COMMITTED
313end