Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/runtime/rx_living.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / runtime / rx_living.c` 871 lines ยท 40084 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * rx_living.c -- R13 reactions. See rx_living.h for the object map. 19 * 20 * Each fn_* is a reaction body: it reads its versioned snapshot and proposes 21 * mutations. Two of them also make an effect outside the world, the R9 22 * proposal and the R9 promotion; both are keyed so that a re-run after an 23 * invalidation does not repeat the effect. 24 */ 25 #include "rx_living.h" 26 #include "aienos_cap.h" 27 #include "sha256.h" 28 29 #include 30 #include 31 32 static const RxSnapshotDep *input(const RxCtx *c, RxObjRef r) { 33 for (uint32_t i = 0; i < c->n_in; i++) 34 if (c->in[i].obj.id == r.id) return &c->in[i]; 35 return NULL; 36 } 37 38 static void put(RxCtx *c, RxObjRef r, uint32_t field, uint64_t value) { 39 c->out[c->n_out++] = (RxMutation){r, field, value}; 40 } 41 42 static uint64_t now_ns(void) { 43 struct timespec ts; 44 clock_gettime(CLOCK_MONOTONIC, &ts); 45 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 46 } 47 48 void rx_living_object_digest(uint32_t type, const uint64_t field[RX_MAX_FIELDS], 49 uint8_t out[32]) { 50 sha256_ctx c; 51 uint8_t b[8]; 52 sha256_init(&c); 53 sha256_update(&c, (const uint8_t *)"AIEN_RX_OBJECT_V1", 17); 54 for (uint32_t i = 0; i < 4; i++) b[i] = (uint8_t)(type >> (8 * i)); 55 sha256_update(&c, b, 4); 56 for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) { 57 for (uint32_t i = 0; i < 8; i++) b[i] = (uint8_t)(field[f] >> (8 * i)); 58 sha256_update(&c, b, 8); 59 } 60 sha256_final(&c, out); 61 } 62 63 /* Operands of trial t. They wrap past 2^32, and consecutive sums differ, so 64 * every trial changes the output the next evidence check wakes on. */ 65 static uint64_t trial_a(uint64_t plan, uint64_t t) { 66 return (uint32_t)(0xffffff00u + (uint32_t)plan * 16u + (uint32_t)t); 67 } 68 static uint64_t trial_b(uint64_t epoch, uint64_t t) { 69 return (uint32_t)((uint32_t)epoch * 0x100u + (uint32_t)t); 70 } 71 72 /* ---- living.experiment.prepare ---- 73 * Omega took up an AIEN plan (search field 6 names it) and opened a new 74 * search epoch. Trial 0 of the experiment is derived from both. Plan and 75 * search are ordinary dependencies; nothing here calls Omega or AIEN. */ 76 static int fn_prepare(RxCtx *c) { 77 RxLiving *l = c->user; 78 const RxSnapshotDep *s = input(c, l->omega->o.search); 79 const RxSnapshotDep *p = input(c, l->aien->o.plan); 80 const RxSnapshotDep *i = input(c, l->o.input); 81 if (!s || !p || !i) return -1; 82 if (s->field[0] < 2 || p->field[0] == 0 || 83 s->field[6] != p->field[0] || i->field[2] == s->field[0]) return 0; 84 put(c, l->o.input, 0, trial_a(p->field[0], 0)); 85 put(c, l->o.input, 1, trial_b(s->field[0], 0)); 86 put(c, l->o.input, 2, s->field[0]); 87 put(c, l->o.input, 3, 0); 88 put(c, l->o.input, 4, p->field[0]); 89 return 0; 90 } 91 92 /* The seat reaction is executed by the resident GB10 seat (or R12's 93 * processor stand-in on hosts). The world never calls this body. */ 94 static int fn_seat_is_not_cpu(RxCtx *c) { (void)c; return -1; } 95 96 /* ---- living.experiment.evidence ---- 97 * The seat published field 0 into the same canonical object. Each trial's 98 * sum is checked against its operands; a match poses the next trial, the 99 * last match or any mismatch ends the experiment with its evidence. */ 100 static int fn_evidence(RxCtx *c) { 101 RxLiving *l = c->user; 102 const RxSnapshotDep *i = input(c, l->o.input); 103 const RxSnapshotDep *o = input(c, l->o.output); 104 const RxSnapshotDep *e = input(c, l->o.evidence); 105 if (!i || !o || !e) return -1; 106 uint64_t epoch = i->field[2], t = i->field[3]; 107 if (epoch < 2 || epoch <= e->field[0]) return 0; 108 uint64_t expected = (uint32_t)((uint32_t)i->field[0] + (uint32_t)i->field[1]); 109 uint64_t matched = t; /* trials before this one all matched */ 110 int ok = o->field[0] == expected; 111 if (ok) matched++; 112 put(c, l->o.evidence, 1, expected); 113 put(c, l->o.evidence, 2, o->field[0]); 114 put(c, l->o.evidence, 4, matched); 115 if (ok && matched < RX_LIVING_TRIALS) { 116 put(c, l->o.input, 0, trial_a(i->field[4], matched)); 117 put(c, l->o.input, 1, trial_b(epoch, matched)); 118 put(c, l->o.input, 3, matched); 119 return 0; 120 } 121 put(c, l->o.evidence, 0, epoch); 122 put(c, l->o.evidence, 3, ok ? 1 : 0); 123 return 0; 124 } 125 126 /* The crumb that published this field, if `reaction` published it. */ 127 static const RxCrumb *authored(RxWorld *w, uint64_t crumb, uint32_t reaction) { 128 const RxCrumb *k = rx_world_crumb(w, crumb); 129 if (!k || k->reaction != reaction) return NULL; 130 if (k->kind != RX_CRUMB_COMMIT && k->kind != RX_CRUMB_EXTERNAL) return NULL; 131 return k; 132 } 133 134 /* Latest parent of `k` published by `reaction`. Parents are sorted. */ 135 static const RxCrumb *parent_by(RxWorld *w, const RxCrumb *k, uint32_t reaction) { 136 for (uint32_t i = k->n_parents; i-- > 0;) { 137 const RxCrumb *p = authored(w, k->parents[i], reaction); 138 if (p) return p; 139 } 140 return NULL; 141 } 142 143 /* Walk the trials back from the last seat publication: seat(t) <- the 144 * evidence commit that posed trial t <- seat(t-1) ... <- the prepare commit 145 * that posed trial 0. */ 146 static const RxCrumb *first_trial(RxWorld *w, const RxLiving *l, const RxCrumb *seat) { 147 for (uint32_t t = 0; seat && t <= RX_LIVING_TRIALS; t++) { 148 const RxCrumb *p = parent_by(w, seat, l->r_prepare); 149 if (p) return p; 150 const RxCrumb *e = parent_by(w, seat, l->r_evidence); 151 seat = e ? parent_by(w, e, l->r_seat) : NULL; 152 } 153 return NULL; 154 } 155 156 static void link(RxLivingProvenance *pv, uint32_t which, const RxCrumb *k, RxObjRef obj) { 157 pv->link[which].obj = obj; 158 pv->link[which].reaction = k->reaction; 159 pv->link[which].crumb = k->id; 160 memcpy(pv->link[which].digest, k->digest, 32); 161 } 162 163 static void bind_snapshot(RxGenObject *o, const RxSnapshotDep *d, uint32_t type) { 164 o->id = d->obj.id; 165 o->generation = d->obj.generation; 166 rx_living_object_digest(type, d->field, o->digest); 167 } 168 169 /* An object outside the snapshot is bound by the crumb that published it. */ 170 static void bind_crumb(RxGenObject *o, const RxLivingLink *k) { 171 o->id = k->obj.id; 172 o->generation = k->obj.generation; 173 memcpy(o->digest, k->digest, 32); 174 } 175 176 static int decline(RxLiving *l, int why) { 177 l->refusals++; 178 l->last_refusal = why; 179 return 0; 180 } 181 182 /* ---- generation.prepare ---- 183 * A new Omega selection for an epoch AIEN believes the physical experiment 184 * supported. Checks who wrote every input it relies on, walks the causal 185 * crumbs back to the measurement, verdict and synthesis of the selected 186 * realization, and proposes an R9 draft. It proposes; it cannot promote. */ 187 /* Durable executor completions: run the deciding activation again. */ 188 static void resume_candidate(void *ctx) { 189 RxLiving *l = ctx; 190 rx_world_resume(l->world, l->r_candidate); 191 } 192 193 static void resume_promoter(void *ctx) { 194 RxLivingPromoter *p = ctx; 195 rx_world_resume(p->world, p->reaction); 196 } 197 198 /* R16 G6: a durable store step refused because an operator stop is in force 199 * (RX_GEN_ERR_HALTED, from the world or the durable mark) is not an outcome. 200 * The activation waits in place: it defers and asks to run again, and no 201 * worker takes work while the world is stopped, so the re-run comes after the 202 * resume and posts the step again (spec section 3: in-flight work runs again 203 * on resume). Recording the refusal instead would make it final. */ 204 static int halted_wait(RxWorld *w, uint32_t reaction) { 205 rx_world_resume(w, reaction); 206 return RX_FN_DEFER; 207 } 208 209 static void digest_words(const uint8_t d[32], uint64_t w[4]) { 210 for (int i = 0; i < 4; i++) { 211 w[i] = 0; 212 for (int b = 0; b < 8; b++) w[i] |= (uint64_t)d[8 * i + b] << (8 * b); 213 } 214 } 215 216 /* G1/N6: with a recorder attached, the body journals its own lesson for the 217 * epoch: the evidence reference (bound to the evidence object's digest) and 218 * the CX_K_CANDIDATE lesson, linked to the evidence and experiment-belief 219 * crumb records. Without a recorder nothing happens. */ 220 static void record_lesson(RxLiving *l, uint64_t epoch, const RxCrumb *k_evi, const RxCrumb *k_bel, 221 const RxSnapshotDep *e, const RxLivingEvidence *ev, 222 const SemanticId *realization, uint64_t cls, uint64_t regime) { 223 RxWorld *w = l->world; 224 CxStore *st = rx_cortex_store_in(w, NULL); 225 if (!st || l->lesson.epoch == epoch) return; 226 uint64_t rec_evi = rx_cortex_record_in(w, st, k_evi->id), rec_bel = rx_cortex_record_in(w, st, k_bel->id); 227 uint8_t dg[32]; 228 rx_living_object_digest(RX_OT_LIVING_EVIDENCE, e->field, dg); 229 uint64_t evp[RX_LIVING_EV_WORDS]; 230 digest_words(dg, evp); 231 evp[RX_LIVING_EV_CRUMB] = k_evi->id; 232 CxHeader h; 233 memset(&h, 0, sizeof h); 234 h.cls = CX_EVIDENCE; h.kind = CX_K_EVIDENCE_REF; h.protect = CX_PROT_VERIFY_EVIDENCE; 235 h.subject = (uint64_t)l->o.evidence.id + 1u; h.tag = epoch; 236 h.links[0] = rec_evi; h.links[1] = rec_bel; 237 h.t = rx_cortex_next_t_in(w, st); 238 uint64_t ref = 0, cand = 0; 239 if (rx_cortex_append_in(w, st, &h, evp, RX_LIVING_EV_WORDS, &ref) != RX_OK) return; 240 uint64_t lp[RX_LIVING_LESSON_WORDS]; 241 _Static_assert(sizeof(RxLivingEvidence) == 7 * sizeof(uint64_t), "lesson layout"); 242 memcpy(lp, ev, sizeof *ev); 243 digest_words(realization->bytes, lp + RX_LIVING_LESSON_REALIZATION); 244 lp[RX_LIVING_LESSON_REGIME] = regime; 245 lp[RX_LIVING_LESSON_KEY] = rx_aien_key(cls, regime); 246 h.cls = CX_CLAIM; h.kind = CX_K_CANDIDATE; h.protect = 0; 247 h.links[0] = rec_bel; h.links[1] = ref; h.links[2] = rec_evi; 248 h.t = rx_cortex_next_t_in(w, st); 249 if (rx_cortex_append_in(w, st, &h, lp, RX_LIVING_LESSON_WORDS, &cand) != RX_OK) return; 250 l->lesson = (RxLivingLesson){epoch, cand, ref, 0, st, {0}}; 251 memcpy(l->lesson.digest, dg, 32); 252 } 253 254 static int fn_candidate(RxCtx *c) { 255 RxLiving *l = c->user; 256 RxWorld *w = l->world; 257 const RxSnapshotDep *s = input(c, l->omega->o.selection); 258 const RxSnapshotDep *b = input(c, l->aien->o.experiment_belief); 259 const RxSnapshotDep *e = input(c, l->o.evidence); 260 const RxSnapshotDep *cr = input(c, l->o.candidate); 261 const RxSnapshotDep *g = input(c, l->aien->o.goal); 262 const RxSnapshotDep *p = input(c, l->aien->o.plan); 263 const RxSnapshotDep *sr = input(c, l->omega->o.search); 264 const RxSnapshotDep *out = input(c, l->o.output); 265 if (!s || !b || !e || !cr || !g || !p || !sr || !out) return -1; 266 uint64_t epoch = s->field[0]; 267 if (epoch < 2 || cr->field[1] == epoch || s->field[1] == 0 || 268 b->field[0] != epoch || b->field[2] != RX_AIEN_EXP_SUPPORTED || 269 e->field[0] != epoch || e->field[3] != 1 || e->field[4] != RX_LIVING_TRIALS || 270 sr->field[0] != epoch) 271 return 0; 272 273 /* Authorship of every record the proposal stands on. */ 274 RxLivingProvenance pv; 275 memset(&pv, 0, sizeof pv); 276 memcpy(pv.magic, "R13PROV1", 8); 277 const RxCrumb *k_sel = authored(w, s->field_writer[0], l->omega->r_select); 278 const RxCrumb *k_bel = authored(w, b->field_writer[0], l->aien->r_experiment); 279 const RxCrumb *k_evi = authored(w, e->field_writer[0], l->r_evidence); 280 const RxCrumb *k_out = authored(w, out->field_writer[0], l->r_seat); 281 const RxCrumb *k_srch = authored(w, sr->field_writer[0], l->omega->r_reconsider); 282 const RxCrumb *k_plan = authored(w, p->field_writer[0], l->aien->r_plan); 283 const RxCrumb *k_goal = authored(w, g->field_writer[0], UINT32_MAX); 284 if (!k_sel || !k_bel || !k_evi || !k_out || !k_srch || !k_plan || !k_goal || 285 s->field_writer[1] != k_sel->id || sr->field[6] != p->field[0]) 286 return decline(l, RX_LIVING_WHY_AUTHOR); 287 288 /* The realization Omega's store verified for exactly these bytes. */ 289 RxOmegaRealization real; 290 if (rx_omega_store_find(l->omega, s->field[1], &real) != 0 || !real.verified || 291 real.code_len == 0 || real.code_len > sizeof(real.code) || 292 real.kind >= l->omega->cfg.n_slots) 293 return decline(l, RX_LIVING_WHY_STORE); 294 for (uint32_t k = 0; k < 4; k++) { 295 uint64_t word = 0; 296 for (uint32_t j = 0; j < 8; j++) word |= (uint64_t)real.id.bytes[k*8+j] << (8*j); 297 if (word != s->field[1+k]) return decline(l, RX_LIVING_WHY_STORE); 298 } 299 SemanticId again; 300 if (rx_omega_identity_of(l->omega, real.code, real.code_len, &again) != 0 || 301 memcmp(again.bytes, real.id.bytes, 32) != 0) 302 return decline(l, RX_LIVING_WHY_STORE); 303 304 /* Selection <- measure <- verdict <- synthesis, for the selected slot, 305 * and GPU output <- GPU input (the seat's claim) <- prepare. */ 306 uint32_t k = real.kind; 307 const RxCrumb *k_meas = parent_by(w, k_sel, l->omega->r_measure[k]); 308 const RxCrumb *k_verd = k_meas ? parent_by(w, k_meas, l->omega->r_verify[k]) : NULL; 309 const RxCrumb *k_syn = k_verd ? parent_by(w, k_verd, l->omega->r_synth[k]) : NULL; 310 const RxCrumb *k_in = first_trial(w, l, k_out); 311 if (!k_meas || !k_verd || !k_syn || !k_in || 312 !parent_by(w, k_bel, l->r_evidence) || !parent_by(w, k_evi, l->r_seat)) 313 return decline(l, RX_LIVING_WHY_CHAIN); 314 315 pv.epoch = epoch; 316 pv.goal_seq = g->field[0]; 317 pv.plan_seq = p->field[0]; 318 link(&pv, RX_LINK_GOAL, k_goal, l->aien->o.goal); 319 link(&pv, RX_LINK_PLAN, k_plan, l->aien->o.plan); 320 link(&pv, RX_LINK_SEARCH, k_srch, l->omega->o.search); 321 link(&pv, RX_LINK_GPU_INPUT, k_in, l->o.input); 322 link(&pv, RX_LINK_GPU_OUTPUT, k_out, l->o.output); 323 link(&pv, RX_LINK_EVIDENCE, k_evi, l->o.evidence); 324 link(&pv, RX_LINK_BELIEF, k_bel, l->aien->o.experiment_belief); 325 link(&pv, RX_LINK_SYNTH, k_syn, l->omega->o.candidate[k]); 326 link(&pv, RX_LINK_VERDICT, k_verd, l->omega->o.verdict[k]); 327 link(&pv, RX_LINK_MEASURE, k_meas, l->omega->o.measure[k]); 328 link(&pv, RX_LINK_SELECTION, k_sel, l->omega->o.selection); 329 330 RxGenObject objs[RX_LINK_COUNT]; 331 bind_snapshot(&objs[RX_LINK_GOAL], g, RX_OT_GOAL); 332 bind_snapshot(&objs[RX_LINK_PLAN], p, RX_OT_PLAN); 333 bind_snapshot(&objs[RX_LINK_SEARCH], sr, RX_OT_SEARCH); 334 bind_crumb(&objs[RX_LINK_GPU_INPUT], &pv.link[RX_LINK_GPU_INPUT]); 335 bind_snapshot(&objs[RX_LINK_GPU_OUTPUT], out, RX_OT_LIVING_OUTPUT); 336 bind_snapshot(&objs[RX_LINK_EVIDENCE], e, RX_OT_LIVING_EVIDENCE); 337 bind_snapshot(&objs[RX_LINK_BELIEF], b, RX_OT_EXPERIMENT_BELIEF); 338 bind_crumb(&objs[RX_LINK_SYNTH], &pv.link[RX_LINK_SYNTH]); 339 bind_crumb(&objs[RX_LINK_VERDICT], &pv.link[RX_LINK_VERDICT]); 340 bind_crumb(&objs[RX_LINK_MEASURE], &pv.link[RX_LINK_MEASURE]); 341 bind_snapshot(&objs[RX_LINK_SELECTION], s, RX_OT_SELECTION); 342 343 RxLivingEvidence ev; 344 memset(&ev, 0, sizeof ev); 345 memcpy(ev.magic, "R13EVID1", 8); 346 ev.epoch = epoch; 347 ev.expected = e->field[1]; 348 ev.observed = e->field[2]; 349 ev.selected_ps = s->field[5]; 350 ev.reference_ps = s->field[7]; 351 ev.trials = e->field[4]; 352 RxLivingConfig cfg; 353 memset(&cfg, 0, sizeof cfg); 354 memcpy(cfg.magic, "R13CONF1", 8); 355 cfg.regime = s->field[6]; 356 cfg.margin_pct = l->omega->cfg.margin_pct; 357 cfg.kind = real.kind; 358 cfg.code_len = real.code_len; 359 memcpy(cfg.identity, real.id.bytes, 32); 360 361 /* The authority epoch and generation of the experiment grant the seat 362 * held: the grant the GPU work ran under. */ 363 RxObject slot; 364 AienosCapEntry grant; 365 if (rx_world_read(w, l->caps.output_slot, &slot) != RX_OK || 366 aienos_cap_inspect(l->authority, 367 (AienosCapRef){(uint32_t)slot.field[0], slot.field[1]}, &grant) != 0) 368 return decline(l, RX_LIVING_WHY_GRANT); 369 370 uint64_t id = 0; 371 const int durable = rx_gen_exec_running(l->store); 372 if (durable && l->proposed_epoch != epoch) { 373 /* A proposal the durable executor finished since the last run. */ 374 RxGenJobResult jr; 375 if (rx_gen_job_state(l->store, RX_GEN_JOB_PROPOSE, &jr) == RX_GEN_JOB_DONE) { 376 rx_gen_job_take(l->store, RX_GEN_JOB_PROPOSE); 377 if (jr.rc == RX_GEN_ERR_HALTED) return halted_wait(w, l->r_candidate); 378 if (jr.rc == RX_GEN_OK) { 379 l->proposed_epoch = jr.key; 380 l->candidate_id = jr.id; 381 } else if (jr.key == epoch) { 382 decline(l, RX_LIVING_WHY_PROPOSE); 383 return -1; 384 } 385 } 386 } 387 if (l->proposed_epoch == epoch) { 388 id = l->candidate_id; 389 } else { 390 RxGenDraft draft; 391 memset(&draft, 0, sizeof draft); 392 draft.authority_epoch = grant.epoch; 393 draft.authority_generation = grant.generation; 394 draft.proofs_ok = real.verified && e->field[3] == 1 && b->field[2] == 1; 395 draft.objects = objs; 396 draft.n_objects = RX_LINK_COUNT; 397 draft.evidence = (const uint8_t *)&ev; 398 draft.evidence_len = sizeof ev; 399 draft.model = (const uint8_t *)b->field; 400 draft.model_len = sizeof b->field; 401 draft.realization = real.code; 402 draft.realization_len = real.code_len; 403 draft.config = (const uint8_t *)&cfg; 404 draft.config_len = sizeof cfg; 405 draft.provenance = (const uint8_t *)&pv; 406 draft.provenance_len = sizeof pv; 407 if (durable) { 408 /* The store's fsyncs run on the durable executor, not on this 409 * worker. A proposal still in flight (this epoch's or an older 410 * one's) resumes this activation when it lands. */ 411 int prc = RX_GEN_OK; 412 if (rx_gen_job_state(l->store, RX_GEN_JOB_PROPOSE, NULL) == RX_GEN_JOB_IDLE) 413 prc = rx_gen_post_propose_as(l->store, epoch, RX_LIVING_PREPARE_SUBJ, 414 rx_caller_find(l->keys, RX_LIVING_PREPARE_SUBJ), &draft, 415 resume_candidate, l); 416 if (prc == RX_GEN_ERR_HALTED) return halted_wait(w, l->r_candidate); 417 if (prc != RX_GEN_OK) { 418 decline(l, RX_LIVING_WHY_PROPOSE); 419 return -1; 420 } 421 return RX_FN_DEFER; 422 } 423 if (rx_gen_propose_as(l->store, RX_LIVING_PREPARE_SUBJ, 424 rx_caller_find(l->keys, RX_LIVING_PREPARE_SUBJ), &draft, &id) != RX_GEN_OK) { 425 decline(l, RX_LIVING_WHY_PROPOSE); 426 return -1; 427 } 428 l->proposed_epoch = epoch; 429 l->candidate_id = id; 430 } 431 record_lesson(l, epoch, k_evi, k_bel, e, &ev, &real.id, p->field[3], s->field[6]); 432 put(c, l->o.candidate, 0, id); 433 put(c, l->o.candidate, 1, epoch); 434 for (uint32_t j = 0; j < 4; j++) put(c, l->o.candidate, 2 + j, s->field[1 + j]); 435 put(c, l->o.candidate, 6, s->field[6]); 436 put(c, l->o.candidate, 7, s->field[5]); 437 return 0; 438 } 439 440 /* R16 C5: the promotion subject presents its own credential. C7: a promoter 441 * given a keyring without that credential fails here, before anything is 442 * posted to the executor (a promoter with no keyring at all serves an 443 * unbound store, which checks no credential). */ 444 static int promotion_caller(const RxLivingPromoter *p, RxPromotionRequest *req) { 445 const RxCallerCred *c = rx_caller_find(p->keys, RX_LIVING_PROMOTE_SUBJ); 446 if (c) { req->caller = *c; return RX_GEN_OK; } 447 return p->keys ? RX_GEN_ERR_IDENTITY : RX_GEN_OK; 448 } 449 450 int rx_living_native_authority(void *ctx, uint32_t cap_id, uint64_t generation, 451 uint32_t subject, uint64_t resource, uint32_t rights) { 452 AienosCapEntry entry; 453 return aienos_cap_validate(ctx, (AienosCapRef){cap_id, generation}, 454 subject, resource, rights, &entry); 455 } 456 457 static int native_promotion(void *ctx, uint32_t cap_id, uint64_t generation, 458 uint32_t subject, uint64_t resource, uint32_t rights) { 459 AienosCapEntry entry; 460 return aienos_cap_validate(ctx, (AienosCapRef){cap_id, generation}, 461 subject, resource, rights, &entry); 462 } 463 464 /* ---- generation.promote ---- 465 * The separate promotion authority. It holds RX_GEN_RIGHT_PROMOTE on 466 * RX_GEN_RES_PROMOTION; R9 validates that against the native authority and 467 * refuses a promoter that is also the proposer. Only after R9 commits does 468 * the in-force record, which production reads, name the new realization. */ 469 /* The promotion's durable work (blobs, root, journal, flip, receipt, event, 470 * all fsynced) runs on the store's executor; this worker only decides it and, 471 * once it is done, publishes it, so production keeps the worker meanwhile. 472 * R9 still validates the promotion right and refuses a self-promotion inside 473 * rx_gen_promote. Returns 1 while the promotion is in flight. */ 474 static int promote_durable(RxLivingPromoter *p, uint64_t id, int *rc, uint64_t *active, 475 uint64_t *ns) { 476 RxGenJobResult jr; 477 uint64_t lineage = 0; 478 int st = rx_gen_job_state(p->store, RX_GEN_JOB_PROMOTE, &jr); 479 if (st == RX_GEN_JOB_DONE && jr.key != id) { 480 /* An earlier candidate's outcome: it is on disk; the record now 481 * describes a newer candidate. */ 482 rx_gen_job_take(p->store, RX_GEN_JOB_PROMOTE); 483 st = RX_GEN_JOB_IDLE; 484 } 485 if (st == RX_GEN_JOB_DONE) { 486 rx_gen_job_take(p->store, RX_GEN_JOB_PROMOTE); 487 *rc = jr.rc; 488 *ns = jr.ns; 489 *active = jr.active; 490 return 0; 491 } 492 rx_gen_active(p->store, active, &lineage); 493 if (*active == id) { 494 *rc = RX_GEN_OK; /* a re-run after an invalidated publication */ 495 return 0; 496 } 497 if (st == RX_GEN_JOB_PENDING) return 1; 498 RxPromotionRequest req = {id, RX_LIVING_PROMOTE_SUBJ, 499 p->promotion_authority.cap_id, p->promotion_authority.generation, 500 RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, {0, {0}}}; 501 *rc = promotion_caller(p, &req); 502 if (*rc != RX_GEN_OK) return 0; 503 *rc = rx_gen_post_promote(p->store, id, &req, native_promotion, (void *)p->authority, 504 resume_promoter, p); 505 rx_caller_wipe(&req.caller); 506 return *rc == RX_GEN_OK; 507 } 508 509 /* Without an executor (the sequential reference) the stage runs to completion. */ 510 static void promote_inline(RxLivingPromoter *p, uint64_t id, int *rc, uint64_t *active, 511 uint64_t *ns) { 512 uint64_t lineage = 0, t0 = now_ns(); 513 rx_gen_active(p->store, active, &lineage); 514 if (*active == id) { 515 *rc = RX_GEN_OK; /* a re-run after an invalidated publication */ 516 return; 517 } 518 RxPromotionRequest req = {id, RX_LIVING_PROMOTE_SUBJ, 519 p->promotion_authority.cap_id, p->promotion_authority.generation, 520 RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, {0, {0}}}; 521 *rc = promotion_caller(p, &req); 522 if (*rc == RX_GEN_OK) 523 *rc = rx_gen_promote(p->store, &req, native_promotion, (void *)p->authority, 524 NULL, NULL, NULL, NULL); 525 rx_caller_wipe(&req.caller); 526 *ns = now_ns() - t0; 527 rx_gen_active(p->store, active, &lineage); 528 } 529 530 static int fn_promote(RxCtx *c) { 531 RxLivingPromoter *p = c->user; 532 const RxSnapshotDep *cand = input(c, p->candidate); 533 const RxSnapshotDep *done = input(c, p->promotion); 534 if (!cand || !done) return -1; 535 uint64_t id = cand->field[0]; 536 if (!id || done->field[0] == id) return 0; 537 538 uint64_t active = 0, ns = 0; 539 int rc = RX_GEN_OK; 540 if (rx_gen_exec_running(p->store)) { 541 if (promote_durable(p, id, &rc, &active, &ns)) return RX_FN_DEFER; 542 if (rc == RX_GEN_ERR_HALTED) return halted_wait(p->world, p->reaction); 543 } else { 544 promote_inline(p, id, &rc, &active, &ns); 545 } 546 p->result = rc; 547 if (rc == RX_GEN_OK && p->lesson && p->lesson->store && p->lesson->epoch == cand->field[1] && 548 !p->lesson->promotion) { 549 RxLivingLesson *ls = p->lesson; /* G1/N6: the matching lesson becomes a fact */ 550 (void)rx_living_lesson_promote(p->world, ls->store, ls->candidate, ls->evidence_ref, ls->digest, &ls->promotion); 551 } 552 put(c, p->promotion, 0, id); 553 put(c, p->promotion, 1, (uint64_t)(int64_t)rc); 554 put(c, p->promotion, 2, ns); 555 put(c, p->promotion, 3, active); 556 if (rc != RX_GEN_OK) return 0; 557 put(c, p->inforce, 0, cand->field[1]); 558 for (uint32_t j = 0; j < 4; j++) put(c, p->inforce, 1 + j, cand->field[2 + j]); 559 put(c, p->inforce, 5, cand->field[7]); 560 put(c, p->inforce, 6, cand->field[6]); 561 put(c, p->inforce, 7, active); 562 return 0; 563 } 564 565 int rx_living_create(RxLiving *l, RxWorld *w, RxAienFaculty *aien, 566 RxOmegaFaculty *omega, RxGenStore *store, 567 const struct AienosCapView *authority) { 568 if (!l || !w || !aien || !omega || !store || !authority) return RX_ERR_ARG; 569 memset(l, 0, sizeof *l); 570 l->world = w; l->aien = aien; l->omega = omega; 571 l->store = store; l->authority = authority; 572 uint64_t z[RX_MAX_FIELDS] = {0}; 573 RxObjRef *r[6] = {&l->o.input, &l->o.output, &l->o.evidence, 574 &l->o.candidate, &l->o.promotion, &l->o.inforce}; 575 for (uint32_t k = 0; k < 6; k++) { 576 int rc = rx_world_create(w, RX_OT_LIVING_INPUT + k, RX_PERSIST_RESIDENT, 577 RX_LIVING_RES_BASE + k, z, r[k]); 578 if (rc != RX_OK) return rc; 579 } 580 int rc = rx_world_attach_physical(w, l->o.input); 581 if (rc != RX_OK) return rc; 582 return rx_world_attach_physical(w, l->o.output); 583 } 584 585 static void base(RxReactionDesc *d, const char *name, uint32_t faculty, 586 uint32_t subject, RxFn fn, void *user) { 587 memset(d, 0, sizeof *d); 588 d->name = name; d->faculty = faculty; d->subject = subject; 589 d->priority = RX_PRIO_LEARNING; d->fn = fn; d->user = user; 590 } 591 592 static RxCapNeed cap(RxCapRef ref, uint32_t res, uint32_t rights) { 593 return (RxCapNeed){ref, RX_LIVING_RES_BASE + res, rights}; 594 } 595 596 int rx_living_register(RxLiving *l, const RxLivingCaps *caps, 597 RxLivingPromoter *promoter) { 598 RxWorld *w = l->world; 599 if (!promoter || promoter->world != w || promoter->store != l->store || 600 promoter->candidate.id != l->o.candidate.id || 601 promoter->promotion.id != l->o.promotion.id || 602 promoter->inforce.id != l->o.inforce.id) return RX_ERR_ARG; 603 const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE; 604 l->caps = *caps; 605 promoter->lesson = &l->lesson; 606 int rc; 607 RxReactionDesc d; 608 /* A resident world puts the store's physical work on its own executor so 609 * a promotion never occupies a semantic worker. The sequential reference 610 * runs every stage to completion on its one thread, as designed. */ 611 if (!w->sequential && rx_gen_exec_start(l->store) != RX_GEN_OK) return RX_ERR_FULL; 612 613 base(&d, "living.experiment.prepare", RX_FACULTY_OMEGA, RX_LIVING_SUBJ, fn_prepare, l); 614 d.n_triggers = 1; 615 d.triggers[0] = (RxDep){l->omega->o.search, RX_FIELD(0) | RX_FIELD(6)}; 616 d.n_reads = 2; 617 d.reads[0] = (RxDep){l->aien->o.plan, RX_ALL_FIELDS}; 618 d.reads[1] = (RxDep){l->o.input, RX_ALL_FIELDS}; 619 d.n_writes = 1; 620 d.writes[0] = (RxDep){l->o.input, RX_ALL_FIELDS}; 621 d.n_caps = 3; 622 d.caps[0] = (RxCapNeed){caps->search_read, RX_OMEGA_RES_BASE + RX_OMEGA_RES_SEARCH, R}; 623 d.caps[1] = (RxCapNeed){caps->plan_read, RX_AIEN_RES_BASE + RX_AIEN_RES_PLAN, R}; 624 d.caps[2] = cap(caps->input_write, RX_LIVING_RES_INPUT, RW); 625 if ((rc = rx_world_add_reaction_keyed(w, l->keys, &d, &l->r_prepare)) != RX_OK) return rc; 626 627 /* The seat: one data trigger, one write, R5 asks for the Blackwell 628 * feature, and its output WRITE reference is read from the R8 slot at 629 * every check. */ 630 base(&d, "living.blackwell.add", RX_FACULTY_OMEGA, RX_LIVING_SEAT_SUBJ, 631 fn_seat_is_not_cpu, l); 632 d.priority = RX_PRIO_FOREGROUND; 633 d.need.accelerator_features = RX_ACCEL_BLACKWELL; 634 d.n_triggers = 1; 635 d.triggers[0] = (RxDep){l->o.input, RX_FIELD(0) | RX_FIELD(1)}; 636 d.n_writes = 1; 637 d.writes[0] = (RxDep){l->o.output, RX_FIELD(0)}; 638 d.n_caps = 3; 639 d.caps[0] = cap(caps->input_seat_read, RX_LIVING_RES_INPUT, R); 640 d.caps[1] = cap((RxCapRef){UINT32_MAX, 0}, RX_LIVING_RES_OUTPUT, RX_RIGHT_WRITE); 641 d.caps[2] = (RxCapNeed){caps->output_slot_read, 642 w->objects[caps->output_slot.id].resource, R}; 643 rx_aegis_use_slot(&d, 1, caps->output_slot); 644 if ((rc = rx_world_add_reaction_keyed(w, l->keys, &d, &l->r_seat)) != RX_OK) return rc; 645 646 base(&d, "living.experiment.evidence", RX_FACULTY_OMEGA, RX_LIVING_SUBJ, fn_evidence, l); 647 d.n_triggers = 1; 648 d.triggers[0] = (RxDep){l->o.output, RX_FIELD(0)}; 649 d.n_reads = 2; 650 d.reads[0] = (RxDep){l->o.input, RX_ALL_FIELDS}; 651 d.reads[1] = (RxDep){l->o.evidence, RX_ALL_FIELDS}; 652 d.n_writes = 2; 653 d.writes[0] = (RxDep){l->o.evidence, RX_ALL_FIELDS}; 654 d.writes[1] = (RxDep){l->o.input, RX_ALL_FIELDS}; 655 d.n_caps = 3; 656 d.caps[0] = cap(caps->output_read, RX_LIVING_RES_OUTPUT, R); 657 d.caps[1] = cap(caps->input_write, RX_LIVING_RES_INPUT, RW); 658 d.caps[2] = cap(caps->evidence_write, RX_LIVING_RES_EVIDENCE, RW); 659 if ((rc = rx_world_add_reaction_keyed(w, l->keys, &d, &l->r_evidence)) != RX_OK) return rc; 660 661 base(&d, "generation.prepare", RX_FACULTY_OMEGA, RX_LIVING_PREPARE_SUBJ, fn_candidate, l); 662 /* Wakes on the selection, and on the goal and plan the proposal must be 663 * authored by. The body researches a new core class on its own, before 664 * any goal exists (aien.plan, core-class hypothesis), so a selection can 665 * land before the goal; prepare then declines for want of an author, and 666 * that decline must not be final: the goal (or a new plan) arriving runs 667 * the same check again on the same selection. With the goal and plan as 668 * plain reads, nothing woke prepare again and the organism never adapted 669 * (OMEGA-R14-HOST under judge load, 2026-10-20). */ 670 d.n_triggers = 3; 671 d.triggers[0] = (RxDep){l->omega->o.selection, RX_ALL_FIELDS}; 672 d.triggers[1] = (RxDep){l->aien->o.goal, RX_FIELD(0)}; 673 d.triggers[2] = (RxDep){l->aien->o.plan, RX_FIELD(0)}; 674 d.n_reads = 5; 675 d.reads[0] = (RxDep){l->aien->o.experiment_belief, RX_ALL_FIELDS}; 676 d.reads[1] = (RxDep){l->o.evidence, RX_ALL_FIELDS}; 677 d.reads[2] = (RxDep){l->o.candidate, RX_ALL_FIELDS}; 678 d.reads[3] = (RxDep){l->omega->o.search, RX_ALL_FIELDS}; 679 d.reads[4] = (RxDep){l->o.output, RX_ALL_FIELDS}; 680 d.n_writes = 1; 681 d.writes[0] = (RxDep){l->o.candidate, RX_ALL_FIELDS}; 682 d.n_caps = 8; 683 d.caps[0] = (RxCapNeed){caps->selection_read, RX_OMEGA_RES_BASE + RX_OMEGA_RES_SELECTION, R}; 684 d.caps[1] = (RxCapNeed){caps->belief_read, 685 RX_AIEN_RES_BASE + RX_AIEN_RES_EXPERIMENT_BELIEF, R}; 686 d.caps[2] = cap(caps->evidence_prepare_read, RX_LIVING_RES_EVIDENCE, R); 687 d.caps[3] = cap(caps->candidate_write, RX_LIVING_RES_CANDIDATE, RW); 688 d.caps[4] = (RxCapNeed){caps->goal_prepare_read, RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, R}; 689 d.caps[5] = (RxCapNeed){caps->plan_prepare_read, RX_AIEN_RES_BASE + RX_AIEN_RES_PLAN, R}; 690 d.caps[6] = (RxCapNeed){caps->search_prepare_read, 691 RX_OMEGA_RES_BASE + RX_OMEGA_RES_SEARCH, R}; 692 d.caps[7] = cap(caps->output_prepare_read, RX_LIVING_RES_OUTPUT, R); 693 if ((rc = rx_world_add_reaction_keyed(w, l->keys, &d, &l->r_candidate)) != RX_OK) return rc; 694 695 base(&d, "generation.promote", RX_FACULTY_ROOT, RX_LIVING_PROMOTE_SUBJ, fn_promote, 696 promoter); 697 d.priority = RX_PRIO_MAINTENANCE; 698 d.n_triggers = 1; 699 d.triggers[0] = (RxDep){l->o.candidate, RX_ALL_FIELDS}; 700 d.n_reads = 1; 701 d.reads[0] = (RxDep){l->o.promotion, RX_ALL_FIELDS}; 702 d.n_writes = 2; 703 d.writes[0] = (RxDep){l->o.promotion, RX_ALL_FIELDS}; 704 d.writes[1] = (RxDep){l->o.inforce, RX_ALL_FIELDS}; 705 d.n_caps = 3; 706 d.caps[0] = cap(promoter->candidate_read, RX_LIVING_RES_CANDIDATE, R); 707 d.caps[1] = cap(promoter->promotion_write, RX_LIVING_RES_PROMOTION, RW); 708 d.caps[2] = cap(promoter->inforce_write, RX_LIVING_RES_INFORCE, RW); 709 return rx_world_add_reaction_keyed(w, promoter->keys, &d, &promoter->reaction); 710 } 711 712 /* ---- generation.restore (R14) ---- 713 * The body started. The in-force record is world state, so a restarted 714 * process has none; the durable generation R9 recovered says what was in 715 * force. The promotion subject reads it back through R9, Omega verifies the 716 * bytes again with its own verifier, and only then is the record written. 717 * It never promotes and never touches R9's pointer. */ 718 static int restore_refuse(RxCtx *c, RxLiving *l, uint64_t why) { 719 put(c, l->o.restore, 2, RX_LIVING_RESTORE_REFUSED); 720 put(c, l->o.restore, 5, why); 721 return 0; 722 } 723 724 static int fn_restore(RxCtx *c) { 725 RxLiving *l = c->user; 726 const RxSnapshotDep *rs = input(c, l->o.restore); 727 if (!rs) return -1; 728 uint64_t boot = rs->field[0]; 729 if (!boot || rs->field[1] == boot) return 0; 730 uint64_t active = 0, lineage = 0; 731 rx_gen_active(l->store, &active, &lineage); 732 put(c, l->o.restore, 1, boot); 733 put(c, l->o.restore, 3, active); 734 put(c, l->o.restore, 6, lineage); 735 put(c, l->o.restore, 4, 0); 736 put(c, l->o.restore, 5, 0); 737 738 uint8_t code[AARCH64_MAX_CODE_BYTES]; 739 RxLivingConfig cfg; 740 RxLivingEvidence ev; 741 size_t n = 0, cn = 0, en = 0; 742 int rc = rx_gen_read_blob(l->store, active, "realization", code, sizeof code, &n); 743 if (rc != RX_GEN_OK) return restore_refuse(c, l, (uint64_t)(int64_t)rc); 744 if (n == 0) { /* genesis: nothing was ever put in force */ 745 put(c, l->o.restore, 2, RX_LIVING_RESTORE_REFERENCE); 746 return 0; 747 } 748 rc = rx_gen_read_blob(l->store, active, "config", (uint8_t *)&cfg, sizeof cfg, &cn); 749 if (rc == RX_GEN_OK) 750 rc = rx_gen_read_blob(l->store, active, "evidence", (uint8_t *)&ev, sizeof ev, &en); 751 if (rc != RX_GEN_OK) return restore_refuse(c, l, (uint64_t)(int64_t)rc); 752 if (cn != sizeof cfg || en != sizeof ev || memcmp(cfg.magic, "R13CONF1", 8) != 0 || 753 memcmp(ev.magic, "R13EVID1", 8) != 0 || cfg.code_len != n) 754 return restore_refuse(c, l, RX_LIVING_RESTORE_WHY_CONFIG); 755 756 SemanticId id; 757 uint32_t why = 0; 758 int ok = rx_omega_readmit(l->omega, code, n, (uint32_t)cfg.kind, cfg.regime, &id, &why); 759 if (ok < 0) return -1; 760 if (ok != 0) return restore_refuse(c, l, why); 761 if (memcmp(id.bytes, cfg.identity, 32) != 0) 762 return restore_refuse(c, l, RX_LIVING_RESTORE_WHY_IDENTITY); 763 764 uint64_t word[4] = {0, 0, 0, 0}; 765 for (uint32_t k = 0; k < 4; k++) 766 for (uint32_t j = 0; j < 8; j++) word[k] |= (uint64_t)id.bytes[k*8+j] << (8*j); 767 put(c, l->o.inforce, 0, ev.epoch); 768 for (uint32_t k = 0; k < 4; k++) put(c, l->o.inforce, 1 + k, word[k]); 769 put(c, l->o.inforce, 5, ev.selected_ps); 770 put(c, l->o.inforce, 6, cfg.regime); 771 put(c, l->o.inforce, 7, active); 772 put(c, l->o.restore, 2, RX_LIVING_RESTORE_RESTORED); 773 put(c, l->o.restore, 4, word[0]); 774 return 0; 775 } 776 777 int rx_living_register_restore(RxLiving *l, RxLivingPromoter *promoter, RxCapRef restore_write) { 778 RxWorld *w = l->world; 779 if (!promoter || promoter->world != w || promoter->store != l->store || 780 promoter->inforce.id != l->o.inforce.id) return RX_ERR_ARG; 781 uint64_t z[RX_MAX_FIELDS] = {0}; 782 int rc = rx_world_create(w, RX_OT_LIVING_RESTORE, RX_PERSIST_RESIDENT, 783 RX_LIVING_RES_BASE + RX_LIVING_RES_RESTORE, z, &l->o.restore); 784 if (rc != RX_OK) return rc; 785 const uint32_t RW = RX_RIGHT_READ | RX_RIGHT_WRITE; 786 RxReactionDesc d; 787 base(&d, "generation.restore", RX_FACULTY_ROOT, RX_LIVING_PROMOTE_SUBJ, fn_restore, l); 788 d.priority = RX_PRIO_MAINTENANCE; 789 d.n_triggers = 1; 790 d.triggers[0] = (RxDep){l->o.restore, RX_FIELD(0)}; 791 d.n_reads = 1; 792 d.reads[0] = (RxDep){l->o.restore, RX_ALL_FIELDS}; 793 d.n_writes = 2; 794 d.writes[0] = (RxDep){l->o.restore, RX_ALL_FIELDS & ~RX_FIELD(0)}; 795 d.writes[1] = (RxDep){l->o.inforce, RX_ALL_FIELDS}; 796 d.n_caps = 2; 797 d.caps[0] = cap(restore_write, RX_LIVING_RES_RESTORE, RW); 798 d.caps[1] = cap(promoter->inforce_write, RX_LIVING_RES_INFORCE, RW); 799 return rx_world_add_reaction_keyed(w, promoter->keys, &d, &l->r_restore); 800 } 801 802 /* ---- R16 C6: production caller enrollment ---- */ 803 804 static int enroll_one(RxWorld *w, RxCallerKeyring *k, uint32_t subject) { 805 if (k->n >= RX_CALLER_KEYRING_MAX) return RX_ERR_FULL; 806 if (rx_world_enroll_caller(w, subject, &k->cred[k->n]) != RX_CALLER_OK) 807 return RX_ERR_IDENTITY; /* closed, already enrolled, full or no entropy */ 808 k->subject[k->n++] = subject; 809 return RX_OK; 810 } 811 812 int rx_living_enroll_callers(RxWorld *w, RxLivingKeyrings *k) { 813 if (!w || !k) return RX_ERR_ARG; 814 memset(k, 0, sizeof *k); 815 int rc; 816 if ((rc = enroll_one(w, &k->omega, RX_OMEGA_SUBJ_SERVE)) != RX_OK || 817 (rc = enroll_one(w, &k->omega, RX_OMEGA_SUBJ_OMEGA)) != RX_OK || 818 (rc = enroll_one(w, &k->aien, RX_AIEN_SUBJ)) != RX_OK || 819 (rc = enroll_one(w, &k->aegis, RX_AEGIS_SUBJ)) != RX_OK || 820 (rc = enroll_one(w, &k->aegis, RX_AEGIS_ROOT_SUBJ)) != RX_OK || 821 (rc = enroll_one(w, &k->living, RX_LIVING_SUBJ)) != RX_OK || 822 (rc = enroll_one(w, &k->living, RX_LIVING_SEAT_SUBJ)) != RX_OK || 823 (rc = enroll_one(w, &k->living, RX_LIVING_PREPARE_SUBJ)) != RX_OK || 824 (rc = enroll_one(w, &k->promoter, RX_LIVING_PROMOTE_SUBJ)) != RX_OK) { 825 for (unsigned i = 0; i < sizeof *k; i++) ((volatile uint8_t *)k)[i] = 0; 826 return rc; 827 } 828 return RX_OK; 829 } 830 831 int rx_living_lesson_evidence_ok(const CxStore *s, uint64_t evidence, const uint8_t object_digest[32]) { 832 CxRecord rec; 833 if (!s || !object_digest || evidence == 0 || evidence > s->n) return RX_ERR_NOT_FOUND; 834 if (cx_recall_id(s, evidence, &rec) == CX_ERR_DIGEST || !rec.verified) return RX_ERR_IDENTITY; 835 if (rec.hdr.kind != CX_K_EVIDENCE_REF || rec.hdr.n < RX_LIVING_EV_WORDS) return RX_ERR_ARG; 836 for (uint32_t i = 0; i < 4; i++) { 837 uint64_t v = 0; 838 for (uint32_t b = 0; b < 8; b++) v |= (uint64_t)object_digest[8 * i + b] << (8 * b); 839 if (rec.payload[RX_LIVING_EV_DIGEST0 + i] != v) return RX_ERR_ARG; 840 } 841 return RX_OK; 842 } 843 844 static int evidence_gate(const CxStore *s, uint64_t evidence, const void *digest) { 845 return rx_living_lesson_evidence_ok(s, evidence, digest); 846 } 847 848 int rx_living_lesson_promote(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, 849 const uint8_t object_digest[32], uint64_t *out_id) { 850 if (!object_digest) return RX_ERR_ARG; 851 return rx_cortex_promote_checked_in(w, s, candidate, evidence, evidence_gate, object_digest, out_id); 852 } 853 854 uint32_t rx_living_explored_keys(CxStore *s, uint64_t subject, uint64_t *keys, uint32_t max) { 855 static CxRecord recs[64]; 856 CxFilter f = {CX_EVIDENCE, CX_K_PROMOTION, 0, 1}; 857 uint32_t n = 0, got = s && keys ? cx_recall(s, subject, 0, UINT64_MAX, &f, recs, 64) : 0; 858 for (uint32_t i = 0; i < got && n < max; i++) { 859 CxRecord cand, ref; 860 if (!recs[i].verified || recs[i].hdr.n < 8 || 861 cx_recall_id(s, recs[i].hdr.links[0], &cand) != CX_OK || !cand.verified || 862 cx_recall_id(s, recs[i].hdr.links[1], &ref) != CX_OK || !ref.verified || 863 cand.hdr.kind != CX_K_CANDIDATE || cand.hdr.n < RX_LIVING_LESSON_WORDS || 864 ref.hdr.kind != CX_K_EVIDENCE_REF) continue; 865 uint64_t k = cand.payload[RX_LIVING_LESSON_KEY]; 866 uint32_t j = 0; 867 while (j < n && keys[j] != k) j++; 868 if (j == n) keys[n++] = k; 869 } 870 return n; 871 }