Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/runtime/rx_graph.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / runtime / rx_graph.c` 1962 lines ยท 80018 bytes Raw Blame History 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_graph.c -- Omega action graph IR: build, check, compile, optimize, 19 * lower onto resident reactions, and the sequential reference. 20 * 21 * One function gives a node its meaning (semantics()). The lowered reaction 22 * and the reference both call it, so the two executions differ only in who 23 * decides when a node runs: the world's readiness, or a fixed topological 24 * order. 25 */ 26 #include "rx_graph.h" 27 28 #include "omega_core.h" 29 #include "sha256.h" 30 31 #include 32 #include 33 34 /* ---- small helpers ---- */ 35 36 static void put8(sha256_ctx *c, uint8_t v) { sha256_update(c, &v, 1); } 37 38 static void put32(sha256_ctx *c, uint32_t v) { 39 uint8_t b[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; 40 sha256_update(c, b, 4); 41 } 42 43 static void put64(sha256_ctx *c, uint64_t v) { 44 put32(c, (uint32_t)(v >> 32)); 45 put32(c, (uint32_t)v); 46 } 47 48 static uint64_t low64(const uint8_t d[32]) { 49 uint64_t v = 0; 50 for (int i = 0; i < 8; i++) v = (v << 8) | d[i]; 51 return v; 52 } 53 54 /* Eight bytes at d, most significant first. */ 55 static uint64_t word64(const uint8_t *d) { 56 uint64_t v = 0; 57 for (int i = 0; i < 8; i++) v = (v << 8) | d[i]; 58 return v; 59 } 60 61 static uint64_t mono_ns(void) { 62 struct timespec ts; 63 clock_gettime(CLOCK_MONOTONIC, &ts); 64 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 65 } 66 67 /* Real work for cost_us: the node holds its worker for that long. */ 68 static void busy_us(uint32_t us) { 69 if (!us) return; 70 uint64_t end = mono_ns() + (uint64_t)us * 1000ull; 71 volatile uint64_t x = 0; 72 while (mono_ns() < end) x++; 73 } 74 75 const char *rx_graph_kind_name(AgKind k) { 76 static const char *n[AG_KIND_COUNT] = { 77 "?", "ag.const", "ag.pure", "ag.world_read", "ag.world_publish", "ag.recall", 78 "ag.cap_resolve", "ag.physical", "ag.skill", "ag.effect_propose", "ag.effect_perform", 79 "ag.verify", "ag.branch", "ag.join", "ag.retry", "ag.meta" 80 }; 81 return (unsigned)k < AG_KIND_COUNT ? n[k] : "?"; 82 } 83 84 static int is_boundary(AgKind k) { return k == AG_WORLD_PUBLISH || k == AG_EFFECT_PERFORM; } 85 static int reads_world(AgKind k) { 86 return k == AG_WORLD_READ || k == AG_RECALL || k == AG_EFFECT_PERFORM; 87 } 88 static int has_target(AgKind k) { return reads_world(k) || k == AG_WORLD_PUBLISH; } 89 90 /* World objects a node touches: its target, or a META node's object slots. */ 91 static uint32_t node_objs(const AgNode *x, RxObjRef out[AG_META_MAX_OBJ]) { 92 if (x->kind == AG_META) { 93 uint32_t k = x->n_mobj < AG_META_MAX_OBJ ? x->n_mobj : AG_META_MAX_OBJ; 94 for (uint32_t i = 0; i < k; i++) out[i] = x->mobj[i]; 95 return k; 96 } 97 if (!has_target(x->kind)) return 0; 98 out[0] = x->obj; 99 return 1; 100 } 101 102 static int touches(const AgNode *x, uint32_t obj_id) { 103 RxObjRef o[AG_META_MAX_OBJ]; 104 uint32_t k = node_objs(x, o); 105 for (uint32_t i = 0; i < k; i++) 106 if (o[i].id == obj_id) return 1; 107 return 0; 108 } 109 110 uint64_t rx_graph_chain(uint64_t chain, uint64_t run, uint64_t value) { 111 sha256_ctx c; 112 uint8_t d[32]; 113 sha256_init(&c); 114 put64(&c, chain); 115 put64(&c, run); 116 put64(&c, value); 117 sha256_final(&c, d); 118 return low64(d); 119 } 120 121 uint64_t rx_graph_evidence_word(const AgNode *n, uint64_t run, uint32_t status, uint64_t value, 122 const uint64_t *in, uint32_t n_in) { 123 sha256_ctx c; 124 uint8_t d[32]; 125 sha256_init(&c); 126 sha256_update(&c, n->id, 32); 127 put64(&c, run); 128 put32(&c, status); 129 put64(&c, value); 130 put32(&c, n_in); 131 for (uint32_t i = 0; i < n_in; i++) put64(&c, in[i]); 132 sha256_final(&c, d); 133 return low64(d) | 1u; 134 } 135 136 /* ---- building ---- */ 137 138 void rx_graph_init(AgGraph *g, uint32_t subject) { 139 memset(g, 0, sizeof *g); 140 g->subject = subject; 141 } 142 143 int rx_graph_node(AgGraph *g, AgKind kind, AgType out) { 144 if (g->n_nodes >= AG_MAX_NODES) return AG_E_FULL; 145 uint32_t i = g->n_nodes++; 146 AgNode *n = &g->nodes[i]; 147 memset(n, 0, sizeof *n); 148 n->kind = kind; 149 n->out_type = out; 150 n->alive = 1; 151 n->origin = i; 152 return (int)i; 153 } 154 155 int rx_graph_data(AgGraph *g, uint32_t from, uint32_t to, uint32_t port, uint32_t mode) { 156 if (from >= g->n_nodes || to >= g->n_nodes || port >= AG_MAX_IN || g->n_data >= AG_MAX_EDGES) 157 return AG_E_ARG; 158 g->data[g->n_data++] = (AgDataEdge){ (uint16_t)from, (uint16_t)to, (uint8_t)port, 159 (uint8_t)mode, g->nodes[from].out_type }; 160 return 0; 161 } 162 163 static int add_dep(AgGraph *g, uint32_t from, uint32_t to, uint8_t kind, uint8_t pol) { 164 if (from >= g->n_nodes || to >= g->n_nodes || g->n_deps >= AG_MAX_DEPS) return AG_E_ARG; 165 g->deps[g->n_deps++] = (AgDep){ (uint16_t)from, (uint16_t)to, kind, pol }; 166 return 0; 167 } 168 169 int rx_graph_guard(AgGraph *g, uint32_t branch, uint32_t node, uint32_t polarity) { 170 return add_dep(g, branch, node, AG_DEP_GUARD, polarity ? 1 : 0); 171 } 172 173 int rx_graph_order(AgGraph *g, uint32_t before, uint32_t after) { 174 return add_dep(g, before, after, AG_DEP_ORDER, 0); 175 } 176 177 int rx_graph_need_authority(AgGraph *g, uint32_t node, uint64_t resource, uint32_t rights) { 178 if (node >= g->n_nodes) return AG_E_ARG; 179 for (uint32_t i = 0; i < g->n_auth; i++) 180 if (g->auth[i].node == node && g->auth[i].resource == resource) { 181 g->auth[i].rights |= rights; 182 return 0; 183 } 184 if (g->n_auth >= AG_MAX_REQS) return AG_E_FULL; 185 g->auth[g->n_auth++] = (AgAuthReq){ (uint16_t)node, resource, rights }; 186 return 0; 187 } 188 189 int rx_graph_need_resource(AgGraph *g, uint32_t node, const RxResourceNeed *need) { 190 if (node >= g->n_nodes || g->n_res >= AG_MAX_REQS) return AG_E_ARG; 191 g->res[g->n_res++] = (AgResReq){ (uint16_t)node, *need }; 192 return 0; 193 } 194 195 static int add_cond(AgCond *c, uint32_t *n, uint32_t node, uint32_t status) { 196 if (*n >= AG_MAX_CONDS) return AG_E_FULL; 197 c[(*n)++] = (AgCond){ (uint16_t)node, (uint8_t)status }; 198 return 0; 199 } 200 201 int rx_graph_success(AgGraph *g, uint32_t node, uint32_t status) { 202 return node < g->n_nodes ? add_cond(g->success, &g->n_success, node, status) : AG_E_ARG; 203 } 204 205 int rx_graph_failure(AgGraph *g, uint32_t node, uint32_t status) { 206 return node < g->n_nodes ? add_cond(g->failure, &g->n_failure, node, status) : AG_E_ARG; 207 } 208 209 int rx_graph_evidence(AgGraph *g, uint32_t node) { 210 if (node >= g->n_nodes) return AG_E_ARG; 211 for (uint32_t i = 0; i < g->n_evidence; i++) 212 if (g->evidence[i] == node) return 0; 213 g->evidence[g->n_evidence++] = (uint16_t)node; 214 return 0; 215 } 216 217 static int is_evidence(const AgGraph *g, uint32_t n) { 218 for (uint32_t i = 0; i < g->n_evidence; i++) 219 if (g->evidence[i] == n) return 1; 220 return 0; 221 } 222 223 static int in_conds(const AgGraph *g, uint32_t n) { 224 for (uint32_t i = 0; i < g->n_success; i++) 225 if (g->success[i].node == n) return 1; 226 for (uint32_t i = 0; i < g->n_failure; i++) 227 if (g->failure[i].node == n) return 1; 228 return 0; 229 } 230 231 /* Nodes that no pass may merge, fuse into another, or remove. */ 232 static int pinned(const AgGraph *g, uint32_t n) { 233 return is_boundary(g->nodes[n].kind) || is_evidence(g, n) || in_conds(g, n); 234 } 235 236 /* ---- structure ---- */ 237 238 /* Deterministic topological order over live nodes (smallest index first). 239 * Returns the count, or AG_E_CYCLE. */ 240 static int topo(const AgGraph *g, uint16_t order[AG_MAX_NODES]); 241 242 int rx_graph_topo(const AgGraph *g, uint16_t order[AG_MAX_NODES]) { return topo(g, order); } 243 244 static int topo(const AgGraph *g, uint16_t order[AG_MAX_NODES]) { 245 uint32_t indeg[AG_MAX_NODES] = { 0 }; 246 uint32_t live = 0; 247 for (uint32_t i = 0; i < g->n_nodes; i++) live += g->nodes[i].alive; 248 for (uint32_t e = 0; e < g->n_data; e++) indeg[g->data[e].to]++; 249 for (uint32_t e = 0; e < g->n_deps; e++) indeg[g->deps[e].to]++; 250 uint8_t done[AG_MAX_NODES] = { 0 }; 251 uint32_t k = 0; 252 while (k < live) { 253 int pick = -1; 254 for (uint32_t i = 0; i < g->n_nodes; i++) 255 if (g->nodes[i].alive && !done[i] && indeg[i] == 0) { pick = (int)i; break; } 256 if (pick < 0) return AG_E_CYCLE; 257 done[pick] = 1; 258 order[k++] = (uint16_t)pick; 259 for (uint32_t e = 0; e < g->n_data; e++) 260 if (g->data[e].from == pick) indeg[g->data[e].to]--; 261 for (uint32_t e = 0; e < g->n_deps; e++) 262 if (g->deps[e].from == pick) indeg[g->deps[e].to]--; 263 } 264 return (int)k; 265 } 266 267 uint64_t rx_graph_ancestors(const AgGraph *g, uint32_t node) { 268 uint64_t anc = 0, frontier = 1ull << node; 269 while (frontier) { 270 uint64_t next = 0; 271 for (uint32_t e = 0; e < g->n_data; e++) 272 if (frontier & (1ull << g->data[e].to)) next |= 1ull << g->data[e].from; 273 for (uint32_t e = 0; e < g->n_deps; e++) 274 if (frontier & (1ull << g->deps[e].to)) next |= 1ull << g->deps[e].from; 275 next &= ~anc; 276 anc |= next; 277 frontier = next; 278 } 279 return anc; 280 } 281 282 static uint32_t n_inputs(const AgGraph *g, uint32_t n, uint32_t *ports) { 283 uint32_t k = 0; 284 for (uint32_t e = 0; e < g->n_data; e++) 285 if (g->data[e].to == n) { 286 if (ports) ports[k] = e; 287 k++; 288 } 289 return k; 290 } 291 292 /* Data edges into n sorted by port (edge indices). */ 293 static uint32_t inputs_by_port(const AgGraph *g, uint32_t n, uint32_t out[AG_MAX_IN]) { 294 uint32_t k = 0; 295 for (uint32_t p = 0; p < AG_MAX_IN; p++) 296 for (uint32_t e = 0; e < g->n_data; e++) 297 if (g->data[e].to == n && g->data[e].port == p && k < AG_MAX_IN) out[k++] = e; 298 return k; 299 } 300 301 static int dep_of(const AgGraph *g, uint32_t n, uint8_t kind) { 302 for (uint32_t e = 0; e < g->n_deps; e++) 303 if (g->deps[e].to == n && g->deps[e].kind == kind) return (int)e; 304 return -1; 305 } 306 307 static int type_ok(const AgGraph *g, uint32_t n) { 308 const AgNode *x = &g->nodes[n]; 309 uint32_t e[AG_MAX_IN]; 310 uint32_t k = inputs_by_port(g, n, e); 311 if (k != n_inputs(g, n, NULL)) return 0; /* duplicate or out-of-range ports */ 312 for (uint32_t i = 0; i < k; i++) 313 if (g->data[e[i]].port != i) return 0; /* ports are dense from 0 */ 314 int numeric_in = 1; 315 for (uint32_t i = 0; i < k; i++) { 316 const AgDataEdge *d = &g->data[e[i]]; 317 if (d->mode == AG_EDGE_ON_FAIL) continue; 318 if (d->type != AG_T_U64 && d->type != AG_T_BOOL) numeric_in = 0; 319 } 320 int numeric_out = x->out_type == AG_T_U64 || x->out_type == AG_T_BOOL; 321 switch (x->kind) { 322 case AG_CONST: case AG_CAP_RESOLVE: case AG_WORLD_READ: 323 return k == 0 && numeric_out; 324 case AG_RECALL: 325 return k <= 1 && numeric_in && x->out_type == AG_T_U64; 326 case AG_PURE: 327 return k >= 1 && k <= 2 && numeric_in && numeric_out; 328 case AG_PHYSICAL: case AG_SKILL: case AG_RETRY: 329 return k >= 1 && numeric_in && x->out_type == AG_T_U64; 330 case AG_WORLD_PUBLISH: 331 return k == 1 && numeric_in && x->out_type == AG_T_RECEIPT; 332 case AG_EFFECT_PROPOSE: 333 return k == 1 && numeric_in && x->out_type == AG_T_PROPOSAL; 334 case AG_EFFECT_PERFORM: 335 return k == 2 && g->data[e[0]].type == AG_T_PROPOSAL && 336 g->data[e[1]].type == AG_T_VERDICT && x->out_type == AG_T_RECEIPT && 337 g->data[e[0]].mode == AG_EDGE_DATA && g->data[e[1]].mode == AG_EDGE_DATA; 338 case AG_VERIFY: 339 return k == 1 && numeric_in && x->out_type == AG_T_VERDICT; 340 case AG_BRANCH: 341 return k == 1 && numeric_in && x->out_type == AG_T_BOOL; 342 case AG_JOIN: 343 if (k < 2) return 0; 344 for (uint32_t i = 0; i < k; i++) 345 if (g->data[e[i]].type != x->out_type || g->data[e[i]].mode != AG_EDGE_DATA) return 0; 346 return 1; 347 case AG_META: 348 /* Inputs match the realization's input contract, port by port. */ 349 if (k > AG_MAX_IN || x->n_mobj > AG_META_MAX_OBJ) return 0; 350 for (uint32_t i = 0; i < k; i++) 351 if (x->in_type[i] == AG_T_NONE || g->data[e[i]].type != x->in_type[i]) return 0; 352 for (uint32_t i = k; i < AG_MAX_IN; i++) 353 if (x->in_type[i] != AG_T_NONE) return 0; 354 return x->out_type != AG_T_NONE; 355 default: 356 return 0; 357 } 358 } 359 360 /* What must hold for node n to end OK: nodes that must be OK, nodes that 361 * must have FAILED, branches that must be true / false. */ 362 typedef struct { uint64_t ok, fail, gt, gf; } Req; 363 364 static void requirements(const AgGraph *g, const uint16_t *ord, uint32_t cnt, Req *r) { 365 memset(r, 0, sizeof(Req) * AG_MAX_NODES); 366 for (uint32_t t = 0; t < cnt; t++) { 367 uint32_t n = ord[t]; 368 Req q = { 0, 0, 0, 0 }; 369 if (g->nodes[n].kind == AG_JOIN) { 370 int first = 1; 371 for (uint32_t e = 0; e < g->n_data; e++) { 372 if (g->data[e].to != n) continue; 373 uint32_t s = g->data[e].from; 374 Req a = r[s]; 375 a.ok |= 1ull << s; 376 if (first) { q = a; first = 0; } 377 else { q.ok &= a.ok; q.fail &= a.fail; q.gt &= a.gt; q.gf &= a.gf; } 378 } 379 } else { 380 for (uint32_t e = 0; e < g->n_data; e++) { 381 if (g->data[e].to != n) continue; 382 uint32_t s = g->data[e].from; 383 if (g->data[e].mode == AG_EDGE_ON_FAIL) { q.fail |= 1ull << s; continue; } 384 q.ok |= r[s].ok | (1ull << s); 385 q.fail |= r[s].fail; 386 q.gt |= r[s].gt; 387 q.gf |= r[s].gf; 388 } 389 } 390 for (uint32_t e = 0; e < g->n_deps; e++) { 391 if (g->deps[e].to != n || g->deps[e].kind != AG_DEP_GUARD) continue; 392 uint32_t b = g->deps[e].from; 393 q.ok |= r[b].ok | (1ull << b); 394 q.fail |= r[b].fail; 395 q.gt |= r[b].gt; 396 q.gf |= r[b].gf; 397 if (g->deps[e].polarity) q.gt |= 1ull << b; 398 else q.gf |= 1ull << b; 399 } 400 r[n] = q; 401 } 402 } 403 404 static int exclusive(const Req *a, const Req *b) { 405 return ((a->gt & b->gf) | (a->gf & b->gt) | (a->ok & b->fail) | (a->fail & b->ok)) != 0; 406 } 407 408 /* Reaction dependencies a lowered node uses (rx_world RX_MAX_DEPS). */ 409 static uint32_t dep_budget(const AgGraph *g, uint32_t n) { 410 uint64_t srcs = 0; 411 for (uint32_t e = 0; e < g->n_data; e++) 412 if (g->data[e].to == n) srcs |= 1ull << g->data[e].from; 413 for (uint32_t e = 0; e < g->n_deps; e++) 414 if (g->deps[e].to == n) srcs |= 1ull << g->deps[e].from; 415 uint32_t k = (uint32_t)__builtin_popcountll(srcs); 416 k += 2; /* run token + own cell */ 417 if (g->nodes[n].kind == AG_META) { 418 k += g->nodes[n].n_mobj; /* the World objects its steps read */ 419 if (g->nodes[n].n_mobj) k++; /* room for an authority slot wake */ 420 return k; 421 } 422 if (reads_world(g->nodes[n].kind)) k++; /* the World object */ 423 if (has_target(g->nodes[n].kind)) k++; /* room for an authority slot wake */ 424 return k; 425 } 426 427 static void derive_requirements(AgGraph *g, const RxWorld *w) { 428 for (uint32_t n = 0; n < g->n_nodes; n++) { 429 AgNode *x = &g->nodes[n]; 430 if (!x->alive) continue; 431 if (x->kind == AG_VERIFY || is_boundary(x->kind)) rx_graph_evidence(g, n); 432 if (x->kind == AG_META) { 433 /* A fused node always owes its step evidence, and reads its objects. */ 434 rx_graph_evidence(g, n); 435 for (uint32_t i = 0; w && i < x->n_mobj && i < AG_META_MAX_OBJ; i++) 436 if (x->mobj[i].id < RX_MAX_OBJECTS) 437 rx_graph_need_authority(g, n, w->objects[x->mobj[i].id].resource, RX_RIGHT_READ); 438 continue; 439 } 440 if (!w || !has_target(x->kind) || x->obj.id >= RX_MAX_OBJECTS) continue; 441 uint64_t res = w->objects[x->obj.id].resource; 442 uint32_t rights = RX_RIGHT_READ; 443 if (x->kind == AG_WORLD_PUBLISH) rights = RX_RIGHT_WRITE; 444 if (x->kind == AG_EFFECT_PERFORM) rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; 445 rx_graph_need_authority(g, n, res, rights); 446 } 447 } 448 449 int rx_graph_validate(AgGraph *g, const RxWorld *w) { 450 uint16_t ord[AG_MAX_NODES]; 451 int cnt = topo(g, ord); 452 if (cnt < 0) return cnt; 453 for (uint32_t e = 0; e < g->n_data; e++) { 454 if (!g->nodes[g->data[e].from].alive || !g->nodes[g->data[e].to].alive) return AG_E_ARG; 455 g->data[e].type = g->nodes[g->data[e].from].out_type; 456 } 457 for (uint32_t n = 0; n < g->n_nodes; n++) { 458 if (!g->nodes[n].alive) continue; 459 if (!type_ok(g, n)) return AG_E_TYPE; 460 uint32_t guards = 0, orders = 0; 461 for (uint32_t e = 0; e < g->n_deps; e++) { 462 if (g->deps[e].to != n) continue; 463 if (g->deps[e].kind == AG_DEP_GUARD) { 464 guards++; 465 if (g->nodes[g->deps[e].from].out_type != AG_T_BOOL) return AG_E_TYPE; 466 } else { 467 orders++; 468 } 469 } 470 (void)orders; 471 if (guards > 1) return AG_E_DEP_BUDGET; 472 if (dep_budget(g, n) > RX_MAX_DEPS) return AG_E_DEP_BUDGET; 473 } 474 derive_requirements(g, w); 475 476 /* Effect boundaries: one total order, recorded in that order. */ 477 g->n_effects = 0; 478 for (int t = 0; t < cnt; t++) 479 if (is_boundary(g->nodes[ord[t]].kind)) g->effects[g->n_effects++] = ord[t]; 480 for (uint32_t i = 1; i < g->n_effects; i++) 481 if (!(rx_graph_ancestors(g, g->effects[i]) & (1ull << g->effects[i - 1]))) 482 return AG_E_EFFECT_ORDER; 483 /* A node that reads an object a boundary writes must be ordered against 484 * that boundary; otherwise what it sees depends on worker timing. */ 485 for (uint32_t i = 0; i < g->n_effects; i++) { 486 uint32_t b = g->effects[i]; 487 for (uint32_t n = 0; n < g->n_nodes; n++) { 488 if (n == b || !g->nodes[n].alive || !touches(&g->nodes[n], g->nodes[b].obj.id)) 489 continue; 490 if (!(rx_graph_ancestors(g, n) & (1ull << b)) && !(rx_graph_ancestors(g, b) & (1ull << n))) 491 return AG_E_EFFECT_ORDER; 492 } 493 } 494 495 /* Joins: every pair of arms mutually exclusive. */ 496 Req r[AG_MAX_NODES]; 497 requirements(g, ord, (uint32_t)cnt, r); 498 for (uint32_t n = 0; n < g->n_nodes; n++) { 499 if (!g->nodes[n].alive || g->nodes[n].kind != AG_JOIN) continue; 500 uint32_t e[AG_MAX_IN]; 501 uint32_t k = inputs_by_port(g, n, e); 502 for (uint32_t i = 0; i < k; i++) 503 for (uint32_t j = i + 1; j < k; j++) { 504 Req a = r[g->data[e[i]].from], b = r[g->data[e[j]].from]; 505 a.ok |= 1ull << g->data[e[i]].from; 506 b.ok |= 1ull << g->data[e[j]].from; 507 if (!exclusive(&a, &b)) return AG_E_JOIN; 508 } 509 } 510 rx_graph_identify(g); 511 return 0; 512 } 513 514 /* ---- identity ---- */ 515 516 void rx_graph_identify(AgGraph *g) { 517 uint16_t ord[AG_MAX_NODES]; 518 int cnt = topo(g, ord); 519 if (cnt < 0) return; 520 for (int t = 0; t < cnt; t++) { 521 uint32_t n = ord[t]; 522 AgNode *x = &g->nodes[n]; 523 sha256_ctx c; 524 sha256_init(&c); 525 sha256_update(&c, (const uint8_t *)"OMEGA_ACTION_NODE_V1", 20); 526 put32(&c, x->kind); 527 put32(&c, x->out_type); 528 put32(&c, x->op); 529 put64(&c, x->imm); 530 put64(&c, x->imm2); 531 put32(&c, x->param); 532 put32(&c, has_target(x->kind) ? x->obj.id : 0); 533 put32(&c, has_target(x->kind) ? x->obj.generation : 0); 534 put32(&c, x->field); 535 put32(&c, x->cost_us); 536 put32(&c, x->n_fused); 537 for (uint32_t i = 0; i < x->n_fused; i++) { 538 put32(&c, x->fused[i].op); 539 put64(&c, x->fused[i].imm); 540 } 541 if (x->kind == AG_META) { 542 put32(&c, x->n_mobj); 543 for (uint32_t i = 0; i < x->n_mobj && i < AG_META_MAX_OBJ; i++) { 544 put32(&c, x->mobj[i].id); 545 put32(&c, x->mobj[i].generation); 546 } 547 for (uint32_t i = 0; i < AG_MAX_IN; i++) put32(&c, x->in_type[i]); 548 } 549 uint32_t e[AG_MAX_IN]; 550 uint32_t k = inputs_by_port(g, n, e); 551 put32(&c, k); 552 for (uint32_t i = 0; i < k; i++) { 553 put8(&c, g->data[e[i]].mode); 554 sha256_update(&c, g->nodes[g->data[e[i]].from].id, 32); 555 } 556 /* Control dependencies, sorted by (kind, polarity, source identity). */ 557 uint32_t d[AG_MAX_DEPS], nd = 0; 558 for (uint32_t j = 0; j < g->n_deps; j++) 559 if (g->deps[j].to == n) d[nd++] = j; 560 for (uint32_t a = 1; a < nd; a++) 561 for (uint32_t b = a; b > 0; b--) { 562 const AgDep *p = &g->deps[d[b - 1]], *q = &g->deps[d[b]]; 563 int cmp = p->kind != q->kind ? p->kind - q->kind 564 : p->polarity != q->polarity ? p->polarity - q->polarity 565 : memcmp(g->nodes[p->from].id, g->nodes[q->from].id, 32); 566 if (cmp <= 0) break; 567 uint32_t tmp = d[b]; d[b] = d[b - 1]; d[b - 1] = tmp; 568 } 569 put32(&c, nd); 570 for (uint32_t j = 0; j < nd; j++) { 571 put8(&c, g->deps[d[j]].kind); 572 put8(&c, g->deps[d[j]].polarity); 573 sha256_update(&c, g->nodes[g->deps[d[j]].from].id, 32); 574 } 575 /* A World read is the same read only between the same publications 576 * of that object: fold in every ancestor publication to it. */ 577 if (x->kind == AG_WORLD_READ || x->kind == AG_RECALL || x->kind == AG_META) { 578 uint64_t anc = rx_graph_ancestors(g, n); 579 for (int u = 0; u < cnt; u++) { 580 uint32_t p = ord[u]; 581 if ((anc & (1ull << p)) && g->nodes[p].kind == AG_WORLD_PUBLISH && 582 touches(x, g->nodes[p].obj.id)) 583 sha256_update(&c, g->nodes[p].id, 32); 584 } 585 } 586 /* A boundary is also its place in the effect chain. */ 587 if (is_boundary(x->kind)) { 588 uint32_t pos = 0; 589 uint64_t anc = rx_graph_ancestors(g, n); 590 for (uint32_t p = 0; p < g->n_nodes; p++) 591 if ((anc & (1ull << p)) && g->nodes[p].alive && is_boundary(g->nodes[p].kind)) pos++; 592 put32(&c, pos); 593 } 594 sha256_final(&c, x->id); 595 } 596 /* Graph digest: sorted node identities, then conditions and evidence by 597 * identity, then the principal. Independent of builder order. */ 598 uint8_t ids[AG_MAX_NODES][32]; 599 uint32_t m = 0; 600 for (uint32_t n = 0; n < g->n_nodes; n++) 601 if (g->nodes[n].alive) memcpy(ids[m++], g->nodes[n].id, 32); 602 for (uint32_t a = 1; a < m; a++) 603 for (uint32_t b = a; b > 0 && memcmp(ids[b - 1], ids[b], 32) > 0; b--) { 604 uint8_t t[32]; 605 memcpy(t, ids[b], 32); memcpy(ids[b], ids[b - 1], 32); memcpy(ids[b - 1], t, 32); 606 } 607 sha256_ctx c; 608 sha256_init(&c); 609 sha256_update(&c, (const uint8_t *)"OMEGA_ACTION_GRAPH_V1", 21); 610 put32(&c, g->subject); 611 put32(&c, m); 612 for (uint32_t i = 0; i < m; i++) sha256_update(&c, ids[i], 32); 613 /* Conditions and evidence as sorted (identity, tag) records. */ 614 uint8_t rec[3 * AG_MAX_CONDS + AG_MAX_NODES][33]; 615 uint32_t nr = 0; 616 for (uint32_t i = 0; i < g->n_success; i++) { 617 memcpy(rec[nr], g->nodes[g->success[i].node].id, 32); 618 rec[nr++][32] = (uint8_t)(0x10 | g->success[i].status); 619 } 620 for (uint32_t i = 0; i < g->n_failure; i++) { 621 memcpy(rec[nr], g->nodes[g->failure[i].node].id, 32); 622 rec[nr++][32] = (uint8_t)(0x20 | g->failure[i].status); 623 } 624 for (uint32_t i = 0; i < g->n_evidence; i++) { 625 memcpy(rec[nr], g->nodes[g->evidence[i]].id, 32); 626 rec[nr++][32] = 0x30; 627 } 628 for (uint32_t a = 1; a < nr; a++) 629 for (uint32_t b = a; b > 0 && memcmp(rec[b - 1], rec[b], 33) > 0; b--) { 630 uint8_t t[33]; 631 memcpy(t, rec[b], 33); memcpy(rec[b], rec[b - 1], 33); memcpy(rec[b - 1], t, 33); 632 } 633 put32(&c, nr); 634 for (uint32_t i = 0; i < nr; i++) sha256_update(&c, rec[i], 33); 635 sha256_final(&c, g->digest); 636 } 637 638 /* ---- semantics (shared by the lowered reaction and the reference) ---- */ 639 640 typedef struct { 641 uint32_t n_in; 642 uint64_t v[AG_MAX_IN]; 643 uint8_t s[AG_MAX_IN]; 644 uint8_t mode[AG_MAX_IN]; 645 int has_guard; 646 uint8_t guard_s, guard_pol; 647 uint64_t guard_v; 648 int has_order; 649 uint8_t order_s; 650 uint64_t world[RX_MAX_FIELDS]; 651 uint64_t mworld[AG_META_MAX_OBJ][RX_MAX_FIELDS]; /* META: its object slots */ 652 } AgIn; 653 654 typedef struct { 655 uint8_t status; 656 uint64_t value; 657 uint32_t attempts; 658 int write; /* a boundary that performs its write */ 659 uint64_t steps; /* META: step evidence */ 660 uint64_t busy_us; /* META: work of the steps that ran */ 661 } AgOut; 662 663 static void meta_semantics(const AgNode *x, const AgSkillTable *sk, const AgIn *in, AgOut *o); 664 665 static const AgSkill *find_skill(const AgSkillTable *t, uint32_t id) { 666 for (uint32_t i = 0; t && i < t->n; i++) 667 if (t->skill[i].id == id) return &t->skill[i]; 668 return NULL; 669 } 670 671 static int pure_op(uint32_t op, uint64_t a, uint64_t b, uint64_t *out) { 672 if (op == OP_IDENTITY) { *out = a; return 0; } 673 if (op == OP_CONSTANT) { *out = b; return 0; } 674 return omega_eval_pure_binary_uint((OpCode)op, OVERFLOW_WRAP, 64, a, b, out); 675 } 676 677 static void semantics(const AgNode *x, const AgSkillTable *sk, const AgIn *in, AgOut *o) { 678 memset(o, 0, sizeof *o); 679 if (in->has_guard) { 680 if (in->guard_s == AG_FAILED) { o->status = AG_FAILED; return; } 681 if (in->guard_s == AG_SKIPPED || (in->guard_v != 0) != (in->guard_pol != 0)) { 682 o->status = AG_SKIPPED; 683 return; 684 } 685 } 686 if (in->has_order && in->order_s == AG_FAILED) { o->status = AG_FAILED; return; } 687 688 /* A fused node sees its inputs' statuses as they are: its steps apply 689 * the skip and failure rules themselves, each with its own edge mode. */ 690 if (x->kind == AG_META) { meta_semantics(x, sk, in, o); return; } 691 692 if (x->kind == AG_JOIN) { 693 int ok = -1, oks = 0, failed = 0; 694 for (uint32_t i = 0; i < in->n_in; i++) { 695 if (in->s[i] == AG_OK) { ok = (int)i; oks++; } 696 if (in->s[i] == AG_FAILED) failed = 1; 697 } 698 if (oks == 1) { o->status = AG_OK; o->value = in->v[ok]; } 699 else if (oks > 1) o->status = AG_FAILED; 700 else o->status = failed ? AG_FAILED : AG_SKIPPED; 701 return; 702 } 703 int skipped = 0, failed = 0; 704 for (uint32_t i = 0; i < in->n_in; i++) { 705 if (in->mode[i] == AG_EDGE_ON_FAIL) { 706 if (in->s[i] != AG_FAILED) skipped = 1; 707 continue; 708 } 709 if (in->s[i] == AG_SKIPPED) skipped = 1; 710 if (in->s[i] == AG_FAILED) failed = 1; 711 } 712 if (skipped) { o->status = AG_SKIPPED; return; } 713 if (failed) { o->status = AG_FAILED; return; } 714 715 uint64_t a = in->n_in > 0 && in->mode[0] == AG_EDGE_DATA ? in->v[0] : 0; 716 uint64_t v = 0; 717 o->status = AG_OK; 718 switch (x->kind) { 719 case AG_CONST: case AG_CAP_RESOLVE: 720 v = x->imm; 721 break; 722 case AG_PURE: { 723 uint64_t b = in->n_in > 1 ? in->v[1] : x->imm; 724 if (pure_op(x->op, a, b, &v) != 0) { o->status = AG_FAILED; return; } 725 for (uint32_t i = 0; i < x->n_fused; i++) 726 if (pure_op(x->fused[i].op, v, x->fused[i].imm, &v) != 0) { 727 o->status = AG_FAILED; 728 return; 729 } 730 break; 731 } 732 case AG_WORLD_READ: 733 v = in->world[x->field & (RX_MAX_FIELDS - 1)]; 734 break; 735 case AG_RECALL: { 736 uint64_t key = in->n_in ? a : x->imm; 737 o->status = AG_FAILED; 738 for (uint32_t i = 0; i + 1 < RX_MAX_FIELDS; i += 2) 739 if (in->world[i] == key && key != 0) { v = in->world[i + 1]; o->status = AG_OK; break; } 740 break; 741 } 742 case AG_PHYSICAL: { 743 /* Deterministic mixing of the inputs: stands for work the body does. */ 744 uint64_t h = 0x9E3779B97F4A7C15ull ^ x->imm; 745 for (uint32_t i = 0; i < in->n_in; i++) { 746 h ^= in->v[i] + 0x9E3779B97F4A7C15ull + (h << 6) + (h >> 2); 747 h *= 0xBF58476D1CE4E5B9ull; 748 } 749 v = h ^ (h >> 31); 750 break; 751 } 752 case AG_SKILL: case AG_RETRY: { 753 const AgSkill *s = find_skill(sk, x->op); 754 if (!s) { o->status = AG_FAILED; return; } 755 uint32_t max = x->kind == AG_RETRY ? (uint32_t)(x->imm ? x->imm : 1) : 1; 756 int fail = 1; 757 for (uint32_t t = 0; t < max && fail; t++) { 758 fail = 0; 759 o->attempts = t + 1; 760 v = s->fn(in->v, in->n_in, t, &fail); 761 if (x->kind == AG_RETRY && t < x->imm2) fail = 1; /* the stand-in's leading failures */ 762 } 763 if (fail) { o->status = AG_FAILED; v = 0; } 764 break; 765 } 766 case AG_EFFECT_PROPOSE: 767 v = a; 768 break; 769 case AG_VERIFY: 770 if (a < x->imm || a > x->imm2) { o->status = AG_FAILED; return; } 771 v = 1; 772 break; 773 case AG_BRANCH: 774 v = a != 0; 775 break; 776 case AG_WORLD_PUBLISH: 777 v = a; 778 o->write = 1; 779 break; 780 case AG_EFFECT_PERFORM: 781 if (in->v[1] != 1) { o->status = AG_FAILED; return; } 782 v = in->world[0] + 1; /* receipt: the effect's sequence number */ 783 o->write = 1; 784 break; 785 default: 786 o->status = AG_FAILED; 787 return; 788 } 789 o->value = v; 790 } 791 792 /* ---- optimization ---- */ 793 794 static void redirect(AgGraph *g, uint32_t from, uint32_t to) { 795 for (uint32_t e = 0; e < g->n_data; e++) 796 if (g->data[e].from == from) g->data[e].from = (uint16_t)to; 797 for (uint32_t e = 0; e < g->n_deps; e++) 798 if (g->deps[e].from == from) g->deps[e].from = (uint16_t)to; 799 } 800 801 static void drop_edges_to(AgGraph *g, uint32_t n) { 802 uint32_t k = 0; 803 for (uint32_t e = 0; e < g->n_data; e++) 804 if (g->data[e].to != n) g->data[k++] = g->data[e]; 805 g->n_data = k; 806 } 807 808 static void drop_edges_of(AgGraph *g, uint32_t n) { 809 uint32_t k = 0; 810 for (uint32_t e = 0; e < g->n_data; e++) 811 if (g->data[e].to != n && g->data[e].from != n) g->data[k++] = g->data[e]; 812 g->n_data = k; 813 k = 0; 814 for (uint32_t e = 0; e < g->n_deps; e++) 815 if (g->deps[e].to != n && g->deps[e].from != n) g->deps[k++] = g->deps[e]; 816 g->n_deps = k; 817 } 818 819 static uint32_t consumers(const AgGraph *g, uint32_t n) { 820 uint32_t k = 0; 821 for (uint32_t e = 0; e < g->n_data; e++) k += g->data[e].from == n; 822 for (uint32_t e = 0; e < g->n_deps; e++) k += g->deps[e].from == n; 823 return k; 824 } 825 826 /* Remove dead nodes and renumber everything that names a node. */ 827 static void compact(AgGraph *g) { 828 int16_t map[AG_MAX_NODES]; 829 uint32_t k = 0; 830 for (uint32_t n = 0; n < g->n_nodes; n++) { 831 map[n] = g->nodes[n].alive ? (int16_t)k : -1; 832 if (g->nodes[n].alive) g->nodes[k++] = g->nodes[n]; 833 } 834 g->n_nodes = k; 835 uint32_t j = 0; 836 for (uint32_t e = 0; e < g->n_data; e++) 837 if (map[g->data[e].from] >= 0 && map[g->data[e].to] >= 0) { 838 g->data[j] = g->data[e]; 839 g->data[j].from = (uint16_t)map[g->data[e].from]; 840 g->data[j].to = (uint16_t)map[g->data[e].to]; 841 j++; 842 } 843 g->n_data = j; 844 j = 0; 845 for (uint32_t e = 0; e < g->n_deps; e++) 846 if (map[g->deps[e].from] >= 0 && map[g->deps[e].to] >= 0) { 847 g->deps[j] = g->deps[e]; 848 g->deps[j].from = (uint16_t)map[g->deps[e].from]; 849 g->deps[j].to = (uint16_t)map[g->deps[e].to]; 850 j++; 851 } 852 g->n_deps = j; 853 #define REMAP(arr, cnt, field) do { \ 854 uint32_t q = 0; \ 855 for (uint32_t i = 0; i < (cnt); i++) \ 856 if (map[(arr)[i] field] >= 0) { \ 857 (arr)[q] = (arr)[i]; \ 858 (arr)[q] field = (uint16_t)map[(arr)[i] field]; \ 859 q++; \ 860 } \ 861 (cnt) = q; \ 862 } while (0) 863 REMAP(g->auth, g->n_auth, .node); 864 REMAP(g->res, g->n_res, .node); 865 REMAP(g->success, g->n_success, .node); 866 REMAP(g->failure, g->n_failure, .node); 867 REMAP(g->evidence, g->n_evidence, ); 868 REMAP(g->effects, g->n_effects, ); 869 #undef REMAP 870 } 871 872 /* Could node n end SKIPPED / FAILED in some run? Conservative. */ 873 static void may_flags(const AgGraph *g, const uint16_t *ord, uint32_t cnt, 874 uint8_t *may_skip, uint8_t *may_fail) { 875 for (uint32_t t = 0; t < cnt; t++) { 876 uint32_t n = ord[t]; 877 AgKind k = g->nodes[n].kind; 878 uint8_t sk = 0, fl = 0; 879 fl = k == AG_VERIFY || k == AG_RECALL || k == AG_RETRY || k == AG_SKILL || 880 k == AG_JOIN || is_boundary(k) || k == AG_PURE || /* PURE: division by zero */ 881 k == AG_META; 882 for (uint32_t e = 0; e < g->n_data; e++) { 883 if (g->data[e].to != n) continue; 884 if (g->data[e].mode == AG_EDGE_ON_FAIL) sk = 1; 885 sk |= may_skip[g->data[e].from]; 886 fl |= may_fail[g->data[e].from]; 887 } 888 for (uint32_t e = 0; e < g->n_deps; e++) { 889 if (g->deps[e].to != n) continue; 890 if (g->deps[e].kind == AG_DEP_GUARD) sk = 1; 891 sk |= may_skip[g->deps[e].from]; 892 fl |= may_fail[g->deps[e].from]; 893 } 894 may_skip[n] = sk; 895 may_fail[n] = fl; 896 } 897 } 898 899 /* Is `a` an ancestor of `b` along strict paths only: DATA edges into 900 * non-JOIN nodes, and order edges, through nodes that can never be SKIPPED? 901 * Along such a path OK stays OK-or-FAILED and FAILED arrives as FAILED, so 902 * an order edge a -> b says nothing the path does not. A node that can be 903 * skipped would turn an arriving FAILED into SKIPPED (guards and skipped 904 * inputs are looked at first), so the path stops there. */ 905 static int strict_ancestor(const AgGraph *g, uint32_t a, uint32_t b, int skip_dep, 906 const uint8_t *may_skip) { 907 uint64_t seen = 1ull << a, frontier = 1ull << a; 908 while (frontier) { 909 uint64_t next = 0; 910 for (uint32_t e = 0; e < g->n_data; e++) 911 if ((frontier & (1ull << g->data[e].from)) && g->data[e].mode == AG_EDGE_DATA && 912 g->nodes[g->data[e].to].kind != AG_JOIN && !may_skip[g->data[e].to]) 913 next |= 1ull << g->data[e].to; 914 for (uint32_t e = 0; e < g->n_deps; e++) 915 if ((int)e != skip_dep && (frontier & (1ull << g->deps[e].from)) && 916 g->deps[e].kind == AG_DEP_ORDER && !may_skip[g->deps[e].to]) 917 next |= 1ull << g->deps[e].to; 918 next &= ~seen; 919 seen |= next; 920 frontier = next; 921 } 922 return (seen >> b) & 1u; 923 } 924 925 int rx_graph_optimize(AgGraph *g, AgReport *rep) { 926 AgReport dummy; 927 if (!rep) { memset(&dummy, 0, sizeof dummy); rep = &dummy; } 928 uint16_t ord[AG_MAX_NODES]; 929 int cnt; 930 931 /* Passes 1-4 can open work for each other (dropping an order edge can 932 * make two nodes identical; merging can make a producer single-use), so 933 * they repeat until a round changes nothing. Each round only removes 934 * nodes or edges, so this ends. */ 935 for (;;) { 936 uint32_t before_round = rep->passes.const_folded + rep->passes.cse + rep->passes.world_reads + 937 rep->passes.fused + rep->passes.deps_dropped; 938 /* 1. Constant propagation: a PURE node whose inputs are all CONST, with 939 * no control dependency, becomes a CONST. Guarded nodes keep their guard. */ 940 for (int changed = 1; changed;) { 941 changed = 0; 942 if ((cnt = topo(g, ord)) < 0) return cnt; 943 for (int t = 0; t < cnt; t++) { 944 uint32_t n = ord[t]; 945 AgNode *x = &g->nodes[n]; 946 if (x->kind != AG_PURE || pinned(g, n)) continue; 947 uint32_t e[AG_MAX_IN]; 948 uint32_t k = inputs_by_port(g, n, e); 949 int all = k > 0; 950 AgIn in; 951 memset(&in, 0, sizeof in); 952 in.n_in = k; 953 for (uint32_t i = 0; i < k && all; i++) { 954 const AgNode *s = &g->nodes[g->data[e[i]].from]; 955 if (s->kind != AG_CONST || g->data[e[i]].mode != AG_EDGE_DATA || 956 dep_of(g, g->data[e[i]].from, AG_DEP_GUARD) >= 0) 957 all = 0; 958 in.v[i] = s->imm; 959 in.s[i] = AG_OK; 960 } 961 if (!all) continue; 962 AgOut o; 963 semantics(x, NULL, &in, &o); 964 if (o.status != AG_OK) continue; /* keep a failing fold as it is */ 965 x->kind = AG_CONST; 966 x->imm = o.value; 967 x->op = 0; 968 x->n_fused = 0; 969 drop_edges_to(g, n); 970 rep->passes.const_folded++; 971 changed = 1; 972 } 973 } 974 975 /* 2. Common subexpressions and redundant World reads. */ 976 rx_graph_identify(g); 977 if ((cnt = topo(g, ord)) < 0) return cnt; 978 for (int t = 0; t < cnt; t++) { 979 uint32_t n = ord[t]; 980 AgNode *x = &g->nodes[n]; 981 if (!x->alive || pinned(g, n) || x->kind == AG_BRANCH || x->kind == AG_JOIN) continue; 982 for (int u = 0; u < t; u++) { 983 uint32_t m = ord[u]; 984 if (!g->nodes[m].alive || pinned(g, m)) continue; 985 if (memcmp(g->nodes[m].id, x->id, 32) != 0) continue; 986 redirect(g, n, m); 987 drop_edges_of(g, n); 988 x->alive = 0; 989 if (x->kind == AG_WORLD_READ || x->kind == AG_RECALL) rep->passes.world_reads++; 990 else rep->passes.cse++; 991 break; 992 } 993 rx_graph_identify(g); 994 } 995 996 /* 3. Pure fusion: a unary PURE step whose only producer is a PURE node 997 * with no other consumer folds into that producer. */ 998 for (int changed = 1; changed;) { 999 changed = 0; 1000 if ((cnt = topo(g, ord)) < 0) return cnt; 1001 for (int t = 0; t < cnt && !changed; t++) { 1002 uint32_t c = ord[t]; 1003 AgNode *y = &g->nodes[c]; 1004 if (y->kind != AG_PURE || pinned(g, c) || y->op == OP_IDENTITY || 1005 y->op == OP_CONSTANT) 1006 continue; 1007 uint32_t e[AG_MAX_IN]; 1008 if (inputs_by_port(g, c, e) != 1 || g->data[e[0]].mode != AG_EDGE_DATA) continue; 1009 int has_ctl = 0; 1010 for (uint32_t d = 0; d < g->n_deps; d++) has_ctl |= g->deps[d].to == c; 1011 if (has_ctl) continue; 1012 uint32_t p = g->data[e[0]].from; 1013 AgNode *x = &g->nodes[p]; 1014 if (x->kind != AG_PURE || pinned(g, p) || consumers(g, p) != 1) continue; 1015 if (x->n_fused + 1 + y->n_fused > AG_MAX_FUSED) continue; 1016 x->fused[x->n_fused++] = (AgFusedStep){ y->op, y->imm }; 1017 for (uint32_t i = 0; i < y->n_fused; i++) x->fused[x->n_fused++] = y->fused[i]; 1018 x->out_type = y->out_type; 1019 x->cost_us += y->cost_us; 1020 drop_edges_to(g, c); 1021 redirect(g, c, p); 1022 y->alive = 0; 1023 rep->passes.fused++; 1024 changed = 1; 1025 } 1026 } 1027 1028 /* 4. Dependency simplification. An order edge goes when the order it 1029 * carries is already implied by a strict path, or when it only delays an 1030 * effect-free node behind a source that can neither fail nor skip. An 1031 * order edge that touches an effect boundary stays unless implied. */ 1032 if ((cnt = topo(g, ord)) < 0) return cnt; 1033 uint8_t may_skip[AG_MAX_NODES] = { 0 }, may_fail[AG_MAX_NODES] = { 0 }; 1034 may_flags(g, ord, (uint32_t)cnt, may_skip, may_fail); 1035 for (uint32_t e = 0; e < g->n_deps;) { 1036 const AgDep *d = &g->deps[e]; 1037 int drop = 0; 1038 if (d->kind == AG_DEP_ORDER) { 1039 int touches = is_boundary(g->nodes[d->from].kind) || is_boundary(g->nodes[d->to].kind); 1040 if (!may_skip[d->from] && strict_ancestor(g, d->from, d->to, (int)e, may_skip)) drop = 1; 1041 else if (!touches && !may_skip[d->from] && !may_fail[d->from]) drop = 1; 1042 } 1043 if (drop) { 1044 g->deps[e] = g->deps[--g->n_deps]; 1045 rep->passes.deps_dropped++; 1046 } else { 1047 e++; 1048 } 1049 } 1050 1051 if (rep->passes.const_folded + rep->passes.cse + rep->passes.world_reads + 1052 rep->passes.fused + rep->passes.deps_dropped == before_round) 1053 break; 1054 } 1055 1056 /* 5. Dead nodes: keep what reaches a boundary, evidence or condition. 1057 * Runs after 4 so a node held only by a dropped order edge goes too. */ 1058 uint64_t keep = 0; 1059 for (uint32_t n = 0; n < g->n_nodes; n++) 1060 if (g->nodes[n].alive && pinned(g, n)) keep |= (1ull << n) | rx_graph_ancestors(g, n); 1061 for (uint32_t n = 0; n < g->n_nodes; n++) 1062 if (g->nodes[n].alive && !(keep & (1ull << n))) { 1063 drop_edges_of(g, n); 1064 g->nodes[n].alive = 0; 1065 rep->passes.dead++; 1066 } 1067 1068 compact(g); 1069 return 0; 1070 } 1071 1072 int rx_graph_check_preserved(const AgGraph *before, const AgGraph *after) { 1073 int16_t where[AG_MAX_NODES]; 1074 for (uint32_t i = 0; i < AG_MAX_NODES; i++) where[i] = -1; 1075 for (uint32_t n = 0; n < after->n_nodes; n++) where[after->nodes[n].origin] = (int16_t)n; 1076 if (before->n_effects != after->n_effects) return -1; 1077 for (uint32_t i = 0; i < before->n_effects; i++) { 1078 const AgNode *b = &before->nodes[before->effects[i]]; 1079 const AgNode *a = &after->nodes[after->effects[i]]; 1080 if (a->origin != b->origin || a->kind != b->kind || a->obj.id != b->obj.id || 1081 a->obj.generation != b->obj.generation || a->field != b->field) 1082 return -2; 1083 } 1084 for (uint32_t i = 0; i < before->n_evidence; i++) { 1085 int16_t n = where[before->nodes[before->evidence[i]].origin]; 1086 if (n < 0 || after->nodes[n].kind != before->nodes[before->evidence[i]].kind) return -3; 1087 int found = 0; 1088 for (uint32_t j = 0; j < after->n_evidence; j++) found |= after->evidence[j] == n; 1089 if (!found) return -3; 1090 } 1091 for (uint32_t i = 0; i < before->n_success; i++) 1092 if (where[before->nodes[before->success[i].node].origin] < 0) return -4; 1093 for (uint32_t i = 0; i < before->n_failure; i++) 1094 if (where[before->nodes[before->failure[i].node].origin] < 0) return -4; 1095 if (before->n_success != after->n_success || before->n_failure != after->n_failure) return -4; 1096 return 0; 1097 } 1098 1099 void rx_graph_costs(const AgGraph *g, uint64_t *critical_us, uint64_t *total_us) { 1100 uint16_t ord[AG_MAX_NODES]; 1101 int cnt = topo(g, ord); 1102 uint64_t fin[AG_MAX_NODES] = { 0 }, best = 0, total = 0; 1103 for (int t = 0; t < cnt; t++) { 1104 uint32_t n = ord[t]; 1105 uint64_t start = 0; 1106 for (uint32_t e = 0; e < g->n_data; e++) 1107 if (g->data[e].to == n && fin[g->data[e].from] > start) start = fin[g->data[e].from]; 1108 for (uint32_t e = 0; e < g->n_deps; e++) 1109 if (g->deps[e].to == n && fin[g->deps[e].from] > start) start = fin[g->deps[e].from]; 1110 fin[n] = start + g->nodes[n].cost_us; 1111 total += g->nodes[n].cost_us; 1112 if (fin[n] > best) best = fin[n]; 1113 } 1114 if (critical_us) *critical_us = best; 1115 if (total_us) *total_us = total; 1116 } 1117 1118 /* ---- compile ---- */ 1119 1120 static int cap_lookup(const RxWorld *w, const AgCapTable *caps, uint32_t subject, 1121 uint64_t resource, uint32_t rights, RxCapRef *out) { 1122 for (uint32_t i = 0; caps && i < caps->n; i++) { 1123 if (caps->cap[i].resource != resource || (caps->cap[i].rights & rights) != rights) continue; 1124 if (rx_world_validate_cap(w, caps->cap[i].ref, subject, resource, rights, NULL) != RX_CAP_OK) 1125 continue; 1126 if (out) *out = caps->cap[i].ref; 1127 return 1; 1128 } 1129 return 0; 1130 } 1131 1132 static int fits(const RxResourceNeed *n, const RxResourceBudget *b) { 1133 if (n->memory_bytes > b->memory_bytes) return 0; 1134 if (n->accelerator_features & ~b->offered_accel) return 0; 1135 if (n->locality & ~b->offered_locality) return 0; 1136 if (n->compute_class && !(b->compute_mask & (1u << (n->compute_class & 31u)))) return 0; 1137 if (n->energy_cost > b->energy_budget) return 0; 1138 return 1; 1139 } 1140 1141 int rx_graph_compile(const AgGoal *goal, RxWorld *w, const AgCapTable *caps, 1142 const AgConstraints *cons, const AgLibrary *lib, int optimize, 1143 AgGraph *out, AgReport *rep) { 1144 if (!goal || !w || !caps || !cons || !lib || !out || !rep) return AG_E_ARG; 1145 memset(rep, 0, sizeof *rep); 1146 const AgGraph *tmpl = NULL; 1147 for (uint32_t i = 0; i < lib->n; i++) 1148 if (lib->proc[i].goal_kind == goal->kind) tmpl = lib->proc[i].tmpl; 1149 if (!tmpl) return rep->verdict = AG_E_NO_PROCEDURE; 1150 *out = *tmpl; 1151 out->subject = caps->subject; 1152 1153 /* Bind World objects and check they are current. */ 1154 for (uint32_t n = 0; n < out->n_nodes; n++) { 1155 AgNode *x = &out->nodes[n]; 1156 if (!x->alive || !x->param) continue; 1157 if (x->param > goal->n_args) return rep->verdict = AG_E_ARG; 1158 x->obj = goal->args[x->param - 1]; 1159 } 1160 for (uint32_t n = 0; n < out->n_nodes; n++) { 1161 AgNode *x = &out->nodes[n]; 1162 if (!x->alive || !has_target(x->kind)) continue; 1163 RxObject o; 1164 if (rx_world_read(w, x->obj, &o) != RX_OK) rep->stale[rep->n_stale++] = (uint16_t)n; 1165 } 1166 if (rep->n_stale) return rep->verdict = AG_E_STALE; 1167 1168 /* Capability resolution happens here, against what the principal holds. */ 1169 for (uint32_t n = 0; n < out->n_nodes; n++) { 1170 AgNode *x = &out->nodes[n]; 1171 if (!x->alive || x->kind != AG_CAP_RESOLVE) continue; 1172 x->imm = (uint64_t)cap_lookup(w, caps, caps->subject, x->imm, (uint32_t)x->imm2, NULL); 1173 x->imm2 = 0; 1174 x->kind = AG_CONST; 1175 } 1176 int rc = rx_graph_validate(out, w); 1177 if (rc != 0) return rep->verdict = rc; 1178 1179 /* Constraints. */ 1180 if (cons->max_effects != UINT32_MAX) { 1181 uint32_t performs = 0; 1182 for (uint32_t i = 0; i < out->n_effects; i++) 1183 performs += out->nodes[out->effects[i]].kind == AG_EFFECT_PERFORM; 1184 if (performs > cons->max_effects) return rep->verdict = AG_E_CONSTRAINT; 1185 } 1186 if (cons->forbid_hi) 1187 for (uint32_t i = 0; i < out->n_auth; i++) 1188 if (out->auth[i].resource >= cons->forbid_lo && out->auth[i].resource <= cons->forbid_hi) 1189 return rep->verdict = AG_E_CONSTRAINT; 1190 1191 rep->nodes_before = 0; 1192 for (uint32_t n = 0; n < out->n_nodes; n++) rep->nodes_before += out->nodes[n].alive; 1193 AgGraph before = *out; 1194 if (optimize) { 1195 if ((rc = rx_graph_optimize(out, rep)) != 0) return rep->verdict = rc; 1196 if ((rc = rx_graph_validate(out, w)) != 0) return rep->verdict = rc; 1197 if (rx_graph_check_preserved(&before, out) != 0) return rep->verdict = AG_E_CONSTRAINT; 1198 } 1199 return rep->verdict = rx_graph_report_needs(out, w, caps, cons, rep); 1200 } 1201 1202 int rx_graph_report_needs(const AgGraph *out, RxWorld *w, const AgCapTable *caps, 1203 const AgConstraints *cons, AgReport *rep) { 1204 rep->nodes_after = out->n_nodes; 1205 rx_graph_costs(out, &rep->critical_path_us, &rep->total_work_us); 1206 if (cons->critical_path_us && rep->critical_path_us > cons->critical_path_us) 1207 return AG_E_CONSTRAINT; 1208 1209 /* Authority: found, or reported missing. Never created here. */ 1210 rep->n_missing = 0; 1211 for (uint32_t i = 0; i < out->n_auth; i++) 1212 if (!cap_lookup(w, caps, caps->subject, out->auth[i].resource, out->auth[i].rights, NULL)) 1213 rep->missing[rep->n_missing++] = out->auth[i]; 1214 /* Resources: what the body offers now. */ 1215 rep->n_resource_blocked = 0; 1216 for (uint32_t i = 0; i < out->n_res; i++) 1217 if (!fits(&out->res[i].need, &cons->budget)) 1218 rep->resource_blocked[rep->n_resource_blocked++] = out->res[i].node; 1219 return AG_OK_READY; 1220 } 1221 1222 /* ---- lowering ---- */ 1223 1224 #define CELL_MASK (RX_FIELD(0) | RX_FIELD(1) | RX_FIELD(2) | RX_FIELD(3) | RX_FIELD(4)) 1225 1226 static const RxSnapshotDep *snap(const RxCtx *c, RxObjRef r) { 1227 for (uint32_t i = 0; i < c->n_in; i++) 1228 if (c->in[i].obj.id == r.id && c->in[i].obj.generation == r.generation) return &c->in[i]; 1229 return NULL; 1230 } 1231 1232 static int node_fn(RxCtx *c) { 1233 const struct AgNodeCtx *nc = c->user; 1234 const AgLowered *L = nc->L; 1235 const AgGraph *g = L->g; 1236 uint32_t n = nc->node; 1237 const AgNode *x = &g->nodes[n]; 1238 const RxSnapshotDep *run = snap(c, L->run), *own = snap(c, L->cell[n]); 1239 if (!run || !own) return -1; 1240 uint64_t r = run->field[0]; 1241 if (r == 0 || own->field[0] == r) return 0; /* no run, or done for this run */ 1242 1243 AgIn in; 1244 memset(&in, 0, sizeof in); 1245 uint32_t e[AG_MAX_IN]; 1246 in.n_in = inputs_by_port(g, n, e); 1247 for (uint32_t i = 0; i < in.n_in; i++) { 1248 const RxSnapshotDep *s = snap(c, L->cell[g->data[e[i]].from]); 1249 if (!s) return -1; 1250 if (s->field[0] != r) return 0; /* an input is not in for this run */ 1251 in.s[i] = (uint8_t)s->field[1]; 1252 in.v[i] = s->field[2]; 1253 in.mode[i] = g->data[e[i]].mode; 1254 } 1255 int gd = dep_of(g, n, AG_DEP_GUARD); 1256 if (gd >= 0) { 1257 const RxSnapshotDep *s = snap(c, L->cell[g->deps[gd].from]); 1258 if (!s) return -1; 1259 if (s->field[0] != r) return 0; 1260 in.has_guard = 1; 1261 in.guard_s = (uint8_t)s->field[1]; 1262 in.guard_v = s->field[2]; 1263 in.guard_pol = g->deps[gd].polarity; 1264 } 1265 for (uint32_t od = 0; od < g->n_deps; od++) { 1266 if (g->deps[od].to != n || g->deps[od].kind != AG_DEP_ORDER) continue; 1267 const RxSnapshotDep *s = snap(c, L->cell[g->deps[od].from]); 1268 if (!s) return -1; 1269 if (s->field[0] != r) return 0; 1270 in.has_order = 1; 1271 if (s->field[1] == AG_FAILED) in.order_s = AG_FAILED; 1272 } 1273 const RxSnapshotDep *wo = NULL; 1274 if (reads_world(x->kind)) { 1275 wo = snap(c, x->obj); 1276 if (!wo) return -1; 1277 memcpy(in.world, wo->field, sizeof in.world); 1278 } 1279 if (x->kind == AG_META) 1280 for (uint32_t i = 0; i < x->n_mobj && i < AG_META_MAX_OBJ; i++) { 1281 const RxSnapshotDep *s = snap(c, x->mobj[i]); 1282 if (!s) return -1; 1283 memcpy(in.mworld[i], s->field, sizeof in.mworld[i]); 1284 } 1285 AgOut o; 1286 semantics(x, L->skills, &in, &o); 1287 if (x->kind == AG_META) busy_us((uint32_t)o.busy_us); 1288 else if (o.status == AG_OK) busy_us(x->cost_us); 1289 uint64_t ev = rx_graph_evidence_word(x, r, o.status, o.value, in.v, in.n_in); 1290 c->out[c->n_out++] = (RxMutation){ L->cell[n], 0, r }; 1291 c->out[c->n_out++] = (RxMutation){ L->cell[n], 1, o.status }; 1292 c->out[c->n_out++] = (RxMutation){ L->cell[n], 2, o.value }; 1293 c->out[c->n_out++] = (RxMutation){ L->cell[n], 3, ev }; 1294 c->out[c->n_out++] = (RxMutation){ L->cell[n], 4, o.attempts }; 1295 if (x->kind == AG_META) c->out[c->n_out++] = (RxMutation){ L->cell[n], 5, o.steps }; 1296 if (o.status == AG_OK && o.write) { 1297 if (x->kind == AG_WORLD_PUBLISH) { 1298 c->out[c->n_out++] = (RxMutation){ x->obj, x->field, o.value }; 1299 } else { 1300 c->out[c->n_out++] = (RxMutation){ x->obj, 0, wo->field[0] + 1 }; 1301 c->out[c->n_out++] = (RxMutation){ x->obj, 1, in.v[0] }; 1302 c->out[c->n_out++] = (RxMutation){ x->obj, 2, rx_graph_chain(wo->field[2], r, in.v[0]) }; 1303 c->out[c->n_out++] = (RxMutation){ x->obj, 3, r }; 1304 } 1305 } 1306 return 0; 1307 } 1308 1309 /* The principal asks AEGIS for authority it lacks, once per run while the 1310 * slot is empty. request: 0 seq, 1 resource, 2 rights, 3 lease, 4 slot, 5 op. */ 1311 static int ask_fn(RxCtx *c) { 1312 const struct AgAskCtx *ac = c->user; 1313 const AgLowered *L = ac->L; 1314 uint32_t k = ac->k; 1315 const RxAegisClientObjects *cl = L->aegis_client; 1316 const RxSnapshotDep *run = snap(c, L->run), *rq = snap(c, cl->request), 1317 *sl = snap(c, cl->slot[L->asked[k].slot]); 1318 if (!run || !rq || !sl) return -1; 1319 uint64_t r = run->field[0]; 1320 if (r == 0 || sl->field[2] == RX_AEGIS_SLOT_LIVE || rq->field[0] >= r) return 0; 1321 const AgAuthReq *a = &L->ask_req[k]; 1322 c->out[c->n_out++] = (RxMutation){ cl->request, 0, r }; 1323 c->out[c->n_out++] = (RxMutation){ cl->request, 1, a->resource }; 1324 c->out[c->n_out++] = (RxMutation){ cl->request, 2, a->rights }; 1325 c->out[c->n_out++] = (RxMutation){ cl->request, 3, 0 }; 1326 c->out[c->n_out++] = (RxMutation){ cl->request, 4, L->asked[k].slot }; 1327 c->out[c->n_out++] = (RxMutation){ cl->request, 5, RX_AEGIS_OP_ACQUIRE }; 1328 return 0; 1329 } 1330 1331 static void add_trigger(RxReactionDesc *d, RxObjRef o, uint64_t mask) { 1332 for (uint32_t i = 0; i < d->n_triggers; i++) 1333 if (d->triggers[i].obj.id == o.id) { d->triggers[i].mask |= mask; return; } 1334 d->triggers[d->n_triggers++] = (RxDep){ o, mask }; 1335 } 1336 1337 static void add_read(RxReactionDesc *d, RxObjRef o, uint64_t mask) { 1338 for (uint32_t i = 0; i < d->n_triggers; i++) 1339 if (d->triggers[i].obj.id == o.id) return; 1340 for (uint32_t i = 0; i < d->n_reads; i++) 1341 if (d->reads[i].obj.id == o.id) { d->reads[i].mask |= mask; return; } 1342 d->reads[d->n_reads++] = (RxDep){ o, mask }; 1343 } 1344 1345 int rx_graph_lower(AgLowered *L, RxWorld *w, const AgGraph *g, const AgSkillTable *skills, 1346 const AgCapTable *caps, uint64_t cell_res, RxCapRef cell_cap, 1347 uint64_t run_res, RxCapRef run_cap, const AgAegisBinding *aegis) { 1348 memset(L, 0, sizeof *L); 1349 L->w = w; 1350 L->g = g; 1351 L->skills = skills; 1352 L->cell_res = cell_res; 1353 L->run_res = run_res; 1354 L->aegis_client = aegis ? aegis->client : NULL; 1355 uint64_t z[RX_MAX_FIELDS] = { 0 }; 1356 if (rx_world_create(w, AG_OT_RUN, RX_PERSIST_RESIDENT, run_res, z, &L->run) != RX_OK) 1357 return AG_E_LOWER; 1358 for (uint32_t n = 0; n < g->n_nodes; n++) 1359 if (rx_world_create(w, AG_OT_CELL, RX_PERSIST_RESIDENT, cell_res, z, &L->cell[n]) != RX_OK) 1360 return AG_E_LOWER; 1361 uint16_t ord[AG_MAX_NODES]; 1362 int cnt = topo(g, ord); 1363 if (cnt < 0) return cnt; 1364 for (int t = 0; t < cnt; t++) { 1365 uint32_t n = ord[t]; 1366 const AgNode *x = &g->nodes[n]; 1367 RxReactionDesc d; 1368 memset(&d, 0, sizeof d); 1369 d.name = rx_graph_kind_name(x->kind); 1370 d.faculty = RX_FACULTY_OMEGA; 1371 d.subject = g->subject; 1372 d.priority = RX_PRIO_FOREGROUND; 1373 d.stamp_proposed = true; 1374 d.fn = node_fn; 1375 L->nctx[n] = (struct AgNodeCtx){ L, n }; 1376 d.user = &L->nctx[n]; 1377 for (uint32_t i = 0; i < g->n_res; i++) 1378 if (g->res[i].node == n) d.need = g->res[i].need; 1379 for (uint32_t e = 0; e < g->n_data; e++) 1380 if (g->data[e].to == n) add_trigger(&d, L->cell[g->data[e].from], CELL_MASK); 1381 for (uint32_t e = 0; e < g->n_deps; e++) 1382 if (g->deps[e].to == n) add_trigger(&d, L->cell[g->deps[e].from], CELL_MASK); 1383 if (d.n_triggers == 0) add_trigger(&d, L->run, RX_FIELD(0)); 1384 else add_read(&d, L->run, RX_FIELD(0)); 1385 add_read(&d, L->cell[n], RX_FIELD(0)); 1386 if (reads_world(x->kind)) add_read(&d, x->obj, RX_ALL_FIELDS); 1387 if (x->kind == AG_META) 1388 for (uint32_t i = 0; i < x->n_mobj && i < AG_META_MAX_OBJ; i++) 1389 add_read(&d, x->mobj[i], RX_ALL_FIELDS); 1390 d.writes[d.n_writes++] = (RxDep){ L->cell[n], CELL_MASK | (x->kind == AG_META ? RX_FIELD(5) : 0) }; 1391 if (x->kind == AG_WORLD_PUBLISH) d.writes[d.n_writes++] = (RxDep){ x->obj, RX_FIELD(x->field) }; 1392 if (x->kind == AG_EFFECT_PERFORM) 1393 d.writes[d.n_writes++] = (RxDep){ x->obj, RX_FIELD(0) | RX_FIELD(1) | RX_FIELD(2) | RX_FIELD(3) }; 1394 d.caps[d.n_caps++] = (RxCapNeed){ cell_cap, cell_res, RX_RIGHT_READ | RX_RIGHT_WRITE }; 1395 d.caps[d.n_caps++] = (RxCapNeed){ run_cap, run_res, RX_RIGHT_READ }; 1396 for (uint32_t i = 0; i < g->n_auth; i++) { 1397 if (g->auth[i].node != n) continue; 1398 RxCapRef ref = { UINT32_MAX, 0 }; /* missing: the engine blocks and records it */ 1399 int have = cap_lookup(w, caps, g->subject, g->auth[i].resource, g->auth[i].rights, &ref); 1400 uint32_t ci = d.n_caps; 1401 d.caps[d.n_caps++] = (RxCapNeed){ ref, g->auth[i].resource, g->auth[i].rights }; 1402 if (!have && aegis && aegis->client) { 1403 if (L->n_ask >= RX_AEGIS_SLOTS || L->n_ask >= 1) return AG_E_LOWER; 1404 uint32_t k = L->n_ask++; 1405 L->asked[k].node = n; 1406 L->asked[k].slot = k; 1407 L->ask_req[k] = g->auth[i]; 1408 d.caps[d.n_caps++] = (RxCapNeed){ aegis->slot_cap[k], aegis->slot_res[k], RX_RIGHT_READ }; 1409 rx_aegis_use_slot(&d, ci, aegis->client->slot[k]); 1410 } 1411 } 1412 if (d.n_triggers + d.n_reads > RX_MAX_DEPS) return AG_E_DEP_BUDGET; 1413 if (rx_world_add_reaction(w, &d, &L->reaction[n]) != RX_OK) return AG_E_LOWER; 1414 } 1415 for (uint32_t k = 0; k < L->n_ask; k++) { 1416 RxReactionDesc d; 1417 memset(&d, 0, sizeof d); 1418 d.name = "ag.ask"; 1419 d.faculty = RX_FACULTY_OMEGA; 1420 d.subject = g->subject; 1421 d.priority = RX_PRIO_FOREGROUND; 1422 d.stamp_proposed = true; 1423 d.fn = ask_fn; 1424 L->actx[k] = (struct AgAskCtx){ L, k }; 1425 d.user = &L->actx[k]; 1426 d.n_triggers = 1; 1427 d.triggers[0] = (RxDep){ L->run, RX_FIELD(0) }; 1428 d.n_reads = 2; 1429 d.reads[0] = (RxDep){ aegis->client->request, RX_ALL_FIELDS }; 1430 d.reads[1] = (RxDep){ aegis->client->slot[L->asked[k].slot], RX_ALL_FIELDS }; 1431 d.n_writes = 1; 1432 d.writes[0] = (RxDep){ aegis->client->request, RX_ALL_FIELDS }; 1433 d.n_caps = 3; 1434 d.caps[0] = (RxCapNeed){ run_cap, run_res, RX_RIGHT_READ }; 1435 d.caps[1] = (RxCapNeed){ aegis->request_cap, aegis->request_res, 1436 RX_RIGHT_READ | RX_RIGHT_WRITE }; 1437 d.caps[2] = (RxCapNeed){ aegis->slot_cap[L->asked[k].slot], 1438 aegis->slot_res[L->asked[k].slot], RX_RIGHT_READ }; 1439 if (rx_world_add_reaction(w, &d, &L->ask_reaction[k]) != RX_OK) return AG_E_LOWER; 1440 } 1441 return 0; 1442 } 1443 1444 int64_t rx_graph_start(AgLowered *L, RxCapRef external_run_cap, uint64_t run) { 1445 RxMutation m = { L->run, 0, run }; 1446 return rx_world_publish_external(L->w, external_run_cap, &m, 1); 1447 } 1448 1449 static int outcome(const AgGraph *g, const uint8_t *status) { 1450 for (uint32_t i = 0; i < g->n_failure; i++) 1451 if (status[g->failure[i].node] == g->failure[i].status) return AG_RUN_FAILURE; 1452 for (uint32_t i = 0; i < g->n_success; i++) 1453 if (status[g->success[i].node] != g->success[i].status) return AG_RUN_INCOMPLETE; 1454 return g->n_success ? AG_RUN_SUCCESS : AG_RUN_INCOMPLETE; 1455 } 1456 1457 int rx_graph_collect(AgLowered *L, uint64_t run, AgResult *out) { 1458 memset(out, 0, sizeof *out); 1459 out->run = run; 1460 for (uint32_t n = 0; n < L->g->n_nodes; n++) { 1461 RxObject o; 1462 if (rx_world_read(L->w, L->cell[n], &o) != RX_OK) return AG_E_STALE; 1463 if (o.field[0] != run) continue; 1464 out->status[n] = (uint8_t)o.field[1]; 1465 out->value[n] = o.field[2]; 1466 out->evidence[n] = o.field[3]; 1467 out->attempts[n] = (uint32_t)o.field[4]; 1468 out->steps[n] = o.field[5]; 1469 } 1470 out->outcome = outcome(L->g, out->status); 1471 return 0; 1472 } 1473 1474 /* ---- reference ---- */ 1475 1476 /* Shadow of the World objects a graph touches, one entry per reference 1477 * (id, generation). A write goes to every entry of that object id. */ 1478 typedef struct { 1479 uint32_t n; 1480 struct { RxObjRef ref; uint8_t live; RxObject o; } e[AG_MAX_NODES * AG_META_MAX_OBJ]; 1481 } Shadow; 1482 1483 static int shadow_at(const Shadow *sh, RxObjRef r) { 1484 for (uint32_t i = 0; i < sh->n; i++) 1485 if (sh->e[i].ref.id == r.id && sh->e[i].ref.generation == r.generation) return (int)i; 1486 return -1; 1487 } 1488 1489 static void shadow_add(Shadow *sh, RxObjRef r, int live, const RxObject *o) { 1490 if (shadow_at(sh, r) >= 0 || sh->n >= AG_MAX_NODES * AG_META_MAX_OBJ) return; 1491 sh->e[sh->n].ref = r; 1492 sh->e[sh->n].live = (uint8_t)(live != 0); 1493 if (o) sh->e[sh->n].o = *o; 1494 else memset(&sh->e[sh->n].o, 0, sizeof sh->e[sh->n].o); 1495 sh->n++; 1496 } 1497 1498 static void shadow_write(Shadow *sh, uint32_t id, const RxObject *o) { 1499 for (uint32_t i = 0; i < sh->n; i++) 1500 if (sh->e[i].ref.id == id) sh->e[i].o = *o; 1501 } 1502 1503 static int reference_core(const AgGraph *g, const AgSkillTable *skills, Shadow *sh, RxWorld *w, 1504 const AgCapTable *caps, uint64_t run, AgReference *ref) { 1505 ref->r.run = run; 1506 uint16_t ord[AG_MAX_NODES]; 1507 int cnt = topo(g, ord); 1508 if (cnt < 0) return cnt; 1509 uint8_t resolved[AG_MAX_NODES] = { 0 }; 1510 uint32_t eff = 0; 1511 for (int t = 0; t < cnt; t++) { 1512 uint32_t n = ord[t]; 1513 const AgNode *x = &g->nodes[n]; 1514 RxObjRef objs[AG_META_MAX_OBJ]; 1515 uint32_t no = node_objs(x, objs); 1516 int slot[AG_META_MAX_OBJ], live = 1; 1517 for (uint32_t i = 0; i < no; i++) { 1518 slot[i] = shadow_at(sh, objs[i]); 1519 if (slot[i] < 0 || !sh->e[slot[i]].live) live = 0; 1520 } 1521 if (!live) continue; /* stale: never runs */ 1522 int allowed = 1; 1523 for (uint32_t i = 0; caps && i < g->n_auth; i++) 1524 if (g->auth[i].node == n && 1525 !cap_lookup(w, caps, g->subject, g->auth[i].resource, g->auth[i].rights, NULL)) 1526 allowed = 0; 1527 if (!allowed) continue; /* blocked: never runs */ 1528 AgIn in; 1529 memset(&in, 0, sizeof in); 1530 uint32_t e[AG_MAX_IN]; 1531 in.n_in = inputs_by_port(g, n, e); 1532 int ready = 1; 1533 for (uint32_t i = 0; i < in.n_in; i++) { 1534 uint32_t s = g->data[e[i]].from; 1535 if (!resolved[s]) ready = 0; 1536 in.s[i] = ref->r.status[s]; 1537 in.v[i] = ref->r.value[s]; 1538 in.mode[i] = g->data[e[i]].mode; 1539 } 1540 int gd = dep_of(g, n, AG_DEP_GUARD); 1541 if (gd >= 0) { 1542 uint32_t s = g->deps[gd].from; 1543 if (!resolved[s]) ready = 0; 1544 in.has_guard = 1; 1545 in.guard_s = ref->r.status[s]; 1546 in.guard_v = ref->r.value[s]; 1547 in.guard_pol = g->deps[gd].polarity; 1548 } 1549 for (uint32_t od = 0; od < g->n_deps; od++) { 1550 if (g->deps[od].to != n || g->deps[od].kind != AG_DEP_ORDER) continue; 1551 uint32_t s = g->deps[od].from; 1552 if (!resolved[s]) ready = 0; 1553 in.has_order = 1; 1554 if (ref->r.status[s] == AG_FAILED) in.order_s = AG_FAILED; 1555 } 1556 if (!ready) continue; 1557 /* The latest shadow of each object: the last boundary that wrote it. */ 1558 if (reads_world(x->kind)) { 1559 memcpy(in.world, sh->e[slot[0]].o.field, sizeof in.world); 1560 memcpy(ref->world_seen[n][0], in.world, sizeof in.world); 1561 } 1562 if (x->kind == AG_META) 1563 for (uint32_t i = 0; i < no; i++) { 1564 memcpy(in.mworld[i], sh->e[slot[i]].o.field, sizeof in.mworld[i]); 1565 memcpy(ref->world_seen[n][i], in.mworld[i], sizeof in.mworld[i]); 1566 } 1567 AgOut o; 1568 semantics(x, skills, &in, &o); 1569 resolved[n] = 1; 1570 ref->r.status[n] = o.status; 1571 ref->r.value[n] = o.value; 1572 ref->r.attempts[n] = o.attempts; 1573 ref->r.steps[n] = o.steps; 1574 ref->r.evidence[n] = rx_graph_evidence_word(x, run, o.status, o.value, in.v, in.n_in); 1575 if (is_boundary(x->kind)) { 1576 RxObject so = sh->e[slot[0]].o; 1577 if (o.status == AG_OK && o.write) { 1578 if (x->kind == AG_WORLD_PUBLISH) { 1579 so.field[x->field] = o.value; 1580 ref->published[eff] = o.value; 1581 } else { 1582 so.field[2] = rx_graph_chain(so.field[2], run, in.v[0]); 1583 so.field[0] += 1; 1584 so.field[1] = in.v[0]; 1585 so.field[3] = run; 1586 ref->effect_count++; 1587 } 1588 shadow_write(sh, x->obj.id, &so); 1589 } 1590 ref->effect_chain[eff] = so.field[2]; 1591 eff++; 1592 } 1593 } 1594 ref->r.outcome = outcome(g, ref->r.status); 1595 return 0; 1596 } 1597 1598 int rx_graph_reference(const AgGraph *g, RxWorld *w, const AgSkillTable *skills, 1599 const AgCapTable *caps, uint64_t run, AgReference *ref) { 1600 memset(ref, 0, sizeof *ref); 1601 static __thread Shadow sh; 1602 sh.n = 0; 1603 for (uint32_t n = 0; n < g->n_nodes; n++) { 1604 RxObjRef objs[AG_META_MAX_OBJ]; 1605 uint32_t no = node_objs(&g->nodes[n], objs); 1606 for (uint32_t i = 0; i < no; i++) { 1607 RxObject o; 1608 int live = rx_world_read(w, objs[i], &o) == RX_OK; 1609 shadow_add(&sh, objs[i], live, live ? &o : NULL); 1610 } 1611 } 1612 return reference_core(g, skills, &sh, w, caps, run, ref); 1613 } 1614 1615 int rx_graph_reference_on(const AgGraph *g, const AgSkillTable *skills, const AgObjContents *objs, 1616 uint32_t n_objs, uint64_t run, AgReference *ref) { 1617 memset(ref, 0, sizeof *ref); 1618 static __thread Shadow sh; 1619 sh.n = 0; 1620 for (uint32_t i = 0; i < n_objs; i++) { 1621 RxObject o; 1622 memset(&o, 0, sizeof o); 1623 o.id = objs[i].obj.id; 1624 o.generation = objs[i].obj.generation; 1625 memcpy(o.field, objs[i].field, sizeof o.field); 1626 shadow_add(&sh, objs[i].obj, 1, &o); 1627 } 1628 for (uint32_t n = 0; n < g->n_nodes; n++) { 1629 RxObjRef r[AG_META_MAX_OBJ]; 1630 uint32_t no = node_objs(&g->nodes[n], r); 1631 for (uint32_t i = 0; i < no; i++) shadow_add(&sh, r[i], 0, NULL); 1632 } 1633 return reference_core(g, skills, &sh, NULL, NULL, run, ref); 1634 } 1635 1636 /* ---- MetaSkill: step programs, realizations, fusion ---- */ 1637 1638 uint64_t rx_graph_step_record(uint64_t acc, const uint8_t local_id[32], uint32_t status, 1639 uint64_t value, const uint64_t *in, uint32_t n_in) { 1640 sha256_ctx c; 1641 uint8_t d[32]; 1642 sha256_init(&c); 1643 sha256_update(&c, (const uint8_t *)"OMEGA_META_STEP_RECORD_V1", 25); 1644 put64(&c, acc); 1645 sha256_update(&c, local_id, 32); 1646 put32(&c, status); 1647 put64(&c, value); 1648 put32(&c, n_in); 1649 for (uint32_t i = 0; i < n_in; i++) put64(&c, in[i]); 1650 sha256_final(&c, d); 1651 return low64(d) | 1u; 1652 } 1653 1654 void rx_graph_meta_seal(AgMetaProgram *p) { 1655 for (uint32_t j = 0; j < p->n_steps && j < AG_META_MAX_STEPS; j++) { 1656 AgMetaStep *q = &p->step[j]; 1657 const AgNode *x = &q->node; 1658 sha256_ctx c; 1659 sha256_init(&c); 1660 sha256_update(&c, (const uint8_t *)"OMEGA_META_STEP_V1", 18); 1661 put32(&c, x->kind); 1662 put32(&c, x->out_type); 1663 put32(&c, x->op); 1664 put64(&c, x->imm); 1665 put64(&c, x->imm2); 1666 put32(&c, x->field); 1667 put32(&c, x->cost_us); 1668 put32(&c, x->n_fused); 1669 for (uint32_t i = 0; i < x->n_fused && i < AG_MAX_FUSED; i++) { 1670 put32(&c, x->fused[i].op); 1671 put64(&c, x->fused[i].imm); 1672 } 1673 put32(&c, (uint32_t)q->obj); 1674 put32(&c, q->obj >= 0 && (uint32_t)q->obj < AG_META_MAX_OBJ ? p->obj_rights[q->obj] : 0); 1675 put32(&c, q->n_in); 1676 for (uint32_t i = 0; i < q->n_in && i < AG_MAX_IN; i++) { 1677 put8(&c, q->in[i].step); 1678 put8(&c, q->in[i].mode); 1679 if (q->in[i].step && q->in[i].index < j) { 1680 sha256_update(&c, p->step[q->in[i].index].local_id, 32); 1681 } else { 1682 put32(&c, q->in[i].index); 1683 put32(&c, q->in[i].index < AG_MAX_IN ? p->in_type[q->in[i].index] : 0); 1684 } 1685 } 1686 sha256_final(&c, q->local_id); 1687 } 1688 sha256_ctx c; 1689 sha256_init(&c); 1690 sha256_update(&c, (const uint8_t *)"OMEGA_META_SIGNATURE_V1", 23); 1691 put32(&c, p->n_steps); 1692 for (uint32_t j = 0; j < p->n_steps && j < AG_META_MAX_STEPS; j++) 1693 sha256_update(&c, p->step[j].local_id, 32); 1694 put32(&c, p->n_in); 1695 for (uint32_t i = 0; i < AG_MAX_IN; i++) { 1696 put32(&c, p->in_type[i]); 1697 put8(&c, p->in_mode[i]); 1698 } 1699 put32(&c, p->n_obj); 1700 for (uint32_t i = 0; i < AG_META_MAX_OBJ; i++) put32(&c, p->obj_rights[i]); 1701 put32(&c, p->need.compute_class); 1702 put32(&c, p->need.locality); 1703 put32(&c, p->need.accelerator_features); 1704 put32(&c, p->need.latency_class); 1705 put64(&c, p->need.memory_bytes); 1706 put64(&c, p->need.deadline); 1707 put64(&c, p->need.energy_cost); 1708 put32(&c, p->out_type); 1709 sha256_final(&c, p->signature); 1710 } 1711 1712 static uint64_t world_key(const AgMetaProgram *p, 1713 const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]) { 1714 sha256_ctx c; 1715 uint8_t d[32]; 1716 sha256_init(&c); 1717 put32(&c, p->n_obj); 1718 for (uint32_t i = 0; i < p->n_obj && i < AG_META_MAX_OBJ; i++) 1719 for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) put64(&c, world[i][f]); 1720 sha256_final(&c, d); 1721 return low64(d); 1722 } 1723 1724 uint64_t rx_graph_meta_world_key(const AgMetaProgram *p, 1725 const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]) { 1726 return world_key(p, world); 1727 } 1728 1729 int rx_graph_meta_run(const AgRealization *r, const AgSkillTable *sk, const uint8_t *s, 1730 const uint64_t *v, uint32_t n_in, 1731 const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS], AgMetaOut *out) { 1732 memset(out, 0, sizeof *out); 1733 out->status = AG_FAILED; 1734 if (!r) return -1; 1735 const AgMetaProgram *p = &r->prog; 1736 if (n_in != p->n_in || n_in > AG_MAX_IN || p->n_steps == 0 || p->n_steps > AG_META_MAX_STEPS || 1737 p->n_obj > AG_META_MAX_OBJ) 1738 return -1; 1739 if (r->kind == AG_REAL_LEARNED || r->kind == AG_REAL_HYBRID) { 1740 uint64_t wk = world_key(p, world); 1741 for (uint32_t t = 0; t < r->n_table && t < AG_META_TABLE; t++) { 1742 const AgMetaEntry *q = &r->table[t]; 1743 int match = q->world == wk; 1744 for (uint32_t i = 0; i < n_in && match; i++) 1745 match = q->s[i] == s[i] && q->v[i] == v[i]; 1746 if (!match) continue; 1747 out->status = q->status; 1748 out->value = q->value; 1749 out->steps = q->steps; 1750 out->attempts = q->attempts; 1751 out->table_hit = 1; 1752 return 0; 1753 } 1754 if (r->kind == AG_REAL_LEARNED) return -1; /* outside what it learned */ 1755 } else if (r->kind != AG_REAL_COMPILED) { 1756 return -1; /* no host realization */ 1757 } 1758 uint8_t st[AG_META_MAX_STEPS]; 1759 uint64_t val[AG_META_MAX_STEPS]; 1760 uint64_t acc = 0; 1761 for (uint32_t j = 0; j < p->n_steps; j++) { 1762 const AgMetaStep *q = &p->step[j]; 1763 if (q->node.kind == AG_META || q->n_in > AG_MAX_IN) return -1; 1764 AgIn in; 1765 memset(&in, 0, sizeof in); 1766 in.n_in = q->n_in; 1767 for (uint32_t i = 0; i < q->n_in; i++) { 1768 uint32_t k = q->in[i].index; 1769 if (q->in[i].step) { 1770 if (k >= j) return -1; 1771 in.s[i] = st[k]; 1772 in.v[i] = val[k]; 1773 in.mode[i] = AG_EDGE_DATA; 1774 } else { 1775 if (k >= n_in) return -1; 1776 in.s[i] = s[k]; 1777 in.v[i] = v[k]; 1778 in.mode[i] = q->in[i].mode; 1779 } 1780 } 1781 if (q->obj >= 0) { 1782 if ((uint32_t)q->obj >= p->n_obj) return -1; 1783 memcpy(in.world, world[q->obj], sizeof in.world); 1784 } 1785 AgOut o; 1786 semantics(&q->node, sk, &in, &o); 1787 st[j] = o.status; 1788 val[j] = o.value; 1789 acc = rx_graph_step_record(acc, q->local_id, o.status, o.value, in.v, in.n_in); 1790 if (o.status == AG_OK) out->busy_us += q->node.cost_us; 1791 if (j + 1 == p->n_steps) { 1792 out->status = o.status; 1793 out->value = o.value; 1794 out->attempts = o.attempts; 1795 } 1796 } 1797 out->steps = acc; 1798 return 0; 1799 } 1800 1801 static void meta_semantics(const AgNode *x, const AgSkillTable *sk, const AgIn *in, AgOut *o) { 1802 const AgRealization *r = NULL; 1803 for (uint32_t i = 0; sk && i < sk->n_meta && i < AG_MAX_SKILLS; i++) 1804 if (sk->meta[i].id == x->op) r = sk->meta[i].r; 1805 o->status = AG_FAILED; 1806 /* Only the realization this node was built for may run in it. */ 1807 if (!r || word64(r->identity) != x->imm || word64(r->identity + 8) != x->imm2) return; 1808 AgMetaOut mo; 1809 rx_graph_meta_run(r, sk, in->s, in->v, in->n_in, in->mworld, &mo); 1810 o->status = mo.status; 1811 o->value = mo.value; 1812 o->attempts = mo.attempts; 1813 o->steps = mo.steps; 1814 o->busy_us = mo.busy_us; 1815 } 1816 1817 /* Canonical bytes of a realization: the steps as they will run (not only 1818 * the identities they claim) and the learned table. */ 1819 typedef struct { uint8_t *buf; size_t cap, len; } Enc; 1820 1821 static void e8(Enc *e, uint8_t v) { 1822 if (e->buf && e->len < e->cap) e->buf[e->len] = v; 1823 e->len++; 1824 } 1825 static void e32(Enc *e, uint32_t v) { for (int i = 3; i >= 0; i--) e8(e, (uint8_t)(v >> (8 * i))); } 1826 static void e64(Enc *e, uint64_t v) { e32(e, (uint32_t)(v >> 32)); e32(e, (uint32_t)v); } 1827 static void ebytes(Enc *e, const uint8_t *d, size_t n) { for (size_t i = 0; i < n; i++) e8(e, d[i]); } 1828 1829 size_t rx_graph_realization_encode(const AgRealization *r, uint8_t *buf, size_t cap) { 1830 Enc e = { buf, cap, 0 }; 1831 const AgMetaProgram *p = &r->prog; 1832 e32(&e, r->kind); 1833 ebytes(&e, p->signature, 32); 1834 e32(&e, p->n_in); 1835 for (uint32_t i = 0; i < AG_MAX_IN; i++) { 1836 e32(&e, p->in_type[i]); 1837 e8(&e, p->in_mode[i]); 1838 } 1839 e32(&e, p->n_obj); 1840 for (uint32_t i = 0; i < AG_META_MAX_OBJ; i++) e32(&e, p->obj_rights[i]); 1841 e32(&e, p->out_type); 1842 e32(&e, p->n_steps); 1843 for (uint32_t j = 0; j < p->n_steps && j < AG_META_MAX_STEPS; j++) { 1844 const AgMetaStep *q = &p->step[j]; 1845 ebytes(&e, q->local_id, 32); 1846 e32(&e, q->node.kind); 1847 e32(&e, q->node.out_type); 1848 e32(&e, q->node.op); 1849 e64(&e, q->node.imm); 1850 e64(&e, q->node.imm2); 1851 e32(&e, q->node.field); 1852 e32(&e, q->node.cost_us); 1853 e32(&e, q->node.n_fused); 1854 for (uint32_t i = 0; i < q->node.n_fused && i < AG_MAX_FUSED; i++) { 1855 e32(&e, q->node.fused[i].op); 1856 e64(&e, q->node.fused[i].imm); 1857 } 1858 e32(&e, (uint32_t)q->obj); 1859 e32(&e, q->n_in); 1860 for (uint32_t i = 0; i < q->n_in && i < AG_MAX_IN; i++) { 1861 e8(&e, q->in[i].step); 1862 e8(&e, q->in[i].index); 1863 e8(&e, q->in[i].mode); 1864 } 1865 } 1866 e32(&e, r->n_table); 1867 for (uint32_t t = 0; t < r->n_table && t < AG_META_TABLE; t++) { 1868 const AgMetaEntry *q = &r->table[t]; 1869 for (uint32_t i = 0; i < AG_MAX_IN; i++) { 1870 e8(&e, q->s[i]); 1871 e64(&e, q->v[i]); 1872 } 1873 e64(&e, q->world); 1874 e8(&e, q->status); 1875 e64(&e, q->value); 1876 e64(&e, q->steps); 1877 e32(&e, q->attempts); 1878 } 1879 return e.len; 1880 } 1881 1882 void rx_graph_realization_identity(AgRealization *r) { 1883 static __thread uint8_t buf[AG_REALIZATION_MAX_BYTES]; 1884 size_t n = rx_graph_realization_encode(r, buf, sizeof buf); 1885 sha256_ctx c; 1886 sha256_init(&c); 1887 sha256_update(&c, (const uint8_t *)"OMEGA_REALIZATION_V1", 20); 1888 sha256_update(&c, buf, n < sizeof buf ? n : sizeof buf); 1889 put64(&c, n); 1890 sha256_final(&c, r->identity); 1891 } 1892 1893 int rx_graph_fuse(AgGraph *g, uint64_t members, uint32_t exit, uint32_t skill_id, 1894 const AgRealization *r, const uint16_t *ext, uint32_t n_ext, 1895 const RxObjRef *objs, uint32_t n_objs) { 1896 if (!g || !r || exit >= g->n_nodes || !((members >> exit) & 1u) || n_ext > AG_MAX_IN || 1897 n_objs > AG_META_MAX_OBJ || n_ext != r->prog.n_in || n_objs != r->prog.n_obj) 1898 return AG_E_ARG; 1899 for (uint32_t i = 0; i < n_ext; i++) 1900 if (ext[i] >= g->n_nodes || ((members >> ext[i]) & 1u) || !g->nodes[ext[i]].alive) 1901 return AG_E_ARG; 1902 for (uint32_t n = 0; n < g->n_nodes; n++) 1903 if (((members >> n) & 1u) && (!g->nodes[n].alive || is_boundary(g->nodes[n].kind))) 1904 return AG_E_ARG; 1905 int mi = rx_graph_node(g, AG_META, g->nodes[exit].out_type); 1906 if (mi < 0) return mi; 1907 uint32_t m = (uint32_t)mi; 1908 AgNode *x = &g->nodes[m]; 1909 x->op = skill_id; 1910 x->imm = word64(r->identity); 1911 x->imm2 = word64(r->identity + 8); 1912 x->n_mobj = n_objs; 1913 for (uint32_t i = 0; i < n_objs; i++) x->mobj[i] = objs[i]; 1914 for (uint32_t i = 0; i < n_ext; i++) x->in_type[i] = r->prog.in_type[i]; 1915 x->origin = g->nodes[exit].origin; 1916 for (uint32_t n = 0; n < g->n_nodes; n++) 1917 if ((members >> n) & 1u) x->cost_us += g->nodes[n].cost_us; 1918 for (uint32_t i = 0; i < n_ext; i++) 1919 if (rx_graph_data(g, ext[i], m, i, r->prog.in_mode[i]) != 0) return AG_E_FULL; 1920 /* What the exit fed now comes from the fused node. */ 1921 for (uint32_t e = 0; e < g->n_data; e++) 1922 if (g->data[e].from == exit && !((members >> g->data[e].to) & 1u)) g->data[e].from = (uint16_t)m; 1923 for (uint32_t e = 0; e < g->n_deps; e++) 1924 if (g->deps[e].from == exit && !((members >> g->deps[e].to) & 1u)) g->deps[e].from = (uint16_t)m; 1925 /* Authority, resources, conditions and evidence of the members. */ 1926 uint32_t n_auth = g->n_auth; 1927 for (uint32_t i = 0; i < n_auth; i++) 1928 if ((members >> g->auth[i].node) & 1u) 1929 if (rx_graph_need_authority(g, m, g->auth[i].resource, g->auth[i].rights) != 0) return AG_E_FULL; 1930 RxResourceNeed need; 1931 memset(&need, 0, sizeof need); 1932 int any = 0; 1933 for (uint32_t i = 0; i < g->n_res; i++) { 1934 if (!((members >> g->res[i].node) & 1u)) continue; 1935 const RxResourceNeed *q = &g->res[i].need; 1936 if (q->compute_class && need.compute_class && q->compute_class != need.compute_class) 1937 return AG_E_ARG; 1938 if (q->compute_class) need.compute_class = q->compute_class; 1939 need.locality |= q->locality; 1940 need.accelerator_features |= q->accelerator_features; 1941 if (q->latency_class > need.latency_class) need.latency_class = q->latency_class; 1942 if (q->memory_bytes > need.memory_bytes) need.memory_bytes = q->memory_bytes; 1943 if (q->deadline && (!need.deadline || q->deadline < need.deadline)) need.deadline = q->deadline; 1944 need.energy_cost += q->energy_cost; 1945 any = 1; 1946 } 1947 if (any && rx_graph_need_resource(g, m, &need) != 0) return AG_E_FULL; 1948 for (uint32_t i = 0; i < g->n_success; i++) 1949 if (g->success[i].node == exit) g->success[i].node = (uint16_t)m; 1950 for (uint32_t i = 0; i < g->n_failure; i++) 1951 if (g->failure[i].node == exit) g->failure[i].node = (uint16_t)m; 1952 int owed = 0; 1953 for (uint32_t i = 0; i < g->n_evidence; i++) owed |= (int)((members >> g->evidence[i]) & 1u); 1954 if (owed) rx_graph_evidence(g, m); 1955 for (uint32_t n = 0; n < g->n_nodes; n++) 1956 if ((members >> n) & 1u) { 1957 drop_edges_of(g, n); 1958 g->nodes[n].alive = 0; 1959 } 1960 compact(g); 1961 return (int)(g->n_nodes - 1); 1962 }