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_i11_wake_merge.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_i11_wake_merge.c` 280 lines ยท 12429 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 /* I11: a dependent popped while its upstream writer is still computing must 18 * not commit early and then again (omega.risk behind aien.hypothesis, found 19 * by the causal replay gate, omega PR #176). 20 * 21 * World: sensor{temp, humidity} -> aien.hypothesis -> belief, and omega.risk 22 * triggered by sensor humidity and belief (the replay world's shape). 23 * 24 * The bad interleaving is forced, not hoped for: with 2 workers, 25 * aien.hypothesis's fn does not return until the second worker has taken 26 * omega.risk off the ready ring and either held it (fixed runtime) or run and 27 * committed it (defect). Each stimulus therefore takes the same path every 28 * run. Expected, as with 1 worker: per stimulus exactly one omega.risk 29 * crumb, a COMMIT, no INVALIDATED crumb; when both fields change its wake is 30 * the hypothesis commit with one merged wake, and it saw the new belief. 31 * The 1-worker and 2-worker crumb signatures must be identical. 32 * 33 * Mutation: built with -DRX_WORLD_MUTATE_NO_UPSTREAM_HOLD the runtime has no 34 * hold and this test must FAIL (make test-i11-wake-merge checks both). */ 35 #include "runtime/rx_caproot.h" 36 #include "runtime/rx_world.h" 37 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 45 enum { SUBJ_AIEN = 1, SUBJ_OMEGA = 2, SUBJ_EXTERNAL = 100, ISSUER_AEGIS_POLICY = 3 }; 46 enum { RES_SENSOR = 0x10, RES_BELIEF = 0x20, RES_RISK = 0x40 }; 47 enum { F_TEMP = 0, F_HUMIDITY = 1 }; 48 enum { STIMULI = 24 }; 49 50 static int g_fail; 51 #define CHECK(c, ...) do { if (!(c)) { g_fail = 1; fprintf(stderr, "FAIL: " __VA_ARGS__); fputc('\n', stderr); } } while (0) 52 53 typedef struct { 54 RxCapRoot root; 55 RxCapAdmin admin; 56 RxWorld w; 57 RxObjRef sensor, belief, risk; 58 uint32_t id_hyp, id_risk; 59 RxCapRef c_ext; 60 /* gate for the forced interleaving (2-worker run, both fields changed) */ 61 int gate_on; 62 uint64_t risk_commits0; 63 int gate_timeout; 64 } Env; 65 66 static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef o) { 67 for (uint32_t i = 0; i < c->n_in; i++) 68 if (c->in[i].obj.id == o.id) return &c->in[i]; 69 return NULL; 70 } 71 72 static uint64_t now_ms(void) { 73 struct timespec t; 74 clock_gettime(CLOCK_MONOTONIC, &t); 75 return (uint64_t)t.tv_sec * 1000u + (uint64_t)t.tv_nsec / 1000000u; 76 } 77 78 /* Runs outside the world lock. Wait until the other worker has popped 79 * omega.risk: held (READY, off the ring) or already run and committed. */ 80 static void gate(Env *e) { 81 if (!e->gate_on) return; 82 uint64_t limit = now_ms() + 5000; 83 for (;;) { 84 pthread_mutex_lock(&e->w.mu); 85 const RxReaction *r = &e->w.reactions[e->id_risk]; 86 uint32_t p = r->desc.priority; 87 int off_ring = e->w.ready_len[p] == 0; 88 int held = off_ring && r->state == RX_READY; 89 int ran = r->commits > e->risk_commits0 && r->state != RX_RUNNING && 90 r->state != RX_PUBLISHING; 91 pthread_mutex_unlock(&e->w.mu); 92 if (held || ran) return; 93 if (now_ms() > limit) { e->gate_timeout = 1; return; } 94 sched_yield(); 95 } 96 } 97 98 static int fn_hypothesis(RxCtx *c) { 99 Env *e = c->user; 100 const RxSnapshotDep *s = in_of(c, e->sensor); 101 if (!s) return -1; 102 gate(e); 103 c->out[c->n_out++] = (RxMutation){ e->belief, 0, s->field[F_TEMP] * 2 + 1 }; 104 return 0; 105 } 106 107 static int fn_risk(RxCtx *c) { 108 Env *e = c->user; 109 const RxSnapshotDep *s = in_of(c, e->sensor); 110 const RxSnapshotDep *b = in_of(c, e->belief); 111 if (!s || !b) return -1; 112 c->out[c->n_out++] = (RxMutation){ e->risk, 0, s->field[F_HUMIDITY] * 100000 + b->field[0] }; 113 return 0; 114 } 115 116 static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { 117 RxCapMint m; 118 memset(&m, 0, sizeof m); 119 m.issuer = ISSUER_AEGIS_POLICY; 120 m.subject = subject; 121 m.resource = resource; 122 m.rights = rights; 123 m.parent = (RxCapRef){ UINT32_MAX, 0 }; 124 m.authority = rx_capadmin_office(&e->admin); 125 RxCapRef r = { UINT32_MAX, 0 }; 126 if (rx_capadmin_mint(&e->admin, &m, &r) != RX_CAP_OK) fprintf(stderr, "mint failed\n"); 127 return r; 128 } 129 130 static RxObjRef mkobj(Env *e, uint64_t resource) { 131 uint64_t init[RX_MAX_FIELDS] = { 0 }; 132 RxObjRef r = { UINT32_MAX, 0 }; 133 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource, init, &r); 134 return r; 135 } 136 137 static void desc(RxReactionDesc *d, const char *name, uint32_t fac, uint32_t subj, RxFn fn, void *u) { 138 memset(d, 0, sizeof *d); 139 d->name = name; 140 d->faculty = fac; 141 d->subject = subj; 142 d->priority = RX_PRIO_FOREGROUND; 143 d->fn = fn; 144 d->user = u; 145 } 146 static void trig(RxReactionDesc *d, RxObjRef o, uint64_t m) { d->triggers[d->n_triggers++] = (RxDep){ o, m }; } 147 static void wr(RxReactionDesc *d, RxObjRef o, uint64_t m) { d->writes[d->n_writes++] = (RxDep){ o, m }; } 148 static void cap(RxReactionDesc *d, RxCapRef c, uint64_t res, uint32_t rights) { 149 d->caps[d->n_caps++] = (RxCapNeed){ c, res, rights }; 150 } 151 152 static int setup(Env *e, uint32_t workers) { 153 memset(e, 0, sizeof *e); 154 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1; 155 if (rx_world_init(&e->w, &e->root, workers, 1u << 16) != RX_OK) return -1; 156 e->w.external_subject = SUBJ_EXTERNAL; 157 e->sensor = mkobj(e, RES_SENSOR); 158 e->belief = mkobj(e, RES_BELIEF); 159 e->risk = mkobj(e, RES_RISK); 160 e->c_ext = mint(e, SUBJ_EXTERNAL, RES_SENSOR, RX_RIGHT_WRITE); 161 RxReactionDesc d; 162 desc(&d, "aien.hypothesis", RX_FACULTY_AIEN, SUBJ_AIEN, fn_hypothesis, e); 163 trig(&d, e->sensor, RX_FIELD(F_TEMP)); 164 wr(&d, e->belief, RX_FIELD(0)); 165 cap(&d, mint(e, SUBJ_AIEN, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 166 cap(&d, mint(e, SUBJ_AIEN, RES_BELIEF, RX_RIGHT_WRITE), RES_BELIEF, RX_RIGHT_WRITE); 167 if (rx_world_add_reaction(&e->w, &d, &e->id_hyp) != RX_OK) return -1; 168 desc(&d, "omega.risk", RX_FACULTY_OMEGA, SUBJ_OMEGA, fn_risk, e); 169 trig(&d, e->sensor, RX_FIELD(F_HUMIDITY)); 170 trig(&d, e->belief, RX_FIELD(0)); 171 wr(&d, e->risk, RX_FIELD(0)); 172 cap(&d, mint(e, SUBJ_OMEGA, RES_SENSOR, RX_RIGHT_READ), RES_SENSOR, RX_RIGHT_READ); 173 cap(&d, mint(e, SUBJ_OMEGA, RES_BELIEF, RX_RIGHT_READ), RES_BELIEF, RX_RIGHT_READ); 174 cap(&d, mint(e, SUBJ_OMEGA, RES_RISK, RX_RIGHT_WRITE), RES_RISK, RX_RIGHT_WRITE); 175 if (rx_world_add_reaction(&e->w, &d, &e->id_risk) != RX_OK) return -1; 176 return 0; 177 } 178 179 static uint64_t field(Env *e, RxObjRef r, uint32_t f) { 180 RxObject o; 181 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; 182 return o.field[f]; 183 } 184 185 /* One crumb's schedule-independent signature, relative to the stimulus's 186 * first crumb so the two runs compare. */ 187 typedef struct { uint32_t who, kind; uint64_t cause_rel, coalesced, value; } Sig; 188 189 static Sig g_sig[2][STIMULI * 8]; 190 static uint32_t g_nsig[2]; 191 192 /* kinds: 0 temp only, 1 humidity only, 2 both. Fixed sequence. */ 193 static uint32_t pick(uint32_t k) { return (k * 7u + 2u) % 3u; } 194 195 static void run(uint32_t workers, int slot) { 196 Env *e = calloc(1, sizeof *e); 197 if (!e || setup(e, workers)) { CHECK(0, "setup (%u workers)", workers); free(e); return; } 198 uint32_t n_both = 0; 199 for (uint32_t k = 0; k < STIMULI; k++) { 200 uint32_t kind = pick(k); 201 RxMutation m[2]; 202 uint32_t n = 0; 203 uint64_t t = field(e, e->sensor, F_TEMP), h = field(e, e->sensor, F_HUMIDITY); 204 if (kind != 1) m[n++] = (RxMutation){ e->sensor, F_TEMP, (t + 1 + k % 7) % 8 }; 205 if (kind != 0) m[n++] = (RxMutation){ e->sensor, F_HUMIDITY, (h + 1 + k % 5) % 8 }; 206 if (kind == 2) n_both++; 207 pthread_mutex_lock(&e->w.mu); 208 e->risk_commits0 = e->w.reactions[e->id_risk].commits; 209 pthread_mutex_unlock(&e->w.mu); 210 e->gate_on = workers > 1 && kind == 2; 211 uint64_t first = e->w.n_crumbs + 1; 212 int64_t rc = rx_world_publish_external(&e->w, e->c_ext, m, n); 213 CHECK(rc > 0, "publish refused %lld", (long long)rc); 214 CHECK(rx_world_wait_quiescent(&e->w, 10000) == RX_OK, "no quiescence"); 215 e->gate_on = 0; 216 CHECK(!e->gate_timeout, "%u workers: forced interleaving never reached (stimulus %u)", workers, k); 217 uint32_t risk_crumbs = 0; 218 uint64_t hyp_commit = 0; 219 for (uint64_t i = first; i <= e->w.n_crumbs; i++) { 220 const RxCrumb *c = rx_world_crumb(&e->w, i); 221 CHECK(c->kind != RX_CRUMB_INVALIDATED, "%u workers: INVALIDATED crumb %llu (stimulus %u)", 222 workers, (unsigned long long)i, k); 223 if (c->reaction == e->id_hyp && c->kind == RX_CRUMB_COMMIT) hyp_commit = i; 224 if (c->reaction == e->id_risk) { 225 risk_crumbs++; 226 CHECK(c->kind == RX_CRUMB_COMMIT, "%u workers: risk crumb %llu kind %u", workers, 227 (unsigned long long)i, (unsigned)c->kind); 228 uint64_t want_coal = kind == 2 ? 1 : 0; 229 CHECK(c->coalesced_wakes == want_coal, 230 "%u workers: stimulus %u (kind %u) risk merged %llu wakes, want %llu", workers, k, 231 kind, (unsigned long long)c->coalesced_wakes, (unsigned long long)want_coal); 232 if (kind != 1) 233 CHECK(hyp_commit && c->wake_cause == hyp_commit, 234 "%u workers: stimulus %u risk woken by %llu, want hypothesis commit %llu", 235 workers, k, (unsigned long long)c->wake_cause, (unsigned long long)hyp_commit); 236 } 237 if (g_nsig[slot] < STIMULI * 8) { 238 Sig *s = &g_sig[slot][g_nsig[slot]++]; 239 s->who = c->reaction == e->id_hyp ? 1 : c->reaction == e->id_risk ? 2 : 0; 240 s->kind = (uint32_t)c->kind; 241 s->cause_rel = c->wake_cause >= first ? c->wake_cause - first + 1 : 0; 242 s->coalesced = c->coalesced_wakes; 243 s->value = c->n_outputs ? c->outputs[0].version : 0; 244 } 245 } 246 CHECK(risk_crumbs == 1, "%u workers: stimulus %u (kind %u): %u omega.risk crumbs, want 1", 247 workers, k, kind, risk_crumbs); 248 uint64_t want = field(e, e->sensor, F_HUMIDITY) * 100000 + field(e, e->belief, 0); 249 CHECK(field(e, e->risk, 0) == want, "%u workers: stimulus %u risk %llu want %llu", workers, k, 250 (unsigned long long)field(e, e->risk, 0), (unsigned long long)want); 251 } 252 printf(" %u worker(s): %u stimuli (%u both-field), omega.risk commits %llu, crumbs %llu\n", workers, 253 STIMULI, n_both, (unsigned long long)e->w.reactions[e->id_risk].commits, 254 (unsigned long long)e->w.n_crumbs); 255 CHECK(e->w.reactions[e->id_risk].commits == STIMULI, "%u workers: omega.risk commits %llu, want %u", 256 workers, (unsigned long long)e->w.reactions[e->id_risk].commits, STIMULI); 257 uint64_t checked = 0; 258 CHECK(rx_world_verify_crumbs(&e->w, &checked) == 0, "crumb chain verification failed"); 259 rx_world_destroy(&e->w); 260 rx_caproot_stop(&e->root, &e->admin); 261 free(e); 262 } 263 264 int main(void) { 265 #ifdef RX_WORLD_MUTATE_NO_UPSTREAM_HOLD 266 printf("rx_i11_wake_merge: MUTANT build (no upstream hold)\n"); 267 #endif 268 run(1, 0); 269 run(2, 1); 270 CHECK(g_nsig[0] == g_nsig[1], "crumb count 1 worker %u vs 2 workers %u", g_nsig[0], g_nsig[1]); 271 for (uint32_t i = 0; i < g_nsig[0] && i < g_nsig[1]; i++) 272 if (memcmp(&g_sig[0][i], &g_sig[1][i], sizeof(Sig)) != 0) { 273 CHECK(0, "crumb %u differs between 1 and 2 workers (who %u/%u kind %u/%u coalesced %llu/%llu)", i, 274 g_sig[0][i].who, g_sig[1][i].who, g_sig[0][i].kind, g_sig[1][i].kind, 275 (unsigned long long)g_sig[0][i].coalesced, (unsigned long long)g_sig[1][i].coalesced); 276 break; 277 } 278 printf("rx_i11_wake_merge: %s\n", g_fail ? "FAIL" : "PASS"); 279 return g_fail ? 1 : 0; 280 }