spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / eval / evaluator.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/rsi/om/eval/evaluator.om |
| 3 | note Pure OM-2 conversion of spark-rsi-mv/src/evaluator/{mod,metrics,stats,layers/*}.rs. |
| 4 | note Deterministic multi-layer candidate evaluator: correctness, security, longitudinal |
| 5 | note replay, performance delta (bps), resource efficiency delta (bps), and style bounds. |
| 6 | note Compose immediately after si/directives/om2.om (before om/tls/sha2.om because of struct). |
| 7 | |
| 8 | note Evaluator verdicts |
| 9 | const EV_PASS ulong 0 |
| 10 | const EV_DENY_UNINIT ulong 1 |
| 11 | const EV_DENY_INVALID_SAMPLE ulong 2 |
| 12 | const EV_DENY_CORRECTNESS ulong 3 |
| 13 | const EV_DENY_SECURITY ulong 4 |
| 14 | const EV_DENY_REPLAY ulong 5 |
| 15 | const EV_DENY_PERF_REGRESS ulong 6 |
| 16 | const EV_DENY_RES_REGRESS ulong 7 |
| 17 | const EV_DENY_STYLE ulong 8 |
| 18 | |
| 19 | const EV_BPS_SCALE ulong 10000 |
| 20 | const EV_MAX_SAFE_METRIC ulong 900000000000000 |
| 21 | |
| 22 | struct EvalSample cand_id ulong tests_passed ulong tests_total ulong sec_critical ulong sec_high ulong base_ns ulong cand_ns ulong base_mem_kb ulong cand_mem_kb ulong replay_ok ulong style_violations ulong end |
| 23 | |
| 24 | note Config: [0]=init(0/1), [1]=min_corr_bps, [2]=max_sec_high, [3]=max_perf_reg_bps, [4]=max_res_reg_bps, [5]=max_style |
| 25 | global ev_cfg ulong 8 |
| 26 | |
| 27 | note Last computed metrics & counters: [0]=evaluated, [1]=passed, [2]=denied, [3]=last_corr_bps |
| 28 | global ev_stats ulong 4 |
| 29 | note Signed deltas: [0]=last_perf_delta_bps, [1]=last_res_delta_bps |
| 30 | global ev_deltas long 2 |
| 31 | |
| 32 | fn ev_init min_corr_bps ulong max_sec_high ulong max_perf_reg_bps ulong max_res_reg_bps ulong max_style ulong to void |
| 33 | put ev_cfg 0 1 |
| 34 | put ev_cfg 1 min_corr_bps |
| 35 | put ev_cfg 2 max_sec_high |
| 36 | put ev_cfg 3 max_perf_reg_bps |
| 37 | put ev_cfg 4 max_res_reg_bps |
| 38 | put ev_cfg 5 max_style |
| 39 | for i ulong 4 |
| 40 | put ev_stats i 0 |
| 41 | end |
| 42 | put ev_deltas 0 long 0 |
| 43 | put ev_deltas 1 long 0 |
| 44 | end |
| 45 | |
| 46 | fn ev_evaluated to ulong |
| 47 | ret get ev_stats 0 |
| 48 | end |
| 49 | |
| 50 | fn ev_passed to ulong |
| 51 | ret get ev_stats 1 |
| 52 | end |
| 53 | |
| 54 | fn ev_denied to ulong |
| 55 | ret get ev_stats 2 |
| 56 | end |
| 57 | |
| 58 | fn ev_last_corr_bps to ulong |
| 59 | ret get ev_stats 3 |
| 60 | end |
| 61 | |
| 62 | fn ev_last_perf_delta_bps to long |
| 63 | ret get ev_deltas 0 |
| 64 | end |
| 65 | |
| 66 | fn ev_last_res_delta_bps to long |
| 67 | ret get ev_deltas 1 |
| 68 | end |
| 69 | |
| 70 | note Compute improvement delta in basis points: positive when cand < base, negative when cand > base. |
| 71 | fn ev_compute_delta_bps base_v ulong cand_v ulong to long |
| 72 | if ge base_v cand_v |
| 73 | diff = sub base_v cand_v |
| 74 | if gt diff EV_MAX_SAFE_METRIC |
| 75 | ret long 10000 |
| 76 | end |
| 77 | ret long div mul diff EV_BPS_SCALE base_v |
| 78 | else |
| 79 | diff = sub cand_v base_v |
| 80 | if gt diff EV_MAX_SAFE_METRIC |
| 81 | ret long -100000 |
| 82 | end |
| 83 | mag = div mul diff EV_BPS_SCALE base_v |
| 84 | if gt mag 100000 |
| 85 | ret long -100000 |
| 86 | end |
| 87 | ret neg long mag |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | fn ev_record v ulong to ulong |
| 92 | put ev_stats 0 add get ev_stats 0 1 |
| 93 | if eq v EV_PASS |
| 94 | put ev_stats 1 add get ev_stats 1 1 |
| 95 | else |
| 96 | put ev_stats 2 add get ev_stats 2 1 |
| 97 | end |
| 98 | ret v |
| 99 | end |
| 100 | |
| 101 | note Evaluate candidate sample across all 6 fail-closed evaluator layers. |
| 102 | fn ev_evaluate s view EvalSample to ulong |
| 103 | if eq get ev_cfg 0 0 |
| 104 | ret ev_record EV_DENY_UNINIT |
| 105 | end |
| 106 | tot = get s tests_total |
| 107 | passed = get s tests_passed |
| 108 | bns = get s base_ns |
| 109 | cns = get s cand_ns |
| 110 | bmem = get s base_mem_kb |
| 111 | cmem = get s cand_mem_kb |
| 112 | if or eq tot 0 gt passed tot |
| 113 | ret ev_record EV_DENY_INVALID_SAMPLE |
| 114 | end |
| 115 | if or eq bns 0 eq cns 0 |
| 116 | ret ev_record EV_DENY_INVALID_SAMPLE |
| 117 | end |
| 118 | if or eq bmem 0 eq cmem 0 |
| 119 | ret ev_record EV_DENY_INVALID_SAMPLE |
| 120 | end |
| 121 | note Layer 1: Security (zero critical, bounded high) |
| 122 | if gt get s sec_critical 0 |
| 123 | ret ev_record EV_DENY_SECURITY |
| 124 | end |
| 125 | if gt get s sec_high get ev_cfg 2 |
| 126 | ret ev_record EV_DENY_SECURITY |
| 127 | end |
| 128 | note Layer 2: Correctness floor |
| 129 | corr_bps = div mul passed EV_BPS_SCALE tot |
| 130 | put ev_stats 3 corr_bps |
| 131 | if lt corr_bps get ev_cfg 1 |
| 132 | ret ev_record EV_DENY_CORRECTNESS |
| 133 | end |
| 134 | note Layer 3: Longitudinal replay |
| 135 | if eq get s replay_ok 0 |
| 136 | ret ev_record EV_DENY_REPLAY |
| 137 | end |
| 138 | note Layer 4: Performance regression guard |
| 139 | pdelta = ev_compute_delta_bps bns cns |
| 140 | put ev_deltas 0 pdelta |
| 141 | max_preg_neg = neg long get ev_cfg 3 |
| 142 | if lt pdelta max_preg_neg |
| 143 | ret ev_record EV_DENY_PERF_REGRESS |
| 144 | end |
| 145 | note Layer 5: Resource efficiency regression guard |
| 146 | rdelta = ev_compute_delta_bps bmem cmem |
| 147 | put ev_deltas 1 rdelta |
| 148 | max_rreg_neg = neg long get ev_cfg 4 |
| 149 | if lt rdelta max_rreg_neg |
| 150 | ret ev_record EV_DENY_RES_REGRESS |
| 151 | end |
| 152 | note Layer 6: Style violations bound |
| 153 | if gt get s style_violations get ev_cfg 5 |
| 154 | ret ev_record EV_DENY_STYLE |
| 155 | end |
| 156 | ret ev_record EV_PASS |
| 157 | end |