spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / eval / eval_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/rsi/om/eval/eval_test.om |
| 3 | note Positive, negative, and mutant test suite for core/rsi/om/eval/{evaluator,canary,rollback}.om. |
| 4 | note Compose with: |
| 5 | note si/directives/om2.om core/rsi/om/eval/evaluator.om om/tls/sha2.om |
| 6 | note core/rsi/om/eval/canary.om core/rsi/om/eval/rollback.om core/rsi/om/eval/eval_test.om |
| 7 | |
| 8 | text ev_t_pass "PASS " |
| 9 | text ev_t_fail "FAIL " |
| 10 | text ev_t_nl "\n" |
| 11 | text ev_t_all_ok "RSI EVAL CANARY ROLLBACK TEST SUITE PASS\n" |
| 12 | text ev_t_all_bad "RSI EVAL CANARY ROLLBACK TEST SUITE FAIL\n" |
| 13 | |
| 14 | text ev_t_n1 "rsi_eval_pos_end_to_end_commit" |
| 15 | text ev_t_n2 "rsi_eval_neg_evaluator_layers_and_invalid_samples" |
| 16 | text ev_t_n3 "rsi_eval_neg_canary_synthetic_crash_error_latency_window" |
| 17 | text ev_t_n4 "rsi_eval_neg_rollback_on_eval_and_canary_failure" |
| 18 | text ev_t_n5 "rsi_eval_mutant_rot_identity_and_audit_chain_tamper" |
| 19 | |
| 20 | text ev_base_art "rsi-baseline-v1" |
| 21 | text ev_cand1_art "rsi-candidate-101-kernel-opt" |
| 22 | text ev_cand2_art "rsi-candidate-102-regressed" |
| 23 | |
| 24 | global ev_t_fails ulong 1 |
| 25 | |
| 26 | fn ev_t_w s view uchar * to void |
| 27 | n = len s |
| 28 | off = ulong 0 |
| 29 | while lt off n bound 256 |
| 30 | k = write 1 s off sub n off |
| 31 | if le k 0 |
| 32 | break |
| 33 | end |
| 34 | off = add off ulong k |
| 35 | end |
| 36 | end |
| 37 | |
| 38 | fn ev_t_rep name view uchar * ok bool to void |
| 39 | if ok |
| 40 | ev_t_w ev_t_pass |
| 41 | else |
| 42 | ev_t_w ev_t_fail |
| 43 | put ev_t_fails 0 add get ev_t_fails 0 1 |
| 44 | end |
| 45 | ev_t_w name |
| 46 | ev_t_w ev_t_nl |
| 47 | end |
| 48 | |
| 49 | fn ev_t_sha msg view uchar * out_h edit uchar 32 to void |
| 50 | ctx = new ulong 40 0 |
| 51 | sha256_init ctx |
| 52 | sha256_update ctx msg |
| 53 | sha256_final ctx out_h |
| 54 | end |
| 55 | |
| 56 | note Test 1 (POSITIVE): End-to-end valid candidate passes evaluator, canary, and commits through rollback gate. |
| 57 | fn test_ev_pos_e2e to bool |
| 58 | ev_init 10000 0 50 50 0 |
| 59 | cnr_init 100 50 100 |
| 60 | base_d = new uchar 32 0 |
| 61 | cand_d = new uchar 32 0 |
| 62 | ev_t_sha ev_base_art base_d |
| 63 | ev_t_sha ev_cand1_art cand_d |
| 64 | if ne rb_init 1 base_d RB_COMMITTED |
| 65 | ret false |
| 66 | end |
| 67 | if ne rb_stage 101 cand_d RB_COMMITTED |
| 68 | ret false |
| 69 | end |
| 70 | note Evaluator: 50/50 tests, 0 sec, base_ns=10000 -> cand_ns=9000 (+1000 bps), mem 4096 -> 3891, replay ok, 0 style |
| 71 | s = new EvalSample 101 50 50 0 0 10000 9000 4096 3891 1 0 |
| 72 | ev_v = ev_evaluate s |
| 73 | if ne ev_v EV_PASS |
| 74 | ret false |
| 75 | end |
| 76 | if or ne ev_last_corr_bps 10000 ne ev_last_perf_delta_bps long 1000 |
| 77 | ret false |
| 78 | end |
| 79 | note Canary: measured host, 2 batches totaling 120 reqs, 0 err, 0 crash, p99=9100 <= 10000 |
| 80 | if ne cnr_begin 101 CNR_PROV_CANARY_HOST 10000 cand_d CNR_PASS |
| 81 | ret false |
| 82 | end |
| 83 | if ne cnr_observe 60 0 0 9100 CNR_PASS |
| 84 | ret false |
| 85 | end |
| 86 | if ne cnr_observe 60 0 0 9050 CNR_PASS |
| 87 | ret false |
| 88 | end |
| 89 | cnr_v = cnr_evaluate 101 cand_d |
| 90 | if ne cnr_v CNR_PASS |
| 91 | ret false |
| 92 | end |
| 93 | note Rollback gate commits candidate 101 -> active_id=101, gen=2, commits=1, audit intact |
| 94 | if ne rb_gate_decide ev_v cnr_v RB_COMMITTED |
| 95 | ret false |
| 96 | end |
| 97 | if or ne rb_active_id 101 ne rb_active_gen 2 |
| 98 | ret false |
| 99 | end |
| 100 | if or ne rb_commit_count 1 ne rb_rollback_count 0 |
| 101 | ret false |
| 102 | end |
| 103 | if not rb_verify_rot |
| 104 | ret false |
| 105 | end |
| 106 | if ne rb_verify_audit_chain RB_COMMITTED |
| 107 | ret false |
| 108 | end |
| 109 | ret true |
| 110 | end |
| 111 | |
| 112 | note Test 2 (NEGATIVE): Evaluator refuses invalid samples, security, correctness, replay, perf/res regression, style. |
| 113 | fn test_ev_neg_evaluator to bool |
| 114 | ev_init 10000 0 50 50 2 |
| 115 | s_inv = new EvalSample 200 10 0 0 0 10000 9500 1000 1000 1 0 |
| 116 | if ne ev_evaluate s_inv EV_DENY_INVALID_SAMPLE |
| 117 | ret false |
| 118 | end |
| 119 | s_sec = new EvalSample 201 50 50 1 0 10000 9500 1000 1000 1 0 |
| 120 | if ne ev_evaluate s_sec EV_DENY_SECURITY |
| 121 | ret false |
| 122 | end |
| 123 | s_corr = new EvalSample 202 49 50 0 0 10000 9500 1000 1000 1 0 |
| 124 | if ne ev_evaluate s_corr EV_DENY_CORRECTNESS |
| 125 | ret false |
| 126 | end |
| 127 | s_rep = new EvalSample 203 50 50 0 0 10000 9500 1000 1000 0 0 |
| 128 | if ne ev_evaluate s_rep EV_DENY_REPLAY |
| 129 | ret false |
| 130 | end |
| 131 | note Perf regression: 10000 -> 10100 (-100 bps < -50 bps) |
| 132 | s_preg = new EvalSample 204 50 50 0 0 10000 10100 1000 1000 1 0 |
| 133 | if ne ev_evaluate s_preg EV_DENY_PERF_REGRESS |
| 134 | ret false |
| 135 | end |
| 136 | note Resource regression: 1000 -> 1020 (-200 bps < -50 bps) |
| 137 | s_rreg = new EvalSample 205 50 50 0 0 10000 9900 1000 1020 1 0 |
| 138 | if ne ev_evaluate s_rreg EV_DENY_RES_REGRESS |
| 139 | ret false |
| 140 | end |
| 141 | note Style violations: 3 > 2 |
| 142 | s_sty = new EvalSample 206 50 50 0 0 10000 9900 1000 1000 1 3 |
| 143 | if ne ev_evaluate s_sty EV_DENY_STYLE |
| 144 | ret false |
| 145 | end |
| 146 | if or ne ev_denied 7 ne ev_passed 0 |
| 147 | ret false |
| 148 | end |
| 149 | ret true |
| 150 | end |
| 151 | |
| 152 | note Test 3 (NEGATIVE): Canary refuses synthetic provenance, zero digest, insufficient window, crash, error rate, latency. |
| 153 | fn test_ev_neg_canary to bool |
| 154 | cnr_init 100 50 100 |
| 155 | zero_d = new uchar 32 0 |
| 156 | cand_d = new uchar 32 0 |
| 157 | ev_t_sha ev_cand1_art cand_d |
| 158 | if ne cnr_begin 301 CNR_PROV_SYNTHETIC 10000 cand_d CNR_DENY_SYNTHETIC |
| 159 | ret false |
| 160 | end |
| 161 | if ne cnr_begin 302 CNR_PROV_CANARY_HOST 10000 zero_d CNR_DENY_UNBOUND_ID |
| 162 | ret false |
| 163 | end |
| 164 | note Insufficient samples -> HOLD |
| 165 | if ne cnr_begin 303 CNR_PROV_CANARY_HOST 10000 cand_d CNR_PASS |
| 166 | ret false |
| 167 | end |
| 168 | if ne cnr_observe 50 0 0 10000 CNR_PASS |
| 169 | ret false |
| 170 | end |
| 171 | if ne cnr_evaluate 303 cand_d CNR_HOLD_INSUFFICIENT_SAMPLES |
| 172 | ret false |
| 173 | end |
| 174 | note Crash -> DENY_CRASH |
| 175 | if ne cnr_observe 60 0 1 10000 CNR_PASS |
| 176 | ret false |
| 177 | end |
| 178 | if ne cnr_evaluate 303 cand_d CNR_DENY_CRASH |
| 179 | ret false |
| 180 | end |
| 181 | note Error rate: 2 / 200 = 100 bps > 50 bps -> DENY_ERROR_RATE |
| 182 | if ne cnr_begin 304 CNR_PROV_SILICON 10000 cand_d CNR_PASS |
| 183 | ret false |
| 184 | end |
| 185 | if ne cnr_observe 200 2 0 10000 CNR_PASS |
| 186 | ret false |
| 187 | end |
| 188 | if ne cnr_evaluate 304 cand_d CNR_DENY_ERROR_RATE |
| 189 | ret false |
| 190 | end |
| 191 | note Latency regression: 10000 -> 10200 (+200 bps > 100 bps) -> DENY_LATENCY |
| 192 | if ne cnr_begin 305 CNR_PROV_SILICON 10000 cand_d CNR_PASS |
| 193 | ret false |
| 194 | end |
| 195 | if ne cnr_observe 200 0 0 10200 CNR_PASS |
| 196 | ret false |
| 197 | end |
| 198 | if ne cnr_evaluate 305 cand_d CNR_DENY_LATENCY |
| 199 | ret false |
| 200 | end |
| 201 | ret true |
| 202 | end |
| 203 | |
| 204 | note Test 4 (NEGATIVE): Rollback gate automatically rolls back on evaluator or canary failure and preserves baseline. |
| 205 | fn test_ev_neg_rollback to bool |
| 206 | base_d = new uchar 32 0 |
| 207 | cand_d = new uchar 32 0 |
| 208 | ev_t_sha ev_base_art base_d |
| 209 | ev_t_sha ev_cand2_art cand_d |
| 210 | if ne rb_init 10 base_d RB_COMMITTED |
| 211 | ret false |
| 212 | end |
| 213 | note No staged candidate -> RB_DENY_NO_STAGED |
| 214 | if ne rb_gate_decide EV_PASS CNR_PASS RB_DENY_NO_STAGED |
| 215 | ret false |
| 216 | end |
| 217 | note Stage candidate 201, fail evaluator -> RB_ROLLED_BACK_EVAL, baseline stays 10 gen 1 |
| 218 | if ne rb_stage 201 cand_d RB_COMMITTED |
| 219 | ret false |
| 220 | end |
| 221 | if ne rb_gate_decide EV_DENY_PERF_REGRESS CNR_PASS RB_ROLLED_BACK_EVAL |
| 222 | ret false |
| 223 | end |
| 224 | if or ne rb_active_id 10 ne rb_active_gen 1 |
| 225 | ret false |
| 226 | end |
| 227 | if rb_has_staged |
| 228 | ret false |
| 229 | end |
| 230 | note Stage candidate 202, pass evaluator, fail canary -> RB_ROLLED_BACK_CANARY, baseline still 10 gen 1 |
| 231 | if ne rb_stage 202 cand_d RB_COMMITTED |
| 232 | ret false |
| 233 | end |
| 234 | if ne rb_gate_decide EV_PASS CNR_DENY_CRASH RB_ROLLED_BACK_CANARY |
| 235 | ret false |
| 236 | end |
| 237 | if or ne rb_active_id 10 ne rb_rollback_count 2 |
| 238 | ret false |
| 239 | end |
| 240 | if ne rb_audit_count 2 |
| 241 | ret false |
| 242 | end |
| 243 | if ne rb_verify_audit_chain RB_COMMITTED |
| 244 | ret false |
| 245 | end |
| 246 | ret true |
| 247 | end |
| 248 | |
| 249 | note Test 5 (MUTANT CONTROLS): Tampered canary artifact digest, tampered ROT, and tampered audit chain are caught. |
| 250 | fn test_ev_mutant_controls to bool |
| 251 | cnr_init 100 50 100 |
| 252 | base_d = new uchar 32 0 |
| 253 | cand_d = new uchar 32 0 |
| 254 | mut_d = new uchar 32 0 |
| 255 | ev_t_sha ev_base_art base_d |
| 256 | ev_t_sha ev_cand1_art cand_d |
| 257 | for i ulong 32 |
| 258 | put mut_d i get cand_d i |
| 259 | end |
| 260 | put mut_d 0 xor get mut_d 0 uchar 1 |
| 261 | note Mutant A: canary bound to cand_d, evaluated with 1-bit mutated mut_d -> CNR_DENY_ID_MISMATCH |
| 262 | if ne cnr_begin 401 CNR_PROV_SILICON 10000 cand_d CNR_PASS |
| 263 | ret false |
| 264 | end |
| 265 | if ne cnr_observe 150 0 0 9900 CNR_PASS |
| 266 | ret false |
| 267 | end |
| 268 | if ne cnr_evaluate 401 mut_d CNR_DENY_ID_MISMATCH |
| 269 | ret false |
| 270 | end |
| 271 | note Mutant B: flip 1 bit in active root-of-trust digest -> rb_gate_decide refuses RB_DENY_CORRUPT_ROT |
| 272 | if ne rb_init 50 base_d RB_COMMITTED |
| 273 | ret false |
| 274 | end |
| 275 | if ne rb_stage 401 cand_d RB_COMMITTED |
| 276 | ret false |
| 277 | end |
| 278 | orig_rot = get rb_active_rot 7 |
| 279 | put rb_active_rot 7 xor orig_rot uchar 1 |
| 280 | if ne rb_gate_decide EV_PASS CNR_PASS RB_DENY_CORRUPT_ROT |
| 281 | ret false |
| 282 | end |
| 283 | put rb_active_rot 7 orig_rot |
| 284 | if ne rb_stage 401 cand_d RB_COMMITTED |
| 285 | ret false |
| 286 | end |
| 287 | if ne rb_gate_decide EV_PASS CNR_PASS RB_COMMITTED |
| 288 | ret false |
| 289 | end |
| 290 | note Mutant C: flip 1 bit in audit chain hash -> rb_verify_audit_chain returns RB_DENY_AUDIT_CORRUPT |
| 291 | orig_ah = get rb_aud_hash 3 |
| 292 | put rb_aud_hash 3 xor orig_ah uchar 1 |
| 293 | if ne rb_verify_audit_chain RB_DENY_AUDIT_CORRUPT |
| 294 | ret false |
| 295 | end |
| 296 | put rb_aud_hash 3 orig_ah |
| 297 | if ne rb_verify_audit_chain RB_COMMITTED |
| 298 | ret false |
| 299 | end |
| 300 | ret true |
| 301 | end |
| 302 | |
| 303 | fn main to int |
| 304 | put ev_t_fails 0 0 |
| 305 | ev_t_rep ev_t_n1 test_ev_pos_e2e |
| 306 | ev_t_rep ev_t_n2 test_ev_neg_evaluator |
| 307 | ev_t_rep ev_t_n3 test_ev_neg_canary |
| 308 | ev_t_rep ev_t_n4 test_ev_neg_rollback |
| 309 | ev_t_rep ev_t_n5 test_ev_mutant_controls |
| 310 | if eq get ev_t_fails 0 0 |
| 311 | ev_t_w ev_t_all_ok |
| 312 | ret 0 |
| 313 | end |
| 314 | ev_t_w ev_t_all_bad |
| 315 | ret 1 |
| 316 | end |