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
GitHub aien-dev/omega-mv#398 (code only)
Apply it
git checkout -b patch-a9cfc49e0c5c3f06 430df5b556d0430e6917bab89f078fa08a2ebd76 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/a9cfc49e0c5c3f06.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
398
4cc745755237dc9e57387677f1f9e4d499eda0fc
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":398,"url":"https://github.com/aien-dev/omega-mv/pull/398","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:44Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index d10c7a095..1c1277f31 100644 --- a/.crumb +++ b/.crumb @@ -64,7 +64,7 @@ }, { "digest": "83ae9e5727fbb3847b73e2027e6502276f1ccd165064ad9b672fcbf02954d2ee", - "last_commit": "30ff08f", + "last_commit": "430df5b", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "9254e5dfe5085071154ed78456c74218ccacb45ac058c06a705c0fcf6a4e300e", - "last_commit": "30ff08f", + "digest": "7e444b7d72a7c021d5c17c48d406394c36fef94f7dd91e38673e908ca0f87450", + "last_commit": "c70f820", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "ac4506602f55fbf72b85484a5d77ff817a1895358556a2117b403194781ec0f0", - "last_commit": "46cc7b5", + "digest": "9454b0ab0f8471bc801cc980698c86857924a166b61d84473fa1fbaa0a69384d", + "last_commit": "c70f820", "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:6d2bdbcd9374bc8c2523198181c15a2bd685d18d2e10df221da4876ca2379afb", - "compiled_at": "2026-10-11T14:41:00Z", + "children_root": "sha256:4089d87be11619ce575297b15d89590a788766a7e09860dc594ef440146f4a9d", + "compiled_at": "2026-10-11T14:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:14abc34bd1f652ffafa2cebf2c10b0326be451c949fd8b6d86bc2c35b49887fe", + "digest": "sha256:acb4933794fb6e1cbcb8fd0b334abed5e519dba4e70fc4db8dfe7867cb73bcaa", "evidence_files": 7243, "evidence_root": "sha256:7283d2aa790caa9b49833f970995b6aa5e18f680037a07bf35f1dcd0b6f4ad47", - "files": 9660, - "generated_at_commit": "46cc7b5b94626a292ed130be9f9315d4b069615d", - "source_tree": "sha256:9f20ea9b986721e5c6d4c829c1c9370e20af2f4ee6bb2aa0c94714924bbc3fdd" + "files": 9661, + "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", + "source_tree": "sha256:7d1703f519af364d288500ab574867a5dc35db06202fbf5c3fa9c2986d3d50db" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index fa57a6c5b..7d640f676 100644 --- a/Makefile +++ b/Makefile @@ -1305,6 +1305,19 @@ $(RX_R13_HOST): $(RX_R13_SRCS) src/runtime/rx_living.h src/runtime/rx_argus.h $( $(CC) $(CFLAGS) $(RX_PROD_ARGUS_FLAGS) -pthread -o $@ $(RX_R13_SRCS) $(RX_PROD_ARGUS_SRCS) \ $(AIENOS_CAP_LIB) -lm +# G1: the R13 living body journals one episode into Cortex (CxStore). Same +# production recipe as test-r13-host; the program is the R13 rig plus tests/runtime/rx_r13_cortex_episode.c. +RX_R13_CORTEX = $(OUT_DIR)/rx_r13_cortex_episode +RX_R13_CORTEX_SRCS = $(filter-out tests/runtime/rx_r13_living.c,$(RX_R13_SRCS)) tests/runtime/rx_r13_cortex_episode.c +$(RX_R13_CORTEX): $(RX_R13_CORTEX_SRCS) tests/runtime/rx_r13_living.c src/runtime/rx_living.h src/runtime/rx_argus.h \ + $(RX_COMPOSE_LIVING_CHECKS) $(ARGUS_STAMP) $(AIENOS_CAP_LIB) | $(OUT_DIR) + $(RX_PROD_REFUSE_TEST) + $(CC) $(CFLAGS) $(RX_PROD_ARGUS_FLAGS) -pthread -o $@ $(RX_R13_CORTEX_SRCS) $(RX_PROD_ARGUS_SRCS) \ + $(AIENOS_CAP_LIB) -lm + +test-r13-cortex-episode: $(RX_R13_CORTEX) + ./$(RX_R13_CORTEX) + $(RX_R13_SILICON): $(RX_R13_SRCS) $(RX_R13_SILICON_EXTRA) src/runtime/rx_living.h src/runtime/rx_argus.h \ $(RX_COMPOSE_LIVING_CHECKS) $(ARGUS_STAMP) $(AIENOS_CAP_LIB) | $(OUT_DIR) $(RX_PROD_REFUSE_TEST) diff --git a/src/.crumb b/src/.crumb index 70b9c49dd..de3334522 100644 --- a/src/.crumb +++ b/src/.crumb @@ -128,7 +128,7 @@ }, { "digest": "4b55679e40d65b6c29ef738eead218eb97844b1e38d2241d04568d895c7e9dc3", - "last_commit": "30ff08f", + "last_commit": "430df5b", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, @@ -139,8 +139,8 @@ "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { - "digest": "98acfce1a1131f63cb89cce389b153d85952ccdcf5f3e0462c080a159b45ec44", - "last_commit": "01f6a74", + "digest": "44927d9c42231abe027de1821d96edecb702fa0e1a791843bf3b9e0142a31bbf", + "last_commit": "c70f820", "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:4c7959973b37e8ebc108f32cbac6ee94cbf0f2ef5b3d975b511d3ee162be4684", - "compiled_at": "2026-10-11T14:41:00Z", + "children_root": "sha256:3294f4412d2e2eacc3eaeebd4cd09fc5729c47332ff41c7441fb457e7699ed09", + "compiled_at": "2026-10-11T14:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:9254e5dfe5085071154ed78456c74218ccacb45ac058c06a705c0fcf6a4e300e", + "digest": "sha256:7e444b7d72a7c021d5c17c48d406394c36fef94f7dd91e38673e908ca0f87450", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 449, - "generated_at_commit": "46cc7b5b94626a292ed130be9f9315d4b069615d", - "source_tree": "sha256:ea1a21268e8f7722742a2ff7d4389666b6f07576134eec5e4f3adf6eced1ccb4" + "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", + "source_tree": "sha256:05d15fbbabc4c2385696f8d31813a0d71810c961cb6f511e0e25cdccf7ed5f5d" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index ab993a3b5..d0f8b4dd6 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T16:27:12Z", + "compiled_at": "2026-10-11T14:53:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:98acfce1a1131f63cb89cce389b153d85952ccdcf5f3e0462c080a159b45ec44", + "digest": "sha256:44927d9c42231abe027de1821d96edecb702fa0e1a791843bf3b9e0142a31bbf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 63, - "generated_at_commit": "957b5a387ad5e8ff373696fff56b95946b402618", - "source_tree": "sha256:31946e6a01c1769f37f0fa21815ed092334ba160a1ae08e1c70165d0dbcef3b2" + "generated_at_commit": "8511f5e8e192c1d58b30effe9ee70ea9e90663bc", + "source_tree": "sha256:3a3c52eb04693e295ef581a1e66a84043f95b2eb0a3f3029ec3bec7cc6970c66" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_living.c b/src/runtime/rx_living.c index 2efb1c46b..5ee6cb535 100644 --- a/src/runtime/rx_living.c +++ b/src/runtime/rx_living.c @@ -751,3 +751,23 @@ int rx_living_enroll_callers(RxWorld *w, RxLivingKeyrings *k) { } return RX_OK; } + +int rx_living_lesson_evidence_ok(const CxStore *s, uint64_t evidence, const uint8_t object_digest[32]) { + CxRecord rec; + if (!s || !object_digest || evidence == 0 || evidence > s->n) return RX_ERR_NOT_FOUND; + if (cx_recall_id(s, evidence, &rec) == CX_ERR_DIGEST || !rec.verified) return RX_ERR_IDENTITY; + if (rec.hdr.kind != CX_K_EVIDENCE_REF || rec.hdr.n < RX_LIVING_EV_WORDS) return RX_ERR_ARG; + for (uint32_t i = 0; i < 4; i++) { + uint64_t v = 0; + for (uint32_t b = 0; b < 8; b++) v |= (uint64_t)object_digest[8 * i + b] << (8 * b); + if (rec.payload[RX_LIVING_EV_DIGEST0 + i] != v) return RX_ERR_ARG; + } + return RX_OK; +} + +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); +} diff --git a/src/runtime/rx_living.h b/src/runtime/rx_living.h index aa94b1ba2..4d7fbe1e7 100644 --- a/src/runtime/rx_living.h +++ b/src/runtime/rx_living.h @@ -35,6 +35,7 @@ #include "rx_aien.h" #include "rx_omega.h" #include "rx_generation.h" +#include "rx_cortex_record.h" /* Objects in the caller's RxWorld. Not a second world, not a device namespace. * input: 0 operand a, 1 operand b, 2 search epoch it is for, 3 trial, @@ -197,4 +198,18 @@ int rx_living_native_authority(void *ctx, uint32_t cap_id, uint64_t generation, void rx_living_object_digest(uint32_t type, const uint64_t field[RX_MAX_FIELDS], uint8_t out[32]); +/* ---- Cortex journal of the experience (G1) ---------------------------------- + * A lesson is a CX_K_CANDIDATE. It becomes a fact only through + * rx_living_lesson_promote, which refuses unless the evidence record is a + * CX_K_EVIDENCE_REF that verifies and whose payload words + * RX_LIVING_EV_DIGEST0.. hold `object_digest`, the rx_living_object_digest of + * the R13 evidence object. cx_promote alone only checks record kinds. */ +enum { RX_LIVING_EV_DIGEST0 = 0, RX_LIVING_EV_CRUMB = 4, RX_LIVING_EV_WORDS = 5 }; +/* RX_OK, RX_ERR_NOT_FOUND (no such record), RX_ERR_ARG (wrong kind or digest + * mismatch), RX_ERR_IDENTITY (record fails its digest check). */ +int rx_living_lesson_evidence_ok(const CxStore *s, uint64_t evidence, const uint8_t object_digest[32]); +/* 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); + #endif diff --git a/tests/.crumb b/tests/.crumb index cd3f7e721..093d9e259 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -212,7 +212,7 @@ }, { "digest": "b1b593c229ffdd13bf48e79fcac4ae6b4592b4f030fa7667b42864b160c35b6a", - "last_commit": "46cc7b5", + "last_commit": "430df5b", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, @@ -259,8 +259,8 @@ "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { - "digest": "930ee5e080ab63c9c6b20b7a1eade45f6b249246dccdb3039a01c5c31a217368", - "last_commit": "01f6a74", + "digest": "4f0e66038cd243c0ac75a088f2ea7e7b1038011193a0595d4a18ef202a780c6e", + "last_commit": "c70f820", "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:a93512812143f661a270e000d626260885d67808300a9af1c24d613109e1eba5", - "compiled_at": "2026-10-11T14:41:00Z", + "children_root": "sha256:d30b17f6f4909b4eb164a94ae45dbf0052a56258e2de0cce2e6b4d6a737ec711", + "compiled_at": "2026-10-11T14:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:ac4506602f55fbf72b85484a5d77ff817a1895358556a2117b403194781ec0f0", + "digest": "sha256:9454b0ab0f8471bc801cc980698c86857924a166b61d84473fa1fbaa0a69384d", "evidence_files": 14, "evidence_root": "sha256:080bc99a4a66250db3cda56908658fd3f09c53170ca32c85caa2d11ef3126f1d", - "files": 956, - "generated_at_commit": "46cc7b5b94626a292ed130be9f9315d4b069615d", - "source_tree": "sha256:12e751705e7b0ea7dc939028e157f5a211b431465f710c8e04be26db8de409f5" + "files": 957, + "generated_at_commit": "afb919f6b0d3e5c311bbe097e06a16766ef34f82", + "source_tree": "sha256:00cbf54395becbc7c951b00dec0c59af96540d728398893e36a6ac3e804ddd6e" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 318f29efa..217f47fd1 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T16:27:12Z", + "compiled_at": "2026-10-11T14:53:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:930ee5e080ab63c9c6b20b7a1eade45f6b249246dccdb3039a01c5c31a217368", + "digest": "sha256:4f0e66038cd243c0ac75a088f2ea7e7b1038011193a0595d4a18ef202a780c6e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 64, - "generated_at_commit": "957b5a387ad5e8ff373696fff56b95946b402618", - "source_tree": "sha256:9ac61c07a540d959b59500930bb3e3603932b98a83cd906cda71ee575cf20693" + "files": 65, + "generated_at_commit": "8511f5e8e192c1d58b30effe9ee70ea9e90663bc", + "source_tree": "sha256:3ac2434d5b8682d6f6751694bb5a53bc9545daa1cdd060c1c4368005f03deb91" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_r13_cortex_episode.c b/tests/runtime/rx_r13_cortex_episode.c new file mode 100644 index 000000000..bae6a2d53 --- /dev/null +++ b/tests/runtime/rx_r13_cortex_episode.c @@ -0,0 +1,261 @@ +/* G1: the R13 living body journals one positive episode into canonical Cortex. + * + * 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 + * the provenance chain, the digest-gated promotion, tamper refusal and restart. */ +#define main r13_main_unused +#include "rx_r13_living.c" +#undef main + +static int g_pass, g_fail; +#define CHECK(c, ...) do { if (c) { g_pass++; printf(" ok "); } else { g_fail++; printf(" FAIL "); } \ + printf(__VA_ARGS__); printf("\n"); } while (0) + +static void 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); } +} + +enum { N_STEP = 12 }; +static const char *const g_step[N_STEP] = {"observation", "belief", "prediction", "hypothesis", "plan", + "search", "gpu-input", "experiment", "evidence", "exp-belief", "candidate", "promotion"}; + +/* Cortex record of the newest crumb that wrote any field of `o`. */ +static uint64_t rec_of(Rig *r, RxObjRef o) { + uint64_t best = 0; + for (uint32_t fi = 0; fi < RX_MAX_FIELDS; fi++) { + uint64_t id = rx_cortex_record_of(&r->w, rx_world_explain(&r->w, o, fi)); + if (id > best) best = id; + } + return best; +} +/* Nearest ancestor of `from` (provenance, nearest first) written for object slot `slot`. */ +static uint64_t nearest(const CxStore *s, uint64_t from, uint32_t slot) { + static uint64_t p[4096]; + uint32_t n = cx_provenance(s, from, p, 4096); + 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) { + p->n = 0; + for (uint64_t i = 1; i <= s->n && p->n < 8192; i++) { + const CxObject *o = cx_get(s, i); + if (!o->protect || o->n > CX_WREC_WORDS + 8) continue; + p->id[p->n] = i; p->o[p->n] = *o; + memcpy(p->pl[p->n], cx_payload(s, o), o->n * sizeof(uint64_t)); + p->n++; + } +} +static int corrupt_copy(const char *src, const char *dst, long off) { + FILE *a = fopen(src, "rb"), *b = fopen(dst, "wb"); + if (!a || !b) return -1; + int c; long i = 0; + while ((c = fgetc(a)) != EOF) { if (i++ == off) c ^= 0x01; fputc(c, b); } + fclose(a); fclose(b); + return 0; +} + +int main(void) { + signal(SIGPIPE, SIG_IGN); + setvbuf(stdout, NULL, _IONBF, 0); + if (operator_state(NULL) != 0 || seen_fit(1u << 17) != 0) return 2; + char dir[192], jr[256], jr2[256]; + if (scratch_dir(dir, sizeof dir, "r13-cortex") != 0) return 2; + snprintf(jr, sizeof jr, "%s/episode.cxj", dir); + snprintf(jr2, sizeof jr2, "%s/episode-flipped.cxj", dir); + Rig *r = calloc(1, sizeof *r); + static CxStore s, s2; + if (!r) return 2; + if (start(r, POSITIVE) != 0) { fprintf(stderr, "setup failed at stage %d\n", g_stage); return 1; } + int rc = cx_open(&s, jr, RX_CORTEX_SUBJECTS, 0); + CHECK(rc == CX_OK, "journal opened (rc %d)", rc); + rc = rx_cortex_attach(&r->w, &s, 1); + CHECK(rc == RX_OK, "recorder attached to the living world (rc %d)", rc); + rc = run(r, POSITIVE); + CHECK(rc == 0, "R13 POSITIVE episode ran to promotion and in force (rc %d)", rc); + 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; } + rx_world_wait_quiescent(&r->w, 5000); + uint64_t recs = 0, errs = 0; + rx_cortex_status(&r->w, &recs, &errs); + CHECK(recs > 0 && errs == 0, "recorder wrote %llu records, %llu errors", U(recs), U(errs)); + + /* 1. a Cortex record for each crumb of the episode, and the chain */ + RxObjRef ref[N_STEP] = {r->aien.o.placement, r->aien.o.belief, r->aien.o.prediction, + r->aien.o.hypothesis, r->aien.o.plan, r->omega.o.search, r->living.o.input, + r->living.o.output, r->living.o.evidence, r->aien.o.experiment_belief, + r->living.o.candidate, r->living.o.promotion}; + uint64_t id[N_STEP]; + for (int i = 0; i < N_STEP; i++) { + id[i] = rec_of(r, ref[i]); + const CxObject *o = id[i] ? cx_get(&s, id[i]) : NULL; + CHECK(o != NULL, "%-12s -> cortex record %llu kind 0x%x", g_step[i], U(id[i]), o ? o->kind : 0); + } + /* Ground truth of the POSITIVE run (see the report): two causal chains meet in the same world. + * A (action): goal -> assessment -> plan -> experiment -> evidence -> experiment belief -> candidate -> promotion. + * B (sense): observation -> belief -> prediction (failed) -> hypothesis. + * Each link below is the nearest cx_provenance ancestor of the one before. */ + uint64_t as = rec_of(r, r->aien.o.assessment); + uint64_t ca[6], nca = 0; + uint64_t cur = id[8]; + RxObjRef chainA[] = {r->living.o.output, r->aien.o.plan, r->aien.o.assessment, r->aien.o.goal}; + char line[400]; int ln = snprintf(line, sizeof line, "A: evidence %llu", U(id[8])); + for (unsigned i = 0; i < 4; i++) { + uint64_t nx = cur ? nearest(&s, cur, chainA[i].id) : 0; + ca[nca++] = nx; + ln += snprintf(line + ln, sizeof line - ln, " <- %s %llu", i == 0 ? "experiment" : i == 1 ? "plan" : i == 2 ? "assessment" : "goal", U(nx)); + if (nx) cur = nx; + } + printf("CHAIN %s\n", line); + CHECK(ca[0] && ca[1] && ca[2] && ca[3] && cx_get(&s, ca[3])->kind == CX_K_WORK_ACCEPTED && as, + "chain A: evidence <- experiment <- plan <- assessment <- goal (outside input) is in the Cortex provenance"); + CxFilter fc = {CX_EXECUTION, CX_K_EXEC_COMMIT, 0, 1}; + CxRecord hr; + uint64_t hyp = cx_recall(&s, (uint64_t)r->aien.o.hypothesis.id + 1u, 0, UINT64_MAX, &fc, &hr, 1) ? hr.hdr.id : 0; + uint64_t pr = hyp ? nearest(&s, hyp, r->aien.o.prediction.id) : 0; + uint64_t be = pr ? nearest(&s, pr, r->aien.o.belief.id) : 0; + uint64_t ob = 0; + if (be) { uint64_t p[4096]; uint32_t n = cx_provenance(&s, be, p, 4096); + for (uint32_t i = 0; i < n && !ob; i++) if (cx_get(&s, p[i])->kind == CX_K_WORK_ACCEPTED) ob = p[i]; } + printf("CHAIN B: hypothesis %llu <- prediction %llu <- belief %llu <- observation %llu\n", U(hyp), U(pr), U(be), U(ob)); + CHECK(hyp && pr && be && ob, "chain B: hypothesis <- prediction <- belief <- observation (outside input) is in the Cortex provenance"); + 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)); + { /* the lesson side: later records descend from evidence */ + uint64_t p[4096]; uint32_t n = cx_provenance(&s, id[10], p, 4096), has = 0, has_eb = 0; + for (uint32_t i = 0; i < n; i++) { has |= p[i] == id[8]; has_eb |= p[i] == id[9]; } + 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); + uint64_t q[4096]; uint32_t m = cx_provenance(&s, id[11], q, 4096), hc = 0; + for (uint32_t i = 0; i < m; i++) hc |= q[i] == id[10]; + CHECK(hc, "promotion %llu descends from candidate %llu", U(id[11]), U(id[10])); + } + CHECK(cx_verify_chain(&s) == CX_OK, "cx_verify_chain passes (%llu records)", U(s.n)); + + /* 2. lesson candidate and digest-bound evidence */ + RxObject ev; + rx_world_read(&r->w, r->living.o.evidence, &ev); + uint8_t dg[32]; + rx_living_object_digest(ev.type, ev.field, dg); + 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); + 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; + 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); + uint32_t n_auth = 0, n_commit = 0; + 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); } + printf("protected records: %u (authority %u, commit receipt %u)\n", before.n, n_auth, n_commit); + CHECK(before.n > 0 && n_auth > 0, "authority and receipt evidence exist to compare"); + + stop(r); + free(r); + uint64_t n_records = s.n; + cx_close(&s); + CHECK(g_first_failure[0] == 0, "no World failure noted while stopping"); + + /* tampering: flip one byte of a journal copy, inside the evidence-ref record */ + struct stat st; stat(jr, &st); + int refused = 0; + for (long off = st.st_size - 400; off < st.st_size - 300 && !refused; off += 7) { + corrupt_copy(jr, jr2, off); + CxStore t; memset(&t, 0, sizeof t); + int trc = cx_open(&t, jr2, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); + if (trc != CX_OK) refused = trc; else cx_close(&t); + } + CHECK(refused != 0, "journal with one flipped byte is refused at open (rc %d)", refused); + + /* 3. restart: a new body opens the same journal */ + rc = cx_open(&s2, jr, RX_CORTEX_SUBJECTS, 0); + CHECK(rc == CX_OK && s2.n == n_records, "journal reopened: %llu records (was %llu)", U(s2.n), U(n_records)); + CHECK(cx_verify_chain(&s2) == CX_OK, "cx_verify_chain passes after restart"); + CxRecord a[64], b[64]; + CxFilter f = {0, 0, 0, 1}; + uint32_t na = cx_recall(&s2, sub, 0, UINT64_MAX, &f, a, 64), nb = cx_recall(&s2, sub, 0, UINT64_MAX, &f, b, 64); + int same = na == nb && na > 0; + 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, + "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]; + cx_tamper(&s2, ev_ref, 0, keep ^ 1u); + CHECK(cx_verify_chain(&s2) != CX_OK && rx_living_lesson_evidence_ok(&s2, ev_ref, dg) == RX_ERR_IDENTITY, + "tampered evidence record: cx_verify_chain fails and the gate refuses"); + cx_tamper(&s2, ev_ref, 0, keep); + CHECK(cx_verify_chain(&s2) == CX_OK, "restored record verifies again"); + /* what is not in Cortex */ + uint64_t kinds[16] = {0}; uint32_t kid[16] = {0}; int nk = 0; + for (uint64_t i = 1; i <= s2.n; i++) { + uint32_t k = cx_get(&s2, i)->kind; int j = 0; + while (j < nk && kid[j] != k) j++; + if (j == nk && nk < 16) kid[nk++] = k; + if (j < 16) kinds[j]++; + } + printf("kinds in Cortex:"); + for (int j = 0; j < nk; j++) printf(" 0x%x x%llu", kid[j], U(kinds[j])); + 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"); + int ephemeral = 1; + for (int j = 0; j < nk; j++) ephemeral &= (kid[j] >= CX_K_BASE); + CHECK(ephemeral, "every record is a canonical kind; there is no kind for an object table or reaction state"); + + /* 4. protected evidence byte-identical */ + snap(&s2, &after); + int ident = after.n == before.n; + for (uint32_t i = 0; ident && i < before.n; i++) + 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); + 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); + return g_fail ? 1 : 0; +}