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 / src / runtime / rx_regulate.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* ADAPTIVE-M4: bounded resource regulation. Contract in rx_regulate.h. |
| 18 | * Core decision, envelope validation, policy derivation, bounds check, |
| 19 | * and step transition execute in pure OM-2 (om/runtime/rx_regulate.om). */ |
| 20 | #include "runtime/rx_regulate.h" |
| 21 | #include "omega_native.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | static void pack_env(const RxRegulateEnvelope *e, uint64_t out[16]) { |
| 26 | memset(out, 0, sizeof(uint64_t) * 16); |
| 27 | out[0] = e->slots_min; |
| 28 | out[1] = e->slots_max; |
| 29 | out[2] = e->memory_min; |
| 30 | out[3] = e->memory_max; |
| 31 | out[4] = e->energy_min; |
| 32 | out[5] = e->energy_max; |
| 33 | out[6] = e->base.slots; |
| 34 | out[7] = e->base.memory_bytes; |
| 35 | out[8] = e->base.energy_budget; |
| 36 | out[9] = e->contention_hi; |
| 37 | out[10] = e->contention_lo; |
| 38 | out[11] = e->pressure_hi; |
| 39 | out[12] = e->pressure_lo; |
| 40 | out[13] = e->min_dwell; |
| 41 | } |
| 42 | |
| 43 | static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) { |
| 44 | for (uint32_t i = 0; i < c->n_in; i++) |
| 45 | if (c->in[i].obj.id == r.id) return &c->in[i]; |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | static void put(RxCtx *c, RxObjRef o, uint32_t field, uint64_t v) { |
| 50 | c->out[c->n_out++] = (RxMutation){ o, field, v }; |
| 51 | } |
| 52 | |
| 53 | int rx_regulate_envelope_ok(const RxRegulateEnvelope *e) { |
| 54 | if (!e) return RX_ERR_ARG; |
| 55 | uint64_t env[16], res = 0; |
| 56 | pack_env(e, env); |
| 57 | if (omega2_rx_reg_envelope_ok(&res, env, 16) != OMEGA_OK) return RX_ERR_ARG; |
| 58 | return (int)(int64_t)res; |
| 59 | } |
| 60 | |
| 61 | void rx_regulate_policy_for(const RxRegulateEnvelope *e, uint32_t regime, RxResourceBudget *out) { |
| 62 | *out = e->base; |
| 63 | uint64_t env[16], dst[4] = { 0 }, res = 0; |
| 64 | pack_env(e, env); |
| 65 | if (omega2_rx_reg_policy_for(&res, env, 16, (uint64_t)regime, dst, 4) == OMEGA_OK) { |
| 66 | out->slots = (uint32_t)dst[0]; |
| 67 | out->memory_bytes = dst[1]; |
| 68 | out->energy_budget = dst[2]; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | uint32_t rx_regulate_decide(const RxRegulateEnvelope *e, uint32_t regime, uint64_t dwell, |
| 73 | uint64_t contention, uint64_t pressure) { |
| 74 | uint64_t env[16], res = RX_REG_NOMINAL; |
| 75 | pack_env(e, env); |
| 76 | if (omega2_rx_reg_decide(&res, env, 16, (uint64_t)regime, dwell, contention, pressure) != OMEGA_OK) |
| 77 | return RX_REG_PROTECT; |
| 78 | return (uint32_t)res; |
| 79 | } |
| 80 | |
| 81 | /* Policy written only here. A condition sample already decided on (same seq) |
| 82 | * publishes nothing, so a re-run of the same activation is a no-op. */ |
| 83 | static int fn_regulate(RxCtx *c) { |
| 84 | RxRegulator *r = c->user; |
| 85 | const RxSnapshotDep *cond = in_of(c, r->condition); |
| 86 | const RxSnapshotDep *pol = in_of(c, r->policy); |
| 87 | if (!cond || !pol) return RX_ERR_ARG; |
| 88 | uint64_t env[16], dst[8] = { 0 }, act = 0; |
| 89 | pack_env(&r->env, env); |
| 90 | if (omega2_rx_reg_step(&act, env, 16, cond->field, 8, pol->field, 8, dst, 8) != OMEGA_OK) |
| 91 | return RX_ERR_ARG; |
| 92 | if (act == 0) return 0; |
| 93 | put(c, r->policy, 5, dst[5]); |
| 94 | if (act == 1) { |
| 95 | put(c, r->policy, 6, dst[6]); |
| 96 | return 0; |
| 97 | } |
| 98 | put(c, r->policy, 0, dst[0]); |
| 99 | put(c, r->policy, 1, dst[1]); |
| 100 | put(c, r->policy, 2, dst[2]); |
| 101 | put(c, r->policy, 3, dst[3]); |
| 102 | put(c, r->policy, 4, dst[4]); |
| 103 | put(c, r->policy, 6, dst[6]); |
| 104 | put(c, r->policy, 7, dst[7]); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int rx_regulate_create_objects(RxRegulator *r, RxWorld *w, const RxRegulateEnvelope *e) { |
| 109 | if (!r || !w) return RX_ERR_ARG; |
| 110 | int rc = rx_regulate_envelope_ok(e); |
| 111 | if (rc != RX_OK) return rc; |
| 112 | memset(r, 0, sizeof(*r)); |
| 113 | r->w = w; |
| 114 | r->env = *e; |
| 115 | uint64_t z[RX_MAX_FIELDS] = { 0 }; |
| 116 | rc = rx_world_create(w, RX_OT_REG_CONDITION, RX_PERSIST_RESIDENT, |
| 117 | RX_REG_RES_BASE + RX_REG_RES_CONDITION, z, &r->condition); |
| 118 | if (rc != RX_OK) return rc; |
| 119 | uint64_t p[RX_MAX_FIELDS] = { 1, e->base.slots, e->base.memory_bytes, e->base.energy_budget, |
| 120 | RX_REG_NOMINAL, 0, 0, 0 }; |
| 121 | return rx_world_create(w, RX_OT_REG_POLICY, RX_PERSIST_RESIDENT, |
| 122 | RX_REG_RES_BASE + RX_REG_RES_POLICY, p, &r->policy); |
| 123 | } |
| 124 | |
| 125 | int rx_regulate_register(RxRegulator *r, const RxRegulateCaps *caps, const RxCallerCred *cred) { |
| 126 | if (!r || !r->w || !caps) return RX_ERR_ARG; |
| 127 | RxReactionDesc d; |
| 128 | memset(&d, 0, sizeof d); |
| 129 | d.name = "regulate.budget"; |
| 130 | d.faculty = RX_FACULTY_AIEN; |
| 131 | d.subject = RX_REG_SUBJ; |
| 132 | d.priority = RX_PRIO_CRITICAL; |
| 133 | d.fn = fn_regulate; |
| 134 | d.user = r; |
| 135 | if (cred) d.caller = *cred; |
| 136 | d.n_triggers = 1; |
| 137 | d.triggers[0] = (RxDep){ r->condition, RX_FIELD(0) }; |
| 138 | d.n_reads = 2; |
| 139 | d.reads[0] = (RxDep){ r->condition, RX_ALL_FIELDS }; |
| 140 | d.reads[1] = (RxDep){ r->policy, RX_ALL_FIELDS }; |
| 141 | d.n_writes = 1; |
| 142 | d.writes[0] = (RxDep){ r->policy, RX_ALL_FIELDS }; |
| 143 | d.n_caps = 2; |
| 144 | d.caps[0] = (RxCapNeed){ caps->condition_read, RX_REG_RES_BASE + RX_REG_RES_CONDITION, |
| 145 | RX_RIGHT_READ }; |
| 146 | d.caps[1] = (RxCapNeed){ caps->policy_rw, RX_REG_RES_BASE + RX_REG_RES_POLICY, |
| 147 | RX_RIGHT_READ | RX_RIGHT_WRITE }; |
| 148 | return rx_world_add_reaction(r->w, &d, &r->rid); |
| 149 | } |
| 150 | |
| 151 | int rx_regulate_apply(RxRegulator *r) { |
| 152 | if (!r || !r->w) return RX_ERR_ARG; |
| 153 | RxHaltStatus h; |
| 154 | rx_world_halt_status(r->w, &h); |
| 155 | if (h.halted) { r->refused++; return RX_ERR_HALTED; } |
| 156 | RxObject p; |
| 157 | int rc = rx_world_read(r->w, r->policy, &p); |
| 158 | if (rc != RX_OK) return rc; |
| 159 | if (p.field[0] == r->applied_version) return 1; |
| 160 | const RxRegulateEnvelope *e = &r->env; |
| 161 | uint64_t env[16], chk = 0; |
| 162 | pack_env(e, env); |
| 163 | if (omega2_rx_reg_apply_check(&chk, env, 16, p.field, 8) != OMEGA_OK || |
| 164 | (int64_t)chk != RX_OK) { |
| 165 | r->refused++; |
| 166 | return RX_ERR_BOUNDS; |
| 167 | } |
| 168 | RxResourceBudget b = e->base; /* fairness and offers: never regulated */ |
| 169 | b.slots = (uint32_t)p.field[1]; |
| 170 | b.memory_bytes = p.field[2]; |
| 171 | b.energy_budget = p.field[3]; |
| 172 | rx_world_set_resources(r->w, &b); |
| 173 | r->applied = b; |
| 174 | r->applied_version = p.field[0]; |
| 175 | return RX_OK; |
| 176 | } |