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_omega.h
| 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 | /* |
| 18 | * rx_omega.h -- Omega as a resident realization faculty (ADR 0016 ยง43, R10). |
| 19 | * |
| 20 | * Nothing here is a service. No caller asks Omega for a realization and waits. |
| 21 | * Omega's work is a set of reactions on the shared world: |
| 22 | * |
| 23 | * workload.serve request -> result, demand (production) |
| 24 | * omega.watch demand window -> search (CostEstimate) |
| 25 | * omega.synthesize search -> candidate[k] (RealizationCandidate) |
| 26 | * omega.verify.k candidate[k] -> verdict[k] (V0 + sandboxed differential) |
| 27 | * omega.measure.k verdict[k] -> measure[k] (benchmark against the incumbent) |
| 28 | * omega.select measure[*] -> selection (recorded, eligible) |
| 29 | * |
| 30 | * Production reads `selection` as an ordinary input. It never waits for it. |
| 31 | * The first request of a regime runs the semantic reference; a verified, |
| 32 | * measured realization replaces it only after omega.select publishes one for |
| 33 | * that regime. |
| 34 | * |
| 35 | * The operation is Omega's integer matrix-vector product (M12, omega_matvec): |
| 36 | * y[i] = sum_j A[i*N+j] * x[j], wrapping mod 2^64. Candidates are the native |
| 37 | * AArch64 realizations omega_matvec_synthesize emits. Candidate code lives in a |
| 38 | * content-addressed realization store owned by this faculty; world objects |
| 39 | * carry the 32-byte realization identity, never code or pointers. |
| 40 | * |
| 41 | * Unverified code is only ever run in a forked child. The parent executes a |
| 42 | * realization only after its verdict passed and its bytes still hash to the |
| 43 | * identity the candidate object names. |
| 44 | */ |
| 45 | #ifndef RX_OMEGA_H |
| 46 | #define RX_OMEGA_H |
| 47 | |
| 48 | #include "rx_world.h" |
| 49 | #include "omega_machine.h" |
| 50 | #include "omega_matvec.h" |
| 51 | #include "omega_matvec_quad.h" |
| 52 | |
| 53 | #include <pthread.h> |
| 54 | #include <stdint.h> |
| 55 | |
| 56 | #define RX_OMEGA_SLOTS 5u /* four Omega kinds + one optional test defect */ |
| 57 | #define RX_OMEGA_STORE 16u |
| 58 | #define RX_OMEGA_WINDOW 16u /* calls per published cost window */ |
| 59 | #define RX_OMEGA_MAX_ELEMS (1u << 16) |
| 60 | |
| 61 | /* Object types (RxObject.type). */ |
| 62 | enum { |
| 63 | RX_OT_REQUEST = 0x5210u, RX_OT_DEMAND, RX_OT_RESULT, RX_OT_SEARCH, |
| 64 | RX_OT_CANDIDATE, RX_OT_VERDICT, RX_OT_MEASURE, RX_OT_SELECTION |
| 65 | }; |
| 66 | |
| 67 | /* request: 0 seq, 1 M, 2 N, 3 seed (external) |
| 68 | * result: 0 seq, 1 digest, 2 realization used (0 = reference), 3 ns |
| 69 | * demand: 0 regime calls, 1 regime ns, 2 window seq, 3 window mean ns, |
| 70 | * 4 M, 5 N, 6 realization in use (id word 0), 7 calls all regimes |
| 71 | * search: 0 epoch, 1 M, 2 N, 3 window mean ns, 4 regime calls, 5 regime ns, |
| 72 | * 6 last inquiry sequence taken up (R11) |
| 73 | * candidate: 0 epoch, 1 kind, 2..5 realization id, 6 code bytes, 7 state |
| 74 | * verdict: 0 epoch, 1 state, 2 checks, 3 failures, 4 id word 0, 5 reason |
| 75 | * measure: 0 epoch, 1 state, 2 candidate ps/call, 3 reference ps/call, |
| 76 | * 4..7 realization id |
| 77 | * selection: 0 epoch, 1..4 realization id (zero = reference), 5 ps/call, |
| 78 | * 6 regime, 7 reference ps/call |
| 79 | * regime = (M << 32) | N. */ |
| 80 | enum { RX_OMEGA_SYNTHESIZED = 1 }; |
| 81 | enum { RX_OMEGA_PASSED = 1, RX_OMEGA_REFUSED = 2 }; |
| 82 | enum { RX_OMEGA_MEASURED = 1, RX_OMEGA_DECLINED = 2 }; |
| 83 | |
| 84 | /* Why a verdict refused. */ |
| 85 | enum { |
| 86 | RX_OMEGA_WHY_NONE = 0, |
| 87 | RX_OMEGA_WHY_UNKNOWN, /* the store has no bytes for this identity */ |
| 88 | RX_OMEGA_WHY_IDENTITY, /* the bytes do not hash to the named identity */ |
| 89 | RX_OMEGA_WHY_STRUCTURE, /* Omega V0 structural check failed */ |
| 90 | RX_OMEGA_WHY_DIFFERENTIAL, /* disagreed with the semantic reference */ |
| 91 | RX_OMEGA_WHY_CRASHED, /* the sandboxed child died */ |
| 92 | RX_OMEGA_WHY_HUNG, /* the sandboxed child did not finish */ |
| 93 | RX_OMEGA_WHY_BOUNDS /* wrote outside y or changed A or x */ |
| 94 | }; |
| 95 | |
| 96 | /* Test-only faults. They add or alter a candidate; they never touch the |
| 97 | * verifier, the store lookup or the selection rule. */ |
| 98 | typedef enum { |
| 99 | RX_OMEGA_DEFECT_NONE = 0, |
| 100 | RX_OMEGA_DEFECT_SKIP_REMAINDER, /* slot 4: unroll4 that ignores N % 4 */ |
| 101 | RX_OMEGA_DEFECT_CRASH, /* slot 4: loads from address zero */ |
| 102 | RX_OMEGA_DEFECT_TAMPER /* slot 4: stored bytes differ from the named identity */ |
| 103 | } RxOmegaDefect; |
| 104 | |
| 105 | typedef struct { |
| 106 | SemanticId id; |
| 107 | uint32_t kind; /* 0..2 M12 kinds, 3 quad4, 4 the test defect */ |
| 108 | size_t code_len; |
| 109 | uint8_t code[AARCH64_MAX_CODE_BYTES]; |
| 110 | void *page; /* read+execute mapping of code */ |
| 111 | int verified; /* verdict passed for exactly these bytes */ |
| 112 | uint64_t parent_runs; /* times the parent process executed it */ |
| 113 | } RxOmegaRealization; |
| 114 | |
| 115 | typedef struct { |
| 116 | uint32_t n_slots; |
| 117 | RxOmegaDefect defect; |
| 118 | uint64_t hot_calls; /* both must be reached in one regime */ |
| 119 | uint64_t hot_ns; |
| 120 | uint32_t margin_pct; /* a realization must beat the reference by this */ |
| 121 | uint32_t sandbox_ms; |
| 122 | /* Test only (R15 SEQ parity): when set, the cost of `calls` matvec calls |
| 123 | * replaces the wall-clock time measured around them, in serve and in |
| 124 | * measure. The calls still run. kind is the realization kind, or |
| 125 | * RX_OMEGA_COST_REFERENCE for the reference. NULL measures, as always. */ |
| 126 | uint64_t (*cost_ns)(void *ctx, uint32_t kind, uint32_t M, uint32_t N, uint64_t calls); |
| 127 | void *cost_ctx; |
| 128 | } RxOmegaConfig; |
| 129 | |
| 130 | #define RX_OMEGA_COST_REFERENCE UINT32_MAX |
| 131 | |
| 132 | typedef struct { |
| 133 | RxObjRef request, demand, result, search, selection; |
| 134 | RxObjRef candidate[RX_OMEGA_SLOTS]; |
| 135 | RxObjRef verdict[RX_OMEGA_SLOTS]; |
| 136 | RxObjRef measure[RX_OMEGA_SLOTS]; |
| 137 | } RxOmegaObjects; |
| 138 | |
| 139 | /* Resources: RX_OMEGA_RES_BASE + object index; the caller mints against them. */ |
| 140 | #define RX_OMEGA_RES_BASE 0x5210000ull |
| 141 | enum { |
| 142 | RX_OMEGA_RES_REQUEST = 0, RX_OMEGA_RES_DEMAND, RX_OMEGA_RES_RESULT, RX_OMEGA_RES_SEARCH, |
| 143 | RX_OMEGA_RES_SELECTION, RX_OMEGA_RES_CANDIDATE0, |
| 144 | RX_OMEGA_RES_VERDICT0 = RX_OMEGA_RES_CANDIDATE0 + RX_OMEGA_SLOTS, |
| 145 | RX_OMEGA_RES_MEASURE0 = RX_OMEGA_RES_VERDICT0 + RX_OMEGA_SLOTS, |
| 146 | RX_OMEGA_RES_COUNT = RX_OMEGA_RES_MEASURE0 + RX_OMEGA_SLOTS |
| 147 | }; |
| 148 | |
| 149 | /* Subjects the capabilities are bound to. */ |
| 150 | enum { RX_OMEGA_SUBJ_SERVE = 21, RX_OMEGA_SUBJ_OMEGA = 22 }; |
| 151 | |
| 152 | /* One capability reference per (subject, object resource). Each reaction |
| 153 | * declares READ for what it observes and READ|WRITE for what it publishes; |
| 154 | * the root decides at run time whether the reference still grants that. |
| 155 | * An entry a subject never touches may be left { UINT32_MAX, 0 }. */ |
| 156 | typedef struct { |
| 157 | RxCapRef serve[RX_OMEGA_RES_COUNT]; |
| 158 | RxCapRef omega[RX_OMEGA_RES_COUNT]; |
| 159 | } RxOmegaCaps; |
| 160 | |
| 161 | typedef struct RxOmegaFaculty { |
| 162 | RxWorld *w; |
| 163 | RxOmegaConfig cfg; |
| 164 | RxOmegaObjects o; |
| 165 | OmegaMachineGraph machine; |
| 166 | MatVecSemanticSpec spec; |
| 167 | |
| 168 | pthread_mutex_t mu; /* guards the store and the counters below */ |
| 169 | RxOmegaRealization store[RX_OMEGA_STORE]; |
| 170 | uint32_t n_store; |
| 171 | |
| 172 | uint32_t r_serve, r_watch, r_select, r_reconsider; |
| 173 | RxObjRef inquiry; /* R11: a plan object Omega may react to */ |
| 174 | /* Optional belief barrier for later searches. The first selection |
| 175 | * establishes the incumbent. A later selection must observe AIEN's |
| 176 | * belief about physical experiment evidence for its own search epoch. */ |
| 177 | RxObjRef experiment_evidence; |
| 178 | RxCapRef experiment_evidence_cap; |
| 179 | int require_experiment_evidence; |
| 180 | /* R13. When set, production runs the realization this record names, |
| 181 | * not the selection. The record has the selection layout. Omega's |
| 182 | * selection is then only eligible; what is in force is decided by |
| 183 | * whoever writes this record (in R13: the promotion authority). */ |
| 184 | RxObjRef serve_record; |
| 185 | RxCapRef serve_record_cap; |
| 186 | int serve_from_record; |
| 187 | uint32_t r_synth[RX_OMEGA_SLOTS], r_verify[RX_OMEGA_SLOTS], r_measure[RX_OMEGA_SLOTS]; |
| 188 | struct { struct RxOmegaFaculty *f; uint32_t k; } slot[RX_OMEGA_SLOTS]; |
| 189 | |
| 190 | /* Observability only; no reaction reads these. */ |
| 191 | uint64_t served_reference, served_realized; |
| 192 | uint64_t sandbox_runs, sandbox_crashes; |
| 193 | /* R16 C5: this faculty's caller credentials (rx_caller.h); null in a |
| 194 | * world without bound callers. Held here, never published. */ |
| 195 | const RxCallerKeyring *keys; |
| 196 | } RxOmegaFaculty; |
| 197 | |
| 198 | void rx_omega_default_config(RxOmegaConfig *cfg); |
| 199 | |
| 200 | /* Create the objects. Resources are RX_OMEGA_RES_BASE + index. */ |
| 201 | int rx_omega_create_objects(RxOmegaFaculty *f, RxWorld *w, const RxOmegaConfig *cfg); |
| 202 | |
| 203 | /* Register the reactions with capabilities the caller minted. */ |
| 204 | int rx_omega_register(RxOmegaFaculty *f, const RxOmegaCaps *caps); |
| 205 | |
| 206 | /* Call after create_objects and before register. Belief field 0 is the |
| 207 | * search epoch accepted from the resident physical experiment; field 2 is |
| 208 | * the validity bit set by AIEN's evidence reaction. */ |
| 209 | int rx_omega_require_evidence(RxOmegaFaculty *f, RxObjRef evidence, RxCapRef read_cap); |
| 210 | |
| 211 | /* R13. Call after create_objects and before register. Production reads the |
| 212 | * realization in force from `record` (fields 0 epoch, 1..4 realization id, |
| 213 | * 6 regime) with `read_cap`, held by the serve subject. R10 and R11 never |
| 214 | * call this; production then follows the selection as before. */ |
| 215 | int rx_omega_serve_from(RxOmegaFaculty *f, RxObjRef record, RxCapRef read_cap); |
| 216 | |
| 217 | /* Identity of these bytes as an Omega matvec realization on this machine, |
| 218 | * computed the way the synthesizer names them. 0 on success. */ |
| 219 | int rx_omega_identity_of(const RxOmegaFaculty *f, const uint8_t *code, size_t len, |
| 220 | SemanticId *out); |
| 221 | |
| 222 | void rx_omega_destroy(RxOmegaFaculty *f); |
| 223 | |
| 224 | /* Same function the verifier uses as its oracle. */ |
| 225 | void rx_omega_fill(uint64_t seed, uint64_t *A, uint64_t *x, uint32_t M, uint32_t N); |
| 226 | uint64_t rx_omega_digest(const uint64_t *y, uint32_t M); |
| 227 | |
| 228 | static inline uint64_t rx_omega_regime(uint64_t M, uint64_t N) { return (M << 32) | N; } |
| 229 | |
| 230 | /* R11. Let Omega react to a plan another faculty publishes. Omega needs only |
| 231 | * this layout: field 0 sequence, 1 action (RX_OMEGA_INQ_RESEARCH: search this |
| 232 | * regime again, on whatever cores Omega runs now), 2 regime. Omega reads the |
| 233 | * plan with `read_cap` and cannot write it. A plan that arrives while a |
| 234 | * search is in flight waits for that search's selection. Search field 6 |
| 235 | * records the last plan Omega took up. R10 alone never registers this. */ |
| 236 | enum { RX_OMEGA_INQ_RESEARCH = 1 }; |
| 237 | int rx_omega_register_reconsider(RxOmegaFaculty *f, RxObjRef plan, RxCapRef read_cap, |
| 238 | RxCapRef search_cap, RxCapRef selection_cap); |
| 239 | |
| 240 | /* R14. Admit durable bytes after a restart: store them under the identity |
| 241 | * they hash to, then run the same verifier omega.verify.k runs (identity, V0 |
| 242 | * structure, sandboxed differential on the regime's shape). 0 when verified; |
| 243 | * production may then execute them. 1 with *out_why when refused; -1 when the |
| 244 | * check could not run. It does not select or put anything in force. */ |
| 245 | int rx_omega_readmit(RxOmegaFaculty *f, const uint8_t *code, size_t len, uint32_t kind, |
| 246 | uint64_t regime, SemanticId *out_id, uint32_t *out_why); |
| 247 | |
| 248 | /* Read-only store lookup for tests. Returns a copy; 0 if found. */ |
| 249 | int rx_omega_store_find(RxOmegaFaculty *f, uint64_t id_word0, RxOmegaRealization *out); |
| 250 | |
| 251 | #endif /* RX_OMEGA_H */ |