interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / runtime / rx_omega.c

1014 lines · 40314 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * rx_omega.c -- Omega as a resident realization faculty (ADR 0016 ยง43, R10).
19 *
20 * See rx_omega.h for the object and reaction map. Every function named fn_*
21 * is a reaction body: it reads its versioned snapshot and proposes
22 * mutations. None of them calls another faculty or waits for one.
23 *
24 * Side effects outside the world are limited to the realization store, which
25 * is content-addressed and append-only. Writing the same identity twice with
26 * the same bytes is a no-op; the same identity with different bytes is
27 * refused. So a reaction that is invalidated and runs again cannot leave the
28 * store in a different state.
29 */
30#include "rx_omega.h"
31
32#include "aarch64_encoder.h"
33#include "aarch64_target.h"
34#include "omega_realize.h"
35#include "omega_verify.h"
36#include "sha256.h"
37
38#include <errno.h>
39#include <poll.h>
40#include <signal.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sys/mman.h>
44#include <sys/wait.h>
45#include <time.h>
46#include <unistd.h>
47
48typedef void (*MatVecFn)(const uint64_t *A, const uint64_t *x, uint64_t *y, uint64_t M, uint64_t N);
49
50#define KIND_DEFECT (OMEGA_MATVEC_KIND_QUAD4 + 1u)
51#define PAGE 4096u
52
53static uint64_t now_ns(void) {
54 struct timespec ts;
55 clock_gettime(CLOCK_MONOTONIC, &ts);
56 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
57}
58
59/* What a matvec costs: the time the calling thread spent executing it.
60 * Serve and measure time their calls with this, so a preemption or a busy
61 * neighbour on the same core does not count as cost, while the core it runs
62 * on and the code it runs still do (omega #313, #527). Deadlines stay on
63 * now_ns. */
64static uint64_t cost_ns(void) {
65 struct timespec ts;
66 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
67 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
68}
69
70static uint64_t id_word(const SemanticId *id, uint32_t i) {
71 uint64_t v = 0;
72 for (uint32_t b = 0; b < 8; b++) v |= (uint64_t)id->bytes[i * 8 + b] << (8 * b);
73 return v;
74}
75
76static void id_from_words(SemanticId *id, const uint64_t w[4]) {
77 for (uint32_t i = 0; i < 4; i++)
78 for (uint32_t b = 0; b < 8; b++) id->bytes[i * 8 + b] = (uint8_t)(w[i] >> (8 * b));
79}
80
81static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) {
82 for (uint32_t i = 0; i < c->n_in; i++)
83 if (c->in[i].obj.id == r.id) return &c->in[i];
84 return NULL;
85}
86
87static void put(RxCtx *c, RxObjRef o, uint32_t field, uint64_t v) {
88 c->out[c->n_out++] = (RxMutation){ o, field, v };
89}
90
91/* ---- data shared by production, verification and measurement ---- */
92
93static uint64_t mix(uint64_t *s) {
94 uint64_t z = (*s += 0x9e3779b97f4a7c15ull);
95 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
96 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
97 return z ^ (z >> 31);
98}
99
100void rx_omega_fill(uint64_t seed, uint64_t *A, uint64_t *x, uint32_t M, uint32_t N) {
101 uint64_t s = seed;
102 for (size_t i = 0; i < (size_t)M * N; i++) A[i] = mix(&s);
103 for (uint32_t j = 0; j < N; j++) x[j] = mix(&s);
104}
105
106uint64_t rx_omega_digest(const uint64_t *y, uint32_t M) {
107 sha256_ctx c;
108 sha256_init(&c);
109 uint8_t b[8];
110 for (uint32_t i = 0; i < M; i++) {
111 for (int k = 0; k < 8; k++) b[k] = (uint8_t)(y[i] >> (8 * k));
112 sha256_update(&c, b, 8);
113 }
114 uint8_t d[32];
115 sha256_final(&c, d);
116 uint64_t v = 0;
117 for (int k = 0; k < 8; k++) v |= (uint64_t)d[k] << (8 * k);
118 return v;
119}
120
121static int shape_ok(uint64_t M, uint64_t N) {
122 return M >= 1 && N >= 1 && M <= RX_OMEGA_MAX_ELEMS && N <= RX_OMEGA_MAX_ELEMS &&
123 M * N <= RX_OMEGA_MAX_ELEMS;
124}
125
126/* ---- realization store ---- */
127
128static void real_object(const RxOmegaFaculty *f, const uint8_t *code, size_t len,
129 RealizationObject *r) {
130 memset(r, 0, sizeof(*r));
131 r->target_profile = f->machine.target_profile;
132 r->entry_offset = 0;
133 r->semantic_id = f->spec.spec_id;
134 r->machine_id = f->machine.machine_id;
135 r->has_machine_id = true;
136 r->code_len = len;
137 memcpy(r->code_bytes, code, len);
138}
139
140static int ids_equal(const SemanticId *a, const SemanticId *b) {
141 return memcmp(a->bytes, b->bytes, sizeof(a->bytes)) == 0;
142}
143
144/* Caller holds f->mu. */
145static RxOmegaRealization *store_find_locked(RxOmegaFaculty *f, const SemanticId *id) {
146 for (uint32_t i = 0; i < f->n_store; i++)
147 if (ids_equal(&f->store[i].id, id)) return &f->store[i];
148 return NULL;
149}
150
151/* Record bytes under an identity. The identity is what the synthesizer
152 * claims; only the verifier decides whether the bytes live up to it. */
153static int store_put(RxOmegaFaculty *f, const SemanticId *id, uint32_t kind,
154 const uint8_t *code, size_t len) {
155 if (len == 0 || len > AARCH64_MAX_CODE_BYTES || len > PAGE) return -1;
156 pthread_mutex_lock(&f->mu);
157 RxOmegaRealization *e = store_find_locked(f, id);
158 int rc = 0;
159 if (e) {
160 if (e->code_len != len || memcmp(e->code, code, len) != 0) rc = -1;
161 goto out;
162 }
163 if (f->n_store >= RX_OMEGA_STORE) { rc = -1; goto out; }
164 void *page = mmap(NULL, PAGE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
165 if (page == MAP_FAILED) { rc = -1; goto out; }
166 memcpy(page, code, len);
167 __builtin___clear_cache((char *)page, (char *)page + len);
168 if (mprotect(page, PAGE, PROT_READ | PROT_EXEC) != 0) {
169 munmap(page, PAGE);
170 rc = -1;
171 goto out;
172 }
173 e = &f->store[f->n_store++];
174 memset(e, 0, sizeof(*e));
175 e->id = *id;
176 e->kind = kind;
177 e->code_len = len;
178 memcpy(e->code, code, len);
179 e->page = page;
180out:
181 pthread_mutex_unlock(&f->mu);
182 return rc;
183}
184
185int rx_omega_store_find(RxOmegaFaculty *f, uint64_t w0, RxOmegaRealization *out) {
186 pthread_mutex_lock(&f->mu);
187 int rc = -1;
188 for (uint32_t i = 0; i < f->n_store; i++)
189 if (id_word(&f->store[i].id, 0) == w0) {
190 *out = f->store[i];
191 rc = 0;
192 break;
193 }
194 pthread_mutex_unlock(&f->mu);
195 return rc;
196}
197
198/* A realization the parent may execute: verified, and named by all 32 bytes. */
199static MatVecFn verified_fn(RxOmegaFaculty *f, const SemanticId *id, uint32_t *kind) {
200 pthread_mutex_lock(&f->mu);
201 RxOmegaRealization *e = store_find_locked(f, id);
202 MatVecFn fn = NULL;
203 if (e && e->verified) {
204 union { void *p; MatVecFn fn; } u;
205 u.p = e->page;
206 fn = u.fn;
207 if (kind) *kind = e->kind;
208 e->parent_runs++;
209 }
210 pthread_mutex_unlock(&f->mu);
211 return fn;
212}
213
214/* ---- workload.serve: the production path ---- */
215
216static int fn_serve(RxCtx *c) {
217 RxOmegaFaculty *f = c->user;
218 const RxSnapshotDep *req = in_of(c, f->o.request);
219 const RxSnapshotDep *sel = in_of(c, f->serve_from_record ? f->serve_record : f->o.selection);
220 const RxSnapshotDep *dem = in_of(c, f->o.demand);
221 if (!req || !sel || !dem) return -1;
222 uint64_t seq = req->field[0], M = req->field[1], N = req->field[2];
223 if (seq == 0) return 0;
224 if (!shape_ok(M, N)) return -1;
225
226 uint64_t *A = malloc((size_t)M * N * sizeof(uint64_t));
227 uint64_t *x = malloc((size_t)N * sizeof(uint64_t));
228 uint64_t *y = malloc((size_t)M * sizeof(uint64_t));
229 if (!A || !x || !y) { free(A); free(x); free(y); return -1; }
230 rx_omega_fill(req->field[3], A, x, (uint32_t)M, (uint32_t)N);
231
232 /* Whatever the world currently records for this regime. Never waits. */
233 MatVecFn fn = NULL;
234 uint32_t kind = RX_OMEGA_COST_REFERENCE;
235 uint64_t used = 0;
236 if (sel->field[0] != 0 && sel->field[6] == rx_omega_regime(M, N) && sel->field[1] != 0) {
237 SemanticId id;
238 id_from_words(&id, &sel->field[1]);
239 fn = verified_fn(f, &id, &kind);
240 if (fn) used = sel->field[1];
241 }
242 uint64_t t0 = cost_ns();
243 if (fn) fn(A, x, y, M, N);
244 else omega_matvec_reference(A, x, y, (uint32_t)M, (uint32_t)N);
245 uint64_t ns = cost_ns() - t0;
246 if (f->cfg.cost_ns)
247 ns = f->cfg.cost_ns(f->cfg.cost_ctx, kind, (uint32_t)M, (uint32_t)N, 1);
248 uint64_t digest = rx_omega_digest(y, (uint32_t)M);
249 free(A); free(x); free(y);
250
251 pthread_mutex_lock(&f->mu);
252 if (fn) f->served_realized++; else f->served_reference++;
253 pthread_mutex_unlock(&f->mu);
254
255 put(c, f->o.result, 0, seq);
256 put(c, f->o.result, 1, digest);
257 put(c, f->o.result, 2, used);
258 put(c, f->o.result, 3, ns);
259
260 /* Cost evidence. A new regime starts its own count. The window field
261 * moves only every RX_OMEGA_WINDOW calls; that is what wakes omega.watch. */
262 int same = dem->field[4] == M && dem->field[5] == N;
263 uint64_t calls = same ? dem->field[0] + 1 : 1;
264 uint64_t spent = same ? dem->field[1] + ns : ns;
265 put(c, f->o.demand, 0, calls);
266 put(c, f->o.demand, 1, spent);
267 if (calls % RX_OMEGA_WINDOW == 0) {
268 put(c, f->o.demand, 2, dem->field[2] + 1);
269 put(c, f->o.demand, 3, spent / calls);
270 }
271 if (!same) {
272 put(c, f->o.demand, 4, M);
273 put(c, f->o.demand, 5, N);
274 }
275 put(c, f->o.demand, 6, used);
276 put(c, f->o.demand, 7, dem->field[7] + 1);
277 return 0;
278}
279
280/* ---- omega.watch: cost evidence crosses the threshold ---- */
281
282static int fn_watch(RxCtx *c) {
283 RxOmegaFaculty *f = c->user;
284 const RxSnapshotDep *dem = in_of(c, f->o.demand);
285 const RxSnapshotDep *s = in_of(c, f->o.search);
286 if (!dem || !s) return -1;
287 uint64_t M = dem->field[4], N = dem->field[5];
288 if (M == 0 || N == 0) return 0;
289 if (s->field[0] != 0 && s->field[1] == M && s->field[2] == N) return 0; /* already searched */
290 if (dem->field[0] < f->cfg.hot_calls || dem->field[1] < f->cfg.hot_ns) return 0; /* not hot */
291 put(c, f->o.search, 0, s->field[0] + 1);
292 put(c, f->o.search, 1, M);
293 put(c, f->o.search, 2, N);
294 put(c, f->o.search, 3, dem->field[3]);
295 put(c, f->o.search, 4, dem->field[0]);
296 put(c, f->o.search, 5, dem->field[1]);
297 return 0;
298}
299
300/* ---- omega.synthesize.k: Omega emits one candidate ---- */
301
302static int patch_word(uint8_t *code, size_t len, uint32_t word, const uint8_t insn[4]) {
303 if ((size_t)(word + 1) * 4u > len) return -1;
304 memcpy(code + (size_t)word * 4u, insn, 4);
305 return 0;
306}
307
308static int encode_one(uint8_t out[4], int which, int32_t imm) {
309 size_t pos = 0;
310 switch (which) {
311 case 0: return aarch64_emit_cbz(out, &pos, 4, true, REG_XZR, imm); /* always taken */
312 case 1: return aarch64_emit_movz(out, &pos, 4, true, REG_X16, 0, 0); /* x16 = 0 */
313 case 2: return aarch64_emit_ldr_x_post(out, &pos, 4, REG_X10, REG_X16, 8); /* load [0] */
314 }
315 return -1;
316}
317
318/* Omega's unroll4_dual with one test fault applied. Word numbers are from
319 * the emitter's own layout in omega_matvec.c. */
320static int defect_bytes(RxOmegaFaculty *f, RxOmegaDefect d, MatVecRealization *base,
321 SemanticId *claimed, uint8_t *code, size_t *len) {
322 memcpy(code, base->realization.code_bytes, base->realization.code_len);
323 *len = base->realization.code_len;
324 uint8_t insn[4];
325 /* Word 31 is "cbz x15, row_end": skipping it always drops N % 4. */
326 if (encode_one(insn, 0, 8) != 0 || patch_word(code, *len, 31, insn) != 0) return -1;
327 RealizationObject r;
328 real_object(f, code, *len, &r);
329 if (omega_realize_compute_triple_id(&f->spec.spec_id, &f->machine.machine_id, &r, claimed) != 0)
330 return -1;
331 if (d == RX_OMEGA_DEFECT_SKIP_REMAINDER) return 0;
332 if (d == RX_OMEGA_DEFECT_TAMPER) {
333 /* Keep the identity of the bytes above; store different bytes. */
334 uint8_t mov[4];
335 if (encode_one(mov, 1, 0) != 0 || patch_word(code, *len, 2, mov) != 0) return -1;
336 return 0;
337 }
338 if (d == RX_OMEGA_DEFECT_CRASH) {
339 uint8_t mov[4], ld[4];
340 if (encode_one(mov, 1, 0) != 0 || encode_one(ld, 2, 0) != 0 ||
341 patch_word(code, *len, 0, mov) != 0 || patch_word(code, *len, 1, ld) != 0)
342 return -1;
343 real_object(f, code, *len, &r);
344 return omega_realize_compute_triple_id(&f->spec.spec_id, &f->machine.machine_id, &r,
345 claimed);
346 }
347 return -1;
348}
349
350static int fn_synthesize(RxCtx *c) {
351 struct { RxOmegaFaculty *f; uint32_t k; } *sl = c->user;
352 RxOmegaFaculty *f = sl->f;
353 uint32_t k = sl->k;
354 const RxSnapshotDep *s = in_of(c, f->o.search);
355 const RxSnapshotDep *cand = in_of(c, f->o.candidate[k]);
356 if (!s || !cand) return -1;
357 uint64_t epoch = s->field[0];
358 if (epoch == 0 || cand->field[0] == epoch) return 0;
359
360 MatVecLivingKernel kernel;
361 memset(&kernel, 0, sizeof(kernel));
362 kernel.spec = f->spec;
363 kernel.machine = &f->machine;
364 MatVecRealization mv;
365 uint32_t kind = k;
366 int rc;
367 if (k < MATVEC_REALIZATION_COUNT)
368 rc = omega_matvec_synthesize(&kernel, (MatVecRealizationKind)k, &mv);
369 else if (k == OMEGA_MATVEC_KIND_QUAD4)
370 rc = omega_matvec_synthesize_quad4(&f->spec, &f->machine, &mv);
371 else
372 rc = omega_matvec_synthesize(&kernel, MATVEC_REALIZATION_UNROLL4_DUAL, &mv);
373 if (rc != 0) return -1;
374
375 SemanticId id = mv.realization_id;
376 uint8_t code[AARCH64_MAX_CODE_BYTES];
377 size_t len = mv.realization.code_len;
378 memcpy(code, mv.realization.code_bytes, len);
379 if (kind == KIND_DEFECT && defect_bytes(f, f->cfg.defect, &mv, &id, code, &len) != 0)
380 return -1;
381 if (store_put(f, &id, kind, code, len) != 0) return -1;
382
383 put(c, f->o.candidate[k], 0, epoch);
384 put(c, f->o.candidate[k], 1, kind);
385 for (uint32_t i = 0; i < 4; i++) put(c, f->o.candidate[k], 2 + i, id_word(&id, i));
386 put(c, f->o.candidate[k], 6, len);
387 put(c, f->o.candidate[k], 7, RX_OMEGA_SYNTHESIZED);
388 return 0;
389}
390
391/* ---- omega.verify.k: V0 structure, then differential in a sandbox ---- */
392
393typedef struct {
394 uint32_t checks;
395 uint32_t fails;
396 uint32_t bounds;
397 uint32_t done;
398} SandboxReport;
399
400#define CANARY 0xC0FFEE0DDBA11AD5ull
401#define GUARD_WORDS 8u
402#define VMAX_ELEMS (64u * 257u)
403
404typedef struct {
405 uint64_t *A, *A0, *x, *x0, *y, *yref;
406} Buffers;
407
408static int buffers_alloc(Buffers *b) {
409 memset(b, 0, sizeof(*b));
410 b->A = malloc(VMAX_ELEMS * sizeof(uint64_t));
411 b->A0 = malloc(VMAX_ELEMS * sizeof(uint64_t));
412 b->x = malloc(512 * sizeof(uint64_t));
413 b->x0 = malloc(512 * sizeof(uint64_t));
414 b->y = malloc((512 + GUARD_WORDS) * sizeof(uint64_t));
415 b->yref = malloc(512 * sizeof(uint64_t));
416 return b->A && b->A0 && b->x && b->x0 && b->y && b->yref ? 0 : -1;
417}
418
419static void buffers_free(Buffers *b) {
420 free(b->A); free(b->A0); free(b->x); free(b->x0); free(b->y); free(b->yref);
421}
422
423/* Edge values that stress wrap-around and the zero/one identities. */
424static void fill_edge(uint64_t seed, uint64_t *A, uint64_t *x, uint32_t M, uint32_t N) {
425 static const uint64_t edge[] = { 0, 1, 2, UINT64_MAX, UINT64_MAX - 1, 1ull << 63,
426 (1ull << 32) - 1, 1ull << 32 };
427 uint64_t s = seed;
428 for (size_t i = 0; i < (size_t)M * N; i++) A[i] = edge[mix(&s) % 8];
429 for (uint32_t j = 0; j < N; j++) x[j] = edge[mix(&s) % 8];
430}
431
432/* Runs in the child only. Never returns to the caller's stack frame. */
433static void sandbox_body(MatVecFn fn, Buffers *b, uint64_t regime_m, uint64_t regime_n,
434 uint64_t seed, int wfd) {
435 static const uint32_t Ms[] = { 1, 2, 3, 4, 5, 7, 16 };
436 static const uint32_t Ns[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 31, 64 };
437 SandboxReport rep = { 0, 0, 0, 0 };
438 uint32_t shapes[128][2];
439 uint32_t n = 0;
440 for (uint32_t i = 0; i < sizeof Ms / sizeof Ms[0]; i++)
441 for (uint32_t j = 0; j < sizeof Ns / sizeof Ns[0]; j++) {
442 shapes[n][0] = Ms[i];
443 shapes[n][1] = Ns[j];
444 n++;
445 }
446 shapes[n][0] = 64; shapes[n][1] = 256; n++;
447 shapes[n][0] = 33; shapes[n][1] = 257; n++;
448 if (regime_m && regime_n && regime_m * regime_n <= VMAX_ELEMS && regime_m <= 512 &&
449 regime_n <= 512) {
450 shapes[n][0] = (uint32_t)regime_m;
451 shapes[n][1] = (uint32_t)regime_n;
452 n++;
453 }
454 uint64_t s = seed;
455 for (uint32_t t = 0; t < n; t++) {
456 uint32_t M = shapes[t][0], N = shapes[t][1];
457 for (int variant = 0; variant < 3; variant++) {
458 uint64_t cs = mix(&s);
459 if (variant == 1) fill_edge(cs, b->A, b->x, M, N);
460 else rx_omega_fill(cs, b->A, b->x, M, N);
461 memcpy(b->A0, b->A, (size_t)M * N * sizeof(uint64_t));
462 memcpy(b->x0, b->x, (size_t)N * sizeof(uint64_t));
463 for (uint32_t i = 0; i < M + GUARD_WORDS; i++) b->y[i] = CANARY;
464 omega_matvec_reference(b->A0, b->x0, b->yref, M, N);
465 fn(b->A, b->x, b->y, M, N);
466 rep.checks++;
467 if (memcmp(b->y, b->yref, (size_t)M * sizeof(uint64_t)) != 0) rep.fails++;
468 int oob = 0;
469 for (uint32_t i = M; i < M + GUARD_WORDS; i++) oob |= b->y[i] != CANARY;
470 oob |= memcmp(b->A, b->A0, (size_t)M * N * sizeof(uint64_t)) != 0;
471 oob |= memcmp(b->x, b->x0, (size_t)N * sizeof(uint64_t)) != 0;
472 rep.checks++;
473 if (oob) { rep.fails++; rep.bounds++; }
474 }
475 }
476 rep.done = 1;
477 ssize_t wr = write(wfd, &rep, sizeof rep);
478 _exit(wr == (ssize_t)sizeof rep ? 0 : 3);
479}
480
481static int sandbox_run(RxOmegaFaculty *f, void *page, uint64_t M, uint64_t N, uint64_t seed,
482 SandboxReport *rep, uint32_t *why) {
483 Buffers b;
484 if (buffers_alloc(&b) != 0) { buffers_free(&b); return -1; }
485 int fds[2];
486 if (pipe(fds) != 0) { buffers_free(&b); return -1; }
487 union { void *p; MatVecFn fn; } u;
488 u.p = page;
489 pid_t pid = fork();
490 if (pid < 0) {
491 close(fds[0]); close(fds[1]);
492 buffers_free(&b);
493 return -1;
494 }
495 if (pid == 0) {
496 close(fds[0]);
497 signal(SIGPIPE, SIG_DFL);
498 sandbox_body(u.fn, &b, M, N, seed, fds[1]);
499 }
500 close(fds[1]);
501 pthread_mutex_lock(&f->mu);
502 f->sandbox_runs++;
503 pthread_mutex_unlock(&f->mu);
504
505 memset(rep, 0, sizeof(*rep));
506 size_t got = 0;
507 uint64_t deadline = now_ns() + (uint64_t)f->cfg.sandbox_ms * 1000000ull;
508 int hung = 0;
509 while (got < sizeof(*rep)) {
510 uint64_t t = now_ns();
511 if (t >= deadline) { hung = 1; break; }
512 struct pollfd p = { fds[0], POLLIN, 0 };
513 int pr = poll(&p, 1, (int)((deadline - t) / 1000000ull) + 1);
514 if (pr < 0 && errno == EINTR) continue;
515 if (pr <= 0) continue;
516 ssize_t r = read(fds[0], (uint8_t *)rep + got, sizeof(*rep) - got);
517 if (r <= 0) break; /* child closed: crashed or exited early */
518 got += (size_t)r;
519 }
520 close(fds[0]);
521 if (hung) kill(pid, SIGKILL);
522 int status = 0;
523 while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
524 buffers_free(&b);
525
526 if (hung) { *why = RX_OMEGA_WHY_HUNG; return 1; }
527 if (WIFSIGNALED(status) || got != sizeof(*rep) || !rep->done ||
528 !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
529 pthread_mutex_lock(&f->mu);
530 f->sandbox_crashes++;
531 pthread_mutex_unlock(&f->mu);
532 *why = RX_OMEGA_WHY_CRASHED;
533 return 1;
534 }
535 if (rep->bounds) { *why = RX_OMEGA_WHY_BOUNDS; return 1; }
536 if (rep->fails) { *why = RX_OMEGA_WHY_DIFFERENTIAL; return 1; }
537 *why = RX_OMEGA_WHY_NONE;
538 return 0;
539}
540
541/* Identity, V0 structure, then the sandboxed differential on shape M x N.
542 * On a pass the store entry is marked verified for exactly the bytes that
543 * were checked. Returns the reason (RX_OMEGA_WHY_NONE on a pass), or -1 when
544 * the check itself could not run. */
545static int verify_stored(RxOmegaFaculty *f, const SemanticId *id, uint64_t M, uint64_t N,
546 uint32_t *out_checks, uint32_t *out_fails) {
547 RxOmegaRealization e;
548 int have = 0;
549 pthread_mutex_lock(&f->mu);
550 RxOmegaRealization *p = store_find_locked(f, id);
551 if (p) { e = *p; have = 1; }
552 pthread_mutex_unlock(&f->mu);
553
554 uint32_t why = RX_OMEGA_WHY_NONE, checks = 0, fails = 0;
555 if (!have) why = RX_OMEGA_WHY_UNKNOWN;
556
557 RealizationObject r;
558 if (why == RX_OMEGA_WHY_NONE) {
559 /* The bytes must be the ones the candidate names. */
560 SemanticId again;
561 real_object(f, e.code, e.code_len, &r);
562 checks++;
563 if (omega_realize_compute_triple_id(&f->spec.spec_id, &f->machine.machine_id, &r,
564 &again) != 0 || !ids_equal(&again, id)) {
565 fails++;
566 why = RX_OMEGA_WHY_IDENTITY;
567 }
568 }
569 if (why == RX_OMEGA_WHY_NONE) {
570 VerifyReport rep;
571 memset(&rep, 0, sizeof(rep));
572 r.realization_id = *id;
573 r.has_id = true;
574 int v0 = omega_verify_v0_structural(NULL, &r, &rep);
575 checks += rep.check_count;
576 fails += rep.fail_count;
577 if (v0 != 0 || !rep.passed) why = RX_OMEGA_WHY_STRUCTURE;
578 }
579 if (why == RX_OMEGA_WHY_NONE) {
580 SandboxReport sb;
581 int rc = sandbox_run(f, e.page, M, N, id_word(id, 0), &sb, &why);
582 if (rc < 0) return -1;
583 checks += sb.checks;
584 fails += sb.fails;
585 if (rc > 0 && why == RX_OMEGA_WHY_NONE) why = RX_OMEGA_WHY_CRASHED;
586 }
587 if (why == RX_OMEGA_WHY_NONE) {
588 pthread_mutex_lock(&f->mu);
589 RxOmegaRealization *q = store_find_locked(f, id);
590 if (q && q->code_len == e.code_len && memcmp(q->code, e.code, e.code_len) == 0)
591 q->verified = 1;
592 else why = RX_OMEGA_WHY_IDENTITY;
593 pthread_mutex_unlock(&f->mu);
594 }
595 *out_checks = checks;
596 *out_fails = fails;
597 return (int)why;
598}
599
600static int fn_verify(RxCtx *c) {
601 struct { RxOmegaFaculty *f; uint32_t k; } *sl = c->user;
602 RxOmegaFaculty *f = sl->f;
603 uint32_t k = sl->k;
604 const RxSnapshotDep *cand = in_of(c, f->o.candidate[k]);
605 const RxSnapshotDep *v = in_of(c, f->o.verdict[k]);
606 const RxSnapshotDep *s = in_of(c, f->o.search);
607 if (!cand || !v || !s) return -1;
608 uint64_t epoch = cand->field[0];
609 if (cand->field[7] != RX_OMEGA_SYNTHESIZED || epoch == 0 || v->field[0] == epoch) return 0;
610
611 SemanticId id;
612 id_from_words(&id, &cand->field[2]);
613 uint32_t checks = 0, fails = 0;
614 int why = verify_stored(f, &id, s->field[1], s->field[2], &checks, &fails);
615 if (why < 0) return -1;
616
617 put(c, f->o.verdict[k], 0, epoch);
618 put(c, f->o.verdict[k], 1, why == RX_OMEGA_WHY_NONE ? RX_OMEGA_PASSED : RX_OMEGA_REFUSED);
619 put(c, f->o.verdict[k], 2, checks);
620 put(c, f->o.verdict[k], 3, fails);
621 put(c, f->o.verdict[k], 4, id_word(&id, 0));
622 put(c, f->o.verdict[k], 5, (uint64_t)why);
623 return 0;
624}
625
626/* ---- omega.measure.k: benchmark a verified candidate against the incumbent ---- */
627
628#define ROUNDS 7u
629
630static uint64_t time_calls(const RxOmegaFaculty *f, MatVecFn fn, uint32_t kind,
631 const uint64_t *A, const uint64_t *x, uint64_t *y,
632 uint32_t M, uint32_t N, uint32_t iters) {
633 uint64_t t0 = cost_ns();
634 for (uint32_t i = 0; i < iters; i++) {
635 if (fn) fn(A, x, y, M, N);
636 else omega_matvec_reference(A, x, y, M, N);
637 }
638 uint64_t ns = cost_ns() - t0;
639 if (f->cfg.cost_ns)
640 ns = f->cfg.cost_ns(f->cfg.cost_ctx, fn ? kind : RX_OMEGA_COST_REFERENCE, M, N, iters);
641 return ns;
642}
643
644static int fn_measure(RxCtx *c) {
645 struct { RxOmegaFaculty *f; uint32_t k; } *sl = c->user;
646 RxOmegaFaculty *f = sl->f;
647 uint32_t k = sl->k;
648 const RxSnapshotDep *v = in_of(c, f->o.verdict[k]);
649 const RxSnapshotDep *cand = in_of(c, f->o.candidate[k]);
650 const RxSnapshotDep *s = in_of(c, f->o.search);
651 const RxSnapshotDep *m = in_of(c, f->o.measure[k]);
652 if (!v || !cand || !s || !m) return -1;
653 uint64_t epoch = v->field[0];
654 if (epoch == 0 || epoch != cand->field[0] || epoch != s->field[0] || m->field[0] == epoch)
655 return 0;
656
657 SemanticId id;
658 id_from_words(&id, &cand->field[2]);
659 uint64_t cps = 0, rps = 0, state = RX_OMEGA_DECLINED;
660 uint32_t kind = RX_OMEGA_COST_REFERENCE;
661 MatVecFn fn = v->field[1] == RX_OMEGA_PASSED ? verified_fn(f, &id, &kind) : NULL;
662 uint32_t M = (uint32_t)s->field[1], N = (uint32_t)s->field[2];
663 if (fn && shape_ok(M, N)) {
664 uint64_t *A = malloc((size_t)M * N * sizeof(uint64_t));
665 uint64_t *x = malloc((size_t)N * sizeof(uint64_t));
666 uint64_t *y = malloc((size_t)M * sizeof(uint64_t));
667 uint64_t *yr = malloc((size_t)M * sizeof(uint64_t));
668 if (!A || !x || !y || !yr) { free(A); free(x); free(y); free(yr); return -1; }
669 rx_omega_fill(epoch * 0x9e37u + k, A, x, M, N);
670 fn(A, x, y, M, N);
671 omega_matvec_reference(A, x, yr, M, N);
672 if (memcmp(y, yr, (size_t)M * sizeof(uint64_t)) == 0) {
673 /* Size one round to about 200 microseconds of the reference. */
674 uint64_t one = time_calls(f, NULL, kind, A, x, yr, M, N, 1);
675 uint32_t iters = one ? (uint32_t)(200000u / one) : 1000u;
676 if (iters < 4) iters = 4;
677 if (iters > 20000) iters = 20000;
678 uint64_t best_c = UINT64_MAX, best_r = UINT64_MAX;
679 for (uint32_t r = 0; r < ROUNDS; r++) {
680 uint64_t tc = time_calls(f, fn, kind, A, x, y, M, N, iters);
681 uint64_t tr = time_calls(f, NULL, kind, A, x, yr, M, N, iters);
682 if (tc < best_c) best_c = tc;
683 if (tr < best_r) best_r = tr;
684 }
685 cps = best_c * 1000u / iters;
686 rps = best_r * 1000u / iters;
687 state = RX_OMEGA_MEASURED;
688 pthread_mutex_lock(&f->mu);
689 RxOmegaRealization *e = store_find_locked(f, &id);
690 if (e) e->parent_runs += (uint64_t)iters * ROUNDS;
691 pthread_mutex_unlock(&f->mu);
692 }
693 free(A); free(x); free(y); free(yr);
694 }
695 put(c, f->o.measure[k], 0, epoch);
696 put(c, f->o.measure[k], 1, state);
697 put(c, f->o.measure[k], 2, cps);
698 put(c, f->o.measure[k], 3, rps);
699 for (uint32_t i = 0; i < 4; i++) put(c, f->o.measure[k], 4 + i, id_word(&id, i));
700 return 0;
701}
702
703/* ---- omega.select: record the fastest verified realization ---- */
704
705static int fn_select(RxCtx *c) {
706 RxOmegaFaculty *f = c->user;
707 const RxSnapshotDep *s = in_of(c, f->o.search);
708 const RxSnapshotDep *sel = in_of(c, f->o.selection);
709 if (!s || !sel) return -1;
710 uint64_t epoch = s->field[0];
711 if (epoch == 0 || sel->field[0] == epoch) return 0;
712 if (f->require_experiment_evidence && epoch > 1) {
713 const RxSnapshotDep *e = in_of(c, f->experiment_evidence);
714 if (!e || e->field[0] != epoch || e->field[2] != 1) return 0;
715 }
716 const RxSnapshotDep *best = NULL;
717 uint64_t ref_ps = 0;
718 for (uint32_t k = 0; k < f->cfg.n_slots; k++) {
719 const RxSnapshotDep *m = in_of(c, f->o.measure[k]);
720 if (!m) return -1;
721 if (m->field[0] != epoch) return 0; /* not every candidate has reported */
722 if (m->field[1] != RX_OMEGA_MEASURED) continue;
723 if (!ref_ps || m->field[3] < ref_ps) ref_ps = m->field[3];
724 /* Beat the incumbent measured in the same rounds by the margin. */
725 if (m->field[2] * 100u > m->field[3] * (100u - f->cfg.margin_pct)) continue;
726 if (!best || m->field[2] < best->field[2]) best = m;
727 }
728 put(c, f->o.selection, 0, epoch);
729 for (uint32_t i = 0; i < 4; i++) put(c, f->o.selection, 1 + i, best ? best->field[4 + i] : 0);
730 put(c, f->o.selection, 5, best ? best->field[2] : ref_ps);
731 put(c, f->o.selection, 6, rx_omega_regime(s->field[1], s->field[2]));
732 put(c, f->o.selection, 7, best ? best->field[3] : ref_ps);
733 return 0;
734}
735
736/* ---- omega.reconsider: a published plan reopens a regime (R11) ----
737 *
738 * Omega is not asked. It observes a plan object and decides for itself:
739 * a regime it can realize, no search already in flight. The new epoch runs
740 * the ordinary synthesize -> verify -> measure -> select chain, measured on
741 * the cores the world runs on now. */
742static int fn_reconsider(RxCtx *c) {
743 RxOmegaFaculty *f = c->user;
744 const RxSnapshotDep *p = in_of(c, f->inquiry);
745 const RxSnapshotDep *s = in_of(c, f->o.search);
746 const RxSnapshotDep *sel = in_of(c, f->o.selection);
747 if (!p || !s || !sel) return -1;
748 uint64_t seq = p->field[0];
749 if (seq == 0 || seq == s->field[6]) return 0;
750 if (s->field[0] != sel->field[0]) return 0; /* a search is in flight; its selection re-wakes us */
751 uint64_t M = p->field[2] >> 32, N = p->field[2] & 0xffffffffu;
752 if (p->field[1] != RX_OMEGA_INQ_RESEARCH || !shape_ok(M, N)) {
753 put(c, f->o.search, 6, seq); /* declined, and recorded as seen */
754 return 0;
755 }
756 put(c, f->o.search, 0, s->field[0] + 1);
757 put(c, f->o.search, 1, M);
758 put(c, f->o.search, 2, N);
759 put(c, f->o.search, 6, seq);
760 return 0;
761}
762
763/* ---- setup ---- */
764
765void rx_omega_default_config(RxOmegaConfig *cfg) {
766 memset(cfg, 0, sizeof(*cfg));
767 cfg->n_slots = OMEGA_MATVEC_KIND_QUAD4 + 1u;
768 cfg->defect = RX_OMEGA_DEFECT_NONE;
769 cfg->hot_calls = 64;
770 cfg->hot_ns = 1000000;
771 cfg->margin_pct = 5;
772 cfg->sandbox_ms = 5000;
773}
774
775int rx_omega_create_objects(RxOmegaFaculty *f, RxWorld *w, const RxOmegaConfig *cfg) {
776 memset(f, 0, sizeof(*f));
777 f->w = w;
778 f->cfg = *cfg;
779 if (cfg->defect != RX_OMEGA_DEFECT_NONE) f->cfg.n_slots = RX_OMEGA_SLOTS;
780 if (f->cfg.n_slots < 1 || f->cfg.n_slots > RX_OMEGA_SLOTS) return RX_ERR_ARG;
781 if (pthread_mutex_init(&f->mu, NULL) != 0) return RX_ERR_ARG;
782 if (omega_machine_build_dgx_spark(&f->machine) != 0) return RX_ERR_ARG;
783 if (omega_matvec_spec_init(&f->spec, "rx_omega_matvec", RX_OMEGA_MAX_ELEMS,
784 RX_OMEGA_MAX_ELEMS) != 0)
785 return RX_ERR_ARG;
786 uint64_t z[RX_MAX_FIELDS] = { 0 };
787#define MK(ref, type, res) do { \
788 int rc_ = rx_world_create(w, (type), RX_PERSIST_RESIDENT, \
789 RX_OMEGA_RES_BASE + (res), z, &(ref)); \
790 if (rc_ != RX_OK) return rc_; \
791 } while (0)
792 MK(f->o.request, RX_OT_REQUEST, RX_OMEGA_RES_REQUEST);
793 MK(f->o.demand, RX_OT_DEMAND, RX_OMEGA_RES_DEMAND);
794 MK(f->o.result, RX_OT_RESULT, RX_OMEGA_RES_RESULT);
795 MK(f->o.search, RX_OT_SEARCH, RX_OMEGA_RES_SEARCH);
796 MK(f->o.selection, RX_OT_SELECTION, RX_OMEGA_RES_SELECTION);
797 for (uint32_t k = 0; k < f->cfg.n_slots; k++) {
798 MK(f->o.candidate[k], RX_OT_CANDIDATE, RX_OMEGA_RES_CANDIDATE0 + k);
799 MK(f->o.verdict[k], RX_OT_VERDICT, RX_OMEGA_RES_VERDICT0 + k);
800 MK(f->o.measure[k], RX_OT_MEASURE, RX_OMEGA_RES_MEASURE0 + k);
801 }
802#undef MK
803 return RX_OK;
804}
805
806static uint32_t res_index(const RxWorld *w, RxObjRef o) {
807 return (uint32_t)(w->objects[o.id].resource - RX_OMEGA_RES_BASE);
808}
809
810typedef struct {
811 RxReactionDesc d;
812 const RxCapRef *table;
813} Builder;
814
815static void need(Builder *b, RxWorld *w, RxObjRef o, uint32_t rights) {
816 uint64_t res = w->objects[o.id].resource;
817 for (uint32_t i = 0; i < b->d.n_caps; i++)
818 if (b->d.caps[i].resource == res) { b->d.caps[i].rights |= rights; return; }
819 b->d.caps[b->d.n_caps++] = (RxCapNeed){ b->table[res_index(w, o)], res, rights };
820}
821
822static void begin(Builder *b, const char *name, uint32_t faculty, uint32_t subject,
823 uint32_t prio, RxFn fn, void *user, const RxCapRef *table) {
824 memset(b, 0, sizeof(*b));
825 b->d.name = name;
826 b->d.faculty = faculty;
827 b->d.subject = subject;
828 b->d.priority = prio;
829 b->d.fn = fn;
830 b->d.user = user;
831 b->table = table;
832}
833
834static void trig(Builder *b, RxWorld *w, RxObjRef o, uint64_t mask) {
835 b->d.triggers[b->d.n_triggers++] = (RxDep){ o, mask };
836 need(b, w, o, RX_RIGHT_READ);
837}
838
839static void rd(Builder *b, RxWorld *w, RxObjRef o) {
840 b->d.reads[b->d.n_reads++] = (RxDep){ o, RX_ALL_FIELDS };
841 need(b, w, o, RX_RIGHT_READ);
842}
843
844static void wr(Builder *b, RxWorld *w, RxObjRef o) {
845 b->d.writes[b->d.n_writes++] = (RxDep){ o, RX_ALL_FIELDS };
846 need(b, w, o, RX_RIGHT_READ | RX_RIGHT_WRITE);
847}
848
849static const char *const synth_names[RX_OMEGA_SLOTS] = {
850 "omega.synthesize.0", "omega.synthesize.1", "omega.synthesize.2", "omega.synthesize.3",
851 "omega.synthesize.4" };
852static const char *const verify_names[RX_OMEGA_SLOTS] = {
853 "omega.verify.0", "omega.verify.1", "omega.verify.2", "omega.verify.3", "omega.verify.4" };
854static const char *const measure_names[RX_OMEGA_SLOTS] = {
855 "omega.measure.0", "omega.measure.1", "omega.measure.2", "omega.measure.3", "omega.measure.4" };
856
857int rx_omega_register(RxOmegaFaculty *f, const RxOmegaCaps *caps) {
858 RxWorld *w = f->w;
859 Builder b;
860 int rc;
861
862 /* The production path. It stands for any consumer of the operation;
863 * it is not labelled as AIEN. */
864 begin(&b, "workload.matvec.serve", RX_FACULTY_EXTERNAL, RX_OMEGA_SUBJ_SERVE,
865 RX_PRIO_INTERACTIVE, fn_serve, f, caps->serve);
866 trig(&b, w, f->o.request, RX_ALL_FIELDS);
867 if (f->serve_from_record) {
868 b.d.reads[b.d.n_reads++] = (RxDep){ f->serve_record, RX_ALL_FIELDS };
869 b.d.caps[b.d.n_caps++] = (RxCapNeed){ f->serve_record_cap,
870 w->objects[f->serve_record.id].resource, RX_RIGHT_READ };
871 } else {
872 rd(&b, w, f->o.selection);
873 }
874 rd(&b, w, f->o.demand);
875 wr(&b, w, f->o.demand);
876 wr(&b, w, f->o.result);
877 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_serve)) != RX_OK) return rc;
878
879 begin(&b, "omega.watch", RX_FACULTY_OMEGA, RX_OMEGA_SUBJ_OMEGA, RX_PRIO_FOREGROUND,
880 fn_watch, f, caps->omega);
881 trig(&b, w, f->o.demand, RX_FIELD(2));
882 rd(&b, w, f->o.search);
883 wr(&b, w, f->o.search);
884 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_watch)) != RX_OK) return rc;
885
886 for (uint32_t k = 0; k < f->cfg.n_slots; k++) {
887 f->slot[k].f = f;
888 f->slot[k].k = k;
889
890 begin(&b, synth_names[k], RX_FACULTY_OMEGA, RX_OMEGA_SUBJ_OMEGA, RX_PRIO_LEARNING,
891 fn_synthesize, &f->slot[k], caps->omega);
892 trig(&b, w, f->o.search, RX_FIELD(0));
893 rd(&b, w, f->o.candidate[k]);
894 wr(&b, w, f->o.candidate[k]);
895 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_synth[k])) != RX_OK) return rc;
896
897 begin(&b, verify_names[k], RX_FACULTY_OMEGA, RX_OMEGA_SUBJ_OMEGA, RX_PRIO_LEARNING,
898 fn_verify, &f->slot[k], caps->omega);
899 trig(&b, w, f->o.candidate[k], RX_ALL_FIELDS);
900 rd(&b, w, f->o.search);
901 rd(&b, w, f->o.verdict[k]);
902 wr(&b, w, f->o.verdict[k]);
903 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_verify[k])) != RX_OK) return rc;
904
905 begin(&b, measure_names[k], RX_FACULTY_OMEGA, RX_OMEGA_SUBJ_OMEGA, RX_PRIO_LEARNING,
906 fn_measure, &f->slot[k], caps->omega);
907 trig(&b, w, f->o.verdict[k], RX_ALL_FIELDS);
908 rd(&b, w, f->o.candidate[k]);
909 rd(&b, w, f->o.search);
910 rd(&b, w, f->o.measure[k]);
911 wr(&b, w, f->o.measure[k]);
912 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_measure[k])) != RX_OK) return rc;
913 }
914
915 begin(&b, "omega.select", RX_FACULTY_OMEGA, RX_OMEGA_SUBJ_OMEGA, RX_PRIO_LEARNING,
916 fn_select, f, caps->omega);
917 /* Every epoch republishes the whole selection, even when the same
918 * realization wins again: the proposal checks that the identity words
919 * and the epoch were written by one commit, so unchanged words must
920 * still carry this epoch's author (OMEGA-R14-HOST, 2026-10-20). */
921 b.d.stamp_proposed = true;
922 for (uint32_t k = 0; k < f->cfg.n_slots; k++) trig(&b, w, f->o.measure[k], RX_ALL_FIELDS);
923 if (f->require_experiment_evidence) {
924 b.d.triggers[b.d.n_triggers++] = (RxDep){ f->experiment_evidence, RX_FIELD(0) };
925 b.d.caps[b.d.n_caps++] = (RxCapNeed){ f->experiment_evidence_cap,
926 w->objects[f->experiment_evidence.id].resource, RX_RIGHT_READ };
927 }
928 rd(&b, w, f->o.search);
929 rd(&b, w, f->o.selection);
930 wr(&b, w, f->o.selection);
931 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_select)) != RX_OK) return rc;
932 return RX_OK;
933}
934
935int rx_omega_require_evidence(RxOmegaFaculty *f, RxObjRef evidence, RxCapRef read_cap) {
936 if (!f || !f->w || evidence.id >= RX_MAX_OBJECTS ||
937 !f->w->objects[evidence.id].live ||
938 f->w->objects[evidence.id].generation != evidence.generation ||
939 read_cap.cap_id == UINT32_MAX) return RX_ERR_ARG;
940 f->experiment_evidence = evidence;
941 f->experiment_evidence_cap = read_cap;
942 f->require_experiment_evidence = 1;
943 return RX_OK;
944}
945
946int rx_omega_register_reconsider(RxOmegaFaculty *f, RxObjRef plan, RxCapRef read_cap,
947 RxCapRef search_cap, RxCapRef selection_cap) {
948 RxWorld *w = f->w;
949 f->inquiry = plan;
950 RxReactionDesc d;
951 memset(&d, 0, sizeof(d));
952 d.name = "omega.reconsider";
953 d.faculty = RX_FACULTY_OMEGA;
954 d.subject = RX_OMEGA_SUBJ_OMEGA;
955 d.priority = RX_PRIO_LEARNING;
956 d.fn = fn_reconsider;
957 d.user = f;
958 d.n_triggers = 2;
959 d.triggers[0] = (RxDep){ plan, RX_FIELD(0) };
960 d.triggers[1] = (RxDep){ f->o.selection, RX_FIELD(0) };
961 d.n_reads = 1;
962 d.reads[0] = (RxDep){ f->o.search, RX_ALL_FIELDS };
963 d.n_writes = 1;
964 d.writes[0] = (RxDep){ f->o.search, RX_ALL_FIELDS };
965 d.n_caps = 3;
966 d.caps[0] = (RxCapNeed){ read_cap, w->objects[plan.id].resource, RX_RIGHT_READ };
967 d.caps[1] = (RxCapNeed){ search_cap, w->objects[f->o.search.id].resource,
968 RX_RIGHT_READ | RX_RIGHT_WRITE };
969 d.caps[2] = (RxCapNeed){ selection_cap, w->objects[f->o.selection.id].resource, RX_RIGHT_READ };
970 return rx_world_add_reaction_keyed(w, f->keys, &d, &f->r_reconsider);
971}
972
973void rx_omega_destroy(RxOmegaFaculty *f) {
974 for (uint32_t i = 0; i < f->n_store; i++)
975 if (f->store[i].page) munmap(f->store[i].page, PAGE);
976 f->n_store = 0;
977 pthread_mutex_destroy(&f->mu);
978}
979
980int rx_omega_serve_from(RxOmegaFaculty *f, RxObjRef record, RxCapRef read_cap) {
981 if (!f || !f->w || record.id >= RX_MAX_OBJECTS || !f->w->objects[record.id].live ||
982 f->w->objects[record.id].generation != record.generation ||
983 read_cap.cap_id == UINT32_MAX) return RX_ERR_ARG;
984 f->serve_record = record;
985 f->serve_record_cap = read_cap;
986 f->serve_from_record = 1;
987 return RX_OK;
988}
989
990int rx_omega_identity_of(const RxOmegaFaculty *f, const uint8_t *code, size_t len,
991 SemanticId *out) {
992 if (!f || !code || !out || len == 0 || len > AARCH64_MAX_CODE_BYTES) return -1;
993 RealizationObject r;
994 real_object(f, code, len, &r);
995 return omega_realize_compute_triple_id(&f->spec.spec_id, &f->machine.machine_id, &r, out);
996}
997
998int rx_omega_readmit(RxOmegaFaculty *f, const uint8_t *code, size_t len, uint32_t kind,
999 uint64_t regime, SemanticId *out_id, uint32_t *out_why) {
1000 if (!f || !code || !out_id || !out_why || kind >= f->cfg.n_slots) return -1;
1001 uint64_t M = regime >> 32, N = regime & 0xffffffffu;
1002 if (!shape_ok(M, N) || rx_omega_identity_of(f, code, len, out_id) != 0) return -1;
1003 /* The store holds one set of bytes per identity. Bytes that hash to
1004 * this identity can only be these bytes. */
1005 if (store_put(f, out_id, kind, code, len) != 0) {
1006 *out_why = RX_OMEGA_WHY_IDENTITY;
1007 return 1;
1008 }
1009 uint32_t checks = 0, fails = 0;
1010 int why = verify_stored(f, out_id, M, N, &checks, &fails);
1011 if (why < 0) return -1;
1012 *out_why = (uint32_t)why;
1013 return why == RX_OMEGA_WHY_NONE ? 0 : 1;
1014}