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_route.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_route.h -- Omega cognitive compute routing (OMEGA_COGNITIVE_ROUTING). |
| 19 | * |
| 20 | * A request states what cognition it needs (CognitiveRequirement). Several |
| 21 | * engines may be able to supply it (CognitiveRealization). The router picks |
| 22 | * the cheapest escalation ladder that the evidence says will meet the |
| 23 | * requirement, runs the cheapest rung, and climbs only when the answer's |
| 24 | * verification or calibrated confidence is not enough. |
| 25 | * |
| 26 | * Nothing here assumes an engine is a language model. An engine is a |
| 27 | * function with a declared class, the operations it supports, its hard |
| 28 | * capacities, and a measured profile. |
| 29 | * |
| 30 | * The measured profiles (quality, uncertainty calibration, cost, latency, |
| 31 | * energy, evidence) are a generation's "model" blob. The router reads them |
| 32 | * only from the active generation of an RxGenStore, through the SHA-256 root |
| 33 | * check. It has no call that installs profiles from memory. Observations go |
| 34 | * into a ledger, the ledger becomes a candidate generation, and the routing |
| 35 | * changes only after that candidate is promoted with the promotion right on |
| 36 | * the native authority. rx_route.o links no rx_gen_promote and no aienos_cap_* |
| 37 | * symbol; the build checks this. |
| 38 | */ |
| 39 | #ifndef RX_ROUTE_H |
| 40 | #define RX_ROUTE_H |
| 41 | |
| 42 | #include "rx_generation.h" |
| 43 | |
| 44 | #include <stddef.h> |
| 45 | #include <stdint.h> |
| 46 | |
| 47 | #define RX_COG_OK 0 |
| 48 | #define RX_COG_ERR_ARG -60 |
| 49 | #define RX_COG_ERR_UNSATISFIABLE -61 /* no ladder is expected to meet the requirement */ |
| 50 | #define RX_COG_ERR_EXHAUSTED -62 /* the ladder ran out before an answer was accepted */ |
| 51 | #define RX_COG_ERR_MODEL -63 /* the generation's model is missing, torn or foreign */ |
| 52 | #define RX_COG_ERR_FULL -64 |
| 53 | #define RX_COG_ERR_ENGINE -65 |
| 54 | |
| 55 | #define RX_COG_MAX_ENGINES 8u |
| 56 | #define RX_COG_MAX_OPS 4u |
| 57 | #define RX_COG_MAX_LADDER 4u /* rungs in one ladder: at most 3 escalations */ |
| 58 | #define RX_COG_BINS 16u /* confidence bins */ |
| 59 | #define RX_COG_MAX_SAMPLE 2048u /* evidence records kept per operation */ |
| 60 | #define RX_COG_MIN_EVIDENCE 200u /* fewer records: the profile does not qualify anything */ |
| 61 | #define RX_COG_CONF_ONE 1000000u |
| 62 | |
| 63 | /* Operation classes. */ |
| 64 | enum { RX_COG_OP_RECOGNIZE = 0, /* label an observation; no cheap verifier */ |
| 65 | RX_COG_OP_PLAN = 1, /* reach a goal within a bound; answers are checkable */ |
| 66 | RX_COG_OP_COUNT = 2 }; |
| 67 | |
| 68 | /* Realization classes. */ |
| 69 | enum { RX_COG_CLASS_DETERMINISTIC = 1, /* compiled procedure */ |
| 70 | RX_COG_CLASS_SMALL_POLICY, /* small learned policy */ |
| 71 | RX_COG_CLASS_NEURAL_MODULE, /* specialized neural module */ |
| 72 | RX_COG_CLASS_GENERAL, /* general cognition */ |
| 73 | RX_COG_CLASS_ENSEMBLE, |
| 74 | RX_COG_CLASS_JSPACE }; /* exhaustive exploration of the answer space */ |
| 75 | |
| 76 | /* Hardware a realization needs / a requirement allows. */ |
| 77 | enum { RX_COG_HW_CPU_P = 1u, RX_COG_HW_CPU_E = 2u, RX_COG_HW_GPU = 4u }; |
| 78 | |
| 79 | enum { RX_COG_PREC_APPROX = 1, RX_COG_PREC_EXACT = 2 }; |
| 80 | |
| 81 | typedef struct { |
| 82 | uint32_t operation_class; |
| 83 | uint32_t minimum_quality_ppm; /* fraction of answers that must be right, over the class */ |
| 84 | uint32_t uncertainty_budget_ppm; /* an accepted answer's calibrated error may not exceed this */ |
| 85 | uint64_t memory_requirement; /* bytes of memory the cognition must hold */ |
| 86 | uint32_t temporal_requirement; /* steps of history the answer depends on (0 = none) */ |
| 87 | uint32_t planning_depth; /* longest plan the answer may need */ |
| 88 | uint32_t precision_requirement; /* RX_COG_PREC_* */ |
| 89 | uint64_t latency_budget_ns; /* expected time of the whole ladder must fit (admission) */ |
| 90 | uint64_t energy_budget_nj; /* expected energy for the whole ladder (0 = unbounded) */ |
| 91 | uint32_t hardware_constraints; /* RX_COG_HW_* the request may use */ |
| 92 | uint32_t escalation_allowed; /* escalations allowed after the first rung (0 = none) */ |
| 93 | } RxCogRequirement; |
| 94 | |
| 95 | /* What an engine declares about itself. Fixed at registration; part of the |
| 96 | * registry digest a model is bound to. */ |
| 97 | typedef struct { |
| 98 | uint32_t cognitive_engine_id; |
| 99 | uint32_t realization_class; |
| 100 | uint32_t supported_operations; /* bit per RX_COG_OP_* */ |
| 101 | uint32_t hardware_requirements; /* all of these must be allowed */ |
| 102 | uint64_t memory_capacity; |
| 103 | uint32_t temporal_horizon; |
| 104 | uint32_t max_planning_depth; |
| 105 | uint32_t precision; /* RX_COG_PREC_* */ |
| 106 | uint32_t verifiable; /* answers carry a sound check (confidence 0 or 1) */ |
| 107 | } RxCogDeclaration; |
| 108 | |
| 109 | typedef struct { |
| 110 | uint32_t n[RX_COG_BINS]; /* observations per confidence bin */ |
| 111 | uint32_t correct[RX_COG_BINS]; |
| 112 | } RxCogCalibration; |
| 113 | |
| 114 | typedef struct { |
| 115 | uint32_t observations; |
| 116 | uint32_t correct; |
| 117 | uint32_t failures; /* engine errors */ |
| 118 | } RxCogQuality; |
| 119 | |
| 120 | typedef struct { |
| 121 | uint64_t calls; |
| 122 | uint64_t mean_ns; /* time per call on the calibrating core */ |
| 123 | uint64_t p99_ns; |
| 124 | uint64_t nj_per_call; /* energy per call, from the energy meter; 0 = not measured */ |
| 125 | uint32_t energy_measured; |
| 126 | } RxCogCost; |
| 127 | |
| 128 | /* The routing view of one engine: declaration plus the profile of the |
| 129 | * active generation. `generation` is the id of the generation the profile |
| 130 | * came from (0 = none loaded). */ |
| 131 | typedef struct { |
| 132 | uint32_t cognitive_engine_id; |
| 133 | uint64_t generation; |
| 134 | RxCogDeclaration decl; |
| 135 | RxCogQuality quality_profile[RX_COG_OP_COUNT]; |
| 136 | RxCogCalibration uncertainty_profile[RX_COG_OP_COUNT]; |
| 137 | RxCogCost resource_cost_model[RX_COG_OP_COUNT]; /* latency_model = mean/p99, energy_model = nj */ |
| 138 | uint8_t evidence[32]; /* SHA-256 of the evidence sample this profile came from */ |
| 139 | } RxCogRealization; |
| 140 | |
| 141 | /* An engine answers `input` into `output`. confidence is 0..RX_COG_CONF_ONE. |
| 142 | * A verifiable engine reports 0 or RX_COG_CONF_ONE and only after its check. |
| 143 | * Returns 0, or nonzero on an engine failure. */ |
| 144 | typedef int (*RxCogEngineFn)(void *ctx, uint32_t op, const void *input, void *output, |
| 145 | uint32_t *confidence); |
| 146 | |
| 147 | typedef struct RxCogRouter RxCogRouter; |
| 148 | |
| 149 | /* One ladder: engines in the order they are tried. */ |
| 150 | typedef struct { |
| 151 | uint32_t n; |
| 152 | uint32_t engine[RX_COG_MAX_LADDER]; |
| 153 | uint32_t accept_min_bin[RX_COG_MAX_LADDER]; /* accept a rung's answer from this bin up */ |
| 154 | uint64_t expected_nj; /* 0 when energy is not measured */ |
| 155 | uint64_t expected_ns; |
| 156 | uint32_t expected_quality_ppm; /* on the evidence sample */ |
| 157 | uint32_t quality_lower_ppm; /* Wilson 95% lower bound */ |
| 158 | uint32_t reason; /* RX_COG_WHY_* */ |
| 159 | } RxCogLadder; |
| 160 | |
| 161 | enum { RX_COG_WHY_EVIDENCE = 1, /* cheapest ladder the evidence supports */ |
| 162 | RX_COG_WHY_NO_EVIDENCE }; /* no profile yet: the reference realization alone */ |
| 163 | |
| 164 | typedef struct { |
| 165 | uint32_t rungs_run; |
| 166 | uint32_t engine[RX_COG_MAX_LADDER]; |
| 167 | uint32_t confidence[RX_COG_MAX_LADDER]; |
| 168 | uint64_t ns[RX_COG_MAX_LADDER]; |
| 169 | uint32_t final_engine; |
| 170 | uint32_t escalations; |
| 171 | int accepted; /* 1 accepted, 0 exhausted */ |
| 172 | } RxCogTrace; |
| 173 | |
| 174 | int rx_route_create(RxCogRouter **out); |
| 175 | void rx_route_destroy(RxCogRouter *r); |
| 176 | |
| 177 | /* Register before the first load. `reference` marks the realization used for |
| 178 | * an operation while no evidence exists (at most one per operation). */ |
| 179 | int rx_route_register(RxCogRouter *r, const RxCogDeclaration *decl, RxCogEngineFn fn, void *ctx, |
| 180 | uint32_t reference_ops); |
| 181 | |
| 182 | /* SHA-256 of all declarations; a model is bound to it. */ |
| 183 | void rx_route_registry_digest(const RxCogRouter *r, uint8_t out[32]); |
| 184 | |
| 185 | /* Load the profiles of the active generation. With no committed generation, |
| 186 | * or a generation whose model is empty, the router has no profiles and uses |
| 187 | * each operation's reference realization. A torn or foreign model refuses |
| 188 | * and leaves the loaded profiles as they were. */ |
| 189 | int rx_route_load(RxCogRouter *r, const RxGenStore *store); |
| 190 | uint64_t rx_route_generation(const RxCogRouter *r); |
| 191 | |
| 192 | int rx_route_realization(const RxCogRouter *r, uint32_t engine_id, RxCogRealization *out); |
| 193 | |
| 194 | /* Choose the ladder for a requirement (cached per requirement and |
| 195 | * generation). */ |
| 196 | int rx_route_plan(RxCogRouter *r, const RxCogRequirement *req, RxCogLadder *out); |
| 197 | |
| 198 | /* Choose, then run the ladder. */ |
| 199 | int rx_route_run(RxCogRouter *r, const RxCogRequirement *req, const void *input, void *output, |
| 200 | size_t output_size, RxCogTrace *trace); |
| 201 | |
| 202 | /* The always-most-expensive policy used as the gate baseline: the eligible |
| 203 | * realization with the highest measured cost, alone. */ |
| 204 | int rx_route_most_expensive(RxCogRouter *r, const RxCogRequirement *req, uint32_t *engine_id); |
| 205 | |
| 206 | /* ---- learning ---- */ |
| 207 | |
| 208 | typedef struct RxCogLedger RxCogLedger; |
| 209 | |
| 210 | int rx_route_ledger_create(const RxCogRouter *r, RxCogLedger **out); |
| 211 | void rx_route_ledger_destroy(RxCogLedger *l); |
| 212 | |
| 213 | /* One labelled trial: every listed engine ran on the same request. |
| 214 | * conf/correct/ns/failed are indexed like engines[]. */ |
| 215 | int rx_route_observe(RxCogLedger *l, uint32_t op, uint32_t n, const uint32_t *engines, |
| 216 | const uint32_t *conf, const uint8_t *correct, const uint64_t *ns, |
| 217 | const uint8_t *failed); |
| 218 | /* Batch cost of `calls` calls of one engine on one operation: processor time, |
| 219 | * and metered energy when energy_measured. The mean cost in the model comes |
| 220 | * from batches when there are any (no clock read per call). */ |
| 221 | int rx_route_observe_batch(RxCogLedger *l, uint32_t engine_id, uint32_t op, uint64_t calls, |
| 222 | uint64_t cpu_ns, int energy_measured, uint64_t nj); |
| 223 | |
| 224 | /* Serialize the ledger into a model blob (caller frees *out). */ |
| 225 | int rx_route_ledger_model(const RxCogLedger *l, uint8_t **out, size_t *out_len); |
| 226 | |
| 227 | /* Propose the ledger as a candidate generation. It is not active, and the |
| 228 | * router does not change, until someone holding the promotion right promotes |
| 229 | * it. */ |
| 230 | int rx_route_propose(const RxCogLedger *l, RxGenStore *store, uint32_t proposer, |
| 231 | uint64_t authority_epoch, uint64_t authority_generation, uint64_t *candidate); |
| 232 | |
| 233 | #endif |