omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / gpu / rx / regulate.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gpu/rx/regulate.om: pure-Omega ADAPTIVE-M4 bounded resource regulation (replaces src/runtime/rx_regulate.{h,c}). |
| 3 | note Hysteresis + dwell regime state machine, authority-independent envelope enforcement, |
| 4 | note fairness preservation, idempotent condition seq, and emergency-stop refusal. |
| 5 | note Compose: si/directives/om2.om gpu/rx/regulate.om gpu/rx/skillroute.om gpu/rx/test.om |
| 6 | |
| 7 | const RX_OK int 0 |
| 8 | const RX_NOOP int 1 |
| 9 | const RX_ERR_ARG int -1 |
| 10 | const RX_ERR_BOUNDS int -2 |
| 11 | const RX_ERR_HALTED int -3 |
| 12 | |
| 13 | const RX_REG_NOMINAL uint 0 |
| 14 | const RX_REG_EXPAND uint 1 |
| 15 | const RX_REG_PROTECT uint 2 |
| 16 | |
| 17 | const SR_OK int 0 |
| 18 | const SR_E_ARG int -40 |
| 19 | const SR_E_NO_CANDIDATE int -41 |
| 20 | const SR_E_REMOTE int -42 |
| 21 | const SR_E_STALE int -45 |
| 22 | const SR_E_WITHDRAWN int -46 |
| 23 | const SR_E_UNAVAILABLE int -47 |
| 24 | const SR_E_MACHINE int -48 |
| 25 | |
| 26 | const SR_WHY_GONE uint 0x001 |
| 27 | const SR_WHY_UNAVAILABLE uint 0x002 |
| 28 | const SR_WHY_AUTHORITY uint 0x004 |
| 29 | const SR_WHY_NOT_SKILL uint 0x008 |
| 30 | const SR_WHY_NOT_EXECUTABLE uint 0x010 |
| 31 | const SR_WHY_DIGEST uint 0x020 |
| 32 | const SR_WHY_REMOTE_REFUSED uint 0x040 |
| 33 | const SR_WHY_PIN uint 0x080 |
| 34 | const SR_WHY_MACHINE uint 0x100 |
| 35 | const SR_WHY_MACHINE_UNKNOWN uint 0x200 |
| 36 | |
| 37 | note All structs for gpu/rx/ declared before first fn (required by omc). |
| 38 | struct RxBudget slots uint mem ulong energy ulong starv ulong loc uint end |
| 39 | struct RxEnv smin uint smax uint mmin ulong mmax ulong emin ulong emax ulong chi ulong clo ulong phi ulong plo ulong dwell ulong end |
| 40 | struct RxPolicy ver ulong slots ulong mem ulong energy ulong regime ulong cseq ulong dwell ulong flips ulong end |
| 41 | struct RxRegState app_ver ulong refused ulong halted int end |
| 42 | struct SrReq op uint req_held int local_only int pin_ver uint pin_dlo ulong pin_dhi ulong pin_mset int pin_mid uint prio_cost int end |
| 43 | struct SrRoute verdict int entry_idx int skill_id uint ver uint machine_id uint remote int gen ulong why uint n_adm uint end |
| 44 | |
| 45 | note ---- rx_regulate envelope & decision ---- |
| 46 | |
| 47 | fn rx_regulate_envelope_ok e view RxEnv base view RxBudget to int |
| 48 | if or eq get e smin 0 gt get e smin get e smax |
| 49 | ret RX_ERR_ARG |
| 50 | end |
| 51 | if or gt get e mmin get e mmax gt get e emin get e emax |
| 52 | ret RX_ERR_ARG |
| 53 | end |
| 54 | if or lt get base slots get e smin gt get base slots get e smax |
| 55 | ret RX_ERR_ARG |
| 56 | end |
| 57 | if or lt get base mem get e mmin gt get base mem get e mmax |
| 58 | ret RX_ERR_ARG |
| 59 | end |
| 60 | if or lt get base energy get e emin gt get base energy get e emax |
| 61 | ret RX_ERR_ARG |
| 62 | end |
| 63 | if ge get e clo get e chi |
| 64 | ret RX_ERR_ARG |
| 65 | end |
| 66 | if or ge get e plo get e phi gt get e phi 100 |
| 67 | ret RX_ERR_ARG |
| 68 | end |
| 69 | if eq get e dwell 0 |
| 70 | ret RX_ERR_ARG |
| 71 | end |
| 72 | ret RX_OK |
| 73 | end |
| 74 | |
| 75 | fn rx_regulate_policy_for e view RxEnv base view RxBudget regime uint dst edit RxBudget to void |
| 76 | put dst slots get base slots |
| 77 | put dst mem get base mem |
| 78 | put dst energy get base energy |
| 79 | put dst starv get base starv |
| 80 | put dst loc get base loc |
| 81 | if eq regime RX_REG_EXPAND |
| 82 | put dst slots get e smax |
| 83 | put dst mem get e mmax |
| 84 | put dst energy get e emax |
| 85 | else |
| 86 | if eq regime RX_REG_PROTECT |
| 87 | put dst slots get e smin |
| 88 | put dst mem get e mmin |
| 89 | put dst energy get e emin |
| 90 | end |
| 91 | end |
| 92 | ret |
| 93 | end |
| 94 | |
| 95 | fn rx_regulate_decide e view RxEnv regime uint cur_dwell ulong contention ulong pressure ulong to uint |
| 96 | settled = ge cur_dwell get e dwell |
| 97 | note Protection enters immediately and takes precedence over expansion. |
| 98 | if ge pressure get e phi |
| 99 | ret RX_REG_PROTECT |
| 100 | end |
| 101 | if eq regime RX_REG_PROTECT |
| 102 | if or eq settled false gt pressure get e plo |
| 103 | ret RX_REG_PROTECT |
| 104 | end |
| 105 | if ge contention get e chi |
| 106 | ret RX_REG_EXPAND |
| 107 | end |
| 108 | ret RX_REG_NOMINAL |
| 109 | end |
| 110 | if eq regime RX_REG_EXPAND |
| 111 | if or eq settled false gt contention get e clo |
| 112 | ret RX_REG_EXPAND |
| 113 | end |
| 114 | ret RX_REG_NOMINAL |
| 115 | end |
| 116 | if ge contention get e chi |
| 117 | ret RX_REG_EXPAND |
| 118 | end |
| 119 | ret RX_REG_NOMINAL |
| 120 | end |
| 121 | |
| 122 | note Reaction step: decides next regime from condition (cseq, contention, pressure). |
| 123 | note Same cseq is an idempotent no-op (returns RX_NOOP). |
| 124 | fn rx_regulate_step e view RxEnv base view RxBudget pol edit RxPolicy cseq ulong contention ulong pressure ulong to int |
| 125 | if eq cseq get pol cseq |
| 126 | ret RX_NOOP |
| 127 | end |
| 128 | reg = uint get pol regime |
| 129 | dw = get pol dwell |
| 130 | nxt = rx_regulate_decide e reg dw contention pressure |
| 131 | put pol cseq cseq |
| 132 | if eq nxt reg |
| 133 | put pol dwell add dw 1 |
| 134 | ret RX_OK |
| 135 | end |
| 136 | b = new RxBudget 0 0 0 0 0 |
| 137 | rx_regulate_policy_for e base nxt edit b |
| 138 | put pol ver add get pol ver 1 |
| 139 | put pol slots ulong get b slots |
| 140 | put pol mem get b mem |
| 141 | put pol energy get b energy |
| 142 | put pol regime ulong nxt |
| 143 | put pol dwell ulong 0 |
| 144 | put pol flips add get pol flips 1 |
| 145 | ret RX_OK |
| 146 | end |
| 147 | |
| 148 | fn rx_policy_in_envelope e view RxEnv pol view RxPolicy to bool |
| 149 | if gt get pol slots ulong 0xffffffff |
| 150 | ret false |
| 151 | end |
| 152 | sl = uint get pol slots |
| 153 | if or lt sl get e smin gt sl get e smax |
| 154 | ret false |
| 155 | end |
| 156 | if or lt get pol mem get e mmin gt get pol mem get e mmax |
| 157 | ret false |
| 158 | end |
| 159 | if or lt get pol energy get e emin gt get pol energy get e emax |
| 160 | ret false |
| 161 | end |
| 162 | if gt get pol regime ulong RX_REG_PROTECT |
| 163 | ret false |
| 164 | end |
| 165 | ret true |
| 166 | end |
| 167 | |
| 168 | note Host step: installs policy into applied budget iff not halted, version is new, and within envelope. |
| 169 | note Fairness fields (starv, loc) always come from base budget and are never regulated away. |
| 170 | fn rx_regulate_apply e view RxEnv base view RxBudget pol view RxPolicy st edit RxRegState app edit RxBudget to int |
| 171 | if ne get st halted 0 |
| 172 | put st refused add get st refused 1 |
| 173 | ret RX_ERR_HALTED |
| 174 | end |
| 175 | if eq get pol ver get st app_ver |
| 176 | ret RX_NOOP |
| 177 | end |
| 178 | if eq rx_policy_in_envelope e pol false |
| 179 | put st refused add get st refused 1 |
| 180 | ret RX_ERR_BOUNDS |
| 181 | end |
| 182 | put app slots uint get pol slots |
| 183 | put app mem get pol mem |
| 184 | put app energy get pol energy |
| 185 | put app starv get base starv |
| 186 | put app loc get base loc |
| 187 | put st app_ver get pol ver |
| 188 | ret RX_OK |
| 189 | end |