omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / rx_r13_cortex_episode.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 | /* G1: the R13 living body journals one positive episode into canonical Cortex. |
| 18 | * |
| 19 | * Reuses the whole R13 rig by including rx_r13_living.c (its main renamed). |
| 20 | * Attaches the existing World->Cortex recorder (rx_cortex_record.h) to the |
| 21 | * living world on a journal file, runs the POSITIVE episode, finds the lesson |
| 22 | * lesson and evidence reference the BODY wrote (generation.prepare/promote), then proves |
| 23 | * the provenance chain, the digest-gated promotion, tamper refusal and restart. */ |
| 24 | #define main r13_main_unused |
| 25 | #include "rx_r13_living.c" |
| 26 | #undef main |
| 27 | |
| 28 | static int g_pass, g_fail; |
| 29 | #define CHECK(c, ...) do { if (c) { g_pass++; printf(" ok "); } else { g_fail++; printf(" FAIL "); } \ |
| 30 | printf(__VA_ARGS__); printf("\n"); } while (0) |
| 31 | |
| 32 | static void words(const uint8_t d[32], uint64_t w[4]) { |
| 33 | for (int i = 0; i < 4; i++) { w[i] = 0; for (int b = 0; b < 8; b++) w[i] |= (uint64_t)d[8 * i + b] << (8 * b); } |
| 34 | } |
| 35 | |
| 36 | enum { N_STEP = 12 }; |
| 37 | static const char *const g_step[N_STEP] = {"observation", "belief", "prediction", "hypothesis", "plan", |
| 38 | "search", "gpu-input", "experiment", "evidence", "exp-belief", "candidate", "promotion"}; |
| 39 | |
| 40 | /* Cortex record of the newest crumb that wrote any field of `o`. */ |
| 41 | static uint64_t rec_of(Rig *r, RxObjRef o) { |
| 42 | uint64_t best = 0; |
| 43 | for (uint32_t fi = 0; fi < RX_MAX_FIELDS; fi++) { |
| 44 | uint64_t id = rx_cortex_record_of(&r->w, rx_world_explain(&r->w, o, fi)); |
| 45 | if (id > best) best = id; |
| 46 | } |
| 47 | return best; |
| 48 | } |
| 49 | /* Nearest ancestor of `from` (provenance, nearest first) written for object slot `slot`. */ |
| 50 | static uint64_t nearest(const CxStore *s, uint64_t from, uint32_t slot) { |
| 51 | static uint64_t p[4096]; |
| 52 | uint32_t n = cx_provenance(s, from, p, 4096); |
| 53 | for (uint32_t i = 0; i < n; i++) if (cx_get(s, p[i])->subject == (uint64_t)slot + 1u && cx_get(s, p[i])->kind != CX_K_ENTITY_CREATED) return p[i]; |
| 54 | return 0; |
| 55 | } |
| 56 | /* Protected records, copied for the byte-identical comparison. */ |
| 57 | typedef struct { uint32_t n; uint64_t id[8192]; CxObject o[8192]; uint64_t pl[8192][CX_WREC_WORDS + 8]; } Prot; |
| 58 | static void snap(CxStore *s, Prot *p) { |
| 59 | p->n = 0; |
| 60 | for (uint64_t i = 1; i <= s->n && p->n < 8192; i++) { |
| 61 | const CxObject *o = cx_get(s, i); |
| 62 | if (!o->protect || o->n > CX_WREC_WORDS + 8) continue; |
| 63 | p->id[p->n] = i; p->o[p->n] = *o; |
| 64 | memcpy(p->pl[p->n], cx_payload(s, o), o->n * sizeof(uint64_t)); |
| 65 | p->n++; |
| 66 | } |
| 67 | } |
| 68 | static int corrupt_copy(const char *src, const char *dst, long off) { |
| 69 | FILE *a = fopen(src, "rb"), *b = fopen(dst, "wb"); |
| 70 | if (!a || !b) return -1; |
| 71 | int c; long i = 0; |
| 72 | while ((c = fgetc(a)) != EOF) { if (i++ == off) c ^= 0x01; fputc(c, b); } |
| 73 | fclose(a); fclose(b); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* ---- C2: a second life after a restart ---- |
| 79 | * A fresh body (new World and faculties, same R9 store) starts from the |
| 80 | * journal. Before AIEN plans, its explored-condition memory is seeded from |
| 81 | * the PROMOTED lessons in Cortex. `to_x`: the body moves to the X925 cores |
| 82 | * (the condition the first life explored); otherwise it stays on the A725. */ |
| 83 | typedef struct { uint32_t seeded; uint64_t status, plan, mem_count, mem_key; } Outcome; |
| 84 | static int episode(CxStore *src, int to_x, Outcome *o) { |
| 85 | memset(o, 0, sizeof *o); |
| 86 | Rig *r = calloc(1, sizeof *r); |
| 87 | cpu_set_t a, x; |
| 88 | if (!r || start(r, POSITIVE) != 0 || core_sets(&a, &x) != 0 || move_threads(&a) <= 0) return -1; |
| 89 | uint64_t keys[RX_AIEN_MEMORY_SLOTS]; |
| 90 | o->seeded = src ? rx_living_explored_keys(src, (uint64_t)r->living.o.evidence.id + 1u, keys, RX_AIEN_MEMORY_SLOTS) : 0; |
| 91 | if (o->seeded) { |
| 92 | RxCapRef mc = mint(r, EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_MEMORY, RX_RIGHT_WRITE); |
| 93 | if (rx_aien_seed_explored(&r->aien, mc, keys, o->seeded) != RX_OK) return -1; |
| 94 | } |
| 95 | int bad = publish_placement(r, CLASS_A725) != 0; |
| 96 | uint64_t limit = run_clock(r) + 60000000000ull; |
| 97 | while (!bad && field(r, r->omega.o.selection, 0) < 1 && run_clock(r) < limit) bad = serve(r) != 0; |
| 98 | while (!bad && field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED && run_clock(r) < limit) bad = serve(r) != 0; |
| 99 | if (bad || field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED) { stop(r); free(r); return -2; } |
| 100 | uint64_t target = field(r, r->aien.o.prediction, 5) * TARGET_PCT / 100u; |
| 101 | if (start_producer(r) != 0) { stop(r); free(r); return -1; } |
| 102 | if (to_x && (move_threads(&x) <= 0 || publish_placement(r, CLASS_X925) != 0)) bad = 1; |
| 103 | if (bad || publish_goal(r, target) != 0) { stop_producer(r); stop(r); free(r); return -1; } |
| 104 | limit = run_clock(r) + 60000000000ull; |
| 105 | while (run_clock(r) < limit && !atomic_load(&r->producer_error)) { |
| 106 | pause_us(1000); |
| 107 | o->plan = field(r, r->aien.o.plan, 0); |
| 108 | o->status = field(r, r->aien.o.assessment, 4); |
| 109 | if (o->plan || o->status == RX_AIEN_GOAL_UNMET_EXPLORED) break; |
| 110 | } |
| 111 | o->mem_count = field(r, r->aien.o.memory, 0); |
| 112 | o->mem_key = field(r, r->aien.o.plan, 3); |
| 113 | stop_producer(r); |
| 114 | stop(r); |
| 115 | free(r); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int main(void) { |
| 120 | signal(SIGPIPE, SIG_IGN); |
| 121 | setvbuf(stdout, NULL, _IONBF, 0); |
| 122 | if (operator_state(NULL) != 0 || seen_fit(1u << 17) != 0) return 2; |
| 123 | char dir[192], jr[256], jr2[256]; |
| 124 | if (scratch_dir(dir, sizeof dir, "r13-cortex") != 0) return 2; |
| 125 | snprintf(jr, sizeof jr, "%s/episode.cxj", dir); |
| 126 | snprintf(jr2, sizeof jr2, "%s/episode-flipped.cxj", dir); |
| 127 | Rig *r = calloc(1, sizeof *r); |
| 128 | static CxStore s, s2; |
| 129 | if (!r) return 2; |
| 130 | if (start(r, POSITIVE) != 0) { fprintf(stderr, "setup failed at stage %d\n", g_stage); return 1; } |
| 131 | int rc = cx_open(&s, jr, RX_CORTEX_SUBJECTS, 0); |
| 132 | CHECK(rc == CX_OK, "journal opened (rc %d)", rc); |
| 133 | rc = rx_cortex_attach(&r->w, &s, 1); |
| 134 | CHECK(rc == RX_OK, "recorder attached to the living world (rc %d)", rc); |
| 135 | rc = run(r, POSITIVE); |
| 136 | CHECK(rc == 0, "R13 POSITIVE episode ran to promotion and in force (rc %d)", rc); |
| 137 | if (rc != 0 || g_fail) { printf("R13_CORTEX_EPISODE FAIL %d pass %d fail %d\n", 1, g_pass, g_fail); stop(r); return 1; } |
| 138 | rx_world_wait_quiescent(&r->w, 5000); |
| 139 | uint64_t recs = 0, errs = 0; |
| 140 | rx_cortex_status(&r->w, &recs, &errs); |
| 141 | CHECK(recs > 0 && errs == 0, "recorder wrote %llu records, %llu errors", U(recs), U(errs)); |
| 142 | |
| 143 | /* 1. a Cortex record for each crumb of the episode, and the chain */ |
| 144 | RxObjRef ref[N_STEP] = {r->aien.o.placement, r->aien.o.belief, r->aien.o.prediction, |
| 145 | r->aien.o.hypothesis, r->aien.o.plan, r->omega.o.search, r->living.o.input, |
| 146 | r->living.o.output, r->living.o.evidence, r->aien.o.experiment_belief, |
| 147 | r->living.o.candidate, r->living.o.promotion}; |
| 148 | uint64_t id[N_STEP]; |
| 149 | for (int i = 0; i < N_STEP; i++) { |
| 150 | id[i] = rec_of(r, ref[i]); |
| 151 | const CxObject *o = id[i] ? cx_get(&s, id[i]) : NULL; |
| 152 | CHECK(o != NULL, "%-12s -> cortex record %llu kind 0x%x", g_step[i], U(id[i]), o ? o->kind : 0); |
| 153 | } |
| 154 | /* Ground truth of the POSITIVE run (see the report): two causal chains meet in the same world. |
| 155 | * A (action): goal -> assessment -> plan -> experiment -> evidence -> experiment belief -> candidate -> promotion. |
| 156 | * B (sense): observation -> belief -> prediction (failed) -> hypothesis. |
| 157 | * Each link below is the nearest cx_provenance ancestor of the one before. */ |
| 158 | uint64_t as = rec_of(r, r->aien.o.assessment); |
| 159 | uint64_t ca[6], nca = 0; |
| 160 | uint64_t cur = id[8]; |
| 161 | RxObjRef chainA[] = {r->living.o.output, r->aien.o.plan, r->aien.o.assessment, r->aien.o.goal}; |
| 162 | char line[400]; int ln = snprintf(line, sizeof line, "A: evidence %llu", U(id[8])); |
| 163 | for (unsigned i = 0; i < 4; i++) { |
| 164 | uint64_t nx = cur ? nearest(&s, cur, chainA[i].id) : 0; |
| 165 | ca[nca++] = nx; |
| 166 | ln += snprintf(line + ln, sizeof line - ln, " <- %s %llu", i == 0 ? "experiment" : i == 1 ? "plan" : i == 2 ? "assessment" : "goal", U(nx)); |
| 167 | if (nx) cur = nx; |
| 168 | } |
| 169 | printf("CHAIN %s\n", line); |
| 170 | CHECK(ca[0] && ca[1] && ca[2] && ca[3] && cx_get(&s, ca[3])->kind == CX_K_WORK_ACCEPTED && as, |
| 171 | "chain A: evidence <- experiment <- plan <- assessment <- goal (outside input) is in the Cortex provenance"); |
| 172 | CxFilter fc = {CX_EXECUTION, CX_K_EXEC_COMMIT, 0, 1}; |
| 173 | CxRecord hr; |
| 174 | uint64_t hyp = cx_recall(&s, (uint64_t)r->aien.o.hypothesis.id + 1u, 0, UINT64_MAX, &fc, &hr, 1) ? hr.hdr.id : 0; |
| 175 | uint64_t pr = hyp ? nearest(&s, hyp, r->aien.o.prediction.id) : 0; |
| 176 | uint64_t be = pr ? nearest(&s, pr, r->aien.o.belief.id) : 0; |
| 177 | uint64_t ob = 0; |
| 178 | if (be) { uint64_t p[4096]; uint32_t n = cx_provenance(&s, be, p, 4096); |
| 179 | for (uint32_t i = 0; i < n && !ob; i++) if (cx_get(&s, p[i])->kind == CX_K_WORK_ACCEPTED) ob = p[i]; } |
| 180 | printf("CHAIN B: hypothesis %llu <- prediction %llu <- belief %llu <- observation %llu\n", U(hyp), U(pr), U(be), U(ob)); |
| 181 | CHECK(hyp && pr && be && ob, "chain B: hypothesis <- prediction <- belief <- observation (outside input) is in the Cortex provenance"); |
| 182 | CHECK(!ob || cx_get(&s, ob)->subject != 0, "observation record is an outside input about object slot %llu", U(ob ? cx_get(&s, ob)->subject - 1 : 0)); |
| 183 | { /* the lesson side: later records descend from evidence */ |
| 184 | uint64_t p[4096]; uint32_t n = cx_provenance(&s, id[10], p, 4096), has = 0, has_eb = 0; |
| 185 | for (uint32_t i = 0; i < n; i++) { has |= p[i] == id[8]; has_eb |= p[i] == id[9]; } |
| 186 | printf("candidate %llu has evidence %llu in its provenance: %u; experiment belief %llu: %u\n", U(id[10]), U(id[8]), has, U(id[9]), has_eb); |
| 187 | uint64_t q[4096]; uint32_t m = cx_provenance(&s, id[11], q, 4096), hc = 0; |
| 188 | for (uint32_t i = 0; i < m; i++) hc |= q[i] == id[10]; |
| 189 | CHECK(hc, "promotion %llu descends from candidate %llu", U(id[11]), U(id[10])); |
| 190 | } |
| 191 | CHECK(cx_verify_chain(&s) == CX_OK, "cx_verify_chain passes (%llu records)", U(s.n)); |
| 192 | |
| 193 | /* 2. lesson candidate and digest-bound evidence */ |
| 194 | RxObject ev; |
| 195 | rx_world_read(&r->w, r->living.o.evidence, &ev); |
| 196 | uint8_t dg[32]; |
| 197 | rx_living_object_digest(ev.type, ev.field, dg); |
| 198 | CHECK(memcmp(dg, ev.digest, 32) == 0, "rx_living_object_digest equals the World's digest of the evidence object"); |
| 199 | uint64_t sub = (uint64_t)r->living.o.evidence.id + 1u; |
| 200 | uint64_t dw[4]; words(dg, dw); |
| 201 | /* The lesson records are written by the body (generation.prepare and |
| 202 | * generation.promote); the test only finds them. */ |
| 203 | uint64_t ev_ref = 0, cand = 0, prom = 0, n_ref = 0, n_cand = 0, n_prom = 0; |
| 204 | for (uint64_t i = 1; i <= s.n; i++) { |
| 205 | const CxObject *o = cx_get(&s, i); |
| 206 | if (o->subject != sub) continue; |
| 207 | if (o->kind == CX_K_EVIDENCE_REF) { ev_ref = i; n_ref++; } |
| 208 | if (o->kind == CX_K_CANDIDATE) { cand = i; n_cand++; } |
| 209 | if (o->kind == CX_K_PROMOTION) { prom = i; n_prom++; } |
| 210 | } |
| 211 | CHECK(n_ref == 1 && n_cand == 1 && n_prom == 1, "body wrote one evidence-ref (%llu), one lesson candidate (%llu), one promotion (%llu)", |
| 212 | U(ev_ref), U(cand), U(prom)); |
| 213 | if (n_ref != 1 || n_cand != 1 || n_prom != 1) { printf("R13_CORTEX_EPISODE FAIL %d pass %d fail %d\n", 1, g_pass, g_fail); stop(r); return 1; } |
| 214 | const CxObject *co = cx_get(&s, cand), *eo = cx_get(&s, ev_ref), *po = cx_get(&s, prom); |
| 215 | CHECK(co->links[0] == id[9] && co->links[1] == ev_ref && co->links[2] == id[8], |
| 216 | "lesson links {experiment belief %llu, evidence ref %llu, evidence %llu}", U(co->links[0]), U(co->links[1]), U(co->links[2])); |
| 217 | CHECK(eo->links[0] == id[8] && eo->links[1] == id[9] && (eo->protect & CX_PROT_VERIFY_EVIDENCE) && eo->tag == co->tag, |
| 218 | "evidence ref links {evidence %llu, experiment belief %llu}, protected, same epoch tag %llu", U(eo->links[0]), U(eo->links[1]), U(eo->tag)); |
| 219 | const uint64_t *cp = cx_payload(&s, co), *ep = cx_payload(&s, eo); |
| 220 | CHECK(eo->n == RX_LIVING_EV_WORDS && memcmp(ep, dw, sizeof dw) == 0 && ep[RX_LIVING_EV_CRUMB] == rx_world_explain(&r->w, r->living.o.evidence, 0), |
| 221 | "evidence ref holds the World digest of the evidence object and its crumb"); |
| 222 | CHECK(co->n == RX_LIVING_LESSON_WORDS && cp[1] == ev.field[0] && |
| 223 | cp[RX_LIVING_LESSON_KEY] == rx_aien_key(field(r, r->aien.o.plan, 3), field(r, r->aien.o.plan, 2)) && |
| 224 | cp[RX_LIVING_LESSON_REGIME] == field(r, r->aien.o.plan, 2), |
| 225 | "lesson payload: epoch %llu, regime %llx, condition key %llx", U(cp[1]), U(cp[RX_LIVING_LESSON_REGIME]), U(cp[RX_LIVING_LESSON_KEY])); |
| 226 | CHECK(po->links[0] == cand && po->links[1] == ev_ref && (po->protect & CX_PROT_VERIFY_EVIDENCE), |
| 227 | "body promotion %llu links {candidate %llu, evidence ref %llu}", U(prom), U(po->links[0]), U(po->links[1])); |
| 228 | uint8_t other[32]; |
| 229 | RxObject goal; rx_world_read(&r->w, r->aien.o.goal, &goal); |
| 230 | rx_living_object_digest(goal.type, goal.field, other); |
| 231 | uint64_t out = 0, n_before = s.n; |
| 232 | int rc2 = rx_living_lesson_promote(&r->w, &s, cand, 999999, dg, &out); |
| 233 | CHECK(rc2 == RX_ERR_NOT_FOUND, "promotion with a missing evidence record refused (rc %d)", rc2); |
| 234 | rc2 = rx_living_lesson_promote(&r->w, &s, cand, ev_ref, other, &out); |
| 235 | CHECK(rc2 == RX_ERR_ARG, "promotion against a different object digest (wrong-digest evidence) refused (rc %d)", rc2); |
| 236 | CHECK(s.n == n_before, "no record was written by the refusals"); |
| 237 | CHECK(cx_verify_chain(&s) == CX_OK, "cx_verify_chain passes after promotion (%llu records)", U(s.n)); |
| 238 | static Prot before, after; |
| 239 | snap(&s, &before); |
| 240 | uint32_t n_auth = 0, n_commit = 0; |
| 241 | for (uint32_t i = 0; i < before.n; i++) { n_auth += !!(before.o[i].protect & CX_PROT_AUTHORITY); n_commit += !!(before.o[i].protect & CX_PROT_COMMIT_RECEIPT); } |
| 242 | printf("protected records: %u (authority %u, commit receipt %u)\n", before.n, n_auth, n_commit); |
| 243 | CHECK(before.n > 0 && n_auth > 0, "authority and receipt evidence exist to compare"); |
| 244 | |
| 245 | stop(r); |
| 246 | free(r); |
| 247 | uint64_t n_records = s.n; |
| 248 | cx_close(&s); |
| 249 | CHECK(g_first_failure[0] == 0, "no World failure noted while stopping"); |
| 250 | |
| 251 | /* tampering: flip one byte of a journal copy, inside the evidence-ref record */ |
| 252 | struct stat st; stat(jr, &st); |
| 253 | int refused = 0; |
| 254 | for (long off = st.st_size - 400; off < st.st_size - 300 && !refused; off += 7) { |
| 255 | corrupt_copy(jr, jr2, off); |
| 256 | CxStore t; memset(&t, 0, sizeof t); |
| 257 | int trc = cx_open(&t, jr2, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); |
| 258 | if (trc != CX_OK) refused = trc; else cx_close(&t); |
| 259 | } |
| 260 | CHECK(refused != 0, "journal with one flipped byte is refused at open (rc %d)", refused); |
| 261 | |
| 262 | /* 3. restart: a new body opens the same journal */ |
| 263 | rc = cx_open(&s2, jr, RX_CORTEX_SUBJECTS, 0); |
| 264 | CHECK(rc == CX_OK && s2.n == n_records, "journal reopened: %llu records (was %llu)", U(s2.n), U(n_records)); |
| 265 | CHECK(cx_verify_chain(&s2) == CX_OK, "cx_verify_chain passes after restart"); |
| 266 | CxRecord a[64], b[64]; |
| 267 | CxFilter f = {0, 0, 0, 1}; |
| 268 | uint32_t na = cx_recall(&s2, sub, 0, UINT64_MAX, &f, a, 64), nb = cx_recall(&s2, sub, 0, UINT64_MAX, &f, b, 64); |
| 269 | int same = na == nb && na > 0; |
| 270 | for (uint32_t i = 0; same && i < na; i++) same = a[i].hdr.id == b[i].hdr.id && a[i].verified; |
| 271 | for (uint32_t i = 1; same && i < na; i++) same = a[i].hdr.id > a[i - 1].hdr.id; |
| 272 | CHECK(same, "cx_recall by the evidence subject returns %u records, ordered and repeatable", na); |
| 273 | CHECK(cx_get(&s2, cand)->kind == CX_K_CANDIDATE && cx_get(&s2, prom)->kind == CX_K_PROMOTION && cx_get(&s2, prom)->links[0] == cand, |
| 274 | "after restart lesson %llu is a promoted fact (promotion %llu)", U(cand), U(prom)); |
| 275 | CHECK(rx_living_lesson_evidence_ok(&s2, ev_ref, dg) == RX_OK && rx_living_lesson_evidence_ok(&s2, ev_ref, other) == RX_ERR_ARG, |
| 276 | "after restart the evidence gate still accepts the match and refuses the wrong digest"); |
| 277 | /* tampered evidence record: refused, chain fails; restored value passes again */ |
| 278 | uint64_t keep = cx_payload(&s2, cx_get(&s2, ev_ref))[0]; |
| 279 | cx_tamper(&s2, ev_ref, 0, keep ^ 1u); |
| 280 | CHECK(cx_verify_chain(&s2) != CX_OK && rx_living_lesson_evidence_ok(&s2, ev_ref, dg) == RX_ERR_IDENTITY, |
| 281 | "tampered evidence record: cx_verify_chain fails and the gate refuses"); |
| 282 | cx_tamper(&s2, ev_ref, 0, keep); |
| 283 | CHECK(cx_verify_chain(&s2) == CX_OK, "restored record verifies again"); |
| 284 | /* what is not in Cortex */ |
| 285 | uint64_t kinds[16] = {0}; uint32_t kid[16] = {0}; int nk = 0; |
| 286 | for (uint64_t i = 1; i <= s2.n; i++) { |
| 287 | uint32_t k = cx_get(&s2, i)->kind; int j = 0; |
| 288 | while (j < nk && kid[j] != k) j++; |
| 289 | if (j == nk && nk < 16) kid[nk++] = k; |
| 290 | if (j < 16) kinds[j]++; |
| 291 | } |
| 292 | printf("kinds in Cortex:"); |
| 293 | for (int j = 0; j < nk; j++) printf(" 0x%x x%llu", kid[j], U(kinds[j])); |
| 294 | printf("\nnot in Cortex: live World objects and their field versions, reaction state, capabilities and the crumb log itself (only each crumb's result is kept)\n"); |
| 295 | int ephemeral = 1; |
| 296 | for (int j = 0; j < nk; j++) ephemeral &= (kid[j] >= CX_K_BASE); |
| 297 | CHECK(ephemeral, "every record is a canonical kind; there is no kind for an object table or reaction state"); |
| 298 | |
| 299 | /* 4. protected evidence byte-identical */ |
| 300 | snap(&s2, &after); |
| 301 | int ident = after.n == before.n; |
| 302 | for (uint32_t i = 0; ident && i < before.n; i++) |
| 303 | ident = before.id[i] == after.id[i] && memcmp(&before.o[i], &after.o[i], sizeof(CxObject)) == 0 && |
| 304 | memcmp(before.pl[i], after.pl[i], before.o[i].n * sizeof(uint64_t)) == 0; |
| 305 | CHECK(ident, "%u protected records (authority, receipts, verification evidence, promotion) byte-identical after restart", after.n); |
| 306 | |
| 307 | /* ---- C2: related-problem retrieval after a restart ---- */ |
| 308 | uint64_t want = rx_aien_key(CLASS_X925, rx_omega_regime(M, N)); |
| 309 | uint64_t k_prom[RX_AIEN_MEMORY_SLOTS]; |
| 310 | uint32_t nk_prom = rx_living_explored_keys(&s2, sub, k_prom, RX_AIEN_MEMORY_SLOTS); |
| 311 | CHECK(nk_prom == 1 && k_prom[0] == want, "Cortex holds %u promoted condition (key %llx; X925 at this regime = %llx)", nk_prom, U(k_prom[0]), U(want)); |
| 312 | /* the same journal with the promotion left out: the lesson is only a candidate */ |
| 313 | static CxStore sn; |
| 314 | int cp_ok = cx_init(&sn, RX_CORTEX_SUBJECTS) == CX_OK; |
| 315 | for (uint64_t i = 1; cp_ok && i < prom; i++) { |
| 316 | const CxObject *q = cx_get(&s2, i); |
| 317 | CxHeader h = {q->cls, q->kind, q->subject, q->t, q->generation, q->branch, q->protect, q->tag, {q->links[0], q->links[1], q->links[2], q->links[3]}}; |
| 318 | cp_ok = cx_append(&sn, &h, cx_payload(&s2, q), q->n, NULL) == CX_OK; |
| 319 | } |
| 320 | uint64_t k_open[RX_AIEN_MEMORY_SLOTS]; |
| 321 | CHECK(cp_ok && sn.n == prom - 1 && cx_get(&sn, cand)->kind == CX_K_CANDIDATE && rx_living_explored_keys(&sn, sub, k_open, RX_AIEN_MEMORY_SLOTS) == 0, |
| 322 | "journal without the promotion: lesson %llu is a candidate and yields no memory", U(cand)); |
| 323 | Outcome oe, on, ou; |
| 324 | int e1 = episode(&s2, 1, &oe), e2 = episode(&s2, 0, &on), e3 = episode(&sn, 1, &ou); |
| 325 | printf("C2 explored-not-replanned: seeded %u key, assessment %s, plan seq %llu, memory count %llu\n", oe.seeded, |
| 326 | oe.status == RX_AIEN_GOAL_UNMET_EXPLORED ? "UNMET_EXPLORED" : "other", U(oe.plan), U(oe.mem_count)); |
| 327 | printf("C2 new-condition-planned: seeded %u key, core class A725 (not seeded), assessment status %llu, plan seq %llu, memory count %llu\n", on.seeded, U(on.status), U(on.plan), U(on.mem_count)); |
| 328 | printf("C2 unpromoted-replanned: seeded %u keys, X925 condition, plan seq %llu for class %llx, memory count %llu\n", ou.seeded, U(ou.plan), U(ou.mem_key), U(ou.mem_count)); |
| 329 | CHECK(e1 == 0 && oe.seeded == 1 && oe.status == RX_AIEN_GOAL_UNMET_EXPLORED && oe.plan == 0 && oe.mem_count == 1, |
| 330 | "second life, same condition: explored, NOT re-planned (rc %d)", e1); |
| 331 | CHECK(e2 == 0 && on.seeded == 1 && on.plan != 0 && on.mem_count == 2 && on.mem_key == CLASS_A725, |
| 332 | "second life, new core class: still gets a plan (rc %d)", e2); |
| 333 | CHECK(e3 == 0 && ou.seeded == 0 && ou.plan != 0 && ou.mem_key == CLASS_X925, |
| 334 | "second life, lesson never promoted: the condition IS re-explored (rc %d)", e3); |
| 335 | cx_free(&sn); |
| 336 | cx_close(&s2); |
| 337 | free(g_seen); free(g_stack); |
| 338 | printf("R13_CORTEX_EPISODE %s: pass %d fail %d\n", g_fail ? "FAIL" : "PASS", g_pass, g_fail); |
| 339 | return g_fail ? 1 : 0; |
| 340 | } |