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