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_skillroute.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 | /* |
| 18 | * rx_skillroute.c -- Skill Router. See rx_skillroute.h. |
| 19 | */ |
| 20 | #include "rx_skillroute.h" |
| 21 | #include "omega_native.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | int sr_register_skill(CqCatalog *graph, const SrSkill *skill, const CqEntry *provides, uint32_t n) { |
| 26 | if (!graph || !skill || (n && !provides) || skill->skill_id == 0 || skill->machine == 0) |
| 27 | return SR_E_ARG; |
| 28 | for (uint32_t i = 0; i < n; i++) { |
| 29 | CqEntry e = provides[i]; |
| 30 | e.skill_id = skill->skill_id; |
| 31 | e.skill_version = skill->version; |
| 32 | memcpy(e.skill_digest, skill->digest, sizeof e.skill_digest); |
| 33 | e.machine_id = skill->machine; |
| 34 | e.source = skill->machine == graph->self_machine ? CQ_SRC_SKILL : CQ_SRC_FABRIC; |
| 35 | int rc = cq_register(graph, &e, NULL, 0); |
| 36 | if (rc != CQ_OK) return rc; |
| 37 | } |
| 38 | return SR_OK; |
| 39 | } |
| 40 | |
| 41 | uint32_t sr_skill_capabilities(const CqCatalog *graph, uint32_t skill_id, uint32_t version, |
| 42 | CqKey *out, uint32_t max) { |
| 43 | uint32_t n = 0; |
| 44 | if (!graph || skill_id == 0) return 0; |
| 45 | for (uint32_t i = 0; i < graph->n; i++) { |
| 46 | const CqEntry *e = &graph->e[i]; |
| 47 | if (e->skill_id != skill_id || e->skill_version != version || e->live == CQ_LIVE_WITHDRAWN) |
| 48 | continue; |
| 49 | if (out && n < max) out[n] = cq_key_of(e); |
| 50 | n++; |
| 51 | } |
| 52 | return n; |
| 53 | } |
| 54 | |
| 55 | typedef struct { |
| 56 | const SrRouter *r; |
| 57 | const SrRequirement *req; |
| 58 | SrRoute *out; |
| 59 | uint32_t pin_index; /* canonical pin resolved to a graph index (0 = none) */ |
| 60 | } AdmitCtx; |
| 61 | |
| 62 | static const AgSkill *executable(const AgSkillTable *t, uint32_t id) { |
| 63 | for (uint32_t i = 0; t && i < t->n; i++) |
| 64 | if (t->skill[i].id == id) return &t->skill[i]; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | static int digest_zero(const uint8_t *d) { |
| 69 | uint64_t res = 1; |
| 70 | if (omega2_rx_sr_digest_zero(&res, d, 32) != OMEGA_OK) return 1; |
| 71 | return res != 0; |
| 72 | } |
| 73 | |
| 74 | /* Admissible target: a Skill; matching the requirement's pins (version, |
| 75 | * digest, machine); on a machine with a canonical identity when the graph is |
| 76 | * canonical; locally, one we can run, byte-identical to what the graph |
| 77 | * advertises; remotely, allowed by the requirement (the lease was already |
| 78 | * checked by the query). */ |
| 79 | static int admit(void *ctx, const CqEntry *e, const CqCandidate *c) { |
| 80 | AdmitCtx *a = ctx; |
| 81 | const SrRequirement *q = a->req; |
| 82 | if (e->skill_id == 0) { a->out->rejected.not_a_skill++; return 0; } |
| 83 | if ((q->pin_skill_version && e->skill_version != q->pin_skill_version) || |
| 84 | (!digest_zero(q->pin_skill_digest) && memcmp(e->skill_digest, q->pin_skill_digest, 32) != 0)) { |
| 85 | a->out->rejected.pin_mismatch++; |
| 86 | return 0; |
| 87 | } |
| 88 | if (a->pin_index && e->machine_id != a->pin_index) { a->out->rejected.machine_mismatch++; return 0; } |
| 89 | if (a->r->graph->machines) { |
| 90 | AienMachineId m; |
| 91 | if (cq_machine_identity(a->r->graph, e->machine_id, &m) != CQ_OK) { |
| 92 | a->out->rejected.machine_unknown++; |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | if (!c->local) { |
| 97 | if (q->local_only) { a->out->rejected.remote_refused++; return 0; } |
| 98 | return 1; |
| 99 | } |
| 100 | const AgSkill *s = executable(a->r->skills, e->skill_id); |
| 101 | if (!s) { a->out->rejected.not_executable++; return 0; } |
| 102 | if (!digest_zero(e->skill_digest) && memcmp(e->skill_digest, s->identity, 32) != 0) { |
| 103 | a->out->rejected.digest_mismatch++; |
| 104 | return 0; |
| 105 | } |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static uint32_t why_of(const SrRoute *o) { |
| 110 | uint64_t counts[10] = { |
| 111 | o->stats.rejected_dead, |
| 112 | o->stats.rejected_unavailable, |
| 113 | o->stats.rejected_authority, |
| 114 | o->rejected.not_a_skill, |
| 115 | o->rejected.not_executable, |
| 116 | o->rejected.digest_mismatch, |
| 117 | o->rejected.remote_refused, |
| 118 | o->rejected.pin_mismatch, |
| 119 | o->rejected.machine_mismatch, |
| 120 | o->rejected.machine_unknown |
| 121 | }; |
| 122 | uint64_t res = 0; |
| 123 | (void)omega2_rx_sr_why_of(&res, counts, 10); |
| 124 | return (uint32_t)res; |
| 125 | } |
| 126 | |
| 127 | static void fill_target(const SrRouter *r, const CqCandidate *c, SrRoute *x) { |
| 128 | x->chosen = *c; |
| 129 | x->remote = !c->local; |
| 130 | x->key = cq_key_of_candidate(c); |
| 131 | const CqEntry *e = cq_lookup(r->graph, &x->key); |
| 132 | if (e) { |
| 133 | x->skill_version = e->skill_version; |
| 134 | memcpy(x->skill_digest, e->skill_digest, 32); |
| 135 | x->generation = e->generation; |
| 136 | } |
| 137 | x->target_known = cq_machine_identity(r->graph, c->machine_id, &x->target) == CQ_OK; |
| 138 | x->verdict = x->remote ? SR_E_REMOTE : SR_OK; |
| 139 | } |
| 140 | |
| 141 | /* Shared by sr_route (k = 1) and sr_route_alternatives (dominated included). |
| 142 | * Fills out[0..n-1]; returns n (>= 1) or the SR_E_* left in out[0].verdict. */ |
| 143 | static int route_k(const SrRouter *r, const SrRequirement *req, const CqHeld *held, |
| 144 | uint64_t now_us, SrRoute *out, uint32_t max, int alternatives) { |
| 145 | SrRoute *o = &out[0]; |
| 146 | memset(o, 0, sizeof *o); |
| 147 | if (!r || !r->graph || !req) return o->verdict = SR_E_ARG; |
| 148 | AdmitCtx ctx = { r, req, o, 0 }; |
| 149 | if (req->pin_machine_set) { |
| 150 | ctx.pin_index = cq_machine_index(r->graph, &req->pin_machine); |
| 151 | if (ctx.pin_index == 0) return o->verdict = SR_E_MACHINE; |
| 152 | } |
| 153 | CqPlan plan; |
| 154 | o->query_verdict = cq_compile(r->graph, &req->need, &plan); |
| 155 | memcpy(o->plan_digest, plan.digest, sizeof o->plan_digest); |
| 156 | if (o->query_verdict == CQ_E_NO_SOURCE) return o->verdict = SR_E_NO_CANDIDATE; |
| 157 | if (o->query_verdict != CQ_OK) return o->verdict = SR_E_QUERY; |
| 158 | CqTradeoffs t = req->t; |
| 159 | if (alternatives) { |
| 160 | t.k = max < CQ_MAX_K ? max : CQ_MAX_K; |
| 161 | t.include_dominated = 1; |
| 162 | } else { |
| 163 | t.k = 1; /* the router needs the winner only */ |
| 164 | } |
| 165 | CqResult res; |
| 166 | o->query_verdict = cq_query_admit(r->graph, &plan, &req->need, &t, held, now_us, admit, &ctx, |
| 167 | &res, &o->stats); |
| 168 | if (o->query_verdict != CQ_OK) return o->verdict = SR_E_QUERY; |
| 169 | o->n_admissible = res.n_feasible; |
| 170 | if (res.n == 0) { |
| 171 | o->why = why_of(o); |
| 172 | return o->verdict = SR_E_NO_CANDIDATE; |
| 173 | } |
| 174 | uint32_t n = res.n < t.k ? res.n : t.k; |
| 175 | for (uint32_t i = 0; i < n; i++) { |
| 176 | SrRoute *x = &out[i]; |
| 177 | if (i) { |
| 178 | memset(x, 0, sizeof *x); |
| 179 | x->query_verdict = o->query_verdict; |
| 180 | memcpy(x->plan_digest, o->plan_digest, sizeof x->plan_digest); |
| 181 | x->n_admissible = o->n_admissible; |
| 182 | } |
| 183 | fill_target(r, &res.cand[i], x); |
| 184 | } |
| 185 | return (int)n; |
| 186 | } |
| 187 | |
| 188 | int sr_route(const SrRouter *r, const SrRequirement *req, const CqHeld *held, uint64_t now_us, |
| 189 | SrRoute *out) { |
| 190 | if (!out) return SR_E_ARG; |
| 191 | int n = route_k(r, req, held, now_us, out, 1, 0); |
| 192 | return n < 0 ? n : out->verdict; |
| 193 | } |
| 194 | |
| 195 | /* Bind without leaving a half-bound node behind on failure. */ |
| 196 | static int bind_closed(AgGraph *g, uint32_t node, const CqCandidate *c) { |
| 197 | if (node >= g->n_nodes) return SR_E_BIND; |
| 198 | uint32_t op = g->nodes[node].op, n_auth = g->n_auth; |
| 199 | if (cq_bind_skill_node(g, node, c) != CQ_OK) { |
| 200 | g->nodes[node].op = op; |
| 201 | g->n_auth = n_auth; |
| 202 | return SR_E_BIND; |
| 203 | } |
| 204 | return SR_OK; |
| 205 | } |
| 206 | |
| 207 | int sr_bind_node(const SrRouter *r, AgGraph *g, uint32_t node, const SrRequirement *req, |
| 208 | const CqHeld *held, uint64_t now_us, SrRoute *out) { |
| 209 | if (!g) return SR_E_ARG; |
| 210 | int rc = sr_route(r, req, held, now_us, out); |
| 211 | if (rc != SR_OK) return rc; |
| 212 | return out->verdict = bind_closed(g, node, &out->chosen); |
| 213 | } |
| 214 | |
| 215 | /* COMPOSITION-2: up to `max` admissible local-or-remote alternatives, ranked |
| 216 | * like sr_route (dominated providers included, so a costlier fallback is |
| 217 | * still offered). out[0] is what sr_route would choose: the banded order |
| 218 | * keeps a dominator wherever it keeps what it dominates, and the dominator |
| 219 | * sorts first. Returns the number filled (>= 1), or the sr_route error. |
| 220 | * Discovers only; mints nothing. */ |
| 221 | int sr_route_alternatives(const SrRouter *r, const SrRequirement *req, const CqHeld *held, |
| 222 | uint64_t now_us, SrRoute *out, uint32_t max) { |
| 223 | if (!out || max == 0) return SR_E_ARG; |
| 224 | return route_k(r, req, held, now_us, out, max, 1); |
| 225 | } |
| 226 | |
| 227 | /* A machine is live when it is this one or its Fabric lease has not ended. |
| 228 | * Scans the catalog's lease table (keys are unique; no hash assumption). */ |
| 229 | static int lease_live(const CqCatalog *c, uint32_t machine, uint64_t now_us) { |
| 230 | if (machine == c->self_machine) return 1; |
| 231 | if (machine == 0 || !c->mach_key || !c->mach_lease) return 0; |
| 232 | for (uint32_t i = 0; i <= c->mach_mask; i++) |
| 233 | if (c->mach_key[i] == machine) return c->mach_lease[i] > now_us; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int sr_route_check(const SrRouter *r, const SrRoute *route, uint64_t now_us) { |
| 238 | if (!r || !r->graph || !route || (route->verdict != SR_OK && route->verdict != SR_E_REMOTE)) |
| 239 | return SR_E_ARG; |
| 240 | const CqEntry *e = cq_lookup(r->graph, &route->key); |
| 241 | if (!e || e->live == CQ_LIVE_WITHDRAWN) return SR_E_WITHDRAWN; |
| 242 | if (e->live != CQ_LIVE_AVAILABLE) return SR_E_UNAVAILABLE; |
| 243 | if (e->generation != route->generation || e->skill_version != route->skill_version || |
| 244 | memcmp(e->skill_digest, route->skill_digest, 32) != 0 || |
| 245 | e->skill_id != route->chosen.skill_id || |
| 246 | e->auth_resource != route->chosen.required_authority.resource || |
| 247 | e->auth_rights != route->chosen.required_authority.rights) |
| 248 | return SR_E_STALE; |
| 249 | if (r->graph->machines) { |
| 250 | AienMachineId m; |
| 251 | if (cq_machine_identity(r->graph, e->machine_id, &m) != CQ_OK || |
| 252 | (route->target_known && !aien_mid_equal(&m, &route->target))) |
| 253 | return SR_E_MACHINE; |
| 254 | } |
| 255 | if (!lease_live(r->graph, e->machine_id, now_us)) return SR_E_MACHINE; /* lease ended */ |
| 256 | if (route->remote) return SR_E_REMOTE; |
| 257 | if (e->machine_id != r->graph->self_machine) return SR_E_MACHINE; |
| 258 | const AgSkill *s = executable(r->skills, e->skill_id); |
| 259 | if (!s || (!digest_zero(e->skill_digest) && memcmp(e->skill_digest, s->identity, 32) != 0)) |
| 260 | return SR_E_STALE; |
| 261 | return SR_OK; |
| 262 | } |
| 263 | |
| 264 | int sr_bind_route(const SrRouter *r, AgGraph *g, uint32_t node, const SrRoute *route, |
| 265 | uint64_t now_us) { |
| 266 | if (!g) return SR_E_ARG; |
| 267 | int rc = sr_route_check(r, route, now_us); |
| 268 | if (rc != SR_OK) return rc; |
| 269 | return bind_closed(g, node, &route->chosen); |
| 270 | } |