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_r15_rig.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 | /* rx_r15_rig.c -- see rx_r15_rig.h. The body is built exactly as |
| 18 | * tests/runtime/rx_r13_living.c builds its POSITIVE case. */ |
| 19 | #include "rx_r15_rig.h" |
| 20 | #include "omega_evidence.h" |
| 21 | |
| 22 | #include <dirent.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | enum { EXTERNAL = 100, ISSUER = 3 }; |
| 30 | #define RES_INTENT 0x6130010ull |
| 31 | #ifndef TARGET_PCT |
| 32 | #define TARGET_PCT 55u |
| 33 | #endif |
| 34 | |
| 35 | static uint64_t now_ns(void) { |
| 36 | struct timespec ts; |
| 37 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 38 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 39 | } |
| 40 | |
| 41 | /* Busy wait without a system call: a 20 us sleep costs about 80 us. */ |
| 42 | static void spin_us(unsigned us) { |
| 43 | uint64_t until = now_ns() + (uint64_t)us * 1000u; |
| 44 | while (now_ns() < until) { |
| 45 | #if defined(__aarch64__) |
| 46 | __asm__ volatile("yield"); |
| 47 | #endif |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void pause_us(int us) { |
| 52 | struct timespec ts = {0, (long)us * 1000L}; |
| 53 | nanosleep(&ts, NULL); |
| 54 | } |
| 55 | |
| 56 | static RxAienConfig g_aien_cfg; |
| 57 | static int g_aien_cfg_set; |
| 58 | |
| 59 | void r15_set_aien_config(const RxAienConfig *cfg) { |
| 60 | g_aien_cfg_set = cfg != NULL; |
| 61 | if (cfg) g_aien_cfg = *cfg; |
| 62 | } |
| 63 | |
| 64 | const char *r15_config_name(R15Config c) { |
| 65 | return c == R15_RES4 ? "RES-4" : c == R15_RES1 ? "RES-1" : "SEQ"; |
| 66 | } |
| 67 | |
| 68 | static RxCapRef mint(R15Rig *r, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 69 | AienosCapRef office, ref = {UINT32_MAX, 0}; |
| 70 | aienos_cap_office(r->admin, &office); |
| 71 | AienosCapMint m = {ISSUER, subject, resource, rights, 0, {UINT32_MAX, 0}, office}; |
| 72 | if (aienos_cap_mint(r->admin, &m, &ref) == 0) r->minted++; |
| 73 | return (RxCapRef){ref.cap_id, ref.generation}; |
| 74 | } |
| 75 | |
| 76 | static uint64_t field(R15Rig *r, RxObjRef ref, uint32_t i) { |
| 77 | RxObject o; |
| 78 | return rx_world_read(&r->w, ref, &o) == RX_OK ? o.field[i] : UINT64_MAX; |
| 79 | } |
| 80 | |
| 81 | /* Fixed matvec cost per call in ns, by core class and realization kind |
| 82 | * (0..3, then the reference; any other kind is priced as the reference): |
| 83 | * Omega's own measure of each kind on this GB10, unloaded, 2026-10-19. On |
| 84 | * A725 quad4 is inside the 10% margin, so the first search keeps the |
| 85 | * reference; on X925 quad4 wins and its cost is under 55% of the A725 |
| 86 | * incumbent, so the goal is MET. */ |
| 87 | static const uint64_t k_model_ns[2][5] = { |
| 88 | {13300, 11100, 11300, 6060, 6200}, /* A725 */ |
| 89 | {4900, 4400, 4900, 2700, 4600}, /* X925 */ |
| 90 | }; |
| 91 | |
| 92 | static int g_cost_model; |
| 93 | |
| 94 | void r15_use_cost_model(int on) { g_cost_model = on; } |
| 95 | |
| 96 | /* X925 costs apply once the episode's goal is in the world. The goal is |
| 97 | * published after the X925 placement, so the cost change is tied to a world |
| 98 | * event, not to when the episode's thread gets to run: a thread paused |
| 99 | * between steps cannot open a window of X925 costs under the A725 |
| 100 | * placement, or of A725 costs in the X925 search. The goal is read live, so |
| 101 | * the one demand window spanning the goal mixes both costs; it can only add |
| 102 | * a miss to the A725 prediction, which the X925 costs fail anyway. A failed |
| 103 | * read counts as X925. */ |
| 104 | static uint64_t model_cost(void *ctx, uint32_t kind, uint32_t M, uint32_t N, uint64_t calls) { |
| 105 | R15Rig *r = ctx; |
| 106 | (void)M; (void)N; /* the rig serves one shape, R15_M x R15_N */ |
| 107 | uint32_t cls = field(r, r->aien.o.goal, 0) != 0; |
| 108 | return calls * k_model_ns[cls][kind < 4 ? kind : 4]; |
| 109 | } |
| 110 | |
| 111 | static const RxSnapshotDep *snapshot(RxCtx *c, RxObjRef ref) { |
| 112 | for (uint32_t i = 0; i < c->n_in; i++) |
| 113 | if (c->in[i].obj.id == ref.id) return &c->in[i]; |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | /* Client intent becomes an R8 request through a reaction of that client. */ |
| 118 | static int ask(RxCtx *c) { |
| 119 | R15Rig *r = c->user; |
| 120 | const RxSnapshotDep *in = snapshot(c, r->intent); |
| 121 | const RxSnapshotDep *out = snapshot(c, r->aegis.o[0].request); |
| 122 | if (!in || !out) return -1; |
| 123 | if (!in->field[0] || in->field[0] == out->field[0]) return 0; |
| 124 | for (uint32_t i = 0; i < 6; i++) |
| 125 | c->out[c->n_out++] = (RxMutation){r->aegis.o[0].request, i, in->field[i]}; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int core_sets(cpu_set_t *a, cpu_set_t *x) { |
| 130 | CPU_ZERO(a); CPU_ZERO(x); |
| 131 | DIR *d = opendir("/sys/devices/system/cpu"); |
| 132 | if (!d) return -1; |
| 133 | struct dirent *e; |
| 134 | int na = 0, nx = 0; |
| 135 | while ((e = readdir(d))) { |
| 136 | int cpu = -1; |
| 137 | if (sscanf(e->d_name, "cpu%d", &cpu) != 1 || cpu < 0 || cpu >= CPU_SETSIZE) continue; |
| 138 | char path[256]; |
| 139 | snprintf(path, sizeof path, |
| 140 | "/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", cpu); |
| 141 | FILE *f = fopen(path, "r"); |
| 142 | unsigned long long midr = 0; |
| 143 | if (!f) continue; |
| 144 | int ok = fscanf(f, "%llx", &midr); |
| 145 | fclose(f); |
| 146 | if (ok != 1) continue; |
| 147 | uint32_t part = (uint32_t)(midr >> 4) & 0xfffu; |
| 148 | if (part == R15_CLASS_A725) { CPU_SET(cpu, a); na++; } |
| 149 | if (part == R15_CLASS_X925) { CPU_SET(cpu, x); nx++; } |
| 150 | } |
| 151 | closedir(d); |
| 152 | return na && nx ? 0 : -1; |
| 153 | } |
| 154 | |
| 155 | static int move_threads(const cpu_set_t *set) { |
| 156 | DIR *d = opendir("/proc/self/task"); |
| 157 | if (!d) return -1; |
| 158 | struct dirent *e; |
| 159 | int moved = 0; |
| 160 | while ((e = readdir(d))) { |
| 161 | int tid = atoi(e->d_name); |
| 162 | if (tid > 0 && sched_setaffinity(tid, sizeof(*set), set) == 0) moved++; |
| 163 | } |
| 164 | closedir(d); |
| 165 | return moved; |
| 166 | } |
| 167 | |
| 168 | static int new_object(R15Rig *r, uint32_t type, uint64_t resource, RxObjRef *out) { |
| 169 | uint64_t z[RX_MAX_FIELDS] = {0}; |
| 170 | return rx_world_create(&r->w, type, RX_PERSIST_RESIDENT, resource, z, out); |
| 171 | } |
| 172 | |
| 173 | /* ---- completion transport (both orchestrations) ------------------------- */ |
| 174 | |
| 175 | /* Ring transport only: move seat completions into the world. */ |
| 176 | static int progress(R15Rig *r) { |
| 177 | #ifdef R15_SILICON |
| 178 | int rc = rx_resident_accept(&r->w); |
| 179 | return rc == RX_ERR_NOT_FOUND || rc == RX_OK ? 0 : rc; |
| 180 | #else |
| 181 | int rc = rx_resident_seat_step(&r->w); |
| 182 | if (rc == 1) return rx_resident_accept(&r->w); |
| 183 | return rc < 0 ? rc : 0; |
| 184 | #endif |
| 185 | } |
| 186 | |
| 187 | /* RES: sleeps while no claim is outstanding (no polling nap that would |
| 188 | * distort GPU latency), then moves completions as they arrive. */ |
| 189 | static void *transport_main(void *arg) { |
| 190 | R15Rig *r = arg; |
| 191 | while (!atomic_load(&r->transport_stop)) { |
| 192 | if (rx_resident_wait_outstanding(&r->w, 50) != RX_OK) continue; |
| 193 | int rc = progress(r); |
| 194 | if (rc < 0) atomic_store(&r->transport_error, rc); |
| 195 | else sched_yield(); |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | /* SEQ: the orchestrator's "dispatch GPU, wait" step. */ |
| 201 | static int seq_gpu_step(void *ctx) { |
| 202 | return progress((R15Rig *)ctx); |
| 203 | } |
| 204 | |
| 205 | /* ---- SEQ orchestrator -------------------------------------------------- */ |
| 206 | |
| 207 | /* spec §2.2, in order; omega.{synthesize,verify,measure}.k interleaved per k. */ |
| 208 | static const char *const k_order[] = { |
| 209 | "workload.matvec.serve", "living.experiment.ask", "aegis.decide", "root.install", |
| 210 | "aien.observe", "aien.predict", "aien.explain", "aien.assess", "aien.plan", |
| 211 | "omega.watch", "omega.reconsider", "omega.k", "omega.select", |
| 212 | "living.experiment.prepare", "living.blackwell.add", "living.experiment.evidence", |
| 213 | "aien.experiment.observe", "generation.prepare", "generation.promote", |
| 214 | "generation.restore", |
| 215 | }; |
| 216 | |
| 217 | static int omega_slot_rank(const char *name, uint32_t *rank) { |
| 218 | static const char *const stage[] = {"omega.synthesize.", "omega.verify.", "omega.measure."}; |
| 219 | for (uint32_t s = 0; s < 3; s++) { |
| 220 | size_t n = strlen(stage[s]); |
| 221 | if (strncmp(name, stage[s], n) == 0) { |
| 222 | *rank = (uint32_t)atoi(name + n) * 3u + s; |
| 223 | return 1; |
| 224 | } |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* Caller holds w->mu. Every registered reaction appears exactly once: |
| 230 | * listed ones in §2.2 order, then any others in registration order. */ |
| 231 | static void build_plan(R15Rig *r) { |
| 232 | uint32_t n = 0, nr = r->w.n_reactions; |
| 233 | uint8_t used[RX_MAX_REACTIONS] = {0}; |
| 234 | for (uint32_t o = 0; o < sizeof k_order / sizeof k_order[0]; o++) { |
| 235 | if (strcmp(k_order[o], "omega.k") == 0) { |
| 236 | for (uint32_t rank = 0; rank < 3u * RX_OMEGA_SLOTS; rank++) |
| 237 | for (uint32_t i = 0; i < nr; i++) { |
| 238 | uint32_t got; |
| 239 | if (!used[i] && r->w.reactions[i].desc.name && |
| 240 | omega_slot_rank(r->w.reactions[i].desc.name, &got) && got == rank) { |
| 241 | r->order[n++] = i; |
| 242 | used[i] = 1; |
| 243 | } |
| 244 | } |
| 245 | continue; |
| 246 | } |
| 247 | for (uint32_t i = 0; i < nr; i++) |
| 248 | if (!used[i] && r->w.reactions[i].desc.name && |
| 249 | strcmp(r->w.reactions[i].desc.name, k_order[o]) == 0) { |
| 250 | r->order[n++] = i; |
| 251 | used[i] = 1; |
| 252 | } |
| 253 | } |
| 254 | for (uint32_t i = 0; i < nr; i++) |
| 255 | if (!used[i]) r->order[n++] = i; |
| 256 | r->plan.order = r->order; |
| 257 | r->plan.n = n; |
| 258 | r->plan.gpu_step = seq_gpu_step; |
| 259 | r->plan.gpu_ctx = r; |
| 260 | r->plan.gpu_timeout_ms = 5000; |
| 261 | r->plan_for = nr; |
| 262 | } |
| 263 | |
| 264 | /* The legacy loop: pulse forever; when a whole pulse runs nothing, poll |
| 265 | * again without sleeping (spec §2.1). */ |
| 266 | static void *orchestrator_main(void *arg) { |
| 267 | R15Rig *r = arg; |
| 268 | while (!atomic_load(&r->orch_stop)) { |
| 269 | pthread_mutex_lock(&r->w.mu); |
| 270 | if (r->plan_for != r->w.n_reactions) build_plan(r); |
| 271 | pthread_mutex_unlock(&r->w.mu); |
| 272 | uint32_t ran = 0; |
| 273 | int rc = rx_seq_pulse(&r->w, &r->plan, &ran); |
| 274 | if (rc != RX_OK) { atomic_store(&r->orch_error, rc); break; } |
| 275 | if (ran == 0) sched_yield(); |
| 276 | } |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | /* ---- build ------------------------------------------------------------- */ |
| 281 | |
| 282 | static void take_baseline(R15Rig *r) { |
| 283 | pthread_mutex_lock(&r->w.mu); |
| 284 | for (uint32_t i = 0; i < r->w.n_reactions; i++) { |
| 285 | const RxReactionDesc *d = &r->w.reactions[i].desc; |
| 286 | for (uint32_t t = 0; t < d->n_triggers; t++) { |
| 287 | const RxObject *o = &r->w.objects[d->triggers[t].obj.id]; |
| 288 | uint64_t v = 0; |
| 289 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) |
| 290 | if ((d->triggers[t].mask & RX_FIELD(f)) && o->field_version[f] > v) |
| 291 | v = o->field_version[f]; |
| 292 | r->baseline[i][t] = v; |
| 293 | } |
| 294 | } |
| 295 | pthread_mutex_unlock(&r->w.mu); |
| 296 | } |
| 297 | |
| 298 | /* R16 C5: enroll one subject into a keyring (and, for a subject two |
| 299 | * components act as, copy the same credential into a second one). */ |
| 300 | static int enroll(R15Rig *r, RxCallerKeyring *k, uint32_t subject) { |
| 301 | if (k->n >= RX_CALLER_KEYRING_MAX) return -1; |
| 302 | if (rx_world_enroll_caller(&r->w, subject, &k->cred[k->n]) != RX_CALLER_OK) return -1; |
| 303 | k->subject[k->n++] = subject; |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int share(RxCallerKeyring *to, const RxCallerKeyring *from, uint32_t subject) { |
| 308 | const RxCallerCred *c = rx_caller_find(from, subject); |
| 309 | if (!c || to->n >= RX_CALLER_KEYRING_MAX) return -1; |
| 310 | to->subject[to->n] = subject; |
| 311 | to->cred[to->n++] = *c; |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int enroll_callers(R15Rig *r, const uint32_t *fixtures, uint32_t n_fixtures) { |
| 316 | if (n_fixtures > RX_CALLER_KEYRING_MAX) return -1; |
| 317 | if (enroll(r, &r->keys_omega, RX_OMEGA_SUBJ_SERVE) || enroll(r, &r->keys_omega, RX_OMEGA_SUBJ_OMEGA) || |
| 318 | enroll(r, &r->keys_aien, RX_AIEN_SUBJ) || |
| 319 | enroll(r, &r->keys_aegis, RX_AEGIS_SUBJ) || enroll(r, &r->keys_aegis, RX_AEGIS_ROOT_SUBJ) || |
| 320 | enroll(r, &r->keys_living, RX_LIVING_SUBJ) || enroll(r, &r->keys_living, RX_LIVING_SEAT_SUBJ) || |
| 321 | enroll(r, &r->keys_living, RX_LIVING_PREPARE_SUBJ) || |
| 322 | enroll(r, &r->keys_promoter, RX_LIVING_PROMOTE_SUBJ) || |
| 323 | share(&r->keys_rig, &r->keys_living, RX_LIVING_SEAT_SUBJ)) |
| 324 | return -1; |
| 325 | for (uint32_t i = 0; i < n_fixtures; i++) |
| 326 | if (enroll(r, &r->fixtures, fixtures[i])) return -1; |
| 327 | return rx_world_bind_callers(&r->w) == RX_OK ? 0 : -1; |
| 328 | } |
| 329 | |
| 330 | int r15_start(R15Rig *r, R15Config config) { |
| 331 | return r15_start_with(r, config, NULL, 0); |
| 332 | } |
| 333 | |
| 334 | int r15_start_with(R15Rig *r, R15Config config, const uint32_t *fixture_subjects, |
| 335 | uint32_t n_fixtures) { |
| 336 | memset(r, 0, sizeof *r); |
| 337 | r->config = config; |
| 338 | r->stage = 1; |
| 339 | if (aienos_cap_start(&r->admin, &r->view) != 0) return -1; |
| 340 | int wrc = config == R15_SEQ |
| 341 | ? rx_world_init_native_sequential_reference(&r->w, r->view, RX_CRUMBS_LONG_EPISODE) |
| 342 | : rx_world_init_native(&r->w, r->view, config == R15_RES4 ? 4 : 1, |
| 343 | RX_CRUMBS_LONG_EPISODE); |
| 344 | if (wrc != RX_OK) return -1; |
| 345 | r->w.external_subject = EXTERNAL; |
| 346 | if (enroll_callers(r, fixture_subjects, n_fixtures) != 0) { |
| 347 | r->stage_why = "R16 C5: caller enrollment failed"; |
| 348 | return -1; |
| 349 | } |
| 350 | if (config == R15_SEQ) { |
| 351 | /* The orchestrator is the only thing that runs reactions in SEQ, so |
| 352 | * it runs from the start, as the workers do in RES. */ |
| 353 | pthread_mutex_lock(&r->w.mu); |
| 354 | build_plan(r); |
| 355 | pthread_mutex_unlock(&r->w.mu); |
| 356 | if (pthread_create(&r->orchestrator, NULL, orchestrator_main, r) != 0) return -1; |
| 357 | r->orch_live = 1; |
| 358 | } |
| 359 | if (!mkdtemp(strcpy(r->generation_dir, "/tmp/r15-rig-XXXXXX"))) return -1; |
| 360 | if (rx_gen_open(r->generation_dir, &r->gen) != RX_GEN_OK) return -1; |
| 361 | /* R16 C5: the store checks every proposer and promoter credential against |
| 362 | * this world, and uses the native authority whatever a caller passes. */ |
| 363 | if (rx_gen_bind_authority(r->gen, rx_world_caller_check_fn, &r->w, |
| 364 | rx_living_native_authority, r->view) != RX_GEN_OK) return -1; |
| 365 | r->stage = 2; |
| 366 | const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE; |
| 367 | RxOmegaConfig oc; |
| 368 | rx_omega_default_config(&oc); |
| 369 | oc.hot_calls = 64; oc.hot_ns = 200000; oc.margin_pct = 10; |
| 370 | if (g_cost_model) { |
| 371 | oc.cost_ns = model_cost; |
| 372 | oc.cost_ctx = r; |
| 373 | } |
| 374 | if (rx_omega_create_objects(&r->omega, &r->w, &oc) != RX_OK) return -1; |
| 375 | r->omega.keys = &r->keys_omega; |
| 376 | RxAienConfig ac; |
| 377 | if (g_aien_cfg_set) ac = g_aien_cfg; |
| 378 | else rx_aien_default_config(&ac); |
| 379 | RxAienInputs ai = {r->omega.o.demand, r->omega.o.selection}; |
| 380 | if (rx_aien_create_objects(&r->aien, &r->w, &ac, &ai) != RX_OK) return -1; |
| 381 | r->aien.keys = &r->keys_aien; |
| 382 | if (rx_living_create(&r->living, &r->w, &r->aien, &r->omega, r->gen, r->view) != RX_OK) |
| 383 | return -1; |
| 384 | r->living.keys = &r->keys_living; |
| 385 | if (new_object(r, 0x6135, RES_INTENT, &r->intent) != RX_OK) return -1; |
| 386 | |
| 387 | RxAegisPolicy pol; |
| 388 | memset(&pol, 0, sizeof pol); |
| 389 | pol.n_rules = 1; |
| 390 | pol.rules[0] = (RxAegisRule){1, RX_LIVING_SEAT_SUBJ, |
| 391 | RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, |
| 392 | RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, RW, 0, 0}; |
| 393 | RxAegisClient cl = {RX_LIVING_SEAT_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, |
| 394 | RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, RW}; |
| 395 | if (rx_aegis_create(&r->aegis, &r->w, r->admin, &pol, &cl, 1) != RX_OK) return -1; |
| 396 | r->aegis.keys = &r->keys_aegis; |
| 397 | RxAegisCaps aeg; |
| 398 | memset(&aeg, 0, sizeof aeg); |
| 399 | aeg.aegis_request = mint(r, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); |
| 400 | aeg.aegis_approval = mint(r, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_APPROVAL), R); |
| 401 | aeg.aegis_decision = mint(r, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RW); |
| 402 | aeg.root_request = mint(r, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); |
| 403 | aeg.root_decision = mint(r, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), R); |
| 404 | for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) |
| 405 | aeg.root_slot[j] = mint(r, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + j), RW); |
| 406 | if (rx_aegis_register(&r->aegis, &aeg) != RX_OK) return -1; |
| 407 | r->stage = 3; |
| 408 | |
| 409 | RxOmegaCaps omega; |
| 410 | memset(&omega, 0xff, sizeof omega); |
| 411 | omega.serve[RX_OMEGA_RES_REQUEST] = mint(r, RX_OMEGA_SUBJ_SERVE, |
| 412 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_REQUEST, R); |
| 413 | omega.serve[RX_OMEGA_RES_SELECTION] = mint(r, RX_OMEGA_SUBJ_SERVE, |
| 414 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_SELECTION, R); |
| 415 | omega.serve[RX_OMEGA_RES_DEMAND] = mint(r, RX_OMEGA_SUBJ_SERVE, |
| 416 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_DEMAND, RW); |
| 417 | omega.serve[RX_OMEGA_RES_RESULT] = mint(r, RX_OMEGA_SUBJ_SERVE, |
| 418 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_RESULT, RW); |
| 419 | omega.omega[RX_OMEGA_RES_DEMAND] = mint(r, RX_OMEGA_SUBJ_OMEGA, |
| 420 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_DEMAND, R); |
| 421 | omega.omega[RX_OMEGA_RES_SEARCH] = mint(r, RX_OMEGA_SUBJ_OMEGA, |
| 422 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_SEARCH, RW); |
| 423 | omega.omega[RX_OMEGA_RES_SELECTION] = mint(r, RX_OMEGA_SUBJ_OMEGA, |
| 424 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_SELECTION, RW); |
| 425 | for (uint32_t k = 0; k < r->omega.cfg.n_slots; k++) { |
| 426 | uint32_t idx[3] = {RX_OMEGA_RES_CANDIDATE0 + k, RX_OMEGA_RES_VERDICT0 + k, |
| 427 | RX_OMEGA_RES_MEASURE0 + k}; |
| 428 | for (uint32_t j = 0; j < 3; j++) |
| 429 | omega.omega[idx[j]] = mint(r, RX_OMEGA_SUBJ_OMEGA, RX_OMEGA_RES_BASE + idx[j], RW); |
| 430 | } |
| 431 | RxCapRef omega_belief = mint(r, RX_OMEGA_SUBJ_OMEGA, |
| 432 | RX_AIEN_RES_BASE + RX_AIEN_RES_EXPERIMENT_BELIEF, R); |
| 433 | if (rx_omega_require_evidence(&r->omega, r->aien.o.experiment_belief, omega_belief) != RX_OK) |
| 434 | return -1; |
| 435 | if (rx_omega_serve_from(&r->omega, r->living.o.inforce, mint(r, RX_OMEGA_SUBJ_SERVE, |
| 436 | RX_LIVING_RES_BASE + RX_LIVING_RES_INFORCE, R)) != RX_OK) return -1; |
| 437 | if (rx_omega_register(&r->omega, &omega) != RX_OK) return -1; |
| 438 | r->stage = 4; |
| 439 | |
| 440 | RxAienCaps aien; |
| 441 | memset(&aien, 0xff, sizeof aien); |
| 442 | for (uint32_t i = 0; i < RX_AIEN_RES_COUNT; i++) |
| 443 | aien.own[i] = mint(r, RX_AIEN_SUBJ, RX_AIEN_RES_BASE + i, |
| 444 | i == RX_AIEN_RES_PLACEMENT || i == RX_AIEN_RES_GOAL ? R : RW); |
| 445 | aien.demand = mint(r, RX_AIEN_SUBJ, RX_OMEGA_RES_BASE + RX_OMEGA_RES_DEMAND, R); |
| 446 | aien.selection = mint(r, RX_AIEN_SUBJ, RX_OMEGA_RES_BASE + RX_OMEGA_RES_SELECTION, R); |
| 447 | if (rx_aien_register(&r->aien, &aien) != RX_OK) return -1; |
| 448 | RxCapRef ev = mint(r, RX_AIEN_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_EVIDENCE, R); |
| 449 | if (rx_aien_register_experiment(&r->aien, r->living.o.evidence, ev, |
| 450 | aien.own[RX_AIEN_RES_EXPERIMENT_BELIEF]) != RX_OK) return -1; |
| 451 | RxCapRef plan_read = mint(r, RX_OMEGA_SUBJ_OMEGA, RX_AIEN_RES_BASE + RX_AIEN_RES_PLAN, R); |
| 452 | if (rx_omega_register_reconsider(&r->omega, r->aien.o.plan, plan_read, |
| 453 | omega.omega[RX_OMEGA_RES_SEARCH], omega.omega[RX_OMEGA_RES_SELECTION]) != RX_OK) |
| 454 | return -1; |
| 455 | r->stage = 5; |
| 456 | |
| 457 | RxLivingCaps lc; |
| 458 | memset(&lc, 0, sizeof lc); |
| 459 | lc.plan_read = mint(r, RX_LIVING_SUBJ, RX_AIEN_RES_BASE + RX_AIEN_RES_PLAN, R); |
| 460 | lc.search_read = mint(r, RX_LIVING_SUBJ, RX_OMEGA_RES_BASE + RX_OMEGA_RES_SEARCH, R); |
| 461 | lc.input_write = mint(r, RX_LIVING_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_INPUT, RW); |
| 462 | lc.input_seat_read = mint(r, RX_LIVING_SEAT_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_INPUT, R); |
| 463 | lc.output_read = mint(r, RX_LIVING_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, R); |
| 464 | lc.evidence_write = mint(r, RX_LIVING_SUBJ, RX_LIVING_RES_BASE + RX_LIVING_RES_EVIDENCE, RW); |
| 465 | lc.belief_read = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 466 | RX_AIEN_RES_BASE + RX_AIEN_RES_EXPERIMENT_BELIEF, R); |
| 467 | lc.selection_read = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 468 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_SELECTION, R); |
| 469 | lc.evidence_prepare_read = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 470 | RX_LIVING_RES_BASE + RX_LIVING_RES_EVIDENCE, R); |
| 471 | lc.candidate_write = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 472 | RX_LIVING_RES_BASE + RX_LIVING_RES_CANDIDATE, RW); |
| 473 | lc.goal_prepare_read = mint(r, RX_LIVING_PREPARE_SUBJ, RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, R); |
| 474 | lc.plan_prepare_read = mint(r, RX_LIVING_PREPARE_SUBJ, RX_AIEN_RES_BASE + RX_AIEN_RES_PLAN, R); |
| 475 | lc.search_prepare_read = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 476 | RX_OMEGA_RES_BASE + RX_OMEGA_RES_SEARCH, R); |
| 477 | lc.output_prepare_read = mint(r, RX_LIVING_PREPARE_SUBJ, |
| 478 | RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT, R); |
| 479 | r->promoter.world = &r->w; |
| 480 | r->promoter.keys = &r->keys_promoter; |
| 481 | r->promoter.store = r->gen; |
| 482 | r->promoter.authority = r->view; |
| 483 | r->promoter.candidate = r->living.o.candidate; |
| 484 | r->promoter.promotion = r->living.o.promotion; |
| 485 | r->promoter.inforce = r->living.o.inforce; |
| 486 | r->promoter.candidate_read = mint(r, RX_LIVING_PROMOTE_SUBJ, |
| 487 | RX_LIVING_RES_BASE + RX_LIVING_RES_CANDIDATE, R); |
| 488 | r->promoter.promotion_write = mint(r, RX_LIVING_PROMOTE_SUBJ, |
| 489 | RX_LIVING_RES_BASE + RX_LIVING_RES_PROMOTION, RW); |
| 490 | r->promoter.inforce_write = mint(r, RX_LIVING_PROMOTE_SUBJ, |
| 491 | RX_LIVING_RES_BASE + RX_LIVING_RES_INFORCE, RW); |
| 492 | r->promoter.promotion_authority = mint(r, RX_LIVING_PROMOTE_SUBJ, |
| 493 | RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE); |
| 494 | lc.output_slot = r->aegis.o[0].slot[0]; |
| 495 | lc.output_slot_read = mint(r, RX_LIVING_SEAT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_SLOT0), R); |
| 496 | /* R8 acquisition takes place before the measured episode. */ |
| 497 | r->ext_intent = mint(r, EXTERNAL, RES_INTENT, RX_RIGHT_WRITE); |
| 498 | RxReactionDesc d; |
| 499 | memset(&d, 0, sizeof d); |
| 500 | d.name = "living.experiment.ask"; |
| 501 | d.faculty = RX_FACULTY_OMEGA; |
| 502 | d.subject = RX_LIVING_SEAT_SUBJ; |
| 503 | d.priority = RX_PRIO_FOREGROUND; |
| 504 | d.fn = ask; d.user = r; d.stamp_proposed = true; |
| 505 | d.n_triggers = 1; |
| 506 | d.triggers[0] = (RxDep){r->intent, RX_ALL_FIELDS}; |
| 507 | d.n_reads = 1; |
| 508 | d.reads[0] = (RxDep){r->aegis.o[0].request, RX_ALL_FIELDS}; |
| 509 | d.n_writes = 1; |
| 510 | d.writes[0] = (RxDep){r->aegis.o[0].request, RX_ALL_FIELDS}; |
| 511 | d.n_caps = 2; |
| 512 | d.caps[0] = (RxCapNeed){mint(r, RX_LIVING_SEAT_SUBJ, RES_INTENT, R), RES_INTENT, R}; |
| 513 | d.caps[1] = (RxCapNeed){mint(r, RX_LIVING_SEAT_SUBJ, |
| 514 | rx_aegis_res(0, RX_AEGIS_RES_REQUEST), RW), rx_aegis_res(0, RX_AEGIS_RES_REQUEST), RW}; |
| 515 | if (rx_world_add_reaction_keyed(&r->w, &r->keys_rig, &d, &r->r_ask) != RX_OK) return -1; |
| 516 | RxMutation request[6] = { |
| 517 | {r->intent, 0, 1}, {r->intent, 1, RX_LIVING_RES_BASE + RX_LIVING_RES_OUTPUT}, |
| 518 | {r->intent, 2, RW}, {r->intent, 3, 0}, {r->intent, 4, 0}, |
| 519 | {r->intent, 5, RX_AEGIS_OP_ACQUIRE}}; |
| 520 | if (rx_world_publish_external(&r->w, r->ext_intent, request, 6) <= 0) { |
| 521 | r->stage_why = "R8 acquire: intent publication refused"; |
| 522 | return -1; |
| 523 | } |
| 524 | if (rx_world_wait_quiescent(&r->w, 10000) != RX_OK) { |
| 525 | r->stage_why = "R8 acquire: body did not quiesce"; |
| 526 | return -1; |
| 527 | } |
| 528 | r->seat_output = (RxCapRef){field(r, lc.output_slot, 0), field(r, lc.output_slot, 1)}; |
| 529 | r->seat_input = lc.input_seat_read; |
| 530 | if (field(r, lc.output_slot, 2) != RX_AEGIS_SLOT_LIVE) { |
| 531 | r->stage_why = "R8 acquire: slot not live after quiescence"; |
| 532 | return -1; |
| 533 | } |
| 534 | r->stage = 6; |
| 535 | if (rx_world_bind_capability(&r->w, r->living.o.input, r->seat_input) != RX_OK || |
| 536 | rx_world_bind_capability(&r->w, r->living.o.output, r->seat_output) != RX_OK) |
| 537 | return -1; |
| 538 | if (rx_world_enable_resident(&r->w) != RX_OK) return -1; |
| 539 | if (rx_living_register(&r->living, &lc, &r->promoter) != RX_OK) return -1; |
| 540 | RxResourceBudget budget = {0}; |
| 541 | budget.slots = 8; |
| 542 | budget.memory_bytes = UINT64_MAX; |
| 543 | budget.energy_budget = UINT64_MAX; |
| 544 | budget.offered_locality = UINT32_MAX; |
| 545 | budget.offered_accel = RX_ACCEL_BLACKWELL; |
| 546 | budget.compute_mask = UINT32_MAX; |
| 547 | rx_world_set_resources(&r->w, &budget); |
| 548 | r->ext_request = mint(r, EXTERNAL, RX_OMEGA_RES_BASE + RX_OMEGA_RES_REQUEST, RX_RIGHT_WRITE); |
| 549 | r->ext_goal = mint(r, EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, RX_RIGHT_WRITE); |
| 550 | r->ext_placement = mint(r, EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_PLACEMENT, |
| 551 | RX_RIGHT_WRITE); |
| 552 | #ifdef R15_SILICON |
| 553 | if (rx_gpu_seat_begin(&r->w, &r->seat) != 0 || !r->seat) return -1; |
| 554 | #endif |
| 555 | if (config != R15_SEQ) { |
| 556 | atomic_store(&r->transport_stop, 0); |
| 557 | if (pthread_create(&r->transport, NULL, transport_main, r) != 0) return -1; |
| 558 | r->transport_live = 1; |
| 559 | } |
| 560 | if (rx_world_wait_quiescent(&r->w, 10000) != RX_OK) return -1; |
| 561 | take_baseline(r); |
| 562 | r->stage = 7; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /* ---- production ---------------------------------------------------------- */ |
| 567 | |
| 568 | static uint64_t expected_digest(uint64_t seed) { |
| 569 | uint64_t *A = malloc((size_t)R15_M * R15_N * sizeof(uint64_t)); |
| 570 | uint64_t *x = malloc((size_t)R15_N * sizeof(uint64_t)); |
| 571 | uint64_t *y = malloc((size_t)R15_M * sizeof(uint64_t)); |
| 572 | if (!A || !x || !y) { free(A); free(x); free(y); return 0; } |
| 573 | rx_omega_fill(seed, A, x, R15_M, R15_N); |
| 574 | omega_matvec_reference(A, x, y, R15_M, R15_N); |
| 575 | uint64_t result = rx_omega_digest(y, R15_M); |
| 576 | free(A); free(x); free(y); |
| 577 | return result; |
| 578 | } |
| 579 | |
| 580 | /* One production request (spec §4 W-PROD). */ |
| 581 | static int serve(R15Rig *r) { |
| 582 | uint64_t seq = ++r->request_seq; |
| 583 | uint64_t seed = seq * 0x2545F4914F6CDD1Dull; |
| 584 | RxMutation m[4] = {{r->omega.o.request, 0, seq}, {r->omega.o.request, 1, R15_M}, |
| 585 | {r->omega.o.request, 2, R15_N}, {r->omega.o.request, 3, seed}}; |
| 586 | int64_t prc = rx_world_publish_external(&r->w, r->ext_request, m, 4); |
| 587 | if (prc <= 0) { |
| 588 | snprintf(r->serve_why, sizeof r->serve_why, "request %llu refused: %lld", |
| 589 | (unsigned long long)seq, (long long)prc); |
| 590 | return -1; |
| 591 | } |
| 592 | uint64_t t = now_ns(); |
| 593 | while (field(r, r->omega.o.result, 0) != seq) { |
| 594 | if (now_ns() - t > 5000000000ull) { |
| 595 | /* What was the body doing when the answer did not come? */ |
| 596 | char busy[200] = ""; |
| 597 | size_t at = 0; |
| 598 | pthread_mutex_lock(&r->w.mu); |
| 599 | for (uint32_t i = 0; i < r->w.n_reactions && at < sizeof busy - 40; i++) { |
| 600 | const RxReaction *rx = &r->w.reactions[i]; |
| 601 | if (rx->state != RX_DORMANT || rx->quarantined) |
| 602 | at += (size_t)snprintf(busy + at, sizeof busy - at, " %s:%s%s", |
| 603 | rx->desc.name ? rx->desc.name : "?", |
| 604 | rx_state_name(rx->state), |
| 605 | rx->quarantined ? "(Q)" : ""); |
| 606 | } |
| 607 | uint64_t serve_state = r->w.reactions[r->omega.r_serve].state; |
| 608 | uint64_t serve_acts = r->w.reactions[r->omega.r_serve].activations; |
| 609 | pthread_mutex_unlock(&r->w.mu); |
| 610 | snprintf(r->serve_why, sizeof r->serve_why, |
| 611 | "request %llu unanswered 5 s (result seq %llu, serve %s acts %llu," |
| 612 | " orch err %d):%s", (unsigned long long)seq, |
| 613 | (unsigned long long)field(r, r->omega.o.result, 0), |
| 614 | rx_state_name((RxState)serve_state), (unsigned long long)serve_acts, |
| 615 | atomic_load(&r->orch_error), busy); |
| 616 | return -1; |
| 617 | } |
| 618 | spin_us(5); |
| 619 | } |
| 620 | if (field(r, r->omega.o.result, 1) != expected_digest(seed)) r->wrong++; |
| 621 | uint64_t used = field(r, r->omega.o.result, 2); |
| 622 | if (used != 0 && (field(r, r->living.o.inforce, 0) == 0 || |
| 623 | field(r, r->living.o.inforce, 1) != used)) |
| 624 | r->unpromoted_use++; |
| 625 | __atomic_add_fetch(&r->served, 1, __ATOMIC_RELAXED); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | static void *producer_main(void *arg) { |
| 630 | R15Rig *r = arg; |
| 631 | while (!atomic_load(&r->producer_stop)) |
| 632 | if (serve(r) != 0) { atomic_store(&r->producer_error, 1); break; } |
| 633 | return NULL; |
| 634 | } |
| 635 | |
| 636 | static void stop_producer(R15Rig *r) { |
| 637 | if (!r->producer_live) return; |
| 638 | atomic_store(&r->producer_stop, 1); |
| 639 | pthread_join(r->producer, NULL); |
| 640 | r->producer_live = 0; |
| 641 | } |
| 642 | |
| 643 | static int publish_placement(R15Rig *r, uint64_t cls) { |
| 644 | RxMutation m[2] = {{r->aien.o.placement, 0, field(r, r->aien.o.placement, 0) + 1}, |
| 645 | {r->aien.o.placement, 1, cls}}; |
| 646 | return rx_world_publish_external(&r->w, r->ext_placement, m, 2) > 0 ? 0 : -1; |
| 647 | } |
| 648 | |
| 649 | static int publish_goal(R15Rig *r, uint64_t target) { |
| 650 | RxMutation m[3] = {{r->aien.o.goal, 0, 1}, {r->aien.o.goal, 1, rx_omega_regime(R15_M, R15_N)}, |
| 651 | {r->aien.o.goal, 2, target}}; |
| 652 | r->goal_crumb = rx_world_publish_external(&r->w, r->ext_goal, m, 3); |
| 653 | return r->goal_crumb > 0 ? 0 : -1; |
| 654 | } |
| 655 | |
| 656 | int r15_producer_start(R15Rig *r) { |
| 657 | if (r->producer_live) return 0; |
| 658 | atomic_store(&r->producer_stop, 0); |
| 659 | if (pthread_create(&r->producer, NULL, producer_main, r) != 0) return -1; |
| 660 | r->producer_live = 1; |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | int r15_producer_stop(R15Rig *r) { |
| 665 | stop_producer(r); |
| 666 | return atomic_load(&r->producer_error) ? -1 : 0; |
| 667 | } |
| 668 | |
| 669 | int r15_placement(R15Rig *r, uint32_t cls) { |
| 670 | return publish_placement(r, cls); |
| 671 | } |
| 672 | |
| 673 | RxCapRef r15_mint(R15Rig *r, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 674 | return mint(r, subject, resource, rights); |
| 675 | } |
| 676 | |
| 677 | int r15_move_class(uint32_t cls) { |
| 678 | cpu_set_t a, x; |
| 679 | if (core_sets(&a, &x) != 0) return -1; |
| 680 | return move_threads(cls == R15_CLASS_A725 ? &a : &x) > 0 ? 0 : -1; |
| 681 | } |
| 682 | |
| 683 | static int orchestration_error(R15Rig *r) { |
| 684 | return atomic_load(&r->orch_error) || atomic_load(&r->transport_error); |
| 685 | } |
| 686 | |
| 687 | /* ---- the episode ---------------------------------------------------------- */ |
| 688 | |
| 689 | #define EFAIL(...) do { snprintf(out->why, sizeof out->why, __VA_ARGS__); return -1; } while (0) |
| 690 | #define ULL(x) ((unsigned long long)(x)) |
| 691 | |
| 692 | static void slot_snapshot(R15Rig *r, R15Outcome *out) { |
| 693 | for (uint32_t k = 0; k < r->omega.cfg.n_slots && k < RX_OMEGA_SLOTS; k++) { |
| 694 | out->slot_verdict[k] = field(r, r->omega.o.verdict[k], 1); |
| 695 | out->slot_measure[k] = field(r, r->omega.o.measure[k], 1); |
| 696 | out->slot_cps[k] = field(r, r->omega.o.measure[k], 2); |
| 697 | out->slot_rps[k] = field(r, r->omega.o.measure[k], 3); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* How far a failed adaptation got (reported only; the episode still fails). */ |
| 702 | static void progress_snapshot(R15Rig *r, R15Outcome *out) { |
| 703 | slot_snapshot(r, out); |
| 704 | out->goal_status = field(r, r->aien.o.assessment, 4); |
| 705 | out->plan_action = field(r, r->aien.o.plan, 1); |
| 706 | out->plan_seq = field(r, r->aien.o.plan, 0); |
| 707 | out->plan_regime = field(r, r->aien.o.plan, 2); |
| 708 | out->plan_condition = field(r, r->aien.o.plan, 3); |
| 709 | out->plan_reason = field(r, r->aien.o.plan, 5); |
| 710 | out->plan_goal = field(r, r->aien.o.plan, 6); |
| 711 | out->selection_regime = field(r, r->omega.o.selection, 6); |
| 712 | out->search_epoch = field(r, r->omega.o.search, 0); |
| 713 | out->selection_epoch = field(r, r->omega.o.selection, 0); |
| 714 | out->candidate_id = field(r, r->living.o.candidate, 0); |
| 715 | out->gpu_claims = r->w.stats.resident_claims; |
| 716 | out->seat_commits = r->w.reactions[r->living.r_seat].commits; |
| 717 | for (uint32_t i = 0; i < 5; i++) out->evidence[i] = field(r, r->living.o.evidence, i); |
| 718 | for (uint32_t i = 0; i < 4; i++) out->belief[i] = field(r, r->aien.o.experiment_belief, i); |
| 719 | out->selected_ps = field(r, r->omega.o.selection, 5); |
| 720 | out->reference_ps = field(r, r->omega.o.selection, 7); |
| 721 | out->served = r->served; |
| 722 | out->crumbs = r->w.n_crumbs; |
| 723 | } |
| 724 | |
| 725 | int r15_episode(R15Rig *r, uint64_t target_ns, R15Outcome *out) { |
| 726 | memset(out, 0, sizeof *out); |
| 727 | cpu_set_t a, x; |
| 728 | if (core_sets(&a, &x) != 0 || move_threads(&a) <= 0) EFAIL("needs both core classes"); |
| 729 | if (publish_placement(r, R15_CLASS_A725) != 0) EFAIL("placement"); |
| 730 | uint64_t limit = now_ns() + 60000000000ull; |
| 731 | while (field(r, r->omega.o.selection, 0) < 1 && now_ns() < limit) |
| 732 | if (serve(r) != 0 || orchestration_error(r)) EFAIL("serve before first selection: %s", r->serve_why); |
| 733 | if (field(r, r->omega.o.selection, 0) != 1) EFAIL("no first selection"); |
| 734 | while (field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED && now_ns() < limit) |
| 735 | if (serve(r) != 0 || orchestration_error(r)) EFAIL("serve before confirmation: %s", r->serve_why); |
| 736 | if (field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED) |
| 737 | EFAIL("AIEN never confirmed the incumbent cost (belief: baseline mean %llu ns, last " |
| 738 | "interval %llu ns, intervals %llu, miss streak %llu; prediction state %llu, hits " |
| 739 | "%llu; %llu requests served)", ULL(field(r, r->aien.o.belief, 4)), |
| 740 | ULL(field(r, r->aien.o.belief, 6)), ULL(field(r, r->aien.o.belief, 5)), |
| 741 | ULL(field(r, r->aien.o.belief, 7)), ULL(field(r, r->aien.o.prediction, 6)), |
| 742 | ULL(field(r, r->aien.o.prediction, 3)), ULL(r->request_seq)); |
| 743 | out->incumbent_ns = field(r, r->aien.o.prediction, 5); |
| 744 | out->target_ns = target_ns ? target_ns : out->incumbent_ns * TARGET_PCT / 100u; |
| 745 | rx_gen_active(r->gen, &out->gen_before, &out->lineage_before); |
| 746 | uint64_t before_decide = r->w.reactions[r->aegis.r_decide[0]].activations; |
| 747 | uint64_t before_install = r->w.reactions[r->aegis.r_install[0]].activations; |
| 748 | |
| 749 | if (r->hooks && r->hooks->idle && |
| 750 | r->hooks->idle((R15Hooks *)r->hooks, r) != 0) EFAIL("idle window"); |
| 751 | atomic_store(&r->producer_stop, 0); |
| 752 | if (pthread_create(&r->producer, NULL, producer_main, r) != 0) EFAIL("producer"); |
| 753 | r->producer_live = 1; |
| 754 | if (r->hooks && r->hooks->before && |
| 755 | r->hooks->before((R15Hooks *)r->hooks, r) != 0) EFAIL("BEFORE window"); |
| 756 | if (atomic_load(&r->producer_error)) EFAIL("serve in BEFORE: %s", r->serve_why); |
| 757 | if (move_threads(&x) <= 0 || publish_placement(r, R15_CLASS_X925) != 0 || |
| 758 | publish_goal(r, out->target_ns) != 0) EFAIL("goal"); |
| 759 | /* 90 s (spec-era R13 value); R15_PROMOTION_TIMEOUT_MS shortens it only to |
| 760 | * exercise the failure path in tests. */ |
| 761 | uint64_t wait_ms = 90000; |
| 762 | const char *env = getenv("R15_PROMOTION_TIMEOUT_MS"); |
| 763 | if (env && *env) wait_ms = strtoull(env, NULL, 10); |
| 764 | limit = now_ns() + wait_ms * 1000000ull; |
| 765 | while (now_ns() < limit && field(r, r->living.o.promotion, 0) == 0) { |
| 766 | pause_us(20); |
| 767 | if (atomic_load(&r->producer_error)) EFAIL("serve during adaptation: %s", r->serve_why); |
| 768 | if (orchestration_error(r)) EFAIL("orchestration error %d/%d", |
| 769 | atomic_load(&r->orch_error), atomic_load(&r->transport_error)); |
| 770 | } |
| 771 | if (field(r, r->living.o.promotion, 0) == 0 || r->promoter.result != RX_GEN_OK) { |
| 772 | progress_snapshot(r, out); |
| 773 | EFAIL("no promotion: result %d, prepare refusal %d", r->promoter.result, |
| 774 | r->living.last_refusal); |
| 775 | } |
| 776 | uint64_t epoch = field(r, r->living.o.inforce, 0); |
| 777 | limit = now_ns() + 60000000000ull; |
| 778 | /* Wait for AIEN's decided assessment of the confirmed X925 prediction. |
| 779 | * Confirming a prediction does not change its sequence (field 0), so an |
| 780 | * assessment made just before the confirmation also matches field 6 and |
| 781 | * legitimately reads UNKNOWN; AIEN re-assesses when the confirmation |
| 782 | * lands. Clarification C4: the decisive status is the first decided one |
| 783 | * (MET or UNMET*). Still failing: UNMET*, and UNKNOWN when the limit |
| 784 | * expires. */ |
| 785 | while (now_ns() < limit && !atomic_load(&r->producer_error) && |
| 786 | !(field(r, r->aien.o.prediction, 1) == epoch && |
| 787 | field(r, r->aien.o.prediction, 6) == RX_AIEN_PRED_CONFIRMED && |
| 788 | field(r, r->aien.o.prediction, 4) == R15_CLASS_X925 && |
| 789 | field(r, r->aien.o.assessment, 6) == field(r, r->aien.o.prediction, 0) && |
| 790 | field(r, r->aien.o.assessment, 4) != RX_AIEN_GOAL_UNKNOWN)) |
| 791 | pause_us(100); |
| 792 | /* The goal status that decides the episode is the one at goal MET; the |
| 793 | * AFTER window keeps production running and AIEN keeps re-assessing, so |
| 794 | * the status after it is only reported (goal_status_final). */ |
| 795 | uint64_t met_status = field(r, r->aien.o.assessment, 4); |
| 796 | if (r->hooks && r->hooks->after && !atomic_load(&r->producer_error) && |
| 797 | met_status == RX_AIEN_GOAL_MET && |
| 798 | r->hooks->after((R15Hooks *)r->hooks, r) != 0) { |
| 799 | stop_producer(r); |
| 800 | EFAIL("AFTER window"); |
| 801 | } |
| 802 | stop_producer(r); |
| 803 | if (atomic_load(&r->producer_error)) EFAIL("serve after promotion: %s", r->serve_why); |
| 804 | if (rx_world_wait_quiescent(&r->w, 10000) != RX_OK) EFAIL("did not quiesce"); |
| 805 | if (orchestration_error(r)) EFAIL("orchestration error at the end"); |
| 806 | |
| 807 | /* exact semantic results */ |
| 808 | out->goal_seq = field(r, r->aien.o.assessment, 0); |
| 809 | out->goal_regime = field(r, r->aien.o.assessment, 1); |
| 810 | out->goal_status_final = field(r, r->aien.o.assessment, 4); |
| 811 | out->goal_status = r->hooks ? met_status : out->goal_status_final; |
| 812 | out->goal_class = field(r, r->aien.o.assessment, 5); |
| 813 | out->plan_action = field(r, r->aien.o.plan, 1); |
| 814 | out->plan_regime = field(r, r->aien.o.plan, 2); |
| 815 | out->plan_condition = field(r, r->aien.o.plan, 3); |
| 816 | out->plan_reason = field(r, r->aien.o.plan, 5); |
| 817 | out->plan_goal = field(r, r->aien.o.plan, 6); |
| 818 | out->search_epoch = field(r, r->omega.o.search, 0); |
| 819 | out->selection_epoch = field(r, r->omega.o.selection, 0); |
| 820 | for (uint32_t j = 0; j < 4; j++) { |
| 821 | out->selection_id[j] = field(r, r->omega.o.selection, 1 + j); |
| 822 | out->inforce_id[j] = field(r, r->living.o.inforce, 1 + j); |
| 823 | } |
| 824 | out->selection_regime = field(r, r->omega.o.selection, 6); |
| 825 | out->selected_verdict = UINT64_MAX; |
| 826 | for (uint32_t k = 0; k < r->omega.cfg.n_slots; k++) { |
| 827 | int same = 1; |
| 828 | for (uint32_t j = 0; j < 4; j++) |
| 829 | if (field(r, r->omega.o.candidate[k], 2 + j) != out->selection_id[j]) same = 0; |
| 830 | if (same) out->selected_verdict = field(r, r->omega.o.verdict[k], 1); |
| 831 | } |
| 832 | for (uint32_t i = 0; i < 5; i++) out->evidence[i] = field(r, r->living.o.evidence, i); |
| 833 | for (uint32_t i = 0; i < 4; i++) out->belief[i] = field(r, r->aien.o.experiment_belief, i); |
| 834 | rx_gen_active(r->gen, &out->gen_after, &out->lineage_after); |
| 835 | out->inforce_epoch = field(r, r->living.o.inforce, 0); |
| 836 | out->inforce_regime = field(r, r->living.o.inforce, 6); |
| 837 | out->inforce_generation = field(r, r->living.o.inforce, 7); |
| 838 | out->promote_result = (int64_t)field(r, r->living.o.promotion, 1); |
| 839 | out->candidate_id = field(r, r->living.o.candidate, 0); |
| 840 | out->promotion_candidate = field(r, r->living.o.promotion, 0); |
| 841 | out->promotion_active = field(r, r->living.o.promotion, 3); |
| 842 | out->aegis_woken = r->w.reactions[r->aegis.r_decide[0]].activations - before_decide; |
| 843 | out->root_woken = r->w.reactions[r->aegis.r_install[0]].activations - before_install; |
| 844 | out->gpu_claims = r->w.stats.resident_claims; |
| 845 | out->seat_commits = r->w.reactions[r->living.r_seat].commits; |
| 846 | /* integrity */ |
| 847 | out->crumbs_verified = rx_world_verify_crumbs(&r->w, &out->crumbs_checked) == 0; |
| 848 | out->crumb_overflow = r->w.stats.crumb_overflow; |
| 849 | out->illegal = r->w.stats.illegal_transitions; |
| 850 | out->wrong = r->wrong; |
| 851 | out->unpromoted_use = r->unpromoted_use; |
| 852 | out->lost_triggers = r15_lost_triggers(r, NULL, 0); |
| 853 | /* measured */ |
| 854 | out->selected_ps = field(r, r->omega.o.selection, 5); |
| 855 | out->reference_ps = field(r, r->omega.o.selection, 7); |
| 856 | out->inforce_ps = field(r, r->living.o.inforce, 5); |
| 857 | out->final_expected_ns = field(r, r->aien.o.assessment, 3); |
| 858 | out->plan_seq = field(r, r->aien.o.plan, 0); |
| 859 | out->served = r->served; |
| 860 | out->crumbs = r->w.n_crumbs; |
| 861 | slot_snapshot(r, out); |
| 862 | out->ok = out->goal_status == RX_AIEN_GOAL_MET; |
| 863 | if (!out->ok) EFAIL("goal not MET (status %llu)", (unsigned long long)out->goal_status); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | uint64_t r15_lost_triggers(R15Rig *r, char *first, size_t n) { |
| 868 | static uint64_t last_read[RX_MAX_REACTIONS][RX_MAX_DEPS]; |
| 869 | uint64_t lost = 0; |
| 870 | pthread_mutex_lock(&r->w.mu); |
| 871 | memset(last_read, 0, sizeof last_read); |
| 872 | uint32_t nr = r->w.n_reactions; |
| 873 | for (uint64_t id = 1; id <= r->w.n_crumbs; id++) { |
| 874 | const RxCrumb *k = &r->w.crumbs[id - 1]; |
| 875 | if (k->reaction >= nr) continue; |
| 876 | const RxReactionDesc *d = &r->w.reactions[k->reaction].desc; |
| 877 | for (uint32_t i = 0; i < k->n_inputs; i++) |
| 878 | for (uint32_t t = 0; t < d->n_triggers; t++) |
| 879 | if (k->inputs[i].obj.id == d->triggers[t].obj.id && |
| 880 | k->inputs[i].obj.generation == d->triggers[t].obj.generation && |
| 881 | k->inputs[i].version > last_read[k->reaction][t]) |
| 882 | last_read[k->reaction][t] = k->inputs[i].version; |
| 883 | } |
| 884 | for (uint32_t i = 0; i < nr; i++) { |
| 885 | const RxReaction *rx = &r->w.reactions[i]; |
| 886 | if (rx->quarantined) continue; |
| 887 | for (uint32_t t = 0; t < rx->desc.n_triggers; t++) { |
| 888 | const RxDep *dep = &rx->desc.triggers[t]; |
| 889 | const RxObject *o = &r->w.objects[dep->obj.id]; |
| 890 | if (!o->live || o->generation != dep->obj.generation) continue; |
| 891 | uint64_t cur = 0; |
| 892 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) |
| 893 | if ((dep->mask & RX_FIELD(f)) && o->field_version[f] > cur) |
| 894 | cur = o->field_version[f]; |
| 895 | if (cur > r->baseline[i][t] && cur > last_read[i][t]) { |
| 896 | if (!lost && first) |
| 897 | snprintf(first, n, "%s trigger %u: object %u at version %llu, last read %llu", |
| 898 | rx->desc.name ? rx->desc.name : "?", t, dep->obj.id, |
| 899 | (unsigned long long)cur, (unsigned long long)last_read[i][t]); |
| 900 | lost++; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | pthread_mutex_unlock(&r->w.mu); |
| 905 | return lost; |
| 906 | } |
| 907 | |
| 908 | void r15_stop(R15Rig *r) { |
| 909 | R15StageClock *sc = &r->stop_clock; |
| 910 | memset(sc, 0, sizeof *sc); |
| 911 | r15_stage_mark(sc, "begin"); |
| 912 | stop_producer(r); |
| 913 | r15_stage_mark(sc, "producer"); |
| 914 | if (r->transport_live) { |
| 915 | atomic_store(&r->transport_stop, 1); |
| 916 | pthread_join(r->transport, NULL); |
| 917 | r->transport_live = 0; |
| 918 | } |
| 919 | r15_stage_mark(sc, "transport"); |
| 920 | if (r->orch_live) { |
| 921 | atomic_store(&r->orch_stop, 1); |
| 922 | pthread_join(r->orchestrator, NULL); |
| 923 | r->orch_live = 0; |
| 924 | } |
| 925 | r15_stage_mark(sc, "orchestrator"); |
| 926 | #ifdef R15_SILICON |
| 927 | if (r->seat) { |
| 928 | (void)rx_resident_shutdown(&r->w); |
| 929 | r15_stage_mark(sc, "seat_shutdown_post"); |
| 930 | RxGpuSeatLeave lv = {0}; |
| 931 | r->leave_rc = rx_gpu_seat_finish_ex(r->seat, &lv); |
| 932 | r->leave_marker = lv.marker_value; |
| 933 | r->leave_sem = lv.sem_value; |
| 934 | r->leave_valid = 1; |
| 935 | r15_stage_mark(sc, "seat_finish"); |
| 936 | } |
| 937 | #endif |
| 938 | rx_world_destroy(&r->w); |
| 939 | r15_stage_mark(sc, "world_destroy"); |
| 940 | rx_aegis_destroy(&r->aegis); |
| 941 | rx_omega_destroy(&r->omega); |
| 942 | if (r->gen) rx_gen_close(r->gen); |
| 943 | if (r->admin) aienos_cap_stop(r->admin, r->view); |
| 944 | r15_stage_mark(sc, "faculties_admin"); |
| 945 | if (r->generation_dir[0]) { |
| 946 | char cmd[200]; |
| 947 | snprintf(cmd, sizeof cmd, "rm -rf '%s'", r->generation_dir); |
| 948 | if (system(cmd) != 0) fprintf(stderr, "note: could not remove %s\n", r->generation_dir); |
| 949 | } |
| 950 | r15_stage_mark(sc, "generation_dir"); |
| 951 | } |