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_compose_test.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 | * COMPOSITION-2 WP-B + WP-C: one World, one causal path, OLD-or-NEW. |
| 19 | * |
| 20 | * WP-B: every fault point on the path (before fork, candidate, before |
| 21 | * commit, after validation, seal, reclaim, Cortex), each in process and as a |
| 22 | * crashed child (_exit(77)); after reopen the World value, the durable |
| 23 | * J-Space branch and the Cortex record agree on exactly OLD or exactly NEW, |
| 24 | * losers are never recoverable, and the path still makes progress. Also: no |
| 25 | * winner keeps OLD, a candidate subject that tries to write the state is |
| 26 | * refused, a foreign machine identity is refused. |
| 27 | * |
| 28 | * WP-C: a World action becomes a Cortex claim / evidence / promotion with |
| 29 | * provenance (World crumbs, branch ref, Skill digest); replay verifies the |
| 30 | * journal and recall finds the record; a torn tail is repaired and the |
| 31 | * composition completed; a corrupt middle is refused; branch identity |
| 32 | * survives recording; a loser is never promoted. |
| 33 | */ |
| 34 | #include "rx_compose_fixture.h" |
| 35 | |
| 36 | #include <ftw.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/wait.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | static int g_checks, g_fail; |
| 42 | |
| 43 | #define CHECK(cond, ...) do { \ |
| 44 | g_checks++; \ |
| 45 | if (!(cond)) { \ |
| 46 | g_fail++; \ |
| 47 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 48 | fprintf(stderr, __VA_ARGS__); \ |
| 49 | fputc('\n', stderr); \ |
| 50 | } \ |
| 51 | } while (0) |
| 52 | |
| 53 | static Fx g_fx; |
| 54 | static RxCompose g_c; /* large: static */ |
| 55 | static char g_base[128]; |
| 56 | |
| 57 | static int rm_one(const char *p, const struct stat *sb, int flag, struct FTW *ftw) { |
| 58 | (void)sb; (void)flag; (void)ftw; |
| 59 | return remove(p); |
| 60 | } |
| 61 | static void rmtree(const char *p) { nftw(p, rm_one, 16, FTW_DEPTH | FTW_PHYS); } |
| 62 | |
| 63 | static void dir_for(char *out, size_t n, const char *name) { |
| 64 | snprintf(out, n, "%s/%s", g_base, name); |
| 65 | rmtree(out); |
| 66 | } |
| 67 | |
| 68 | static void reset_c(void) { |
| 69 | g_c.test.fault_point = RXC_FP_NONE; |
| 70 | g_c.test.fault_crash = 0; |
| 71 | g_c.test.fault_k = 0; |
| 72 | g_c.test.rogue_candidate = 0; |
| 73 | } |
| 74 | |
| 75 | static int ref_eq(JsBranchRef a, JsBranchRef b) { return a.id == b.id && a.gen == b.gen; } |
| 76 | static int noop_fn(RxCtx *x) { (void)x; return 0; } |
| 77 | |
| 78 | /* Staged (never-committed) J-Space branches still held in memory. */ |
| 79 | static uint32_t staged_branches(const JsSpace *s) { |
| 80 | uint32_t n = 0; |
| 81 | for (uint32_t i = 0; i < s->n_branches; i++) n += s->branches[i] && s->branches[i]->staged; |
| 82 | return n; |
| 83 | } |
| 84 | |
| 85 | /* Cleanup in process, before any close or reopen (L7-LIVING): every |
| 86 | * candidate branch the run forked and did not commit is reclaimed and no |
| 87 | * staged branch survives. A run that did not move the World also leaves |
| 88 | * exactly one live branch (the state's) and promotes nothing. */ |
| 89 | static void check_cleanup(const char *what, const RxcResult *o, uint32_t promotions_before) { |
| 90 | int moved = !ref_eq(rx_compose_state(&g_c), o->old_ref); |
| 91 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 92 | if (!o->cand_ref[k].id && !o->cand_ref[k].gen) continue; |
| 93 | if (moved && ref_eq(o->cand_ref[k], rx_compose_state(&g_c))) continue; |
| 94 | CHECK(js_branch_check(&g_c.js, o->cand_ref[k]) == JS_ERR_STALE, |
| 95 | "%s: candidate %u reclaimed in process", what, k); |
| 96 | } |
| 97 | CHECK(staged_branches(&g_c.js) == 0, "%s: no staged branch survives (%u)", what, |
| 98 | staged_branches(&g_c.js)); |
| 99 | if (!moved) { |
| 100 | CHECK(fx_live_branches(&g_c.js) == 1, "%s: one live branch in process (%u)", what, |
| 101 | fx_live_branches(&g_c.js)); |
| 102 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == promotions_before, |
| 103 | "%s: nothing promoted", what); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* Reference run: what OLD and NEW are for input 5 in a fresh directory. */ |
| 108 | static struct { |
| 109 | JsBranchRef old_ref, new_ref, cand[RXC_K]; |
| 110 | uint8_t old_dig[32], new_dig[32]; |
| 111 | uint64_t result; |
| 112 | } R; |
| 113 | |
| 114 | static void t_reference(void) { |
| 115 | printf("[*] reference run: fresh World, one goal, two alternatives, winner committed\n"); |
| 116 | char d[200]; |
| 117 | dir_for(d, sizeof d, "ref"); |
| 118 | reset_c(); |
| 119 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "open ref"); |
| 120 | JsBranchRef s0 = rx_compose_state(&g_c); |
| 121 | CHECK(js_branch_content_digest(&g_c.js, s0.id, R.old_dig) == JS_OK, "old digest"); |
| 122 | RxcResult o; |
| 123 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK, "run"); |
| 124 | CHECK(o.outcome == RXC_OUT_COMMITTED, "committed (%d)", o.outcome); |
| 125 | CHECK(o.n_alternatives == 2, "two alternatives (%u)", o.n_alternatives); |
| 126 | CHECK(o.route[0].chosen.skill_id == FX_SKILL_A && o.route[1].chosen.skill_id == FX_SKILL_B, |
| 127 | "rank order: A then B"); |
| 128 | CHECK(o.winner == 0 && o.result == 16, "winner A, result 16 (%u, %llu)", o.winner, |
| 129 | (unsigned long long)o.result); |
| 130 | CHECK(ref_eq(o.old_ref, s0), "old ref"); |
| 131 | CHECK(ref_eq(o.new_ref, o.cand_ref[0]), "new = candidate 0"); |
| 132 | CHECK(ref_eq(rx_compose_state(&g_c), o.new_ref), "World names NEW"); |
| 133 | CHECK(js_branch_check(&g_c.js, o.cand_ref[1]) == JS_ERR_STALE, "loser reclaimed"); |
| 134 | CHECK(js_branch_check(&g_c.js, s0) == JS_ERR_STALE, "superseded branch released"); |
| 135 | JsBranchInfo bi; |
| 136 | CHECK(js_branch_info(&g_c.js, o.new_ref, &bi) == JS_OK && !bi.staged && bi.owner == 0, |
| 137 | "winner sealed, no owner"); |
| 138 | CHECK(fx_live_branches(&g_c.js) == 1, "one live branch"); |
| 139 | check_cleanup("reference", &o, 0); /* cleanup after success */ |
| 140 | R.old_ref = s0; |
| 141 | R.new_ref = o.new_ref; |
| 142 | R.cand[0] = o.cand_ref[0]; |
| 143 | R.cand[1] = o.cand_ref[1]; |
| 144 | memcpy(R.new_dig, o.winner_digest, 32); |
| 145 | R.result = o.result; |
| 146 | CHECK(memcmp(R.old_dig, R.new_dig, 32) != 0 && !ref_eq(R.old_ref, R.new_ref), "OLD and NEW differ"); |
| 147 | fx_close(&g_fx, &g_c); |
| 148 | rmtree(d); |
| 149 | } |
| 150 | |
| 151 | /* After reopen: World value, durable branch and Cortex agree on want_new. */ |
| 152 | static void check_reopened(const char *what, int want_new, int point) { |
| 153 | JsBranchRef want = want_new ? R.new_ref : R.old_ref; |
| 154 | JsBranchRef s = rx_compose_state(&g_c); |
| 155 | CHECK(ref_eq(s, want), "%s: World names %s (%u/%u)", what, want_new ? "NEW" : "OLD", s.id, |
| 156 | s.gen); |
| 157 | CHECK(ref_eq(g_c.recovered, s), "%s: recovered = World", what); |
| 158 | const CxObject *ro = cx_get(&g_c.cx, g_c.recovered_record); |
| 159 | if (ro) { |
| 160 | const uint64_t *p = cx_payload(&g_c.cx, ro); |
| 161 | CHECK(p[CX_WREC_FIELD0 + RXC_S_REF] == fx_pack(s), "%s: Cortex record names it", what); |
| 162 | CHECK(ro->kind == (want_new ? CX_K_EXEC_COMMIT : CX_K_ENTITY_CREATED), |
| 163 | "%s: Cortex record kind", what); |
| 164 | } else { |
| 165 | CHECK(0, "%s: no recovered record", what); |
| 166 | } |
| 167 | JsBranchInfo bi; |
| 168 | CHECK(js_branch_info(&g_c.js, s, &bi) == JS_OK && !bi.staged, "%s: durable, sealed", what); |
| 169 | CHECK(fx_live_branches(&g_c.js) == 1, "%s: exactly one durable branch (%u)", what, |
| 170 | fx_live_branches(&g_c.js)); |
| 171 | uint8_t dg[32]; |
| 172 | CHECK(js_branch_content_digest(&g_c.js, s.id, dg) == JS_OK && |
| 173 | memcmp(dg, want_new ? R.new_dig : R.old_dig, 32) == 0, "%s: content is exactly %s", |
| 174 | what, want_new ? "NEW" : "OLD"); |
| 175 | /* Losers (and NEW when OLD was kept) are never recoverable. */ |
| 176 | CHECK(js_branch_check(&g_c.js, R.cand[1]) == JS_ERR_STALE, "%s: loser gone", what); |
| 177 | if (!want_new) |
| 178 | CHECK(js_branch_check(&g_c.js, R.new_ref) == JS_ERR_STALE, "%s: NEW gone", what); |
| 179 | RxObject so; |
| 180 | rx_world_read(&g_c.w, g_c.state, &so); |
| 181 | CHECK(so.field[RXC_S_RESULT] == (want_new ? R.result : 0), "%s: World result", what); |
| 182 | CHECK(g_c.rolled_back == (point == RXC_FP_RECLAIM ? 1u : 0u), "%s: rollbacks %u", what, |
| 183 | g_c.rolled_back); |
| 184 | CHECK((g_c.recovered_completed > 0) == (point == RXC_FP_CORTEX), "%s: completed %u", what, |
| 185 | g_c.recovered_completed); |
| 186 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == (want_new ? 1u : 0u), |
| 187 | "%s: promotions", what); |
| 188 | } |
| 189 | |
| 190 | static const char *fp_name[RXC_FP_END] = { "none", "before_fork", "candidate", "before_commit", |
| 191 | "after_validation", "seal", "reclaim", "cortex" }; |
| 192 | |
| 193 | static void fault_case(int point, int crash, uint32_t k) { |
| 194 | char d[200], what[64]; |
| 195 | snprintf(what, sizeof what, "%s/%s/k%u", fp_name[point], crash ? "crash" : "inproc", k); |
| 196 | char leaf[64]; |
| 197 | snprintf(leaf, sizeof leaf, "f%d_%d_%u", point, crash, k); |
| 198 | dir_for(d, sizeof d, leaf); |
| 199 | int want_new = point == RXC_FP_CORTEX; |
| 200 | if (crash) { |
| 201 | fflush(NULL); |
| 202 | pid_t pid = fork(); |
| 203 | if (pid == 0) { |
| 204 | reset_c(); |
| 205 | g_c.test.fault_point = point; |
| 206 | g_c.test.fault_crash = 1; |
| 207 | g_c.test.fault_k = k; |
| 208 | if (fx_open(&g_fx, &g_c, d, 1) != RX_OK) _exit(2); |
| 209 | RxcResult o; |
| 210 | fx_run(&g_fx, &g_c, 5, &o); |
| 211 | _exit(3); /* the fault did not fire */ |
| 212 | } |
| 213 | int st = 0; |
| 214 | waitpid(pid, &st, 0); |
| 215 | CHECK(WIFEXITED(st) && WEXITSTATUS(st) == RXC_CRASH_EXIT, "%s: child crashed at point (%d)", |
| 216 | what, WIFEXITED(st) ? WEXITSTATUS(st) : -1); |
| 217 | } else { |
| 218 | reset_c(); |
| 219 | g_c.test.fault_point = point; |
| 220 | g_c.test.fault_k = k; |
| 221 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "%s: open", what); |
| 222 | RxcResult o; |
| 223 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK, "%s: run", what); |
| 224 | int want = point == RXC_FP_RECLAIM ? RXC_OUT_NOT_DURABLE |
| 225 | : point == RXC_FP_CORTEX ? RXC_OUT_RECORD_FAILED : RXC_OUT_NOT_COMMITTED; |
| 226 | CHECK(o.outcome == want, "%s: outcome %d want %d", what, o.outcome, want); |
| 227 | CHECK(g_c.test.fault_hit, "%s: fault fired", what); |
| 228 | check_cleanup(what, &o, 0); |
| 229 | fx_close(&g_fx, &g_c); |
| 230 | } |
| 231 | reset_c(); |
| 232 | int rc = fx_open(&g_fx, &g_c, d, 1); |
| 233 | CHECK(rc == RX_OK, "%s: reopen (%d)", what, rc); |
| 234 | if (rc != RX_OK) return; |
| 235 | check_reopened(what, want_new, point); |
| 236 | /* The path still makes progress from what was recovered. */ |
| 237 | JsBranchRef before = rx_compose_state(&g_c); |
| 238 | RxcResult o; |
| 239 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, |
| 240 | "%s: next goal commits (%d)", what, o.outcome); |
| 241 | JsBranchInfo bi; |
| 242 | CHECK(js_branch_info(&g_c.js, o.new_ref, &bi) == JS_OK && ref_eq(o.old_ref, before) && |
| 243 | o.result == 19, "%s: next goal built on the recovered state", what); |
| 244 | fx_close(&g_fx, &g_c); |
| 245 | /* And that is durable too. */ |
| 246 | reset_c(); |
| 247 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK && ref_eq(rx_compose_state(&g_c), o.new_ref), |
| 248 | "%s: second reopen names the next commit", what); |
| 249 | fx_close(&g_fx, &g_c); |
| 250 | rmtree(d); |
| 251 | } |
| 252 | |
| 253 | static void t_faults(void) { |
| 254 | printf("[*] fault points x {in process, crashed child}: exactly OLD or exactly NEW\n"); |
| 255 | for (int crash = 0; crash < 2; crash++) |
| 256 | for (int p = RXC_FP_BEFORE_FORK; p < RXC_FP_END; p++) { |
| 257 | fault_case(p, crash, 0); |
| 258 | if (p == RXC_FP_BEFORE_FORK || p == RXC_FP_CANDIDATE) fault_case(p, crash, 1); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void t_no_winner(void) { |
| 263 | printf("[*] no winner: both candidates break the contract, OLD kept, nothing promoted\n"); |
| 264 | char d[200]; |
| 265 | dir_for(d, sizeof d, "nowin"); |
| 266 | reset_c(); |
| 267 | fx_a_bad = fx_b_bad = 1; |
| 268 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "open"); |
| 269 | RxcResult o; |
| 270 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_NO_WINNER, "no winner (%d)", |
| 271 | o.outcome); |
| 272 | fx_a_bad = fx_b_bad = 0; |
| 273 | CHECK(o.winner == RXC_NONE && ref_eq(rx_compose_state(&g_c), R.old_ref), "OLD kept"); |
| 274 | CHECK(o.cx_promotion == 0 && fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 0, "no promotion"); |
| 275 | CHECK(o.cx_candidate[0] && o.cx_candidate[1] && o.cx_evidence, "claims and evidence recorded"); |
| 276 | CHECK(o.cx_admission[0] && o.cx_admission[1], "both admitted as losers"); |
| 277 | const CxObject *ev = cx_get(&g_c.cx, o.cx_evidence); |
| 278 | CHECK(ev && cx_payload(&g_c.cx, ev)[RXC_EP_WINNER] == RXC_NONE && |
| 279 | cx_payload(&g_c.cx, ev)[RXC_EP_PASSMASK] == 0, "evidence: no winner, nothing passed"); |
| 280 | for (uint32_t k = 0; k < RXC_K; k++) |
| 281 | CHECK(js_branch_check(&g_c.js, o.cand_ref[k]) == JS_ERR_STALE, "candidate %u reclaimed", k); |
| 282 | check_cleanup("no winner", &o, 0); |
| 283 | fx_close(&g_fx, &g_c); |
| 284 | reset_c(); |
| 285 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "reopen"); |
| 286 | check_reopened("no winner", 0, RXC_FP_NONE); |
| 287 | fx_close(&g_fx, &g_c); |
| 288 | rmtree(d); |
| 289 | } |
| 290 | |
| 291 | static void t_authority(void) { |
| 292 | printf("[*] authority: a candidate cannot write the state; outside input needs its cap\n"); |
| 293 | char d[200]; |
| 294 | dir_for(d, sizeof d, "rogue"); |
| 295 | reset_c(); |
| 296 | g_c.test.rogue_candidate = 1; |
| 297 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "open"); |
| 298 | RxcResult o; |
| 299 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_NOT_COMMITTED, |
| 300 | "rogue run not committed (%d)", o.outcome); |
| 301 | CHECK(ref_eq(rx_compose_state(&g_c), R.old_ref), "state untouched"); |
| 302 | check_cleanup("rogue", &o, 0); /* cleanup after a refusal */ |
| 303 | /* The refusal is on the record. */ |
| 304 | uint32_t refused = 0; |
| 305 | for (uint64_t id = 1; id <= g_c.cx.n; id++) { |
| 306 | const CxObject *x = cx_get(&g_c.cx, id); |
| 307 | if (!x || x->kind != CX_K_EXEC_FAILED || |
| 308 | cx_payload(&g_c.cx, x)[CX_WREC_REACTION] != g_c.rx_cand[0]) continue; |
| 309 | if ((int64_t)cx_payload(&g_c.cx, x)[CX_WREC_REASON] == RX_ERR_WRITE_SET) refused++; |
| 310 | } |
| 311 | CHECK(refused >= 1, "EXEC_FAILED write-set refusal recorded (%u)", refused); |
| 312 | /* A reaction under a candidate subject, holding only candidate rights, |
| 313 | * that declares a state write is refused at registration. */ |
| 314 | RxReactionDesc r; |
| 315 | memset(&r, 0, sizeof r); |
| 316 | r.name = "rogue.state"; |
| 317 | r.faculty = RX_FACULTY_OMEGA; |
| 318 | r.subject = RXC_SUBJ_CAND0; |
| 319 | r.priority = RX_PRIO_FOREGROUND; |
| 320 | r.triggers[r.n_triggers++] = (RxDep){ g_c.goal, RX_ALL_FIELDS }; |
| 321 | r.writes[r.n_writes++] = (RxDep){ g_c.state, RX_ALL_FIELDS }; |
| 322 | r.caps[r.n_caps++] = (RxCapNeed){ g_c.cap_cand[0][1], RXC_RES_STATE, RX_RIGHT_WRITE }; |
| 323 | r.fn = noop_fn; |
| 324 | uint32_t id = 0; |
| 325 | int rc = rx_world_add_reaction(&g_c.w, &r, &id); |
| 326 | CHECK(rc == RX_ERR_AUTHORITY, "candidate cap cannot write state (%d)", rc); |
| 327 | /* Outside input with a candidate's capability is refused. */ |
| 328 | RxMutation m = { g_c.goal, RXC_G_INPUT, 9 }; |
| 329 | CHECK(rx_world_publish_external(&g_c.w, g_c.cap_cand[0][0], &m, 1) < 0, |
| 330 | "goal publish with a read cap refused"); |
| 331 | fx_close(&g_fx, &g_c); |
| 332 | /* A foreign machine cannot open this composition. */ |
| 333 | AienMachineId other = fx_mid(2), mine = g_fx.self; |
| 334 | g_fx.self = other; |
| 335 | reset_c(); |
| 336 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_ERR_IDENTITY, "foreign machine refused"); |
| 337 | g_fx.self = mine; |
| 338 | rmtree(d); |
| 339 | } |
| 340 | |
| 341 | /* ---- WP-C ---------------------------------------------------------------- */ |
| 342 | |
| 343 | static int words_eq(const uint64_t *w, const uint8_t d[32]) { |
| 344 | for (int i = 0; i < 4; i++) { |
| 345 | uint64_t v = 0; |
| 346 | for (int b = 0; b < 8; b++) v |= (uint64_t)d[8 * i + b] << (8 * b); |
| 347 | if (w[i] != v) return 0; |
| 348 | } |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | static void t_cortex(void) { |
| 353 | printf("[*] Cortex: provenance, replay + recall, torn tail, corrupt middle\n"); |
| 354 | char d[200]; |
| 355 | dir_for(d, sizeof d, "cortex"); |
| 356 | reset_c(); |
| 357 | fx_a_bad = 1; /* A breaks the contract: B wins, A is the loser */ |
| 358 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "open"); |
| 359 | RxcResult o; |
| 360 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "commit (%d)", |
| 361 | o.outcome); |
| 362 | fx_a_bad = 0; |
| 363 | CHECK(o.winner == 1, "B wins when A fails the contract (%u)", o.winner); |
| 364 | CxStore *s = &g_c.cx; |
| 365 | uint64_t wc = o.cx_candidate[1], lc = o.cx_candidate[0]; |
| 366 | const CxObject *cl = cx_get(s, wc); |
| 367 | CHECK(cl && cl->kind == CX_K_CANDIDATE && cl->cls == CX_CLAIM, "winner claim"); |
| 368 | if (!cl) { fx_close(&g_fx, &g_c); return; } |
| 369 | const uint64_t *cp = cx_payload(s, cl); |
| 370 | CHECK(cp[RXC_CP_REF] == fx_pack(o.new_ref), "claim names the committed branch"); |
| 371 | CHECK((uint32_t)cp[RXC_CP_SKILL] == FX_SKILL_B && cp[RXC_CP_PASS] == 1, "claim: Skill B, passed"); |
| 372 | CHECK(words_eq(cp + RXC_CP_SKILLDIG0, g_fx.skills.skill[1].identity), "claim: Skill digest"); |
| 373 | CHECK(words_eq(cp + RXC_CP_DIGEST0, o.winner_digest), "claim: branch content digest"); |
| 374 | const CxObject *ck = cx_get(s, cl->links[0]), *g = cx_get(s, cl->links[1]); |
| 375 | CHECK(ck && ck->kind == CX_K_EXEC_COMMIT && ck->subject == RXC_CX_SUBJECT(RXC_SLOT_CAND1), |
| 376 | "claim -> candidate World record"); |
| 377 | CHECK(ck && cx_payload(s, ck)[CX_WREC_FIELD0 + RXC_C_REF] == fx_pack(o.new_ref), |
| 378 | "World record carries the branch ref"); |
| 379 | CHECK(g && g->kind == CX_K_WORK_ACCEPTED && cx_payload(s, g)[CX_WREC_CRUMB] == o.goal_crumb, |
| 380 | "claim -> goal record (crumb %llu)", (unsigned long long)o.goal_crumb); |
| 381 | uint64_t prov[64]; |
| 382 | uint32_t np = cx_provenance(s, o.cx_promotion, prov, 64); |
| 383 | int has_goal = 0, has_cand = 0; |
| 384 | for (uint32_t i = 0; i < np; i++) { |
| 385 | has_goal |= prov[i] == cl->links[1]; |
| 386 | has_cand |= prov[i] == cl->links[0]; |
| 387 | } |
| 388 | CHECK(has_goal && has_cand, "promotion provenance reaches candidate and goal crumbs"); |
| 389 | const CxObject *pr = cx_get(s, o.cx_promotion), *ev = cx_get(s, o.cx_evidence); |
| 390 | CHECK(pr && pr->kind == CX_K_PROMOTION && pr->links[0] == wc && pr->links[1] == o.cx_evidence, |
| 391 | "promotion of the winner on the evidence"); |
| 392 | CHECK(ev && ev->protect == CX_PROT_VERIFY_EVIDENCE && |
| 393 | cx_payload(s, ev)[RXC_EP_VERIFIER] == RXC_SUBJ_AEGIS && |
| 394 | cx_payload(s, ev)[RXC_EP_PASSMASK] == 2, "evidence from the AEGIS verifier"); |
| 395 | const CxObject *sr = ev ? cx_get(s, ev->links[3]) : NULL; |
| 396 | CHECK(sr && sr->kind == CX_K_EXEC_COMMIT && |
| 397 | cx_payload(s, sr)[CX_WREC_FIELD0 + RXC_S_REF] == fx_pack(o.new_ref), |
| 398 | "evidence -> state World record naming the branch"); |
| 399 | /* The loser is admitted as a loser and never promoted. */ |
| 400 | const CxObject *la = cx_get(s, o.cx_admission[0]); |
| 401 | CHECK(la && la->tag == RXC_ADMIT_LOSER && la->links[0] == lc, "loser admission"); |
| 402 | uint32_t lp = 0; |
| 403 | for (uint64_t id = 1; id <= s->n; id++) { |
| 404 | const CxObject *x = cx_get(s, id); |
| 405 | if (x && x->kind == CX_K_PROMOTION && x->links[0] == lc) lp++; |
| 406 | } |
| 407 | CHECK(lp == 0, "loser claim never promoted"); |
| 408 | CHECK(cx_payload(s, cx_get(s, lc))[RXC_CP_PASS] == 0, "loser claim records the failure"); |
| 409 | uint64_t n_before = s->n; |
| 410 | fx_close(&g_fx, &g_c); |
| 411 | |
| 412 | /* Replay: reopen verifies the chain; recall finds the claim, verified. */ |
| 413 | reset_c(); |
| 414 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "reopen"); |
| 415 | s = &g_c.cx; |
| 416 | CHECK(ref_eq(rx_compose_state(&g_c), o.new_ref), "branch identity survives recording"); |
| 417 | CHECK(cx_verify_chain(s) == CX_OK, "journal chain verifies"); |
| 418 | CxFilter f = { CX_CLAIM, CX_K_CANDIDATE, 0, 1 }; |
| 419 | CxRecord rec[16]; |
| 420 | uint32_t nr = cx_recall(s, RXC_CX_SUBJECT(RXC_SLOT_STATE), 0, UINT64_MAX, &f, rec, 16); |
| 421 | int found = 0; |
| 422 | for (uint32_t i = 0; i < nr; i++) |
| 423 | if (rec[i].hdr.id == wc && rec[i].verified && |
| 424 | rec[i].payload[RXC_CP_REF] == fx_pack(o.new_ref)) found = 1; |
| 425 | CHECK(found, "recall finds the verified winner claim (%u records)", nr); |
| 426 | CHECK(fx_count(s, CX_K_PROMOTION, UINT64_MAX) == 1, "still one promotion"); |
| 427 | uint64_t n_reopen = s->n; |
| 428 | CHECK(n_reopen > n_before, "reopen records the recovered entities"); |
| 429 | fx_close(&g_fx, &g_c); |
| 430 | |
| 431 | /* Torn tail: cut into the last record. Repair drops it; nothing else. */ |
| 432 | char p[256]; |
| 433 | snprintf(p, sizeof p, "%s/cortex.cx", d); |
| 434 | struct stat sb; |
| 435 | CHECK(stat(p, &sb) == 0 && truncate(p, sb.st_size - 3) == 0, "tear the tail"); |
| 436 | reset_c(); |
| 437 | int rc = fx_open(&g_fx, &g_c, d, 1); |
| 438 | CHECK(rc == RX_OK, "torn tail repaired on open (%d)", rc); |
| 439 | if (rc == RX_OK) { |
| 440 | CHECK(ref_eq(rx_compose_state(&g_c), o.new_ref), "state after repair"); |
| 441 | CHECK(cx_verify_chain(&g_c.cx) == CX_OK, "chain after repair"); |
| 442 | fx_close(&g_fx, &g_c); |
| 443 | } |
| 444 | /* Admission cut short by a real crash: the process dies after the first |
| 445 | * composition record and before the loser admission, so the checkpoint |
| 446 | * still holds the previous anchor. Open completes the rest. A winning |
| 447 | * composition is 5 records (2 claims, evidence, promotion, 1 loser |
| 448 | * admission); one was written, so 4 are completed at open. */ |
| 449 | char d2[200]; |
| 450 | dir_for(d2, sizeof d2, "cortex2"); |
| 451 | fflush(NULL); |
| 452 | pid_t pid = fork(); |
| 453 | if (pid == 0) { |
| 454 | reset_c(); |
| 455 | g_c.test.fault_point = RXC_FP_CORTEX; |
| 456 | g_c.test.fault_crash = 1; |
| 457 | if (fx_open(&g_fx, &g_c, d2, 1) != RX_OK) _exit(2); |
| 458 | RxcResult oc; |
| 459 | fx_run(&g_fx, &g_c, 5, &oc); |
| 460 | _exit(3); /* the fault did not fire */ |
| 461 | } |
| 462 | int cst = 0; |
| 463 | waitpid(pid, &cst, 0); |
| 464 | CHECK(WIFEXITED(cst) && WEXITSTATUS(cst) == RXC_CRASH_EXIT, "crash mid-admission (%d)", |
| 465 | WIFEXITED(cst) ? WEXITSTATUS(cst) : -1); |
| 466 | reset_c(); |
| 467 | int rc2 = fx_open(&g_fx, &g_c, d2, 1); |
| 468 | CHECK(rc2 == RX_OK && g_c.recovered_completed == 4, "torn admission rewritten at open (%d, %u)", |
| 469 | rc2, g_c.recovered_completed); |
| 470 | if (rc2 == RX_OK) { |
| 471 | CHECK(fx_count(&g_c.cx, CX_K_ADMISSION, RXC_ADMIT_LOSER) == 1 && |
| 472 | fx_count(&g_c.cx, CX_K_ADMISSION, RXC_ADMIT_RECOVERED) == 1, "completed + marked"); |
| 473 | fx_close(&g_fx, &g_c); |
| 474 | } |
| 475 | |
| 476 | /* Artificial tear: a clean commit anchored the last admission, then the |
| 477 | * record is cut away. That is durable data that vanished, so open refuses |
| 478 | * (the checkpoint anchor is ahead of the journal). */ |
| 479 | char d3[200]; |
| 480 | dir_for(d3, sizeof d3, "cortex3"); |
| 481 | reset_c(); |
| 482 | CHECK(fx_open(&g_fx, &g_c, d3, 1) == RX_OK, "open 3"); |
| 483 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "commit 3"); |
| 484 | CHECK(o.cx_admission[1] == g_c.cx.n, "loser admission is the last record"); |
| 485 | fx_close(&g_fx, &g_c); |
| 486 | snprintf(p, sizeof p, "%s/cortex.cx", d3); |
| 487 | CHECK(stat(p, &sb) == 0 && truncate(p, sb.st_size - 3) == 0, "tear the anchored admission"); |
| 488 | reset_c(); |
| 489 | rc = fx_open(&g_fx, &g_c, d3, 1); |
| 490 | CHECK(rc == RX_ERR_REPLAY, "anchored record lost is refused (%d)", rc); |
| 491 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 492 | rmtree(d3); |
| 493 | snprintf(p, sizeof p, "%s/cortex.cx", d2); |
| 494 | |
| 495 | /* Corrupt middle: one flipped byte in the body is refused, not repaired. */ |
| 496 | CHECK(stat(p, &sb) == 0, "stat"); |
| 497 | FILE *fp = fopen(p, "r+b"); |
| 498 | if (fp) { |
| 499 | fseek(fp, sb.st_size / 2, SEEK_SET); |
| 500 | int ch = fgetc(fp); |
| 501 | fseek(fp, sb.st_size / 2, SEEK_SET); |
| 502 | fputc(ch ^ 0x5A, fp); |
| 503 | fclose(fp); |
| 504 | } |
| 505 | reset_c(); |
| 506 | rc = fx_open(&g_fx, &g_c, d2, 1); |
| 507 | CHECK(rc == RX_ERR_REPLAY, "corrupt middle refused (%d)", rc); |
| 508 | if (rc == RX_OK) fx_close(&g_fx, &g_c); |
| 509 | rmtree(d); |
| 510 | rmtree(d2); |
| 511 | } |
| 512 | |
| 513 | /* The first goal's WORK_ACCEPTED record: claims and evidence of goal 1 carry it. */ |
| 514 | static uint64_t first_goal(void) { |
| 515 | for (uint64_t id = 1; id <= g_c.cx.n; id++) { |
| 516 | const CxObject *o = cx_get(&g_c.cx, id); |
| 517 | if (o && o->kind == CX_K_WORK_ACCEPTED && o->subject == RXC_CX_SUBJECT(RXC_SLOT_GOAL)) |
| 518 | return id; |
| 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /* Goal 1's no-winner record is whole: two claims, one evidence, two losers. */ |
| 524 | static void check_nowin_whole(const char *what, uint64_t G) { |
| 525 | CHECK(G && fx_count(&g_c.cx, CX_K_CANDIDATE, G) == 2, "%s: goal 1 claims %u", what, |
| 526 | fx_count(&g_c.cx, CX_K_CANDIDATE, G)); |
| 527 | CHECK(fx_count(&g_c.cx, CX_K_EVIDENCE_REF, G) == 1, "%s: goal 1 evidence", what); |
| 528 | uint32_t losers = 0; |
| 529 | for (uint64_t id = 1; id <= g_c.cx.n; id++) { |
| 530 | const CxObject *o = cx_get(&g_c.cx, id); |
| 531 | if (!o || o->kind != CX_K_ADMISSION || o->tag != RXC_ADMIT_LOSER) continue; |
| 532 | const CxObject *cl = cx_get(&g_c.cx, o->links[0]); |
| 533 | losers += cl && cl->kind == CX_K_CANDIDATE && cl->tag == G; |
| 534 | } |
| 535 | CHECK(losers == 2, "%s: goal 1 losers %u", what, losers); |
| 536 | } |
| 537 | |
| 538 | static void t_pending(void) { |
| 539 | printf("[*] incomplete records: completed before the next run, or at reopen\n"); |
| 540 | char d[200]; |
| 541 | RxcResult o; |
| 542 | |
| 543 | /* (a) no winner, record fails in process; the next run completes it first. */ |
| 544 | dir_for(d, sizeof d, "pend_nowin"); |
| 545 | reset_c(); |
| 546 | fx_a_bad = fx_b_bad = 1; |
| 547 | g_c.test.fault_point = RXC_FP_CORTEX; |
| 548 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "a: open"); |
| 549 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_RECORD_FAILED && g_c.test.fault_hit, |
| 550 | "a: no-winner record failed (%d)", o.outcome); |
| 551 | fx_a_bad = fx_b_bad = 0; |
| 552 | uint64_t G1 = first_goal(); |
| 553 | CHECK(fx_count(&g_c.cx, CX_K_CANDIDATE, G1) == 1, "a: record partial"); |
| 554 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 19, |
| 555 | "a: next run commits (%d)", o.outcome); |
| 556 | CHECK(o.prior_completed == 4, "a: completed first %u", o.prior_completed); |
| 557 | check_nowin_whole("a", G1); |
| 558 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 1, "a: only goal 2 promoted"); |
| 559 | JsBranchRef nw = o.new_ref; |
| 560 | fx_close(&g_fx, &g_c); |
| 561 | reset_c(); |
| 562 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK && g_c.recovered_completed == 0 && |
| 563 | g_c.rolled_back == 0 && ref_eq(rx_compose_state(&g_c), nw), "a: reopen clean (%u)", |
| 564 | g_c.recovered_completed); |
| 565 | fx_close(&g_fx, &g_c); |
| 566 | rmtree(d); |
| 567 | |
| 568 | /* (b) no winner, crash inside the record; reopen completes it. */ |
| 569 | dir_for(d, sizeof d, "pend_nowin_crash"); |
| 570 | fflush(NULL); |
| 571 | pid_t pid = fork(); |
| 572 | if (pid == 0) { |
| 573 | reset_c(); |
| 574 | fx_a_bad = fx_b_bad = 1; |
| 575 | g_c.test.fault_point = RXC_FP_CORTEX; |
| 576 | g_c.test.fault_crash = 1; |
| 577 | if (fx_open(&g_fx, &g_c, d, 1) != RX_OK) _exit(2); |
| 578 | fx_run(&g_fx, &g_c, 5, &o); |
| 579 | _exit(3); |
| 580 | } |
| 581 | int st = 0; |
| 582 | waitpid(pid, &st, 0); |
| 583 | CHECK(WIFEXITED(st) && WEXITSTATUS(st) == RXC_CRASH_EXIT, "b: child crashed in the record (%d)", |
| 584 | WIFEXITED(st) ? WEXITSTATUS(st) : -1); |
| 585 | reset_c(); |
| 586 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "b: reopen"); |
| 587 | CHECK(g_c.recovered_completed > 0 && g_c.rolled_back == 0, "b: completed %u at open", |
| 588 | g_c.recovered_completed); |
| 589 | check_nowin_whole("b", first_goal()); |
| 590 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 0, "b: nothing promoted"); |
| 591 | CHECK(fx_count(&g_c.cx, CX_K_ADMISSION, RXC_ADMIT_RECOVERED) == 1, "b: one recovered admission"); |
| 592 | const CxObject *ro = cx_get(&g_c.cx, g_c.recovered_record); |
| 593 | CHECK(ro && ro->kind == CX_K_ENTITY_CREATED, "b: OLD kept"); |
| 594 | fx_close(&g_fx, &g_c); |
| 595 | reset_c(); |
| 596 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK && g_c.recovered_completed == 0, "b: second reopen clean"); |
| 597 | fx_close(&g_fx, &g_c); |
| 598 | rmtree(d); |
| 599 | |
| 600 | /* (c) winner, record fails in process; the next run completes it first. */ |
| 601 | dir_for(d, sizeof d, "pend_win"); |
| 602 | reset_c(); |
| 603 | g_c.test.fault_point = RXC_FP_CORTEX; |
| 604 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "c: open"); |
| 605 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_RECORD_FAILED, |
| 606 | "c: record failed (%d)", o.outcome); |
| 607 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 0, "c: winner not yet promoted"); |
| 608 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED && o.result == 19, |
| 609 | "c: next run commits (%d)", o.outcome); |
| 610 | CHECK(o.prior_completed > 0, "c: completed first %u", o.prior_completed); |
| 611 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 2, "c: both winners promoted"); |
| 612 | CHECK(fx_live_branches(&g_c.js) == 1, "c: superseded branches released (%u)", |
| 613 | fx_live_branches(&g_c.js)); |
| 614 | nw = o.new_ref; |
| 615 | fx_close(&g_fx, &g_c); |
| 616 | reset_c(); |
| 617 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK && g_c.recovered_completed == 0 && |
| 618 | ref_eq(rx_compose_state(&g_c), nw), "c: reopen clean (%u)", g_c.recovered_completed); |
| 619 | fx_close(&g_fx, &g_c); |
| 620 | rmtree(d); |
| 621 | |
| 622 | /* (d) NOT_DURABLE: no further run until reopened. */ |
| 623 | dir_for(d, sizeof d, "pend_notdurable"); |
| 624 | reset_c(); |
| 625 | g_c.test.fault_point = RXC_FP_RECLAIM; |
| 626 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "d: open"); |
| 627 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_NOT_DURABLE, |
| 628 | "d: not durable (%d)", o.outcome); |
| 629 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_ERR_REPLAY, "d: next run refused"); |
| 630 | fx_close(&g_fx, &g_c); |
| 631 | reset_c(); |
| 632 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK && g_c.rolled_back == 1, "d: reopen rolls back"); |
| 633 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, "d: then commits"); |
| 634 | fx_close(&g_fx, &g_c); |
| 635 | rmtree(d); |
| 636 | |
| 637 | /* (e) an earlier durable winner whose record was never completed, under a |
| 638 | * newer commit (pending bypassed): reopen completes the older one too. */ |
| 639 | dir_for(d, sizeof d, "pend_older"); |
| 640 | reset_c(); |
| 641 | g_c.test.fault_point = RXC_FP_CORTEX; |
| 642 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "e: open"); |
| 643 | CHECK(fx_run(&g_fx, &g_c, 5, &o) == RX_OK && o.outcome == RXC_OUT_RECORD_FAILED, |
| 644 | "e: record failed (%d)", o.outcome); |
| 645 | g_c.pending = 0; /* simulate a writer that did not complete it */ |
| 646 | CHECK(fx_run(&g_fx, &g_c, 6, &o) == RX_OK && o.outcome == RXC_OUT_COMMITTED, |
| 647 | "e: newer commit (%d)", o.outcome); |
| 648 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 1, "e: older winner unpromoted"); |
| 649 | nw = o.new_ref; |
| 650 | fx_close(&g_fx, &g_c); |
| 651 | reset_c(); |
| 652 | CHECK(fx_open(&g_fx, &g_c, d, 1) == RX_OK, "e: reopen"); |
| 653 | CHECK(g_c.recovered_completed > 0 && g_c.rolled_back == 0 && ref_eq(rx_compose_state(&g_c), nw), |
| 654 | "e: older record completed at open (%u)", g_c.recovered_completed); |
| 655 | CHECK(fx_count(&g_c.cx, CX_K_PROMOTION, UINT64_MAX) == 2, "e: older winner promoted"); |
| 656 | CHECK(fx_live_branches(&g_c.js) == 1, "e: one durable branch"); |
| 657 | fx_close(&g_fx, &g_c); |
| 658 | rmtree(d); |
| 659 | } |
| 660 | |
| 661 | /* A dir refused by src/omega_path_check.h is an argument error and creates nothing. */ |
| 662 | static void t_refused_dir(void) { |
| 663 | printf("[*] refused dir: RX_ERR_ARG, nothing created\n"); |
| 664 | char bad[240]; |
| 665 | snprintf(bad, sizeof bad, "%s/refused dir", g_base); |
| 666 | reset_c(); |
| 667 | int rc = fx_open(&g_fx, &g_c, bad, 1); |
| 668 | CHECK(rc == RX_ERR_ARG, "refused dir -> RX_ERR_ARG (%d)", rc); |
| 669 | CHECK(access(bad, F_OK) != 0, "refused dir not created"); |
| 670 | } |
| 671 | |
| 672 | int main(void) { |
| 673 | snprintf(g_base, sizeof g_base, "/tmp/rx_compose_test.XXXXXX"); |
| 674 | if (!mkdtemp(g_base)) { perror("mkdtemp"); return 1; } |
| 675 | if (fx_init(&g_fx) != 0) { fprintf(stderr, "fixture init failed\n"); return 1; } |
| 676 | t_reference(); |
| 677 | t_faults(); |
| 678 | t_no_winner(); |
| 679 | t_authority(); |
| 680 | t_cortex(); |
| 681 | t_pending(); |
| 682 | t_refused_dir(); |
| 683 | fx_free(&g_fx); |
| 684 | rmtree(g_base); |
| 685 | printf("%s: %d checks, %d failed\n", g_fail ? "FAIL" : "PASS", g_checks, g_fail); |
| 686 | return g_fail ? 1 : 0; |
| 687 | } |