spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / governance / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/rsi/om/governance/test.om |
| 3 | note Comprehensive behavioral test suite with negative/refusal controls for the |
| 4 | note pure-Omega RSI safety envelope, promotion gate, and SHA-256 Merkle ledger. |
| 5 | note Compose with: |
| 6 | note si/directives/om2.om core/rsi/om/governance/promotion_gate.om om/tls/sha2.om |
| 7 | note core/rsi/om/governance/ledger.om core/rsi/om/governance/test.om |
| 8 | |
| 9 | text t_pass "PASS " |
| 10 | text t_fail "FAIL " |
| 11 | text t_nl "\n" |
| 12 | text t_all_ok "RSI GOVERNANCE TEST SUITE PASS\n" |
| 13 | text t_all_bad "RSI GOVERNANCE TEST SUITE FAIL\n" |
| 14 | |
| 15 | text t_n1 "rsi_gate_promote_valid_candidate" |
| 16 | text t_n2 "rsi_gate_negative_synthetic_branch_protected" |
| 17 | text t_n3 "rsi_gate_negative_security_replay_envelope_regression" |
| 18 | text t_n4 "rsi_gate_meta_tier_and_no_gain_hold" |
| 19 | text t_n5 "rsi_ledger_hash_chain_and_merkle_inclusion" |
| 20 | text t_n6 "rsi_ledger_negative_tamper_and_merkle_mismatch" |
| 21 | |
| 22 | global t_fails ulong 1 |
| 23 | |
| 24 | fn t_w s view uchar * to void |
| 25 | n = len s |
| 26 | off = ulong 0 |
| 27 | while lt off n bound 256 |
| 28 | k = write 1 s off sub n off |
| 29 | if le k 0 |
| 30 | break |
| 31 | end |
| 32 | off = add off ulong k |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | fn t_report name view uchar * ok bool to void |
| 37 | if ok |
| 38 | t_w t_pass |
| 39 | else |
| 40 | t_w t_fail |
| 41 | put t_fails 0 add get t_fails 0 1 |
| 42 | end |
| 43 | t_w name |
| 44 | t_w t_nl |
| 45 | end |
| 46 | |
| 47 | note Test 1: Valid measured-canary branch-only candidate promotes cleanly. |
| 48 | fn test_rsi_promote_valid to bool |
| 49 | note max_diff=500, max_files=12, min_corr=10000, max_reg=50, min_perf=100, min_res=100 |
| 50 | rsi_gate_init 500 12 10000 50 100 100 |
| 51 | c = new RsiCandidate 101 RSI_TIER_L1_KERNEL 120 3 10000 long 250 long 10 0 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 52 | v = rsi_evaluate_candidate c |
| 53 | if ne v RSI_PROMOTE |
| 54 | ret false |
| 55 | end |
| 56 | if or ne rsi_gate_evaluated 1 ne rsi_gate_promoted 1 |
| 57 | ret false |
| 58 | end |
| 59 | ret true |
| 60 | end |
| 61 | |
| 62 | note Test 2 (NEGATIVE): Synthetic trust, non-branch-only, and protected paths are all refused. |
| 63 | fn test_rsi_negative_trust_branch_protected to bool |
| 64 | rsi_gate_init 500 12 10000 50 100 100 |
| 65 | c_syn = new RsiCandidate 201 RSI_TIER_L1_KERNEL 50 1 10000 long 300 long 0 0 1 RSI_TRUST_SYNTHETIC 0 1 |
| 66 | if ne rsi_evaluate_candidate c_syn RSI_DENY_SYNTHETIC_TRUST |
| 67 | ret false |
| 68 | end |
| 69 | c_nob = new RsiCandidate 202 RSI_TIER_L1_KERNEL 50 1 10000 long 300 long 0 0 1 RSI_TRUST_MEASURED_CANARY 0 0 |
| 70 | if ne rsi_evaluate_candidate c_nob RSI_DENY_NOT_BRANCH_ONLY |
| 71 | ret false |
| 72 | end |
| 73 | c_prot = new RsiCandidate 203 RSI_TIER_L1_KERNEL 50 1 10000 long 300 long 0 0 1 RSI_TRUST_MEASURED_SILICON 1 1 |
| 74 | if ne rsi_evaluate_candidate c_prot RSI_DENY_PROTECTED_PATH |
| 75 | ret false |
| 76 | end |
| 77 | if or ne rsi_gate_denied 3 ne rsi_gate_promoted 0 |
| 78 | ret false |
| 79 | end |
| 80 | ret true |
| 81 | end |
| 82 | |
| 83 | note Test 3 (NEGATIVE): Security findings, replay failure, blast radius, and regression refused. |
| 84 | fn test_rsi_negative_sec_replay_env_reg to bool |
| 85 | rsi_gate_init 500 12 10000 50 100 100 |
| 86 | c_sec = new RsiCandidate 301 RSI_TIER_L1_KERNEL 40 2 10000 long 200 long 0 1 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 87 | if ne rsi_evaluate_candidate c_sec RSI_DENY_SECURITY |
| 88 | ret false |
| 89 | end |
| 90 | c_corr = new RsiCandidate 302 RSI_TIER_L1_KERNEL 40 2 9999 long 200 long 0 0 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 91 | if ne rsi_evaluate_candidate c_corr RSI_DENY_CORRECTNESS |
| 92 | ret false |
| 93 | end |
| 94 | c_rep = new RsiCandidate 303 RSI_TIER_L1_KERNEL 40 2 10000 long 200 long 0 0 0 RSI_TRUST_MEASURED_CANARY 0 1 |
| 95 | if ne rsi_evaluate_candidate c_rep RSI_DENY_REPLAY |
| 96 | ret false |
| 97 | end |
| 98 | c_env = new RsiCandidate 304 RSI_TIER_L1_KERNEL 501 2 10000 long 200 long 0 0 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 99 | if ne rsi_evaluate_candidate c_env RSI_DENY_ENVELOPE |
| 100 | ret false |
| 101 | end |
| 102 | c_reg = new RsiCandidate 305 RSI_TIER_L1_KERNEL 40 2 10000 long 400 long -51 0 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 103 | if ne rsi_evaluate_candidate c_reg RSI_DENY_REGRESSION |
| 104 | ret false |
| 105 | end |
| 106 | if ne rsi_gate_denied 5 |
| 107 | ret false |
| 108 | end |
| 109 | ret true |
| 110 | end |
| 111 | |
| 112 | note Test 4: No-gain hold and L3_META stricter 2x gain threshold. |
| 113 | fn test_rsi_meta_and_hold to bool |
| 114 | rsi_gate_init 500 12 10000 50 100 100 |
| 115 | c_hold = new RsiCandidate 401 RSI_TIER_L0_PARAM 10 1 10000 long 40 long 20 0 1 RSI_TRUST_MEASURED_CANARY 0 1 |
| 116 | if ne rsi_evaluate_candidate c_hold RSI_HOLD_NO_GAIN |
| 117 | ret false |
| 118 | end |
| 119 | note L3_META with 150 bps gain (< 2*100 = 200) is denied with RSI_DENY_META_THRESHOLD |
| 120 | c_mlow = new RsiCandidate 402 RSI_TIER_L3_META 80 2 10000 long 150 long 0 0 1 RSI_TRUST_MEASURED_SILICON 0 1 |
| 121 | if ne rsi_evaluate_candidate c_mlow RSI_DENY_META_THRESHOLD |
| 122 | ret false |
| 123 | end |
| 124 | note L3_META with 200 bps gain (>= 2*100) promotes |
| 125 | c_mok = new RsiCandidate 403 RSI_TIER_L3_META 80 2 10000 long 200 long 0 0 1 RSI_TRUST_MEASURED_SILICON 0 1 |
| 126 | if ne rsi_evaluate_candidate c_mok RSI_PROMOTE |
| 127 | ret false |
| 128 | end |
| 129 | if or ne rsi_gate_held 1 or ne rsi_gate_denied 1 ne rsi_gate_promoted 1 |
| 130 | ret false |
| 131 | end |
| 132 | ret true |
| 133 | end |
| 134 | |
| 135 | note Test 5: Cryptographic SHA-256 ledger hash chain and Merkle root + leaf inclusion proofs (5 leaves). |
| 136 | fn test_rsi_ledger_and_merkle to bool |
| 137 | rsi_ledger_reset |
| 138 | root1 = new uchar 32 0 |
| 139 | note Empty ledger must refuse Merkle root |
| 140 | if ne rsi_merkle_root root1 RSI_LDG_ERR_EMPTY |
| 141 | ret false |
| 142 | end |
| 143 | for i ulong 5 |
| 144 | st = rsi_ledger_append add ulong 1000 i rem i 4 RSI_PROMOTE mul long i long 50 10000 RSI_TRUST_MEASURED_CANARY |
| 145 | if ne st RSI_LDG_OK |
| 146 | ret false |
| 147 | end |
| 148 | end |
| 149 | if ne rsi_ledger_count 5 |
| 150 | ret false |
| 151 | end |
| 152 | if ne rsi_ledger_verify_chain RSI_LDG_OK |
| 153 | ret false |
| 154 | end |
| 155 | if ne rsi_merkle_root root1 RSI_LDG_OK |
| 156 | ret false |
| 157 | end |
| 158 | note Root must be deterministic on second call |
| 159 | root2 = new uchar 32 0 |
| 160 | if ne rsi_merkle_root root2 RSI_LDG_OK |
| 161 | ret false |
| 162 | end |
| 163 | if not rldg_hash_eq root1 root2 |
| 164 | ret false |
| 165 | end |
| 166 | note Verify Merkle inclusion proof for every leaf 0..4 |
| 167 | for i ulong 5 |
| 168 | if not rsi_merkle_verify_leaf i root1 |
| 169 | ret false |
| 170 | end |
| 171 | end |
| 172 | note Out-of-bounds leaf index must fail |
| 173 | if rsi_merkle_verify_leaf 5 root1 |
| 174 | ret false |
| 175 | end |
| 176 | ret true |
| 177 | end |
| 178 | |
| 179 | note Test 6 (NEGATIVE): Tampered record payload, broken prev_hash, and mutated Merkle root are caught. |
| 180 | fn test_rsi_ledger_negative_tamper to bool |
| 181 | rsi_ledger_reset |
| 182 | for i ulong 4 |
| 183 | st = rsi_ledger_append add ulong 2000 i RSI_TIER_L1_KERNEL RSI_PROMOTE long 120 10000 RSI_TRUST_MEASURED_SILICON |
| 184 | if ne st RSI_LDG_OK |
| 185 | ret false |
| 186 | end |
| 187 | end |
| 188 | root = new uchar 32 0 |
| 189 | if ne rsi_merkle_root root RSI_LDG_OK |
| 190 | ret false |
| 191 | end |
| 192 | note Mutated root must fail inclusion check |
| 193 | bad_root = new uchar 32 0 |
| 194 | for j ulong 32 |
| 195 | put bad_root j get root j |
| 196 | end |
| 197 | put bad_root 0 xor get bad_root 0 uchar 1 |
| 198 | if rsi_merkle_verify_leaf 2 bad_root |
| 199 | ret false |
| 200 | end |
| 201 | note Tamper with record 2 perf_delta_bps -> chain verification must report RSI_LDG_ERR_HASH |
| 202 | rsi_ledger_tamper_perf 2 long 9999 |
| 203 | if ne rsi_ledger_verify_chain RSI_LDG_ERR_HASH |
| 204 | ret false |
| 205 | end |
| 206 | note Restore perf_delta_bps, then tamper with record 3 prev_hash -> must report RSI_LDG_ERR_PREV_HASH |
| 207 | rsi_ledger_tamper_perf 2 long 120 |
| 208 | if ne rsi_ledger_verify_chain RSI_LDG_OK |
| 209 | ret false |
| 210 | end |
| 211 | rsi_ledger_tamper_prev 3 7 uchar 0x80 |
| 212 | if ne rsi_ledger_verify_chain RSI_LDG_ERR_PREV_HASH |
| 213 | ret false |
| 214 | end |
| 215 | ret true |
| 216 | end |
| 217 | |
| 218 | fn main to int |
| 219 | put t_fails 0 0 |
| 220 | t_report t_n1 test_rsi_promote_valid |
| 221 | t_report t_n2 test_rsi_negative_trust_branch_protected |
| 222 | t_report t_n3 test_rsi_negative_sec_replay_env_reg |
| 223 | t_report t_n4 test_rsi_meta_and_hold |
| 224 | t_report t_n5 test_rsi_ledger_and_merkle |
| 225 | t_report t_n6 test_rsi_ledger_negative_tamper |
| 226 | if eq get t_fails 0 0 |
| 227 | t_w t_all_ok |
| 228 | ret 0 |
| 229 | end |
| 230 | t_w t_all_bad |
| 231 | ret 1 |
| 232 | end |