diff --git a/.crumb b/.crumb index 1c1277f31..f2fcace88 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "7e444b7d72a7c021d5c17c48d406394c36fef94f7dd91e38673e908ca0f87450", - "last_commit": "c70f820", + "digest": "212dc7f6cab19c56362ed1937d03a7f0344b84f360083cb139e88509cfd056d2", + "last_commit": "84107ae", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "9454b0ab0f8471bc801cc980698c86857924a166b61d84473fa1fbaa0a69384d", - "last_commit": "c70f820", + "digest": "14a81e1167174309d45775dd87378918c8819531b8c91366629711d8e6f8beb9", + "last_commit": "84107ae", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:4089d87be11619ce575297b15d89590a788766a7e09860dc594ef440146f4a9d", - "compiled_at": "2026-10-11T14:54:33Z", + "children_root": "sha256:a93ce547ef751a07ac0efedd86dc359a325c737dacf03355ff203693fea6cd8c", + "compiled_at": "2026-10-11T15:14:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:acb4933794fb6e1cbcb8fd0b334abed5e519dba4e70fc4db8dfe7867cb73bcaa", + "digest": "sha256:7f23613e9f548e3ca8bd980feb47c370b8c816f540b8ffbf1a175ee8bed1f184", "evidence_files": 7243, "evidence_root": "sha256:7283d2aa790caa9b49833f970995b6aa5e18f680037a07bf35f1dcd0b6f4ad47", "files": 9661, - "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", - "source_tree": "sha256:7d1703f519af364d288500ab574867a5dc35db06202fbf5c3fa9c2986d3d50db" + "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", + "source_tree": "sha256:217c1621b69a354180a35d6b963f61fd6ce4e48a915cbd9bf0f0009e9b6d7d55" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index de3334522..17c56f3f8 100644 --- a/src/.crumb +++ b/src/.crumb @@ -139,8 +139,8 @@ "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { - "digest": "44927d9c42231abe027de1821d96edecb702fa0e1a791843bf3b9e0142a31bbf", - "last_commit": "c70f820", + "digest": "9ca48dec58f57f6a8b9595fcf03318b766b5f0cf6fe2b7e286428cf2a4a5b98e", + "last_commit": "84107ae", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -175,15 +175,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:3294f4412d2e2eacc3eaeebd4cd09fc5729c47332ff41c7441fb457e7699ed09", - "compiled_at": "2026-10-11T14:54:33Z", + "children_root": "sha256:807af43ddb6a096e8b2e6f70e1a4d31a4bf9969ae383c2b7b8ccb247fee37252", + "compiled_at": "2026-10-11T15:14:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:7e444b7d72a7c021d5c17c48d406394c36fef94f7dd91e38673e908ca0f87450", + "digest": "sha256:212dc7f6cab19c56362ed1937d03a7f0344b84f360083cb139e88509cfd056d2", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 449, - "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", - "source_tree": "sha256:05d15fbbabc4c2385696f8d31813a0d71810c961cb6f511e0e25cdccf7ed5f5d" + "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", + "source_tree": "sha256:614605e2bf551f5c33ef8e76a786f31cca7bf52c26601edd8ae52ab6eabb63e1" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index d0f8b4dd6..140282476 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T14:53:31Z", + "compiled_at": "2026-10-11T15:14:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:44927d9c42231abe027de1821d96edecb702fa0e1a791843bf3b9e0142a31bbf", + "digest": "sha256:9ca48dec58f57f6a8b9595fcf03318b766b5f0cf6fe2b7e286428cf2a4a5b98e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 63, - "generated_at_commit": "8511f5e8e192c1d58b30effe9ee70ea9e90663bc", - "source_tree": "sha256:3a3c52eb04693e295ef581a1e66a84043f95b2eb0a3f3029ec3bec7cc6970c66" + "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", + "source_tree": "sha256:04e809d33fdbefe0a5c25559db70c691f68927578eefb6077feb78932891cb59" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_aien.c b/src/runtime/rx_aien.c index 5d6bbb6e5..231bce775 100644 --- a/src/runtime/rx_aien.c +++ b/src/runtime/rx_aien.c @@ -473,3 +473,12 @@ int rx_aien_register(RxAienFaculty *f, const RxAienCaps *caps) { if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_plan)) != RX_OK) return rc; return RX_OK; } + +/* ---- restart: explored-condition memory from outside ---- */ +int rx_aien_seed_explored(RxAienFaculty *f, RxCapRef memory_write, const uint64_t *keys, uint32_t n) { + if (!f || !f->w || !keys || n == 0 || n > RX_AIEN_MEMORY_SLOTS) return RX_ERR_ARG; + RxMutation m[1 + RX_AIEN_MEMORY_SLOTS]; + m[0] = (RxMutation){f->o.memory, 0, n}; + for (uint32_t i = 0; i < n; i++) m[1 + i] = (RxMutation){f->o.memory, 1 + i, keys[i]}; + return rx_world_publish_external(f->w, memory_write, m, n + 1) > 0 ? RX_OK : RX_ERR_AUTHORITY; +} diff --git a/src/runtime/rx_aien.h b/src/runtime/rx_aien.h index a710f044e..1724bc680 100644 --- a/src/runtime/rx_aien.h +++ b/src/runtime/rx_aien.h @@ -156,6 +156,13 @@ int rx_aien_register(RxAienFaculty *f, const RxAienCaps *caps); int rx_aien_register_experiment(RxAienFaculty *f, RxObjRef evidence, RxCapRef evidence_read, RxCapRef belief_write); +/* Restart. Publishes `n` (1..RX_AIEN_MEMORY_SLOTS) explored condition keys into + * the memory object as an outside publication (`memory_write` is WRITE on + * RX_AIEN_RES_MEMORY for the outside subject), before any goal. The caller + * decides what may be remembered: only conditions with a promoted lesson + * (rx_living_explored_keys), never a candidate. */ +int rx_aien_seed_explored(RxAienFaculty *f, RxCapRef memory_write, const uint64_t *keys, uint32_t n); + static inline uint64_t rx_aien_regime(uint64_t M, uint64_t N) { return (M << 32) | N; } /* One explored condition: core class and regime. */ diff --git a/src/runtime/rx_cortex_record.c b/src/runtime/rx_cortex_record.c index bb6fd3e87..ce10b9bf9 100644 --- a/src/runtime/rx_cortex_record.c +++ b/src/runtime/rx_cortex_record.c @@ -356,3 +356,31 @@ uint64_t rx_cortex_next_t_in(RxWorld *w, const CxStore *s) { } uint64_t rx_cortex_next_t(RxWorld *w) { return rx_cortex_next_t_in(w, NULL); } + +/* The store of the default link (or the one named), NULL if none. The pointer + * is stable while attached; reading through it needs the writer's quiet or + * rx_cortex_promote_checked_in. */ +CxStore *rx_cortex_store_in(RxWorld *w, const CxStore *s) { + if (!w) return NULL; + pthread_mutex_lock(&w->mu); + CortexLink *l = link_of(w, s); + CxStore *out = l ? l->s : NULL; + pthread_mutex_unlock(&w->mu); + return out; +} + +/* The gate reads the store and cx_promote appends to it under one hold of the + * world mutex, so no crumb record lands between check and promotion. */ +int rx_cortex_promote_checked_in(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, + int (*gate)(const CxStore *, uint64_t, const void *), + const void *arg, uint64_t *out_id) { + if (!w || !gate) return RX_ERR_ARG; + pthread_mutex_lock(&w->mu); + CortexLink *l = link_of(w, s); + int rc = !l ? RX_ERR_NOT_FOUND : gate(l->s, evidence, arg); + if (l && rc == RX_OK) + rc = cx_promote(l->s, l->token, candidate, evidence, l->s->n + 1, out_id) == CX_OK + ? RX_OK : RX_ERR_ARG; + pthread_mutex_unlock(&w->mu); + return rc; +} diff --git a/src/runtime/rx_cortex_record.h b/src/runtime/rx_cortex_record.h index f9551e496..fc3cead26 100644 --- a/src/runtime/rx_cortex_record.h +++ b/src/runtime/rx_cortex_record.h @@ -91,5 +91,12 @@ int rx_cortex_append_in(RxWorld *w, CxStore *s, const CxHeader *h, const uint64_ int rx_cortex_promote_in(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, uint64_t *out_id); uint64_t rx_cortex_next_t_in(RxWorld *w, const CxStore *s); +/* The store a link writes (NULL = default link; NULL result = none attached). */ +CxStore *rx_cortex_store_in(RxWorld *w, const CxStore *s); +/* cx_promote preceded by `gate(store, evidence, arg)` under one hold of the + * world mutex; the gate's nonzero result is returned and nothing is written. */ +int rx_cortex_promote_checked_in(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, + int (*gate)(const CxStore *, uint64_t, const void *), + const void *arg, uint64_t *out_id); #endif /* RX_CORTEX_RECORD_H */ diff --git a/src/runtime/rx_living.c b/src/runtime/rx_living.c index 5ee6cb535..6533645a2 100644 --- a/src/runtime/rx_living.c +++ b/src/runtime/rx_living.c @@ -190,6 +190,51 @@ static int halted_wait(RxWorld *w, uint32_t reaction) { return RX_FN_DEFER; } +static void digest_words(const uint8_t d[32], uint64_t w[4]) { + 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); + } +} + +/* G1/N6: with a recorder attached, the body journals its own lesson for the + * epoch: the evidence reference (bound to the evidence object's digest) and + * the CX_K_CANDIDATE lesson, linked to the evidence and experiment-belief + * crumb records. Without a recorder nothing happens. */ +static void record_lesson(RxLiving *l, uint64_t epoch, const RxCrumb *k_evi, const RxCrumb *k_bel, + const RxSnapshotDep *e, const RxLivingEvidence *ev, + const SemanticId *realization, uint64_t cls, uint64_t regime) { + RxWorld *w = l->world; + CxStore *st = rx_cortex_store_in(w, NULL); + if (!st || l->lesson.epoch == epoch) return; + uint64_t rec_evi = rx_cortex_record_in(w, st, k_evi->id), rec_bel = rx_cortex_record_in(w, st, k_bel->id); + uint8_t dg[32]; + rx_living_object_digest(RX_OT_LIVING_EVIDENCE, e->field, dg); + uint64_t evp[RX_LIVING_EV_WORDS]; + digest_words(dg, evp); + evp[RX_LIVING_EV_CRUMB] = k_evi->id; + CxHeader h; + memset(&h, 0, sizeof h); + h.cls = CX_EVIDENCE; h.kind = CX_K_EVIDENCE_REF; h.protect = CX_PROT_VERIFY_EVIDENCE; + h.subject = (uint64_t)l->o.evidence.id + 1u; h.tag = epoch; + h.links[0] = rec_evi; h.links[1] = rec_bel; + h.t = rx_cortex_next_t_in(w, st); + uint64_t ref = 0, cand = 0; + if (rx_cortex_append_in(w, st, &h, evp, RX_LIVING_EV_WORDS, &ref) != RX_OK) return; + uint64_t lp[RX_LIVING_LESSON_WORDS]; + _Static_assert(sizeof(RxLivingEvidence) == 7 * sizeof(uint64_t), "lesson layout"); + memcpy(lp, ev, sizeof *ev); + digest_words(realization->bytes, lp + RX_LIVING_LESSON_REALIZATION); + lp[RX_LIVING_LESSON_REGIME] = regime; + lp[RX_LIVING_LESSON_KEY] = rx_aien_key(cls, regime); + h.cls = CX_CLAIM; h.kind = CX_K_CANDIDATE; h.protect = 0; + h.links[0] = rec_bel; h.links[1] = ref; h.links[2] = rec_evi; + h.t = rx_cortex_next_t_in(w, st); + if (rx_cortex_append_in(w, st, &h, lp, RX_LIVING_LESSON_WORDS, &cand) != RX_OK) return; + l->lesson = (RxLivingLesson){epoch, cand, ref, 0, st, {0}}; + memcpy(l->lesson.digest, dg, 32); +} + static int fn_candidate(RxCtx *c) { RxLiving *l = c->user; RxWorld *w = l->world; @@ -367,6 +412,7 @@ static int fn_candidate(RxCtx *c) { l->proposed_epoch = epoch; l->candidate_id = id; } + record_lesson(l, epoch, k_evi, k_bel, e, &ev, &real.id, p->field[3], s->field[6]); put(c, l->o.candidate, 0, id); put(c, l->o.candidate, 1, epoch); for (uint32_t j = 0; j < 4; j++) put(c, l->o.candidate, 2 + j, s->field[1 + j]); @@ -482,6 +528,11 @@ static int fn_promote(RxCtx *c) { promote_inline(p, id, &rc, &active, &ns); } p->result = rc; + if (rc == RX_GEN_OK && p->lesson && p->lesson->store && p->lesson->epoch == cand->field[1] && + !p->lesson->promotion) { + RxLivingLesson *ls = p->lesson; /* G1/N6: the matching lesson becomes a fact */ + (void)rx_living_lesson_promote(p->world, ls->store, ls->candidate, ls->evidence_ref, ls->digest, &ls->promotion); + } put(c, p->promotion, 0, id); put(c, p->promotion, 1, (uint64_t)(int64_t)rc); put(c, p->promotion, 2, ns); @@ -535,6 +586,7 @@ int rx_living_register(RxLiving *l, const RxLivingCaps *caps, promoter->inforce.id != l->o.inforce.id) return RX_ERR_ARG; const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE; l->caps = *caps; + promoter->lesson = &l->lesson; int rc; RxReactionDesc d; /* A resident world puts the store's physical work on its own executor so @@ -765,9 +817,31 @@ int rx_living_lesson_evidence_ok(const CxStore *s, uint64_t evidence, const uint return RX_OK; } +static int evidence_gate(const CxStore *s, uint64_t evidence, const void *digest) { + return rx_living_lesson_evidence_ok(s, evidence, digest); +} + int rx_living_lesson_promote(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, const uint8_t object_digest[32], uint64_t *out_id) { - int rc = rx_living_lesson_evidence_ok(s, evidence, object_digest); - if (rc != RX_OK) return rc; - return rx_cortex_promote_in(w, s, candidate, evidence, out_id); + if (!object_digest) return RX_ERR_ARG; + return rx_cortex_promote_checked_in(w, s, candidate, evidence, evidence_gate, object_digest, out_id); +} + +uint32_t rx_living_explored_keys(CxStore *s, uint64_t subject, uint64_t *keys, uint32_t max) { + static CxRecord recs[64]; + CxFilter f = {CX_EVIDENCE, CX_K_PROMOTION, 0, 1}; + uint32_t n = 0, got = s && keys ? cx_recall(s, subject, 0, UINT64_MAX, &f, recs, 64) : 0; + for (uint32_t i = 0; i < got && n < max; i++) { + CxRecord cand, ref; + if (!recs[i].verified || recs[i].hdr.n < 8 || + cx_recall_id(s, recs[i].hdr.links[0], &cand) != CX_OK || !cand.verified || + cx_recall_id(s, recs[i].hdr.links[1], &ref) != CX_OK || !ref.verified || + cand.hdr.kind != CX_K_CANDIDATE || cand.hdr.n < RX_LIVING_LESSON_WORDS || + ref.hdr.kind != CX_K_EVIDENCE_REF) continue; + uint64_t k = cand.payload[RX_LIVING_LESSON_KEY]; + uint32_t j = 0; + while (j < n && keys[j] != k) j++; + if (j == n) keys[n++] = k; + } + return n; } diff --git a/src/runtime/rx_living.h b/src/runtime/rx_living.h index 4d7fbe1e7..9711de670 100644 --- a/src/runtime/rx_living.h +++ b/src/runtime/rx_living.h @@ -105,6 +105,15 @@ typedef struct { uint64_t selected_ps, reference_ps, trials; } RxLivingEvidence; +/* The lesson the body wrote into Cortex for one epoch (G1/N6); zero when no + * recorder was attached. Written by generation.prepare, read by + * generation.promote. */ +typedef struct { + uint64_t epoch, candidate, evidence_ref, promotion; /* promotion: 0 until recorded */ + CxStore *store; + uint8_t digest[32]; /* rx_living_object_digest of the evidence object */ +} RxLivingLesson; + typedef struct { /* living.experiment.prepare (RX_LIVING_SUBJ) */ RxCapRef plan_read, search_read, input_write; @@ -126,6 +135,7 @@ typedef struct { RxGenStore *store; const struct AienosCapView *authority; RxObjRef candidate, promotion, inforce; + RxLivingLesson *lesson; /* the proposer's lesson record (set by rx_living_register) */ RxCapRef candidate_read, promotion_write, inforce_write, promotion_authority; uint32_t reaction; int result; @@ -147,6 +157,7 @@ typedef struct { * made it is invalidated and runs again for the same epoch, it reuses * the proposal instead of making a second one. */ uint64_t proposed_epoch, candidate_id; + RxLivingLesson lesson; uint64_t refusals; /* observability: why nothing was proposed */ int last_refusal; /* R16 C5: credentials of the living subjects other than the promotion @@ -211,5 +222,17 @@ int rx_living_lesson_evidence_ok(const CxStore *s, uint64_t evidence, const uint /* The gate above, then rx_cortex_promote_in on the attached writer of `s`. */ int rx_living_lesson_promote(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence, const uint8_t object_digest[32], uint64_t *out_id); +/* Layout of the CX_K_CANDIDATE lesson generation.prepare writes (words): + * the RxLivingEvidence, the realization id (4), the regime, the condition key + * rx_aien_key(core class, regime). Its subject is the evidence object slot + 1, + * its tag the epoch; links {experiment belief, evidence ref, evidence}. */ +enum { RX_LIVING_LESSON_REALIZATION = 7, RX_LIVING_LESSON_REGIME = 11, RX_LIVING_LESSON_KEY = 12, + RX_LIVING_LESSON_WORDS = 13 }; +/* Condition keys of the PROMOTED lessons about `subject` (typed recall of + * CX_K_PROMOTION; each promotion, its candidate and its evidence ref must + * verify and agree). An un-promoted candidate contributes nothing. Up to + * `max` distinct keys, oldest first; returns the number. Call when no writer + * is appending (restart, or before the body runs). */ +uint32_t rx_living_explored_keys(CxStore *s, uint64_t subject, uint64_t *keys, uint32_t max); #endif diff --git a/tests/.crumb b/tests/.crumb index 093d9e259..c9ff64417 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -259,8 +259,8 @@ "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { - "digest": "4f0e66038cd243c0ac75a088f2ea7e7b1038011193a0595d4a18ef202a780c6e", - "last_commit": "c70f820", + "digest": "aa03a550299d9f3effb477f19a4c177b84b0679850d019430ba362e185b4317a", + "last_commit": "84107ae", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -289,15 +289,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:d30b17f6f4909b4eb164a94ae45dbf0052a56258e2de0cce2e6b4d6a737ec711", - "compiled_at": "2026-10-11T14:54:33Z", + "children_root": "sha256:b15562174dfa4bc07053cf4ef71f8d829db85edf05927be530ff0c1d972d1674", + "compiled_at": "2026-10-11T15:14:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:9454b0ab0f8471bc801cc980698c86857924a166b61d84473fa1fbaa0a69384d", + "digest": "sha256:14a81e1167174309d45775dd87378918c8819531b8c91366629711d8e6f8beb9", "evidence_files": 14, "evidence_root": "sha256:080bc99a4a66250db3cda56908658fd3f09c53170ca32c85caa2d11ef3126f1d", "files": 957, - "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", - "source_tree": "sha256:00cbf54395becbc7c951b00dec0c59af96540d728398893e36a6ac3e804ddd6e" + "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", + "source_tree": "sha256:bef70d44f9229f9132b51fc346007b32ed3dee9d80b07a52b9d04d97410654a0" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 217f47fd1..4fac95dad 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T14:53:31Z", + "compiled_at": "2026-10-11T15:14:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:4f0e66038cd243c0ac75a088f2ea7e7b1038011193a0595d4a18ef202a780c6e", + "digest": "sha256:aa03a550299d9f3effb477f19a4c177b84b0679850d019430ba362e185b4317a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 65, - "generated_at_commit": "8511f5e8e192c1d58b30effe9ee70ea9e90663bc", - "source_tree": "sha256:3ac2434d5b8682d6f6751694bb5a53bc9545daa1cdd060c1c4368005f03deb91" + "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", + "source_tree": "sha256:2df5ab291dd24ca44332c45a19729e5f2141155aeea05bfb6243e305be99d6eb" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_r13_cortex_episode.c b/tests/runtime/rx_r13_cortex_episode.c index bae6a2d53..8b8ad1a61 100644 --- a/tests/runtime/rx_r13_cortex_episode.c +++ b/tests/runtime/rx_r13_cortex_episode.c @@ -2,8 +2,8 @@ * * Reuses the whole R13 rig by including rx_r13_living.c (its main renamed). * Attaches the existing World->Cortex recorder (rx_cortex_record.h) to the - * living world on a journal file, runs the POSITIVE episode, appends a lesson - * (CX_K_CANDIDATE) and its evidence reference (CX_K_EVIDENCE_REF), then proves + * living world on a journal file, runs the POSITIVE episode, finds the lesson + * lesson and evidence reference the BODY wrote (generation.prepare/promote), then proves * the provenance chain, the digest-gated promotion, tamper refusal and restart. */ #define main r13_main_unused #include "rx_r13_living.c" @@ -37,17 +37,6 @@ static uint64_t nearest(const CxStore *s, uint64_t from, uint32_t slot) { 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]; return 0; } -static uint64_t append(Rig *r, CxStore *s, uint32_t cls, uint32_t kind, uint32_t prot, uint64_t subj, - uint64_t tag, const uint64_t *l, const uint64_t *p, uint32_t n) { - CxHeader h; - memset(&h, 0, sizeof h); - h.cls = cls; h.kind = kind; h.protect = prot; h.subject = subj; h.tag = tag; - h.t = rx_cortex_next_t_in(&r->w, s); - for (int i = 0; i < 4 && l; i++) h.links[i] = l[i]; - uint64_t id = 0; - if (rx_cortex_append_in(&r->w, s, &h, p, n, &id) != RX_OK) return 0; - return id; -} /* Protected records, copied for the byte-identical comparison. */ typedef struct { uint32_t n; uint64_t id[8192]; CxObject o[8192]; uint64_t pl[8192][CX_WREC_WORDS + 8]; } Prot; static void snap(CxStore *s, Prot *p) { @@ -69,6 +58,48 @@ static int corrupt_copy(const char *src, const char *dst, long off) { return 0; } + +/* ---- C2: a second life after a restart ---- + * A fresh body (new World and faculties, same R9 store) starts from the + * journal. Before AIEN plans, its explored-condition memory is seeded from + * the PROMOTED lessons in Cortex. `to_x`: the body moves to the X925 cores + * (the condition the first life explored); otherwise it stays on the A725. */ +typedef struct { uint32_t seeded; uint64_t status, plan, mem_count, mem_key; } Outcome; +static int episode(CxStore *src, int to_x, Outcome *o) { + memset(o, 0, sizeof *o); + Rig *r = calloc(1, sizeof *r); + cpu_set_t a, x; + if (!r || start(r, POSITIVE) != 0 || core_sets(&a, &x) != 0 || move_threads(&a) <= 0) return -1; + uint64_t keys[RX_AIEN_MEMORY_SLOTS]; + o->seeded = src ? rx_living_explored_keys(src, (uint64_t)r->living.o.evidence.id + 1u, keys, RX_AIEN_MEMORY_SLOTS) : 0; + if (o->seeded) { + RxCapRef mc = mint(r, EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_MEMORY, RX_RIGHT_WRITE); + if (rx_aien_seed_explored(&r->aien, mc, keys, o->seeded) != RX_OK) return -1; + } + int bad = publish_placement(r, CLASS_A725) != 0; + uint64_t limit = run_clock(r) + 60000000000ull; + while (!bad && field(r, r->omega.o.selection, 0) < 1 && run_clock(r) < limit) bad = serve(r) != 0; + while (!bad && field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED && run_clock(r) < limit) bad = serve(r) != 0; + if (bad || field(r, r->aien.o.prediction, 6) != RX_AIEN_PRED_CONFIRMED) { stop(r); free(r); return -2; } + uint64_t target = field(r, r->aien.o.prediction, 5) * TARGET_PCT / 100u; + if (start_producer(r) != 0) { stop(r); free(r); return -1; } + if (to_x && (move_threads(&x) <= 0 || publish_placement(r, CLASS_X925) != 0)) bad = 1; + if (bad || publish_goal(r, target) != 0) { stop_producer(r); stop(r); free(r); return -1; } + limit = run_clock(r) + 60000000000ull; + while (run_clock(r) < limit && !atomic_load(&r->producer_error)) { + pause_us(1000); + o->plan = field(r, r->aien.o.plan, 0); + o->status = field(r, r->aien.o.assessment, 4); + if (o->plan || o->status == RX_AIEN_GOAL_UNMET_EXPLORED) break; + } + o->mem_count = field(r, r->aien.o.memory, 0); + o->mem_key = field(r, r->aien.o.plan, 3); + stop_producer(r); + stop(r); + free(r); + return 0; +} + int main(void) { signal(SIGPIPE, SIG_IGN); setvbuf(stdout, NULL, _IONBF, 0); @@ -151,33 +182,42 @@ int main(void) { CHECK(memcmp(dg, ev.digest, 32) == 0, "rx_living_object_digest equals the World's digest of the evidence object"); uint64_t sub = (uint64_t)r->living.o.evidence.id + 1u; uint64_t dw[4]; words(dg, dw); - uint64_t evp[RX_LIVING_EV_WORDS] = {dw[0], dw[1], dw[2], dw[3], rx_world_explain(&r->w, r->living.o.evidence, 0)}; - uint64_t evl[4] = {id[8], id[9]}; - uint64_t ev_ref = append(r, &s, CX_EVIDENCE, CX_K_EVIDENCE_REF, CX_PROT_VERIFY_EVIDENCE, sub, 1, evl, evp, RX_LIVING_EV_WORDS); + /* The lesson records are written by the body (generation.prepare and + * generation.promote); the test only finds them. */ + uint64_t ev_ref = 0, cand = 0, prom = 0, n_ref = 0, n_cand = 0, n_prom = 0; + for (uint64_t i = 1; i <= s.n; i++) { + const CxObject *o = cx_get(&s, i); + if (o->subject != sub) continue; + if (o->kind == CX_K_EVIDENCE_REF) { ev_ref = i; n_ref++; } + if (o->kind == CX_K_CANDIDATE) { cand = i; n_cand++; } + if (o->kind == CX_K_PROMOTION) { prom = i; n_prom++; } + } + CHECK(n_ref == 1 && n_cand == 1 && n_prom == 1, "body wrote one evidence-ref (%llu), one lesson candidate (%llu), one promotion (%llu)", + U(ev_ref), U(cand), U(prom)); + 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; } + const CxObject *co = cx_get(&s, cand), *eo = cx_get(&s, ev_ref), *po = cx_get(&s, prom); + CHECK(co->links[0] == id[9] && co->links[1] == ev_ref && co->links[2] == id[8], + "lesson links {experiment belief %llu, evidence ref %llu, evidence %llu}", U(co->links[0]), U(co->links[1]), U(co->links[2])); + CHECK(eo->links[0] == id[8] && eo->links[1] == id[9] && (eo->protect & CX_PROT_VERIFY_EVIDENCE) && eo->tag == co->tag, + "evidence ref links {evidence %llu, experiment belief %llu}, protected, same epoch tag %llu", U(eo->links[0]), U(eo->links[1]), U(eo->tag)); + const uint64_t *cp = cx_payload(&s, co), *ep = cx_payload(&s, eo); + 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), + "evidence ref holds the World digest of the evidence object and its crumb"); + CHECK(co->n == RX_LIVING_LESSON_WORDS && cp[1] == ev.field[0] && + cp[RX_LIVING_LESSON_KEY] == rx_aien_key(field(r, r->aien.o.plan, 3), field(r, r->aien.o.plan, 2)) && + cp[RX_LIVING_LESSON_REGIME] == field(r, r->aien.o.plan, 2), + "lesson payload: epoch %llu, regime %llx, condition key %llx", U(cp[1]), U(cp[RX_LIVING_LESSON_REGIME]), U(cp[RX_LIVING_LESSON_KEY])); + CHECK(po->links[0] == cand && po->links[1] == ev_ref && (po->protect & CX_PROT_VERIFY_EVIDENCE), + "body promotion %llu links {candidate %llu, evidence ref %llu}", U(prom), U(po->links[0]), U(po->links[1])); uint8_t other[32]; RxObject goal; rx_world_read(&r->w, r->aien.o.goal, &goal); rx_living_object_digest(goal.type, goal.field, other); - uint64_t ow[4]; words(other, ow); - uint64_t wrongp[RX_LIVING_EV_WORDS] = {ow[0], ow[1], ow[2], ow[3], 0}; - uint64_t ev_wrong = append(r, &s, CX_EVIDENCE, CX_K_EVIDENCE_REF, CX_PROT_VERIFY_EVIDENCE, sub, 2, evl, wrongp, RX_LIVING_EV_WORDS); - uint64_t lp[2] = {ev.field[0], ev.field[1]}; - uint64_t cl[4] = {id[9], ev_ref, id[8]}; - uint64_t cand = append(r, &s, CX_CLAIM, CX_K_CANDIDATE, 0, sub, 1, cl, lp, 2); - uint64_t cand_open = append(r, &s, CX_CLAIM, CX_K_CANDIDATE, 0, sub, 2, cl, lp, 2); - CHECK(ev_ref && ev_wrong && cand && cand_open, "lesson records appended: evidence-ref %llu (wrong %llu), candidates %llu and %llu", - U(ev_ref), U(ev_wrong), U(cand), U(cand_open)); - uint64_t out = 0; - uint64_t n_before = s.n; - rc = rx_living_lesson_promote(&r->w, &s, cand, 999999, dg, &out); - CHECK(rc == RX_ERR_NOT_FOUND, "promotion with a missing evidence record refused (rc %d)", rc); - rc = rx_living_lesson_promote(&r->w, &s, cand, ev_wrong, dg, &out); - CHECK(rc == RX_ERR_ARG, "promotion with a wrong-digest evidence record refused (rc %d)", rc); - CHECK(s.n == n_before, "no promotion record was written by the refusals"); - rc = rx_living_lesson_promote(&r->w, &s, cand, ev_ref, dg, &out); - CHECK(rc == RX_OK && out && cx_get(&s, out)->kind == CX_K_PROMOTION && - cx_get(&s, out)->links[0] == cand && cx_get(&s, out)->links[1] == ev_ref, - "promotion with the matching evidence record accepted: promotion %llu links {%llu,%llu}", U(out), U(cand), U(ev_ref)); - uint64_t prom = out; + uint64_t out = 0, n_before = s.n; + int rc2 = rx_living_lesson_promote(&r->w, &s, cand, 999999, dg, &out); + CHECK(rc2 == RX_ERR_NOT_FOUND, "promotion with a missing evidence record refused (rc %d)", rc2); + rc2 = rx_living_lesson_promote(&r->w, &s, cand, ev_ref, other, &out); + CHECK(rc2 == RX_ERR_ARG, "promotion against a different object digest (wrong-digest evidence) refused (rc %d)", rc2); + CHECK(s.n == n_before, "no record was written by the refusals"); CHECK(cx_verify_chain(&s) == CX_OK, "cx_verify_chain passes after promotion (%llu records)", U(s.n)); static Prot before, after; snap(&s, &before); @@ -214,16 +254,9 @@ int main(void) { for (uint32_t i = 0; same && i < na; i++) same = a[i].hdr.id == b[i].hdr.id && a[i].verified; for (uint32_t i = 1; same && i < na; i++) same = a[i].hdr.id > a[i - 1].hdr.id; CHECK(same, "cx_recall by the evidence subject returns %u records, ordered and repeatable", na); - int is_fact = 0, open_cand = 1; - for (uint64_t i = 1; i <= s2.n; i++) { - const CxObject *o = cx_get(&s2, i); - if (o->kind == CX_K_PROMOTION && o->links[0] == cand) is_fact = 1; - if (o->kind == CX_K_PROMOTION && o->links[0] == cand_open) open_cand = 0; - } - CHECK(is_fact && cx_get(&s2, cand)->kind == CX_K_CANDIDATE && cx_get(&s2, prom)->links[0] == cand, - "lesson %llu is a promoted fact (promotion %llu)", U(cand), U(prom)); - CHECK(open_cand && cx_get(&s2, cand_open)->kind == CX_K_CANDIDATE, "lesson %llu is still only a candidate", U(cand_open)); - CHECK(rx_living_lesson_evidence_ok(&s2, ev_ref, dg) == RX_OK && rx_living_lesson_evidence_ok(&s2, ev_wrong, dg) == RX_ERR_ARG, + CHECK(cx_get(&s2, cand)->kind == CX_K_CANDIDATE && cx_get(&s2, prom)->kind == CX_K_PROMOTION && cx_get(&s2, prom)->links[0] == cand, + "after restart lesson %llu is a promoted fact (promotion %llu)", U(cand), U(prom)); + CHECK(rx_living_lesson_evidence_ok(&s2, ev_ref, dg) == RX_OK && rx_living_lesson_evidence_ok(&s2, ev_ref, other) == RX_ERR_ARG, "after restart the evidence gate still accepts the match and refuses the wrong digest"); /* tampered evidence record: refused, chain fails; restored value passes again */ uint64_t keep = cx_payload(&s2, cx_get(&s2, ev_ref))[0]; @@ -254,6 +287,36 @@ int main(void) { ident = before.id[i] == after.id[i] && memcmp(&before.o[i], &after.o[i], sizeof(CxObject)) == 0 && memcmp(before.pl[i], after.pl[i], before.o[i].n * sizeof(uint64_t)) == 0; CHECK(ident, "%u protected records (authority, receipts, verification evidence, promotion) byte-identical after restart", after.n); + + /* ---- C2: related-problem retrieval after a restart ---- */ + uint64_t want = rx_aien_key(CLASS_X925, rx_omega_regime(M, N)); + uint64_t k_prom[RX_AIEN_MEMORY_SLOTS]; + uint32_t nk_prom = rx_living_explored_keys(&s2, sub, k_prom, RX_AIEN_MEMORY_SLOTS); + 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)); + /* the same journal with the promotion left out: the lesson is only a candidate */ + static CxStore sn; + int cp_ok = cx_init(&sn, RX_CORTEX_SUBJECTS) == CX_OK; + for (uint64_t i = 1; cp_ok && i < prom; i++) { + const CxObject *q = cx_get(&s2, i); + 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]}}; + cp_ok = cx_append(&sn, &h, cx_payload(&s2, q), q->n, NULL) == CX_OK; + } + uint64_t k_open[RX_AIEN_MEMORY_SLOTS]; + 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, + "journal without the promotion: lesson %llu is a candidate and yields no memory", U(cand)); + Outcome oe, on, ou; + int e1 = episode(&s2, 1, &oe), e2 = episode(&s2, 0, &on), e3 = episode(&sn, 1, &ou); + printf("C2 explored-not-replanned: seeded %u key, assessment %s, plan seq %llu, memory count %llu\n", oe.seeded, + oe.status == RX_AIEN_GOAL_UNMET_EXPLORED ? "UNMET_EXPLORED" : "other", U(oe.plan), U(oe.mem_count)); + 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)); + 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)); + CHECK(e1 == 0 && oe.seeded == 1 && oe.status == RX_AIEN_GOAL_UNMET_EXPLORED && oe.plan == 0 && oe.mem_count == 1, + "second life, same condition: explored, NOT re-planned (rc %d)", e1); + CHECK(e2 == 0 && on.seeded == 1 && on.plan != 0 && on.mem_count == 2 && on.mem_key == CLASS_A725, + "second life, new core class: still gets a plan (rc %d)", e2); + CHECK(e3 == 0 && ou.seeded == 0 && ou.plan != 0 && ou.mem_key == CLASS_X925, + "second life, lesson never promoted: the condition IS re-explored (rc %d)", e3); + cx_free(&sn); cx_close(&s2); free(g_seen); free(g_stack); printf("R13_CORTEX_EPISODE %s: pass %d fail %d\n", g_fail ? "FAIL" : "PASS", g_pass, g_fail);