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_fusion.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_fusion.c -- Omega verified workflow fusion: observe verified runs, |
| 19 | * mine repeated fragments, propose MetaSkills, verify them against the |
| 20 | * original fragment, gate them through measurement, canary and the |
| 21 | * generation barrier, and apply only published ones. |
| 22 | * |
| 23 | * Fragment shape, identity and the step program are defined in rx_graph.c |
| 24 | * (rx_graph_meta_seal, rx_graph_meta_run); this file decides which fragments |
| 25 | * qualify and moves a MetaSkill through its states. |
| 26 | */ |
| 27 | #include "rx_fusion.h" |
| 28 | |
| 29 | #include "omega_core.h" |
| 30 | #include "omega_native.h" |
| 31 | #include "sha256.h" |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | /* ---- small helpers ---- */ |
| 36 | |
| 37 | static void put8(sha256_ctx *c, uint8_t v) { sha256_update(c, &v, 1); } |
| 38 | |
| 39 | static void put32(sha256_ctx *c, uint32_t v) { |
| 40 | uint8_t b[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; |
| 41 | sha256_update(c, b, 4); |
| 42 | } |
| 43 | |
| 44 | static void put64(sha256_ctx *c, uint64_t v) { |
| 45 | put32(c, (uint32_t)(v >> 32)); |
| 46 | put32(c, (uint32_t)v); |
| 47 | } |
| 48 | |
| 49 | static uint64_t word64(const uint8_t *d) { |
| 50 | uint64_t res = 0; |
| 51 | (void)omega2_rx_fus_word64(&res, d, 8); |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | static int bit(uint64_t set, uint32_t n) { return (int)((set >> n) & 1u); } |
| 56 | |
| 57 | static int eligible_kind(AgKind k) { |
| 58 | uint64_t res = 0; |
| 59 | if (omega2_rx_fus_eligible_kind(&res, (uint64_t)k) != OMEGA_OK) return 0; |
| 60 | return res != 0; |
| 61 | } |
| 62 | |
| 63 | static int step_reads(AgKind k) { return k == AG_WORLD_READ || k == AG_RECALL; } |
| 64 | |
| 65 | static int boundary_kind(AgKind k) { return k == AG_WORLD_PUBLISH || k == AG_EFFECT_PERFORM; } |
| 66 | |
| 67 | static int pinned(const AgGraph *g, uint32_t n) { |
| 68 | for (uint32_t i = 0; i < g->n_evidence; i++) |
| 69 | if (g->evidence[i] == n) return 1; |
| 70 | for (uint32_t i = 0; i < g->n_success; i++) |
| 71 | if (g->success[i].node == n) return 1; |
| 72 | for (uint32_t i = 0; i < g->n_failure; i++) |
| 73 | if (g->failure[i].node == n) return 1; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static uint32_t inputs_by_port(const AgGraph *g, uint32_t n, uint32_t out[AG_MAX_IN]) { |
| 78 | uint32_t k = 0; |
| 79 | for (uint32_t p = 0; p < AG_MAX_IN; p++) |
| 80 | for (uint32_t e = 0; e < g->n_data; e++) |
| 81 | if (g->data[e].to == n && g->data[e].port == p && k < AG_MAX_IN) out[k++] = e; |
| 82 | return k; |
| 83 | } |
| 84 | |
| 85 | const char *rx_fusion_state_name(uint32_t s) { |
| 86 | static const char *n[] = { "?", "candidate", "verified", "rejected", "measured", "slower", |
| 87 | "canary_passed", "quarantined", "promoted", "published" }; |
| 88 | return s < sizeof n / sizeof n[0] ? n[s] : "?"; |
| 89 | } |
| 90 | |
| 91 | const char *rx_fusion_reason_name(uint32_t r) { |
| 92 | static const char *n[] = { "ok", "identity", "contract", "authority", "effect", "resource", |
| 93 | "not_runnable", "semantic", "failure_behaviour", "evidence" }; |
| 94 | return r < sizeof n / sizeof n[0] ? n[r] : "?"; |
| 95 | } |
| 96 | |
| 97 | /* ---- fragments: extraction ---- */ |
| 98 | |
| 99 | enum { X_OK = 0, X_NO = -1, X_BUDGET = -2 }; |
| 100 | |
| 101 | typedef struct { |
| 102 | const AgGraph *g; |
| 103 | uint64_t set; |
| 104 | uint64_t visited; |
| 105 | uint32_t n; |
| 106 | uint16_t order[AG_META_MAX_STEPS]; |
| 107 | } Walk; |
| 108 | |
| 109 | /* Canonical step order: post-order from the exit over data inputs by port. */ |
| 110 | static void visit(Walk *wk, uint32_t n) { |
| 111 | if (bit(wk->visited, n)) return; |
| 112 | wk->visited |= 1ull << n; |
| 113 | uint32_t e[AG_MAX_IN]; |
| 114 | uint32_t k = inputs_by_port(wk->g, n, e); |
| 115 | for (uint32_t i = 0; i < k; i++) { |
| 116 | uint32_t s = wk->g->data[e[i]].from; |
| 117 | if (bit(wk->set, s)) visit(wk, s); |
| 118 | } |
| 119 | if (wk->n < AG_META_MAX_STEPS) wk->order[wk->n] = (uint16_t)n; |
| 120 | wk->n++; |
| 121 | } |
| 122 | |
| 123 | static int extract(const AgGraph *g, const RxWorld *w, uint64_t S, uint32_t exit, AgMetaProgram *p, |
| 124 | AgFusionSite *site) { |
| 125 | memset(p, 0, sizeof *p); |
| 126 | memset(site, 0, sizeof *site); |
| 127 | if (exit >= g->n_nodes || !bit(S, exit)) return X_NO; |
| 128 | for (uint32_t n = 0; n < g->n_nodes; n++) { |
| 129 | if (!bit(S, n)) continue; |
| 130 | const AgNode *x = &g->nodes[n]; |
| 131 | if (!x->alive || !eligible_kind(x->kind)) return X_NO; |
| 132 | if (n != exit && (pinned(g, n) || x->kind == AG_VERIFY)) return X_NO; |
| 133 | } |
| 134 | for (uint32_t d = 0; d < g->n_deps; d++) { |
| 135 | if (bit(S, g->deps[d].to)) return X_NO; /* control into a step */ |
| 136 | if (bit(S, g->deps[d].from) && g->deps[d].from != exit) return X_NO; |
| 137 | } |
| 138 | for (uint32_t e = 0; e < g->n_data; e++) { |
| 139 | const AgDataEdge *d = &g->data[e]; |
| 140 | if (bit(S, d->from) && !bit(S, d->to) && d->from != exit) return X_NO; /* second exit */ |
| 141 | if (bit(S, d->from) && bit(S, d->to) && d->mode != AG_EDGE_DATA) return X_NO; |
| 142 | } |
| 143 | Walk wk = { g, S, 0, 0, { 0 } }; |
| 144 | visit(&wk, exit); |
| 145 | if (wk.visited != S || wk.n > AG_META_MAX_STEPS) return X_NO; /* every step feeds the exit */ |
| 146 | if (wk.n < 2) return X_NO; |
| 147 | |
| 148 | int budget = 0; |
| 149 | for (uint32_t j = 0; j < wk.n; j++) { |
| 150 | uint32_t n = wk.order[j]; |
| 151 | AgMetaStep *q = &p->step[j]; |
| 152 | q->node = g->nodes[n]; |
| 153 | /* The step carries its meaning, not its place: no identity, origin, |
| 154 | * parameter or bound object (those are the site's). */ |
| 155 | memset(q->node.id, 0, sizeof q->node.id); |
| 156 | q->node.origin = 0; |
| 157 | q->node.param = 0; |
| 158 | q->node.obj = (RxObjRef){ 0, 0 }; |
| 159 | q->node.alive = 1; |
| 160 | q->obj = -1; |
| 161 | site->node[j] = (uint16_t)n; |
| 162 | if (step_reads(g->nodes[n].kind)) { |
| 163 | RxObjRef o = g->nodes[n].obj; |
| 164 | int slot = -1; |
| 165 | for (uint32_t i = 0; i < p->n_obj; i++) |
| 166 | if (site->obj[i].id == o.id && site->obj[i].generation == o.generation) slot = (int)i; |
| 167 | if (slot < 0) { |
| 168 | if (p->n_obj >= AG_META_MAX_OBJ) { budget = 1; continue; } |
| 169 | slot = (int)p->n_obj++; |
| 170 | site->obj[slot] = o; |
| 171 | } |
| 172 | q->obj = slot; |
| 173 | p->obj_rights[slot] |= RX_RIGHT_READ; |
| 174 | } |
| 175 | uint32_t e[AG_MAX_IN]; |
| 176 | q->n_in = inputs_by_port(g, n, e); |
| 177 | for (uint32_t i = 0; i < q->n_in; i++) { |
| 178 | const AgDataEdge *d = &g->data[e[i]]; |
| 179 | if (bit(S, d->from)) { |
| 180 | uint32_t k = 0; |
| 181 | while (k < j && wk.order[k] != d->from) k++; |
| 182 | if (k >= j) return X_NO; |
| 183 | q->in[i].step = 1; |
| 184 | q->in[i].index = (uint8_t)k; |
| 185 | q->in[i].mode = AG_EDGE_DATA; |
| 186 | continue; |
| 187 | } |
| 188 | int port = -1; |
| 189 | for (uint32_t k = 0; k < site->n_ext; k++) |
| 190 | if (site->ext[k] == d->from) port = (int)k; |
| 191 | if (port >= 0) { |
| 192 | if (p->in_mode[port] != d->mode) return X_NO; /* one source, two meanings */ |
| 193 | } else { |
| 194 | if (site->n_ext >= AG_MAX_IN) { budget = 1; continue; } |
| 195 | port = (int)site->n_ext++; |
| 196 | site->ext[port] = d->from; |
| 197 | p->in_type[port] = g->nodes[d->from].out_type; |
| 198 | p->in_mode[port] = d->mode; |
| 199 | } |
| 200 | q->in[i].step = 0; |
| 201 | q->in[i].index = (uint8_t)port; |
| 202 | q->in[i].mode = d->mode; |
| 203 | } |
| 204 | } |
| 205 | p->n_in = site->n_ext; |
| 206 | site->n_obj = p->n_obj; |
| 207 | /* One lowered reaction: run token, own cell, one per port, one per |
| 208 | * object, and room for an authority slot wake. */ |
| 209 | if (budget || site->n_ext + 2u + p->n_obj + (p->n_obj ? 1u : 0u) > RX_MAX_DEPS) return X_BUDGET; |
| 210 | |
| 211 | /* Authority pattern: READ on the objects the steps read, nothing else. */ |
| 212 | for (uint32_t i = 0; i < g->n_auth; i++) { |
| 213 | if (!bit(S, g->auth[i].node)) continue; |
| 214 | if (g->auth[i].rights != RX_RIGHT_READ) return X_NO; |
| 215 | int found = 0; |
| 216 | for (uint32_t k = 0; w && k < p->n_obj; k++) |
| 217 | if (site->obj[k].id < RX_MAX_OBJECTS && |
| 218 | w->objects[site->obj[k].id].resource == g->auth[i].resource) |
| 219 | found = 1; |
| 220 | if (w && !found) return X_NO; |
| 221 | } |
| 222 | /* Resource needs, merged as one reaction would carry them. */ |
| 223 | for (uint32_t i = 0; i < g->n_res; i++) { |
| 224 | if (!bit(S, g->res[i].node)) continue; |
| 225 | const RxResourceNeed *q = &g->res[i].need; |
| 226 | RxResourceNeed *m = &p->need; |
| 227 | if (q->compute_class && m->compute_class && q->compute_class != m->compute_class) return X_NO; |
| 228 | if (q->compute_class) m->compute_class = q->compute_class; |
| 229 | m->locality |= q->locality; |
| 230 | m->accelerator_features |= q->accelerator_features; |
| 231 | if (q->latency_class > m->latency_class) m->latency_class = q->latency_class; |
| 232 | if (q->memory_bytes > m->memory_bytes) m->memory_bytes = q->memory_bytes; |
| 233 | if (q->deadline && (!m->deadline || q->deadline < m->deadline)) m->deadline = q->deadline; |
| 234 | m->energy_cost += q->energy_cost; |
| 235 | } |
| 236 | p->n_steps = wk.n; |
| 237 | p->out_type = g->nodes[exit].out_type; |
| 238 | rx_graph_meta_seal(p); |
| 239 | site->members = S; |
| 240 | site->exit = exit; |
| 241 | site->n_steps = wk.n; |
| 242 | return X_OK; |
| 243 | } |
| 244 | |
| 245 | /* ---- fragments: enumeration ---- |
| 246 | * From each possible exit, grow backwards by adding a producer whose every |
| 247 | * consumer is already inside. Every set is convex (nothing leaves and comes |
| 248 | * back) and has one exit by construction; extract() checks the rest. */ |
| 249 | |
| 250 | typedef int (*SetFn)(void *ctx, uint64_t set, uint32_t exit); |
| 251 | |
| 252 | #define SEEN_MAX 512u |
| 253 | typedef struct { uint32_t n; uint64_t s[SEEN_MAX]; } Seen; |
| 254 | |
| 255 | static int seen_add(Seen *sn, uint64_t s) { |
| 256 | for (uint32_t i = 0; i < sn->n; i++) |
| 257 | if (sn->s[i] == s) return 0; |
| 258 | if (sn->n >= SEEN_MAX) return 0; |
| 259 | sn->s[sn->n++] = s; |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | static int addable(const AgGraph *g, uint64_t S, uint32_t p) { |
| 264 | for (uint32_t e = 0; e < g->n_data; e++) |
| 265 | if (g->data[e].from == p && !bit(S, g->data[e].to)) return 0; |
| 266 | for (uint32_t d = 0; d < g->n_deps; d++) |
| 267 | if (g->deps[d].from == p || g->deps[d].to == p) return 0; |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | static int grow(const AgGraph *g, uint64_t elig, uint64_t S, uint32_t exit, Seen *sn, SetFn fn, |
| 272 | void *ctx) { |
| 273 | if (!seen_add(sn, S)) return 0; |
| 274 | if (fn(ctx, S, exit)) return 1; |
| 275 | if ((uint32_t)__builtin_popcountll(S) >= AG_META_MAX_STEPS) return 0; |
| 276 | for (uint32_t e = 0; e < g->n_data; e++) { |
| 277 | const AgDataEdge *d = &g->data[e]; |
| 278 | if (!bit(S, d->to) || bit(S, d->from) || !bit(elig, d->from) || d->mode != AG_EDGE_DATA) continue; |
| 279 | if (!addable(g, S, d->from)) continue; |
| 280 | if (grow(g, elig, S | (1ull << d->from), exit, sn, fn, ctx)) return 1; |
| 281 | } |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static void enumerate(const AgGraph *g, SetFn fn, void *ctx) { |
| 286 | uint64_t elig = 0; |
| 287 | for (uint32_t n = 0; n < g->n_nodes; n++) |
| 288 | if (g->nodes[n].alive && eligible_kind(g->nodes[n].kind) && !boundary_kind(g->nodes[n].kind)) |
| 289 | elig |= 1ull << n; |
| 290 | static __thread Seen sn; |
| 291 | for (uint32_t x = 0; x < g->n_nodes; x++) { |
| 292 | if (!bit(elig, x)) continue; |
| 293 | sn.n = 0; |
| 294 | if (grow(g, elig, 1ull << x, x, &sn, fn, ctx)) return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* ---- observation ---- */ |
| 299 | |
| 300 | void rx_fusion_observer_init(AgFusionObserver *obs) { memset(obs, 0, sizeof *obs); } |
| 301 | |
| 302 | uint64_t rx_fusion_site_steps(const AgGraph *g, const AgFusionSite *site, const AgMetaProgram *p, |
| 303 | const AgResult *r) { |
| 304 | uint64_t acc = 0; |
| 305 | for (uint32_t j = 0; j < p->n_steps && j < site->n_steps; j++) { |
| 306 | uint32_t n = site->node[j]; |
| 307 | uint32_t e[AG_MAX_IN]; |
| 308 | uint32_t k = inputs_by_port(g, n, e); |
| 309 | uint64_t in[AG_MAX_IN]; |
| 310 | for (uint32_t i = 0; i < k; i++) in[i] = r->value[g->data[e[i]].from]; |
| 311 | acc = rx_graph_step_record(acc, p->step[j].local_id, r->status[n], r->value[n], in, k); |
| 312 | } |
| 313 | return acc; |
| 314 | } |
| 315 | |
| 316 | static void site_inputs(const AgGraph *g, const AgFusionSite *site, const AgMetaProgram *p, |
| 317 | const AgResult *r, const AgReference *ref, AgFusionSample *s) { |
| 318 | memset(s, 0, sizeof *s); |
| 319 | for (uint32_t i = 0; i < site->n_ext; i++) { |
| 320 | s->s[i] = r->status[site->ext[i]]; |
| 321 | s->v[i] = r->value[site->ext[i]]; |
| 322 | } |
| 323 | for (uint32_t j = 0; j < p->n_steps; j++) |
| 324 | if (p->step[j].obj >= 0) |
| 325 | memcpy(s->world[p->step[j].obj], ref->world_seen[site->node[j]][0], sizeof s->world[0]); |
| 326 | (void)g; |
| 327 | } |
| 328 | |
| 329 | static int same_key(const AgFusionSample *a, const AgFusionSample *b, uint32_t n_in) { |
| 330 | for (uint32_t i = 0; i < n_in; i++) |
| 331 | if (a->s[i] != b->s[i] || a->v[i] != b->v[i]) return 0; |
| 332 | return memcmp(a->world, b->world, sizeof a->world) == 0; |
| 333 | } |
| 334 | |
| 335 | typedef struct { |
| 336 | AgFusionObserver *obs; |
| 337 | const RxWorld *w; |
| 338 | const AgGraph *g; |
| 339 | const AgResult *got; |
| 340 | const AgReference *ref; |
| 341 | } ObsCtx; |
| 342 | |
| 343 | static int observe_set(void *vc, uint64_t S, uint32_t exit) { |
| 344 | ObsCtx *c = vc; |
| 345 | AgFusionObserver *obs = c->obs; |
| 346 | static __thread AgMetaProgram p; |
| 347 | AgFusionSite site; |
| 348 | int rc = extract(c->g, c->w, S, exit, &p, &site); |
| 349 | if (rc == X_BUDGET) { obs->fragments_over_budget++; return 0; } |
| 350 | if (rc != X_OK) return 0; |
| 351 | obs->fragments_seen++; |
| 352 | for (uint32_t j = 0; j < site.n_steps; j++) |
| 353 | if (c->got->status[site.node[j]] == AG_PENDING) return 0; /* did not run */ |
| 354 | AgFusionPattern *pat = NULL; |
| 355 | for (uint32_t i = 0; i < obs->n; i++) |
| 356 | if (memcmp(obs->p[i].prog.signature, p.signature, 32) == 0) pat = &obs->p[i]; |
| 357 | if (!pat) { |
| 358 | if (obs->n >= AG_FUSION_MAX_PATTERNS) { obs->patterns_dropped++; return 0; } |
| 359 | pat = &obs->p[obs->n++]; |
| 360 | memset(pat, 0, sizeof *pat); |
| 361 | pat->prog = p; |
| 362 | } |
| 363 | pat->occurrences++; |
| 364 | int known = 0; |
| 365 | for (uint32_t i = 0; i < pat->n_graphs; i++) known |= memcmp(pat->graphs[i], c->g->digest, 32) == 0; |
| 366 | if (!known && pat->n_graphs < AG_FUSION_MAX_GRAPHS) memcpy(pat->graphs[pat->n_graphs++], c->g->digest, 32); |
| 367 | if (pat->n_ancestry < AG_FUSION_MAX_ANCESTRY) { |
| 368 | AgAncestry *a = &pat->ancestry[pat->n_ancestry++]; |
| 369 | memcpy(a->graph, c->g->digest, 32); |
| 370 | a->run = c->got->run; |
| 371 | a->exit_origin = c->g->nodes[exit].origin; |
| 372 | } |
| 373 | uint8_t st = c->got->status[exit]; |
| 374 | if (st == AG_OK) pat->ok++; |
| 375 | else if (st == AG_FAILED) pat->failed++; |
| 376 | else if (st == AG_SKIPPED) pat->skipped++; |
| 377 | |
| 378 | AgFusionSample s; |
| 379 | site_inputs(c->g, &site, &p, c->got, c->ref, &s); |
| 380 | s.status = st; |
| 381 | s.value = c->got->value[exit]; |
| 382 | s.attempts = c->got->attempts[exit]; |
| 383 | s.steps = rx_fusion_site_steps(c->g, &site, &p, c->got); |
| 384 | /* The resident run and the reference must tell the same step story. */ |
| 385 | if (rx_fusion_site_steps(c->g, &site, &p, &c->ref->r) != s.steps) { |
| 386 | pat->evidence_unstable++; |
| 387 | return 0; |
| 388 | } |
| 389 | for (uint32_t i = 0; i < pat->n_samples; i++) { |
| 390 | const AgFusionSample *q = &pat->sample[i]; |
| 391 | if (!same_key(q, &s, p.n_in)) continue; |
| 392 | if (q->status == s.status && q->value == s.value && q->steps == s.steps && |
| 393 | q->attempts == s.attempts) |
| 394 | pat->evidence_stable++; |
| 395 | else |
| 396 | pat->evidence_unstable++; |
| 397 | return 0; |
| 398 | } |
| 399 | if (pat->n_samples < AG_FUSION_MAX_SAMPLES) pat->sample[pat->n_samples++] = s; |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | int rx_fusion_observe(AgFusionObserver *obs, const RxWorld *w, const AgGraph *g, |
| 404 | const AgResult *got, const AgReference *ref) { |
| 405 | int ok = got->outcome != AG_RUN_INCOMPLETE && got->outcome == ref->r.outcome; |
| 406 | for (uint32_t n = 0; ok && n < g->n_nodes; n++) |
| 407 | if (got->status[n] != ref->r.status[n] || got->value[n] != ref->r.value[n] || |
| 408 | (got->status[n] && got->evidence[n] != ref->r.evidence[n]) || |
| 409 | got->steps[n] != ref->r.steps[n]) |
| 410 | ok = 0; |
| 411 | if (ok && got->outcome == AG_RUN_SUCCESS) |
| 412 | for (uint32_t i = 0; i < g->n_evidence; i++) |
| 413 | if (got->status[g->evidence[i]] == AG_PENDING || got->evidence[g->evidence[i]] == 0) ok = 0; |
| 414 | if (!ok) { |
| 415 | obs->runs_refused++; |
| 416 | return 0; |
| 417 | } |
| 418 | obs->runs_observed++; |
| 419 | ObsCtx c = { obs, w, g, got, ref }; |
| 420 | enumerate(g, observe_set, &c); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | int rx_fusion_judge(const AgFusionObserver *obs, uint32_t i, const AgFusionPolicy *pol) { |
| 425 | const AgFusionPattern *p = &obs->p[i]; |
| 426 | uint32_t min_steps = pol->min_steps > 2 ? pol->min_steps : 2; |
| 427 | if (p->prog.n_steps < min_steps) return AG_FUSE_TOO_SMALL; |
| 428 | /* What the fragment is comes before how often it was seen. */ |
| 429 | if (p->evidence_unstable) return AG_FUSE_UNSTABLE; |
| 430 | uint64_t decided = (uint64_t)p->ok + p->failed; |
| 431 | if (!decided || (uint64_t)p->failed * 1000u > (uint64_t)pol->max_fail_permille * decided) |
| 432 | return AG_FUSE_FAILURE_RATE; |
| 433 | if (p->occurrences < pol->min_occurrences) return AG_FUSE_TOO_FEW; |
| 434 | if (p->n_graphs < pol->min_graphs) return AG_FUSE_TOO_FEW_GRAPHS; |
| 435 | return AG_FUSE_ELIGIBLE; |
| 436 | } |
| 437 | |
| 438 | uint32_t rx_fusion_candidates(const AgFusionObserver *obs, const AgFusionPolicy *pol, |
| 439 | uint32_t *idx, uint32_t max) { |
| 440 | uint32_t k = 0; |
| 441 | for (uint32_t i = 0; i < obs->n && k < max; i++) |
| 442 | if (rx_fusion_judge(obs, i, pol) == AG_FUSE_ELIGIBLE) idx[k++] = i; |
| 443 | for (uint32_t a = 1; a < k; a++) |
| 444 | for (uint32_t b = a; b > 0; b--) { |
| 445 | const AgFusionPattern *x = &obs->p[idx[b - 1]], *y = &obs->p[idx[b]]; |
| 446 | int cmp = x->prog.n_steps != y->prog.n_steps ? (int)y->prog.n_steps - (int)x->prog.n_steps |
| 447 | : x->occurrences != y->occurrences ? (int)y->occurrences - (int)x->occurrences |
| 448 | : memcmp(x->prog.signature, y->prog.signature, 32); |
| 449 | if (cmp <= 0) break; |
| 450 | uint32_t t = idx[b]; idx[b] = idx[b - 1]; idx[b - 1] = t; |
| 451 | } |
| 452 | return k; |
| 453 | } |
| 454 | |
| 455 | /* ---- candidate ---- */ |
| 456 | |
| 457 | static uint32_t count_kind(const AgMetaProgram *p, AgKind k) { |
| 458 | uint32_t n = 0; |
| 459 | for (uint32_t j = 0; j < p->n_steps && j < AG_META_MAX_STEPS; j++) n += p->step[j].node.kind == k; |
| 460 | return n; |
| 461 | } |
| 462 | |
| 463 | static int may_fail(const AgMetaProgram *p) { |
| 464 | if (p->n_in) return 1; |
| 465 | for (uint32_t j = 0; j < p->n_steps && j < AG_META_MAX_STEPS; j++) { |
| 466 | AgKind k = p->step[j].node.kind; |
| 467 | if (k == AG_VERIFY || k == AG_RECALL || k == AG_RETRY || k == AG_SKILL || k == AG_PURE) return 1; |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* Contracts are derived from the fragment, never chosen. */ |
| 473 | static void derive_contracts(const AgMetaProgram *q, AgMetaSkill *m) { |
| 474 | m->input_contract.n = q->n_in; |
| 475 | for (uint32_t i = 0; i < AG_MAX_IN; i++) { |
| 476 | m->input_contract.type[i] = q->in_type[i]; |
| 477 | m->input_contract.mode[i] = q->in_mode[i]; |
| 478 | } |
| 479 | m->output_contract.type = q->out_type; |
| 480 | m->output_contract.may_fail = (uint8_t)may_fail(q); |
| 481 | m->authority_contract.n_obj = q->n_obj; |
| 482 | for (uint32_t i = 0; i < AG_META_MAX_OBJ; i++) m->authority_contract.rights[i] = q->obj_rights[i]; |
| 483 | m->resource_contract = q->need; |
| 484 | m->effect_contract.performs = count_kind(q, AG_EFFECT_PERFORM); |
| 485 | m->effect_contract.publishes = count_kind(q, AG_WORLD_PUBLISH); |
| 486 | m->effect_contract.proposals = count_kind(q, AG_EFFECT_PROPOSE); |
| 487 | } |
| 488 | |
| 489 | void rx_fusion_identity(const AgMetaSkill *m, uint8_t out[32]) { |
| 490 | sha256_ctx c; |
| 491 | sha256_init(&c); |
| 492 | sha256_update(&c, (const uint8_t *)"OMEGA_METASKILL_V1", 18); |
| 493 | put32(&c, m->id); |
| 494 | put32(&c, m->input_contract.n); |
| 495 | for (uint32_t i = 0; i < AG_MAX_IN; i++) { |
| 496 | put32(&c, m->input_contract.type[i]); |
| 497 | put8(&c, m->input_contract.mode[i]); |
| 498 | } |
| 499 | put32(&c, m->output_contract.type); |
| 500 | put8(&c, m->output_contract.may_fail); |
| 501 | put32(&c, m->authority_contract.n_obj); |
| 502 | for (uint32_t i = 0; i < AG_META_MAX_OBJ; i++) put32(&c, m->authority_contract.rights[i]); |
| 503 | const RxResourceNeed *r = &m->resource_contract; |
| 504 | put32(&c, r->compute_class); |
| 505 | put32(&c, r->locality); |
| 506 | put32(&c, r->accelerator_features); |
| 507 | put32(&c, r->latency_class); |
| 508 | put64(&c, r->memory_bytes); |
| 509 | put64(&c, r->deadline); |
| 510 | put64(&c, r->energy_cost); |
| 511 | put32(&c, m->effect_contract.performs); |
| 512 | put32(&c, m->effect_contract.publishes); |
| 513 | put32(&c, m->effect_contract.proposals); |
| 514 | sha256_update(&c, m->realization.identity, 32); |
| 515 | sha256_update(&c, m->reference.signature, 32); |
| 516 | put32(&c, m->n_ancestry); |
| 517 | for (uint32_t i = 0; i < m->n_ancestry && i < AG_FUSION_MAX_ANCESTRY; i++) { |
| 518 | sha256_update(&c, m->ancestry[i].graph, 32); |
| 519 | put64(&c, m->ancestry[i].run); |
| 520 | put32(&c, m->ancestry[i].exit_origin); |
| 521 | } |
| 522 | sha256_final(&c, out); |
| 523 | } |
| 524 | |
| 525 | int rx_fusion_build(const AgFusionPattern *p, AgRealKind kind, uint32_t skill_id, AgMetaSkill *out) { |
| 526 | if (!p || !out || p->prog.n_steps < 2 || kind < AG_REAL_COMPILED || kind > AG_REAL_HARDWARE) |
| 527 | return AG_E_ARG; |
| 528 | memset(out, 0, sizeof *out); |
| 529 | out->id = skill_id; |
| 530 | out->state = AG_MS_CANDIDATE; |
| 531 | out->n_ancestry = p->n_ancestry; |
| 532 | memcpy(out->ancestry, p->ancestry, sizeof out->ancestry); |
| 533 | derive_contracts(&p->prog, out); |
| 534 | out->reference = p->prog; |
| 535 | out->n_samples = p->n_samples; |
| 536 | memcpy(out->samples, p->sample, sizeof out->samples); |
| 537 | out->realization.kind = kind; |
| 538 | out->realization.prog = p->prog; |
| 539 | if (kind == AG_REAL_LEARNED || kind == AG_REAL_HYBRID) |
| 540 | for (uint32_t i = 0; i < p->n_samples && i < AG_META_TABLE; i++) { |
| 541 | const AgFusionSample *s = &p->sample[i]; |
| 542 | AgMetaEntry *t = &out->realization.table[out->realization.n_table++]; |
| 543 | memcpy(t->s, s->s, sizeof t->s); |
| 544 | memcpy(t->v, s->v, sizeof t->v); |
| 545 | t->world = rx_graph_meta_world_key(&p->prog, s->world); |
| 546 | t->status = s->status; |
| 547 | t->value = s->value; |
| 548 | t->steps = s->steps; |
| 549 | t->attempts = s->attempts; |
| 550 | } |
| 551 | rx_graph_realization_identity(&out->realization); |
| 552 | rx_fusion_identity(out, out->identity); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* ---- verification ---- */ |
| 557 | |
| 558 | #define FAIL_SKILL 0xFFFFFF01u |
| 559 | |
| 560 | static uint64_t always_fails(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { |
| 561 | (void)in; (void)n; (void)attempt; |
| 562 | *failed = 1; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static uint64_t rng(uint64_t *s) { |
| 567 | uint64_t x = *s; |
| 568 | x ^= x << 13; |
| 569 | x ^= x >> 7; |
| 570 | x ^= x << 17; |
| 571 | return *s = x; |
| 572 | } |
| 573 | |
| 574 | /* Port drivers: each port gets a node of its contract type that ends OK |
| 575 | * with value v, FAILED, or SKIPPED. */ |
| 576 | static int drivers(AgGraph *g, const AgMetaProgram *p, const uint8_t *s, const uint64_t *v, |
| 577 | uint16_t *port_node) { |
| 578 | int br = -1; |
| 579 | for (uint32_t i = 0; i < p->n_in; i++) { |
| 580 | int base = rx_graph_node(g, AG_CONST, AG_T_U64); |
| 581 | if (base < 0) return -1; |
| 582 | g->nodes[base].imm = v[i]; |
| 583 | int src = base; |
| 584 | if (s[i] == AG_FAILED) { |
| 585 | int k = rx_graph_node(g, AG_SKILL, AG_T_U64); |
| 586 | if (k < 0) return -1; |
| 587 | g->nodes[k].op = FAIL_SKILL; |
| 588 | rx_graph_data(g, (uint32_t)base, (uint32_t)k, 0, AG_EDGE_DATA); |
| 589 | src = k; |
| 590 | } |
| 591 | int c; |
| 592 | switch (p->in_type[i]) { |
| 593 | case AG_T_U64: case AG_T_BOOL: |
| 594 | c = rx_graph_node(g, AG_PURE, p->in_type[i]); |
| 595 | if (c >= 0) g->nodes[c].op = OP_IDENTITY; |
| 596 | break; |
| 597 | case AG_T_PROPOSAL: |
| 598 | c = rx_graph_node(g, AG_EFFECT_PROPOSE, AG_T_PROPOSAL); |
| 599 | break; |
| 600 | case AG_T_VERDICT: |
| 601 | c = rx_graph_node(g, AG_VERIFY, AG_T_VERDICT); |
| 602 | if (c >= 0) g->nodes[c].imm2 = UINT64_MAX; |
| 603 | break; |
| 604 | default: |
| 605 | return -1; |
| 606 | } |
| 607 | if (c < 0) return -1; |
| 608 | rx_graph_data(g, (uint32_t)src, (uint32_t)c, 0, AG_EDGE_DATA); |
| 609 | if (s[i] == AG_SKIPPED) { |
| 610 | if (br < 0) { |
| 611 | int zero = rx_graph_node(g, AG_CONST, AG_T_U64); |
| 612 | br = rx_graph_node(g, AG_BRANCH, AG_T_BOOL); |
| 613 | if (zero < 0 || br < 0) return -1; |
| 614 | rx_graph_data(g, (uint32_t)zero, (uint32_t)br, 0, AG_EDGE_DATA); |
| 615 | } |
| 616 | rx_graph_guard(g, (uint32_t)br, (uint32_t)c, 1); |
| 617 | } |
| 618 | port_node[i] = (uint16_t)c; |
| 619 | } |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | #define VOBJ_ID 250u |
| 624 | |
| 625 | /* The original fragment as graph nodes after its drivers. */ |
| 626 | static int harness_original(AgGraph *g, const AgMetaProgram *p, const uint8_t *s, const uint64_t *v, |
| 627 | AgFusionSite *site) { |
| 628 | rx_graph_init(g, 0); |
| 629 | memset(site, 0, sizeof *site); |
| 630 | uint16_t port[AG_MAX_IN]; |
| 631 | if (drivers(g, p, s, v, port) != 0) return -1; |
| 632 | for (uint32_t j = 0; j < p->n_steps; j++) { |
| 633 | const AgMetaStep *q = &p->step[j]; |
| 634 | int n = rx_graph_node(g, q->node.kind, q->node.out_type); |
| 635 | if (n < 0) return -1; |
| 636 | AgNode *x = &g->nodes[n]; |
| 637 | x->op = q->node.op; |
| 638 | x->imm = q->node.imm; |
| 639 | x->imm2 = q->node.imm2; |
| 640 | x->field = q->node.field; |
| 641 | x->cost_us = 0; /* verification checks meaning, not time */ |
| 642 | x->n_fused = q->node.n_fused; |
| 643 | memcpy(x->fused, q->node.fused, sizeof x->fused); |
| 644 | if (q->obj >= 0) x->obj = (RxObjRef){ VOBJ_ID + (uint32_t)q->obj, 1 }; |
| 645 | for (uint32_t i = 0; i < q->n_in; i++) { |
| 646 | uint32_t from = q->in[i].step ? site->node[q->in[i].index] : port[q->in[i].index]; |
| 647 | rx_graph_data(g, from, (uint32_t)n, i, q->in[i].step ? AG_EDGE_DATA : q->in[i].mode); |
| 648 | } |
| 649 | site->node[j] = (uint16_t)n; |
| 650 | } |
| 651 | site->n_steps = p->n_steps; |
| 652 | site->exit = site->node[p->n_steps - 1]; |
| 653 | return rx_graph_validate(g, NULL); |
| 654 | } |
| 655 | |
| 656 | /* The same drivers and one AG_META node running the candidate. */ |
| 657 | static int harness_fused(AgGraph *g, const AgMetaSkill *m, const uint8_t *s, const uint64_t *v, |
| 658 | uint32_t *meta) { |
| 659 | const AgMetaProgram *p = &m->realization.prog; |
| 660 | rx_graph_init(g, 0); |
| 661 | uint16_t port[AG_MAX_IN]; |
| 662 | if (drivers(g, p, s, v, port) != 0) return -1; |
| 663 | int n = rx_graph_node(g, AG_META, p->out_type); |
| 664 | if (n < 0) return -1; |
| 665 | AgNode *x = &g->nodes[n]; |
| 666 | x->op = m->id; |
| 667 | x->imm = word64(m->realization.identity); |
| 668 | x->imm2 = word64(m->realization.identity + 8); |
| 669 | x->n_mobj = p->n_obj; |
| 670 | for (uint32_t i = 0; i < p->n_obj; i++) x->mobj[i] = (RxObjRef){ VOBJ_ID + i, 1 }; |
| 671 | for (uint32_t i = 0; i < p->n_in; i++) { |
| 672 | x->in_type[i] = p->in_type[i]; |
| 673 | rx_graph_data(g, port[i], (uint32_t)n, i, p->in_mode[i]); |
| 674 | } |
| 675 | *meta = (uint32_t)n; |
| 676 | return rx_graph_validate(g, NULL); |
| 677 | } |
| 678 | |
| 679 | static int prog_equal_ids(const AgMetaProgram *a, const AgMetaProgram *b) { |
| 680 | if (a->n_steps != b->n_steps || memcmp(a->signature, b->signature, 32) != 0) return 0; |
| 681 | for (uint32_t j = 0; j < a->n_steps && j < AG_META_MAX_STEPS; j++) |
| 682 | if (memcmp(a->step[j].local_id, b->step[j].local_id, 32) != 0) return 0; |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | typedef struct { |
| 687 | AgGraph h0, h1; |
| 688 | AgReference r0, r1; |
| 689 | AgSkillTable t0, t1; |
| 690 | } Bench; |
| 691 | |
| 692 | static Bench g_bench; /* verification is not re-entrant */ |
| 693 | |
| 694 | static void run_vector(AgMetaSkill *m, Bench *b, const uint8_t *s, const uint64_t *v, |
| 695 | const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]) { |
| 696 | AgFusionVerdict *vd = &m->verdict; |
| 697 | vd->vectors++; |
| 698 | AgFusionSite site; |
| 699 | uint32_t meta = 0; |
| 700 | if (harness_original(&b->h0, &m->reference, s, v, &site) != 0 || |
| 701 | harness_fused(&b->h1, m, s, v, &meta) != 0) { |
| 702 | vd->semantic_mismatch++; |
| 703 | return; |
| 704 | } |
| 705 | AgObjContents objs[AG_META_MAX_OBJ]; |
| 706 | for (uint32_t i = 0; i < AG_META_MAX_OBJ; i++) { |
| 707 | objs[i].obj = (RxObjRef){ VOBJ_ID + i, 1 }; |
| 708 | memcpy(objs[i].field, world[i], sizeof objs[i].field); |
| 709 | } |
| 710 | rx_graph_reference_on(&b->h0, &b->t0, objs, AG_META_MAX_OBJ, 1, &b->r0); |
| 711 | rx_graph_reference_on(&b->h1, &b->t1, objs, AG_META_MAX_OBJ, 1, &b->r1); |
| 712 | /* What the ports actually carried (a VERDICT is 1 when OK). */ |
| 713 | uint8_t ps[AG_MAX_IN]; |
| 714 | uint64_t pv[AG_MAX_IN]; |
| 715 | int any_bad = 0; |
| 716 | for (uint32_t i = 0; i < m->reference.n_in; i++) { |
| 717 | uint32_t e[AG_MAX_IN]; |
| 718 | (void)e; |
| 719 | uint32_t pn = 0; |
| 720 | for (uint32_t d = 0; d < b->h1.n_data; d++) |
| 721 | if (b->h1.data[d].to == meta && b->h1.data[d].port == i) pn = b->h1.data[d].from; |
| 722 | ps[i] = b->r1.r.status[pn]; |
| 723 | pv[i] = b->r1.r.value[pn]; |
| 724 | any_bad |= ps[i] != AG_OK; |
| 725 | } |
| 726 | AgMetaOut mo; |
| 727 | if (rx_graph_meta_run(&m->realization, &b->t1, ps, pv, m->reference.n_in, world, &mo) != 0) |
| 728 | vd->not_runnable++; |
| 729 | vd->table_hits += (uint32_t)mo.table_hit; |
| 730 | uint32_t x = site.exit; |
| 731 | uint8_t es = b->r0.r.status[x], gs = b->r1.r.status[meta]; |
| 732 | if (es != gs || b->r0.r.value[x] != b->r1.r.value[meta] || |
| 733 | b->r0.r.attempts[x] != b->r1.r.attempts[meta]) { |
| 734 | if (any_bad || es != AG_OK) vd->failure_mismatch++; |
| 735 | else vd->semantic_mismatch++; |
| 736 | return; |
| 737 | } |
| 738 | if (es != AG_PENDING && |
| 739 | rx_fusion_site_steps(&b->h0, &site, &m->reference, &b->r0.r) != b->r1.r.steps[meta]) |
| 740 | vd->evidence_mismatch++; |
| 741 | } |
| 742 | |
| 743 | int rx_fusion_verify(AgMetaSkill *m, const AgSkillTable *skills, uint64_t seed) { |
| 744 | if (!m || m->state != AG_MS_CANDIDATE) return AG_FUSION_E_STATE; |
| 745 | AgFusionVerdict *vd = &m->verdict; |
| 746 | memset(vd, 0, sizeof *vd); |
| 747 | const AgMetaProgram *ref = &m->reference; |
| 748 | const AgRealization *rz = &m->realization; |
| 749 | |
| 750 | /* 1. Identity: what it carries is what it is. */ |
| 751 | static __thread AgMetaProgram pr; |
| 752 | static __thread AgRealization rr; |
| 753 | pr = *ref; |
| 754 | rx_graph_meta_seal(&pr); |
| 755 | rr = *rz; |
| 756 | rx_graph_meta_seal(&rr.prog); |
| 757 | rx_graph_realization_identity(&rr); |
| 758 | uint8_t id[32]; |
| 759 | rx_fusion_identity(m, id); |
| 760 | vd->identity_ok = prog_equal_ids(&pr, ref) && prog_equal_ids(&rr.prog, &rz->prog) && |
| 761 | memcmp(rr.identity, rz->identity, 32) == 0 && memcmp(id, m->identity, 32) == 0; |
| 762 | |
| 763 | /* 2. Contracts: derived from the reference fragment, and the realization |
| 764 | * serves that fragment. */ |
| 765 | static __thread AgMetaSkill d; |
| 766 | memset(&d, 0, sizeof d); |
| 767 | derive_contracts(ref, &d); |
| 768 | vd->contract_ok = ref->n_steps >= 2 && ref->n_steps <= AG_META_MAX_STEPS && |
| 769 | memcmp(&d.input_contract, &m->input_contract, sizeof d.input_contract) == 0 && |
| 770 | d.output_contract.type == m->output_contract.type && |
| 771 | d.output_contract.may_fail == m->output_contract.may_fail && |
| 772 | memcmp(rz->prog.signature, ref->signature, 32) == 0; |
| 773 | vd->authority_equal = m->authority_contract.n_obj == ref->n_obj && |
| 774 | memcmp(m->authority_contract.rights, ref->obj_rights, |
| 775 | sizeof m->authority_contract.rights) == 0 && |
| 776 | memcmp(rz->prog.obj_rights, ref->obj_rights, sizeof ref->obj_rights) == 0; |
| 777 | for (uint32_t i = 0; i < ref->n_obj && i < AG_META_MAX_OBJ; i++) |
| 778 | if (ref->obj_rights[i] != RX_RIGHT_READ) vd->authority_equal = 0; |
| 779 | vd->effect_equal = d.effect_contract.performs == 0 && d.effect_contract.publishes == 0 && |
| 780 | count_kind(&rz->prog, AG_EFFECT_PERFORM) == 0 && |
| 781 | count_kind(&rz->prog, AG_WORLD_PUBLISH) == 0 && |
| 782 | memcmp(&d.effect_contract, &m->effect_contract, sizeof d.effect_contract) == 0; |
| 783 | vd->resource_equal = memcmp(&d.resource_contract, &m->resource_contract, sizeof d.resource_contract) == 0 && |
| 784 | memcmp(&rz->prog.need, &ref->need, sizeof ref->need) == 0; |
| 785 | if (!vd->identity_ok) vd->reason = AG_VR_IDENTITY; |
| 786 | else if (!vd->contract_ok) vd->reason = AG_VR_CONTRACT; |
| 787 | else if (!vd->authority_equal) vd->reason = AG_VR_AUTHORITY; |
| 788 | else if (!vd->effect_equal) vd->reason = AG_VR_EFFECT; |
| 789 | else if (!vd->resource_equal) vd->reason = AG_VR_RESOURCE; |
| 790 | if (vd->reason) { |
| 791 | m->state = AG_MS_REJECTED; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /* 3. Differential: the fragment step by step against the candidate. */ |
| 796 | Bench *b = &g_bench; |
| 797 | b->t0 = skills ? *skills : (AgSkillTable){ 0 }; |
| 798 | b->t0.n_meta = 0; |
| 799 | if (b->t0.n < AG_MAX_SKILLS) b->t0.skill[b->t0.n++] = (AgSkill){ FAIL_SKILL, always_fails, { 0 } }; |
| 800 | b->t1 = b->t0; |
| 801 | b->t1.meta[b->t1.n_meta++] = (typeof(b->t1.meta[0])){ m->id, &m->realization }; |
| 802 | |
| 803 | uint32_t n_in = ref->n_in; |
| 804 | /* Observed inputs, as they were: the fragment must reproduce what was |
| 805 | * recorded, and the candidate must agree with the fragment. */ |
| 806 | for (uint32_t i = 0; i < m->n_samples && i < AG_FUSION_MAX_SAMPLES; i++) { |
| 807 | const AgFusionSample *q = &m->samples[i]; |
| 808 | run_vector(m, b, q->s, q->v, (const uint64_t (*)[RX_MAX_FIELDS])q->world); |
| 809 | vd->observed++; |
| 810 | AgFusionSite site; |
| 811 | if (harness_original(&b->h0, ref, q->s, q->v, &site) == 0) { |
| 812 | AgObjContents objs[AG_META_MAX_OBJ]; |
| 813 | for (uint32_t o = 0; o < AG_META_MAX_OBJ; o++) { |
| 814 | objs[o].obj = (RxObjRef){ VOBJ_ID + o, 1 }; |
| 815 | memcpy(objs[o].field, q->world[o], sizeof objs[o].field); |
| 816 | } |
| 817 | rx_graph_reference_on(&b->h0, &b->t0, objs, AG_META_MAX_OBJ, 1, &b->r0); |
| 818 | if (b->r0.r.status[site.exit] != q->status || b->r0.r.value[site.exit] != q->value || |
| 819 | rx_fusion_site_steps(&b->h0, &site, ref, &b->r0.r) != q->steps) |
| 820 | vd->semantic_mismatch++; |
| 821 | } |
| 822 | } |
| 823 | /* Generated: every status combination over the ports, several value and |
| 824 | * World sets each (zeros, ones, all-ones, random, VERIFY/imm bounds). */ |
| 825 | uint64_t bounds[16]; |
| 826 | uint32_t nb = 0; |
| 827 | for (uint32_t j = 0; j < ref->n_steps && nb + 4 <= 16; j++) { |
| 828 | const AgNode *x = &ref->step[j].node; |
| 829 | if (x->kind == AG_VERIFY || x->kind == AG_RECALL || x->kind == AG_PURE) { |
| 830 | bounds[nb++] = x->imm; |
| 831 | bounds[nb++] = x->imm - 1; |
| 832 | bounds[nb++] = x->imm2; |
| 833 | bounds[nb++] = x->imm2 + 1; |
| 834 | } |
| 835 | } |
| 836 | uint32_t combos = 1; |
| 837 | for (uint32_t i = 0; i < n_in; i++) combos *= 3; |
| 838 | vd->status_combos = combos; |
| 839 | uint64_t rs = seed ? seed : 0x9E3779B97F4A7C15ull; |
| 840 | const uint32_t sets = 6 + nb + 24; /* then 24 sets over the full range */ |
| 841 | for (uint32_t c = 0; c < combos; c++) { |
| 842 | uint8_t s[AG_MAX_IN] = { 0 }; |
| 843 | uint32_t cc = c; |
| 844 | for (uint32_t i = 0; i < n_in; i++) { |
| 845 | s[i] = (uint8_t)(cc % 3 == 0 ? AG_OK : cc % 3 == 1 ? AG_FAILED : AG_SKIPPED); |
| 846 | cc /= 3; |
| 847 | } |
| 848 | for (uint32_t k = 0; k < sets; k++) { |
| 849 | uint64_t v[AG_MAX_IN] = { 0 }; |
| 850 | uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]; |
| 851 | memset(world, 0, sizeof world); |
| 852 | for (uint32_t i = 0; i < n_in; i++) { |
| 853 | v[i] = k == 0 ? 0 : k == 1 ? 1 : k == 2 ? UINT64_MAX |
| 854 | : k < 6 ? rng(&rs) % (k == 3 ? 1000u : k == 4 ? 1000000u : UINT32_MAX) |
| 855 | : k < 6 + nb ? bounds[k - 6] : rng(&rs); |
| 856 | if (s[i] != AG_OK) v[i] = 0; |
| 857 | } |
| 858 | for (uint32_t o = 0; o < ref->n_obj; o++) |
| 859 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) |
| 860 | world[o][f] = k == 0 ? 0 : k == 1 ? 1 : k == 2 ? UINT64_MAX |
| 861 | : k < 6 ? rng(&rs) % (k == 3 ? 1000u : k == 4 ? 1000000u : UINT32_MAX) |
| 862 | : k < 6 + nb ? bounds[k - 6] : rng(&rs); |
| 863 | /* Recall keys the steps look for, present in half the sets. */ |
| 864 | for (uint32_t j = 0; j < ref->n_steps; j++) { |
| 865 | const AgMetaStep *q = &ref->step[j]; |
| 866 | if (q->node.kind != AG_RECALL || q->obj < 0 || (k & 1u)) continue; |
| 867 | uint64_t key = q->n_in ? v[q->in[0].index] : q->node.imm; |
| 868 | if (q->n_in && q->in[0].step) key = world[q->obj][0]; |
| 869 | world[q->obj][2] = key; |
| 870 | world[q->obj][3] = rng(&rs) % 100000u; |
| 871 | } |
| 872 | run_vector(m, b, s, v, (const uint64_t (*)[RX_MAX_FIELDS])world); |
| 873 | vd->generated++; |
| 874 | } |
| 875 | } |
| 876 | vd->pass = vd->semantic_mismatch == 0 && vd->failure_mismatch == 0 && vd->evidence_mismatch == 0 && |
| 877 | vd->not_runnable == 0; |
| 878 | if (!vd->pass) |
| 879 | vd->reason = vd->not_runnable ? AG_VR_NOT_RUNNABLE |
| 880 | : vd->semantic_mismatch ? AG_VR_SEMANTIC |
| 881 | : vd->failure_mismatch ? AG_VR_FAILURE : AG_VR_EVIDENCE; |
| 882 | m->state = vd->pass ? AG_MS_VERIFIED : AG_MS_REJECTED; |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | /* ---- measurement, canary ---- */ |
| 887 | |
| 888 | int rx_fusion_accept_measure(AgMetaSkill *m, const AgFusionMeasure *ms, uint32_t tol) { |
| 889 | if (!m || m->state != AG_MS_VERIFIED) return AG_FUSION_E_STATE; |
| 890 | m->measure = *ms; |
| 891 | /* A latency difference inside the original's own run-to-run spread is |
| 892 | * not a difference. */ |
| 893 | if (ms->spread_permille_before > tol) tol = ms->spread_permille_before; |
| 894 | m->measure.tolerance_permille = tol; |
| 895 | int fewer = ms->commits_after < ms->commits_before && ms->crumbs_after < ms->crumbs_before; |
| 896 | int in_time = (uint64_t)ms->median_ns_after * 1000u <= (uint64_t)ms->median_ns_before * (1000u + tol); |
| 897 | int same = ms->ok_before == ms->ok_after && ms->failed_before == ms->failed_after; |
| 898 | m->state = fewer && in_time && same ? AG_MS_MEASURED : AG_MS_SLOWER; |
| 899 | return m->state == AG_MS_MEASURED ? 0 : -1; |
| 900 | } |
| 901 | |
| 902 | int rx_fusion_canary(AgMetaSkill *m, int diverged, uint32_t required) { |
| 903 | if (!m || m->state != AG_MS_MEASURED) return AG_FUSION_E_STATE; |
| 904 | m->canary_runs++; |
| 905 | if (diverged) { |
| 906 | m->canary_divergences++; |
| 907 | m->state = AG_MS_QUARANTINED; |
| 908 | return -1; |
| 909 | } |
| 910 | if (m->canary_runs >= required && m->canary_divergences == 0) m->state = AG_MS_CANARY_PASSED; |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | /* ---- promotion and publication ---- */ |
| 915 | |
| 916 | static uint8_t g_blob[AG_REALIZATION_MAX_BYTES]; |
| 917 | |
| 918 | /* "model" blob: realization identity, MetaSkill identity. */ |
| 919 | static void model_blob(const AgMetaSkill *m, uint8_t out[64]) { |
| 920 | memcpy(out, m->realization.identity, 32); |
| 921 | memcpy(out + 32, m->identity, 32); |
| 922 | } |
| 923 | |
| 924 | int rx_fusion_promote(AgMetaSkill *m, RxGenStore *store, uint32_t proposer, |
| 925 | const RxPromotionRequest *req, RxGenAuthFn auth, void *auth_ctx) { |
| 926 | if (!m || !store || !req || !auth) return RX_GEN_ERR_ARG; |
| 927 | if (m->state != AG_MS_CANARY_PASSED) return AG_FUSION_E_STATE; |
| 928 | size_t n = rx_graph_realization_encode(&m->realization, g_blob, sizeof g_blob); |
| 929 | if (n > sizeof g_blob) return RX_GEN_ERR_ARG; |
| 930 | uint8_t model[64]; |
| 931 | model_blob(m, model); |
| 932 | /* Evidence: verification, measurement and canary figures as recorded. */ |
| 933 | uint8_t ev[sizeof(AgFusionVerdict) + sizeof(AgFusionMeasure) + 8]; |
| 934 | memcpy(ev, &m->verdict, sizeof m->verdict); |
| 935 | memcpy(ev + sizeof m->verdict, &m->measure, sizeof m->measure); |
| 936 | memcpy(ev + sizeof m->verdict + sizeof m->measure, &m->canary_runs, 4); |
| 937 | memcpy(ev + sizeof m->verdict + sizeof m->measure + 4, &m->canary_divergences, 4); |
| 938 | uint8_t prov[AG_FUSION_MAX_ANCESTRY * 44]; |
| 939 | size_t np = 0; |
| 940 | for (uint32_t i = 0; i < m->n_ancestry && i < AG_FUSION_MAX_ANCESTRY; i++) { |
| 941 | memcpy(prov + np, m->ancestry[i].graph, 32); |
| 942 | memcpy(prov + np + 32, &m->ancestry[i].run, 8); |
| 943 | memcpy(prov + np + 40, &m->ancestry[i].exit_origin, 4); |
| 944 | np += 44; |
| 945 | } |
| 946 | RxGenObject obj; |
| 947 | memset(&obj, 0, sizeof obj); |
| 948 | obj.id = m->id; |
| 949 | obj.generation = 1; |
| 950 | memcpy(obj.digest, m->identity, 32); |
| 951 | RxGenDraft d; |
| 952 | memset(&d, 0, sizeof d); |
| 953 | d.authority_epoch = 1; |
| 954 | d.authority_generation = 1; |
| 955 | d.proofs_ok = m->verdict.pass; |
| 956 | d.objects = &obj; |
| 957 | d.n_objects = 1; |
| 958 | d.evidence = ev; |
| 959 | d.evidence_len = sizeof ev; |
| 960 | d.model = model; |
| 961 | d.model_len = sizeof model; |
| 962 | d.realization = g_blob; |
| 963 | d.realization_len = n; |
| 964 | d.config = (const uint8_t *)&m->authority_contract; |
| 965 | d.config_len = sizeof m->authority_contract; |
| 966 | d.provenance = prov; |
| 967 | d.provenance_len = np; |
| 968 | uint64_t cand = 0; |
| 969 | int rc = rx_gen_propose(store, proposer, &d, &cand); |
| 970 | if (rc != RX_GEN_OK) return rc; |
| 971 | RxPromotionRequest r = *req; |
| 972 | r.candidate_id = cand; |
| 973 | rc = rx_gen_promote(store, &r, auth, auth_ctx, NULL, NULL, NULL, NULL); |
| 974 | if (rc != RX_GEN_OK) return rc; |
| 975 | m->generation = cand; |
| 976 | m->state = AG_MS_PROMOTED; |
| 977 | return RX_GEN_OK; |
| 978 | } |
| 979 | |
| 980 | int rx_fusion_publish(AgMetaSkill *m, const RxGenStore *store, AgSkillTable *skills) { |
| 981 | if (!m || !store || !skills) return AG_E_ARG; |
| 982 | if (m->state != AG_MS_PROMOTED) return AG_FUSION_E_STATE; |
| 983 | uint64_t active = 0, lineage = 0; |
| 984 | if (rx_gen_active(store, &active, &lineage) != RX_GEN_OK || active != m->generation) |
| 985 | return AG_FUSION_E_GENERATION; |
| 986 | /* The MetaSkill as it is now must be the one that was promoted. */ |
| 987 | static __thread AgRealization rz; |
| 988 | rz = m->realization; |
| 989 | rx_graph_realization_identity(&rz); |
| 990 | uint8_t id[32]; |
| 991 | rx_fusion_identity(m, id); |
| 992 | if (memcmp(rz.identity, m->realization.identity, 32) != 0 || memcmp(id, m->identity, 32) != 0) |
| 993 | return AG_FUSION_E_IDENTITY; |
| 994 | uint8_t model[64], want[64]; |
| 995 | size_t len = 0; |
| 996 | if (rx_gen_read_blob(store, m->generation, "model", model, sizeof model, &len) != RX_GEN_OK || |
| 997 | len != sizeof model) |
| 998 | return AG_FUSION_E_IDENTITY; |
| 999 | model_blob(m, want); |
| 1000 | if (memcmp(model, want, sizeof want) != 0) return AG_FUSION_E_IDENTITY; |
| 1001 | static uint8_t got[AG_REALIZATION_MAX_BYTES]; |
| 1002 | size_t n = rx_graph_realization_encode(&m->realization, g_blob, sizeof g_blob); |
| 1003 | if (rx_gen_read_blob(store, m->generation, "realization", got, sizeof got, &len) != RX_GEN_OK || |
| 1004 | len != n || memcmp(got, g_blob, n) != 0) |
| 1005 | return AG_FUSION_E_IDENTITY; |
| 1006 | uint32_t k = 0; |
| 1007 | while (k < skills->n_meta && skills->meta[k].id != m->id) k++; |
| 1008 | if (k >= AG_MAX_SKILLS) return AG_E_FULL; |
| 1009 | skills->meta[k].id = m->id; |
| 1010 | skills->meta[k].r = &m->realization; |
| 1011 | if (k == skills->n_meta) skills->n_meta++; |
| 1012 | m->state = AG_MS_PUBLISHED; |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | /* ---- use ---- */ |
| 1017 | |
| 1018 | typedef struct { |
| 1019 | const RxWorld *w; |
| 1020 | const uint8_t *sig; |
| 1021 | AgFusionSite *sites; |
| 1022 | uint32_t max, n; |
| 1023 | uint64_t taken; |
| 1024 | } FindCtx; |
| 1025 | |
| 1026 | static __thread const AgGraph *find_graph; |
| 1027 | |
| 1028 | static int find_cb(void *vc, uint64_t S, uint32_t exit) { |
| 1029 | FindCtx *c = vc; |
| 1030 | if (c->n >= c->max) return 1; |
| 1031 | if (S & c->taken) return 0; /* sites do not overlap */ |
| 1032 | static __thread AgMetaProgram p; |
| 1033 | AgFusionSite site; |
| 1034 | if (extract(find_graph, c->w, S, exit, &p, &site) != X_OK) return 0; |
| 1035 | if (memcmp(p.signature, c->sig, 32) != 0) return 0; |
| 1036 | c->sites[c->n++] = site; |
| 1037 | c->taken |= S; |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | uint32_t rx_fusion_find(const AgGraph *g, const RxWorld *w, const uint8_t signature[32], |
| 1042 | AgFusionSite *sites, uint32_t max) { |
| 1043 | FindCtx c = { w, signature, sites, max, 0, 0 }; |
| 1044 | find_graph = g; |
| 1045 | enumerate(g, find_cb, &c); |
| 1046 | return c.n; |
| 1047 | } |
| 1048 | |
| 1049 | /* Fuse every site of one MetaSkill, checking authority stays exactly the |
| 1050 | * union of the steps'. */ |
| 1051 | static int fuse_all(AgGraph *g, RxWorld *w, const AgMetaSkill *m, AgFusionReport *rep) { |
| 1052 | for (uint32_t round = 0; round < AG_FUSION_MAX_APPLIED; round++) { |
| 1053 | AgFusionSite site; |
| 1054 | if (rx_fusion_find(g, w, m->reference.signature, &site, 1) == 0) return 0; |
| 1055 | /* Authority the steps needed. */ |
| 1056 | AgAuthReq want[AG_MAX_REQS]; |
| 1057 | uint32_t nw = 0; |
| 1058 | for (uint32_t i = 0; i < g->n_auth; i++) |
| 1059 | if (bit(site.members, g->auth[i].node)) { |
| 1060 | uint32_t k = 0; |
| 1061 | while (k < nw && want[k].resource != g->auth[i].resource) k++; |
| 1062 | if (k == nw) want[nw++] = (AgAuthReq){ 0, g->auth[i].resource, 0 }; |
| 1063 | want[k].rights |= g->auth[i].rights; |
| 1064 | } |
| 1065 | int mi = rx_graph_fuse(g, site.members, site.exit, m->id, &m->realization, site.ext, site.n_ext, |
| 1066 | site.obj, site.n_obj); |
| 1067 | if (mi < 0) return mi; |
| 1068 | int rc = rx_graph_validate(g, w); |
| 1069 | if (rc != 0) return rc; |
| 1070 | /* ... and what the fused node needs now: the same, no more, no less. */ |
| 1071 | uint32_t have = 0; |
| 1072 | for (uint32_t i = 0; i < g->n_auth; i++) { |
| 1073 | if (g->auth[i].node != (uint32_t)mi) continue; |
| 1074 | int match = 0; |
| 1075 | for (uint32_t k = 0; k < nw; k++) |
| 1076 | match |= want[k].resource == g->auth[i].resource && want[k].rights == g->auth[i].rights; |
| 1077 | if (!match) return AG_FUSION_E_AUTHORITY; |
| 1078 | have++; |
| 1079 | } |
| 1080 | if (have != nw) return AG_FUSION_E_AUTHORITY; |
| 1081 | if (rep && rep->applied < AG_FUSION_MAX_APPLIED) { |
| 1082 | rep->fused[rep->applied].skill = m->id; |
| 1083 | rep->fused[rep->applied].steps = site.n_steps; |
| 1084 | rep->fused[rep->applied].node = (uint32_t)mi; |
| 1085 | rep->applied++; |
| 1086 | } |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int published(const AgMetaSkill *m, const AgSkillTable *sk) { |
| 1092 | if (m->state != AG_MS_PUBLISHED) return 0; |
| 1093 | for (uint32_t i = 0; i < sk->n_meta && i < AG_MAX_SKILLS; i++) |
| 1094 | if (sk->meta[i].id == m->id && sk->meta[i].r == &m->realization) return 1; |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | int rx_fusion_apply(AgGraph *g, RxWorld *w, const AgFusionLibrary *lib, const AgSkillTable *sk, |
| 1099 | AgFusionReport *rep) { |
| 1100 | AgFusionReport dummy; |
| 1101 | if (!rep) rep = &dummy; |
| 1102 | memset(rep, 0, sizeof *rep); |
| 1103 | for (uint32_t i = 0; lib && i < lib->n && i < AG_FUSION_MAX_LIBRARY; i++) { |
| 1104 | const AgMetaSkill *m = lib->m[i]; |
| 1105 | if (!m) continue; |
| 1106 | if (!sk || !published(m, sk)) { |
| 1107 | rep->refused_unpublished++; /* never silently: not published, not used */ |
| 1108 | continue; |
| 1109 | } |
| 1110 | int rc = fuse_all(g, w, m, rep); |
| 1111 | if (rc != 0) return rep->verdict = rc; |
| 1112 | } |
| 1113 | return rep->verdict = 0; |
| 1114 | } |
| 1115 | |
| 1116 | int rx_fusion_trial(const AgGraph *g, RxWorld *w, const AgMetaSkill *m, const AgSkillTable *skills, |
| 1117 | AgSkillTable *trial, AgGraph *out, uint32_t *applied) { |
| 1118 | if (!m || (m->state != AG_MS_VERIFIED && m->state != AG_MS_MEASURED)) return AG_FUSION_E_STATE; |
| 1119 | *trial = skills ? *skills : (AgSkillTable){ 0 }; |
| 1120 | trial->n_meta = 0; |
| 1121 | trial->meta[trial->n_meta++] = (typeof(trial->meta[0])){ m->id, &m->realization }; |
| 1122 | *out = *g; |
| 1123 | AgFusionReport rep; |
| 1124 | memset(&rep, 0, sizeof rep); |
| 1125 | int rc = fuse_all(out, w, m, &rep); |
| 1126 | if (applied) *applied = rep.applied; |
| 1127 | return rc; |
| 1128 | } |
| 1129 | |
| 1130 | int rx_fusion_compile(const AgGoal *goal, RxWorld *w, const AgCapTable *caps, |
| 1131 | const AgConstraints *cons, const AgLibrary *procedures, |
| 1132 | const AgFusionLibrary *lib, const AgSkillTable *skills, AgGraph *out, |
| 1133 | AgReport *rep, AgFusionReport *frep) { |
| 1134 | int rc = rx_graph_compile(goal, w, caps, cons, procedures, 1, out, rep); |
| 1135 | if (rc != AG_OK_READY) return rc; |
| 1136 | rc = rx_fusion_apply(out, w, lib, skills, frep); |
| 1137 | if (rc != 0) return rep->verdict = rc; |
| 1138 | return rep->verdict = rx_graph_report_needs(out, w, caps, cons, rep); |
| 1139 | } |