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_g7.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_r15_g7.c -- production keeps serving through a generation promotion |
| 19 | * (R15 G7; spec/r15-performance-proof.md §6.7 and §19). |
| 20 | * |
| 21 | * The R13 body is built by the R15 rig. A resident world hands the R9 store's |
| 22 | * physical work (the proposal's and the promotion's fsyncs) to the store's |
| 23 | * durable executor, so the one semantic worker of RES-1 is free while the |
| 24 | * disk works. These checks prove it on the host stand-in seat: |
| 25 | * |
| 26 | * 1. RES-1: while the promotion is between its durable candidate write and |
| 27 | * its flip, a production request is READY and executes (the disk hook |
| 28 | * waits, on the executor thread, for production to commit). |
| 29 | * 2. RES-1: production commits inside [barrier begin, receipt]. |
| 30 | * 3. The in-force record is published once, by generation.promote, after |
| 31 | * the durable flip, and names the generation now on disk. |
| 32 | * 4. A stop before the flip recovers OLD. |
| 33 | * 5. A stop after the flip recovers NEW. |
| 34 | * 6. A stop during the flip recovers OLD or NEW; never TORN. |
| 35 | * 7. Only generation.promote may write the promotion and in-force records, |
| 36 | * and every such write came from it (the executor holds no authority). |
| 37 | * 8. No orchestrator: every crumb was made by a world worker or the seat; |
| 38 | * the executor runs no reaction; RES has no orchestrator thread. |
| 39 | * 9. RES-4 promotes, reaches goal MET and serves during the barrier. |
| 40 | * 10. SEQ is unchanged: no executor, no deferral, and it still pauses |
| 41 | * production for the whole barrier (the sequential reference). |
| 42 | * |
| 43 | * Episodes can miss the TARGET_PCT goal on a noisy host (a measured margin, |
| 44 | * unrelated to promotion). Each check takes the first of up to |
| 45 | * R15_G7_ATTEMPTS (default 4) episodes that promoted and reached goal MET. |
| 46 | */ |
| 47 | #include "rx_r15_rig.h" |
| 48 | |
| 49 | #include <inttypes.h> |
| 50 | #include <signal.h> |
| 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
| 54 | #include <sys/wait.h> |
| 55 | #include <time.h> |
| 56 | #include <unistd.h> |
| 57 | |
| 58 | static int g_checks, g_fail; |
| 59 | |
| 60 | #define CHECK(c, ...) do { \ |
| 61 | g_checks++; \ |
| 62 | if (!(c)) { \ |
| 63 | g_fail++; \ |
| 64 | fprintf(stderr, " FAIL: " __VA_ARGS__); \ |
| 65 | fputc('\n', stderr); \ |
| 66 | } \ |
| 67 | } while (0) |
| 68 | |
| 69 | static uint64_t now_ns(void) { |
| 70 | struct timespec ts; |
| 71 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 72 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 73 | } |
| 74 | |
| 75 | static int attempts(void) { |
| 76 | const char *e = getenv("R15_G7_ATTEMPTS"); |
| 77 | int n = e ? atoi(e) : 4; |
| 78 | return n > 0 ? n : 4; |
| 79 | } |
| 80 | |
| 81 | /* ---- 1: the disk hook runs on the executor between candidate write and flip */ |
| 82 | |
| 83 | typedef struct { |
| 84 | R15Rig *r; |
| 85 | int called; |
| 86 | uint64_t served_at_hook, served_after, waited_ns; |
| 87 | int ready_seen; /* serve READY or RUNNING at some poll */ |
| 88 | } HookCtx; |
| 89 | |
| 90 | static void disk_hook(const char *dir, void *ctx) { |
| 91 | (void)dir; |
| 92 | HookCtx *h = ctx; |
| 93 | R15Rig *r = h->r; |
| 94 | h->called++; |
| 95 | uint32_t rs = r->omega.r_serve; |
| 96 | uint64_t s0 = __atomic_load_n(&r->served, __ATOMIC_RELAXED); |
| 97 | uint64_t t0 = now_ns(); |
| 98 | h->served_at_hook = s0; |
| 99 | /* Up to 2 s: with a held worker production could never advance here. */ |
| 100 | while (now_ns() - t0 < 2000000000ull) { |
| 101 | pthread_mutex_lock(&r->w.mu); |
| 102 | RxState st = r->w.reactions[rs].state; |
| 103 | pthread_mutex_unlock(&r->w.mu); |
| 104 | if (st == RX_READY || st == RX_RUNNING || st == RX_PUBLISHING) h->ready_seen = 1; |
| 105 | if (__atomic_load_n(&r->served, __ATOMIC_RELAXED) >= s0 + 3 && h->ready_seen) break; |
| 106 | struct timespec ts = {0, 50000}; |
| 107 | nanosleep(&ts, NULL); |
| 108 | } |
| 109 | h->served_after = __atomic_load_n(&r->served, __ATOMIC_RELAXED); |
| 110 | h->waited_ns = now_ns() - t0; |
| 111 | } |
| 112 | |
| 113 | /* ---- crumb scans --------------------------------------------------------- */ |
| 114 | |
| 115 | static uint64_t serve_commits_in(RxWorld *w, uint32_t r_serve, uint64_t lo, uint64_t hi) { |
| 116 | uint64_t n = 0; |
| 117 | for (uint64_t id = 1; id <= w->n_crumbs; id++) { |
| 118 | const RxCrumb *k = rx_world_crumb(w, id); |
| 119 | if (k && k->reaction == r_serve && k->kind == RX_CRUMB_COMMIT && |
| 120 | k->t_end_ns >= lo && k->t_end_ns <= hi) |
| 121 | n++; |
| 122 | } |
| 123 | return n; |
| 124 | } |
| 125 | |
| 126 | static int writes_obj(const RxCrumb *k, RxObjRef o) { |
| 127 | for (uint32_t i = 0; i < k->n_outputs; i++) |
| 128 | if (k->outputs[i].obj.id == o.id) return 1; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* 3, 7, 8 on a finished episode. */ |
| 133 | static void audit(R15Rig *r, const RxGenPhases *ph, const char *cfg) { |
| 134 | RxWorld *w = &r->w; |
| 135 | const RxLivingPromoter *p = &r->promoter; |
| 136 | uint64_t inforce_writes = 0, promo_writes = 0, foreign = 0, bad_worker = 0; |
| 137 | const RxCrumb *inforce = NULL; |
| 138 | for (uint64_t id = 1; id <= w->n_crumbs; id++) { |
| 139 | const RxCrumb *k = rx_world_crumb(w, id); |
| 140 | if (!k) continue; |
| 141 | if (k->kind == RX_CRUMB_COMMIT || k->kind == RX_CRUMB_NOOP) { |
| 142 | int ok_worker = k->worker < w->n_workers || k->worker == RX_SEAT_BLACKWELL || |
| 143 | (w->sequential && k->worker == RX_SEQ_WORKER); |
| 144 | if (!ok_worker) bad_worker++; |
| 145 | } |
| 146 | if (k->kind != RX_CRUMB_COMMIT) continue; |
| 147 | int wi = writes_obj(k, p->inforce), wp = writes_obj(k, p->promotion); |
| 148 | if (!wi && !wp) continue; |
| 149 | if (k->reaction != p->reaction) foreign++; |
| 150 | if (wi) { inforce_writes++; inforce = k; } |
| 151 | if (wp) promo_writes++; |
| 152 | } |
| 153 | CHECK(foreign == 0, "%s: %" PRIu64 " promotion/in-force writes not by generation.promote", |
| 154 | cfg, foreign); |
| 155 | CHECK(inforce_writes == 1 && promo_writes == 1, |
| 156 | "%s: in-force written %" PRIu64 "x, promotion %" PRIu64 "x (want once each)", cfg, |
| 157 | inforce_writes, promo_writes); |
| 158 | CHECK(inforce && ph->flip_ns && ph->receipt_ns && inforce->t_end_ns >= ph->receipt_ns, |
| 159 | "%s: in-force published before the durable flip and receipt", cfg); |
| 160 | uint32_t writers = 0; |
| 161 | for (uint32_t i = 0; i < w->n_reactions; i++) { |
| 162 | const RxReactionDesc *d = &w->reactions[i].desc; |
| 163 | for (uint32_t j = 0; j < d->n_writes; j++) |
| 164 | if (d->writes[j].obj.id == p->inforce.id) writers++; |
| 165 | } |
| 166 | /* generation.promote only: the R15 body registers no restore. */ |
| 167 | CHECK(writers == 1, "%s: %u reactions declare in-force writes (want 1)", cfg, writers); |
| 168 | CHECK(bad_worker == 0, "%s: %" PRIu64 " crumbs from no world worker and no seat", cfg, |
| 169 | bad_worker); |
| 170 | RxObject rec; |
| 171 | uint64_t active = 0, lineage = 0; |
| 172 | rx_gen_active(r->gen, &active, &lineage); |
| 173 | CHECK(rx_world_read(w, p->inforce, &rec) == RX_OK && rec.field[7] == active && |
| 174 | active == ph->candidate_id, |
| 175 | "%s: in-force names generation %" PRIu64 ", store active %" PRIu64 ", promoted %" PRIu64, |
| 176 | cfg, rec.field[7], active, ph->candidate_id); |
| 177 | RxRecoveryRecord rr; |
| 178 | CHECK(rx_gen_recover(r->generation_dir, &rr) == RX_GEN_OK && rr.coherent && |
| 179 | rr.active_id == active && rr.receipt_present, |
| 180 | "%s: recovery of the live store does not name the promoted generation", cfg); |
| 181 | } |
| 182 | |
| 183 | /* One episode that promoted and met its goal, or 0 after the attempts. The |
| 184 | * rig stays started on success; the caller stops it. */ |
| 185 | static int good_episode(R15Rig *r, R15Config cfg, HookCtx *hook, R15Outcome *out) { |
| 186 | for (int a = 1; a <= attempts(); a++) { |
| 187 | if (r15_start(r, cfg) != 0) { |
| 188 | fprintf(stderr, " %s attempt %d: build failed at stage %d (%s)\n", |
| 189 | r15_config_name(cfg), a, r->stage, r->stage_why ? r->stage_why : ""); |
| 190 | r15_stop(r); |
| 191 | continue; |
| 192 | } |
| 193 | if (hook) { |
| 194 | memset(hook, 0, sizeof *hook); |
| 195 | hook->r = r; |
| 196 | rx_gen_set_disk_hook(r->gen, disk_hook, hook); |
| 197 | } |
| 198 | int rc = r15_episode(r, 0, out); |
| 199 | r15_producer_stop(r); |
| 200 | rx_world_wait_quiescent(&r->w, 10000); |
| 201 | if (rc == 0 && out->ok && out->promote_result == RX_GEN_OK) { |
| 202 | printf(" %s: episode %d reached goal MET (generation %llu -> %llu)\n", |
| 203 | r15_config_name(cfg), a, (unsigned long long)out->gen_before, |
| 204 | (unsigned long long)out->gen_after); |
| 205 | return 1; |
| 206 | } |
| 207 | printf(" %s: episode %d did not reach goal MET (%s); another\n", |
| 208 | r15_config_name(cfg), a, out->why); |
| 209 | r15_stop(r); |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static R15Rig g_rig; |
| 215 | |
| 216 | static void res_case(R15Config cfg) { |
| 217 | R15Rig *r = &g_rig; |
| 218 | R15Outcome out; |
| 219 | HookCtx hook; |
| 220 | const char *name = r15_config_name(cfg); |
| 221 | printf("[*] %s: promotion while production runs\n", name); |
| 222 | if (!good_episode(r, cfg, &hook, &out)) { |
| 223 | CHECK(0, "%s: no episode reached goal MET in %d attempts", name, attempts()); |
| 224 | return; |
| 225 | } |
| 226 | RxGenPhases ph; |
| 227 | memset(&ph, 0, sizeof ph); |
| 228 | rx_gen_last_phases(r->gen, &ph); |
| 229 | uint64_t during = serve_commits_in(&r->w, r->omega.r_serve, ph.barrier_ns, ph.receipt_ns); |
| 230 | uint64_t to_flip = serve_commits_in(&r->w, r->omega.r_serve, ph.barrier_ns, ph.flip_ns); |
| 231 | printf(" barrier %.1f ms (flip at %.1f ms); production commits during it %" PRIu64 |
| 232 | " (%" PRIu64 " before the flip); hook saw %" PRIu64 " -> %" PRIu64 " served in %.2f ms\n", |
| 233 | (double)(ph.receipt_ns - ph.barrier_ns) / 1e6, (double)(ph.flip_ns - ph.barrier_ns) / 1e6, |
| 234 | during, to_flip, hook.served_at_hook, hook.served_after, (double)hook.waited_ns / 1e6); |
| 235 | CHECK(rx_gen_exec_running(r->gen), "%s: store has no durable executor", name); |
| 236 | CHECK(hook.called == 1, "%s: disk hook ran %d times (want 1)", name, hook.called); |
| 237 | CHECK(hook.ready_seen, "%s: no production request READY during the promotion", name); |
| 238 | CHECK(hook.served_after >= hook.served_at_hook + 3, |
| 239 | "%s: production did not commit while the promotion's disk work waited", name); |
| 240 | CHECK(during > 0, "%s: no production commit inside the barrier", name); |
| 241 | CHECK(to_flip > 0, "%s: no production commit between barrier begin and flip", name); |
| 242 | CHECK(r->w.stats.deferrals >= 2, "%s: %" PRIu64 " deferrals (want >= 2: proposal, promotion)", |
| 243 | name, r->w.stats.deferrals); |
| 244 | CHECK(!r->orch_live, "%s: an orchestrator thread runs in a resident world", name); |
| 245 | CHECK(out.wrong == 0 && out.unpromoted_use == 0 && out.illegal == 0 && out.crumbs_verified && |
| 246 | out.lost_triggers == 0, |
| 247 | "%s: integrity (wrong %" PRIu64 ", unpromoted %" PRIu64 ", illegal %" PRIu64 |
| 248 | ", verified %d, lost %" PRIu64 ")", name, out.wrong, out.unpromoted_use, out.illegal, |
| 249 | out.crumbs_verified, out.lost_triggers); |
| 250 | audit(r, &ph, name); |
| 251 | r15_stop(r); |
| 252 | } |
| 253 | |
| 254 | static void seq_case(void) { |
| 255 | R15Rig *r = &g_rig; |
| 256 | R15Outcome out; |
| 257 | printf("[*] SEQ: the sequential reference is unchanged\n"); |
| 258 | if (!good_episode(r, R15_SEQ, NULL, &out)) { |
| 259 | CHECK(0, "SEQ: no episode reached goal MET in %d attempts", attempts()); |
| 260 | return; |
| 261 | } |
| 262 | RxGenPhases ph; |
| 263 | memset(&ph, 0, sizeof ph); |
| 264 | rx_gen_last_phases(r->gen, &ph); |
| 265 | uint64_t during = serve_commits_in(&r->w, r->omega.r_serve, ph.barrier_ns, ph.receipt_ns); |
| 266 | printf(" barrier %.1f ms; production commits during it %" PRIu64 "\n", |
| 267 | (double)(ph.receipt_ns - ph.barrier_ns) / 1e6, during); |
| 268 | CHECK(!rx_gen_exec_running(r->gen), "SEQ: a durable executor was started"); |
| 269 | CHECK(r->w.stats.deferrals == 0, "SEQ: %" PRIu64 " deferrals", r->w.stats.deferrals); |
| 270 | CHECK(during == 0, "SEQ: production ran during a stage (the reference is sequential)"); |
| 271 | audit(r, &ph, "SEQ"); |
| 272 | r15_stop(r); |
| 273 | } |
| 274 | |
| 275 | /* ---- 4, 5, 6: stop the process at an R9 step, recover from disk ---------- */ |
| 276 | |
| 277 | static void crash_case(int step, const char *label, int want) { |
| 278 | printf("[*] RES-1: stop %s\n", label); |
| 279 | int fd[2]; |
| 280 | if (pipe(fd) != 0) { CHECK(0, "pipe"); return; } |
| 281 | fflush(NULL); |
| 282 | pid_t pid = fork(); |
| 283 | if (pid == 0) { |
| 284 | close(fd[0]); |
| 285 | static R15Rig c; |
| 286 | if (r15_start(&c, R15_RES1) != 0) _exit(3); |
| 287 | if (write(fd[1], c.generation_dir, sizeof c.generation_dir) != sizeof c.generation_dir) |
| 288 | _exit(4); |
| 289 | close(fd[1]); |
| 290 | rx_gen_set_crash(c.gen, step); |
| 291 | for (int a = 0; a < attempts(); a++) { |
| 292 | R15Outcome o; |
| 293 | (void)r15_episode(&c, 0, &o); |
| 294 | /* the store stops the process at `step` if a promotion began */ |
| 295 | } |
| 296 | _exit(5); |
| 297 | } |
| 298 | close(fd[1]); |
| 299 | char dir[128] = {0}; |
| 300 | ssize_t got = read(fd[0], dir, sizeof dir); |
| 301 | close(fd[0]); |
| 302 | int status = 0; |
| 303 | waitpid(pid, &status, 0); |
| 304 | int code = WIFEXITED(status) ? WEXITSTATUS(status) : -1; |
| 305 | CHECK(got == (ssize_t)sizeof dir && dir[0], "%s: child did not report its store", label); |
| 306 | CHECK(code == 86, "%s: child exit %d (want 86, the injected stop)", label, code); |
| 307 | if (got != (ssize_t)sizeof dir || !dir[0]) return; |
| 308 | RxRecoveryRecord rr; |
| 309 | int rc = rx_gen_recover(dir, &rr); |
| 310 | printf(" recovered generation %" PRIu64 " (lineage %" PRIu64 "), rc %d, coherent %d\n", |
| 311 | rr.active_id, rr.lineage, rc, rr.coherent); |
| 312 | CHECK(rc == RX_GEN_OK && rr.coherent, "%s: recovery rc %d coherent %d (TORN)", label, rc, |
| 313 | rr.coherent); |
| 314 | if (want == 1) CHECK(rr.active_id == 1, "%s: recovered %" PRIu64 ", want OLD (1)", label, |
| 315 | rr.active_id); |
| 316 | else if (want == 2) CHECK(rr.active_id == 2, "%s: recovered %" PRIu64 ", want NEW (2)", label, |
| 317 | rr.active_id); |
| 318 | else CHECK(rr.active_id == 1 || rr.active_id == 2, "%s: recovered %" PRIu64 ", want OLD or NEW", |
| 319 | label, rr.active_id); |
| 320 | /* A second recovery agrees (recovery is idempotent). */ |
| 321 | RxRecoveryRecord again; |
| 322 | CHECK(rx_gen_recover(dir, &again) == RX_GEN_OK && again.active_id == rr.active_id, |
| 323 | "%s: second recovery disagrees", label); |
| 324 | char cmd[200]; |
| 325 | snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir); |
| 326 | if (system(cmd) != 0) fprintf(stderr, "note: could not remove %s\n", dir); |
| 327 | } |
| 328 | |
| 329 | int main(void) { |
| 330 | signal(SIGPIPE, SIG_IGN); |
| 331 | res_case(R15_RES1); |
| 332 | res_case(R15_RES4); |
| 333 | seq_case(); |
| 334 | crash_case(RX_CRASH_BEFORE_ROOT_FLIP, "before the flip", 1); |
| 335 | crash_case(RX_CRASH_DURING_ROOT_FLIP, "during the flip", 0); |
| 336 | crash_case(RX_CRASH_AFTER_ROOT_FLIP, "after the flip", 2); |
| 337 | printf("R15 G7 worker split: %d checks, %d failures\n", g_checks, g_fail); |
| 338 | return g_fail ? 1 : 0; |
| 339 | } |