Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/runtime/rx_workflow_fusion.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / rx_workflow_fusion.c` 1826 lines ยท 84109 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * OMEGA_WORKFLOW_FUSION -- repeated verified workflow fragments become 19 * MetaSkills, and only a verified, measured, canaried, promoted and published 20 * MetaSkill ever replaces what the steps did. 21 * 22 * The test plays the outside: it holds the authority admin (to give the 23 * principal what it is born with, to revoke, and to mint the promotion right), 24 * publishes World inputs and run tokens, and drives the runs. Omega's side 25 * (rx_graph, rx_fusion) gets the world, the principal's capability table and 26 * the generation store; it never holds an admin handle. 27 * 28 * Procedures (three share the workflow W = read A -> fetch(A, base) -> 29 * (*3 + 1) -> score, where `base` is a recall that also feeds a check): 30 * 31 * EFFECT W -> verify -> propose -> perform (effect boundary) 32 * PUBLISH W -> verify, publish (World boundary) 33 * COMPARE W + read B -> add -> verify, publish 34 * UNSTABLE read C -> drift -> +9 -> ... (drift: hidden input) 35 * FLAKY read B -> flaky -> *2 -> ... (fails half the time) 36 * BUDGET four outside sources + two objects (too big for one reaction) 37 * PARALLEL, PARALLEL2 two independent heavy steps joined (fusion would serialize) 38 */ 39 #include "runtime/aienos_cap.h" 40 #include "runtime/rx_fusion.h" 41 #include "runtime/rx_generation.h" 42 #include "runtime/rx_graph.h" 43 #include "runtime/rx_world.h" 44 #include "omega_evidence.h" 45 #include "omega_types.h" 46 #include "sha256.h" 47 48 #include 49 #include 50 #include 51 #include 52 #include 53 #include 54 #include 55 #include 56 #include 57 #include 58 #include 59 #include "rx_quiet_reps.h" 60 61 enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 61, SUBJ_PROPOSER = 71, SUBJ_PROMOTER = 72 }; 62 63 #define RES_CELL 0xB000001ull 64 #define RES_RUN 0xB000002ull 65 #define RES_A 0xB000010ull 66 #define RES_B 0xB000011ull 67 #define RES_C 0xB000012ull 68 #define RES_MEM 0xB000013ull 69 #define RES_OUT 0xB000014ull 70 #define RES_D 0xB000015ull 71 #define RES_EFFECT 0xB000020ull 72 73 enum { GOAL_EFFECT = 1, GOAL_PUBLISH, GOAL_COMPARE, GOAL_UNSTABLE, GOAL_FLAKY, GOAL_BUDGET, 74 GOAL_PARALLEL, GOAL_PARALLEL2, N_GOALS = GOAL_PARALLEL2 }; 75 76 enum { SK_FETCH = 1, SK_SCORE, SK_DRIFT, SK_FLAKY, SK_COMBINE4, SK_HEAVY }; 77 78 enum { MS_COMPILED = 101, MS_HYBRID, MS_LEARNED, MS_HARDWARE, MS_POISONED, MS_PARALLEL }; 79 80 #define FETCH_US 100u 81 #define SCORE_US 100u 82 #define HEAVY_US 1500u 83 #define OBSERVE_RUNS 32u 84 85 static int g_checks; 86 static int g_fail; 87 /* Measurements refused as too busy to time, or for leg skew (rx_quiet_reps.h). */ 88 static int g_untimed_compiled, g_untimed_parallel, g_untimed_poisoned; 89 90 /* One measurement's disturbance: attempts, undisturbed pairs, attempts where 91 * only one leg was disturbed, whether the leg-skew test applied, and its 92 * one-sided p for the leg the check claims is faster. */ 93 typedef struct { 94 RxPairs legs; 95 uint32_t tried, kept; 96 int known, fast, skew; 97 double p; 98 } Quiet; 99 100 #define CHECK(cond, ...) do { \ 101 g_checks++; \ 102 if (!(cond)) { \ 103 g_fail++; \ 104 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ 105 fprintf(stderr, __VA_ARGS__); \ 106 fputc('\n', stderr); \ 107 } \ 108 } while (0) 109 110 static uint64_t now_ns(void) { 111 struct timespec ts; 112 clock_gettime(CLOCK_MONOTONIC, &ts); 113 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 114 } 115 116 static uint64_t cpu_ns(void) { 117 struct timespec ts; 118 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); 119 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 120 } 121 122 /* ---- receipt figures ---- */ 123 static struct { 124 char cpus[64]; 125 /* observation */ 126 uint32_t obs_runs, obs_refused, obs_patterns, obs_fragments, obs_over_budget, obs_dropped; 127 uint32_t w_steps, w_occ, w_graphs, w_ok, w_failed, w_stable, w_samples; 128 uint32_t n_candidates, judged_unstable, judged_failure_rate, judged_few_graphs; 129 char w_signature[65]; 130 /* verification */ 131 struct { uint32_t state, reason, vectors, observed, generated, combos, hits; } ver[4]; 132 uint32_t tamper_cases, tamper_caught; 133 char tamper_names[512]; 134 uint32_t coherent_rewrite_sites; 135 /* never silently */ 136 uint32_t unpublished_refusals, unpublished_digest_equal; 137 /* measurement */ 138 AgFusionMeasure m_compiled, m_hybrid, m_parallel; 139 Quiet q_compiled, q_hybrid, q_parallel; 140 uint32_t parallel_state, hybrid_state; 141 /* canary */ 142 uint32_t canary_runs, canary_divergences, poisoned_state, poisoned_verified, poisoned_promote_refused; 143 uint32_t poisoned_canary_runs; 144 /* promotion */ 145 uint32_t promote_refused_no_right, promote_refused_same_subject, publish_refused_changed; 146 uint64_t generation; 147 /* use */ 148 uint32_t fused_graphs, fused_nodes_before, fused_nodes_after; 149 uint32_t eq_runs, eq_nodes, eq_mismatch, cross_runs, cross_mismatch, steps_checked, steps_mismatch; 150 uint32_t evidence_checked, evidence_missing, ancestry_nodes, ancestry_mismatch; 151 uint32_t det_runs, det_mismatch, bypass; 152 uint32_t n3_blocked_original, n3_blocked_fused, n3_meta_auth_exact; 153 uint32_t applied_after_publish, refused_after_publish; 154 } R; 155 156 /* ---- skills ---- */ 157 158 static uint64_t g_drift; 159 160 static uint64_t sk_fetch(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 161 (void)attempt; 162 *failed = 0; 163 return (n > 0 ? in[0] : 0) + (n > 1 ? in[1] : 0) + 100; 164 } 165 166 static uint64_t sk_score(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 167 (void)attempt; 168 uint64_t v = n ? in[0] : 0; 169 *failed = (v % 16u) == 0; 170 return *failed ? 0 : v * 7 + 3; 171 } 172 173 static uint64_t sk_drift(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 174 (void)attempt; 175 *failed = 0; 176 return (n ? in[0] : 0) + g_drift; /* depends on something the graph does not name */ 177 } 178 179 static uint64_t sk_flaky(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 180 (void)attempt; 181 uint64_t v = n ? in[0] : 0; 182 *failed = (int)(v & 1u); 183 return *failed ? 0 : v + 1; 184 } 185 186 static uint64_t sk_combine4(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 187 (void)attempt; 188 *failed = 0; 189 uint64_t s = 5; 190 for (uint32_t i = 0; i < n; i++) s += in[i] * (i + 2); 191 return s; 192 } 193 194 static uint64_t sk_heavy(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 195 (void)attempt; 196 *failed = 0; 197 return (n ? in[0] : 0) * 5 + 1; 198 } 199 200 static AgSkillTable g_skills; 201 202 static void skills_init(void) { 203 memset(&g_skills, 0, sizeof g_skills); 204 g_skills.skill[g_skills.n++] = (AgSkill){ SK_FETCH, sk_fetch, { 0x11 } }; 205 g_skills.skill[g_skills.n++] = (AgSkill){ SK_SCORE, sk_score, { 0x12 } }; 206 g_skills.skill[g_skills.n++] = (AgSkill){ SK_DRIFT, sk_drift, { 0x13 } }; 207 g_skills.skill[g_skills.n++] = (AgSkill){ SK_FLAKY, sk_flaky, { 0x14 } }; 208 g_skills.skill[g_skills.n++] = (AgSkill){ SK_COMBINE4, sk_combine4, { 0x15 } }; 209 g_skills.skill[g_skills.n++] = (AgSkill){ SK_HEAVY, sk_heavy, { 0x16 } }; 210 } 211 212 /* ---- environment ---- */ 213 214 typedef struct { 215 AienosCapAdmin *admin; 216 AienosCapView *view; 217 RxWorld w; 218 RxObjRef A, B, C, D, mem, out, effect; 219 RxCapRef ext_run, ext_A, ext_B; 220 RxCapRef cell_cap, run_cap, read_A; 221 AgCapTable caps; 222 AgLibrary lib; 223 uint32_t n_lowered; 224 const AgLowered *lowered[16]; 225 } Env; 226 227 static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { 228 AienosCapRef office; 229 aienos_cap_office(e->admin, &office); 230 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; 231 AienosCapRef r = { UINT32_MAX, 0 }; 232 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; 233 return (RxCapRef){ r.cap_id, r.generation }; 234 } 235 236 static void hold(Env *e, RxCapRef ref, uint64_t res, uint32_t rights) { 237 e->caps.cap[e->caps.n].ref = ref; 238 e->caps.cap[e->caps.n].resource = res; 239 e->caps.cap[e->caps.n].rights = rights; 240 e->caps.n++; 241 } 242 243 static uint64_t fld(Env *e, RxObjRef r, uint32_t i) { 244 RxObject o; 245 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; 246 return o.field[i]; 247 } 248 249 static int settle(Env *e) { return rx_world_wait_quiescent(&e->w, 30000); } 250 251 static AgGraph T[N_GOALS + 1]; 252 253 static void library(Env *e) { 254 memset(&e->lib, 0, sizeof e->lib); 255 for (uint32_t k = 1; k <= N_GOALS && e->lib.n < AG_MAX_PROCS; k++) 256 e->lib.proc[e->lib.n++] = (AgProcedure){ k, &T[k] }; 257 } 258 259 static int env_start(Env *e, uint32_t workers) { 260 memset(e, 0, sizeof *e); 261 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; 262 if (rx_world_init_native(&e->w, e->view, workers, 1u << 18) != RX_OK) return -1; 263 e->w.external_subject = SUBJ_EXTERNAL; 264 const uint32_t R_ = RX_RIGHT_READ, W_ = RX_RIGHT_WRITE, RW = R_ | W_; 265 uint64_t z[RX_MAX_FIELDS] = { 0 }; 266 uint64_t a[RX_MAX_FIELDS] = { 40 }, b[RX_MAX_FIELDS] = { 7 }, c[RX_MAX_FIELDS] = { 3 }; 267 uint64_t d[RX_MAX_FIELDS] = { 9 }; 268 uint64_t m[RX_MAX_FIELDS] = { 11, 500, 12, 600, 13, 700, 0, 0 }; 269 if (rx_world_create(&e->w, 0xB01, RX_PERSIST_RESIDENT, RES_A, a, &e->A) != RX_OK || 270 rx_world_create(&e->w, 0xB02, RX_PERSIST_RESIDENT, RES_B, b, &e->B) != RX_OK || 271 rx_world_create(&e->w, 0xB03, RX_PERSIST_RESIDENT, RES_C, c, &e->C) != RX_OK || 272 rx_world_create(&e->w, 0xB04, RX_PERSIST_RESIDENT, RES_D, d, &e->D) != RX_OK || 273 rx_world_create(&e->w, AG_OT_MEMORY, RX_PERSIST_RESIDENT, RES_MEM, m, &e->mem) != RX_OK || 274 rx_world_create(&e->w, 0xB05, RX_PERSIST_RESIDENT, RES_OUT, z, &e->out) != RX_OK || 275 rx_world_create(&e->w, AG_OT_EFFECT, RX_PERSIST_RESIDENT, RES_EFFECT, z, &e->effect) != RX_OK) 276 return -1; 277 e->ext_run = mint(e, SUBJ_EXTERNAL, RES_RUN, W_); 278 e->ext_A = mint(e, SUBJ_EXTERNAL, RES_A, W_); 279 e->ext_B = mint(e, SUBJ_EXTERNAL, RES_B, W_); 280 e->caps.subject = SUBJ_PLAN; 281 e->cell_cap = mint(e, SUBJ_PLAN, RES_CELL, RW); 282 e->run_cap = mint(e, SUBJ_PLAN, RES_RUN, R_); 283 e->read_A = mint(e, SUBJ_PLAN, RES_A, R_); 284 hold(e, e->read_A, RES_A, R_); 285 hold(e, mint(e, SUBJ_PLAN, RES_B, R_), RES_B, R_); 286 hold(e, mint(e, SUBJ_PLAN, RES_C, R_), RES_C, R_); 287 hold(e, mint(e, SUBJ_PLAN, RES_D, R_), RES_D, R_); 288 hold(e, mint(e, SUBJ_PLAN, RES_MEM, R_), RES_MEM, R_); 289 hold(e, mint(e, SUBJ_PLAN, RES_OUT, RW), RES_OUT, RW); 290 hold(e, mint(e, SUBJ_PLAN, RES_EFFECT, RW | RX_RIGHT_EFFECT), RES_EFFECT, RW | RX_RIGHT_EFFECT); 291 library(e); 292 return 0; 293 } 294 295 static uint32_t bypasses(Env *e); 296 297 static void env_stop(Env *e) { 298 rx_world_wait_quiescent(&e->w, 30000); 299 R.bypass += bypasses(e); 300 rx_world_destroy(&e->w); 301 aienos_cap_stop(e->admin, e->view); 302 } 303 304 static int lower(Env *e, AgLowered *L, const AgGraph *g, const AgSkillTable *sk) { 305 if (e->n_lowered < 16) e->lowered[e->n_lowered++] = L; 306 return rx_graph_lower(L, &e->w, g, sk, &e->caps, RES_CELL, e->cell_cap, RES_RUN, e->run_cap, NULL); 307 } 308 309 /* Commits to boundary targets must come from lowered boundary reactions. */ 310 static uint32_t bypasses(Env *e) { 311 uint32_t bad = 0; 312 for (uint64_t id = 1; id <= e->w.n_crumbs; id++) { 313 const RxCrumb *k = rx_world_crumb(&e->w, id); 314 if (k->kind != RX_CRUMB_COMMIT) continue; 315 for (uint32_t o = 0; o < k->n_outputs; o++) { 316 uint32_t oid = k->outputs[o].obj.id; 317 if (oid != e->effect.id && oid != e->out.id) continue; 318 int legit = 0; 319 for (uint32_t l = 0; l < e->n_lowered; l++) { 320 const AgLowered *L = e->lowered[l]; 321 for (uint32_t n = 0; n < L->g->n_nodes; n++) 322 if (L->reaction[n] == k->reaction && 323 (L->g->nodes[n].kind == AG_EFFECT_PERFORM || 324 L->g->nodes[n].kind == AG_WORLD_PUBLISH) && 325 L->g->nodes[n].obj.id == oid) 326 legit = 1; 327 } 328 if (!legit) bad++; 329 } 330 } 331 return bad; 332 } 333 334 static uint32_t count_crumbs(Env *e, uint32_t reaction, RxCrumbKind kind) { 335 uint32_t n = 0; 336 for (uint64_t id = 1; id <= e->w.n_crumbs; id++) { 337 const RxCrumb *k = rx_world_crumb(&e->w, id); 338 if (k->reaction == reaction && k->kind == kind) n++; 339 } 340 return n; 341 } 342 343 static uint32_t commits_between(Env *e, uint64_t from, uint64_t to) { 344 uint32_t n = 0; 345 for (uint64_t id = from + 1; id <= to; id++) n += rx_world_crumb(&e->w, id)->kind == RX_CRUMB_COMMIT; 346 return n; 347 } 348 349 /* ---- procedures ---- 350 * Parameters: 1 A, 2 B, 3 C, 4 memory, 5 out, 6 effect, 7 D. */ 351 352 static int N(AgGraph *g, AgKind k, AgType t) { return rx_graph_node(g, k, t); } 353 354 static int node_read(AgGraph *g, uint32_t param, uint32_t field, uint32_t cost) { 355 int n = N(g, AG_WORLD_READ, AG_T_U64); 356 g->nodes[n].param = param; 357 g->nodes[n].field = field; 358 g->nodes[n].cost_us = cost; 359 return n; 360 } 361 362 static int node_recall(AgGraph *g, uint64_t key) { 363 int n = N(g, AG_RECALL, AG_T_U64); 364 g->nodes[n].param = 4; 365 g->nodes[n].imm = key; 366 return n; 367 } 368 369 static int node_pure(AgGraph *g, uint32_t op, int a, int b, uint64_t imm) { 370 int n = N(g, AG_PURE, AG_T_U64); 371 g->nodes[n].op = op; 372 g->nodes[n].imm = imm; 373 if (a >= 0) rx_graph_data(g, (uint32_t)a, (uint32_t)n, 0, AG_EDGE_DATA); 374 if (b >= 0) rx_graph_data(g, (uint32_t)b, (uint32_t)n, 1, AG_EDGE_DATA); 375 return n; 376 } 377 378 static int node_skill(AgGraph *g, uint32_t id, int a, int b, uint32_t cost) { 379 int n = N(g, AG_SKILL, AG_T_U64); 380 g->nodes[n].op = id; 381 g->nodes[n].cost_us = cost; 382 if (a >= 0) rx_graph_data(g, (uint32_t)a, (uint32_t)n, 0, AG_EDGE_DATA); 383 if (b >= 0) rx_graph_data(g, (uint32_t)b, (uint32_t)n, 1, AG_EDGE_DATA); 384 return n; 385 } 386 387 static int node_verify(AgGraph *g, int in, uint64_t lo, uint64_t hi) { 388 int n = N(g, AG_VERIFY, AG_T_VERDICT); 389 g->nodes[n].imm = lo; 390 g->nodes[n].imm2 = hi; 391 rx_graph_data(g, (uint32_t)in, (uint32_t)n, 0, AG_EDGE_DATA); 392 return n; 393 } 394 395 static int node_publish(AgGraph *g, int in, uint32_t field) { 396 int n = N(g, AG_WORLD_PUBLISH, AG_T_RECEIPT); 397 g->nodes[n].param = 5; 398 g->nodes[n].field = field; 399 rx_graph_data(g, (uint32_t)in, (uint32_t)n, 0, AG_EDGE_DATA); 400 return n; 401 } 402 403 /* The shared workflow. Returns its result; `base` is also checked outside. */ 404 static int workflow(AgGraph *g) { 405 int base = node_recall(g, 11); 406 int vb = node_verify(g, base, 1, `1000000`); 407 rx_graph_success(g, (uint32_t)vb, AG_OK); 408 int a = node_read(g, 1, 0, 0); 409 int f = node_skill(g, SK_FETCH, a, base, FETCH_US); 410 int p = node_pure(g, OP_MUL, f, -1, 3); 411 int q = node_pure(g, OP_ADD, p, -1, 1); 412 return node_skill(g, SK_SCORE, q, -1, SCORE_US); 413 } 414 415 /* verify(x) then publish(x) to out.field; failure when the check fails. */ 416 static void check_publish(AgGraph *g, int x, uint32_t field) { 417 int v = node_verify(g, x, 0, 1ull << 40); 418 int pub = node_publish(g, x, field); 419 rx_graph_order(g, (uint32_t)v, (uint32_t)pub); 420 rx_graph_success(g, (uint32_t)pub, AG_OK); 421 rx_graph_failure(g, (uint32_t)v, AG_FAILED); 422 } 423 424 static void build_templates(void) { 425 AgGraph *g; 426 int x; 427 428 g = &T[GOAL_EFFECT]; 429 rx_graph_init(g, 0); 430 x = workflow(g); 431 int v = node_verify(g, x, 0, 1ull << 40); 432 int pr = N(g, AG_EFFECT_PROPOSE, AG_T_PROPOSAL); 433 rx_graph_data(g, (uint32_t)x, (uint32_t)pr, 0, AG_EDGE_DATA); 434 int pf = N(g, AG_EFFECT_PERFORM, AG_T_RECEIPT); 435 g->nodes[pf].param = 6; 436 rx_graph_data(g, (uint32_t)pr, (uint32_t)pf, 0, AG_EDGE_DATA); 437 rx_graph_data(g, (uint32_t)v, (uint32_t)pf, 1, AG_EDGE_DATA); 438 rx_graph_success(g, (uint32_t)pf, AG_OK); 439 rx_graph_failure(g, (uint32_t)v, AG_FAILED); 440 441 g = &T[GOAL_PUBLISH]; 442 rx_graph_init(g, 0); 443 check_publish(g, workflow(g), 0); 444 445 g = &T[GOAL_COMPARE]; 446 rx_graph_init(g, 0); 447 x = workflow(g); 448 int b = node_read(g, 2, 0, 0); 449 check_publish(g, node_pure(g, OP_ADD, x, b, 0), 1); 450 451 g = &T[GOAL_UNSTABLE]; 452 rx_graph_init(g, 0); 453 int c = node_read(g, 3, 0, 0); 454 check_publish(g, node_pure(g, OP_ADD, node_skill(g, SK_DRIFT, c, -1, 0), -1, 9), 2); 455 456 g = &T[GOAL_FLAKY]; 457 rx_graph_init(g, 0); 458 b = node_read(g, 2, 0, 0); 459 check_publish(g, node_pure(g, OP_MUL, node_skill(g, SK_FLAKY, b, -1, 0), -1, 2), 3); 460 461 /* Four sources the fragment cannot absorb (each is also checked), and 462 * two World objects read inside it. */ 463 g = &T[GOAL_BUDGET]; 464 rx_graph_init(g, 0); 465 int e1 = node_recall(g, 11), e2 = node_recall(g, 12), e3 = node_recall(g, 13); 466 int e4 = node_read(g, 2, 0, 0); 467 int srcs[4] = { e1, e2, e3, e4 }; 468 for (int i = 0; i < 4; i++) rx_graph_success(g, (uint32_t)node_verify(g, srcs[i], 0, UINT64_MAX), AG_OK); 469 int k = N(g, AG_SKILL, AG_T_U64); 470 g->nodes[k].op = SK_COMBINE4; 471 for (int i = 0; i < 4; i++) rx_graph_data(g, (uint32_t)srcs[i], (uint32_t)k, (uint32_t)i, AG_EDGE_DATA); 472 int p1 = node_pure(g, OP_ADD, k, node_read(g, 1, 0, 0), 0); 473 int p2 = node_pure(g, OP_ADD, p1, node_read(g, 7, 0, 0), 0); 474 check_publish(g, p2, 4); 475 476 /* Two heavy steps that run side by side, joined. */ 477 for (uint32_t goal = GOAL_PARALLEL; goal <= GOAL_PARALLEL2; goal++) { 478 g = &T[goal]; 479 rx_graph_init(g, 0); 480 int ha = node_skill(g, SK_HEAVY, node_read(g, 1, 0, 0), -1, HEAVY_US); 481 int hc = node_skill(g, SK_HEAVY, node_read(g, 3, 0, 0), -1, HEAVY_US); 482 check_publish(g, node_skill(g, SK_FETCH, ha, hc, 0), goal == GOAL_PARALLEL ? 5 : 6); 483 } 484 } 485 486 static AgGoal goal_for(Env *e, uint32_t kind) { 487 AgGoal g = { kind, 7, { e->A, e->B, e->C, e->mem, e->out, e->effect, e->D } }; 488 return g; 489 } 490 491 static AgConstraints open_constraints(void) { 492 AgConstraints c; 493 memset(&c, 0, sizeof c); 494 c.max_effects = UINT32_MAX; 495 c.budget.slots = RX_MAX_REACTIONS; 496 c.budget.memory_bytes = UINT64_MAX; 497 c.budget.energy_budget = UINT64_MAX; 498 c.budget.offered_locality = UINT32_MAX; 499 c.budget.offered_accel = UINT32_MAX; 500 c.budget.compute_mask = UINT32_MAX; 501 return c; 502 } 503 504 static int compile(Env *e, uint32_t kind, AgGraph *g) { 505 AgGoal goal = goal_for(e, kind); 506 AgConstraints c = open_constraints(); 507 AgReport rep; 508 return rx_graph_compile(&goal, &e->w, &e->caps, &c, &e->lib, 1, g, &rep); 509 } 510 511 static int compile_fused(Env *e, uint32_t kind, const AgFusionLibrary *lib, const AgSkillTable *sk, 512 AgGraph *g, AgReport *rep, AgFusionReport *frep) { 513 AgGoal goal = goal_for(e, kind); 514 AgConstraints c = open_constraints(); 515 return rx_fusion_compile(&goal, &e->w, &e->caps, &c, &e->lib, lib, sk, g, rep, frep); 516 } 517 518 /* ---- running and comparing ---- */ 519 520 static uint64_t a_value(uint64_t run) { return 2 + 3 * run; } /* run = 5 (mod 16): score fails */ 521 522 static void set_inputs(Env *e, uint64_t a, uint64_t b) { 523 RxMutation m1 = { e->A, 0, a }, m2 = { e->B, 0, b }; 524 CHECK(rx_world_publish_external(&e->w, e->ext_A, &m1, 1) > 0, "publish A"); 525 CHECK(rx_world_publish_external(&e->w, e->ext_B, &m2, 1) > 0, "publish B"); 526 CHECK(settle(e) == RX_OK, "settle inputs"); 527 } 528 529 static int64_t run(Env *e, AgLowered *L, uint64_t id, AgResult *res, uint64_t *ns) { 530 uint64_t t0 = now_ns(); 531 int64_t ext = rx_graph_start(L, e->ext_run, id); 532 CHECK(ext > 0, "run token refused"); 533 CHECK(settle(e) == RX_OK, "settle"); 534 if (ns) *ns = now_ns() - t0; 535 rx_graph_collect(L, id, res); 536 return ext; 537 } 538 539 static AgReference g_ref; 540 541 /* Lowered run against the sequential reference: every node's status, 542 * value, evidence and step evidence; the outcome; the evidence owed. */ 543 static int compare(const AgGraph *g, const AgResult *got, const AgReference *ref, const char *what) { 544 R.eq_runs++; 545 int bad = 0; 546 for (uint32_t n = 0; n < g->n_nodes; n++) { 547 R.eq_nodes++; 548 if (got->status[n] != ref->r.status[n] || got->value[n] != ref->r.value[n] || 549 (got->status[n] && got->evidence[n] != ref->r.evidence[n]) || got->steps[n] != ref->r.steps[n]) { 550 bad = 1; 551 fprintf(stderr, " %s node %u (%s): got s%u v%llu, reference s%u v%llu\n", what, n, 552 rx_graph_kind_name(g->nodes[n].kind), got->status[n], 553 (unsigned long long)got->value[n], ref->r.status[n], 554 (unsigned long long)ref->r.value[n]); 555 } 556 } 557 if (got->outcome != ref->r.outcome) bad = 1; 558 if (bad) R.eq_mismatch++; 559 CHECK(!bad, "%s: lowered run differs from the sequential reference", what); 560 if (got->outcome == AG_RUN_SUCCESS) 561 for (uint32_t i = 0; i < g->n_evidence; i++) { 562 uint32_t n = g->evidence[i]; 563 R.evidence_checked++; 564 if (got->status[n] == AG_PENDING || got->evidence[n] == 0 || 565 (g->nodes[n].kind == AG_META && got->steps[n] == 0)) 566 R.evidence_missing++; 567 } 568 return !bad; 569 } 570 571 /* Crumb ancestry of every node inside the run's episode equals the graph's. */ 572 static void ancestry(Env *e, const AgLowered *L, int64_t ext, const char *what) { 573 const AgGraph *g = L->g; 574 RxWorld *w = &e->w; 575 int32_t node_of[RX_MAX_REACTIONS]; 576 for (uint32_t i = 0; i < RX_MAX_REACTIONS; i++) node_of[i] = -1; 577 for (uint32_t n = 0; n < g->n_nodes; n++) node_of[L->reaction[n]] = (int32_t)n; 578 uint64_t *stack = calloc(w->n_crumbs + 1, sizeof(uint64_t)); 579 uint8_t *seen = calloc(w->n_crumbs + 1, 1); 580 int bad = 0; 581 for (uint32_t n = 0; n < g->n_nodes; n++) { 582 uint64_t mine = 0; 583 for (uint64_t id = 1; id <= w->n_crumbs; id++) { 584 const RxCrumb *k = rx_world_crumb(w, id); 585 if (k->episode == (uint64_t)ext && k->kind == RX_CRUMB_COMMIT && k->reaction == L->reaction[n]) 586 mine = id; 587 } 588 if (!mine) { bad = 1; continue; } 589 memset(seen, 0, w->n_crumbs + 1); 590 uint64_t sp = 0, got = 0; 591 int reached_run = 0; 592 stack[sp++] = mine; 593 seen[mine] = 1; 594 while (sp) { 595 const RxCrumb *k = rx_world_crumb(w, stack[--sp]); 596 for (uint32_t i = 0; i < k->n_parents; i++) { 597 uint64_t p = k->parents[i]; 598 if (!p || seen[p]) continue; 599 const RxCrumb *q = rx_world_crumb(w, p); 600 if (!q || q->episode != (uint64_t)ext) continue; 601 seen[p] = 1; 602 if (p == (uint64_t)ext) reached_run = 1; 603 if (q->reaction < RX_MAX_REACTIONS && node_of[q->reaction] >= 0) 604 got |= 1ull << node_of[q->reaction]; 605 stack[sp++] = p; 606 } 607 } 608 R.ancestry_nodes++; 609 if (got != rx_graph_ancestors(g, n) || !reached_run) { 610 bad = 1; 611 R.ancestry_mismatch++; 612 } 613 } 614 free(stack); 615 free(seen); 616 CHECK(!bad, "%s: causal ancestry differs from the graph", what); 617 } 618 619 static int meta_node(const AgGraph *g, uint32_t skill) { 620 for (uint32_t n = 0; n < g->n_nodes; n++) 621 if (g->nodes[n].kind == AG_META && g->nodes[n].op == skill) return (int)n; 622 return -1; 623 } 624 625 static void hex(const uint8_t *d, char out[65]) { 626 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]); 627 } 628 629 /* ---- scenario 1: observation ---- */ 630 631 static AgFusionObserver g_obs; 632 static AgFusionPolicy g_pol = { 24, 2, 100, 2 }; 633 static int g_w = -1, g_par = -1; /* pattern indices: the workflow, the parallel fragment */ 634 635 static void t_observe(void) { 636 printf("[*] observation: verified runs of eight procedures\n"); 637 static Env e; 638 CHECK(env_start(&e, 4) == 0, "env"); 639 static AgGraph g[N_GOALS + 1]; 640 static AgLowered L[N_GOALS + 1]; 641 for (uint32_t k = 1; k <= N_GOALS; k++) { 642 CHECK(compile(&e, k, &g[k]) == AG_OK_READY, "compile goal %u", k); 643 CHECK(lower(&e, &L[k], &g[k], &g_skills) == 0, "lower goal %u", k); 644 } 645 rx_fusion_observer_init(&g_obs); 646 AgResult res; 647 for (uint64_t r = 1; r <= OBSERVE_RUNS; r++) { 648 set_inputs(&e, a_value(r), r); 649 for (uint32_t k = 1; k <= N_GOALS; k++) { 650 g_drift = r; 651 CHECK(rx_graph_reference(&g[k], &e.w, &g_skills, &e.caps, r, &g_ref) == 0, "reference"); 652 run(&e, &L[k], r, &res, NULL); 653 compare(&g[k], &res, &g_ref, "observe"); 654 CHECK(rx_fusion_observe(&g_obs, &e.w, &g[k], &res, &g_ref) == 1, "verified run taken in"); 655 } 656 } 657 /* A run that does not match its reference is not evidence of anything. */ 658 AgResult bad = res; 659 bad.value[0] ^= 1; 660 CHECK(rx_fusion_observe(&g_obs, &e.w, &g[N_GOALS], &bad, &g_ref) == 0, "unverified run refused"); 661 R.obs_runs = g_obs.runs_observed; 662 R.obs_refused = g_obs.runs_refused; 663 R.obs_patterns = g_obs.n; 664 R.obs_fragments = g_obs.fragments_seen; 665 R.obs_over_budget = g_obs.fragments_over_budget; 666 R.obs_dropped = g_obs.patterns_dropped; 667 CHECK(R.obs_runs == OBSERVE_RUNS * N_GOALS && R.obs_refused == 1, "runs %u refused %u", 668 R.obs_runs, R.obs_refused); 669 CHECK(R.obs_dropped == 0, "no pattern dropped"); 670 CHECK(R.obs_over_budget > 0, "the budget procedure's full fragment does not fit one reaction"); 671 672 /* The workflow: four steps, one port (base), one object (A), in the 673 * three procedures that share it. */ 674 for (uint32_t i = 0; i < g_obs.n; i++) { 675 const AgFusionPattern *p = &g_obs.p[i]; 676 if (p->prog.n_steps == 4 && p->prog.n_in == 1 && p->prog.n_obj == 1 && 677 p->prog.step[3].node.kind == AG_SKILL && p->prog.step[3].node.op == SK_SCORE) 678 g_w = (int)i; 679 if (p->prog.n_steps == 5 && p->prog.n_obj == 2 && p->prog.step[4].node.op == SK_FETCH) g_par = (int)i; 680 CHECK(!(p->prog.n_in == 4 && p->prog.n_obj == 2), "no pattern beyond the reaction budget"); 681 } 682 CHECK(g_w >= 0 && g_par >= 0, "workflow and parallel patterns found"); 683 if (g_w < 0 || g_par < 0) { env_stop(&e); return; } 684 const AgFusionPattern *w = &g_obs.p[g_w]; 685 R.w_steps = w->prog.n_steps; 686 R.w_occ = w->occurrences; 687 R.w_graphs = w->n_graphs; 688 R.w_ok = w->ok; 689 R.w_failed = w->failed; 690 R.w_stable = w->evidence_stable; 691 R.w_samples = w->n_samples; 692 hex(w->prog.signature, R.w_signature); 693 CHECK(w->occurrences == 3 * OBSERVE_RUNS && w->n_graphs == 3, "workflow: %u occurrences in %u graphs", 694 w->occurrences, w->n_graphs); 695 CHECK(w->failed == 3 * 2 && w->ok == 3 * (OBSERVE_RUNS - 2), "workflow fails where score fails"); 696 CHECK(w->evidence_unstable == 0 && w->evidence_stable > 0, "workflow evidence stable"); 697 CHECK(w->prog.obj_rights[0] == RX_RIGHT_READ && w->prog.in_type[0] == AG_T_U64 && 698 w->prog.out_type == AG_T_U64, "workflow contracts"); 699 700 /* Judging: unstable, flaky and single-graph fragments do not qualify. */ 701 uint32_t idx[AG_FUSION_MAX_PATTERNS]; 702 R.n_candidates = rx_fusion_candidates(&g_obs, &g_pol, idx, AG_FUSION_MAX_PATTERNS); 703 int w_in = 0, par_in = 0; 704 for (uint32_t i = 0; i < R.n_candidates; i++) { 705 w_in |= (int)idx[i] == g_w; 706 par_in |= (int)idx[i] == g_par; 707 } 708 CHECK(w_in && par_in, "workflow and parallel fragments are candidates"); 709 CHECK(R.n_candidates > 0 && (int)idx[0] == g_par, "largest candidate first"); 710 for (uint32_t i = 0; i < g_obs.n; i++) { 711 const AgFusionPattern *p = &g_obs.p[i]; 712 int j = rx_fusion_judge(&g_obs, i, &g_pol); 713 int drift = 0, flaky = 0; 714 for (uint32_t s = 0; s < p->prog.n_steps; s++) { 715 drift |= p->prog.step[s].node.kind == AG_SKILL && p->prog.step[s].node.op == SK_DRIFT; 716 flaky |= p->prog.step[s].node.kind == AG_SKILL && p->prog.step[s].node.op == SK_FLAKY; 717 } 718 if (drift) { R.judged_unstable += j == AG_FUSE_UNSTABLE; CHECK(j == AG_FUSE_UNSTABLE, "drift: unstable (%d)", j); } 719 if (flaky) { R.judged_failure_rate += j == AG_FUSE_FAILURE_RATE; CHECK(j == AG_FUSE_FAILURE_RATE, "flaky: failure rate (%d)", j); } 720 if (j == AG_FUSE_TOO_FEW_GRAPHS) R.judged_few_graphs++; 721 } 722 CHECK(R.judged_unstable > 0 && R.judged_failure_rate > 0, "unstable and flaky patterns seen"); 723 CHECK(R.judged_few_graphs > 0, "budget-procedure fragments appear in one graph only"); 724 printf(" %u verified runs (%u refused); %u patterns; workflow %u steps, %u occurrences in %u graphs, " 725 "%u failed; %u candidates\n", R.obs_runs, R.obs_refused, R.obs_patterns, R.w_steps, R.w_occ, 726 R.w_graphs, R.w_failed, R.n_candidates); 727 env_stop(&e); 728 } 729 730 /* ---- scenario 2: candidates and verification ---- */ 731 732 static AgMetaSkill MC, MH, ML, MW, MQ, MP; 733 734 static void verdict_note(uint32_t i, const AgMetaSkill *m) { 735 R.ver[i].state = m->state; 736 R.ver[i].reason = m->verdict.reason; 737 R.ver[i].vectors = m->verdict.vectors; 738 R.ver[i].observed = m->verdict.observed; 739 R.ver[i].generated = m->verdict.generated; 740 R.ver[i].combos = m->verdict.status_combos; 741 R.ver[i].hits = m->verdict.table_hits; 742 } 743 744 static void retag(AgMetaSkill *m) { 745 rx_graph_realization_identity(&m->realization); 746 rx_fusion_identity(m, m->identity); 747 } 748 749 static void tamper(const char *name, AgMetaSkill *t, uint32_t want) { 750 t->state = AG_MS_CANDIDATE; 751 rx_fusion_verify(t, &g_skills, 7); 752 R.tamper_cases++; 753 int caught = t->state == AG_MS_REJECTED && t->verdict.reason == want; 754 R.tamper_caught += caught; 755 CHECK(caught, "tamper %s: state %s reason %s, want %s", name, rx_fusion_state_name(t->state), 756 rx_fusion_reason_name(t->verdict.reason), rx_fusion_reason_name(want)); 757 size_t l = strlen(R.tamper_names); 758 snprintf(R.tamper_names + l, sizeof R.tamper_names - l, "%s\"%s: %s\"", l ? ", " : "", name, 759 rx_fusion_reason_name(t->verdict.reason)); 760 } 761 762 static void t_verify(void) { 763 printf("[*] candidates and verification against the original fragment\n"); 764 if (g_w < 0 || g_par < 0) return; 765 const AgFusionPattern *w = &g_obs.p[g_w]; 766 CHECK(rx_fusion_build(w, AG_REAL_COMPILED, MS_COMPILED, &MC) == 0, "build compiled"); 767 CHECK(rx_fusion_build(w, AG_REAL_HYBRID, MS_HYBRID, &MH) == 0, "build hybrid"); 768 CHECK(rx_fusion_build(w, AG_REAL_LEARNED, MS_LEARNED, &ML) == 0, "build learned"); 769 CHECK(rx_fusion_build(w, AG_REAL_HARDWARE, MS_HARDWARE, &MW) == 0, "build hardware"); 770 CHECK(rx_fusion_build(&g_obs.p[g_par], AG_REAL_COMPILED, MS_PARALLEL, &MP) == 0, "build parallel"); 771 CHECK(MC.n_ancestry > 0 && MC.state == AG_MS_CANDIDATE && MC.effect_contract.performs == 0 && 772 MC.authority_contract.n_obj == 1 && MC.authority_contract.rights[0] == RX_RIGHT_READ, 773 "compiled candidate: ancestry, READ on one object, no effects"); 774 CHECK(rx_fusion_find(&T[GOAL_EFFECT], NULL, MC.reference.signature, NULL, 0) == 0, 775 "find with no room finds nothing"); 776 777 AgMetaSkill *ms[4] = { &MC, &MH, &ML, &MW }; 778 for (uint32_t i = 0; i < 4; i++) { 779 CHECK(rx_fusion_verify(ms[i], &g_skills, 1) == 0, "verify ran"); 780 verdict_note(i, ms[i]); 781 } 782 CHECK(rx_fusion_verify(&MP, &g_skills, 1) == 0 && MP.state == AG_MS_VERIFIED, "parallel verified"); 783 CHECK(MC.state == AG_MS_VERIFIED && MC.verdict.pass && MC.verdict.observed == MC.n_samples && 784 MC.verdict.status_combos == 3 && MC.verdict.semantic_mismatch == 0 && 785 MC.verdict.failure_mismatch == 0 && MC.verdict.evidence_mismatch == 0, 786 "compiled: verified over observed inputs and every port status (%s)", 787 rx_fusion_reason_name(MC.verdict.reason)); 788 CHECK(MH.state == AG_MS_VERIFIED && MH.verdict.table_hits > 0, "hybrid: verified, learned entries used"); 789 CHECK(ML.state == AG_MS_REJECTED && ML.verdict.reason == AG_VR_NOT_RUNNABLE, 790 "learned alone: rejected outside what it saw (%s)", rx_fusion_reason_name(ML.verdict.reason)); 791 CHECK(MW.state == AG_MS_REJECTED && MW.verdict.reason == AG_VR_NOT_RUNNABLE, 792 "hardware: no realization on this host"); 793 794 /* Tampering. Each copy starts from a good candidate. */ 795 static AgMetaSkill t; 796 AgMetaSkill base_c = MC, base_h = MH; 797 base_c.state = base_h.state = AG_MS_CANDIDATE; 798 799 t = base_c; 800 t.authority_contract.rights[0] = 0; 801 retag(&t); 802 tamper("authority narrowed", &t, AG_VR_AUTHORITY); 803 804 t = base_c; 805 t.authority_contract.rights[0] |= RX_RIGHT_WRITE; 806 retag(&t); 807 tamper("authority widened", &t, AG_VR_AUTHORITY); 808 809 t = base_c; 810 t.effect_contract.performs = 1; 811 retag(&t); 812 tamper("claims an effect", &t, AG_VR_EFFECT); 813 814 t = base_c; 815 t.output_contract.may_fail = 0; 816 tamper("changed without new identity", &t, AG_VR_IDENTITY); 817 818 t = base_c; 819 t.realization.prog.step[2].node.imm += 1; /* the *3 step */ 820 rx_graph_meta_seal(&t.realization.prog); 821 retag(&t); 822 tamper("program step changed", &t, AG_VR_CONTRACT); 823 824 t = base_h; 825 uint32_t ok_i = UINT32_MAX, fail_i = UINT32_MAX; 826 for (uint32_t i = 0; i < t.realization.n_table; i++) { 827 if (t.realization.table[i].status == AG_OK && ok_i == UINT32_MAX) ok_i = i; 828 if (t.realization.table[i].status == AG_FAILED && fail_i == UINT32_MAX) fail_i = i; 829 } 830 CHECK(ok_i != UINT32_MAX && fail_i != UINT32_MAX, "hybrid table holds OK and FAILED entries"); 831 if (ok_i != UINT32_MAX && fail_i != UINT32_MAX) { 832 t.realization.table[ok_i].value ^= 1; 833 retag(&t); 834 tamper("learned wrong value", &t, AG_VR_SEMANTIC); 835 836 t = base_h; 837 t.realization.table[ok_i].steps ^= 2; 838 retag(&t); 839 tamper("learned right value, wrong evidence", &t, AG_VR_EVIDENCE); 840 841 t = base_h; 842 t.realization.table[fail_i].status = AG_OK; 843 t.realization.table[fail_i].value = 12345; 844 retag(&t); 845 tamper("learned failure turned into success", &t, AG_VR_FAILURE); 846 } 847 848 /* A coherent rewrite (reference and realization changed together) no 849 * longer reproduces what was observed, and matches no real workflow. */ 850 t = base_c; 851 t.reference.step[2].node.imm += 1; 852 rx_graph_meta_seal(&t.reference); 853 t.realization.prog = t.reference; 854 retag(&t); 855 tamper("reference and realization rewritten together", &t, AG_VR_SEMANTIC); 856 AgGraph g; 857 static Env e; 858 CHECK(env_start(&e, 1) == 0, "env"); 859 CHECK(compile(&e, GOAL_EFFECT, &g) == AG_OK_READY, "compile"); 860 AgFusionSite sites[4]; 861 R.coherent_rewrite_sites = rx_fusion_find(&g, &e.w, t.reference.signature, sites, 4); 862 CHECK(R.coherent_rewrite_sites == 0, "the rewritten fragment matches no real workflow"); 863 CHECK(rx_fusion_find(&g, &e.w, MC.reference.signature, sites, 4) == 1, "the real workflow is found once"); 864 env_stop(&e); 865 printf(" compiled %s (%u vectors: %u observed, %u generated over %u port-status combinations); " 866 "hybrid %s (%u learned hits); learned %s (%s); hardware %s; tampering caught %u/%u\n", 867 rx_fusion_state_name(MC.state), MC.verdict.vectors, MC.verdict.observed, MC.verdict.generated, 868 MC.verdict.status_combos, rx_fusion_state_name(MH.state), MH.verdict.table_hits, 869 rx_fusion_state_name(ML.state), rx_fusion_reason_name(ML.verdict.reason), 870 rx_fusion_state_name(MW.state), R.tamper_caught, R.tamper_cases); 871 } 872 873 /* ---- never silently: an unpublished MetaSkill is not applied ---- */ 874 875 static void unpublished_refused(const AgMetaSkill *m, const char *when) { 876 static Env e; 877 CHECK(env_start(&e, 1) == 0, "env"); 878 AgGraph plain, fused; 879 AgReport rep; 880 AgFusionReport frep; 881 CHECK(compile(&e, GOAL_EFFECT, &plain) == AG_OK_READY, "compile"); 882 AgFusionLibrary lib = { 1, { (AgMetaSkill *)m } }; 883 AgSkillTable sk = g_skills; 884 sk.meta[sk.n_meta++] = (typeof(sk.meta[0])){ m->id, &m->realization }; /* even if present */ 885 CHECK(compile_fused(&e, GOAL_EFFECT, &lib, &sk, &fused, &rep, &frep) == AG_OK_READY, "compile fused"); 886 int same = memcmp(plain.digest, fused.digest, 32) == 0 && meta_node(&fused, m->id) < 0; 887 R.unpublished_refusals += frep.applied == 0 && frep.refused_unpublished == 1; 888 R.unpublished_digest_equal += same; 889 CHECK(frep.applied == 0 && frep.refused_unpublished == 1 && same, 890 "%s MetaSkill not applied (applied %u refused %u)", when, frep.applied, frep.refused_unpublished); 891 env_stop(&e); 892 } 893 894 /* ---- measurement: original and fused, side by side ---- */ 895 896 typedef struct { 897 Env o, f; 898 AgGraph go, gf; 899 AgLowered lo, lf; 900 AgSkillTable trial; 901 AgFusionSite site; 902 int meta; 903 const AgMetaSkill *m; 904 } Pair; 905 906 static Pair g_pair; 907 908 static int pair_start(Pair *p, uint32_t goal, const AgMetaSkill *m, uint32_t workers) { 909 memset(p, 0, sizeof *p); 910 p->m = m; 911 if (env_start(&p->o, workers) != 0 || env_start(&p->f, workers) != 0) return -1; 912 if (compile(&p->o, goal, &p->go) != AG_OK_READY || compile(&p->f, goal, &p->gf) != AG_OK_READY) return -1; 913 AgGraph g = p->gf; 914 uint32_t applied = 0; 915 if (rx_fusion_trial(&g, &p->f.w, m, &g_skills, &p->trial, &p->gf, &applied) != 0 || applied != 1) return -1; 916 p->meta = meta_node(&p->gf, m->id); 917 if (rx_fusion_find(&p->go, &p->o.w, m->reference.signature, &p->site, 1) != 1 || p->meta < 0) return -1; 918 if (lower(&p->o, &p->lo, &p->go, &g_skills) != 0 || lower(&p->f, &p->lf, &p->gf, &p->trial) != 0) return -1; 919 return 0; 920 } 921 922 static void pair_stop(Pair *p) { 923 env_stop(&p->o); 924 env_stop(&p->f); 925 } 926 927 /* Same inputs into both; returns 1 when the fused run diverged from the 928 * original on anything the original produced. */ 929 static int pair_run(Pair *p, uint64_t id, uint64_t a, AgResult *ro, AgResult *rf, uint64_t *ns_o, 930 uint64_t *ns_f, int check_reference) { 931 set_inputs(&p->o, a, id); 932 set_inputs(&p->f, a, id); 933 run(&p->o, &p->lo, id, ro, ns_o); 934 if (check_reference) 935 CHECK(rx_graph_reference(&p->gf, &p->f.w, &p->trial, &p->f.caps, id, &g_ref) == 0, "reference"); 936 run(&p->f, &p->lf, id, rf, ns_f); 937 if (check_reference) compare(&p->gf, rf, &g_ref, "fused"); 938 uint32_t x = p->site.exit, m = (uint32_t)p->meta; 939 R.cross_runs++; 940 int div = ro->status[x] != rf->status[m] || ro->value[x] != rf->value[m] || 941 ro->attempts[x] != rf->attempts[m] || ro->outcome != rf->outcome; 942 for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) 943 div |= fld(&p->o, p->o.out, f) != fld(&p->f, p->f.out, f); 944 for (uint32_t f = 0; f < 4; f++) 945 div |= fld(&p->o, p->o.effect, f) != fld(&p->f, p->f.effect, f); 946 if (ro->status[x] != AG_PENDING) { 947 R.steps_checked++; 948 if (rx_fusion_site_steps(&p->go, &p->site, &p->m->reference, ro) != rf->steps[m]) { 949 R.steps_mismatch++; 950 div = 1; 951 } 952 } 953 R.cross_mismatch += (uint32_t)div; 954 return div; 955 } 956 957 /* ---- energy (package telemetry, where the machine has it) ---- */ 958 959 static char g_epkg[128], g_ecpu[128]; 960 961 static int energy_open(void) { 962 for (int h = 0; h < 32; h++) { 963 char path[128], name[64] = { 0 }; 964 snprintf(path, sizeof path, "/sys/class/hwmon/hwmon%d/name", h); 965 FILE *fp = fopen(path, "r"); 966 if (!fp) continue; 967 int ok = fgets(name, sizeof name, fp) != NULL; 968 fclose(fp); 969 if (!ok || strncmp(name, "aien_spbm", 9) != 0) continue; 970 snprintf(g_epkg, sizeof g_epkg, "/sys/class/hwmon/hwmon%d/energy1_input", h); 971 snprintf(g_ecpu, sizeof g_ecpu, "/sys/class/hwmon/hwmon%d/energy3_input", h); 972 return 1; 973 } 974 return 0; 975 } 976 977 static uint64_t energy_uj(const char *path) { 978 FILE *fp = fopen(path, "r"); 979 if (!fp) return 0; 980 unsigned long long v = 0; 981 if (fscanf(fp, "%llu", &v) != 1) v = 0; 982 fclose(fp); 983 return v; 984 } 985 986 typedef struct { double pkg_uj, cpu_uj, seconds; uint64_t runs; } EnergyLeg; 987 988 static void energy_leg(Pair *p, int fused, double seconds, uint64_t *next_id, EnergyLeg *out) { 989 Env *e = fused ? &p->f : &p->o; 990 AgLowered *L = fused ? &p->lf : &p->lo; 991 AgResult res; 992 uint64_t e0 = energy_uj(g_epkg), c0 = energy_uj(g_ecpu), t0 = now_ns(), n = 0; 993 while ((double)(now_ns() - t0) < seconds * 1e9) { 994 uint64_t id = (*next_id)++; 995 run(e, L, id, &res, NULL); 996 n++; 997 } 998 out->seconds = (double)(now_ns() - t0) / 1e9; 999 out->pkg_uj = (double)(energy_uj(g_epkg) - e0); 1000 out->cpu_uj = (double)(energy_uj(g_ecpu) - c0); 1001 out->runs = n; 1002 } 1003 1004 static void energy_idle(double seconds, EnergyLeg *out) { 1005 uint64_t e0 = energy_uj(g_epkg), c0 = energy_uj(g_ecpu), t0 = now_ns(); 1006 struct timespec ts = { (time_t)seconds, (long)((seconds - (double)(time_t)seconds) * 1e9) }; 1007 nanosleep(&ts, NULL); 1008 out->seconds = (double)(now_ns() - t0) / 1e9; 1009 out->pkg_uj = (double)(energy_uj(g_epkg) - e0); 1010 out->cpu_uj = (double)(energy_uj(g_ecpu) - c0); 1011 out->runs = 0; 1012 } 1013 1014 /* Energy per run of one leg with the idle power around it subtracted. */ 1015 static double per_run(const EnergyLeg *leg, const EnergyLeg *i0, const EnergyLeg *i1, int cpu) { 1016 double idle = ((cpu ? i0->cpu_uj : i0->pkg_uj) + (cpu ? i1->cpu_uj : i1->pkg_uj)) / 1017 (i0->seconds + i1->seconds); 1018 return ((cpu ? leg->cpu_uj : leg->pkg_uj) - idle * leg->seconds) / (double)leg->runs; 1019 } 1020 1021 static struct { 1022 int available; 1023 double pkg[2][2], cpu[2][2]; /* [round][before, after] */ 1024 } g_energy; 1025 1026 /* Two rounds of idle, original, idle, fused, idle (1 s each). The spread 1027 * between rounds is the noise; a difference inside it is not claimed. */ 1028 static void measure_energy(Pair *p, AgFusionMeasure *ms) { 1029 if (!energy_open()) return; 1030 uint64_t id = 100000; 1031 set_inputs(&p->o, a_value(3), 3); 1032 set_inputs(&p->f, a_value(3), 3); 1033 for (int r = 0; r < 2; r++) { 1034 EnergyLeg i0, lo, i1, lf, i2; 1035 energy_idle(1.0, &i0); 1036 energy_leg(p, 0, 1.0, &id, &lo); 1037 energy_idle(1.0, &i1); 1038 energy_leg(p, 1, 1.0, &id, &lf); 1039 energy_idle(1.0, &i2); 1040 if (!lo.runs || !lf.runs) return; 1041 g_energy.pkg[r][0] = per_run(&lo, &i0, &i1, 0); 1042 g_energy.pkg[r][1] = per_run(&lf, &i1, &i2, 0); 1043 g_energy.cpu[r][0] = per_run(&lo, &i0, &i1, 1); 1044 g_energy.cpu[r][1] = per_run(&lf, &i1, &i2, 1); 1045 } 1046 g_energy.available = 1; 1047 ms->energy_available = 1; 1048 ms->energy_uj_before = (g_energy.pkg[0][0] + g_energy.pkg[1][0]) / 2; 1049 ms->energy_uj_after = (g_energy.pkg[0][1] + g_energy.pkg[1][1]) / 2; 1050 ms->energy_noise_uj = fmax(fabs(g_energy.pkg[0][0] - g_energy.pkg[1][0]), 1051 fabs(g_energy.pkg[0][1] - g_energy.pkg[1][1])) / 2; 1052 } 1053 1054 static double cpu_mean(int leg) { return (g_energy.cpu[0][leg] + g_energy.cpu[1][leg]) / 2; } 1055 static double cpu_noise(void) { 1056 return fmax(fabs(g_energy.cpu[0][0] - g_energy.cpu[1][0]), fabs(g_energy.cpu[0][1] - g_energy.cpu[1][1])) / 2; 1057 } 1058 1059 static int cmp_u64(const void *a, const void *b) { 1060 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; 1061 return x < y ? -1 : x > y; 1062 } 1063 1064 /* 41 interleaved runs per leg at 4 workers: reactions that computed per 1065 * run, crumbs per run, median latency, CPU time, outcomes. */ 1066 /* 1 when measured; 0 when the machine was too busy to time it, or the leg 1067 * the check claims is faster (`fast`: 0 original, 1 fused) was disturbed 1068 * significantly more often than the other and the claim holds on the kept 1069 * runs (a refusal, rx_quiet_reps.h): 1070 * nothing was measured and nothing that needs it is judged. `q` gets the 1071 * disturbance counts either way. `skew` 0 skips the leg-skew test: only for 1072 * legs that keep different numbers of threads on cores, where load alone 1073 * disturbs the wider leg more often (the known limit in rx_quiet_reps.h). */ 1074 static int measure(AgMetaSkill *m, uint32_t goal, AgFusionMeasure *ms, int with_energy, const char *what, int fast, 1075 int skew, Quiet *q) { 1076 Pair *p = &g_pair; 1077 CHECK(pair_start(p, goal, m, 4) == 0, "%s: pair", what); 1078 memset(ms, 0, sizeof *ms); 1079 AgResult ro, rf; 1080 for (uint64_t r = 1; r <= 3; r++) pair_run(p, r, a_value(r), &ro, &rf, NULL, NULL, 0); 1081 enum { RUNS = 41 }; 1082 /* RUNS undisturbed pairs (rx_quiet_reps.h): a pair counts only when 1083 * neither leg's threads waited for a core, at most 60 attempts per pair. 1084 * Every attempt is still checked for the same outcome on both legs. */ 1085 enum { ATTEMPTS = RUNS * 60 }; 1086 uint64_t ns_o[RUNS], ns_f[RUNS], cpu_o = 0, cpu_f = 0, cr_o = 0, cr_f = 0, cm_o = 0, cm_f = 0; 1087 uint32_t kept = 0, tried = 0; 1088 int delay_known = 1; 1089 memset(q, 0, sizeof *q); 1090 q->fast = fast; 1091 q->skew = skew; 1092 for (uint64_t i = 0; i < ATTEMPTS && kept < RUNS; i++) { 1093 uint64_t id = 10 + i, a = a_value(id), t_o = 0, t_f = 0; 1094 set_inputs(&p->o, a, id); 1095 set_inputs(&p->f, a, id); 1096 uint64_t c0 = p->o.w.n_crumbs, t0 = cpu_ns(); 1097 int64_t d0 = rx_run_delay_ns(); 1098 run(&p->o, &p->lo, id, &ro, &t_o); 1099 int64_t d1 = rx_run_delay_ns(); 1100 uint64_t u_o = cpu_ns() - t0, k_o = p->o.w.n_crumbs - c0; 1101 uint64_t m_o = commits_between(&p->o, c0, p->o.w.n_crumbs); 1102 c0 = p->f.w.n_crumbs; 1103 t0 = cpu_ns(); 1104 int64_t d2 = rx_run_delay_ns(); 1105 run(&p->f, &p->lf, id, &rf, &t_f); 1106 int64_t d3 = rx_run_delay_ns(); 1107 uint64_t u_f = cpu_ns() - t0, k_f = p->f.w.n_crumbs - c0; 1108 uint64_t m_f = commits_between(&p->f, c0, p->f.w.n_crumbs); 1109 tried++; 1110 CHECK(ro.outcome == rf.outcome, "%s: attempt %u: outcomes differ", what, tried); 1111 if (d0 < 0 || d1 < 0 || d2 < 0 || d3 < 0) delay_known = 0; 1112 int quiet_o = rx_quiet_rep(delay_known ? d1 - d0 : -1, t_o); 1113 int quiet_f = rx_quiet_rep(delay_known ? d3 - d2 : -1, t_f); 1114 rx_pairs_note(&q->legs, quiet_o, quiet_f); 1115 if (!quiet_o || !quiet_f) 1116 continue; 1117 ns_o[kept] = t_o; 1118 ns_f[kept] = t_f; 1119 kept++; 1120 cpu_o += u_o; 1121 cr_o += k_o; 1122 cm_o += m_o; 1123 cpu_f += u_f; 1124 cr_f += k_f; 1125 cm_f += m_f; 1126 ms->ok_before += ro.outcome == AG_RUN_SUCCESS; 1127 ms->failed_before += ro.outcome == AG_RUN_FAILURE; 1128 ms->ok_after += rf.outcome == AG_RUN_SUCCESS; 1129 ms->failed_after += rf.outcome == AG_RUN_FAILURE; 1130 } 1131 q->tried = tried; 1132 q->kept = kept; 1133 q->known = delay_known; 1134 q->p = rx_pairs_skew_p(&q->legs, fast); 1135 printf(" %s: undisturbed pairs %u of %u (%s); only original disturbed %u, only fused disturbed %u, ", what, 1136 kept, tried, delay_known ? "run-queue delay from schedstat" : "schedstat missing: every pair counts", 1137 q->legs.only[0], q->legs.only[1]); 1138 if (skew) 1139 printf("one-sided p that the %s leg was disturbed more %.3g\n", fast ? "fused" : "original", q->p); 1140 else 1141 printf("leg-skew test not applied: the legs keep different numbers of threads on cores\n"); 1142 /* Skew sets aside more of the claimed-faster leg's runs, which only 1143 * favours the claim: a claim that fails on the kept runs is still judged 1144 * (FAIL, as on main); one that holds is refused below. */ 1145 int skewed = skew && delay_known && q->p <= RX_SKEW_ALPHA; 1146 if (skewed && kept < RUNS) { 1147 rx_skew_refuse(what, q->legs.only[fast ? 1 : 0], q->legs.only[fast ? 0 : 1], q->p); 1148 pair_stop(p); 1149 return 0; 1150 } 1151 if (kept < RUNS) { 1152 fprintf(stderr, " %s: machine too busy to time: %u of %u undisturbed pairs in %u attempts\n", 1153 what, kept, RUNS, (unsigned)ATTEMPTS); 1154 rx_busy_refuse(what); 1155 pair_stop(p); 1156 return 0; 1157 } 1158 qsort(ns_o, RUNS, sizeof ns_o[0], cmp_u64); 1159 qsort(ns_f, RUNS, sizeof ns_f[0], cmp_u64); 1160 ms->runs = RUNS; 1161 ms->median_ns_before = ns_o[RUNS / 2]; 1162 ms->spread_permille_before = (uint32_t)((ns_o[(3 * RUNS) / 4] - ns_o[RUNS / 4]) * 1000u / 1163 (ns_o[RUNS / 2] ? ns_o[RUNS / 2] : 1)); 1164 ms->median_ns_after = ns_f[RUNS / 2]; 1165 ms->cpu_ns_before = cpu_o / RUNS; 1166 ms->cpu_ns_after = cpu_f / RUNS; 1167 ms->crumbs_before = (double)cr_o / RUNS; 1168 ms->crumbs_after = (double)cr_f / RUNS; 1169 ms->commits_before = (double)cm_o / RUNS; 1170 ms->commits_after = (double)cm_f / RUNS; 1171 ms->reactions_before = p->go.n_nodes; 1172 ms->reactions_after = p->gf.n_nodes; 1173 ms->objects_before = p->go.n_nodes + 1; 1174 ms->objects_after = p->gf.n_nodes + 1; 1175 if (skewed) { 1176 static AgMetaSkill probe; /* accept reads only state and the measure */ 1177 probe.state = AG_MS_VERIFIED; 1178 int accepted = rx_fusion_accept_measure(&probe, ms, 50) == 0; 1179 if (fast ? accepted : !accepted) { 1180 rx_skew_refuse(what, q->legs.only[fast ? 1 : 0], q->legs.only[fast ? 0 : 1], q->p); 1181 pair_stop(p); 1182 return 0; 1183 } 1184 printf(" %s: leg skew, but the claim fails even on the kept runs: judged\n", what); 1185 } 1186 if (with_energy) measure_energy(p, ms); 1187 pair_stop(p); 1188 printf(" %s: reactions computing per run %.1f -> %.1f, crumbs %.1f -> %.1f, median %.1f -> %.1f us, " 1189 "CPU %.1f -> %.1f us/run", what, ms->commits_before, ms->commits_after, ms->crumbs_before, 1190 ms->crumbs_after, ms->median_ns_before / 1e3, ms->median_ns_after / 1e3, 1191 ms->cpu_ns_before / 1e3, ms->cpu_ns_after / 1e3); 1192 if (ms->energy_available) 1193 printf("; energy per run (idle subtracted, spread between two rounds): package %.1f -> %.1f " 1194 "(+/- %.1f) uJ, performance cores %.1f -> %.1f (+/- %.1f) uJ", ms->energy_uj_before, 1195 ms->energy_uj_after, ms->energy_noise_uj, cpu_mean(0), cpu_mean(1), cpu_noise()); 1196 printf("\n"); 1197 return 1; 1198 } 1199 1200 /* The leg-skew tail (rx_quiet_reps.h) against WolframLanguageEvaluator, 1201 * 2026-10-31: up[n_, k_] := 1 - CDF[BinomialDistribution[n, 1/2], k - 1]; 1202 * crit[n_] := Min[Select[Range[0, n + 1], up[n, #] <= 1/1000 &]]. */ 1203 static void t_skew_math(void) { 1204 printf("[*] leg-skew tails against exact values\n"); 1205 static const struct { uint32_t n, k; double want; } sign[] = { 1206 { 10, 10, 0.`0009765625` }, /* up[10, 10] -> 1/1024 */ 1207 { 10, 9, 0.`0107421875` }, /* up[10, 9] */ 1208 { 21, 21, 4.`76837158203125e`-7 }, /* up[21, 21] */ 1209 { 40, 30, 0.`00111071688661468` }, /* up[40, 30] */ 1210 { 40, 29, 0.`00321328804784571` }, /* up[40, 29] */ 1211 { 600, 340, 0.`000620253095612713` }, /* up[600, 340] */ 1212 { 2460, 1300, 0.`00253021285261641` }, /* up[2460, 1300] */ 1213 { 7, 0, 1.0 }, { 7, 8, 0.0 }, 1214 }; 1215 for (size_t i = 0; i < sizeof sign / sizeof sign[0]; i++) { 1216 double got = rx_sign_upper(sign[i].n, sign[i].k); 1217 CHECK(fabs(got - sign[i].want) <= 1e-9 * sign[i].want + 1e-300, "P(Bin(%u, 1/2) >= %u) = %.12g, want %.12g", 1218 sign[i].n, sign[i].k, got, sign[i].want); 1219 } 1220 /* crit[n] for n = 10, 20, 30, 50, 100, 300, 600, 1000, 2460 */ 1221 static const uint32_t crit[][2] = { { 10, 10 }, { 20, 18 }, { 30, 24 }, { 50, 37 }, { 100, 66 }, 1222 { 300, 178 }, { 600, 339 }, { 1000, 550 }, { 2460, 1308 } }; 1223 for (size_t i = 0; i < sizeof crit / sizeof crit[0]; i++) { 1224 RxPairs s = { { crit[i][0] - crit[i][1], crit[i][1] } }; 1225 RxPairs t = { { crit[i][0] - crit[i][1] + 1, crit[i][1] - 1 } }; 1226 CHECK(rx_pairs_skew_p(&s, 1) <= RX_SKEW_ALPHA && rx_pairs_skew_p(&t, 1) > RX_SKEW_ALPHA, 1227 "n %u: smallest refused count %u", crit[i][0], crit[i][1]); 1228 } 1229 RxPairs nine = { { 0, 9 } }; 1230 CHECK(rx_pairs_skew_p(&nine, 1) > RX_SKEW_ALPHA, "nine one-leg disturbances alone are never refused"); 1231 } 1232 1233 static void t_measure(void) { 1234 printf("[*] measurement: original and fused side by side\n"); 1235 if (MC.state != AG_MS_VERIFIED) return; 1236 unpublished_refused(&MC, "verified"); 1237 /* A refused measurement (machine too busy to time, or leg skew) leaves its 1238 * MetaSkill verified and unmeasured: the acceptance below, and for the 1239 * compiled one the canary, promotion and use that follow it, are not 1240 * judged. The leg each check claims is faster is the one tested for skew: 1241 * the fused leg where it must be accepted, the original where the 1242 * parallel fragment must be refused as slower. The parallel check runs no 1243 * skew test: its original leg keeps up to four workers on cores and the 1244 * fused leg one, so load alone disturbs the original more often (known 1245 * limit, rx_quiet_reps.h). */ 1246 g_untimed_compiled = !measure(&MC, GOAL_EFFECT, &R.m_compiled, 1, "compiled", 1, 1, &R.q_compiled); 1247 if (!g_untimed_compiled) 1248 CHECK(rx_fusion_accept_measure(&MC, &R.m_compiled, 50) == 0 && MC.state == AG_MS_MEASURED, 1249 "compiled accepted: fewer reactions, not slower, same outcomes"); 1250 R.m_compiled = MC.measure; 1251 if (measure(&MH, GOAL_EFFECT, &R.m_hybrid, 0, "hybrid", 1, 1, &R.q_hybrid)) { 1252 rx_fusion_accept_measure(&MH, &R.m_hybrid, 50); 1253 CHECK(MH.state == AG_MS_MEASURED, "hybrid accepted"); 1254 } 1255 R.m_hybrid = MH.measure; 1256 R.hybrid_state = MH.state; 1257 g_untimed_parallel = !measure(&MP, GOAL_PARALLEL, &R.m_parallel, 0, "parallel", 0, 0, &R.q_parallel); 1258 if (!g_untimed_parallel) { 1259 CHECK(rx_fusion_accept_measure(&MP, &R.m_parallel, 50) != 0 && MP.state == AG_MS_SLOWER, 1260 "parallel fragment refused: fusing it serializes independent work (%.0f -> %.0f us)", 1261 R.m_parallel.median_ns_before / 1e3, R.m_parallel.median_ns_after / 1e3); 1262 CHECK(rx_fusion_canary(&MP, 0, 1) == AG_FUSION_E_STATE, "a slower MetaSkill cannot enter canary"); 1263 } 1264 R.parallel_state = MP.state; 1265 R.m_parallel = MP.measure; 1266 unpublished_refused(&MC, "measured"); 1267 } 1268 1269 /* ---- canary ---- */ 1270 1271 static void t_canary(void) { 1272 printf("[*] canary: fused graph in shadow beside production\n"); 1273 if (MC.state != AG_MS_MEASURED) return; 1274 Pair *p = &g_pair; 1275 CHECK(pair_start(p, GOAL_EFFECT, &MC, 4) == 0, "pair"); 1276 AgResult ro, rf; 1277 for (uint64_t r = 1; r <= 16 && MC.state == AG_MS_MEASURED; r++) { 1278 uint64_t a = r == 10 ? 4242 : 1000 + 7 * r; /* traffic it never saw */ 1279 int div = pair_run(p, 200 + r, a, &ro, &rf, NULL, NULL, 1); 1280 rx_fusion_canary(&MC, div, 16); 1281 } 1282 pair_stop(p); 1283 R.canary_runs = MC.canary_runs; 1284 R.canary_divergences = MC.canary_divergences; 1285 CHECK(MC.state == AG_MS_CANARY_PASSED && MC.canary_divergences == 0, "compiled passed canary (%s)", 1286 rx_fusion_state_name(MC.state)); 1287 1288 /* A hybrid with one poisoned learned entry, for an input verification 1289 * never generates. It verifies; the canary catches it. */ 1290 MQ = MH; 1291 MQ.id = MS_POISONED; 1292 MQ.state = AG_MS_CANDIDATE; 1293 AgMetaEntry *q = &MQ.realization.table[MQ.realization.n_table++]; 1294 memset(q, 0, sizeof *q); 1295 q->s[0] = AG_OK; 1296 q->v[0] = 500; 1297 uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS]; 1298 memset(world, 0, sizeof world); 1299 world[0][0] = 4242; 1300 q->world = rx_graph_meta_world_key(&MQ.realization.prog, (const uint64_t (*)[RX_MAX_FIELDS])world); 1301 q->status = AG_OK; 1302 q->value = 1; 1303 q->steps = 1; 1304 retag(&MQ); 1305 rx_fusion_verify(&MQ, &g_skills, 1); 1306 R.poisoned_verified = MQ.state == AG_MS_VERIFIED; 1307 CHECK(MQ.state == AG_MS_VERIFIED, "poisoned hybrid passes verification (the entry is never generated)"); 1308 AgFusionMeasure mq; 1309 Quiet qq; 1310 g_untimed_poisoned = !measure(&MQ, GOAL_EFFECT, &mq, 0, "poisoned hybrid", 1, 1, &qq); 1311 if (g_untimed_poisoned) { /* unmeasured: its canary and quarantine are not judged */ 1312 unpublished_refused(&MC, "canary-passed"); 1313 return; 1314 } 1315 rx_fusion_accept_measure(&MQ, &mq, 50); 1316 CHECK(pair_start(p, GOAL_EFFECT, &MQ, 4) == 0, "pair"); 1317 /* The divergence here is the point; it is kept out of the fusion totals. */ 1318 uint32_t saved_runs = R.cross_runs, saved_mis = R.cross_mismatch, saved_sc = R.steps_checked, 1319 saved_sm = R.steps_mismatch; 1320 for (uint64_t r = 1; r <= 16 && MQ.state == AG_MS_MEASURED; r++) { 1321 uint64_t a = r == 10 ? 4242 : 1000 + 7 * r; 1322 int div = pair_run(p, 300 + r, a, &ro, &rf, NULL, NULL, 0); 1323 rx_fusion_canary(&MQ, div, 16); 1324 } 1325 R.poisoned_canary_runs = MQ.canary_runs; 1326 R.cross_runs = saved_runs; 1327 R.cross_mismatch = saved_mis; 1328 R.steps_checked = saved_sc; 1329 R.steps_mismatch = saved_sm; 1330 pair_stop(p); 1331 R.poisoned_state = MQ.state; 1332 CHECK(MQ.state == AG_MS_QUARANTINED && MQ.canary_divergences == 1, "poisoned hybrid quarantined (%s)", 1333 rx_fusion_state_name(MQ.state)); 1334 unpublished_refused(&MC, "canary-passed"); 1335 } 1336 1337 /* ---- promotion and publication ---- */ 1338 1339 typedef struct { AienosCapView *view; } AuthCtx; 1340 1341 static int native_auth(void *ctx, uint32_t cap_id, uint64_t cap_generation, uint32_t subject, 1342 uint64_t resource, uint32_t rights) { 1343 AuthCtx *a = ctx; 1344 AienosCapEntry entry; 1345 return aienos_cap_validate(a->view, (AienosCapRef){ cap_id, cap_generation }, subject, resource, 1346 rights, &entry); 1347 } 1348 1349 static AgSkillTable g_pub; 1350 static char g_store_dir[64]; 1351 1352 static void t_promote(void) { 1353 printf("[*] promotion through the generation barrier, then publication\n"); 1354 if (g_untimed_compiled) { /* never measured, so never canary-passed */ 1355 printf(" not judged: the compiled MetaSkill was not measured (refused, see NOT RUN above)\n"); 1356 return; 1357 } 1358 static Env e; 1359 CHECK(env_start(&e, 1) == 0, "env"); 1360 strcpy(g_store_dir, "/tmp/rx-fusion-XXXXXX"); 1361 CHECK(mkdtemp(g_store_dir) != NULL, "store dir"); 1362 RxGenStore *store = NULL; 1363 CHECK(rx_gen_open(g_store_dir, &store) == RX_GEN_OK, "open store"); 1364 AuthCtx ac = { e.view }; 1365 1366 RxCapRef weak = mint(&e, SUBJ_PROMOTER, RX_GEN_RES_PROMOTION, RX_RIGHT_READ); 1367 RxCapRef right = mint(&e, SUBJ_PROMOTER, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE); 1368 RxCapRef self = mint(&e, SUBJ_PROPOSER, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE); 1369 RxPromotionRequest req = { 0, SUBJ_PROMOTER, weak.cap_id, weak.generation, RX_GEN_RES_PROMOTION, 1370 RX_GEN_RIGHT_PROMOTE, {0, {0}}}; 1371 uint64_t active0 = 0, lineage0 = 0; 1372 CHECK(rx_gen_active(store, &active0, &lineage0) == RX_GEN_OK, "store has an active generation"); 1373 1374 if (!g_untimed_poisoned) { 1375 CHECK(rx_fusion_promote(&MQ, store, SUBJ_PROPOSER, &req, native_auth, &ac) == AG_FUSION_E_STATE, 1376 "quarantined MetaSkill cannot be promoted"); 1377 R.poisoned_promote_refused = 1; 1378 } 1379 int rc = rx_fusion_promote(&MC, store, SUBJ_PROPOSER, &req, native_auth, &ac); 1380 R.promote_refused_no_right = rc == RX_GEN_ERR_AUTHORITY && MC.state == AG_MS_CANARY_PASSED; 1381 CHECK(R.promote_refused_no_right, "promotion without the promote right refused (%d)", rc); 1382 req = (RxPromotionRequest){ 0, SUBJ_PROPOSER, self.cap_id, self.generation, RX_GEN_RES_PROMOTION, 1383 RX_GEN_RIGHT_PROMOTE, {0, {0}} }; 1384 rc = rx_fusion_promote(&MC, store, SUBJ_PROPOSER, &req, native_auth, &ac); 1385 R.promote_refused_same_subject = rc == RX_GEN_ERR_AUTHORITY && MC.state == AG_MS_CANARY_PASSED; 1386 CHECK(R.promote_refused_same_subject, "the proposer cannot promote its own candidate (%d)", rc); 1387 uint64_t active = 0, lineage = 0; 1388 CHECK(rx_gen_active(store, &active, &lineage) == RX_GEN_OK && active == active0, 1389 "refusals left the active generation as it was"); 1390 1391 req = (RxPromotionRequest){ 0, SUBJ_PROMOTER, right.cap_id, right.generation, RX_GEN_RES_PROMOTION, 1392 RX_GEN_RIGHT_PROMOTE, {0, {0}} }; 1393 rc = rx_fusion_promote(&MC, store, SUBJ_PROPOSER, &req, native_auth, &ac); 1394 CHECK(rc == RX_GEN_OK && MC.state == AG_MS_PROMOTED && MC.generation != active0, "promoted (%d)", rc); 1395 R.generation = MC.generation; 1396 CHECK(rx_gen_active(store, &active, &lineage) == RX_GEN_OK && active == MC.generation, 1397 "the promoted generation is active"); 1398 unpublished_refused(&MC, "promoted"); 1399 1400 /* The MetaSkill changed after promotion: publication refused. */ 1401 static AgMetaSkill changed; 1402 changed = MC; 1403 changed.realization.prog.step[1].node.cost_us += 1; 1404 rx_graph_meta_seal(&changed.realization.prog); 1405 retag(&changed); 1406 rc = rx_fusion_publish(&changed, store, &g_pub); 1407 R.publish_refused_changed = rc == AG_FUSION_E_IDENTITY; 1408 CHECK(R.publish_refused_changed && g_pub.n_meta == 0, "changed after promotion: not published (%d)", rc); 1409 1410 g_pub = g_skills; 1411 CHECK(rx_fusion_publish(&MC, store, &g_pub) == 0 && MC.state == AG_MS_PUBLISHED && g_pub.n_meta == 1, 1412 "published into the Skill Net table"); 1413 rx_gen_close(store); 1414 env_stop(&e); 1415 printf(" refused without the right and for the proposer itself; generation %llu active; " 1416 "published as skill %u\n", (unsigned long long)MC.generation, MC.id); 1417 } 1418 1419 /* ---- use after publication ---- */ 1420 1421 static void semantic_digest(const AgGraph *g, const AgResult *r, Env *e, uint8_t out[32]) { 1422 sha256_ctx c; 1423 sha256_init(&c); 1424 for (uint32_t n = 0; n < g->n_nodes; n++) { 1425 sha256_update(&c, &r->status[n], 1); 1426 sha256_update(&c, (const uint8_t *)&r->value[n], 8); 1427 sha256_update(&c, (const uint8_t *)&r->evidence[n], 8); 1428 sha256_update(&c, (const uint8_t *)&r->steps[n], 8); 1429 } 1430 for (uint32_t f = 0; f < 4; f++) { 1431 uint64_t v = fld(e, e->effect, f); 1432 sha256_update(&c, (const uint8_t *)&v, 8); 1433 } 1434 sha256_final(&c, out); 1435 } 1436 1437 static void t_use(void) { 1438 printf("[*] use: compile applies the published MetaSkill, and only it\n"); 1439 if (MC.state != AG_MS_PUBLISHED) return; 1440 AgFusionLibrary lib = { 4, { &MC, &MQ, &MP, &MH } }; 1441 static Env o, f; 1442 for (uint32_t goal = GOAL_EFFECT; goal <= GOAL_COMPARE; goal++) { 1443 CHECK(env_start(&o, 4) == 0 && env_start(&f, 4) == 0, "envs"); 1444 static AgGraph go, gf; 1445 AgReport rep; 1446 AgFusionReport frep; 1447 CHECK(compile(&o, goal, &go) == AG_OK_READY, "compile original"); 1448 CHECK(compile_fused(&f, goal, &lib, &g_pub, &gf, &rep, &frep) == AG_OK_READY, "compile fused %d", 1449 rep.verdict); 1450 R.applied_after_publish += frep.applied; 1451 R.refused_after_publish += frep.refused_unpublished; 1452 CHECK(frep.applied == 1 && frep.fused[0].skill == MS_COMPILED && frep.fused[0].steps == 4 && 1453 frep.refused_unpublished == 3, "goal %u: one fusion reported, three unpublished refused", goal); 1454 CHECK(memcmp(go.digest, gf.digest, 32) != 0 && gf.n_nodes == go.n_nodes - 3 && rep.n_missing == 0, 1455 "the fused graph is a different, smaller graph with nothing missing"); 1456 R.fused_graphs++; 1457 R.fused_nodes_before += go.n_nodes; 1458 R.fused_nodes_after += gf.n_nodes; 1459 int m = meta_node(&gf, MS_COMPILED); 1460 /* Authority of the fused node: READ on A, exactly. */ 1461 uint32_t na = 0, exact = 1; 1462 for (uint32_t i = 0; i < gf.n_auth; i++) 1463 if (gf.auth[i].node == (uint32_t)m) { 1464 na++; 1465 exact &= gf.auth[i].resource == RES_A && gf.auth[i].rights == RX_RIGHT_READ; 1466 } 1467 CHECK(m >= 0 && na == 1 && exact, "fused node needs READ on A and nothing else"); 1468 R.n3_meta_auth_exact += m >= 0 && na == 1 && exact; 1469 static AgLowered lo, lf; 1470 CHECK(lower(&o, &lo, &go, &g_skills) == 0 && lower(&f, &lf, &gf, &g_pub) == 0, "lower"); 1471 AgFusionSite site; 1472 CHECK(rx_fusion_find(&go, &o.w, MC.reference.signature, &site, 1) == 1, "site"); 1473 AgResult ro, rf; 1474 for (uint64_t r = 1; r <= 24; r++) { 1475 uint64_t a = a_value(r + 40); 1476 set_inputs(&o, a, r); 1477 set_inputs(&f, a, r); 1478 run(&o, &lo, r, &ro, NULL); 1479 CHECK(rx_graph_reference(&gf, &f.w, &g_pub, &f.caps, r, &g_ref) == 0, "reference"); 1480 int64_t ext = run(&f, &lf, r, &rf, NULL); 1481 compare(&gf, &rf, &g_ref, "published"); 1482 ancestry(&f, &lf, ext, "published"); 1483 R.cross_runs++; 1484 int div = ro.status[site.exit] != rf.status[m] || ro.value[site.exit] != rf.value[m] || 1485 ro.outcome != rf.outcome; 1486 for (uint32_t k = 0; k < RX_MAX_FIELDS; k++) div |= fld(&o, o.out, k) != fld(&f, f.out, k); 1487 for (uint32_t k = 0; k < 4; k++) div |= fld(&o, o.effect, k) != fld(&f, f.effect, k); 1488 R.steps_checked++; 1489 if (rx_fusion_site_steps(&go, &site, &MC.reference, &ro) != rf.steps[m]) { 1490 R.steps_mismatch++; 1491 div = 1; 1492 } 1493 R.cross_mismatch += (uint32_t)div; 1494 CHECK(!div, "goal %u run %llu: fused differs from original", goal, (unsigned long long)r); 1495 } 1496 env_stop(&o); 1497 env_stop(&f); 1498 } 1499 /* Procedures without the workflow are left as they were. */ 1500 CHECK(env_start(&o, 1) == 0, "env"); 1501 for (uint32_t goal = GOAL_UNSTABLE; goal <= N_GOALS; goal++) { 1502 static AgGraph go, gf; 1503 AgReport rep; 1504 AgFusionReport frep; 1505 CHECK(compile(&o, goal, &go) == AG_OK_READY && 1506 compile_fused(&o, goal, &lib, &g_pub, &gf, &rep, &frep) == AG_OK_READY, "compile"); 1507 CHECK(frep.applied == 0 && memcmp(go.digest, gf.digest, 32) == 0, "goal %u untouched", goal); 1508 } 1509 env_stop(&o); 1510 1511 /* Deterministic across worker counts. */ 1512 uint8_t first[32]; 1513 int have = 0; 1514 for (uint32_t wc = 1; wc <= 8; wc *= 2) { 1515 CHECK(env_start(&f, wc) == 0, "env"); 1516 static AgGraph gf; 1517 AgReport rep; 1518 AgFusionReport frep; 1519 CHECK(compile_fused(&f, GOAL_EFFECT, &lib, &g_pub, &gf, &rep, &frep) == AG_OK_READY, "compile"); 1520 static AgLowered lf; 1521 CHECK(lower(&f, &lf, &gf, &g_pub) == 0, "lower"); 1522 AgResult rf; 1523 for (uint64_t r = 1; r <= 4; r++) { 1524 set_inputs(&f, a_value(r), r); 1525 run(&f, &lf, r, &rf, NULL); 1526 } 1527 uint8_t d[32]; 1528 semantic_digest(&gf, &rf, &f, d); 1529 R.det_runs++; 1530 if (!have) { memcpy(first, d, 32); have = 1; } 1531 else if (memcmp(first, d, 32) != 0) R.det_mismatch++; 1532 env_stop(&f); 1533 } 1534 CHECK(R.det_mismatch == 0, "fused result identical at 1, 2, 4 and 8 workers"); 1535 } 1536 1537 /* ---- authority removed (N3) ---- */ 1538 1539 static void t_revoked(void) { 1540 printf("[*] authority removed: original and fused both block, and record it\n"); 1541 if (MC.state != AG_MS_PUBLISHED) return; 1542 AgFusionLibrary lib = { 1, { &MC } }; 1543 static Env o, f; 1544 CHECK(env_start(&o, 4) == 0 && env_start(&f, 4) == 0, "envs"); 1545 static AgGraph go, gf; 1546 AgReport rep; 1547 AgFusionReport frep; 1548 CHECK(compile(&o, GOAL_EFFECT, &go) == AG_OK_READY, "compile"); 1549 CHECK(compile_fused(&f, GOAL_EFFECT, &lib, &g_pub, &gf, &rep, &frep) == AG_OK_READY && frep.applied == 1, 1550 "compile fused"); 1551 static AgLowered lo, lf; 1552 CHECK(lower(&o, &lo, &go, &g_skills) == 0 && lower(&f, &lf, &gf, &g_pub) == 0, "lower"); 1553 AienosCapRef office; 1554 aienos_cap_office(o.admin, &office); 1555 CHECK(aienos_cap_revoke(o.admin, office, (AienosCapRef){ o.read_A.cap_id, o.read_A.generation }) == 0, "revoke"); 1556 aienos_cap_office(f.admin, &office); 1557 CHECK(aienos_cap_revoke(f.admin, office, (AienosCapRef){ f.read_A.cap_id, f.read_A.generation }) == 0, "revoke"); 1558 AgResult ro, rf; 1559 run(&o, &lo, 1, &ro, NULL); 1560 run(&f, &lf, 1, &rf, NULL); 1561 int rd = -1; 1562 for (uint32_t n = 0; n < go.n_nodes; n++) 1563 if (go.nodes[n].kind == AG_WORLD_READ && go.nodes[n].obj.id == o.A.id) rd = (int)n; 1564 int m = meta_node(&gf, MS_COMPILED); 1565 R.n3_blocked_original = rd >= 0 ? count_crumbs(&o, lo.reaction[rd], RX_CRUMB_BLOCKED_AUTHORITY) : 0; 1566 R.n3_blocked_fused = m >= 0 ? count_crumbs(&f, lf.reaction[m], RX_CRUMB_BLOCKED_AUTHORITY) : 0; 1567 CHECK(R.n3_blocked_original >= 1 && R.n3_blocked_fused >= 1, "both blocked for authority (%u, %u)", 1568 R.n3_blocked_original, R.n3_blocked_fused); 1569 CHECK(ro.outcome == AG_RUN_INCOMPLETE && rf.outcome == AG_RUN_INCOMPLETE && 1570 fld(&o, o.effect, 0) == 0 && fld(&f, f.effect, 0) == 0, "no effect on either side"); 1571 env_stop(&o); 1572 env_stop(&f); 1573 1574 /* Compiling for a principal without READ on A reports it missing on 1575 * the fused node, as it would on the read. */ 1576 CHECK(env_start(&f, 1) == 0, "env"); 1577 for (uint32_t i = 0; i < f.caps.n; i++) 1578 if (f.caps.cap[i].resource == RES_A) f.caps.cap[i].resource = RES_A + 0x1000; 1579 CHECK(compile_fused(&f, GOAL_EFFECT, &lib, &g_pub, &gf, &rep, &frep) == AG_OK_READY && frep.applied == 1, 1580 "compile fused without READ on A"); 1581 m = meta_node(&gf, MS_COMPILED); 1582 int missing = 0; 1583 for (uint32_t i = 0; i < rep.n_missing; i++) 1584 missing |= rep.missing[i].node == (uint32_t)m && rep.missing[i].resource == RES_A; 1585 CHECK(missing && rep.n_missing == 1, "missing authority reported on the fused node"); 1586 env_stop(&f); 1587 } 1588 1589 /* ---- placement and receipt ---- */ 1590 1591 /* The Cortex-X925 cores of the affinity mask the test started with: a taskset 1592 * or cpuset run is timed only on the cores it was given (as #1002 did for the 1593 * empirical optimizer). With no X925 core in the mask it stays unpinned. */ 1594 static void place(void) { 1595 cpu_set_t set, allowed; 1596 CPU_ZERO(&set); 1597 CPU_ZERO(&allowed); 1598 if (sched_getaffinity(0, sizeof allowed, &allowed) != 0) 1599 for (int c = 0; c < CPU_SETSIZE; c++) CPU_SET(c, &allowed); 1600 int count = 0; 1601 char *p = R.cpus; 1602 long n = sysconf(_SC_NPROCESSORS_CONF); 1603 for (long c = 0; c < n && c < CPU_SETSIZE; c++) { 1604 if (!CPU_ISSET((int)c, &allowed)) continue; /* taskset/cgroup: not ours to time on */ 1605 char path[128]; 1606 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c); 1607 FILE *fp = fopen(path, "r"); 1608 if (!fp) continue; 1609 unsigned long long midr = 0; 1610 int ok = fscanf(fp, "%llx", &midr) == 1; 1611 fclose(fp); 1612 if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) { 1613 CPU_SET(c, &set); 1614 count++; 1615 if (p < R.cpus + sizeof R.cpus - 5) p += sprintf(p, "%s%ld", count > 1 ? "," : "", c); 1616 } 1617 } 1618 if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) { 1619 snprintf(R.cpus, sizeof R.cpus, "unpinned"); 1620 return; 1621 } 1622 printf("[*] placed on %d Cortex-X925 cores: %s\n", count, R.cpus); 1623 } 1624 1625 static void binary_digest(char out[65]) { 1626 strcpy(out, "unavailable"); 1627 FILE *fp = fopen("/proc/self/exe", "rb"); 1628 if (!fp) return; 1629 sha256_ctx c; 1630 sha256_init(&c); 1631 uint8_t buf[65536]; 1632 size_t n; 1633 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); 1634 fclose(fp); 1635 uint8_t d[32]; 1636 sha256_final(&c, d); 1637 hex(d, out); 1638 } 1639 1640 static void measure_json(FILE *fp, const char *name, const AgFusionMeasure *m, const Quiet *q, int last) { 1641 fprintf(fp, 1642 " \"%s\": {\"runs_per_leg\": %u, \"reactions_computing_per_run_before\": %.2f, " 1643 "\"reactions_computing_per_run_after\": %.2f,\n" 1644 " \"cognitive_operations_avoided_per_run\": %.2f, \"crumbs_per_run_before\": %.2f, " 1645 "\"crumbs_per_run_after\": %.2f,\n" 1646 " \"median_run_ns_before\": %llu, \"median_run_ns_after\": %llu, " 1647 "\"original_spread_permille\": %u, \"latency_tolerance_permille\": %u, " 1648 "\"cpu_ns_per_run_before\": %llu, \"cpu_ns_per_run_after\": %llu,\n" 1649 " \"reactions_registered_before\": %u, \"reactions_registered_after\": %u, " 1650 "\"world_objects_before\": %u, \"world_objects_after\": %u,\n" 1651 " \"success_before\": %u, \"failure_before\": %u, \"success_after\": %u, \"failure_after\": %u", 1652 name, m->runs, m->commits_before, m->commits_after, m->commits_before - m->commits_after, 1653 m->crumbs_before, m->crumbs_after, (unsigned long long)m->median_ns_before, 1654 (unsigned long long)m->median_ns_after, m->spread_permille_before, m->tolerance_permille, 1655 (unsigned long long)m->cpu_ns_before, 1656 (unsigned long long)m->cpu_ns_after, m->reactions_before, m->reactions_after, 1657 m->objects_before, m->objects_after, m->ok_before, m->failed_before, m->ok_after, 1658 m->failed_after); 1659 fprintf(fp, 1660 ",\n \"disturbance\": {\"run_queue_delay_known\": %s, \"attempts\": %u, \"undisturbed_pairs\": %u, " 1661 "\"only_original_disturbed\": %u, \"only_fused_disturbed\": %u, \"claimed_faster_leg\": \"%s\", ", 1662 q->known ? "true" : "false", q->tried, q->kept, q->legs.only[0], q->legs.only[1], 1663 q->fast ? "fused" : "original"); 1664 if (q->skew) 1665 fprintf(fp, "\"skew_test\": \"applied\", \"skew_p_one_sided\": %.6g, \"skew_alpha\": %g}", q->p, 1666 RX_SKEW_ALPHA); 1667 else 1668 fprintf(fp, "\"skew_test\": \"not_applied_legs_differ_in_parallelism\"}"); 1669 if (m->energy_available) { 1670 double dp = fabs(m->energy_uj_before - m->energy_uj_after), dc = fabs(cpu_mean(0) - cpu_mean(1)); 1671 fprintf(fp, 1672 ",\n \"energy\": {\"source\": \"aien_spbm accumulators (whole package / whole performance-core cluster, " 1673 "not per process); idle power around each leg subtracted; two rounds\",\n" 1674 " \"package_uj_per_run_before\": %.2f, \"package_uj_per_run_after\": %.2f, " 1675 "\"package_round_spread_uj\": %.2f, \"package_difference_resolved\": %s,\n" 1676 " \"performance_cores_uj_per_run_before\": %.2f, \"performance_cores_uj_per_run_after\": %.2f, " 1677 "\"performance_cores_round_spread_uj\": %.2f, \"performance_cores_difference_resolved\": %s}", 1678 m->energy_uj_before, m->energy_uj_after, m->energy_noise_uj, 1679 dp > 2 * m->energy_noise_uj ? "true" : "false", cpu_mean(0), cpu_mean(1), cpu_noise(), 1680 dc > 2 * cpu_noise() ? "true" : "false"); 1681 } 1682 else 1683 fprintf(fp, ",\n \"energy\": \"unavailable\""); 1684 fprintf(fp, "}%s\n", last ? "" : ","); 1685 } 1686 1687 /* Everything judged held. A refused measurement takes out only the states 1688 * that need it: the compiled chain (canary, promotion, publication, and the 1689 * poisoned hybrid's canary, which runs after it), the parallel refusal, the 1690 * poisoned quarantine. */ 1691 static int judged_pass(void) { 1692 return g_fail == 0 && R.bypass == 0 && R.evidence_missing == 0 && R.ancestry_mismatch == 0 && 1693 R.eq_mismatch == 0 && R.cross_mismatch == 0 && R.steps_mismatch == 0 && R.det_mismatch == 0 && 1694 R.tamper_caught == R.tamper_cases && (g_untimed_compiled || MC.state == AG_MS_PUBLISHED) && 1695 (g_untimed_parallel || R.parallel_state == AG_MS_SLOWER) && 1696 (g_untimed_compiled || g_untimed_poisoned || R.poisoned_state == AG_MS_QUARANTINED); 1697 } 1698 1699 static void write_receipt(void) { 1700 char path[512]; 1701 if (omega_evidence_path("WORKFLOW_FUSION/rx_workflow_fusion_receipt.json", path, sizeof path) != 0) return; 1702 FILE *fp = omega_evidence_create(path); 1703 if (!fp) return; 1704 char commit[41]; 1705 memset(commit, 0, sizeof commit); 1706 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); 1707 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); 1708 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && !omega_evidence_tree_dirty(); 1709 const char *aienos = getenv("AIENOS_COMMIT"); 1710 char digest[65]; 1711 binary_digest(digest); 1712 struct utsname u; 1713 memset(&u, 0, sizeof u); 1714 uname(&u); 1715 static const char *kinds[4] = { "compiled", "hybrid", "learned", "hardware" }; 1716 fprintf(fp, 1717 "{\n" 1718 " \"schema\": \"OMEGA_WORKFLOW_FUSION_V1\",\n" 1719 " \"run_id\": \"%s\",\n" 1720 " \"candidate_commit\": %s%s%s,\n" 1721 " \"candidate_bound\": %s,\n" 1722 " \"run_commit\": \"%s\",\n" 1723 " \"tree_dirty\": %s,\n" 1724 " \"aienos_commit\": %s%s%s,\n" 1725 " \"checks\": %d,\n" 1726 " \"failures\": %d,\n" 1727 " \"test_binary_sha256\": \"%s\",\n" 1728 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"cpus\": \"%s\"},\n" 1729 " \"scope\": \"host processor; native AIENOS authority library; resident reaction world; R9 generation store\",\n" 1730 " \"observation\": {\"verified_runs\": %u, \"refused_runs\": %u, \"patterns\": %u, \"fragments\": %u,\n" 1731 " \"fragments_over_reaction_budget\": %u, \"patterns_dropped\": %u,\n" 1732 " \"workflow\": {\"signature\": \"%s\", \"steps\": %u, \"occurrences\": %u, \"graphs\": %u,\n" 1733 " \"ok\": %u, \"failed\": %u, \"repeat_inputs_stable\": %u, \"samples\": %u},\n" 1734 " \"candidates\": %u, \"refused_unstable\": %u, \"refused_failure_rate\": %u,\n" 1735 " \"refused_one_graph\": %u},\n" 1736 " \"verification\": {\n", 1737 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null", 1738 candidate ? "\"" : "", bound ? "true" : "false", commit, 1739 omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", aienos ? aienos : "null", 1740 aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname, u.release, u.machine, R.cpus, 1741 R.obs_runs, R.obs_refused, R.obs_patterns, R.obs_fragments, R.obs_over_budget, R.obs_dropped, 1742 R.w_signature, R.w_steps, R.w_occ, R.w_graphs, R.w_ok, R.w_failed, R.w_stable, R.w_samples, 1743 R.n_candidates, R.judged_unstable, R.judged_failure_rate, R.judged_few_graphs); 1744 for (int i = 0; i < 4; i++) 1745 fprintf(fp, 1746 " \"%s\": {\"state\": \"%s\", \"reason\": \"%s\", \"vectors\": %u, \"observed\": %u, " 1747 "\"generated\": %u, \"port_status_combinations\": %u, \"learned_hits\": %u},\n", 1748 kinds[i], rx_fusion_state_name(R.ver[i].state), rx_fusion_reason_name(R.ver[i].reason), 1749 R.ver[i].vectors, R.ver[i].observed, R.ver[i].generated, R.ver[i].combos, R.ver[i].hits); 1750 fprintf(fp, 1751 " \"tampering\": {\"cases\": %u, \"caught\": %u, \"detail\": [%s]},\n" 1752 " \"coherent_rewrite_sites_in_real_workflow\": %u\n" 1753 " },\n" 1754 " \"never_silent\": {\"unpublished_states_refused\": %u, \"graph_unchanged\": %u,\n" 1755 " \"refused_in_library_after_publication\": %u},\n" 1756 " \"measurement\": {\n", 1757 R.tamper_cases, R.tamper_caught, R.tamper_names, R.coherent_rewrite_sites, R.unpublished_refusals, 1758 R.unpublished_digest_equal, R.refused_after_publish); 1759 measure_json(fp, "compiled", &R.m_compiled, &R.q_compiled, 0); 1760 measure_json(fp, "hybrid", &R.m_hybrid, &R.q_hybrid, 0); 1761 measure_json(fp, "parallel_fragment", &R.m_parallel, &R.q_parallel, 1); 1762 fprintf(fp, 1763 " },\n" 1764 " \"promotion\": {\"parallel_fragment\": \"%s\", \"hybrid\": \"%s\",\n" 1765 " \"canary_runs\": %u, \"canary_divergences\": %u,\n" 1766 " \"poisoned_hybrid_verified\": %s, \"poisoned_hybrid\": \"%s\", \"poisoned_canary_runs_until_quarantine\": %u,\n" 1767 " \"poisoned_promotion_refused\": %s,\n" 1768 " \"refused_without_right\": %s, \"refused_for_proposer\": %s,\n" 1769 " \"publication_refused_after_change\": %s, \"generation\": %llu, \"compiled\": \"%s\"},\n" 1770 " \"use\": {\"graphs_fused\": %u, \"nodes_before\": %u, \"nodes_after\": %u,\n" 1771 " \"runs_compared_with_reference\": %u, \"node_results_compared\": %u, \"reference_mismatches\": %u,\n" 1772 " \"runs_compared_with_original\": %u, \"original_mismatches\": %u,\n" 1773 " \"step_evidence_checked\": %u, \"step_evidence_mismatches\": %u,\n" 1774 " \"evidence_checked\": %u, \"evidence_missing\": %u,\n" 1775 " \"ancestry_nodes\": %u, \"ancestry_mismatches\": %u,\n" 1776 " \"determinism_worker_counts\": [1, 2, 4, 8], \"determinism_mismatches\": %u,\n" 1777 " \"bypasses\": %u},\n" 1778 " \"authority_removed\": {\"original_blocked_crumbs\": %u, \"fused_blocked_crumbs\": %u,\n" 1779 " \"fused_authority_exactly_steps\": %u},\n" 1780 " \"gates\": {\n" 1781 " \"OMEGA_WORKFLOW_FUSION_PASS\": \"%s\",\n" 1782 " \"not_run_reason\": \"%s\",\n" 1783 " \"not_claimed\": [\"a hardware realization (the kind is in the contract; none is built)\", " 1784 "\"a neural learned module (the learned realization is a table of verified observations)\", " 1785 "\"machine code (the compiled realization is a typed step program run in one reaction)\", " 1786 "\"fusing effect boundaries, branches or joins\", " 1787 "\"observation as resident reactions (observe and propose are host calls)\", " 1788 "\"energy saved per run beyond the meter's noise\", " 1789 "\"AIENOS kernel (the authority runs as a host library)\"]\n" 1790 " }\n" 1791 "}\n", 1792 rx_fusion_state_name(R.parallel_state), rx_fusion_state_name(R.hybrid_state), R.canary_runs, 1793 R.canary_divergences, R.poisoned_verified ? "true" : "false", rx_fusion_state_name(R.poisoned_state), 1794 R.poisoned_canary_runs, R.poisoned_promote_refused ? "true" : "false", R.promote_refused_no_right ? "true" : "false", 1795 R.promote_refused_same_subject ? "true" : "false", R.publish_refused_changed ? "true" : "false", 1796 (unsigned long long)R.generation, rx_fusion_state_name(MC.state), R.fused_graphs, 1797 R.fused_nodes_before, R.fused_nodes_after, R.eq_runs, R.eq_nodes, R.eq_mismatch, R.cross_runs, 1798 R.cross_mismatch, R.steps_checked, R.steps_mismatch, R.evidence_checked, R.evidence_missing, 1799 R.ancestry_nodes, R.ancestry_mismatch, R.det_mismatch, R.bypass, R.n3_blocked_original, 1800 R.n3_blocked_fused, R.n3_meta_auth_exact, rx_busy_verdict(judged_pass()), 1801 *rx_busy_refusals() ? rx_busy_reason() : "none"); 1802 fclose(fp); 1803 printf("receipt: %s\n", path); 1804 } 1805 1806 int main(void) { 1807 signal(SIGPIPE, SIG_IGN); 1808 place(); 1809 skills_init(); 1810 build_templates(); 1811 t_observe(); 1812 t_verify(); 1813 t_skew_math(); 1814 t_measure(); 1815 t_canary(); 1816 t_promote(); 1817 t_use(); 1818 t_revoked(); 1819 printf(" compared %u runs (%u node results) with the reference, %u with the original " 1820 "(%u mismatches); step evidence %u checked, %u mismatches; ancestry %u nodes, %u mismatches; " 1821 "bypasses %u\n", R.eq_runs, R.eq_nodes, R.cross_runs, R.cross_mismatch, R.steps_checked, 1822 R.steps_mismatch, R.ancestry_nodes, R.ancestry_mismatch, R.bypass); 1823 printf("checks %d failures %d\n", g_checks, g_fail); 1824 write_receipt(); 1825 return rx_busy_exit(g_fail, judged_pass()); 1826 }