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 / tests / runtime / rx_action_graph.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * OMEGA_ACTION_GRAPH_IR -- the action graph compiled, optimized, lowered |
| 19 | * onto resident reactions and run by readiness alone. |
| 20 | * |
| 21 | * Every scenario has its own world and its own native Aienux authority. The |
| 22 | * test plays the outside: it holds the admin (to give the principal the |
| 23 | * authority it is born with, and to revoke), publishes World inputs and run |
| 24 | * tokens, and plays the human who approves an AEGIS escalation. The graph |
| 25 | * compiler is given the world and the principal's capability table only. |
| 26 | * |
| 27 | * For each run the lowered graph is compared with the sequential reference |
| 28 | * (same node semantics, one node at a time in topological order): status, |
| 29 | * value and evidence of every node, and the effect objects. Causal ancestry |
| 30 | * is read back from the world's crumbs and compared with the graph. |
| 31 | */ |
| 32 | #include "runtime/aienos_cap.h" |
| 33 | #include "runtime/rx_aegis.h" |
| 34 | #include "runtime/rx_graph.h" |
| 35 | #include "runtime/rx_world.h" |
| 36 | #include "omega_evidence.h" |
| 37 | #include "omega_types.h" |
| 38 | #include "sha256.h" |
| 39 | |
| 40 | #include <sched.h> |
| 41 | #include <signal.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include <sys/utsname.h> |
| 46 | #include <time.h> |
| 47 | #include <unistd.h> |
| 48 | #include "rx_quiet_reps.h" |
| 49 | |
| 50 | enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 61, SUBJ_OTHER = 62 }; |
| 51 | |
| 52 | #define RES_CELL 0xA000001ull |
| 53 | #define RES_RUN 0xA000002ull |
| 54 | #define RES_A 0xA000010ull |
| 55 | #define RES_B 0xA000011ull |
| 56 | #define RES_C 0xA000012ull |
| 57 | #define RES_MEM 0xA000013ull |
| 58 | #define RES_OUT 0xA000014ull |
| 59 | #define RES_EFFECT 0xA000020ull |
| 60 | #define RES_SECRET 0xA000030ull /* the principal holds nothing here */ |
| 61 | |
| 62 | enum { GOAL_LINEAR = 1, GOAL_PARALLEL, GOAL_DIAMOND, GOAL_BRANCH, GOAL_FAILURE, GOAL_EFFECTS, |
| 63 | GOAL_REDUNDANT, GOAL_RESOURCE }; |
| 64 | |
| 65 | static int g_checks; |
| 66 | static int g_fail; |
| 67 | |
| 68 | #define CHECK(cond, ...) do { \ |
| 69 | g_checks++; \ |
| 70 | if (!(cond)) { \ |
| 71 | g_fail++; \ |
| 72 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 73 | fprintf(stderr, __VA_ARGS__); \ |
| 74 | fputc('\n', stderr); \ |
| 75 | } \ |
| 76 | } while (0) |
| 77 | |
| 78 | static uint64_t now_ns(void) { |
| 79 | struct timespec ts; |
| 80 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 81 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 82 | } |
| 83 | |
| 84 | /* ---- receipt figures ---- */ |
| 85 | static struct { |
| 86 | uint32_t runs_compared, nodes_compared, evidence_checked, evidence_missing; |
| 87 | uint32_t ancestry_nodes, ancestry_mismatch; |
| 88 | uint32_t det_runs, det_mismatch; |
| 89 | uint32_t authority_blocked_crumbs, bypass; |
| 90 | uint32_t aegis_mints_during_compile, aegis_mints_after_approval; |
| 91 | uint64_t par_1w_ns, par_nw_ns, par_workers; |
| 92 | uint64_t par_critical_us, par_total_us; |
| 93 | uint32_t opt_before, opt_after, opt_react_before, opt_react_after; |
| 94 | uint32_t opt_fold, opt_cse, opt_reads, opt_fused, opt_dead, opt_deps; |
| 95 | uint64_t opt_ns_before, opt_ns_after, opt_crumbs_before, opt_crumbs_after; |
| 96 | char cpus[64]; |
| 97 | } R; |
| 98 | |
| 99 | /* ---- skills (Skill Net stand-ins: deterministic procedures) ---- */ |
| 100 | |
| 101 | static uint64_t sk_combine(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { |
| 102 | (void)attempt; |
| 103 | *failed = 0; |
| 104 | uint64_t s = 7; |
| 105 | for (uint32_t i = 0; i < n; i++) s += in[i] * (3 * (i + 1)); |
| 106 | return s; |
| 107 | } |
| 108 | |
| 109 | static uint64_t sk_fetch(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { |
| 110 | (void)attempt; |
| 111 | *failed = 0; |
| 112 | return (n ? in[0] : 0) + 100; |
| 113 | } |
| 114 | |
| 115 | static AgSkillTable g_skills; |
| 116 | |
| 117 | static void skills_init(void) { |
| 118 | memset(&g_skills, 0, sizeof g_skills); |
| 119 | g_skills.n = 2; |
| 120 | g_skills.skill[0] = (AgSkill){ 1, sk_combine, { 0x01 } }; |
| 121 | g_skills.skill[1] = (AgSkill){ 2, sk_fetch, { 0x02 } }; |
| 122 | } |
| 123 | |
| 124 | /* ---- environment ---- */ |
| 125 | |
| 126 | typedef struct { |
| 127 | AienosCapAdmin *admin; |
| 128 | AienosCapView *view; |
| 129 | RxWorld w; |
| 130 | int have_aegis; |
| 131 | RxAegisFaculty a; |
| 132 | RxObjRef A, B, C, mem, out, effect, secret; |
| 133 | RxCapRef ext_run, ext_A, ext_B, ext_approval; |
| 134 | RxCapRef cell_cap, run_cap, effect_cap; |
| 135 | AgCapTable caps; |
| 136 | AgLibrary lib; |
| 137 | uint32_t n_lowered; |
| 138 | const AgLowered *lowered[8]; |
| 139 | } Env; |
| 140 | |
| 141 | static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 142 | AienosCapRef office; |
| 143 | aienos_cap_office(e->admin, &office); |
| 144 | AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; |
| 145 | AienosCapRef r = { UINT32_MAX, 0 }; |
| 146 | if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; |
| 147 | return (RxCapRef){ r.cap_id, r.generation }; |
| 148 | } |
| 149 | |
| 150 | static void hold(Env *e, RxCapRef ref, uint64_t res, uint32_t rights) { |
| 151 | e->caps.cap[e->caps.n].ref = ref; |
| 152 | e->caps.cap[e->caps.n].resource = res; |
| 153 | e->caps.cap[e->caps.n].rights = rights; |
| 154 | e->caps.n++; |
| 155 | } |
| 156 | |
| 157 | static uint64_t fld(Env *e, RxObjRef r, uint32_t i) { |
| 158 | RxObject o; |
| 159 | if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; |
| 160 | return o.field[i]; |
| 161 | } |
| 162 | |
| 163 | static int settle(Env *e) { return rx_world_wait_quiescent(&e->w, 30000); } |
| 164 | |
| 165 | /* grant_effect: whether the principal is born holding R|W|E on the effect. */ |
| 166 | static int env_start(Env *e, uint32_t workers, int grant_effect, int with_aegis) { |
| 167 | memset(e, 0, sizeof *e); |
| 168 | if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; |
| 169 | if (rx_world_init_native(&e->w, e->view, workers, 1u << 18) != RX_OK) return -1; |
| 170 | e->w.external_subject = SUBJ_EXTERNAL; |
| 171 | const uint32_t R = RX_RIGHT_READ, W = RX_RIGHT_WRITE, RW = R | W; |
| 172 | uint64_t z[RX_MAX_FIELDS] = { 0 }; |
| 173 | uint64_t a[RX_MAX_FIELDS] = { 40 }, b[RX_MAX_FIELDS] = { 7 }, c[RX_MAX_FIELDS] = { 3 }; |
| 174 | uint64_t m[RX_MAX_FIELDS] = { 11, 500, 12, 600, 13, 700, 0, 0 }; |
| 175 | if (rx_world_create(&e->w, 0xA01, RX_PERSIST_RESIDENT, RES_A, a, &e->A) != RX_OK || |
| 176 | rx_world_create(&e->w, 0xA02, RX_PERSIST_RESIDENT, RES_B, b, &e->B) != RX_OK || |
| 177 | rx_world_create(&e->w, 0xA03, RX_PERSIST_RESIDENT, RES_C, c, &e->C) != RX_OK || |
| 178 | rx_world_create(&e->w, AG_OT_MEMORY, RX_PERSIST_RESIDENT, RES_MEM, m, &e->mem) != RX_OK || |
| 179 | rx_world_create(&e->w, 0xA05, RX_PERSIST_RESIDENT, RES_OUT, z, &e->out) != RX_OK || |
| 180 | rx_world_create(&e->w, AG_OT_EFFECT, RX_PERSIST_RESIDENT, RES_EFFECT, z, &e->effect) != RX_OK || |
| 181 | rx_world_create(&e->w, 0xA07, RX_PERSIST_RESIDENT, RES_SECRET, z, &e->secret) != RX_OK) |
| 182 | return -1; |
| 183 | e->ext_run = mint(e, SUBJ_EXTERNAL, RES_RUN, W); |
| 184 | e->ext_A = mint(e, SUBJ_EXTERNAL, RES_A, W); |
| 185 | e->ext_B = mint(e, SUBJ_EXTERNAL, RES_B, W); |
| 186 | e->caps.subject = SUBJ_PLAN; |
| 187 | e->cell_cap = mint(e, SUBJ_PLAN, RES_CELL, RW); |
| 188 | e->run_cap = mint(e, SUBJ_PLAN, RES_RUN, R); |
| 189 | hold(e, mint(e, SUBJ_PLAN, RES_A, R), RES_A, R); |
| 190 | hold(e, mint(e, SUBJ_PLAN, RES_B, R), RES_B, R); |
| 191 | hold(e, mint(e, SUBJ_PLAN, RES_C, R), RES_C, R); |
| 192 | hold(e, mint(e, SUBJ_PLAN, RES_MEM, R), RES_MEM, R); |
| 193 | hold(e, mint(e, SUBJ_PLAN, RES_OUT, RW), RES_OUT, RW); |
| 194 | if (grant_effect) { |
| 195 | e->effect_cap = mint(e, SUBJ_PLAN, RES_EFFECT, RW | RX_RIGHT_EFFECT); |
| 196 | hold(e, e->effect_cap, RES_EFFECT, RW | RX_RIGHT_EFFECT); |
| 197 | } |
| 198 | if (with_aegis) { |
| 199 | RxAegisPolicy pol; |
| 200 | memset(&pol, 0, sizeof pol); |
| 201 | pol.n_rules = 1; |
| 202 | pol.rules[0] = (RxAegisRule){ 1, SUBJ_PLAN, RES_EFFECT, RES_EFFECT, |
| 203 | RW | RX_RIGHT_EFFECT, 0, 1 }; /* a human must approve */ |
| 204 | RxAegisClient cl = { SUBJ_PLAN, 0xA000000, 0xA0000ff, RW | RX_RIGHT_EFFECT }; |
| 205 | if (rx_aegis_create(&e->a, &e->w, e->admin, &pol, &cl, 1) != RX_OK) return -1; |
| 206 | RxAegisCaps ac; |
| 207 | ac.aegis_request = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); |
| 208 | ac.aegis_approval = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_APPROVAL), R); |
| 209 | ac.aegis_decision = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RW); |
| 210 | ac.root_request = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); |
| 211 | ac.root_decision = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), R); |
| 212 | for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) |
| 213 | ac.root_slot[j] = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + j), RW); |
| 214 | if (rx_aegis_register(&e->a, &ac) != RX_OK) return -1; |
| 215 | e->ext_approval = mint(e, SUBJ_EXTERNAL, rx_aegis_res(0, RX_AEGIS_RES_APPROVAL), W); |
| 216 | e->have_aegis = 1; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static uint32_t bypasses(Env *e); |
| 222 | |
| 223 | static void env_stop(Env *e) { |
| 224 | rx_world_wait_quiescent(&e->w, 30000); |
| 225 | R.bypass += bypasses(e); |
| 226 | rx_world_destroy(&e->w); |
| 227 | if (e->have_aegis) rx_aegis_destroy(&e->a); |
| 228 | aienos_cap_stop(e->admin, e->view); |
| 229 | } |
| 230 | |
| 231 | static int lower(Env *e, AgLowered *L, const AgGraph *g, const AgAegisBinding *ab) { |
| 232 | if (e->n_lowered < 8) e->lowered[e->n_lowered++] = L; |
| 233 | return rx_graph_lower(L, &e->w, g, &g_skills, &e->caps, RES_CELL, e->cell_cap, RES_RUN, |
| 234 | e->run_cap, ab); |
| 235 | } |
| 236 | |
| 237 | /* ---- procedures (graph templates; World objects are parameters) ---- |
| 238 | * Parameters: 1 A, 2 B, 3 C, 4 memory, 5 out, 6 effect, 7 secret. */ |
| 239 | |
| 240 | static AgGraph T_linear, T_parallel, T_diamond, T_branch, T_failure, T_effects, T_redundant, |
| 241 | T_resource; |
| 242 | |
| 243 | static int N(AgGraph *g, AgKind k, AgType t) { return rx_graph_node(g, k, t); } |
| 244 | |
| 245 | static int node_read(AgGraph *g, uint32_t param, uint32_t field, uint32_t cost) { |
| 246 | int n = N(g, AG_WORLD_READ, AG_T_U64); |
| 247 | g->nodes[n].param = param; |
| 248 | g->nodes[n].field = field; |
| 249 | g->nodes[n].cost_us = cost; |
| 250 | return n; |
| 251 | } |
| 252 | |
| 253 | static int node_pure(AgGraph *g, uint32_t op, int a, int b, uint64_t imm) { |
| 254 | int n = N(g, AG_PURE, (op == OP_LESS_THAN || op == OP_EQUAL) ? AG_T_BOOL : AG_T_U64); |
| 255 | g->nodes[n].op = op; |
| 256 | g->nodes[n].imm = imm; |
| 257 | if (a >= 0) rx_graph_data(g, (uint32_t)a, (uint32_t)n, 0, AG_EDGE_DATA); |
| 258 | if (b >= 0) rx_graph_data(g, (uint32_t)b, (uint32_t)n, 1, AG_EDGE_DATA); |
| 259 | return n; |
| 260 | } |
| 261 | |
| 262 | static int node_verify(AgGraph *g, int in, uint64_t lo, uint64_t hi) { |
| 263 | int n = N(g, AG_VERIFY, AG_T_VERDICT); |
| 264 | g->nodes[n].imm = lo; |
| 265 | g->nodes[n].imm2 = hi; |
| 266 | rx_graph_data(g, (uint32_t)in, (uint32_t)n, 0, AG_EDGE_DATA); |
| 267 | return n; |
| 268 | } |
| 269 | |
| 270 | static int node_perform(AgGraph *g, int proposal, int verdict) { |
| 271 | int n = N(g, AG_EFFECT_PERFORM, AG_T_RECEIPT); |
| 272 | g->nodes[n].param = 6; |
| 273 | rx_graph_data(g, (uint32_t)proposal, (uint32_t)n, 0, AG_EDGE_DATA); |
| 274 | rx_graph_data(g, (uint32_t)verdict, (uint32_t)n, 1, AG_EDGE_DATA); |
| 275 | return n; |
| 276 | } |
| 277 | |
| 278 | static int node_propose(AgGraph *g, int in) { |
| 279 | int n = N(g, AG_EFFECT_PROPOSE, AG_T_PROPOSAL); |
| 280 | rx_graph_data(g, (uint32_t)in, (uint32_t)n, 0, AG_EDGE_DATA); |
| 281 | return n; |
| 282 | } |
| 283 | |
| 284 | static int node_publish(AgGraph *g, int in, uint32_t param, uint32_t field) { |
| 285 | int n = N(g, AG_WORLD_PUBLISH, AG_T_RECEIPT); |
| 286 | g->nodes[n].param = param; |
| 287 | g->nodes[n].field = field; |
| 288 | rx_graph_data(g, (uint32_t)in, (uint32_t)n, 0, AG_EDGE_DATA); |
| 289 | return n; |
| 290 | } |
| 291 | |
| 292 | static void build_templates(uint32_t cost) { |
| 293 | AgGraph *g; |
| 294 | int a, b, c, d, v, p, f; |
| 295 | |
| 296 | /* linear: A -> +5 -> *3 -> verify -> propose -> perform */ |
| 297 | g = &T_linear; |
| 298 | rx_graph_init(g, 0); |
| 299 | a = node_read(g, 1, 0, 0); |
| 300 | b = node_pure(g, OP_ADD, a, -1, 5); |
| 301 | c = node_pure(g, OP_MUL, b, -1, 3); |
| 302 | v = node_verify(g, c, 0, 1000); |
| 303 | p = node_propose(g, c); |
| 304 | f = node_perform(g, p, v); |
| 305 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 306 | rx_graph_failure(g, (uint32_t)v, AG_FAILED); |
| 307 | |
| 308 | /* parallel: recall + three World reads, each `cost` of real work, combine */ |
| 309 | g = &T_parallel; |
| 310 | rx_graph_init(g, 0); |
| 311 | int r0 = N(g, AG_RECALL, AG_T_U64); |
| 312 | g->nodes[r0].param = 4; |
| 313 | g->nodes[r0].imm = 11; |
| 314 | g->nodes[r0].cost_us = cost; |
| 315 | int r1 = node_read(g, 1, 0, cost), r2 = node_read(g, 2, 0, cost), r3 = node_read(g, 3, 0, cost); |
| 316 | int sk = N(g, AG_SKILL, AG_T_U64); |
| 317 | g->nodes[sk].op = 1; |
| 318 | rx_graph_data(g, (uint32_t)r0, (uint32_t)sk, 0, AG_EDGE_DATA); |
| 319 | rx_graph_data(g, (uint32_t)r1, (uint32_t)sk, 1, AG_EDGE_DATA); |
| 320 | rx_graph_data(g, (uint32_t)r2, (uint32_t)sk, 2, AG_EDGE_DATA); |
| 321 | rx_graph_data(g, (uint32_t)r3, (uint32_t)sk, 3, AG_EDGE_DATA); |
| 322 | v = node_verify(g, sk, 0, 1ull << 40); |
| 323 | p = node_propose(g, sk); |
| 324 | f = node_perform(g, p, v); |
| 325 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 326 | |
| 327 | /* diamond: A -> (+1, *2) -> add -> verify, publish after verify */ |
| 328 | g = &T_diamond; |
| 329 | rx_graph_init(g, 0); |
| 330 | a = node_read(g, 1, 0, 0); |
| 331 | b = node_pure(g, OP_ADD, a, -1, 1); |
| 332 | c = node_pure(g, OP_MUL, a, -1, 2); |
| 333 | d = node_pure(g, OP_ADD, b, c, 0); |
| 334 | v = node_verify(g, d, 0, 10000); |
| 335 | f = node_publish(g, d, 5, 0); |
| 336 | rx_graph_order(g, (uint32_t)v, (uint32_t)f); |
| 337 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 338 | |
| 339 | /* branch/join: A < 50 ? A*2 : A+100 */ |
| 340 | g = &T_branch; |
| 341 | rx_graph_init(g, 0); |
| 342 | a = node_read(g, 1, 0, 0); |
| 343 | b = node_pure(g, OP_LESS_THAN, a, -1, 50); |
| 344 | int br = N(g, AG_BRANCH, AG_T_BOOL); |
| 345 | rx_graph_data(g, (uint32_t)b, (uint32_t)br, 0, AG_EDGE_DATA); |
| 346 | int t = node_pure(g, OP_MUL, a, -1, 2), e = node_pure(g, OP_ADD, a, -1, 100); |
| 347 | rx_graph_guard(g, (uint32_t)br, (uint32_t)t, 1); |
| 348 | rx_graph_guard(g, (uint32_t)br, (uint32_t)e, 0); |
| 349 | int j = N(g, AG_JOIN, AG_T_U64); |
| 350 | rx_graph_data(g, (uint32_t)t, (uint32_t)j, 0, AG_EDGE_DATA); |
| 351 | rx_graph_data(g, (uint32_t)e, (uint32_t)j, 1, AG_EDGE_DATA); |
| 352 | v = node_verify(g, j, 0, 1000); |
| 353 | p = node_propose(g, j); |
| 354 | f = node_perform(g, p, v); |
| 355 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 356 | |
| 357 | /* failure branch: recall a key memory does not hold; the failure arm |
| 358 | * supplies a fallback; a bounded retry follows */ |
| 359 | g = &T_failure; |
| 360 | rx_graph_init(g, 0); |
| 361 | int rc = N(g, AG_RECALL, AG_T_U64); |
| 362 | g->nodes[rc].param = 4; |
| 363 | g->nodes[rc].imm = 99; |
| 364 | b = node_pure(g, OP_ADD, rc, -1, 1); |
| 365 | int fb = N(g, AG_PURE, AG_T_U64); |
| 366 | g->nodes[fb].op = OP_CONSTANT; |
| 367 | g->nodes[fb].imm = 777; |
| 368 | rx_graph_data(g, (uint32_t)rc, (uint32_t)fb, 0, AG_EDGE_ON_FAIL); |
| 369 | j = N(g, AG_JOIN, AG_T_U64); |
| 370 | rx_graph_data(g, (uint32_t)b, (uint32_t)j, 0, AG_EDGE_DATA); |
| 371 | rx_graph_data(g, (uint32_t)fb, (uint32_t)j, 1, AG_EDGE_DATA); |
| 372 | int rt = N(g, AG_RETRY, AG_T_U64); |
| 373 | g->nodes[rt].op = 2; |
| 374 | g->nodes[rt].imm = 3; /* at most 3 attempts */ |
| 375 | g->nodes[rt].imm2 = 2; /* the stand-in fails the first 2 */ |
| 376 | rx_graph_data(g, (uint32_t)j, (uint32_t)rt, 0, AG_EDGE_DATA); |
| 377 | v = node_verify(g, rt, 0, 1000); |
| 378 | f = node_publish(g, rt, 5, 1); |
| 379 | rx_graph_order(g, (uint32_t)v, (uint32_t)f); |
| 380 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 381 | |
| 382 | /* effect boundary: two identical performs, a publication, and reads of |
| 383 | * the published object before and after it */ |
| 384 | g = &T_effects; |
| 385 | rx_graph_init(g, 0); |
| 386 | a = node_read(g, 1, 0, 0); |
| 387 | p = node_propose(g, a); |
| 388 | v = node_verify(g, a, 0, 1000); |
| 389 | int f1 = node_perform(g, p, v), f2 = node_perform(g, p, v); |
| 390 | rx_graph_order(g, (uint32_t)f1, (uint32_t)f2); |
| 391 | int pub = node_publish(g, a, 5, 2); |
| 392 | rx_graph_order(g, (uint32_t)f2, (uint32_t)pub); |
| 393 | int after = node_read(g, 5, 2, 0), before = node_read(g, 5, 2, 0); |
| 394 | rx_graph_order(g, (uint32_t)pub, (uint32_t)after); |
| 395 | rx_graph_order(g, (uint32_t)before, (uint32_t)pub); |
| 396 | d = node_pure(g, OP_SUB, after, before, 0); |
| 397 | v = node_verify(g, d, 0, 1ull << 62); |
| 398 | rx_graph_success(g, (uint32_t)v, AG_OK); |
| 399 | rx_graph_success(g, (uint32_t)pub, AG_OK); |
| 400 | |
| 401 | /* redundant plan: what a planner emits before Omega cleans it up */ |
| 402 | g = &T_redundant; |
| 403 | rx_graph_init(g, 0); |
| 404 | int n0 = node_read(g, 1, 0, 0), n1 = node_read(g, 1, 0, 0); |
| 405 | int n2 = N(g, AG_CONST, AG_T_U64), n3 = N(g, AG_CONST, AG_T_U64); |
| 406 | g->nodes[n2].imm = 6; |
| 407 | g->nodes[n3].imm = 7; |
| 408 | int n4 = node_pure(g, OP_MUL, n2, n3, 0); |
| 409 | int n5 = node_pure(g, OP_ADD, n0, n4, 0), n6 = node_pure(g, OP_ADD, n1, n4, 0); |
| 410 | int n7 = node_pure(g, OP_MUL, n5, -1, 2), n8 = node_pure(g, OP_ADD, n7, -1, 1); |
| 411 | int n9 = node_pure(g, OP_ADD, n8, n6, 0), n10 = node_pure(g, OP_SUB, n9, -1, 3); |
| 412 | int n11 = node_verify(g, n10, 0, 100000); |
| 413 | int n12 = node_propose(g, n10); |
| 414 | int n13 = node_perform(g, n12, n11); |
| 415 | int n14 = node_read(g, 2, 0, 0); |
| 416 | node_pure(g, OP_ADD, n14, -1, 1); /* unused diagnostic */ |
| 417 | rx_graph_order(g, (uint32_t)n14, (uint32_t)n5); /* false order */ |
| 418 | rx_graph_order(g, (uint32_t)n5, (uint32_t)n9); /* implied order */ |
| 419 | rx_graph_success(g, (uint32_t)n13, AG_OK); |
| 420 | |
| 421 | /* resource: a physical node needs more memory than the body offers */ |
| 422 | g = &T_resource; |
| 423 | rx_graph_init(g, 0); |
| 424 | a = node_read(g, 1, 0, 0); |
| 425 | b = node_read(g, 2, 0, 0); |
| 426 | int ph = N(g, AG_PHYSICAL, AG_T_U64); |
| 427 | g->nodes[ph].imm = 5; |
| 428 | rx_graph_data(g, (uint32_t)b, (uint32_t)ph, 0, AG_EDGE_DATA); |
| 429 | RxResourceNeed need; |
| 430 | memset(&need, 0, sizeof need); |
| 431 | need.memory_bytes = 1ull << 40; |
| 432 | rx_graph_need_resource(g, (uint32_t)ph, &need); |
| 433 | c = node_pure(g, OP_ADD, a, ph, 0); |
| 434 | v = node_verify(g, c, 0, UINT64_MAX); |
| 435 | f = node_publish(g, c, 5, 3); |
| 436 | rx_graph_order(g, (uint32_t)v, (uint32_t)f); |
| 437 | int ind = node_pure(g, OP_ADD, a, -1, 1); |
| 438 | int v2 = node_verify(g, ind, 0, 1000); |
| 439 | rx_graph_success(g, (uint32_t)f, AG_OK); |
| 440 | rx_graph_success(g, (uint32_t)v2, AG_OK); |
| 441 | } |
| 442 | |
| 443 | static void library(Env *e) { |
| 444 | AgLibrary *l = &e->lib; |
| 445 | memset(l, 0, sizeof *l); |
| 446 | const AgGraph *t[] = { &T_linear, &T_parallel, &T_diamond, &T_branch, &T_failure, |
| 447 | &T_effects, &T_redundant, &T_resource }; |
| 448 | for (uint32_t i = 0; i < 8; i++) l->proc[l->n++] = (AgProcedure){ i + 1, t[i] }; |
| 449 | } |
| 450 | |
| 451 | static AgGoal goal_for(Env *e, uint32_t kind) { |
| 452 | AgGoal g = { kind, 7, { e->A, e->B, e->C, e->mem, e->out, e->effect, e->secret } }; |
| 453 | return g; |
| 454 | } |
| 455 | |
| 456 | static AgConstraints open_constraints(void) { |
| 457 | AgConstraints c; |
| 458 | memset(&c, 0, sizeof c); |
| 459 | c.max_effects = UINT32_MAX; |
| 460 | c.budget.slots = RX_MAX_REACTIONS; |
| 461 | c.budget.memory_bytes = UINT64_MAX; |
| 462 | c.budget.energy_budget = UINT64_MAX; |
| 463 | c.budget.offered_locality = UINT32_MAX; |
| 464 | c.budget.offered_accel = UINT32_MAX; |
| 465 | c.budget.compute_mask = UINT32_MAX; |
| 466 | return c; |
| 467 | } |
| 468 | |
| 469 | static int compile(Env *e, uint32_t kind, int optimize, AgGraph *g, AgReport *rep) { |
| 470 | AgGoal goal = goal_for(e, kind); |
| 471 | AgConstraints c = open_constraints(); |
| 472 | return rx_graph_compile(&goal, &e->w, &e->caps, &c, &e->lib, optimize, g, rep); |
| 473 | } |
| 474 | |
| 475 | /* ---- running, comparing, ancestry ---- */ |
| 476 | |
| 477 | static int64_t run(Env *e, AgLowered *L, uint64_t id, AgResult *res, uint64_t *ns) { |
| 478 | uint64_t t0 = now_ns(); |
| 479 | int64_t ext = rx_graph_start(L, e->ext_run, id); |
| 480 | CHECK(ext > 0, "run token refused"); |
| 481 | CHECK(settle(e) == RX_OK, "settle"); |
| 482 | if (ns) *ns = now_ns() - t0; |
| 483 | rx_graph_collect(L, id, res); |
| 484 | return ext; |
| 485 | } |
| 486 | |
| 487 | /* Every node's status/value/evidence, the outcome, and the effect object. */ |
| 488 | static void compare(Env *e, const AgGraph *g, const AgResult *got, const AgReference *ref, |
| 489 | const char *what) { |
| 490 | R.runs_compared++; |
| 491 | int bad = 0; |
| 492 | for (uint32_t n = 0; n < g->n_nodes; n++) { |
| 493 | R.nodes_compared++; |
| 494 | if (got->status[n] != ref->r.status[n] || got->value[n] != ref->r.value[n] || |
| 495 | (got->status[n] && got->evidence[n] != ref->r.evidence[n])) { |
| 496 | bad = 1; |
| 497 | fprintf(stderr, " %s node %u (%s): got s%u v%llu, reference s%u v%llu\n", what, n, |
| 498 | rx_graph_kind_name(g->nodes[n].kind), got->status[n], |
| 499 | (unsigned long long)got->value[n], ref->r.status[n], |
| 500 | (unsigned long long)ref->r.value[n]); |
| 501 | } |
| 502 | } |
| 503 | CHECK(!bad, "%s: lowered run differs from the sequential reference", what); |
| 504 | CHECK(got->outcome == ref->r.outcome, "%s: outcome %d vs reference %d", what, got->outcome, |
| 505 | ref->r.outcome); |
| 506 | uint32_t last_perform = UINT32_MAX; |
| 507 | for (uint32_t i = 0; i < g->n_effects; i++) |
| 508 | if (g->nodes[g->effects[i]].kind == AG_EFFECT_PERFORM) last_perform = i; |
| 509 | if (last_perform != UINT32_MAX) |
| 510 | CHECK(fld(e, e->effect, 2) == ref->effect_chain[last_perform], |
| 511 | "%s: effect order chain differs from the reference", what); |
| 512 | /* Evidence: every evidence node of a completed run has its word. */ |
| 513 | if (got->outcome == AG_RUN_SUCCESS) |
| 514 | for (uint32_t i = 0; i < g->n_evidence; i++) { |
| 515 | uint32_t n = g->evidence[i]; |
| 516 | R.evidence_checked++; |
| 517 | if (got->status[n] == AG_PENDING || got->evidence[n] == 0) R.evidence_missing++; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static AgReference g_ref; |
| 522 | |
| 523 | static void reference(Env *e, const AgGraph *g, uint64_t id) { |
| 524 | CHECK(rx_graph_reference(g, &e->w, &g_skills, &e->caps, id, &g_ref) == 0, "reference"); |
| 525 | } |
| 526 | |
| 527 | /* Crumbs of this run (episode = the run token's EXTERNAL crumb): for each |
| 528 | * node, walk its commit's parents inside the episode and compare the nodes |
| 529 | * reached with the graph's own ancestors. */ |
| 530 | static void ancestry(Env *e, const AgLowered *L, int64_t ext, const char *what) { |
| 531 | const AgGraph *g = L->g; |
| 532 | RxWorld *w = &e->w; |
| 533 | int32_t node_of[RX_MAX_REACTIONS]; |
| 534 | for (uint32_t i = 0; i < RX_MAX_REACTIONS; i++) node_of[i] = -1; |
| 535 | for (uint32_t n = 0; n < g->n_nodes; n++) node_of[L->reaction[n]] = (int32_t)n; |
| 536 | uint64_t *stack = calloc(w->n_crumbs + 1, sizeof(uint64_t)); |
| 537 | uint8_t *seen = calloc(w->n_crumbs + 1, 1); |
| 538 | int bad = 0; |
| 539 | for (uint32_t n = 0; n < g->n_nodes; n++) { |
| 540 | uint64_t mine = 0; |
| 541 | for (uint64_t id = 1; id <= w->n_crumbs; id++) { |
| 542 | const RxCrumb *k = rx_world_crumb(w, id); |
| 543 | if (k->episode == (uint64_t)ext && k->kind == RX_CRUMB_COMMIT && k->reaction == L->reaction[n]) |
| 544 | mine = id; |
| 545 | } |
| 546 | if (!mine) { bad = 1; continue; } |
| 547 | memset(seen, 0, w->n_crumbs + 1); |
| 548 | uint64_t sp = 0, got = 0; |
| 549 | int reached_run = 0; |
| 550 | stack[sp++] = mine; |
| 551 | seen[mine] = 1; |
| 552 | while (sp) { |
| 553 | const RxCrumb *k = rx_world_crumb(w, stack[--sp]); |
| 554 | for (uint32_t i = 0; i < k->n_parents; i++) { |
| 555 | uint64_t p = k->parents[i]; |
| 556 | if (!p || seen[p]) continue; |
| 557 | const RxCrumb *q = rx_world_crumb(w, p); |
| 558 | if (!q || q->episode != (uint64_t)ext) continue; |
| 559 | seen[p] = 1; |
| 560 | if (p == (uint64_t)ext) reached_run = 1; |
| 561 | if (q->reaction < RX_MAX_REACTIONS && node_of[q->reaction] >= 0) |
| 562 | got |= 1ull << node_of[q->reaction]; |
| 563 | stack[sp++] = p; |
| 564 | } |
| 565 | } |
| 566 | R.ancestry_nodes++; |
| 567 | if (got != rx_graph_ancestors(g, n) || !reached_run) { |
| 568 | bad = 1; |
| 569 | R.ancestry_mismatch++; |
| 570 | fprintf(stderr, " %s node %u: crumb ancestry %llx, graph %llx, run %d\n", what, n, |
| 571 | (unsigned long long)got, (unsigned long long)rx_graph_ancestors(g, n), |
| 572 | reached_run); |
| 573 | } |
| 574 | } |
| 575 | free(stack); |
| 576 | free(seen); |
| 577 | CHECK(!bad, "%s: causal ancestry differs from the graph", what); |
| 578 | } |
| 579 | |
| 580 | /* Semantic result of a run: every node's status and value, and the effect |
| 581 | * object. Not timing, not crumb ids. */ |
| 582 | static void semantic_digest(Env *e, const AgGraph *g, const AgResult *r, uint8_t out[32]) { |
| 583 | sha256_ctx c; |
| 584 | sha256_init(&c); |
| 585 | for (uint32_t n = 0; n < g->n_nodes; n++) { |
| 586 | sha256_update(&c, &r->status[n], 1); |
| 587 | sha256_update(&c, (const uint8_t *)&r->value[n], 8); |
| 588 | sha256_update(&c, (const uint8_t *)&r->evidence[n], 8); |
| 589 | } |
| 590 | for (uint32_t f = 0; f < 4; f++) { |
| 591 | uint64_t v = fld(e, e->effect, f); |
| 592 | sha256_update(&c, (const uint8_t *)&v, 8); |
| 593 | } |
| 594 | sha256_final(&c, out); |
| 595 | } |
| 596 | |
| 597 | /* Commits to boundary targets must come from lowered boundary reactions |
| 598 | * holding valid authority. Counts anything else. */ |
| 599 | static uint32_t bypasses(Env *e) { |
| 600 | uint32_t bad = 0; |
| 601 | for (uint64_t id = 1; id <= e->w.n_crumbs; id++) { |
| 602 | const RxCrumb *k = rx_world_crumb(&e->w, id); |
| 603 | if (k->kind != RX_CRUMB_COMMIT) continue; |
| 604 | for (uint32_t o = 0; o < k->n_outputs; o++) { |
| 605 | uint32_t oid = k->outputs[o].obj.id; |
| 606 | if (oid != e->effect.id && oid != e->out.id && oid != e->secret.id) continue; |
| 607 | int legit = 0; |
| 608 | for (uint32_t l = 0; l < e->n_lowered; l++) { |
| 609 | const AgLowered *L = e->lowered[l]; |
| 610 | for (uint32_t n = 0; n < L->g->n_nodes; n++) |
| 611 | if (L->reaction[n] == k->reaction && |
| 612 | (L->g->nodes[n].kind == AG_EFFECT_PERFORM || |
| 613 | L->g->nodes[n].kind == AG_WORLD_PUBLISH) && |
| 614 | L->g->nodes[n].obj.id == oid) |
| 615 | legit = 1; |
| 616 | } |
| 617 | if (!legit) bad++; |
| 618 | } |
| 619 | } |
| 620 | return bad; |
| 621 | } |
| 622 | |
| 623 | static uint32_t count_crumbs(Env *e, uint32_t reaction, RxCrumbKind kind) { |
| 624 | uint32_t n = 0; |
| 625 | for (uint64_t id = 1; id <= e->w.n_crumbs; id++) { |
| 626 | const RxCrumb *k = rx_world_crumb(&e->w, id); |
| 627 | if (k->reaction == reaction && k->kind == kind) n++; |
| 628 | } |
| 629 | return n; |
| 630 | } |
| 631 | |
| 632 | static int node_of_kind(const AgGraph *g, AgKind k) { |
| 633 | for (uint32_t n = 0; n < g->n_nodes; n++) |
| 634 | if (g->nodes[n].kind == k) return (int)n; |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | /* Compile, lower, run with reference and ancestry. */ |
| 639 | static void full_run(Env *e, AgLowered *L, uint64_t id, const char *what, AgResult *res) { |
| 640 | reference(e, L->g, id); |
| 641 | int64_t ext = run(e, L, id, res, NULL); |
| 642 | compare(e, L->g, res, &g_ref, what); |
| 643 | ancestry(e, L, ext, what); |
| 644 | } |
| 645 | |
| 646 | /* ---- scenarios ---- */ |
| 647 | |
| 648 | static void t_linear(void) { |
| 649 | printf("[*] linear graph\n"); |
| 650 | Env e; |
| 651 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 652 | library(&e); |
| 653 | AgGraph g; |
| 654 | AgReport rep; |
| 655 | CHECK(compile(&e, GOAL_LINEAR, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 656 | CHECK(rep.n_missing == 0 && rep.nodes_before == 6 && rep.nodes_after == 5 && rep.passes.fused == 1, |
| 657 | "linear report missing %u nodes %u->%u fused %u", rep.n_missing, rep.nodes_before, |
| 658 | rep.nodes_after, rep.passes.fused); |
| 659 | AgLowered L; |
| 660 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 661 | AgResult res; |
| 662 | for (uint64_t r = 1; r <= 3; r++) full_run(&e, &L, r, "linear", &res); |
| 663 | CHECK(res.outcome == AG_RUN_SUCCESS && res.value[node_of_kind(&g, AG_PURE)] == 135, |
| 664 | "linear: (40+5)*3 = 135"); |
| 665 | CHECK(fld(&e, e.effect, 0) == 3 && fld(&e, e.effect, 1) == 135, "three performed effects"); |
| 666 | env_stop(&e); |
| 667 | } |
| 668 | |
| 669 | static void t_diamond(void) { |
| 670 | printf("[*] diamond graph\n"); |
| 671 | Env e; |
| 672 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 673 | library(&e); |
| 674 | AgGraph g; |
| 675 | AgReport rep; |
| 676 | CHECK(compile(&e, GOAL_DIAMOND, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 677 | AgLowered L; |
| 678 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 679 | AgResult res; |
| 680 | full_run(&e, &L, 1, "diamond", &res); |
| 681 | CHECK(res.outcome == AG_RUN_SUCCESS && fld(&e, e.out, 0) == 41 + 80, "diamond publishes 121"); |
| 682 | RxMutation m = { e.A, 0, 5 }; |
| 683 | CHECK(rx_world_publish_external(&e.w, e.ext_A, &m, 1) > 0, "A"); |
| 684 | settle(&e); |
| 685 | full_run(&e, &L, 2, "diamond", &res); |
| 686 | CHECK(fld(&e, e.out, 0) == 6 + 10, "diamond follows A: 16"); |
| 687 | env_stop(&e); |
| 688 | } |
| 689 | |
| 690 | static void t_branch(void) { |
| 691 | printf("[*] branch/join graph: both arms\n"); |
| 692 | Env e; |
| 693 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 694 | library(&e); |
| 695 | AgGraph g; |
| 696 | AgReport rep; |
| 697 | CHECK(compile(&e, GOAL_BRANCH, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 698 | AgLowered L; |
| 699 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 700 | AgResult res; |
| 701 | full_run(&e, &L, 1, "branch A=40", &res); |
| 702 | CHECK(res.value[node_of_kind(&g, AG_JOIN)] == 80, "A=40 takes the *2 arm"); |
| 703 | RxMutation m = { e.A, 0, 70 }; |
| 704 | CHECK(rx_world_publish_external(&e.w, e.ext_A, &m, 1) > 0, "A"); |
| 705 | settle(&e); |
| 706 | full_run(&e, &L, 2, "branch A=70", &res); |
| 707 | CHECK(res.value[node_of_kind(&g, AG_JOIN)] == 170, "A=70 takes the +100 arm"); |
| 708 | uint32_t skipped = 0; |
| 709 | for (uint32_t n = 0; n < g.n_nodes; n++) skipped += res.status[n] == AG_SKIPPED; |
| 710 | CHECK(skipped == 1, "exactly the untaken arm is skipped (%u)", skipped); |
| 711 | env_stop(&e); |
| 712 | } |
| 713 | |
| 714 | static void t_failure(void) { |
| 715 | printf("[*] failure branch and bounded retry\n"); |
| 716 | Env e; |
| 717 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 718 | library(&e); |
| 719 | AgGraph g; |
| 720 | AgReport rep; |
| 721 | CHECK(compile(&e, GOAL_FAILURE, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 722 | AgLowered L; |
| 723 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 724 | AgResult res; |
| 725 | full_run(&e, &L, 1, "failure", &res); |
| 726 | int rc = node_of_kind(&g, AG_RECALL), rt = node_of_kind(&g, AG_RETRY); |
| 727 | CHECK(res.status[rc] == AG_FAILED, "recall of an unknown key fails"); |
| 728 | CHECK(res.outcome == AG_RUN_SUCCESS && fld(&e, e.out, 1) == 877, "fallback 777 + fetch 100"); |
| 729 | CHECK(res.attempts[rt] == 3, "retry took 3 attempts (%u)", res.attempts[rt]); |
| 730 | |
| 731 | /* Exhausted retry: a declared failure condition makes the run FAILURE. */ |
| 732 | AgGraph x; |
| 733 | rx_graph_init(&x, SUBJ_PLAN); |
| 734 | int c = N(&x, AG_CONST, AG_T_U64); |
| 735 | x.nodes[c].imm = 5; |
| 736 | int r = N(&x, AG_RETRY, AG_T_U64); |
| 737 | x.nodes[r].op = 2; |
| 738 | x.nodes[r].imm = 2; |
| 739 | x.nodes[r].imm2 = 5; |
| 740 | rx_graph_data(&x, (uint32_t)c, (uint32_t)r, 0, AG_EDGE_DATA); |
| 741 | int v = node_verify(&x, r, 0, 1000); |
| 742 | rx_graph_success(&x, (uint32_t)v, AG_OK); |
| 743 | rx_graph_failure(&x, (uint32_t)r, AG_FAILED); |
| 744 | CHECK(rx_graph_validate(&x, &e.w) == 0, "retry graph valid"); |
| 745 | AgLowered L2; |
| 746 | CHECK(lower(&e, &L2, &x, NULL) == 0, "lower"); |
| 747 | full_run(&e, &L2, 1, "retry exhausted", &res); |
| 748 | CHECK(res.outcome == AG_RUN_FAILURE && res.attempts[r] == 2 && res.status[v] == AG_FAILED, |
| 749 | "exhausted retry fails the run after 2 attempts"); |
| 750 | env_stop(&e); |
| 751 | } |
| 752 | |
| 753 | static void t_effects(void) { |
| 754 | printf("[*] effect boundary\n"); |
| 755 | Env e; |
| 756 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 757 | library(&e); |
| 758 | AgGraph g; |
| 759 | AgReport rep; |
| 760 | CHECK(compile(&e, GOAL_EFFECTS, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 761 | uint32_t performs = 0, reads_out = 0; |
| 762 | for (uint32_t n = 0; n < g.n_nodes; n++) { |
| 763 | performs += g.nodes[n].kind == AG_EFFECT_PERFORM; |
| 764 | reads_out += g.nodes[n].kind == AG_WORLD_READ && g.nodes[n].obj.id == e.out.id; |
| 765 | } |
| 766 | CHECK(performs == 2, "identical performs are never merged (%u)", performs); |
| 767 | CHECK(reads_out == 2, "reads across a publication of their object are not merged (%u)", reads_out); |
| 768 | CHECK(g.n_effects == 3, "three boundaries in the chain"); |
| 769 | |
| 770 | AgLowered L; |
| 771 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 772 | AgResult res; |
| 773 | full_run(&e, &L, 1, "effects", &res); |
| 774 | CHECK(res.outcome == AG_RUN_SUCCESS && fld(&e, e.effect, 0) == 2 && fld(&e, e.out, 2) == 40, |
| 775 | "both effects performed in order, then the publication"); |
| 776 | CHECK(res.value[node_of_kind(&g, AG_PURE)] == 40, "read after publish sees it, read before does not"); |
| 777 | |
| 778 | /* Thinking about an effect performs nothing. */ |
| 779 | AgGraph t; |
| 780 | rx_graph_init(&t, SUBJ_PLAN); |
| 781 | int a = N(&t, AG_WORLD_READ, AG_T_U64); |
| 782 | t.nodes[a].obj = e.A; |
| 783 | int p = node_propose(&t, a); |
| 784 | int v = node_verify(&t, a, 0, 1000); |
| 785 | rx_graph_success(&t, (uint32_t)p, AG_OK); |
| 786 | rx_graph_success(&t, (uint32_t)v, AG_OK); |
| 787 | CHECK(rx_graph_validate(&t, &e.w) == 0 && t.n_effects == 0, "proposal graph has no boundary"); |
| 788 | AgLowered L2; |
| 789 | CHECK(lower(&e, &L2, &t, NULL) == 0, "lower"); |
| 790 | uint64_t before[4]; |
| 791 | for (uint32_t f = 0; f < 4; f++) before[f] = fld(&e, e.effect, f); |
| 792 | full_run(&e, &L2, 1, "propose only", &res); |
| 793 | int same = 1; |
| 794 | for (uint32_t f = 0; f < 4; f++) same &= before[f] == fld(&e, e.effect, f); |
| 795 | CHECK(res.outcome == AG_RUN_SUCCESS && res.value[p] == 40 && same, |
| 796 | "a proposal is a value; the effect object is untouched"); |
| 797 | |
| 798 | /* Constraints: the goal may forbid effects or resources outright. */ |
| 799 | AgGoal goal = goal_for(&e, GOAL_EFFECTS); |
| 800 | AgConstraints c = open_constraints(); |
| 801 | c.max_effects = 1; |
| 802 | CHECK(rx_graph_compile(&goal, &e.w, &e.caps, &c, &e.lib, 1, &t, &rep) == AG_E_CONSTRAINT, |
| 803 | "two performs exceed max_effects 1"); |
| 804 | c = open_constraints(); |
| 805 | c.forbid_lo = RES_EFFECT; |
| 806 | c.forbid_hi = RES_EFFECT; |
| 807 | CHECK(rx_graph_compile(&goal, &e.w, &e.caps, &c, &e.lib, 1, &t, &rep) == AG_E_CONSTRAINT, |
| 808 | "a forbidden resource refuses the graph"); |
| 809 | env_stop(&e); |
| 810 | } |
| 811 | |
| 812 | static void t_parallel(void) { |
| 813 | printf("[*] parallel graph: critical path\n"); |
| 814 | /* 15 undisturbed repetitions per leg (rx_quiet_reps.h), at most 60 times |
| 815 | * as many attempts. */ |
| 816 | const uint32_t reps = 15, attempts = 15 * 60; |
| 817 | uint64_t times[2][32]; |
| 818 | uint32_t workers[2] = { 1, 4 }, kept[2] = { 0, 0 }, tried[2] = { 0, 0 }; |
| 819 | int delay_known = 1; |
| 820 | AgReport rep; |
| 821 | for (uint32_t k = 0; k < 2; k++) { |
| 822 | Env e; |
| 823 | CHECK(env_start(&e, workers[k], 1, 0) == 0, "env"); |
| 824 | library(&e); |
| 825 | AgGraph g; |
| 826 | CHECK(compile(&e, GOAL_PARALLEL, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 827 | AgLowered L; |
| 828 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 829 | AgResult res; |
| 830 | full_run(&e, &L, 1, "parallel warm", &res); |
| 831 | CHECK(res.outcome == AG_RUN_SUCCESS && |
| 832 | res.value[node_of_kind(&g, AG_SKILL)] == 7 + 500 * 3 + 40 * 6 + 7 * 9 + 3 * 12, |
| 833 | "combine = 7 + 3*500 + 6*40 + 9*7 + 12*3"); |
| 834 | for (uint32_t i = 0; i < attempts && kept[k] < reps; i++) { |
| 835 | reference(&e, &g, 2 + i); |
| 836 | uint64_t ns = 0; |
| 837 | int64_t d0 = rx_run_delay_ns(); |
| 838 | int64_t ext = run(&e, &L, 2 + i, &res, &ns); |
| 839 | int64_t d1 = rx_run_delay_ns(); |
| 840 | compare(&e, &g, &res, &g_ref, "parallel"); |
| 841 | if (i == 0) ancestry(&e, &L, ext, "parallel"); |
| 842 | tried[k]++; |
| 843 | if (d0 < 0 || d1 < 0) delay_known = 0; |
| 844 | if (rx_quiet_rep(d0 < 0 || d1 < 0 ? -1 : d1 - d0, ns)) times[k][kept[k]++] = ns; |
| 845 | } |
| 846 | env_stop(&e); |
| 847 | } |
| 848 | printf(" undisturbed repetitions: 1 worker %u of %u, %u workers %u of %u (%s)\n", kept[0], |
| 849 | tried[0], workers[1], kept[1], tried[1], |
| 850 | delay_known ? "run-queue delay from schedstat" : "schedstat missing: every repetition counts"); |
| 851 | if (kept[0] < reps || kept[1] < reps) { |
| 852 | fprintf(stderr, " machine too busy to time the parallel graph: %u and %u of %u undisturbed " |
| 853 | "repetitions in %u attempts\n", kept[0], kept[1], reps, attempts); |
| 854 | rx_busy_refuse("parallel graph timing (speedup, beats the sequential sum)"); |
| 855 | return; |
| 856 | } |
| 857 | for (uint32_t k = 0; k < 2; k++) |
| 858 | for (uint32_t i = 1; i < reps; i++) |
| 859 | for (uint32_t j = i; j > 0 && times[k][j - 1] > times[k][j]; j--) { |
| 860 | uint64_t t = times[k][j]; times[k][j] = times[k][j - 1]; times[k][j - 1] = t; |
| 861 | } |
| 862 | R.par_1w_ns = times[0][reps / 2]; |
| 863 | R.par_nw_ns = times[1][reps / 2]; |
| 864 | R.par_workers = workers[1]; |
| 865 | R.par_critical_us = rep.critical_path_us; |
| 866 | R.par_total_us = rep.total_work_us; |
| 867 | double s = (double)R.par_1w_ns / (double)R.par_nw_ns; |
| 868 | printf(" median run: 1 worker %.2f ms, %u workers %.2f ms, speedup %.2fx " |
| 869 | "(critical path %llu us of %llu us work)\n", R.par_1w_ns / 1e6, workers[1], |
| 870 | R.par_nw_ns / 1e6, s, (unsigned long long)rep.critical_path_us, |
| 871 | (unsigned long long)rep.total_work_us); |
| 872 | CHECK(s >= 2.0, "independent nodes run concurrently: speedup %.2f < 2", s); |
| 873 | CHECK(R.par_nw_ns < (uint64_t)rep.total_work_us * 1000ull, |
| 874 | "parallel run beats the sequential sum of work"); |
| 875 | } |
| 876 | |
| 877 | static void t_determinism(void) { |
| 878 | printf("[*] deterministic semantic result across worker counts\n"); |
| 879 | uint8_t first[8][32]; |
| 880 | uint32_t wk[4] = { 1, 2, 4, 8 }; |
| 881 | for (uint32_t k = 0; k < 4; k++) { |
| 882 | Env e; |
| 883 | CHECK(env_start(&e, wk[k], 1, 0) == 0, "env"); |
| 884 | library(&e); |
| 885 | AgGraph g; |
| 886 | AgReport rep; |
| 887 | CHECK(compile(&e, GOAL_BRANCH, 1, &g, &rep) == AG_OK_READY, "compile"); |
| 888 | AgLowered L; |
| 889 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 890 | for (uint64_t r = 1; r <= 8; r++) { |
| 891 | RxMutation m = { e.A, 0, r * 11 }; /* both arms across the runs */ |
| 892 | rx_world_publish_external(&e.w, e.ext_A, &m, 1); |
| 893 | settle(&e); |
| 894 | AgResult res; |
| 895 | full_run(&e, &L, r, "determinism", &res); |
| 896 | uint8_t d[32]; |
| 897 | semantic_digest(&e, &g, &res, d); |
| 898 | R.det_runs++; |
| 899 | if (k == 0) memcpy(first[r - 1], d, 32); |
| 900 | else if (memcmp(first[r - 1], d, 32) != 0) R.det_mismatch++; |
| 901 | } |
| 902 | env_stop(&e); |
| 903 | } |
| 904 | CHECK(R.det_mismatch == 0, "semantic result differs across worker counts (%u)", R.det_mismatch); |
| 905 | } |
| 906 | |
| 907 | static void t_optimize(void) { |
| 908 | printf("[*] optimization passes preserve the result\n"); |
| 909 | AgResult res[2]; |
| 910 | uint64_t chain[2] = { 0, 0 }; |
| 911 | AgGraph gs[2]; |
| 912 | for (int opt = 0; opt < 2; opt++) { |
| 913 | Env e; |
| 914 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 915 | library(&e); |
| 916 | AgReport rep; |
| 917 | CHECK(compile(&e, GOAL_REDUNDANT, opt, &gs[opt], &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 918 | AgLowered L; |
| 919 | CHECK(lower(&e, &L, &gs[opt], NULL) == 0, "lower"); |
| 920 | uint64_t c0 = e.w.n_crumbs, ns = 0, best = UINT64_MAX; |
| 921 | full_run(&e, &L, 1, opt ? "optimized" : "unoptimized", &res[opt]); |
| 922 | uint64_t per_run = e.w.n_crumbs - c0; |
| 923 | for (uint64_t r = 2; r <= 9; r++) { |
| 924 | run(&e, &L, r, &res[opt], &ns); |
| 925 | if (ns < best) best = ns; |
| 926 | } |
| 927 | chain[opt] = fld(&e, e.effect, 2); |
| 928 | if (opt) { |
| 929 | R.opt_before = rep.nodes_before; |
| 930 | R.opt_after = rep.nodes_after; |
| 931 | R.opt_fold = rep.passes.const_folded; |
| 932 | R.opt_cse = rep.passes.cse; |
| 933 | R.opt_reads = rep.passes.world_reads; |
| 934 | R.opt_fused = rep.passes.fused; |
| 935 | R.opt_dead = rep.passes.dead; |
| 936 | R.opt_deps = rep.passes.deps_dropped; |
| 937 | R.opt_ns_after = best; |
| 938 | R.opt_crumbs_after = per_run; |
| 939 | } else { |
| 940 | R.opt_ns_before = best; |
| 941 | R.opt_crumbs_before = per_run; |
| 942 | } |
| 943 | env_stop(&e); |
| 944 | } |
| 945 | R.opt_react_before = gs[0].n_nodes; |
| 946 | R.opt_react_after = gs[1].n_nodes; |
| 947 | printf(" nodes %u -> %u (folded %u, cse %u, world reads %u, fused %u, dead %u, " |
| 948 | "order edges dropped %u); crumbs per run %llu -> %llu\n", R.opt_before, R.opt_after, |
| 949 | R.opt_fold, R.opt_cse, R.opt_reads, R.opt_fused, R.opt_dead, R.opt_deps, |
| 950 | (unsigned long long)R.opt_crumbs_before, (unsigned long long)R.opt_crumbs_after); |
| 951 | CHECK(R.opt_before == 16 && R.opt_after == 8, "16 nodes optimize to 8 (%u)", R.opt_after); |
| 952 | CHECK(R.opt_fold >= 1 && R.opt_cse >= 1 && R.opt_reads >= 1 && R.opt_fused >= 2 && |
| 953 | R.opt_dead >= 2 && R.opt_deps >= 2, "every pass engaged"); |
| 954 | CHECK(rx_graph_check_preserved(&gs[0], &gs[1]) == 0, "effects, evidence and conditions kept"); |
| 955 | /* Same semantic result: the success node, the verified value, the effect chain. */ |
| 956 | int p0 = node_of_kind(&gs[0], AG_EFFECT_PERFORM), p1 = node_of_kind(&gs[1], AG_EFFECT_PERFORM); |
| 957 | int v0 = node_of_kind(&gs[0], AG_VERIFY), v1 = node_of_kind(&gs[1], AG_VERIFY); |
| 958 | CHECK(res[0].outcome == AG_RUN_SUCCESS && res[1].outcome == AG_RUN_SUCCESS, "both succeed"); |
| 959 | CHECK(res[0].value[p0] == res[1].value[p1] && res[0].status[v0] == res[1].status[v1], |
| 960 | "same receipts and verdicts"); |
| 961 | CHECK(chain[0] == chain[1] && chain[0] != 0, "same effect chain before and after optimization"); |
| 962 | /* (40 + 42) * 2 + 1 + (40 + 42) - 3 = 244 */ |
| 963 | CHECK(res[1].value[node_of_kind(&gs[1], AG_EFFECT_PROPOSE)] == 244, "optimized value 244"); |
| 964 | |
| 965 | /* Graph identity does not depend on builder order. */ |
| 966 | AgGraph x, y; |
| 967 | rx_graph_init(&x, SUBJ_PLAN); |
| 968 | rx_graph_init(&y, SUBJ_PLAN); |
| 969 | int a1 = N(&x, AG_CONST, AG_T_U64), b1 = N(&x, AG_CONST, AG_T_U64); |
| 970 | x.nodes[a1].imm = 1; x.nodes[b1].imm = 2; |
| 971 | int s1 = node_pure(&x, OP_ADD, a1, b1, 0); |
| 972 | rx_graph_success(&x, (uint32_t)s1, AG_OK); |
| 973 | int b2 = N(&y, AG_CONST, AG_T_U64), a2 = N(&y, AG_CONST, AG_T_U64); |
| 974 | y.nodes[a2].imm = 1; y.nodes[b2].imm = 2; |
| 975 | int s2 = node_pure(&y, OP_ADD, a2, b2, 0); |
| 976 | rx_graph_success(&y, (uint32_t)s2, AG_OK); |
| 977 | CHECK(rx_graph_validate(&x, NULL) == 0 && rx_graph_validate(&y, NULL) == 0 && |
| 978 | memcmp(x.digest, y.digest, 32) == 0, "same graph, other builder order, same identity"); |
| 979 | y.nodes[a2].imm = 3; |
| 980 | rx_graph_identify(&y); |
| 981 | CHECK(memcmp(x.digest, y.digest, 32) != 0, "a semantic change changes the identity"); |
| 982 | } |
| 983 | |
| 984 | static void t_structure(void) { |
| 985 | printf("[*] structural refusals\n"); |
| 986 | AgGraph g; |
| 987 | /* cycle */ |
| 988 | rx_graph_init(&g, SUBJ_PLAN); |
| 989 | int a = N(&g, AG_PURE, AG_T_U64), b = N(&g, AG_PURE, AG_T_U64); |
| 990 | rx_graph_data(&g, (uint32_t)a, (uint32_t)b, 0, AG_EDGE_DATA); |
| 991 | rx_graph_data(&g, (uint32_t)b, (uint32_t)a, 0, AG_EDGE_DATA); |
| 992 | CHECK(rx_graph_validate(&g, NULL) == AG_E_CYCLE, "cycle refused"); |
| 993 | /* type: perform fed a raw number instead of a proposal */ |
| 994 | rx_graph_init(&g, SUBJ_PLAN); |
| 995 | a = N(&g, AG_CONST, AG_T_U64); |
| 996 | int v = node_verify(&g, a, 0, 9); |
| 997 | node_perform(&g, a, v); |
| 998 | CHECK(rx_graph_validate(&g, NULL) == AG_E_TYPE, "perform without a proposal refused"); |
| 999 | /* effects with no order between them */ |
| 1000 | rx_graph_init(&g, SUBJ_PLAN); |
| 1001 | a = N(&g, AG_CONST, AG_T_U64); |
| 1002 | v = node_verify(&g, a, 0, 9); |
| 1003 | int p = node_propose(&g, a); |
| 1004 | node_perform(&g, p, v); |
| 1005 | node_perform(&g, p, v); |
| 1006 | CHECK(rx_graph_validate(&g, NULL) == AG_E_EFFECT_ORDER, "unordered effects refused"); |
| 1007 | /* a join whose arms can both be OK */ |
| 1008 | rx_graph_init(&g, SUBJ_PLAN); |
| 1009 | a = N(&g, AG_CONST, AG_T_U64); |
| 1010 | b = N(&g, AG_CONST, AG_T_U64); |
| 1011 | int j = N(&g, AG_JOIN, AG_T_U64); |
| 1012 | rx_graph_data(&g, (uint32_t)a, (uint32_t)j, 0, AG_EDGE_DATA); |
| 1013 | rx_graph_data(&g, (uint32_t)b, (uint32_t)j, 1, AG_EDGE_DATA); |
| 1014 | CHECK(rx_graph_validate(&g, NULL) == AG_E_JOIN, "non-exclusive join refused"); |
| 1015 | } |
| 1016 | |
| 1017 | static void t_authority(void) { |
| 1018 | printf("[*] authority-blocked node: AEGIS slow path, human approval, no mint in compile\n"); |
| 1019 | Env e; |
| 1020 | CHECK(env_start(&e, 4, 0, 1) == 0, "env"); |
| 1021 | library(&e); |
| 1022 | AgGraph g; |
| 1023 | AgReport rep; |
| 1024 | uint64_t mints0 = e.a.mints; |
| 1025 | CHECK(compile(&e, GOAL_LINEAR, 1, &g, &rep) == AG_OK_READY, "compile %d", rep.verdict); |
| 1026 | R.aegis_mints_during_compile = (uint32_t)(e.a.mints - mints0); |
| 1027 | CHECK(rep.n_missing == 1 && rep.missing[0].resource == RES_EFFECT && |
| 1028 | rep.missing[0].rights == (RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT), |
| 1029 | "compile reports the missing effect authority"); |
| 1030 | CHECK(e.a.mints == mints0, "compile minted nothing"); |
| 1031 | |
| 1032 | AgAegisBinding ab; |
| 1033 | memset(&ab, 0, sizeof ab); |
| 1034 | ab.client = &e.a.o[0]; |
| 1035 | ab.request_res = rx_aegis_res(0, RX_AEGIS_RES_REQUEST); |
| 1036 | ab.request_cap = mint(&e, SUBJ_PLAN, ab.request_res, RX_RIGHT_READ | RX_RIGHT_WRITE); |
| 1037 | for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) { |
| 1038 | ab.slot_res[j] = rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + j); |
| 1039 | ab.slot_cap[j] = mint(&e, SUBJ_PLAN, ab.slot_res[j], RX_RIGHT_READ); |
| 1040 | } |
| 1041 | AgLowered L; |
| 1042 | CHECK(lower(&e, &L, &g, &ab) == 0 && L.n_ask == 1, "lower with the AEGIS binding"); |
| 1043 | int pf = node_of_kind(&g, AG_EFFECT_PERFORM); |
| 1044 | AgResult res; |
| 1045 | run(&e, &L, 1, &res, NULL); |
| 1046 | uint32_t blocked = count_crumbs(&e, L.reaction[pf], RX_CRUMB_BLOCKED_AUTHORITY); |
| 1047 | R.authority_blocked_crumbs += blocked; |
| 1048 | CHECK(res.outcome == AG_RUN_INCOMPLETE && res.status[pf] == AG_PENDING && blocked >= 1, |
| 1049 | "perform is blocked for authority (%u crumbs)", blocked); |
| 1050 | CHECK(fld(&e, e.effect, 0) == 0, "no effect without authority"); |
| 1051 | CHECK(fld(&e, e.a.o[0].decision, 1) == RX_AEGIS_ESCALATE, "AEGIS escalated to a human"); |
| 1052 | CHECK(count_crumbs(&e, L.reaction[pf], RX_CRUMB_COMMIT) == 0, "perform never committed"); |
| 1053 | |
| 1054 | /* The human approves. Only the root mints; the slot wakes the perform. */ |
| 1055 | uint64_t seq = fld(&e, e.a.o[0].request, 0); |
| 1056 | RxMutation m[2] = { { e.a.o[0].approval, 0, seq }, { e.a.o[0].approval, 1, RX_AEGIS_APPROVE } }; |
| 1057 | CHECK(rx_world_publish_external(&e.w, e.ext_approval, m, 2) > 0, "approval"); |
| 1058 | CHECK(settle(&e) == RX_OK, "settle"); |
| 1059 | R.aegis_mints_after_approval = (uint32_t)(e.a.mints - mints0); |
| 1060 | rx_graph_collect(&L, 1, &res); |
| 1061 | CHECK(e.a.mints == mints0 + 1, "the root minted once, after approval"); |
| 1062 | CHECK(res.outcome == AG_RUN_SUCCESS && fld(&e, e.effect, 0) == 1 && fld(&e, e.effect, 1) == 135, |
| 1063 | "the waiting perform ran once its slot held authority"); |
| 1064 | /* Next run: the fast path. No AEGIS decision, no new mint. */ |
| 1065 | uint64_t decides = count_crumbs(&e, e.a.r_decide[0], RX_CRUMB_COMMIT); |
| 1066 | run(&e, &L, 2, &res, NULL); |
| 1067 | CHECK(res.outcome == AG_RUN_SUCCESS && e.a.mints == mints0 + 1 && |
| 1068 | count_crumbs(&e, e.a.r_decide[0], RX_CRUMB_COMMIT) == decides, |
| 1069 | "existing authority: resident fast path, no AEGIS round trip"); |
| 1070 | env_stop(&e); |
| 1071 | } |
| 1072 | |
| 1073 | static void t_adversarial_authority(void) { |
| 1074 | printf("[*] adversarial authority: stolen, revoked, forged, tampered\n"); |
| 1075 | Env e; |
| 1076 | CHECK(env_start(&e, 4, 0, 0) == 0, "env"); |
| 1077 | library(&e); |
| 1078 | AgGraph g; |
| 1079 | AgReport rep; |
| 1080 | |
| 1081 | /* A capability minted for someone else, placed in the principal's table. */ |
| 1082 | RxCapRef stolen = mint(&e, SUBJ_OTHER, RES_EFFECT, RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT); |
| 1083 | hold(&e, stolen, RES_EFFECT, RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT); |
| 1084 | CHECK(compile(&e, GOAL_LINEAR, 1, &g, &rep) == AG_OK_READY && rep.n_missing == 1, |
| 1085 | "a stolen capability does not count as held"); |
| 1086 | AgLowered L; |
| 1087 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 1088 | int pf = node_of_kind(&g, AG_EFFECT_PERFORM); |
| 1089 | AgResult res; |
| 1090 | run(&e, &L, 1, &res, NULL); |
| 1091 | uint32_t blocked = count_crumbs(&e, L.reaction[pf], RX_CRUMB_BLOCKED_AUTHORITY); |
| 1092 | R.authority_blocked_crumbs += blocked; |
| 1093 | CHECK(blocked >= 1 && fld(&e, e.effect, 0) == 0, "stolen capability: blocked, no effect"); |
| 1094 | env_stop(&e); |
| 1095 | |
| 1096 | /* Revoked between compile and run. */ |
| 1097 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 1098 | library(&e); |
| 1099 | CHECK(compile(&e, GOAL_LINEAR, 1, &g, &rep) == AG_OK_READY && rep.n_missing == 0, "compile"); |
| 1100 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 1101 | AienosCapRef office; |
| 1102 | aienos_cap_office(e.admin, &office); |
| 1103 | CHECK(aienos_cap_revoke(e.admin, office, (AienosCapRef){ e.effect_cap.cap_id, e.effect_cap.generation }) == 0, |
| 1104 | "revoke"); |
| 1105 | pf = node_of_kind(&g, AG_EFFECT_PERFORM); |
| 1106 | run(&e, &L, 1, &res, NULL); |
| 1107 | blocked = count_crumbs(&e, L.reaction[pf], RX_CRUMB_BLOCKED_AUTHORITY); |
| 1108 | R.authority_blocked_crumbs += blocked; |
| 1109 | CHECK(blocked >= 1 && fld(&e, e.effect, 0) == 0 && res.outcome == AG_RUN_INCOMPLETE, |
| 1110 | "revoked after compile: blocked at run time, no effect"); |
| 1111 | |
| 1112 | /* A plan that publishes where the principal holds nothing. */ |
| 1113 | AgGraph f; |
| 1114 | rx_graph_init(&f, SUBJ_PLAN); |
| 1115 | int c = N(&f, AG_CONST, AG_T_U64); |
| 1116 | f.nodes[c].imm = 666; |
| 1117 | int pub = node_publish(&f, c, 0, 0); |
| 1118 | f.nodes[pub].obj = e.secret; |
| 1119 | rx_graph_success(&f, (uint32_t)pub, AG_OK); |
| 1120 | CHECK(rx_graph_validate(&f, &e.w) == 0 && f.n_auth == 1, "forged publish declares its need"); |
| 1121 | AgLowered L2; |
| 1122 | CHECK(lower(&e, &L2, &f, NULL) == 0, "lower"); |
| 1123 | run(&e, &L2, 1, &res, NULL); |
| 1124 | blocked = count_crumbs(&e, L2.reaction[pub], RX_CRUMB_BLOCKED_AUTHORITY); |
| 1125 | R.authority_blocked_crumbs += blocked; |
| 1126 | CHECK(blocked >= 1 && fld(&e, e.secret, 0) == 0, "publish without authority: blocked, untouched"); |
| 1127 | |
| 1128 | /* The outside cannot write a graph cell, and a graph cell cannot be |
| 1129 | * written with the run token's capability. */ |
| 1130 | RxMutation m = { L.cell[0], 2, 12345 }; |
| 1131 | CHECK(rx_world_publish_external(&e.w, e.ext_run, &m, 1) < 0, "outside write to a cell refused"); |
| 1132 | env_stop(&e); |
| 1133 | } |
| 1134 | |
| 1135 | static void t_resource(void) { |
| 1136 | printf("[*] resource-blocked node\n"); |
| 1137 | Env e; |
| 1138 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 1139 | library(&e); |
| 1140 | RxResourceBudget b = e.w.budget; |
| 1141 | b.memory_bytes = 1ull << 30; |
| 1142 | rx_world_set_resources(&e.w, &b); |
| 1143 | AgGoal goal = goal_for(&e, GOAL_RESOURCE); |
| 1144 | AgConstraints c = open_constraints(); |
| 1145 | c.budget = b; |
| 1146 | AgGraph g; |
| 1147 | AgReport rep; |
| 1148 | CHECK(rx_graph_compile(&goal, &e.w, &e.caps, &c, &e.lib, 1, &g, &rep) == AG_OK_READY, |
| 1149 | "compile %d", rep.verdict); |
| 1150 | int ph = node_of_kind(&g, AG_PHYSICAL); |
| 1151 | CHECK(rep.n_resource_blocked == 1 && rep.resource_blocked[0] == ph, |
| 1152 | "compile reports the node the body cannot hold"); |
| 1153 | AgLowered L; |
| 1154 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 1155 | AgResult res; |
| 1156 | reference(&e, &g, 1); |
| 1157 | run(&e, &L, 1, &res, NULL); |
| 1158 | int pub = node_of_kind(&g, AG_WORLD_PUBLISH); |
| 1159 | int ind = -1; |
| 1160 | for (uint32_t n = 0; n < g.n_nodes; n++) |
| 1161 | if (g.nodes[n].kind == AG_VERIFY && res.status[n] == AG_OK) ind = (int)n; |
| 1162 | CHECK(res.status[ph] == AG_PENDING && res.status[pub] == AG_PENDING && ind >= 0, |
| 1163 | "blocked node and its dependants wait; the independent branch completes"); |
| 1164 | CHECK(fld(&e, e.out, 3) == 0 && res.outcome == AG_RUN_INCOMPLETE, "nothing published yet"); |
| 1165 | b.memory_bytes = 1ull << 41; |
| 1166 | rx_world_set_resources(&e.w, &b); |
| 1167 | CHECK(settle(&e) == RX_OK, "settle"); |
| 1168 | rx_graph_collect(&L, 1, &res); |
| 1169 | compare(&e, &g, &res, &g_ref, "resource resumed"); |
| 1170 | CHECK(res.outcome == AG_RUN_SUCCESS && fld(&e, e.out, 3) == res.value[pub], |
| 1171 | "admitted when the budget allows; the run completes"); |
| 1172 | env_stop(&e); |
| 1173 | } |
| 1174 | |
| 1175 | static void t_stale(void) { |
| 1176 | printf("[*] stale-generation node\n"); |
| 1177 | Env e; |
| 1178 | CHECK(env_start(&e, 4, 1, 0) == 0, "env"); |
| 1179 | library(&e); |
| 1180 | AgGraph g; |
| 1181 | AgReport rep; |
| 1182 | /* At compile time: a retired object refuses the graph. */ |
| 1183 | CHECK(rx_world_retire(&e.w, e.C) == RX_OK, "retire C"); |
| 1184 | CHECK(compile(&e, GOAL_PARALLEL, 1, &g, &rep) == AG_E_STALE && rep.n_stale == 1, |
| 1185 | "compile refuses a stale World reference"); |
| 1186 | /* After lowering: B is retired and its slot reused by a new object. */ |
| 1187 | CHECK(compile(&e, GOAL_RESOURCE, 1, &g, &rep) == AG_OK_READY, "compile"); |
| 1188 | AgLowered L; |
| 1189 | CHECK(lower(&e, &L, &g, NULL) == 0, "lower"); |
| 1190 | CHECK(rx_world_retire(&e.w, e.B) == RX_OK, "retire B"); |
| 1191 | uint64_t imposter[RX_MAX_FIELDS] = { 999 }; |
| 1192 | RxObjRef nb; |
| 1193 | CHECK(rx_world_create(&e.w, 0xA02, RX_PERSIST_RESIDENT, RES_B, imposter, &nb) == RX_OK, "new B"); |
| 1194 | AgResult res; |
| 1195 | reference(&e, &g, 1); |
| 1196 | int64_t ext = run(&e, &L, 1, &res, NULL); |
| 1197 | (void)ext; |
| 1198 | int rb = -1; |
| 1199 | for (uint32_t n = 0; n < g.n_nodes; n++) |
| 1200 | if (g.nodes[n].kind == AG_WORLD_READ && g.nodes[n].obj.id == e.B.id) rb = (int)n; |
| 1201 | uint32_t inval = 0; |
| 1202 | for (uint64_t id = 1; id <= e.w.n_crumbs; id++) { |
| 1203 | const RxCrumb *k = rx_world_crumb(&e.w, id); |
| 1204 | if (k->reaction == L.reaction[rb] && k->kind == RX_CRUMB_INVALIDATED && k->reason == RX_ERR_STALE_GEN) |
| 1205 | inval++; |
| 1206 | } |
| 1207 | CHECK(rb >= 0 && inval >= 1 && res.status[rb] == AG_PENDING, |
| 1208 | "the old-generation read is invalidated, not re-bound (%u)", inval); |
| 1209 | CHECK(res.outcome == AG_RUN_INCOMPLETE && fld(&e, e.out, 3) == 0, |
| 1210 | "nothing downstream of a stale read happens"); |
| 1211 | compare(&e, &g, &res, &g_ref, "stale"); |
| 1212 | env_stop(&e); |
| 1213 | } |
| 1214 | |
| 1215 | /* ---- placement and receipt ---- */ |
| 1216 | |
| 1217 | static void place(void) { |
| 1218 | cpu_set_t set; |
| 1219 | CPU_ZERO(&set); |
| 1220 | int count = 0; |
| 1221 | char *p = R.cpus; |
| 1222 | long n = sysconf(_SC_NPROCESSORS_CONF); |
| 1223 | for (long c = 0; c < n; c++) { |
| 1224 | char path[128]; |
| 1225 | snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c); |
| 1226 | FILE *fp = fopen(path, "r"); |
| 1227 | if (!fp) continue; |
| 1228 | unsigned long long midr = 0; |
| 1229 | int ok = fscanf(fp, "%llx", &midr) == 1; |
| 1230 | fclose(fp); |
| 1231 | if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) { |
| 1232 | CPU_SET(c, &set); |
| 1233 | count++; |
| 1234 | if (p < R.cpus + sizeof R.cpus - 5) p += sprintf(p, "%s%ld", count > 1 ? "," : "", c); |
| 1235 | } |
| 1236 | } |
| 1237 | if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) { |
| 1238 | snprintf(R.cpus, sizeof R.cpus, "unpinned"); |
| 1239 | return; |
| 1240 | } |
| 1241 | printf("[*] placed on %d Cortex-X925 cores: %s\n", count, R.cpus); |
| 1242 | } |
| 1243 | |
| 1244 | static void binary_digest(char out[65]) { |
| 1245 | strcpy(out, "unavailable"); |
| 1246 | FILE *fp = fopen("/proc/self/exe", "rb"); |
| 1247 | if (!fp) return; |
| 1248 | sha256_ctx c; |
| 1249 | sha256_init(&c); |
| 1250 | uint8_t buf[65536]; |
| 1251 | size_t n; |
| 1252 | while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); |
| 1253 | fclose(fp); |
| 1254 | uint8_t d[32]; |
| 1255 | sha256_final(&c, d); |
| 1256 | for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]); |
| 1257 | } |
| 1258 | |
| 1259 | static int judged_pass(void) { |
| 1260 | return g_fail == 0 && R.bypass == 0 && R.evidence_missing == 0 && R.ancestry_mismatch == 0 && |
| 1261 | R.det_mismatch == 0 && R.aegis_mints_during_compile == 0; |
| 1262 | } |
| 1263 | |
| 1264 | static void write_receipt(void) { |
| 1265 | char path[512]; |
| 1266 | if (omega_evidence_path("ACTION_GRAPH/rx_action_graph_receipt.json", path, sizeof path) != 0) return; |
| 1267 | FILE *fp = omega_evidence_create(path); |
| 1268 | if (!fp) return; |
| 1269 | char commit[41]; |
| 1270 | memset(commit, 0, sizeof commit); |
| 1271 | if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); |
| 1272 | const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); |
| 1273 | int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && |
| 1274 | !omega_evidence_tree_dirty(); |
| 1275 | const char *aienos = getenv("AIENOS_COMMIT"); |
| 1276 | char digest[65]; |
| 1277 | binary_digest(digest); |
| 1278 | struct utsname u; |
| 1279 | memset(&u, 0, sizeof u); |
| 1280 | uname(&u); |
| 1281 | fprintf(fp, |
| 1282 | "{\n" |
| 1283 | " \"schema\": \"OMEGA_ACTION_GRAPH_IR_V1\",\n" |
| 1284 | " \"run_id\": \"%s\",\n" |
| 1285 | " \"candidate_commit\": %s%s%s,\n" |
| 1286 | " \"candidate_bound\": %s,\n" |
| 1287 | " \"run_commit\": \"%s\",\n" |
| 1288 | " \"tree_dirty\": %s,\n" |
| 1289 | " \"aienos_commit\": %s%s%s,\n" |
| 1290 | " \"checks\": %d,\n" |
| 1291 | " \"failures\": %d,\n" |
| 1292 | " \"test_binary_sha256\": \"%s\",\n" |
| 1293 | " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"cpus\": \"%s\"},\n" |
| 1294 | " \"scope\": \"host processor; native AIENOS authority library; resident reaction world\",\n" |
| 1295 | " \"equivalence\": {\"runs_compared_with_sequential_reference\": %u, \"node_results_compared\": %u},\n" |
| 1296 | " \"evidence\": {\"required_checked\": %u, \"missing\": %u},\n" |
| 1297 | " \"causal_ancestry\": {\"nodes_checked\": %u, \"mismatches\": %u},\n" |
| 1298 | " \"determinism\": {\"runs\": %u, \"worker_counts\": [1, 2, 4, 8], \"mismatches\": %u},\n" |
| 1299 | " \"parallel\": {\"median_run_ns_1_worker\": %llu, \"median_run_ns_%llu_workers\": %llu,\n" |
| 1300 | " \"speedup\": %.3f, \"critical_path_us\": %llu, \"total_work_us\": %llu},\n" |
| 1301 | " \"optimization\": {\"nodes_before\": %u, \"nodes_after\": %u, \"constant_folded\": %u,\n" |
| 1302 | " \"cse\": %u, \"world_reads_removed\": %u, \"fused\": %u, \"dead\": %u,\n" |
| 1303 | " \"order_edges_dropped\": %u, \"crumbs_per_run_before\": %llu,\n" |
| 1304 | " \"crumbs_per_run_after\": %llu, \"best_run_ns_before\": %llu,\n" |
| 1305 | " \"best_run_ns_after\": %llu},\n" |
| 1306 | " \"authority\": {\"bypasses\": %u, \"blocked_authority_crumbs\": %u,\n" |
| 1307 | " \"mints_during_compile\": %u, \"mints_after_human_approval\": %u,\n" |
| 1308 | " \"compile_has_admin_handle\": false},\n" |
| 1309 | " \"gates\": {\n" |
| 1310 | " \"OMEGA_ACTION_GRAPH_IR_PASS\": \"%s\",\n" |
| 1311 | " \"not_claimed\": [\"graphics-processor nodes\", \"AIENOS kernel (the authority runs as a host library)\", " |
| 1312 | "\"a learned planner (procedures are fixed templates)\", \"more than one AEGIS request per graph\", " |
| 1313 | "\"retry that re-enters the world between attempts\", \"graphs above 64 nodes\"]\n" |
| 1314 | " }\n" |
| 1315 | "}\n", |
| 1316 | omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null", |
| 1317 | candidate ? "\"" : "", bound ? "true" : "false", commit, |
| 1318 | omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", |
| 1319 | aienos ? aienos : "null", aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname, |
| 1320 | u.release, u.machine, R.cpus, R.runs_compared, R.nodes_compared, R.evidence_checked, |
| 1321 | R.evidence_missing, R.ancestry_nodes, R.ancestry_mismatch, R.det_runs, R.det_mismatch, |
| 1322 | (unsigned long long)R.par_1w_ns, (unsigned long long)R.par_workers, |
| 1323 | (unsigned long long)R.par_nw_ns, (double)R.par_1w_ns / (double)(R.par_nw_ns ? R.par_nw_ns : 1), |
| 1324 | (unsigned long long)R.par_critical_us, (unsigned long long)R.par_total_us, R.opt_before, |
| 1325 | R.opt_after, R.opt_fold, R.opt_cse, R.opt_reads, R.opt_fused, R.opt_dead, R.opt_deps, |
| 1326 | (unsigned long long)R.opt_crumbs_before, (unsigned long long)R.opt_crumbs_after, |
| 1327 | (unsigned long long)R.opt_ns_before, (unsigned long long)R.opt_ns_after, R.bypass, |
| 1328 | R.authority_blocked_crumbs, R.aegis_mints_during_compile, R.aegis_mints_after_approval, |
| 1329 | rx_busy_verdict(judged_pass())); |
| 1330 | fclose(fp); |
| 1331 | printf("receipt: %s\n", path); |
| 1332 | } |
| 1333 | |
| 1334 | int main(void) { |
| 1335 | signal(SIGPIPE, SIG_IGN); |
| 1336 | place(); |
| 1337 | skills_init(); |
| 1338 | build_templates(3000); |
| 1339 | t_structure(); |
| 1340 | t_linear(); |
| 1341 | t_diamond(); |
| 1342 | t_branch(); |
| 1343 | t_failure(); |
| 1344 | t_effects(); |
| 1345 | t_parallel(); |
| 1346 | t_determinism(); |
| 1347 | t_optimize(); |
| 1348 | t_authority(); |
| 1349 | t_adversarial_authority(); |
| 1350 | t_resource(); |
| 1351 | t_stale(); |
| 1352 | printf(" compared %u runs (%u node results) with the sequential reference; " |
| 1353 | "ancestry %u nodes, %u mismatches; evidence %u required, %u missing; bypasses %u\n", |
| 1354 | R.runs_compared, R.nodes_compared, R.ancestry_nodes, R.ancestry_mismatch, |
| 1355 | R.evidence_checked, R.evidence_missing, R.bypass); |
| 1356 | printf("checks %d failures %d\n", g_checks, g_fail); |
| 1357 | write_receipt(); |
| 1358 | return rx_busy_exit(g_fail, judged_pass()); |
| 1359 | } |