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_graph.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_graph.h -- Omega action graph IR (OMEGA_ACTION_GRAPH_IR). |
| 19 | * |
| 20 | * Routine multi-step behaviour does not go back through deliberation every |
| 21 | * time. AIEN states a goal; Omega compiles it, with the World state, the |
| 22 | * capabilities its principal already holds and the goal's constraints, into a |
| 23 | * typed dependency graph. Omega checks the graph, optimizes it, and lowers |
| 24 | * each node onto one resident reaction (rx_world.h). From then on the graph |
| 25 | * runs by readiness alone: a node runs when the cells it depends on hold this |
| 26 | * run's values. No function sequences the nodes. Independent nodes run on |
| 27 | * whatever workers are free. |
| 28 | * |
| 29 | * Goal + World + Capabilities + Constraints |
| 30 | * -> rx_graph_compile instantiate a procedure, bind World objects, |
| 31 | * resolve authority (never mint), check resources, |
| 32 | * generations and constraints, type check |
| 33 | * -> rx_graph_optimize semantic-preserving passes |
| 34 | * -> rx_graph_lower one cell object + one reaction per node |
| 35 | * -> rx_graph_start publish a run token; the world does the rest |
| 36 | * |
| 37 | * The graph is typed Omega state, not text: nodes, typed data edges, control |
| 38 | * dependencies, authority and resource requirements, success and failure |
| 39 | * conditions, evidence requirements and effect boundaries. Its identity is |
| 40 | * SHA-256 over a canonical encoding that does not depend on builder order. |
| 41 | * |
| 42 | * Authority. Every node declares what it needs. Compilation looks it up in |
| 43 | * the principal's capability table and validates it through the world's |
| 44 | * authority view. It has no path to mint: this file takes no admin handle. |
| 45 | * A requirement that is missing is reported. When an AEGIS client binding is |
| 46 | * given to the lowering, the node reads its authority from a capability slot |
| 47 | * and an ask reaction publishes the request, so new authority arrives only |
| 48 | * through AEGIS policy and the AIENOS root (rx_aegis.h). The engine checks |
| 49 | * every node's authority again when it starts and when it publishes. |
| 50 | * |
| 51 | * Effects. Proposing an effect (EFFECT_PROPOSE) is thinking: a value, no |
| 52 | * authority, no change outside the graph. Performing it (EFFECT_PERFORM) is |
| 53 | * an explicit node that needs WRITE|EFFECT on the effect's resource, a |
| 54 | * passing verdict, and its place in the graph's effect chain. World |
| 55 | * publications are effect boundaries too. Passes never merge, remove or |
| 56 | * reorder boundary nodes. |
| 57 | */ |
| 58 | #ifndef RX_GRAPH_H |
| 59 | #define RX_GRAPH_H |
| 60 | |
| 61 | #include "rx_world.h" |
| 62 | #include "rx_aegis.h" |
| 63 | |
| 64 | #include <stddef.h> |
| 65 | #include <stdint.h> |
| 66 | |
| 67 | #define AG_MAX_NODES 64u |
| 68 | #define AG_MAX_IN 4u /* data inputs per node */ |
| 69 | #define AG_MAX_EDGES (AG_MAX_NODES * AG_MAX_IN) |
| 70 | #define AG_MAX_DEPS (AG_MAX_NODES * 2u) |
| 71 | #define AG_MAX_FUSED 8u |
| 72 | #define AG_MAX_REQS AG_MAX_NODES |
| 73 | #define AG_MAX_CONDS 8u |
| 74 | #define AG_MAX_SKILLS 16u |
| 75 | #define AG_MAX_CAPS 16u |
| 76 | #define AG_MAX_PROCS 8u |
| 77 | #define AG_META_MAX_STEPS 8u /* steps one fused node (MetaSkill) runs */ |
| 78 | #define AG_META_MAX_OBJ 2u /* World objects a fused node reads */ |
| 79 | #define AG_META_TABLE 64u /* entries of a learned realization */ |
| 80 | |
| 81 | /* Value types carried on data edges. */ |
| 82 | typedef enum { |
| 83 | AG_T_NONE = 0, |
| 84 | AG_T_U64, |
| 85 | AG_T_BOOL, |
| 86 | AG_T_PROPOSAL, /* a proposed effect: thought, not performed */ |
| 87 | AG_T_VERDICT, /* a verification outcome */ |
| 88 | AG_T_RECEIPT /* what a performed effect or publication returns */ |
| 89 | } AgType; |
| 90 | |
| 91 | typedef enum { |
| 92 | AG_CONST = 1, |
| 93 | AG_PURE, /* Omega pure operation (omega_eval_pure_binary_uint), maybe fused */ |
| 94 | AG_WORLD_READ, /* read one field of a World object */ |
| 95 | AG_WORLD_PUBLISH, /* write one field of a World object (effect boundary) */ |
| 96 | AG_RECALL, /* Cortex memory lookup by key */ |
| 97 | AG_CAP_RESOLVE, /* does the principal hold this authority? decided at compile */ |
| 98 | AG_PHYSICAL, /* physical computation with a declared resource need */ |
| 99 | AG_SKILL, /* reusable procedure from the Skill Net table */ |
| 100 | AG_EFFECT_PROPOSE, /* thinking about an effect */ |
| 101 | AG_EFFECT_PERFORM, /* performing it (effect boundary) */ |
| 102 | AG_VERIFY, /* verdict: input within [imm, imm2] */ |
| 103 | AG_BRANCH, /* bool decision other nodes are guarded by */ |
| 104 | AG_JOIN, /* the one input that is OK */ |
| 105 | AG_RETRY, /* bounded retry of a skill */ |
| 106 | AG_META, /* a published MetaSkill: several verified steps in one node (rx_fusion.h) */ |
| 107 | AG_KIND_COUNT |
| 108 | } AgKind; |
| 109 | |
| 110 | /* Data edge modes. ON_FAIL runs its node only when the source failed. */ |
| 111 | enum { AG_EDGE_DATA = 0, AG_EDGE_ON_FAIL = 1 }; |
| 112 | |
| 113 | /* Control dependency kinds. */ |
| 114 | enum { AG_DEP_GUARD = 1, /* run only when the source's bool equals polarity */ |
| 115 | AG_DEP_ORDER = 2 }; /* wait for the source to be resolved (effect chain) */ |
| 116 | |
| 117 | /* Node status in a run. */ |
| 118 | enum { AG_PENDING = 0, AG_OK = 1, AG_FAILED = 2, AG_SKIPPED = 3 }; |
| 119 | |
| 120 | /* Graph outcome. */ |
| 121 | enum { AG_RUN_SUCCESS = 1, AG_RUN_FAILURE, AG_RUN_INCOMPLETE }; |
| 122 | |
| 123 | /* Compile verdicts and errors. */ |
| 124 | #define AG_OK_READY 0 |
| 125 | #define AG_E_ARG -1 |
| 126 | #define AG_E_FULL -2 |
| 127 | #define AG_E_TYPE -3 |
| 128 | #define AG_E_CYCLE -4 |
| 129 | #define AG_E_STALE -5 |
| 130 | #define AG_E_CONSTRAINT -6 |
| 131 | #define AG_E_NO_PROCEDURE -7 |
| 132 | #define AG_E_EFFECT_ORDER -8 |
| 133 | #define AG_E_DEP_BUDGET -9 |
| 134 | #define AG_E_JOIN -10 |
| 135 | #define AG_E_LOWER -11 |
| 136 | |
| 137 | typedef struct { |
| 138 | uint32_t op; /* OpCode */ |
| 139 | uint64_t imm; /* right operand */ |
| 140 | } AgFusedStep; |
| 141 | |
| 142 | typedef struct { |
| 143 | AgKind kind; |
| 144 | AgType out_type; |
| 145 | uint32_t op; /* PURE: OpCode; SKILL/RETRY: skill id */ |
| 146 | uint64_t imm, imm2; /* CONST value; PURE right operand when unary; RECALL key; |
| 147 | VERIFY bounds; CAP_RESOLVE resource/rights; RETRY |
| 148 | max attempts / leading failures of the stand-in skill */ |
| 149 | uint32_t param; /* procedure parameter the World object binds from (1-based) */ |
| 150 | RxObjRef obj; /* READ/PUBLISH/RECALL/PERFORM target */ |
| 151 | uint32_t field; |
| 152 | uint32_t cost_us; /* work this node really does (busy time) */ |
| 153 | uint32_t n_fused; /* PURE after fusion: steps applied after the head op */ |
| 154 | AgFusedStep fused[AG_MAX_FUSED]; |
| 155 | uint8_t id[32]; /* semantic identity (rx_graph_identify) */ |
| 156 | uint32_t origin; /* index in the graph it was first built in */ |
| 157 | uint8_t alive; |
| 158 | /* META only. op = skill id; imm, imm2 = the first 128 bits of the |
| 159 | * realization identity the node was built for. */ |
| 160 | uint32_t n_mobj; |
| 161 | RxObjRef mobj[AG_META_MAX_OBJ]; /* World objects its steps read, by slot */ |
| 162 | AgType in_type[AG_MAX_IN]; /* input contract, by port */ |
| 163 | } AgNode; |
| 164 | |
| 165 | typedef struct { uint16_t from, to; uint8_t port, mode; AgType type; } AgDataEdge; |
| 166 | typedef struct { uint16_t from, to; uint8_t kind, polarity; } AgDep; |
| 167 | typedef struct { uint16_t node; uint64_t resource; uint32_t rights; } AgAuthReq; |
| 168 | typedef struct { uint16_t node; RxResourceNeed need; } AgResReq; |
| 169 | typedef struct { uint16_t node; uint8_t status; } AgCond; |
| 170 | |
| 171 | typedef struct { |
| 172 | uint32_t n_nodes; |
| 173 | AgNode nodes[AG_MAX_NODES]; |
| 174 | uint32_t n_data; |
| 175 | AgDataEdge data[AG_MAX_EDGES]; |
| 176 | uint32_t n_deps; |
| 177 | AgDep deps[AG_MAX_DEPS]; |
| 178 | uint32_t n_auth; |
| 179 | AgAuthReq auth[AG_MAX_REQS]; |
| 180 | uint32_t n_res; |
| 181 | AgResReq res[AG_MAX_REQS]; |
| 182 | uint32_t n_success; |
| 183 | AgCond success[AG_MAX_CONDS]; /* all must hold for AG_RUN_SUCCESS */ |
| 184 | uint32_t n_failure; |
| 185 | AgCond failure[AG_MAX_CONDS]; /* any one makes AG_RUN_FAILURE */ |
| 186 | uint32_t n_evidence; |
| 187 | uint16_t evidence[AG_MAX_NODES]; /* nodes whose evidence must exist after a run */ |
| 188 | uint32_t n_effects; |
| 189 | uint16_t effects[AG_MAX_NODES]; /* effect boundaries, in their required order */ |
| 190 | uint32_t subject; /* the principal the graph runs as */ |
| 191 | uint8_t digest[32]; |
| 192 | } AgGraph; |
| 193 | |
| 194 | /* ---- building ---- */ |
| 195 | |
| 196 | void rx_graph_init(AgGraph *g, uint32_t subject); |
| 197 | int rx_graph_node(AgGraph *g, AgKind kind, AgType out); /* index or AG_E_FULL */ |
| 198 | int rx_graph_data(AgGraph *g, uint32_t from, uint32_t to, uint32_t port, uint32_t mode); |
| 199 | int rx_graph_guard(AgGraph *g, uint32_t branch, uint32_t node, uint32_t polarity); |
| 200 | int rx_graph_order(AgGraph *g, uint32_t before, uint32_t after); |
| 201 | int rx_graph_need_authority(AgGraph *g, uint32_t node, uint64_t resource, uint32_t rights); |
| 202 | int rx_graph_need_resource(AgGraph *g, uint32_t node, const RxResourceNeed *need); |
| 203 | int rx_graph_success(AgGraph *g, uint32_t node, uint32_t status); |
| 204 | int rx_graph_failure(AgGraph *g, uint32_t node, uint32_t status); |
| 205 | int rx_graph_evidence(AgGraph *g, uint32_t node); |
| 206 | |
| 207 | /* Declares every derivable requirement (authority on READ/PUBLISH/RECALL/ |
| 208 | * PERFORM targets, evidence on VERIFY/PERFORM/PUBLISH, effect boundaries in |
| 209 | * chain order), then checks: types, acyclicity, one total order over effect |
| 210 | * boundaries, joins fed by mutually exclusive arms, the per-node dependency |
| 211 | * budget. Fills identities and the digest. */ |
| 212 | int rx_graph_validate(AgGraph *g, const RxWorld *w); |
| 213 | |
| 214 | /* Semantic identity of every node and the graph digest. */ |
| 215 | void rx_graph_identify(AgGraph *g); |
| 216 | |
| 217 | /* ---- compile ---- */ |
| 218 | |
| 219 | /* A reusable procedure (Skill Net). Deterministic function of its inputs. */ |
| 220 | typedef uint64_t (*AgSkillFn)(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed); |
| 221 | |
| 222 | /* ---- MetaSkill realizations (what an AG_META node runs) ---- |
| 223 | * |
| 224 | * A step program is a fused fragment: its steps in a canonical order (the |
| 225 | * exit last), each step an ordinary node whose inputs are either an external |
| 226 | * port of the fused node or an earlier step. Every step runs through the same |
| 227 | * node semantics as the graph it came from. */ |
| 228 | typedef struct { |
| 229 | AgNode node; /* kind, out type, op, imm, imm2, field, cost, fused steps */ |
| 230 | uint32_t n_in; |
| 231 | struct { uint8_t step; uint8_t index; uint8_t mode; } in[AG_MAX_IN]; |
| 232 | /* step 1: earlier step `index`; 0: external port `index` */ |
| 233 | int32_t obj; /* object slot it reads, or -1 */ |
| 234 | uint8_t local_id[32]; /* identity inside the fragment (not the graph around it) */ |
| 235 | } AgMetaStep; |
| 236 | |
| 237 | typedef struct { |
| 238 | uint32_t n_steps; |
| 239 | AgMetaStep step[AG_META_MAX_STEPS]; |
| 240 | uint32_t n_in; |
| 241 | AgType in_type[AG_MAX_IN]; |
| 242 | uint8_t in_mode[AG_MAX_IN]; /* AG_EDGE_DATA or AG_EDGE_ON_FAIL, per port */ |
| 243 | uint32_t n_obj; |
| 244 | uint32_t obj_rights[AG_META_MAX_OBJ]; |
| 245 | RxResourceNeed need; /* merged resource need of the steps */ |
| 246 | AgType out_type; |
| 247 | uint8_t signature[32]; /* steps, internal dependencies, authority pattern, contracts */ |
| 248 | } AgMetaProgram; |
| 249 | |
| 250 | typedef enum { |
| 251 | AG_REAL_COMPILED = 1, /* deterministic: the step program, register-resolved, one reaction */ |
| 252 | AG_REAL_LEARNED, /* a table learned from verified observations; nothing else */ |
| 253 | AG_REAL_HYBRID, /* the learned table, and the step program on a miss */ |
| 254 | AG_REAL_HARDWARE /* a hardware realization; none exists on this host */ |
| 255 | } AgRealKind; |
| 256 | |
| 257 | /* A learned entry: inputs (status and value by port) and the World fields |
| 258 | * read, to the result and the step evidence the reference produced. */ |
| 259 | typedef struct { |
| 260 | uint8_t s[AG_MAX_IN]; |
| 261 | uint64_t v[AG_MAX_IN]; |
| 262 | uint64_t world; /* digest of the fields of every object slot */ |
| 263 | uint8_t status; |
| 264 | uint64_t value; |
| 265 | uint64_t steps; |
| 266 | uint32_t attempts; |
| 267 | } AgMetaEntry; |
| 268 | |
| 269 | typedef struct { |
| 270 | AgRealKind kind; |
| 271 | AgMetaProgram prog; /* COMPILED, HYBRID: what runs; others: the contract it serves */ |
| 272 | uint32_t n_table; |
| 273 | AgMetaEntry table[AG_META_TABLE]; |
| 274 | uint8_t identity[32]; /* rx_graph_realization_identity */ |
| 275 | } AgRealization; |
| 276 | |
| 277 | typedef struct { |
| 278 | uint32_t id; |
| 279 | AgSkillFn fn; |
| 280 | uint8_t identity[32]; /* content identity of the procedure */ |
| 281 | } AgSkill; |
| 282 | |
| 283 | typedef struct { |
| 284 | uint32_t n; |
| 285 | AgSkill skill[AG_MAX_SKILLS]; |
| 286 | /* Published MetaSkills: what an AG_META node with this skill id runs. |
| 287 | * Only rx_fusion_publish adds here, after the generation barrier. */ |
| 288 | uint32_t n_meta; |
| 289 | struct { uint32_t id; const AgRealization *r; } meta[AG_MAX_SKILLS]; |
| 290 | } AgSkillTable; |
| 291 | |
| 292 | /* A procedure: a graph template whose World objects are parameters. */ |
| 293 | typedef struct { |
| 294 | uint32_t goal_kind; |
| 295 | const AgGraph *tmpl; |
| 296 | } AgProcedure; |
| 297 | |
| 298 | typedef struct { |
| 299 | uint32_t n; |
| 300 | AgProcedure proc[AG_MAX_PROCS]; |
| 301 | } AgLibrary; |
| 302 | |
| 303 | typedef struct { |
| 304 | uint32_t kind; /* selects the procedure */ |
| 305 | uint32_t n_args; |
| 306 | RxObjRef args[8]; /* World objects bound to parameters 1..n */ |
| 307 | } AgGoal; |
| 308 | |
| 309 | /* Capabilities the principal already holds. Compile only reads this. */ |
| 310 | typedef struct { |
| 311 | uint32_t subject; /* the principal these belong to */ |
| 312 | uint32_t n; |
| 313 | struct { RxCapRef ref; uint64_t resource; uint32_t rights; } cap[AG_MAX_CAPS]; |
| 314 | } AgCapTable; |
| 315 | |
| 316 | typedef struct { |
| 317 | uint32_t max_effects; /* 0 = none allowed; UINT32_MAX = any */ |
| 318 | uint64_t forbid_lo, forbid_hi; /* no node may touch these resources (hi 0 = none) */ |
| 319 | uint64_t critical_path_us; /* 0 = no deadline */ |
| 320 | RxResourceBudget budget; /* what the body offers now */ |
| 321 | } AgConstraints; |
| 322 | |
| 323 | typedef struct { |
| 324 | int verdict; /* AG_OK_READY or AG_E_* */ |
| 325 | uint32_t n_missing; |
| 326 | AgAuthReq missing[AG_MAX_REQS]; /* authority the principal does not hold */ |
| 327 | uint32_t n_resource_blocked; |
| 328 | uint16_t resource_blocked[AG_MAX_NODES]; |
| 329 | uint32_t n_stale; |
| 330 | uint16_t stale[AG_MAX_NODES]; |
| 331 | uint64_t critical_path_us; |
| 332 | uint64_t total_work_us; |
| 333 | uint32_t nodes_before, nodes_after; |
| 334 | uint32_t reactions_before, reactions_after; |
| 335 | struct { uint32_t const_folded, cse, world_reads, fused, dead, deps_dropped; } passes; |
| 336 | } AgReport; |
| 337 | |
| 338 | /* Goal -> typed graph. Validates capabilities through w's authority view; it |
| 339 | * never mints and has no handle that could. Runs rx_graph_optimize when |
| 340 | * `optimize` is set. The graph is produced even when authority or resources |
| 341 | * are missing (the report says what); it is refused for stale World |
| 342 | * references, violated constraints, and type or structure errors. */ |
| 343 | int rx_graph_compile(const AgGoal *goal, RxWorld *w, const AgCapTable *caps, |
| 344 | const AgConstraints *cons, const AgLibrary *lib, int optimize, |
| 345 | AgGraph *out, AgReport *rep); |
| 346 | |
| 347 | /* The tail of compile: costs and the critical-path constraint, missing |
| 348 | * authority, resource-blocked nodes. Also used after a graph is rewritten |
| 349 | * (rx_fusion_apply). Returns AG_OK_READY or AG_E_CONSTRAINT. */ |
| 350 | int rx_graph_report_needs(const AgGraph *g, RxWorld *w, const AgCapTable *caps, |
| 351 | const AgConstraints *cons, AgReport *rep); |
| 352 | |
| 353 | /* Semantic-preserving passes, in order: constant propagation, common |
| 354 | * subexpression elimination (identity-equal pure nodes and World reads with |
| 355 | * no intervening publication of that object), pure-node fusion, dependency |
| 356 | * simplification (implied order edges, order edges that only delay an |
| 357 | * effect-free node), dead-node elimination (evidence, success/failure and |
| 358 | * boundary nodes are roots). Parallelism needs no pass: lowering adds no order the |
| 359 | * graph does not state. Returns 0 and fills rep->passes. */ |
| 360 | int rx_graph_optimize(AgGraph *g, AgReport *rep); |
| 361 | |
| 362 | /* 0 when `after` keeps every effect boundary of `before` (same identity, |
| 363 | * same pairwise order), every evidence node, and every success/failure |
| 364 | * condition; negative otherwise. */ |
| 365 | int rx_graph_check_preserved(const AgGraph *before, const AgGraph *after); |
| 366 | |
| 367 | /* Critical path and total work of the graph from node cost_us. */ |
| 368 | void rx_graph_costs(const AgGraph *g, uint64_t *critical_us, uint64_t *total_us); |
| 369 | |
| 370 | /* ---- lowering ---- */ |
| 371 | |
| 372 | /* Cell object type and layout: 0 run, 1 status, 2 value, 3 evidence, |
| 373 | * 4 attempts. Every field is proposed on every run. */ |
| 374 | #define AG_OT_CELL 0x5A01u |
| 375 | #define AG_OT_RUN 0x5A02u |
| 376 | #define AG_OT_EFFECT 0x5A03u /* 0 count, 1 last value, 2 order chain, 3 last run */ |
| 377 | #define AG_OT_MEMORY 0x5A04u /* key/value pairs in fields (0,1) (2,3) (4,5) (6,7) */ |
| 378 | |
| 379 | /* Optional: route missing authority through an AEGIS client. */ |
| 380 | typedef struct { |
| 381 | const RxAegisClientObjects *client; |
| 382 | RxCapRef request_cap; /* principal RW on its request object */ |
| 383 | RxCapRef slot_cap[RX_AEGIS_SLOTS]; /* principal R on each slot */ |
| 384 | uint64_t request_res, slot_res[RX_AEGIS_SLOTS]; |
| 385 | } AgAegisBinding; |
| 386 | |
| 387 | typedef struct AgLowered { |
| 388 | RxWorld *w; |
| 389 | const AgGraph *g; |
| 390 | const AgSkillTable *skills; |
| 391 | RxObjRef run; /* run token: field 0 = run id */ |
| 392 | RxObjRef cell[AG_MAX_NODES]; |
| 393 | uint32_t reaction[AG_MAX_NODES]; |
| 394 | uint32_t n_ask; |
| 395 | uint32_t ask_reaction[RX_AEGIS_SLOTS]; |
| 396 | uint64_t cell_res, run_res; |
| 397 | const RxAegisClientObjects *aegis_client; |
| 398 | struct { uint32_t node; uint32_t slot; } asked[RX_AEGIS_SLOTS]; |
| 399 | AgAuthReq ask_req[RX_AEGIS_SLOTS]; |
| 400 | struct AgNodeCtx { const struct AgLowered *L; uint32_t node; } nctx[AG_MAX_NODES]; |
| 401 | struct AgAskCtx { const struct AgLowered *L; uint32_t k; } actx[RX_AEGIS_SLOTS]; |
| 402 | } AgLowered; |
| 403 | |
| 404 | /* Create the run token and one cell per live node (resource cell_res), and |
| 405 | * one reaction per node. cell_cap: principal RW on cell_res; run_cap: |
| 406 | * principal R on run_res. Missing authority lowers to an unusable reference |
| 407 | * (the engine blocks the node and records it) or, with `aegis`, to a slot. */ |
| 408 | int rx_graph_lower(AgLowered *L, RxWorld *w, const AgGraph *g, const AgSkillTable *skills, |
| 409 | const AgCapTable *caps, uint64_t cell_res, RxCapRef cell_cap, |
| 410 | uint64_t run_res, RxCapRef run_cap, const AgAegisBinding *aegis); |
| 411 | |
| 412 | /* Publish run id `run` on the token. The only thing the caller does. */ |
| 413 | int64_t rx_graph_start(AgLowered *L, RxCapRef external_run_cap, uint64_t run); |
| 414 | |
| 415 | /* Read the run's per-node status/value and decide the outcome. */ |
| 416 | typedef struct { |
| 417 | uint64_t run; |
| 418 | uint8_t status[AG_MAX_NODES]; |
| 419 | uint64_t value[AG_MAX_NODES]; |
| 420 | uint64_t evidence[AG_MAX_NODES]; |
| 421 | uint32_t attempts[AG_MAX_NODES]; |
| 422 | uint64_t steps[AG_MAX_NODES]; /* META: step evidence (rx_graph_step_record chain) */ |
| 423 | int outcome; |
| 424 | } AgResult; |
| 425 | |
| 426 | int rx_graph_collect(AgLowered *L, uint64_t run, AgResult *out); |
| 427 | |
| 428 | /* ---- reference ---- */ |
| 429 | |
| 430 | /* The same node semantics, one node at a time in a fixed topological order, |
| 431 | * against a copy of the World. Effects and publications go to a shadow; the |
| 432 | * World is not changed. `effect_chain` receives the chain value each effect |
| 433 | * object would hold (indexed like g->effects). `stale` marks a node whose |
| 434 | * World object is no longer live; it and its dependants stay PENDING. */ |
| 435 | typedef struct { |
| 436 | AgResult r; |
| 437 | uint64_t effect_count; |
| 438 | uint64_t effect_chain[AG_MAX_NODES]; |
| 439 | uint64_t published[AG_MAX_NODES]; |
| 440 | /* The World fields each node saw when it ran, by object slot (slot 0 |
| 441 | * for READ/RECALL/PERFORM; META by its own slots). */ |
| 442 | uint64_t world_seen[AG_MAX_NODES][AG_META_MAX_OBJ][RX_MAX_FIELDS]; |
| 443 | } AgReference; |
| 444 | |
| 445 | int rx_graph_reference(const AgGraph *g, RxWorld *w, const AgSkillTable *skills, |
| 446 | const AgCapTable *caps, uint64_t run, AgReference *out); |
| 447 | |
| 448 | /* The reference against given object contents instead of a World: every |
| 449 | * object named here is live with these fields, every other is stale, and |
| 450 | * authority is not consulted. Used to test a fragment on inputs no World |
| 451 | * holds. */ |
| 452 | typedef struct { |
| 453 | RxObjRef obj; |
| 454 | uint64_t field[RX_MAX_FIELDS]; |
| 455 | } AgObjContents; |
| 456 | |
| 457 | int rx_graph_reference_on(const AgGraph *g, const AgSkillTable *skills, const AgObjContents *objs, |
| 458 | uint32_t n_objs, uint64_t run, AgReference *out); |
| 459 | |
| 460 | /* ---- MetaSkill support ---- */ |
| 461 | |
| 462 | /* Deterministic topological order of the live nodes (index order breaks |
| 463 | * ties). Count, or AG_E_CYCLE. */ |
| 464 | int rx_graph_topo(const AgGraph *g, uint16_t order[AG_MAX_NODES]); |
| 465 | |
| 466 | /* One link of a fused node's step evidence: the step's identity inside its |
| 467 | * fragment, its status and value, and the input values it saw. */ |
| 468 | uint64_t rx_graph_step_record(uint64_t acc, const uint8_t local_id[32], uint32_t status, |
| 469 | uint64_t value, const uint64_t *in, uint32_t n_in); |
| 470 | |
| 471 | /* Run a realization on inputs (status and value by port) and the fields of |
| 472 | * each object slot. Fills status, value, attempts, the step evidence and the |
| 473 | * busy time the steps that ran would have taken. Returns 0, or -1 when the |
| 474 | * realization cannot run here (HARDWARE, a LEARNED miss). */ |
| 475 | typedef struct { |
| 476 | uint8_t status; |
| 477 | uint64_t value; |
| 478 | uint32_t attempts; |
| 479 | uint64_t steps; |
| 480 | uint64_t busy_us; |
| 481 | int table_hit; |
| 482 | } AgMetaOut; |
| 483 | |
| 484 | int rx_graph_meta_run(const AgRealization *r, const AgSkillTable *skills, const uint8_t *s, |
| 485 | const uint64_t *v, uint32_t n_in, |
| 486 | const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS], AgMetaOut *out); |
| 487 | |
| 488 | /* Canonical bytes of a realization: its program as it will run and its |
| 489 | * learned table. Returns the length (the bytes written are at most cap). */ |
| 490 | #define AG_REALIZATION_MAX_BYTES 16384u |
| 491 | size_t rx_graph_realization_encode(const AgRealization *r, uint8_t *buf, size_t cap); |
| 492 | |
| 493 | /* Content identity: SHA-256 over the canonical bytes. */ |
| 494 | void rx_graph_realization_identity(AgRealization *r); |
| 495 | |
| 496 | /* Fill every step's local identity and the program signature from the |
| 497 | * steps themselves. */ |
| 498 | void rx_graph_meta_seal(AgMetaProgram *p); |
| 499 | |
| 500 | /* The learned-table key of the World fields a program reads. */ |
| 501 | uint64_t rx_graph_meta_world_key(const AgMetaProgram *p, |
| 502 | const uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]); |
| 503 | |
| 504 | /* Replace the live nodes in `members` (one of them `exit`, the only one whose |
| 505 | * result leaves the set) with one AG_META node. ext[i] is the node feeding |
| 506 | * port i; objs[j] the World object of slot j. Authority and resource |
| 507 | * requirements of the members move to the new node, as do the exit's |
| 508 | * conditions and the members' evidence obligations. The graph is compacted; |
| 509 | * the caller validates it. Returns the new node's index or AG_E_*. */ |
| 510 | int rx_graph_fuse(AgGraph *g, uint64_t members, uint32_t exit, uint32_t skill_id, |
| 511 | const AgRealization *r, const uint16_t *ext, uint32_t n_ext, |
| 512 | const RxObjRef *objs, uint32_t n_objs); |
| 513 | |
| 514 | /* Order chain step used by effect objects (and the reference). */ |
| 515 | uint64_t rx_graph_chain(uint64_t chain, uint64_t run, uint64_t value); |
| 516 | |
| 517 | /* Per-node evidence word: identity, run, status, value, inputs. */ |
| 518 | uint64_t rx_graph_evidence_word(const AgNode *n, uint64_t run, uint32_t status, uint64_t value, |
| 519 | const uint64_t *in, uint32_t n_in); |
| 520 | |
| 521 | /* Static ancestors of `node` over data, guard and order edges (bit set). */ |
| 522 | uint64_t rx_graph_ancestors(const AgGraph *g, uint32_t node); |
| 523 | |
| 524 | const char *rx_graph_kind_name(AgKind k); |
| 525 | |
| 526 | #endif /* RX_GRAPH_H */ |