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 / src / runtime / rx_compose.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_compose.c -- COMPOSITION-2: one World, one causal path (rx_compose.h). |
| 19 | * |
| 20 | * Persistent layout in the composition directory: |
| 21 | * machine.id canonical AienMachineId the composition belongs to |
| 22 | * cortex.cx Cortex journal (World records + composition records) |
| 23 | * jspace/ durable J-Space (sealed state branches only) |
| 24 | */ |
| 25 | #include "rx_compose.h" |
| 26 | #include "rx_cortex_record.h" |
| 27 | #include "sha256.h" |
| 28 | #include "omega_path_check.h" |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #define RXC_ISSUER 3u /* AIENOS issuer: system owner policy office */ |
| 38 | #define RXC_STATE_SUBJECT RXC_CX_SUBJECT(RXC_SLOT_STATE) |
| 39 | |
| 40 | /* ---- deterministic realizer of the state unit ---------------------------- */ |
| 41 | |
| 42 | static uint64_t rxc_mix(uint64_t x) { |
| 43 | x ^= x >> 30; x *= 0xbf58476d1ce4e5b9ull; |
| 44 | x ^= x >> 27; x *= 0x94d049bb133111ebull; |
| 45 | return x ^ (x >> 31); |
| 46 | } |
| 47 | |
| 48 | static void rxc_derive(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { |
| 49 | uint64_t h = rxc_mix(token ^ 0xC0A1E5CEull); |
| 50 | for (size_t i = 0; i < n; i += 8) { |
| 51 | uint64_t p = 0; |
| 52 | if (prev) memcpy(&p, prev + i, n - i < 8 ? n - i : 8); |
| 53 | h = rxc_mix(h + p + i); |
| 54 | memcpy(out + i, &h, n - i < 8 ? n - i : 8); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const JsRealizer RXC_REALIZER = { JS_REAL_WORLD_PROJECTION, "rxc_state", RXC_UNIT, rxc_derive }; |
| 59 | |
| 60 | /* ---- small helpers -------------------------------------------------------- */ |
| 61 | |
| 62 | static void path_in(char *out, size_t n, const char *dir, const char *name) { |
| 63 | snprintf(out, n, "%s/%s", dir, name); |
| 64 | } |
| 65 | |
| 66 | static uint64_t ref_pack(JsBranchRef r) { return js_branch_ref_pack(r); } |
| 67 | static JsBranchRef ref_unpack(uint64_t v) { return js_branch_ref_unpack(v); } |
| 68 | static int ref_eq(JsBranchRef a, JsBranchRef b) { return a.id == b.id && a.gen == b.gen; } |
| 69 | |
| 70 | /* One fault point (test builds only, -DAIEN_TEST_BUILD=1). Fires once; |
| 71 | * candidate-side points fire only for fault_k. Returns 1 when the caller must |
| 72 | * fail in process. Without the test build it never fires. */ |
| 73 | #ifdef RXC_TEST_HOOKS |
| 74 | /* Link-map marker: the production hygiene check (tools/r16_prod_hygiene.sh) |
| 75 | * refuses any binary that carries an aien_test_build_* symbol. */ |
| 76 | __attribute__((used)) const char aien_test_build_rx_compose_hooks[] = |
| 77 | "AIEN_TEST_BUILD piece: rx_compose test hooks (fault points, rogue candidate, hold point)"; |
| 78 | static int fault(RxCompose *c, int point, uint32_t k) { |
| 79 | if (c->test.fault_point != point) return 0; |
| 80 | if ((point == RXC_FP_BEFORE_FORK || point == RXC_FP_CANDIDATE) && k != c->test.fault_k) |
| 81 | return 0; |
| 82 | if (__atomic_exchange_n(&c->test.fault_hit, 1, __ATOMIC_ACQ_REL)) return 0; |
| 83 | if (c->test.fault_crash) _exit(RXC_CRASH_EXIT); |
| 84 | return 1; |
| 85 | } |
| 86 | #define RXC_ROGUE(c) ((c)->test.rogue_candidate) |
| 87 | #else |
| 88 | static int fault(RxCompose *c, int point, uint32_t k) { |
| 89 | (void)c; (void)point; (void)k; |
| 90 | return 0; |
| 91 | } |
| 92 | #define RXC_ROGUE(c) 0 |
| 93 | #endif |
| 94 | |
| 95 | /* Hold point (test builds only): see RxcTestHooks.hold_k1. */ |
| 96 | #ifdef RXC_TEST_HOOKS |
| 97 | static void hold_point(RxCompose *c, uint32_t k) { |
| 98 | if (c->test.hold_k1 != k + 1u) return; |
| 99 | if (__atomic_exchange_n(&c->test.held, 1, __ATOMIC_ACQ_REL)) return; |
| 100 | struct timespec ts = { 0, 100000 }; |
| 101 | while (!__atomic_load_n(&c->test.release, __ATOMIC_ACQUIRE)) nanosleep(&ts, NULL); |
| 102 | __atomic_store_n(&c->test.hold_done, 1, __ATOMIC_RELEASE); |
| 103 | } |
| 104 | #else |
| 105 | static void hold_point(RxCompose *c, uint32_t k) { (void)c; (void)k; } |
| 106 | #endif |
| 107 | |
| 108 | static const RxSnapshotDep *dep_of(const RxCtx *x, RxObjRef o) { |
| 109 | for (uint32_t i = 0; i < x->n_in; i++) |
| 110 | if (x->in[i].obj.id == o.id && x->in[i].obj.generation == o.generation) return &x->in[i]; |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | static void out_put(RxCtx *x, RxObjRef o, uint32_t f, uint64_t v) { |
| 115 | if (x->n_out < RX_MAX_MUTATIONS) x->out[x->n_out++] = (RxMutation){ o, f, v }; |
| 116 | } |
| 117 | |
| 118 | static const AgSkill *skill_of(const RxCompose *c, uint32_t id) { |
| 119 | const AgSkillTable *t = c->router ? c->router->skills : NULL; |
| 120 | for (uint32_t i = 0; t && i < t->n; i++) |
| 121 | if (t->skill[i].id == id) return &t->skill[i]; |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | static int digest_zero(const uint8_t d[32]) { |
| 126 | uint8_t a = 0; |
| 127 | for (int i = 0; i < 32; i++) a |= d[i]; |
| 128 | return a == 0; |
| 129 | } |
| 130 | |
| 131 | static void words_from_digest(const uint8_t d[32], uint64_t *w) { |
| 132 | for (int i = 0; i < 4; i++) { |
| 133 | uint64_t v = 0; |
| 134 | for (int b = 0; b < 8; b++) v |= (uint64_t)d[8 * i + b] << (8 * b); |
| 135 | w[i] = v; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* ---- reactions ------------------------------------------------------------ */ |
| 140 | |
| 141 | /* compose.candidate.k: fork a staged branch from the committed state, run the |
| 142 | * routed Skill, derive the branch with the result, propose it in cand[k]. */ |
| 143 | static int candidate_fn(RxCtx *x) { |
| 144 | struct RxcCandUser *u = x->user; |
| 145 | RxCompose *c = u->c; |
| 146 | uint32_t k = u->k; |
| 147 | const RxSnapshotDep *g = dep_of(x, c->goal), *st = dep_of(x, c->state); |
| 148 | if (!g || !st) return 0; |
| 149 | uint64_t seq = g->field[RXC_G_SEQ]; |
| 150 | if (seq == 0) return 0; |
| 151 | uint64_t skill_word = g->field[k == 0 ? RXC_G_SKILL0 : RXC_G_SKILL1]; |
| 152 | uint64_t ref = 0, result = 0, home = RXC_HOME_LOCAL; |
| 153 | const SrRoute *rt = k < c->n_routes ? &c->run_route[k] : NULL; |
| 154 | const AgSkill *s = NULL; |
| 155 | int runnable = 0; |
| 156 | if (rt && skill_word != 0 && rt->verdict == SR_OK) { |
| 157 | s = skill_of(c, (uint32_t)skill_word); |
| 158 | const uint8_t *want = rt->skill_digest; |
| 159 | runnable = s && s->fn && (digest_zero(want) || memcmp(want, s->identity, 32) == 0); |
| 160 | } else if (rt && skill_word != 0 && rt->verdict == SR_E_REMOTE) { |
| 161 | /* The provider is on another machine: the Fabric runs it there (the |
| 162 | * hook revalidates the route and the machine's lease first). The |
| 163 | * claim names that machine's advertised procedure whether it ran, |
| 164 | * was refused or no hook is set (never a local identity). */ |
| 165 | home = RXC_HOME_FABRIC; |
| 166 | memcpy(c->remote.digest[k], rt->skill_digest, 32); |
| 167 | c->remote.seq[k] = seq; |
| 168 | runnable = c->remote.run != NULL; |
| 169 | } |
| 170 | if (runnable) { |
| 171 | if (fault(c, RXC_FP_BEFORE_FORK, k)) return -1; |
| 172 | JsBranchRef base = ref_unpack(st->field[RXC_S_REF]), next; |
| 173 | uint32_t subj = c->subj_cand[k]; |
| 174 | if (js_branch_fork_staged(&c->js, base, subj, &next) != JS_OK) return -1; |
| 175 | uint64_t in = g->field[RXC_G_INPUT]; |
| 176 | int failed = 0; |
| 177 | uint64_t r = 0; |
| 178 | if (home == RXC_HOME_LOCAL) { |
| 179 | r = s->fn(&in, 1, 0, &failed); |
| 180 | } else if (c->remote.run(c->remote.ctx, c->router, rt, in, c->run_now, &r) != 0) { |
| 181 | failed = 1; |
| 182 | __atomic_add_fetch(&c->remote.refused, 1, __ATOMIC_RELAXED); |
| 183 | } else { |
| 184 | __atomic_add_fetch(&c->remote.ran, 1, __ATOMIC_RELAXED); |
| 185 | } |
| 186 | if (failed || js_branch_derive(&c->js, next.id, r) != JS_OK) { |
| 187 | js_branch_release_ref(&c->js, next, subj); |
| 188 | } else { |
| 189 | if (fault(c, RXC_FP_CANDIDATE, k)) return -1; |
| 190 | ref = ref_pack(next); |
| 191 | result = r; |
| 192 | if (k == 0 && RXC_ROGUE(c)) |
| 193 | out_put(x, c->state, RXC_S_REF, ref); /* refused: not in its write set */ |
| 194 | } |
| 195 | } |
| 196 | hold_point(c, k); |
| 197 | RxObjRef me = c->cand[k]; |
| 198 | out_put(x, me, RXC_C_REF, ref); |
| 199 | out_put(x, me, RXC_C_RESULT, result); |
| 200 | out_put(x, me, RXC_C_SKILL, skill_word); |
| 201 | out_put(x, me, RXC_C_HOME, home); |
| 202 | out_put(x, me, RXC_C_GOAL, seq); |
| 203 | out_put(x, me, RXC_C_DONE, seq); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* compose.verify (AEGIS): the only subject that can write the verdict. */ |
| 208 | static int verify_fn(RxCtx *x) { |
| 209 | RxCompose *c = x->user; |
| 210 | const RxSnapshotDep *g = dep_of(x, c->goal); |
| 211 | const RxSnapshotDep *cd[RXC_K] = { dep_of(x, c->cand[0]), dep_of(x, c->cand[1]) }; |
| 212 | if (!g || !cd[0] || !cd[1]) return 0; |
| 213 | uint64_t seq = g->field[RXC_G_SEQ]; |
| 214 | if (seq == 0) return 0; |
| 215 | for (uint32_t k = 0; k < RXC_K; k++) |
| 216 | if (cd[k]->field[RXC_C_DONE] != seq) return 0; /* wait for every candidate */ |
| 217 | uint64_t input = g->field[RXC_G_INPUT]; |
| 218 | uint32_t winner = RXC_NONE, passmask = 0; |
| 219 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 220 | int pass = cd[k]->field[RXC_C_REF] != 0 && cd[k]->field[RXC_C_GOAL] == seq && |
| 221 | c->contract && c->contract(input, cd[k]->field[RXC_C_RESULT]); |
| 222 | if (pass) { |
| 223 | passmask |= 1u << k; |
| 224 | if (winner == RXC_NONE) winner = k; |
| 225 | } |
| 226 | } |
| 227 | uint64_t wref = winner != RXC_NONE ? cd[winner]->field[RXC_C_REF] : 0; |
| 228 | uint64_t lref = cd[winner == 0 ? 1 : 0]->field[RXC_C_REF]; |
| 229 | uint64_t res = winner != RXC_NONE ? cd[winner]->field[RXC_C_RESULT] : 0; |
| 230 | out_put(x, c->verdict, RXC_V_WINNER, winner); |
| 231 | out_put(x, c->verdict, RXC_V_WREF, wref); |
| 232 | out_put(x, c->verdict, RXC_V_LREF, lref); |
| 233 | out_put(x, c->verdict, RXC_V_PASSMASK, passmask); |
| 234 | out_put(x, c->verdict, RXC_V_GOAL, seq); |
| 235 | out_put(x, c->verdict, RXC_V_RESULT, res); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* compose.commit: names the winner in the state object. */ |
| 240 | static int commit_fn(RxCtx *x) { |
| 241 | RxCompose *c = x->user; |
| 242 | const RxSnapshotDep *v = dep_of(x, c->verdict); |
| 243 | if (!v || v->field[RXC_V_GOAL] == 0 || v->field[RXC_V_WINNER] == RXC_NONE) return 0; |
| 244 | if (fault(c, RXC_FP_BEFORE_COMMIT, 0)) return -1; |
| 245 | out_put(x, c->state, RXC_S_REF, v->field[RXC_V_WREF]); |
| 246 | out_put(x, c->state, RXC_S_RESULT, v->field[RXC_V_RESULT]); |
| 247 | out_put(x, c->state, RXC_S_GOAL, v->field[RXC_V_GOAL]); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* ---- World commit binder: state.REF names a J-Space branch ---------------- */ |
| 252 | |
| 253 | static int bind_check(void *ctx, RxObjRef obj, uint32_t field, uint64_t old_value, |
| 254 | uint64_t new_value, uint32_t subject) { |
| 255 | RxCompose *c = ctx; |
| 256 | if (obj.id != c->state.id || field != RXC_S_REF) return RX_ERR_BINDING; |
| 257 | if (subject != c->subj_commit) return RX_ERR_BINDING; |
| 258 | JsBranchRef old = ref_unpack(old_value), nw = ref_unpack(new_value); |
| 259 | JsBranchInfo bi; |
| 260 | if (js_branch_info(&c->js, nw, &bi) != JS_OK) return RX_ERR_BINDING; |
| 261 | if (!bi.staged || bi.locality != JS_HOME_LOCAL) return RX_ERR_BINDING; |
| 262 | if (bi.owner != c->subj_cand[0] && bi.owner != c->subj_cand[1]) return RX_ERR_BINDING; |
| 263 | if (bi.parent != old.id || bi.parent_gen != old.gen) return RX_ERR_BINDING; |
| 264 | if (fault(c, RXC_FP_AFTER_VALIDATION, 0)) return RX_ERR_BINDING; |
| 265 | return RX_OK; |
| 266 | } |
| 267 | |
| 268 | static int bind_fn(void *ctx, RxObjRef obj, uint32_t field, uint64_t old_value, |
| 269 | uint64_t new_value, uint32_t subject) { |
| 270 | (void)obj; (void)field; (void)old_value; (void)subject; |
| 271 | RxCompose *c = ctx; |
| 272 | JsBranchRef nw = ref_unpack(new_value); |
| 273 | #ifdef RXC_TEST_HOOKS |
| 274 | if (c->test.fault_point == RXC_FP_SEAL && |
| 275 | !__atomic_exchange_n(&c->test.fault_hit, 1, __ATOMIC_ACQ_REL)) { |
| 276 | if (!c->test.fault_crash) return RX_ERR_BINDING; /* seal refused */ |
| 277 | js_branch_seal(&c->js, nw); |
| 278 | js_branch_set_owner(&c->js, nw, 0); |
| 279 | _exit(RXC_CRASH_EXIT); /* crash right after the seal */ |
| 280 | } |
| 281 | #endif |
| 282 | if (js_branch_seal(&c->js, nw) != JS_OK) return RX_ERR_BINDING; |
| 283 | js_branch_set_owner(&c->js, nw, 0); |
| 284 | return RX_OK; |
| 285 | } |
| 286 | |
| 287 | static void bind_abort(void *ctx, RxObjRef obj, uint32_t field, uint64_t value, |
| 288 | uint32_t subject, int was_bound) { |
| 289 | (void)obj; (void)field; (void)subject; |
| 290 | RxCompose *c = ctx; |
| 291 | if (was_bound) js_branch_release_ref(&c->js, ref_unpack(value), 0); |
| 292 | /* not bound: still staged, reclaimed at settle */ |
| 293 | } |
| 294 | |
| 295 | /* ---- Cortex helpers -------------------------------------------------------- */ |
| 296 | |
| 297 | static int cx_put(RxCompose *c, CxHeader *h, const uint64_t *p, uint32_t n, uint64_t *id) { |
| 298 | if (c->attached) { |
| 299 | h->t = rx_cortex_next_t_in(c->world, &c->cx); |
| 300 | return rx_cortex_append_in(c->world, &c->cx, h, p, n, id) == RX_OK ? 0 : -1; |
| 301 | } |
| 302 | h->t = c->cx.n + 1; |
| 303 | return cx_append(&c->cx, h, p, n, id) == CX_OK ? 0 : -1; |
| 304 | } |
| 305 | |
| 306 | static int cx_prom(RxCompose *c, uint64_t cand, uint64_t ev, uint64_t *id) { |
| 307 | if (c->attached) |
| 308 | return rx_cortex_promote_in(c->world, &c->cx, cand, ev, id) == RX_OK ? 0 : -1; |
| 309 | return cx_promote(&c->cx, 0, cand, ev, c->cx.n + 1, id) == CX_OK ? 0 : -1; |
| 310 | } |
| 311 | |
| 312 | static const uint64_t *payload_of(const CxStore *s, uint64_t id, uint32_t min_words) { |
| 313 | const CxObject *o = cx_get(s, id); |
| 314 | if (!o || o->n < min_words) return NULL; |
| 315 | return cx_payload(s, o); |
| 316 | } |
| 317 | |
| 318 | /* Newest record of `kind` about `slot` with id < before (0 = none). */ |
| 319 | static uint64_t latest_before(CxStore *s, uint32_t slot, uint32_t cls, uint32_t kind, |
| 320 | uint64_t before) { |
| 321 | CxFilter f = { cls, kind, 0, 1 }; |
| 322 | return cx_latest(s, RXC_CX_SUBJECT(slot), before > 1 ? before - 1 : 0, &f); |
| 323 | } |
| 324 | |
| 325 | /* C4: 1 if a Cortex promotion names a winner claim whose branch is `packed_ref`. |
| 326 | * A promotion is written only after its state is durable (settle order), so a |
| 327 | * promoted state that J-Space no longer holds is a regressed checkpoint, not a |
| 328 | * crash window: recovery must refuse it rather than roll it back. */ |
| 329 | static int state_ref_promoted(const CxStore *s, uint64_t packed_ref) { |
| 330 | for (uint64_t id = 1; id <= s->n; id++) { |
| 331 | const CxObject *o = cx_get(s, id); |
| 332 | if (!o || o->kind != CX_K_PROMOTION) continue; |
| 333 | const CxObject *claim = cx_get(s, o->links[0]); |
| 334 | if (!claim || claim->kind != CX_K_CANDIDATE) continue; |
| 335 | const uint64_t *pl = payload_of(s, claim->id, RXC_CP_WORDS); |
| 336 | if (pl && pl[RXC_CP_REF] == packed_ref) return 1; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int has_admission(CxStore *s, uint64_t tag, uint64_t link0) { |
| 342 | const CxIdList *l = &s->by_subject[RXC_STATE_SUBJECT]; |
| 343 | for (uint32_t i = 0; i < l->n; i++) { |
| 344 | const CxObject *o = cx_get(s, l->ids[i]); |
| 345 | if (o && o->kind == CX_K_ADMISSION && o->tag == tag && o->links[0] == link0) return 1; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* The composition record of one goal. S = state World record of the commit (0 |
| 351 | * when no winner), V = the verdict World record. Records are written in a |
| 352 | * fixed order (claim 0, claim 1, evidence, [promotion], loser admissions); |
| 353 | * the ones already present after S/V are reused, so recovery can complete an |
| 354 | * interrupted record. `cdig` (may be NULL) holds candidate branch digests |
| 355 | * taken before the reclaim. Returns records written, -1 when an append failed, |
| 356 | * or -2 when its inputs are absent (nothing can be, or was, written). */ |
| 357 | static int compose_records(RxCompose *c, uint64_t S, uint64_t V, const uint8_t (*cdig)[32], |
| 358 | int may_fault, RxcResult *out) { |
| 359 | CxStore *s = &c->cx; |
| 360 | /* Appends may move the Cortex arena: copy every input value out first |
| 361 | * and hold no payload or object pointer across an append. */ |
| 362 | uint64_t vf[8], cf[RXC_K][8], g_input; |
| 363 | const uint64_t *vp = payload_of(s, V, CX_WREC_WORDS); |
| 364 | if (!vp) return -2; |
| 365 | memcpy(vf, vp + CX_WREC_FIELD0, sizeof vf); |
| 366 | uint64_t G = latest_before(s, RXC_SLOT_GOAL, CX_OBSERVATION, CX_K_WORK_ACCEPTED, V); |
| 367 | const uint64_t *gp = payload_of(s, G, CX_WREC_WORDS); |
| 368 | if (!gp) return -2; |
| 369 | g_input = gp[CX_WREC_FIELD0 + RXC_G_INPUT]; |
| 370 | uint64_t Ck[RXC_K]; |
| 371 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 372 | Ck[k] = latest_before(s, RXC_SLOT_CAND0 + k, CX_EXECUTION, CX_K_EXEC_COMMIT, V); |
| 373 | const uint64_t *cp = payload_of(s, Ck[k], CX_WREC_WORDS); |
| 374 | if (!cp) return -2; |
| 375 | memcpy(cf[k], cp + CX_WREC_FIELD0, sizeof cf[k]); |
| 376 | } |
| 377 | uint32_t winner = (uint32_t)vf[RXC_V_WINNER]; |
| 378 | if ((winner == RXC_NONE) != (S == 0)) return -2; |
| 379 | |
| 380 | /* Existing composition records of this goal (claims and evidence carry |
| 381 | * tag G; promotion and loser admissions link one of its claims), in |
| 382 | * append order, after max(S, V) on the state subject. */ |
| 383 | uint64_t after = S > V ? S : V, have[2 + 2 + RXC_K], mine[RXC_K] = { 0, 0 }; |
| 384 | uint32_t n_have = 0, n_mine = 0; |
| 385 | const CxIdList *l = &s->by_subject[RXC_STATE_SUBJECT]; |
| 386 | for (uint32_t i = 0; i < l->n && n_have < 2 + 2 + RXC_K; i++) { |
| 387 | const CxObject *o = cx_get(s, l->ids[i]); |
| 388 | if (!o || o->id <= after) continue; |
| 389 | int ours = 0; |
| 390 | if (o->kind == CX_K_CANDIDATE && o->tag == G) { |
| 391 | ours = 1; |
| 392 | if (n_mine < RXC_K) mine[n_mine++] = o->id; |
| 393 | } else if (o->kind == CX_K_EVIDENCE_REF && o->tag == G) { |
| 394 | ours = 1; |
| 395 | } else if (o->kind == CX_K_PROMOTION || |
| 396 | (o->kind == CX_K_ADMISSION && o->tag == RXC_ADMIT_LOSER)) { |
| 397 | for (uint32_t m = 0; m < n_mine; m++) ours |= o->links[0] == mine[m]; |
| 398 | } |
| 399 | if (ours) have[n_have++] = o->id; |
| 400 | } |
| 401 | uint32_t step = 0; |
| 402 | int written = 0; |
| 403 | #define RXC_STEP(expr_id, ...) \ |
| 404 | do { \ |
| 405 | if (step < n_have) { (expr_id) = have[step]; } \ |
| 406 | else { \ |
| 407 | if (may_fault && written == 1 && fault(c, RXC_FP_CORTEX, 0)) return -1; \ |
| 408 | __VA_ARGS__ \ |
| 409 | written++; \ |
| 410 | } \ |
| 411 | step++; \ |
| 412 | } while (0) |
| 413 | |
| 414 | uint64_t claim[RXC_K] = { 0, 0 }, ev = 0, prom = 0, adm[RXC_K] = { 0, 0 }; |
| 415 | uint8_t wdig[32]; |
| 416 | memset(wdig, 0, sizeof wdig); |
| 417 | if (winner != RXC_NONE) { |
| 418 | JsBranchRef w = ref_unpack(vf[RXC_V_WREF]); |
| 419 | if (js_branch_check(&c->js, w) == JS_OK) js_branch_content_digest(&c->js, w.id, wdig); |
| 420 | } |
| 421 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 422 | RXC_STEP(claim[k], { |
| 423 | uint64_t p[RXC_CP_WORDS]; |
| 424 | memset(p, 0, sizeof p); |
| 425 | p[RXC_CP_K] = k; |
| 426 | p[RXC_CP_REF] = cf[k][RXC_C_REF]; |
| 427 | p[RXC_CP_RESULT] = cf[k][RXC_C_RESULT]; |
| 428 | p[RXC_CP_SKILL] = cf[k][RXC_C_SKILL]; |
| 429 | p[RXC_CP_INPUT] = g_input; |
| 430 | p[RXC_CP_GOALSEQ] = cf[k][RXC_C_GOAL]; |
| 431 | p[RXC_CP_PASS] = (vf[RXC_V_PASSMASK] >> k) & 1u; |
| 432 | uint8_t d[32]; |
| 433 | memset(d, 0, sizeof d); |
| 434 | JsBranchRef r = ref_unpack(cf[k][RXC_C_REF]); |
| 435 | if (cdig) memcpy(d, cdig[k], 32); |
| 436 | else if (r.id || r.gen) { |
| 437 | if (js_branch_check(&c->js, r) == JS_OK) js_branch_content_digest(&c->js, r.id, d); |
| 438 | } |
| 439 | words_from_digest(d, p + RXC_CP_DIGEST0); |
| 440 | /* The procedure that produced it: locally its executable |
| 441 | * identity, which the router admitted only when it equals the |
| 442 | * graph's digest; on a Fabric machine the digest that machine |
| 443 | * advertised (and ran, if the claim has a branch). It is held in |
| 444 | * memory per goal; a pending record is completed only within the |
| 445 | * same open, before the next goal's candidates run. */ |
| 446 | p[RXC_CP_HOME] = cf[k][RXC_C_HOME]; |
| 447 | if (cf[k][RXC_C_HOME] == RXC_HOME_FABRIC) { |
| 448 | if (c->remote.seq[k] && c->remote.seq[k] == cf[k][RXC_C_GOAL]) |
| 449 | words_from_digest(c->remote.digest[k], p + RXC_CP_SKILLDIG0); |
| 450 | } else { |
| 451 | const AgSkill *sk = skill_of(c, (uint32_t)cf[k][RXC_C_SKILL]); |
| 452 | if (sk && cf[k][RXC_C_SKILL]) words_from_digest(sk->identity, p + RXC_CP_SKILLDIG0); |
| 453 | } |
| 454 | CxHeader h; |
| 455 | memset(&h, 0, sizeof h); |
| 456 | h.cls = CX_CLAIM; |
| 457 | h.kind = CX_K_CANDIDATE; |
| 458 | h.subject = RXC_STATE_SUBJECT; |
| 459 | h.tag = G; |
| 460 | h.links[0] = Ck[k]; |
| 461 | h.links[1] = G; |
| 462 | if (cx_put(c, &h, p, RXC_CP_WORDS, &claim[k])) return -1; |
| 463 | }); |
| 464 | } |
| 465 | RXC_STEP(ev, { |
| 466 | uint64_t p[RXC_EP_WORDS]; |
| 467 | memset(p, 0, sizeof p); |
| 468 | p[RXC_EP_WINNER] = winner; |
| 469 | p[RXC_EP_WREF] = vf[RXC_V_WREF]; |
| 470 | p[RXC_EP_LREF] = vf[RXC_V_LREF]; |
| 471 | p[RXC_EP_PASSMASK] = vf[RXC_V_PASSMASK]; |
| 472 | p[RXC_EP_RESULT] = vf[RXC_V_RESULT]; |
| 473 | p[RXC_EP_GOALSEQ] = vf[RXC_V_GOAL]; |
| 474 | p[RXC_EP_VERIFIER] = c->subj_aegis; |
| 475 | words_from_digest(wdig, p + RXC_EP_WDIGEST0); |
| 476 | CxHeader h; |
| 477 | memset(&h, 0, sizeof h); |
| 478 | h.cls = CX_EVIDENCE; |
| 479 | h.kind = CX_K_EVIDENCE_REF; |
| 480 | h.protect = CX_PROT_VERIFY_EVIDENCE; |
| 481 | h.subject = RXC_STATE_SUBJECT; |
| 482 | h.tag = G; |
| 483 | h.links[0] = V; |
| 484 | h.links[1] = claim[0]; |
| 485 | h.links[2] = claim[1]; |
| 486 | h.links[3] = S; |
| 487 | if (cx_put(c, &h, p, RXC_EP_WORDS, &ev)) return -1; |
| 488 | }); |
| 489 | if (winner != RXC_NONE) |
| 490 | RXC_STEP(prom, { if (cx_prom(c, claim[winner], ev, &prom)) return -1; }); |
| 491 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 492 | if (k == winner) continue; |
| 493 | RXC_STEP(adm[k], { |
| 494 | uint64_t p[RXC_AP_WORDS] = { cf[k][RXC_C_REF], vf[RXC_V_GOAL], k }; |
| 495 | CxHeader h; |
| 496 | memset(&h, 0, sizeof h); |
| 497 | h.cls = CX_EVIDENCE; |
| 498 | h.kind = CX_K_ADMISSION; |
| 499 | h.subject = RXC_STATE_SUBJECT; |
| 500 | h.tag = RXC_ADMIT_LOSER; |
| 501 | h.links[0] = claim[k]; |
| 502 | h.links[1] = ev; |
| 503 | if (cx_put(c, &h, p, RXC_AP_WORDS, &adm[k])) return -1; |
| 504 | }); |
| 505 | } |
| 506 | #undef RXC_STEP |
| 507 | if (out) { |
| 508 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 509 | out->cx_candidate[k] = claim[k]; |
| 510 | out->cx_admission[k] = adm[k]; |
| 511 | } |
| 512 | out->cx_evidence = ev; |
| 513 | out->cx_promotion = prom; |
| 514 | memcpy(out->winner_digest, wdig, 32); |
| 515 | } |
| 516 | return written; |
| 517 | } |
| 518 | |
| 519 | /* ---- recovery (OLD-or-NEW), before the World exists ----------------------- */ |
| 520 | |
| 521 | /* Checkpoint the J-Space state together with an anchor naming the Cortex store: |
| 522 | * u64 record count (LE) | digest of the newest record (32 zero bytes if none) | |
| 523 | * u64 flags = 1 (non-zero, so the section reads as present). open_home refuses a |
| 524 | * journal that is shorter than, or disagrees with, the anchor. */ |
| 525 | static int commit_js(RxCompose *c) { |
| 526 | uint8_t a[48] = { 0 }; |
| 527 | uint64_t n = c->cx.n; |
| 528 | for (int i = 0; i < 8; i++) a[i] = (uint8_t)(n >> (8 * i)); |
| 529 | if (n) memcpy(a + 8, c->cx.obj[n - 1].digest, 32); |
| 530 | a[40] = 1; |
| 531 | js_space_set_anchor(&c->js, a); |
| 532 | return js_space_commit(&c->js); |
| 533 | } |
| 534 | |
| 535 | static int recover(RxCompose *c) { |
| 536 | CxStore *s = &c->cx; |
| 537 | const CxIdList *l = &s->by_subject[RXC_STATE_SUBJECT]; |
| 538 | uint64_t chosen = 0; |
| 539 | JsBranchRef ref = { 0, 0 }; |
| 540 | for (uint32_t i = l->n; i-- > 0;) { |
| 541 | const CxObject *o = cx_get(s, l->ids[i]); |
| 542 | if (!o || (o->kind != CX_K_ENTITY_CREATED && o->kind != CX_K_EXEC_COMMIT)) continue; |
| 543 | if (has_admission(s, RXC_ADMIT_ROLLBACK, o->id)) continue; |
| 544 | const uint64_t *p = payload_of(s, o->id, CX_WREC_WORDS); |
| 545 | if (!p) return RX_ERR_REPLAY; |
| 546 | JsBranchRef r = ref_unpack(p[CX_WREC_FIELD0 + RXC_S_REF]); |
| 547 | JsBranchInfo bi; |
| 548 | if (js_branch_info(&c->js, r, &bi) == JS_OK && !bi.staged) { |
| 549 | chosen = o->id; |
| 550 | ref = r; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | /* State was recorded but nothing it names is durable: refuse, never |
| 555 | * invent a genesis over recorded history (the root is made durable |
| 556 | * before the World first records it). */ |
| 557 | if (!chosen) |
| 558 | for (uint32_t i = 0; i < l->n; i++) { |
| 559 | const CxObject *o = cx_get(s, l->ids[i]); |
| 560 | if (o && (o->kind == CX_K_ENTITY_CREATED || o->kind == CX_K_EXEC_COMMIT)) |
| 561 | return RX_ERR_REPLAY; |
| 562 | } |
| 563 | /* Every newer state record did not become durable: answer it. */ |
| 564 | for (uint32_t i = 0; i < l->n; i++) { |
| 565 | const CxObject *o = cx_get(s, l->ids[i]); |
| 566 | if (!o || o->id <= chosen) continue; |
| 567 | if (o->kind != CX_K_ENTITY_CREATED && o->kind != CX_K_EXEC_COMMIT) continue; |
| 568 | if (has_admission(s, RXC_ADMIT_ROLLBACK, o->id)) continue; |
| 569 | { |
| 570 | const uint64_t *pp = payload_of(s, o->id, CX_WREC_WORDS); |
| 571 | if (pp && state_ref_promoted(s, pp[CX_WREC_FIELD0 + RXC_S_REF])) return RX_ERR_REPLAY; |
| 572 | } |
| 573 | const uint64_t *p = payload_of(s, o->id, CX_WREC_WORDS); |
| 574 | uint64_t ap[RXC_AP_WORDS] = { p ? p[CX_WREC_FIELD0 + RXC_S_REF] : 0, |
| 575 | p ? p[CX_WREC_FIELD0 + RXC_S_GOAL] : 0, RXC_NONE }; |
| 576 | CxHeader h; |
| 577 | memset(&h, 0, sizeof h); |
| 578 | h.cls = CX_EVIDENCE; |
| 579 | h.kind = CX_K_ADMISSION; |
| 580 | h.subject = RXC_STATE_SUBJECT; |
| 581 | h.tag = RXC_ADMIT_ROLLBACK; |
| 582 | h.links[0] = o->id; |
| 583 | uint64_t id; |
| 584 | if (cx_put(c, &h, ap, RXC_AP_WORDS, &id)) return RX_ERR_REPLAY; |
| 585 | c->rolled_back++; |
| 586 | } |
| 587 | if (!chosen) { |
| 588 | /* Genesis: drop anything durable, root seeded by the machine identity. */ |
| 589 | /* C4: a lost or emptied Cortex journal must not silently reset a World |
| 590 | * whose J-Space holds committed history. The genesis root is one unit; |
| 591 | * every committed goal derives more. Durable history with no state |
| 592 | * record is refused, never overwritten with a fresh genesis. */ |
| 593 | for (uint32_t b = 0; b < c->js.n_branches; b++) |
| 594 | if (c->js.branches[b] && c->js.branches[b]->n_units > 1) return RX_ERR_REPLAY; |
| 595 | for (uint32_t b = 0; b < c->js.n_branches; b++) |
| 596 | if (c->js.branches[b]) js_branch_release(&c->js, b); |
| 597 | uint32_t root; |
| 598 | if (js_branch_root(&c->js, &RXC_REALIZER, aien_mid_hash(&c->self), &root) != JS_OK || |
| 599 | js_branch_ref(&c->js, root, &ref) != JS_OK) |
| 600 | return RX_ERR_REPLAY; |
| 601 | } else { |
| 602 | for (uint32_t b = 0; b < c->js.n_branches; b++) |
| 603 | if (c->js.branches[b] && b != ref.id) js_branch_release(&c->js, b); |
| 604 | } |
| 605 | if (commit_js(c) != JS_OK) return RX_ERR_REPLAY; |
| 606 | c->recovered = ref; |
| 607 | c->recovered_record = chosen; |
| 608 | |
| 609 | /* Complete every interrupted composition record, not only the newest: |
| 610 | * each recorded verdict whose goal had no winner (S = 0), or whose winner |
| 611 | * was committed by a state record that was not rolled back. A crash or |
| 612 | * failure anywhere inside a record leaves it completable here. */ |
| 613 | const uint64_t vsubj = RXC_CX_SUBJECT(RXC_SLOT_VERDICT); |
| 614 | uint32_t done = 0; |
| 615 | for (uint32_t i = 0; i < s->by_subject[vsubj].n; i++) { |
| 616 | uint64_t V = s->by_subject[vsubj].ids[i]; |
| 617 | const CxObject *vo = cx_get(s, V); |
| 618 | if (!vo || vo->kind != CX_K_EXEC_COMMIT) continue; |
| 619 | const uint64_t *vp = payload_of(s, V, CX_WREC_WORDS); |
| 620 | if (!vp || vp[CX_WREC_N_OUTPUTS] == 0) continue; /* the verifier's own write */ |
| 621 | const uint64_t goal = vp[CX_WREC_FIELD0 + RXC_V_GOAL]; |
| 622 | const uint64_t winner = vp[CX_WREC_FIELD0 + RXC_V_WINNER]; |
| 623 | if (goal == 0) continue; |
| 624 | uint64_t S = 0, sref = 0; |
| 625 | if (winner != RXC_NONE) { |
| 626 | /* Its commit: the first state record of the same goal after V, |
| 627 | * before the next verdict write and the next World. */ |
| 628 | uint64_t bound = UINT64_MAX; |
| 629 | for (uint32_t j = i + 1; j < s->by_subject[vsubj].n; j++) { |
| 630 | uint64_t id = s->by_subject[vsubj].ids[j]; |
| 631 | const CxObject *n2 = cx_get(s, id); |
| 632 | const uint64_t *np = payload_of(s, id, CX_WREC_WORDS); |
| 633 | if (n2 && (n2->kind == CX_K_ENTITY_CREATED || |
| 634 | (n2->kind == CX_K_EXEC_COMMIT && np && np[CX_WREC_N_OUTPUTS]))) { |
| 635 | bound = id; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | for (uint32_t j = 0; j < l->n; j++) { |
| 640 | const CxObject *o = cx_get(s, l->ids[j]); |
| 641 | if (!o || o->id <= V) continue; |
| 642 | if (o->id >= bound || o->kind == CX_K_ENTITY_CREATED) break; |
| 643 | if (o->kind != CX_K_EXEC_COMMIT) continue; |
| 644 | const uint64_t *p = payload_of(s, o->id, CX_WREC_WORDS); |
| 645 | if (p && p[CX_WREC_FIELD0 + RXC_S_GOAL] == goal) { |
| 646 | S = o->id; |
| 647 | sref = p[CX_WREC_FIELD0 + RXC_S_REF]; |
| 648 | } |
| 649 | break; |
| 650 | } |
| 651 | if (!S || has_admission(s, RXC_ADMIT_ROLLBACK, S)) continue; /* never NEW */ |
| 652 | } |
| 653 | RxcResult tmp; |
| 654 | memset(&tmp, 0, sizeof tmp); |
| 655 | int n = compose_records(c, S, V, NULL, 0, &tmp); |
| 656 | if (n == -2 && S != chosen) continue; /* inputs absent: the run wrote nothing either */ |
| 657 | if (n < 0) return RX_ERR_REPLAY; |
| 658 | if (n > 0) { |
| 659 | uint64_t ap[RXC_AP_WORDS] = { sref, goal, winner }; |
| 660 | CxHeader h; |
| 661 | memset(&h, 0, sizeof h); |
| 662 | h.cls = CX_EVIDENCE; |
| 663 | h.kind = CX_K_ADMISSION; |
| 664 | h.subject = RXC_STATE_SUBJECT; |
| 665 | h.tag = RXC_ADMIT_RECOVERED; |
| 666 | h.links[0] = S ? S : V; |
| 667 | h.links[1] = tmp.cx_evidence; |
| 668 | uint64_t id; |
| 669 | if (cx_put(c, &h, ap, RXC_AP_WORDS, &id)) return RX_ERR_REPLAY; |
| 670 | done += (uint32_t)n; |
| 671 | } |
| 672 | } |
| 673 | c->recovered_completed = done; |
| 674 | return RX_OK; |
| 675 | } |
| 676 | |
| 677 | /* ---- authority ------------------------------------------------------------ */ |
| 678 | |
| 679 | static int mint(RxCompose *c, uint32_t subject, uint64_t resource, uint32_t rights, RxCapRef *out) { |
| 680 | AienosCapRef office, ref = { UINT32_MAX, 0 }; |
| 681 | if (aienos_cap_office(c->admin, &office) != 0) return RX_ERR_AUTHORITY; |
| 682 | AienosCapMint m = { RXC_ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; |
| 683 | if (aienos_cap_mint(c->admin, &m, &ref) != 0) return RX_ERR_AUTHORITY; |
| 684 | *out = (RxCapRef){ ref.cap_id, ref.generation }; |
| 685 | return RX_OK; |
| 686 | } |
| 687 | |
| 688 | static const uint64_t RXC_RES[5] = { RXC_RES_GOAL, RXC_RES_CAND0, RXC_RES_CAND1, RXC_RES_VERDICT, |
| 689 | RXC_RES_STATE }; |
| 690 | #define RXC_N_CAPS (1u + RXC_K * 3u + 4u + 2u) |
| 691 | |
| 692 | /* Instance `inst` of a World's composition slots: its own subjects and its |
| 693 | * own resources (rx_compose.h, RXC_SUBJ_OF / RXC_RES_OF). */ |
| 694 | static void set_instance(RxCompose *c, uint32_t inst) { |
| 695 | c->inst = inst; |
| 696 | c->subj_cand[0] = RXC_SUBJ_OF(inst, RXC_SUBJ_CAND0); |
| 697 | c->subj_cand[1] = RXC_SUBJ_OF(inst, RXC_SUBJ_CAND1); |
| 698 | c->subj_aegis = RXC_SUBJ_OF(inst, RXC_SUBJ_AEGIS); |
| 699 | c->subj_commit = RXC_SUBJ_OF(inst, RXC_SUBJ_COMMIT); |
| 700 | for (uint32_t i = 0; i < 5; i++) c->res[i] = RXC_RES_OF(inst, RXC_RES[i]); |
| 701 | } |
| 702 | |
| 703 | /* The capabilities minted at open/attach, in mint order. */ |
| 704 | static RxCapRef *cap_at(RxCompose *c, uint32_t i) { |
| 705 | if (i == 0) return &c->cap_ext; |
| 706 | i -= 1; |
| 707 | if (i < RXC_K * 3u) return &c->cap_cand[i / 3u][i % 3u]; |
| 708 | i -= RXC_K * 3u; |
| 709 | if (i < 4u) return &c->cap_verify[i]; |
| 710 | return &c->cap_commit[i - 4u]; |
| 711 | } |
| 712 | |
| 713 | static int mint_next(RxCompose *c, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 714 | if (c->n_minted >= RXC_N_CAPS) return RX_ERR_FULL; /* cap_at has RXC_N_CAPS slots */ |
| 715 | int rc = mint(c, subject, resource, rights, cap_at(c, c->n_minted)); |
| 716 | if (rc == RX_OK) c->n_minted++; |
| 717 | return rc; |
| 718 | } |
| 719 | |
| 720 | static int add_reaction(RxCompose *c, const RxReactionDesc *d, uint32_t *id) { |
| 721 | int rc = c->keys ? rx_world_add_reaction_keyed(c->world, c->keys, d, id) |
| 722 | : rx_world_add_reaction(c->world, d, id); |
| 723 | if (rc == RX_OK) c->n_rx++; |
| 724 | return rc; |
| 725 | } |
| 726 | |
| 727 | /* Objects, authority, reactions, binder and Cortex link of the composition |
| 728 | * in c->world. `own`: the World was just built for it (open): its object ids |
| 729 | * must be 0..4 and its whole-World recorder is the composition journal, as |
| 730 | * before attach existed. Otherwise (attach) ids are whatever the World hands |
| 731 | * out and a scoped Cortex link maps them to the composition subjects. Every |
| 732 | * subject and resource is the instance's own (set_instance), so two |
| 733 | * compositions in one World hold no right on each other's objects. */ |
| 734 | static int build_in_world(RxCompose *c, int own) { |
| 735 | int rc; |
| 736 | /* The binder first: it claims the instance's slot in the World. */ |
| 737 | if ((rc = rx_world_set_binder(c->world, bind_check, bind_fn, bind_abort, c)) != RX_OK) |
| 738 | return rc; |
| 739 | c->has_binder = 1; |
| 740 | uint64_t z[RX_MAX_FIELDS] = { 0 }, st[RX_MAX_FIELDS] = { 0 }; |
| 741 | st[RXC_S_REF] = ref_pack(c->recovered); |
| 742 | if (c->recovered_record) { |
| 743 | const uint64_t *p = payload_of(&c->cx, c->recovered_record, CX_WREC_WORDS); |
| 744 | if (p) { |
| 745 | st[RXC_S_RESULT] = p[CX_WREC_FIELD0 + RXC_S_RESULT]; |
| 746 | st[RXC_S_GOAL] = p[CX_WREC_FIELD0 + RXC_S_GOAL]; |
| 747 | } |
| 748 | } |
| 749 | RxObjRef o[5]; |
| 750 | for (uint32_t i = 0; i < 5; i++) { |
| 751 | rc = rx_world_create(c->world, 1, RX_PERSIST_RESIDENT, c->res[i], |
| 752 | i == RXC_SLOT_STATE ? st : z, &o[i]); |
| 753 | if (rc != RX_OK) return rc; |
| 754 | c->obj[c->n_objs++] = o[i]; |
| 755 | if (own && o[i].id != i) return RX_ERR_BAD_DESC; /* slot layout is part of the record */ |
| 756 | } |
| 757 | c->goal = o[RXC_SLOT_GOAL]; |
| 758 | c->cand[0] = o[RXC_SLOT_CAND0]; |
| 759 | c->cand[1] = o[RXC_SLOT_CAND1]; |
| 760 | c->verdict = o[RXC_SLOT_VERDICT]; |
| 761 | c->state = o[RXC_SLOT_STATE]; |
| 762 | const uint64_t *R = c->res; |
| 763 | |
| 764 | /* Each step holds only the rights it needs. Outside input comes in under |
| 765 | * the World's own external subject, on the goal resource only. */ |
| 766 | if ((rc = mint_next(c, c->world->external_subject, R[RXC_SLOT_GOAL], RX_RIGHT_WRITE))) |
| 767 | return rc; |
| 768 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 769 | uint32_t subj = c->subj_cand[k]; |
| 770 | if ((rc = mint_next(c, subj, R[RXC_SLOT_GOAL], RX_RIGHT_READ)) || |
| 771 | (rc = mint_next(c, subj, R[RXC_SLOT_STATE], RX_RIGHT_READ)) || |
| 772 | (rc = mint_next(c, subj, R[RXC_SLOT_CAND0 + k], RX_RIGHT_WRITE))) |
| 773 | return rc; |
| 774 | } |
| 775 | if ((rc = mint_next(c, c->subj_aegis, R[RXC_SLOT_CAND0], RX_RIGHT_READ)) || |
| 776 | (rc = mint_next(c, c->subj_aegis, R[RXC_SLOT_CAND1], RX_RIGHT_READ)) || |
| 777 | (rc = mint_next(c, c->subj_aegis, R[RXC_SLOT_GOAL], RX_RIGHT_READ)) || |
| 778 | (rc = mint_next(c, c->subj_aegis, R[RXC_SLOT_VERDICT], RX_RIGHT_WRITE)) || |
| 779 | (rc = mint_next(c, c->subj_commit, R[RXC_SLOT_VERDICT], RX_RIGHT_READ)) || |
| 780 | (rc = mint_next(c, c->subj_commit, R[RXC_SLOT_STATE], RX_RIGHT_WRITE))) |
| 781 | return rc; |
| 782 | |
| 783 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 784 | RxReactionDesc d; |
| 785 | memset(&d, 0, sizeof d); |
| 786 | d.name = k ? "compose.candidate.1" : "compose.candidate.0"; |
| 787 | d.faculty = RX_FACULTY_OMEGA; |
| 788 | d.subject = c->subj_cand[k]; |
| 789 | d.priority = RX_PRIO_FOREGROUND; |
| 790 | d.triggers[d.n_triggers++] = (RxDep){ c->goal, RX_ALL_FIELDS }; |
| 791 | d.reads[d.n_reads++] = (RxDep){ c->state, RX_FIELD(RXC_S_REF) }; |
| 792 | d.writes[d.n_writes++] = (RxDep){ c->cand[k], RX_ALL_FIELDS }; |
| 793 | d.caps[d.n_caps++] = (RxCapNeed){ c->cap_cand[k][0], R[RXC_SLOT_GOAL], RX_RIGHT_READ }; |
| 794 | d.caps[d.n_caps++] = (RxCapNeed){ c->cap_cand[k][1], R[RXC_SLOT_STATE], RX_RIGHT_READ }; |
| 795 | d.caps[d.n_caps++] = (RxCapNeed){ c->cap_cand[k][2], R[RXC_SLOT_CAND0 + k], |
| 796 | RX_RIGHT_WRITE }; |
| 797 | c->cand_user[k] = (struct RxcCandUser){ c, k }; |
| 798 | d.fn = candidate_fn; |
| 799 | d.user = &c->cand_user[k]; |
| 800 | if ((rc = add_reaction(c, &d, &c->rx_cand[k])) != RX_OK) return rc; |
| 801 | } |
| 802 | RxReactionDesc v; |
| 803 | memset(&v, 0, sizeof v); |
| 804 | v.name = "compose.verify"; |
| 805 | v.faculty = RX_FACULTY_AEGIS; |
| 806 | v.subject = c->subj_aegis; |
| 807 | v.priority = RX_PRIO_FOREGROUND; |
| 808 | v.triggers[v.n_triggers++] = (RxDep){ c->cand[0], RX_ALL_FIELDS }; |
| 809 | v.triggers[v.n_triggers++] = (RxDep){ c->cand[1], RX_ALL_FIELDS }; |
| 810 | v.reads[v.n_reads++] = (RxDep){ c->goal, RX_ALL_FIELDS }; |
| 811 | v.writes[v.n_writes++] = (RxDep){ c->verdict, RX_ALL_FIELDS }; |
| 812 | v.caps[v.n_caps++] = (RxCapNeed){ c->cap_verify[0], R[RXC_SLOT_CAND0], RX_RIGHT_READ }; |
| 813 | v.caps[v.n_caps++] = (RxCapNeed){ c->cap_verify[1], R[RXC_SLOT_CAND1], RX_RIGHT_READ }; |
| 814 | v.caps[v.n_caps++] = (RxCapNeed){ c->cap_verify[2], R[RXC_SLOT_GOAL], RX_RIGHT_READ }; |
| 815 | v.caps[v.n_caps++] = (RxCapNeed){ c->cap_verify[3], R[RXC_SLOT_VERDICT], RX_RIGHT_WRITE }; |
| 816 | v.fn = verify_fn; |
| 817 | v.user = c; |
| 818 | if ((rc = add_reaction(c, &v, &c->rx_verify)) != RX_OK) return rc; |
| 819 | |
| 820 | RxReactionDesc m; |
| 821 | memset(&m, 0, sizeof m); |
| 822 | m.name = "compose.commit"; |
| 823 | m.faculty = RX_FACULTY_OMEGA; |
| 824 | m.subject = c->subj_commit; |
| 825 | m.priority = RX_PRIO_FOREGROUND; |
| 826 | m.triggers[m.n_triggers++] = (RxDep){ c->verdict, RX_ALL_FIELDS }; |
| 827 | m.writes[m.n_writes++] = (RxDep){ c->state, RX_ALL_FIELDS }; |
| 828 | m.caps[m.n_caps++] = (RxCapNeed){ c->cap_commit[0], R[RXC_SLOT_VERDICT], RX_RIGHT_READ }; |
| 829 | m.caps[m.n_caps++] = (RxCapNeed){ c->cap_commit[1], R[RXC_SLOT_STATE], RX_RIGHT_WRITE }; |
| 830 | m.fn = commit_fn; |
| 831 | m.user = c; |
| 832 | if ((rc = add_reaction(c, &m, &c->rx_commit)) != RX_OK) return rc; |
| 833 | |
| 834 | if ((rc = rx_world_bind_field(c->world, c, c->state, RXC_S_REF)) != RX_OK) return rc; |
| 835 | if (own) { |
| 836 | rc = rx_cortex_attach(c->world, &c->cx, c->session); |
| 837 | } else { |
| 838 | uint64_t subj[5]; |
| 839 | for (uint32_t i = 0; i < 5; i++) subj[i] = RXC_CX_SUBJECT(i); |
| 840 | rc = rx_cortex_attach_scoped(c->world, &c->cx, c->session, o, subj, 5); |
| 841 | } |
| 842 | if (rc != RX_OK) return rc; |
| 843 | c->attached = 1; |
| 844 | return RX_OK; |
| 845 | } |
| 846 | |
| 847 | static int build_world(RxCompose *c, AienosCapView *view, uint32_t n_workers) { |
| 848 | int rc = rx_world_init_native(&c->w, view, n_workers, 1u << 14); |
| 849 | if (rc != RX_OK) return rc; |
| 850 | c->world = &c->w; |
| 851 | c->owns_world = 1; |
| 852 | c->w.external_subject = RXC_SUBJ_EXTERNAL; |
| 853 | set_instance(c, 0); |
| 854 | return build_in_world(c, 1); |
| 855 | } |
| 856 | |
| 857 | /* ---- public API ------------------------------------------------------------ */ |
| 858 | |
| 859 | /* Shared by open and attach: reset (keeping the caller's test settings, |
| 860 | * but not whether a fault already fired), machine identity, Cortex journal, |
| 861 | * durable J-Space, OLD-or-NEW recovery. */ |
| 862 | static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, uint64_t session, |
| 863 | const SrRouter *router, RxcContract contract, AienosCapAdmin *admin) { |
| 864 | struct RxcTestHooks hooks = c->test; |
| 865 | hooks.fault_hit = 0; |
| 866 | hooks.held = hooks.release = hooks.hold_done = 0; |
| 867 | memset(c, 0, sizeof *c); |
| 868 | c->test = hooks; |
| 869 | c->wait_ms = RXC_WAIT_MS_DEFAULT; |
| 870 | snprintf(c->dir, sizeof c->dir, "%s", dir); |
| 871 | c->self = *self; |
| 872 | c->session = session; |
| 873 | c->router = router; |
| 874 | c->contract = contract; |
| 875 | c->admin = admin; |
| 876 | /* Check before mkdir (src/omega_path_check.h): a refused dir is an argument error and creates nothing. */ |
| 877 | char checked[sizeof c->dir]; |
| 878 | if (omega_path_copy(checked, sizeof checked, dir) != 0) return RX_ERR_ARG; |
| 879 | if (mkdir(checked, 0700) && errno != EEXIST) return RX_ERR_ARG; |
| 880 | |
| 881 | char p[256]; |
| 882 | path_in(p, sizeof p, dir, "machine.id"); |
| 883 | if (access(p, F_OK) == 0) { |
| 884 | AienMachineId stored; |
| 885 | if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, self)) |
| 886 | return RX_ERR_IDENTITY; |
| 887 | } else if (aien_mid_store(p, self) != AIEN_MID_OK) { |
| 888 | return RX_ERR_IDENTITY; |
| 889 | } |
| 890 | |
| 891 | path_in(p, sizeof p, dir, "cortex.cx"); |
| 892 | if (cx_open(&c->cx, p, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) |
| 893 | return RX_ERR_REPLAY; |
| 894 | /* cx_open already verified every record and rebuilt the chain (see rx_cortex.h). */ |
| 895 | |
| 896 | JsHome home; |
| 897 | memset(&home, 0, sizeof home); |
| 898 | aien_mid_to_slot(self, home.machine); |
| 899 | home.locality = JS_HOME_LOCAL; |
| 900 | const JsRealizer *rz[1] = { &RXC_REALIZER }; |
| 901 | path_in(p, sizeof p, dir, "jspace"); |
| 902 | if (js_space_open(&c->js, p, rz, 1, NULL, &home) != JS_OK) { |
| 903 | cx_close(&c->cx); |
| 904 | return RX_ERR_REPLAY; |
| 905 | } |
| 906 | /* A Cortex journal cut exactly at a record boundary opens cleanly in cx_open |
| 907 | * (the journal header carries no count or head). The checkpoint anchor |
| 908 | * (count + head digest) is the cross-check: a journal shorter than the |
| 909 | * anchor, or whose record at the anchored count differs, is refused. Records |
| 910 | * appended after the last commit only make the journal longer: lower bound. |
| 911 | * KNOWN LIMIT: if both the journal and the checkpoint roll back together, |
| 912 | * nothing local detects it (an external anchor problem). A version-2 |
| 913 | * checkpoint has no anchor: the check is audit only (anchor_unknown) until |
| 914 | * the next commit writes one. */ |
| 915 | c->anchor_unknown = 0; |
| 916 | uint8_t an[48]; |
| 917 | if (js_space_anchor(&c->js, an)) { |
| 918 | uint64_t cnt = 0; |
| 919 | for (int i = 0; i < 8; i++) cnt |= (uint64_t)an[i] << (8 * i); |
| 920 | const CxObject *head = cnt ? cx_get(&c->cx, cnt) : NULL; |
| 921 | if (c->cx.n < cnt || (cnt > 0 && (!head || memcmp(head->digest, an + 8, 32) != 0))) { |
| 922 | js_space_destroy(&c->js); |
| 923 | cx_close(&c->cx); |
| 924 | return RX_ERR_REPLAY; |
| 925 | } |
| 926 | } else { |
| 927 | c->anchor_unknown = 1; |
| 928 | } |
| 929 | int rc = recover(c); |
| 930 | if (rc != RX_OK) { |
| 931 | js_space_destroy(&c->js); |
| 932 | cx_close(&c->cx); |
| 933 | } |
| 934 | return rc; |
| 935 | } |
| 936 | |
| 937 | /* 1 while any of the composition's own reactions may still call into it: |
| 938 | * admitted (READY, BLOCKED_RESOURCE), running or publishing, or due to run |
| 939 | * again (re-armed, parked, deferred, resume or sequential pulse pending, |
| 940 | * holding). Read under the World lock. */ |
| 941 | static int own_busy(RxCompose *c) { |
| 942 | uint32_t ids[4] = { c->rx_cand[0], c->rx_cand[1], c->rx_verify, c->rx_commit }; |
| 943 | int busy = 0; |
| 944 | pthread_mutex_lock(&c->world->mu); |
| 945 | for (uint32_t i = 0; i < c->n_rx && i < 4 && !busy; i++) { |
| 946 | if (ids[i] >= c->world->n_reactions) continue; |
| 947 | const RxReaction *r = &c->world->reactions[ids[i]]; |
| 948 | busy = r->state == RX_READY || r->state == RX_RUNNING || r->state == RX_PUBLISHING || |
| 949 | r->state == RX_BLOCKED_RESOURCE || r->rearm || r->parked || r->deferred || |
| 950 | r->resume_pending || r->holding || r->seq_pending; |
| 951 | } |
| 952 | /* A wake held back by the World's fan-out limit waits in its backlog |
| 953 | * with the reaction still DORMANT: that is a pending run as well. */ |
| 954 | for (uint32_t j = 0; j < c->world->deferred_len && !busy; j++) { |
| 955 | uint32_t rid = c->world->deferred[c->world->deferred_head + j].reaction; |
| 956 | for (uint32_t i = 0; i < c->n_rx && i < 4; i++) |
| 957 | if (rid == ids[i]) busy = 1; |
| 958 | } |
| 959 | pthread_mutex_unlock(&c->world->mu); |
| 960 | return busy; |
| 961 | } |
| 962 | |
| 963 | /* Wait until none of the composition's own steps can still run (attach |
| 964 | * mode: the rest of a living World need never be quiet). RX_ERR_TIMEOUT |
| 965 | * after timeout_ms. */ |
| 966 | static int wait_own(RxCompose *c, int timeout_ms) { |
| 967 | struct timespec ts = { 0, 100000 }, t0, t1; |
| 968 | clock_gettime(CLOCK_MONOTONIC, &t0); |
| 969 | while (own_busy(c)) { |
| 970 | clock_gettime(CLOCK_MONOTONIC, &t1); |
| 971 | int64_t ms = (int64_t)(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000; |
| 972 | if (ms >= timeout_ms) return RX_ERR_TIMEOUT; |
| 973 | nanosleep(&ts, NULL); |
| 974 | } |
| 975 | return RX_OK; |
| 976 | } |
| 977 | |
| 978 | /* Undo what attach put into the caller's World; the World keeps running |
| 979 | * (rx_compose.h, close in attach mode, steps 1-5). */ |
| 980 | static void leave_world(RxCompose *c) { |
| 981 | /* 1. Revoke: from here on no composition step can publish anything. */ |
| 982 | AienosCapRef office; |
| 983 | int have_office = aienos_cap_office(c->admin, &office) == 0; |
| 984 | if (have_office) |
| 985 | for (uint32_t i = 0; i < c->n_minted; i++) { |
| 986 | RxCapRef *r = cap_at(c, i); |
| 987 | (void)aienos_cap_revoke(c->admin, office, (AienosCapRef){ r->cap_id, r->generation }); |
| 988 | } |
| 989 | /* 2. The composition journal takes no further World records. */ |
| 990 | if (c->attached) rx_cortex_detach_store(c->world, &c->cx); |
| 991 | c->attached = 0; |
| 992 | /* 3. Its own steps still use c: wait until none can run. No cutoff. */ |
| 993 | struct timespec ts = { 0, 200000 }; |
| 994 | while (own_busy(c)) nanosleep(&ts, NULL); |
| 995 | /* 4. Binder, objects, capability slots. */ |
| 996 | if (c->has_binder) rx_world_clear_binder(c->world, c); |
| 997 | c->has_binder = 0; |
| 998 | for (uint32_t i = 0; i < c->n_objs; i++) rx_world_retire(c->world, c->obj[i]); |
| 999 | c->n_objs = 0; |
| 1000 | if (have_office) |
| 1001 | for (uint32_t i = 0; i < c->n_minted; i++) |
| 1002 | (void)aienos_cap_reclaim(c->admin, office, cap_at(c, i)->cap_id); |
| 1003 | c->n_minted = 0; |
| 1004 | /* 5. Its reactions leave the World (rx_world_remove_reaction): their |
| 1005 | * subscriptions go and their slots are reused by the next attach of the |
| 1006 | * same instance. Every object they name is retired, so nothing can wake |
| 1007 | * them now; a BUSY answer is a step that is just finishing. */ |
| 1008 | uint32_t ids[4] = { c->rx_cand[0], c->rx_cand[1], c->rx_verify, c->rx_commit }; |
| 1009 | for (uint32_t i = 0; i < c->n_rx && i < 4; i++) |
| 1010 | while (rx_world_remove_reaction(c->world, ids[i]) == RX_ERR_BUSY) nanosleep(&ts, NULL); |
| 1011 | c->n_rx = 0; |
| 1012 | } |
| 1013 | |
| 1014 | int rx_compose_open(RxCompose *c, const char *dir, const AienMachineId *self, uint64_t session, |
| 1015 | const SrRouter *router, RxcContract contract, AienosCapAdmin *admin, |
| 1016 | AienosCapView *view, uint32_t n_workers) { |
| 1017 | if (!c || !dir || !self || !admin || !view || strlen(dir) >= sizeof c->dir - 32) |
| 1018 | return RX_ERR_ARG; |
| 1019 | int rc = open_home(c, dir, self, session, router, contract, admin); |
| 1020 | if (rc != RX_OK) return rc; |
| 1021 | c->world = &c->w; |
| 1022 | rc = build_world(c, view, n_workers ? n_workers : 1); |
| 1023 | if (rc != RX_OK) { |
| 1024 | if (c->w.n_workers) rx_world_destroy(&c->w); |
| 1025 | c->world = NULL; |
| 1026 | js_space_destroy(&c->js); |
| 1027 | cx_close(&c->cx); |
| 1028 | return rc; |
| 1029 | } |
| 1030 | return RX_OK; |
| 1031 | } |
| 1032 | |
| 1033 | int rx_compose_enroll_callers(RxWorld *w, RxCallerKeyring *keys) { |
| 1034 | if (!w || !keys) return RX_ERR_ARG; |
| 1035 | static const uint32_t role[4] = { RXC_SUBJ_CAND0, RXC_SUBJ_CAND1, RXC_SUBJ_AEGIS, |
| 1036 | RXC_SUBJ_COMMIT }; |
| 1037 | _Static_assert(RXC_MAX_ACTIVE * 4u <= RX_CALLER_KEYRING_MAX, |
| 1038 | "one keyring holds every instance's composition subjects"); |
| 1039 | memset(keys, 0, sizeof *keys); |
| 1040 | for (uint32_t inst = 0; inst < RXC_MAX_ACTIVE; inst++) |
| 1041 | for (uint32_t i = 0; i < 4; i++) { |
| 1042 | uint32_t s = RXC_SUBJ_OF(inst, role[i]); |
| 1043 | if (keys->n >= RX_CALLER_KEYRING_MAX || |
| 1044 | rx_world_enroll_caller(w, s, &keys->cred[keys->n]) != RX_CALLER_OK) { |
| 1045 | for (unsigned b = 0; b < sizeof *keys; b++) ((volatile uint8_t *)keys)[b] = 0; |
| 1046 | return RX_ERR_IDENTITY; /* bound already, enrolled, full or no entropy */ |
| 1047 | } |
| 1048 | keys->subject[keys->n++] = s; |
| 1049 | } |
| 1050 | return RX_OK; |
| 1051 | } |
| 1052 | |
| 1053 | /* Attaches are serialized process-wide, so choosing an instance slot and |
| 1054 | * claiming it (installing the binder) is one step; runs and closes are not |
| 1055 | * serialized. */ |
| 1056 | static pthread_mutex_t g_attach_mu = PTHREAD_MUTEX_INITIALIZER; |
| 1057 | |
| 1058 | /* Under w->mu: the instance slots taken in `w` by attached compositions |
| 1059 | * (bit i = instance i), and whether one of them uses `dir`. */ |
| 1060 | static uint32_t taken_instances(RxWorld *w, const char *dir, int *same_dir) { |
| 1061 | uint32_t taken = 0; |
| 1062 | *same_dir = 0; |
| 1063 | for (uint32_t i = 0; i < RX_MAX_BINDERS; i++) { |
| 1064 | if (w->binder[i].check != bind_check || !w->binder[i].ctx) continue; |
| 1065 | const RxCompose *o = w->binder[i].ctx; |
| 1066 | if (o->inst < RXC_MAX_ACTIVE) taken |= 1u << o->inst; |
| 1067 | if (strcmp(o->dir, dir) == 0) *same_dir = 1; |
| 1068 | } |
| 1069 | return taken; |
| 1070 | } |
| 1071 | |
| 1072 | /* Under w->mu: table slots the four reactions of `inst` still need (a removed |
| 1073 | * slot of the same subject and faculty is reused, rx_world_remove_reaction). */ |
| 1074 | static uint32_t slots_needed(const RxWorld *w, uint32_t inst) { |
| 1075 | const uint32_t subj[4] = { RXC_SUBJ_OF(inst, RXC_SUBJ_CAND0), RXC_SUBJ_OF(inst, RXC_SUBJ_CAND1), |
| 1076 | RXC_SUBJ_OF(inst, RXC_SUBJ_AEGIS), RXC_SUBJ_OF(inst, RXC_SUBJ_COMMIT) }; |
| 1077 | const uint32_t fac[4] = { RX_FACULTY_OMEGA, RX_FACULTY_OMEGA, RX_FACULTY_AEGIS, |
| 1078 | RX_FACULTY_OMEGA }; |
| 1079 | uint32_t need = 0; |
| 1080 | for (uint32_t k = 0; k < 4; k++) { |
| 1081 | int found = 0; |
| 1082 | for (uint32_t i = 0; i < w->n_reactions && !found; i++) |
| 1083 | found = w->reactions[i].removed && w->reactions[i].desc.subject == subj[k] && |
| 1084 | w->reactions[i].desc.faculty == fac[k]; |
| 1085 | if (!found) need++; |
| 1086 | } |
| 1087 | return need; |
| 1088 | } |
| 1089 | |
| 1090 | int rx_compose_attach(RxCompose *c, RxWorld *w, const RxCallerKeyring *keys, const char *dir, |
| 1091 | const AienMachineId *self, uint64_t session, const SrRouter *router, |
| 1092 | RxcContract contract, AienosCapAdmin *admin) { |
| 1093 | if (!c || !w || !dir || !self || !admin || strlen(dir) >= sizeof c->dir - 32) |
| 1094 | return RX_ERR_ARG; |
| 1095 | /* The World's outside subject must not be a composition subject. */ |
| 1096 | if (w->external_subject >= RXC_SUBJ_EXTERNAL && |
| 1097 | w->external_subject < RXC_SUBJ_OF(RXC_MAX_ACTIVE, RXC_SUBJ_EXTERNAL)) |
| 1098 | return RX_ERR_ARG; |
| 1099 | pthread_mutex_lock(&g_attach_mu); |
| 1100 | /* A free instance slot (RXC_MAX_ACTIVE per World), no composition of this |
| 1101 | * World already on `dir`, and room for its four reactions, read together |
| 1102 | * under the World lock. */ |
| 1103 | char d[sizeof c->dir]; |
| 1104 | snprintf(d, sizeof d, "%s", dir); |
| 1105 | int same_dir; |
| 1106 | pthread_mutex_lock(&w->mu); |
| 1107 | uint32_t taken = taken_instances(w, d, &same_dir), inst = RXC_MAX_ACTIVE; |
| 1108 | for (uint32_t i = 0; i < RXC_MAX_ACTIVE && inst == RXC_MAX_ACTIVE; i++) |
| 1109 | if (!(taken & (1u << i))) inst = i; |
| 1110 | int full = inst < RXC_MAX_ACTIVE && w->n_reactions + slots_needed(w, inst) > RX_MAX_REACTIONS; |
| 1111 | pthread_mutex_unlock(&w->mu); |
| 1112 | int rc = same_dir || inst == RXC_MAX_ACTIVE ? RX_ERR_EXISTS : full ? RX_ERR_FULL : RX_OK; |
| 1113 | if (rc == RX_OK) rc = open_home(c, dir, self, session, router, contract, admin); |
| 1114 | if (rc != RX_OK) { |
| 1115 | pthread_mutex_unlock(&g_attach_mu); |
| 1116 | return rc; |
| 1117 | } |
| 1118 | c->world = w; |
| 1119 | c->owns_world = 0; |
| 1120 | c->keys = keys; |
| 1121 | set_instance(c, inst); |
| 1122 | rc = build_in_world(c, 0); |
| 1123 | if (rc != RX_OK) { |
| 1124 | leave_world(c); |
| 1125 | c->world = NULL; |
| 1126 | js_space_destroy(&c->js); |
| 1127 | cx_close(&c->cx); |
| 1128 | } |
| 1129 | pthread_mutex_unlock(&g_attach_mu); |
| 1130 | return rc; |
| 1131 | } |
| 1132 | |
| 1133 | int rx_compose_set_remote(RxCompose *c, RxcRemoteRun run, void *ctx) { |
| 1134 | if (!c || !c->world) return RX_ERR_ARG; |
| 1135 | c->remote.run = run; |
| 1136 | c->remote.ctx = run ? ctx : NULL; |
| 1137 | return RX_OK; |
| 1138 | } |
| 1139 | |
| 1140 | int rx_compose_set_wait_ms(RxCompose *c, uint32_t wait_ms) { |
| 1141 | if (!c || !c->world || wait_ms == 0 || wait_ms > RXC_WAIT_MS_MAX) return RX_ERR_ARG; |
| 1142 | c->wait_ms = wait_ms; |
| 1143 | return RX_OK; |
| 1144 | } |
| 1145 | |
| 1146 | JsBranchRef rx_compose_state(RxCompose *c) { |
| 1147 | RxObject o; |
| 1148 | if (rx_world_read(c->world, c->state, &o) != RX_OK) return (JsBranchRef){ UINT32_MAX, 0 }; |
| 1149 | return ref_unpack(o.field[RXC_S_REF]); |
| 1150 | } |
| 1151 | |
| 1152 | /* Cortex id of the record of the publication that last wrote obj.field. */ |
| 1153 | static uint64_t record_of(RxCompose *c, RxObjRef obj, uint32_t field) { |
| 1154 | return rx_cortex_record_in(c->world, &c->cx, rx_world_explain(c->world, obj, field)); |
| 1155 | } |
| 1156 | |
| 1157 | /* Remember an incomplete record so the next run completes it first; without |
| 1158 | * the World records naming it, refuse further runs until reopened. */ |
| 1159 | static void set_pending(RxCompose *c, uint64_t S, uint64_t V, int committed, JsBranchRef old, |
| 1160 | int released, const uint8_t (*cdig)[32]) { |
| 1161 | if (!V || (committed && !S)) { |
| 1162 | c->pending = 2; |
| 1163 | return; |
| 1164 | } |
| 1165 | c->pending = 1; |
| 1166 | c->pend_S = S; |
| 1167 | c->pend_V = V; |
| 1168 | c->pend_old = old; |
| 1169 | c->pend_released = released; |
| 1170 | memcpy(c->pend_cdig, cdig, sizeof c->pend_cdig); |
| 1171 | } |
| 1172 | |
| 1173 | int rx_compose_run(RxCompose *c, uint64_t input, const SrRequirement *req, const CqHeld *held, |
| 1174 | uint64_t now_us, RxcResult *out) { |
| 1175 | if (!c || !out) return RX_ERR_ARG; |
| 1176 | memset(out, 0, sizeof *out); |
| 1177 | out->winner = RXC_NONE; |
| 1178 | /* A previous run left its record incomplete or its state not durable: |
| 1179 | * never start another goal over it. */ |
| 1180 | if (c->pending == 2) return RX_ERR_REPLAY; |
| 1181 | if (c->pending == 1) { |
| 1182 | int m = compose_records(c, c->pend_S, c->pend_V, |
| 1183 | (const uint8_t (*)[32])c->pend_cdig, 0, NULL); |
| 1184 | if (m == -1 || (m == -2 && c->pend_S)) return RX_ERR_REPLAY; |
| 1185 | if (c->pend_S && !c->pend_released) { |
| 1186 | js_branch_release_ref(&c->js, c->pend_old, 0); |
| 1187 | c->pend_released = 1; |
| 1188 | } |
| 1189 | if (c->pend_S && commit_js(c) != JS_OK) return RX_ERR_REPLAY; |
| 1190 | out->prior_completed = m > 0 ? (uint32_t)m : 0; |
| 1191 | c->pending = 0; |
| 1192 | } |
| 1193 | /* Routing discovers the alternatives; it mints nothing. */ |
| 1194 | SrRoute routes[RXC_K]; |
| 1195 | int n = sr_route_alternatives(c->router, req, held, now_us, routes, RXC_K); |
| 1196 | if (n < 0) return n; |
| 1197 | c->n_routes = (uint32_t)n; |
| 1198 | memcpy(c->run_route, routes, sizeof routes); |
| 1199 | out->n_alternatives = (uint32_t)n; |
| 1200 | memcpy(out->route, routes, sizeof routes); |
| 1201 | |
| 1202 | JsBranchRef old = rx_compose_state(c); |
| 1203 | out->old_ref = old; |
| 1204 | uint64_t seq = ++c->seq; |
| 1205 | c->run_input = input; |
| 1206 | c->run_now = now_us; |
| 1207 | uint64_t sk[RXC_K] = { 0, 0 }; |
| 1208 | for (uint32_t k = 0; k < (uint32_t)n && k < RXC_K; k++) |
| 1209 | sk[k] = ((uint64_t)routes[k].skill_version << 32) | routes[k].chosen.skill_id; |
| 1210 | RxMutation g[5] = { |
| 1211 | { c->goal, RXC_G_INPUT, input }, { c->goal, RXC_G_OP, req->need.semantic_operation }, |
| 1212 | { c->goal, RXC_G_SKILL0, sk[0] }, { c->goal, RXC_G_SKILL1, sk[1] }, |
| 1213 | { c->goal, RXC_G_SEQ, seq } }; |
| 1214 | int64_t crumb = rx_world_publish_external(c->world, c->cap_ext, g, 5); |
| 1215 | if (crumb < 0) return (int)crumb; |
| 1216 | out->goal_crumb = (uint64_t)crumb; |
| 1217 | /* Its own World: wait for all of it. Attached: only for its own steps (the |
| 1218 | * rest of a living World, or another composition, need never be quiet). */ |
| 1219 | int rc = c->owns_world ? rx_world_wait_quiescent(c->world, (int)c->wait_ms) : wait_own(c, (int)c->wait_ms); |
| 1220 | if (rc != RX_OK) return rc; |
| 1221 | |
| 1222 | /* settle */ |
| 1223 | RxObject so, vo, co[RXC_K]; |
| 1224 | if (rx_world_read(c->world, c->state, &so) != RX_OK || |
| 1225 | rx_world_read(c->world, c->verdict, &vo) || |
| 1226 | rx_world_read(c->world, c->cand[0], &co[0]) || rx_world_read(c->world, c->cand[1], &co[1])) |
| 1227 | return RX_ERR_NOT_FOUND; |
| 1228 | uint8_t cdig[RXC_K][32]; |
| 1229 | memset(cdig, 0, sizeof cdig); |
| 1230 | for (uint32_t k = 0; k < RXC_K; k++) { |
| 1231 | out->cand_ref[k] = co[k].field[RXC_C_GOAL] == seq ? ref_unpack(co[k].field[RXC_C_REF]) |
| 1232 | : (JsBranchRef){ 0, 0 }; |
| 1233 | if (co[k].field[RXC_C_GOAL] == seq && co[k].field[RXC_C_REF] && |
| 1234 | js_branch_check(&c->js, out->cand_ref[k]) == JS_OK) |
| 1235 | js_branch_content_digest(&c->js, out->cand_ref[k].id, cdig[k]); |
| 1236 | } |
| 1237 | JsBranchRef nw = ref_unpack(so.field[RXC_S_REF]); |
| 1238 | int committed = !ref_eq(nw, old); |
| 1239 | out->reclaimed = js_space_reclaim_staged(&c->js); |
| 1240 | if (vo.field[RXC_V_GOAL] == seq) out->winner = (uint32_t)vo.field[RXC_V_WINNER]; |
| 1241 | |
| 1242 | if (committed) { |
| 1243 | out->new_ref = nw; |
| 1244 | out->result = so.field[RXC_S_RESULT]; |
| 1245 | if (fault(c, RXC_FP_RECLAIM, 0) || commit_js(c) != JS_OK) { |
| 1246 | c->pending = 2; |
| 1247 | out->outcome = RXC_OUT_NOT_DURABLE; |
| 1248 | return RX_OK; |
| 1249 | } |
| 1250 | js_branch_content_digest(&c->js, nw.id, out->winner_digest); |
| 1251 | uint64_t S = record_of(c, c->state, RXC_S_REF); |
| 1252 | uint64_t V = record_of(c, c->verdict, RXC_V_GOAL); |
| 1253 | if (!S || !V || compose_records(c, S, V, (const uint8_t (*)[32])cdig, 1, out) < 0) { |
| 1254 | set_pending(c, S, V, 1, old, 0, (const uint8_t (*)[32])cdig); |
| 1255 | out->outcome = RXC_OUT_RECORD_FAILED; |
| 1256 | return RX_OK; |
| 1257 | } |
| 1258 | js_branch_release_ref(&c->js, old, 0); /* the superseded branch */ |
| 1259 | if (commit_js(c) != JS_OK) { |
| 1260 | set_pending(c, S, V, 1, old, 1, (const uint8_t (*)[32])cdig); |
| 1261 | out->outcome = RXC_OUT_RECORD_FAILED; |
| 1262 | return RX_OK; |
| 1263 | } |
| 1264 | out->outcome = RXC_OUT_COMMITTED; |
| 1265 | return RX_OK; |
| 1266 | } |
| 1267 | out->new_ref = old; |
| 1268 | if (vo.field[RXC_V_GOAL] == seq && vo.field[RXC_V_WINNER] == RXC_NONE) { |
| 1269 | uint64_t V = record_of(c, c->verdict, RXC_V_GOAL); |
| 1270 | if (!V || compose_records(c, 0, V, (const uint8_t (*)[32])cdig, 1, out) < 0) { |
| 1271 | set_pending(c, 0, V, 0, old, 1, (const uint8_t (*)[32])cdig); |
| 1272 | out->outcome = RXC_OUT_RECORD_FAILED; |
| 1273 | return RX_OK; |
| 1274 | } |
| 1275 | out->outcome = RXC_OUT_NO_WINNER; |
| 1276 | return RX_OK; |
| 1277 | } |
| 1278 | out->outcome = RXC_OUT_NOT_COMMITTED; |
| 1279 | return RX_OK; |
| 1280 | } |
| 1281 | |
| 1282 | void rx_compose_close(RxCompose *c) { |
| 1283 | if (!c || !c->world) return; |
| 1284 | if (c->owns_world) { |
| 1285 | rx_world_destroy(&c->w); /* detaches the recorder */ |
| 1286 | c->attached = 0; |
| 1287 | c->has_binder = 0; |
| 1288 | c->n_objs = 0; |
| 1289 | } else { |
| 1290 | /* Leave the caller's World running; returns once none of the |
| 1291 | * composition's own steps can run (rx_compose.h). */ |
| 1292 | leave_world(c); |
| 1293 | } |
| 1294 | c->world = NULL; |
| 1295 | js_space_destroy(&c->js); /* never commits: durable state is what settle made durable */ |
| 1296 | cx_close(&c->cx); |
| 1297 | } |
| 1298 | |
| 1299 | void rx_compose_record_digest(const CxStore *s, uint8_t out[32]) { |
| 1300 | sha256_ctx h; |
| 1301 | sha256_init(&h); |
| 1302 | for (uint64_t id = 1; s && id <= s->n; id++) { |
| 1303 | const CxObject *o = cx_get(s, id); |
| 1304 | if (!o) break; |
| 1305 | uint64_t hw[13] = { o->cls, o->kind, o->subject, o->t, o->generation, o->branch, |
| 1306 | o->protect, o->tag, o->links[0], o->links[1], o->links[2], |
| 1307 | o->links[3], o->n }; |
| 1308 | sha256_update(&h, (const uint8_t *)hw, sizeof hw); |
| 1309 | const uint64_t *p = cx_payload(s, o); |
| 1310 | int world = o->kind >= CX_K_WORK_ACCEPTED && o->kind <= CX_K_EXEC_FAILED; |
| 1311 | for (uint32_t i = 0; i < o->n; i++) { |
| 1312 | uint64_t v = p[i]; |
| 1313 | /* Timing, and the crumb digest (it binds capability generations, |
| 1314 | * which the authority seeds per start), are not record content. */ |
| 1315 | if (world && ((i >= CX_WREC_DIGEST0 && i < CX_WREC_DIGEST0 + 4) || |
| 1316 | i == CX_WREC_T_START_NS || i == CX_WREC_T_END_NS)) |
| 1317 | v = 0; |
| 1318 | sha256_update(&h, (const uint8_t *)&v, 8); |
| 1319 | } |
| 1320 | } |
| 1321 | sha256_final(&h, out); |
| 1322 | } |