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 / governance / test.om

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