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/patches/d724af7d25706c20 # 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 ## GitHub aien-dev/omega-mv#311 (code only) closed derived `d724af7d25706c20` #311 Imported from GitHub aien-dev/omega-mv#311 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `a7e626a55f39b94a60393c66805b6a9496940e3c` · Head `2ca1a95626f79f8b7332c5ab584062f149a31d31` ### Apply it ``` git checkout -b patch-d724af7d25706c20 a7e626a55f39b94a60393c66805b6a9496940e3c curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/d724af7d25706c20.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 311 2ca1a95626f79f8b7332c5ab584062f149a31d31 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":311,"url":"https://github.com/aien-dev/omega-mv/pull/311","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:16Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 9d58d0d58..00e7b5f7a 100644 --- a/.crumb +++ b/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "ba7d7c6e399d6a92a34be467f1d5501a9620a63839d38c27bcf8fb90b7448a02", - "last_commit": "2041f24", + "digest": "2e4b0cb0c91b5da2ddd89f9fe8adf08eb30a8652e2a226fd0396257b3081109a", + "last_commit": "c413740", "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": "e96259ccad1aad77187347965341897722b089df3ee84a17145a32e8d308a4a6", - "last_commit": "2041f24", + "digest": "e3f161b4058818a7618cafd193f7991475b5e7888e290d0d0147641e16e735f7", + "last_commit": "df6bc1c", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "03e780fc46936d8b534e4992f9d87e3ce95711a40fdfe0128a96328410e3af5b", - "last_commit": "2041f24", + "digest": "02f1cf99b38d0c0eac5b9e7ab560e1611571922ee3a5ce46fa3440266397ac92", + "last_commit": "df6bc1c", "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:a1b9d0223f485c3d79afd990b37b8a8575e6668d77f9a85ccbe77f136f7388d1", - "compiled_at": "2026-10-06T02:17:50Z", + "children_root": "sha256:e955fb0b2fdac485b29466649db06bd881789eeae19fe8735043333ecdf00b1b", + "compiled_at": "2026-10-06T02:24:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:d26d0d4e2cc5d7f3e9f896ab2516952dd36ff4c7b960d502f1d183ecdc0ce2de", + "digest": "sha256:b1ac2d37f6848d31d594e48c17e8cf646ed8dbd920718d1ba66d5b1da624badb", "evidence_files": 3954, "evidence_root": "sha256:56109875c0df7c0d220d089a628736b72e5294f7280208a0751a2dd6b37b4399", - "files": 5782, - "generated_at_commit": "fc9ac255259bed84ae6d25d7c0858bd91ccbafd5", - "source_tree": "sha256:e462d7c70ea40c040c2138f1b4bc62afc8315379bd248afad1888f06aeb9f5a2" + "files": 5786, + "generated_at_commit": "df6bc1cd3e0fbb70807274858bd734eb677be72c", + "source_tree": "sha256:550a3174f450fa5b36c60083c08946e596448e65eb0453b2dfc1c7352c9a40dc" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index a6b7d4514..5dcc05c9b 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T23:21:14Z", + "compiled_at": "2026-10-06T02:24:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:ba7d7c6e399d6a92a34be467f1d5501a9620a63839d38c27bcf8fb90b7448a02", + "digest": "sha256:2e4b0cb0c91b5da2ddd89f9fe8adf08eb30a8652e2a226fd0396257b3081109a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 58, - "generated_at_commit": "2f8b6f85971afc782d986abb888e55fa200ca3d0", - "source_tree": "sha256:779bf559b726b707d9d7b6bb029cf16b4e9b5ee0f5692f0d22931fa7934e367b" + "files": 59, + "generated_at_commit": "df6bc1cd3e0fbb70807274858bd734eb677be72c", + "source_tree": "sha256:cf008ab8f3f0b846e5762f4209ec3929f9c0f860a1f1b5923df9744ee5782d67" }, "owner_lane": "", "provenance": { diff --git a/mk/rx_compose_lib.mk b/mk/rx_compose_lib.mk new file mode 100644 index 000000000..c1d4da368 --- /dev/null +++ b/mk/rx_compose_lib.mk @@ -0,0 +1,63 @@ +# --------------------------------------------------------------------------- +# NEXT-PHASE-1 cut 1a: build/librx_compose.a, the COMPOSITION-2 runtime as a +# static library for a host program (sovereign-core crates/aien-omega-compose), +# plus its C ABI facade src/runtime/rxc_host_abi.{h,c}. +# +# make build/librx_compose.a the library (PRODUCTION flags only) +# make test-rxc-host-abi open, run, close, reopen + recall, torn tail +# +# Sources: the production living set RX_R13_SRCS (top-level Makefile) minus +# everything under tests/, plus rxc_host_abi.c. The variable is reused, not +# copied, so the library follows the living build. The AIENOS capability +# library pinned by aienos.lock ($(AIENOS_CAP_LIB)) is merged into the same +# archive, so a host links one file (plus -lpthread -lm). +# +# Production hygiene: the objects are compiled with $(CFLAGS) $(RXC_LIB_EXTRA) +# and the recipe refuses AIEN_TEST_BUILD / RXC_TEST_HOOKS in CFLAGS +# ($(RX_PROD_REFUSE_TEST), the same check as the production program). +# -fPIC so the archive can also go into a shared object. +# +# This file is read BEFORE the top-level Makefile defines RX_R13_SRCS, +# CFLAGS and AIENOS_CAP_LIB (mk/*.mk is included at its top), so every +# prerequisite naming them is written $$(...) and expanded in the second +# phase (.SECONDEXPANSION, as mk/polyglot.mk and mk/allen.mk do). +# --------------------------------------------------------------------------- +ifndef RX_COMPOSE_LIB_MK_LOADED +RX_COMPOSE_LIB_MK_LOADED := 1 + +.PHONY: rx-compose-lib test-rxc-host-abi +.SECONDEXPANSION: + +RXC_LIB = $(OUT_DIR)/librx_compose.a +RXC_LIB_DIR = $(OUT_DIR)/rxc_lib +RXC_LIB_EXTRA = -fPIC +RXC_LIB_SRCS = $(filter-out tests/%,$(RX_R13_SRCS)) src/runtime/rxc_host_abi.c +RXC_LIB_OBJS = $(patsubst %.c,$(RXC_LIB_DIR)/%.o,$(RXC_LIB_SRCS)) +RXC_HOST_ABI_TEST = $(OUT_DIR)/rxc_host_abi_test + +rx-compose-lib: $(RXC_LIB) + +$(RXC_LIB_DIR)/%.o: %.c src/runtime/rxc_host_abi.h + $(RX_PROD_REFUSE_TEST) + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(RXC_LIB_EXTRA) -c $< -o $@ + +# One archive: the runtime objects plus the pinned AIENOS capability library +# (GNU ar MRI script; built into a temp name, then moved, so a failed build +# never leaves a half archive). +$(RXC_LIB): $$(RXC_LIB_OBJS) $$(AIENOS_CAP_LIB) + $(RX_PROD_REFUSE_TEST) + @mkdir -p $(dir $@) + @rm -f $@.tmp + @{ echo "create $@.tmp"; echo "addlib $(AIENOS_CAP_LIB)"; \ + for o in $(RXC_LIB_OBJS); do echo "addmod $$o"; done; echo "save"; echo "end"; } | ar -M + @ranlib $@.tmp && mv -f $@.tmp $@ + @echo "librx_compose: $@ ($(words $(RXC_LIB_OBJS)) objects + $(notdir $(AIENOS_CAP_LIB)))" + +$(RXC_HOST_ABI_TEST): tests/runtime/rxc_host_abi_test.c src/runtime/rxc_host_abi.h $(RXC_LIB) + $(CC) $(CFLAGS) -o $@ tests/runtime/rxc_host_abi_test.c $(RXC_LIB) -pthread -lm + +test-rxc-host-abi: $(RXC_HOST_ABI_TEST) + ./$(RXC_HOST_ABI_TEST) + +endif diff --git a/src/.crumb b/src/.crumb index 99d1a96e1..2cba9a017 100644 --- a/src/.crumb +++ b/src/.crumb @@ -133,8 +133,8 @@ "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { - "digest": "63f3df62adf0a29ee2fdfdc4e170409de18f8a2a33c5644ab8d4009cf1c5246f", - "last_commit": "f816473", + "digest": "ea7ee6a21ff8c2789b18dbeda15a9a72ca13369bfe42d93f66d20036ef8f75af", + "last_commit": "df6bc1c", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -169,15 +169,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:920218d42490379078986bd65f6d50e8ef9064df20da67bfc0ca50ea0de4b4c0", - "compiled_at": "2026-10-06T01:57:59Z", + "children_root": "sha256:6418c3add919bdad8d246a8aff39d203104f1733d77d7ff7112c0800edf409fa", + "compiled_at": "2026-10-06T02:24:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:e96259ccad1aad77187347965341897722b089df3ee84a17145a32e8d308a4a6", + "digest": "sha256:e3f161b4058818a7618cafd193f7991475b5e7888e290d0d0147641e16e735f7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 409, - "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", - "source_tree": "sha256:6bf5b4894dc9c29cfa891e8995d4f01de9c2cf5c2e411c1a813fa75828285bc7" + "files": 411, + "generated_at_commit": "df6bc1cd3e0fbb70807274858bd734eb677be72c", + "source_tree": "sha256:00da16f90258cecd4d43d5c7f673680e7b0d2d707068fd1e1da4f3e904595087" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index b30b38677..75f1c61f3 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T00:23:42Z", + "compiled_at": "2026-10-06T01:18:22Z", "compiler": "crumb-compile/1", - "digest": "sha256:63f3df62adf0a29ee2fdfdc4e170409de18f8a2a33c5644ab8d4009cf1c5246f", + "digest": "sha256:ea7ee6a21ff8c2789b18dbeda15a9a72ca13369bfe42d93f66d20036ef8f75af", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 61, - "generated_at_commit": "fa18e58a86a7402de785e07044f5f444a1dcca3c", - "source_tree": "sha256:d6c5fe7ac0396ceedc2c666adae44e38d9c03553974f120898eea988cbc033a4" + "files": 63, + "generated_at_commit": "55f11ad10e21f9f2dd49649a95da0a85f93ce4df", + "source_tree": "sha256:5a85b38f7bdaf9f03d286b7342558d39f843abe4e86c70230b717c2448c49a49" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rxc_host_abi.c b/src/runtime/rxc_host_abi.c new file mode 100644 index 000000000..82314dd2d --- /dev/null +++ b/src/runtime/rxc_host_abi.c @@ -0,0 +1,681 @@ +/* + * rxc_host_abi.c -- the host ABI facade over COMPOSITION-2 (rxc_host_abi.h). + * + * Everything here forwards to existing calls: aien_mid_* (identity), + * cx_open read-only (torn-tail probe), cq_* / sr_register_skill (the Skill + * graph, built exactly as the COMPOSITION-2 tests build it), aienos_cap_* + * (authority), rx_compose_open/run/close, cx_recall/cx_recall_id. The only + * state it adds is the per-handle slot that lets a ctx-less RxcContract and + * AgSkillFn reach the host callback. + */ +#include "runtime/rxc_host_abi.h" + +#include "runtime/aien_machine_id.h" +#include "runtime/aienos_cap.h" +#include "runtime/rx_capq.h" +#include "runtime/rx_compose.h" +#include "runtime/rx_cortex.h" +#include "runtime/rx_cortex_record.h" +#include "runtime/rx_graph.h" +#include "runtime/rx_skillroute.h" +#include "sha256.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +_Static_assert(RXC_HOST_MAX_SKILLS == RXC_K, "one host Skill per composition alternative"); +_Static_assert(RXC_HOST_SUBJECT_GOAL == RXC_CX_SUBJECT(RXC_SLOT_GOAL), "goal subject"); +_Static_assert(RXC_HOST_SUBJECT_STATE == RXC_CX_SUBJECT(RXC_SLOT_STATE), "state subject"); +_Static_assert((int)RXC_HOST_OUT_COMMITTED == (int)RXC_OUT_COMMITTED && (int)RXC_HOST_OUT_RECORD_FAILED == + (int)RXC_OUT_RECORD_FAILED, "outcome codes"); +_Static_assert((int)RXC_HOST_ROOT_PROVISIONED == (int)AIEN_MID_ROOT_PROVISIONED && + (int)RXC_HOST_ROOT_HARDWARE == (int)AIEN_MID_ROOT_HARDWARE, "root kinds"); +_Static_assert(RXC_HOST_NONE == RXC_NONE, "no winner"); +_Static_assert(RXC_HOST_SUBJECT_HOST < RXC_CX_SUBJECT(0), "host records stay off composition subjects"); + +#define RXC_HOST_OP 1u /* the one semantic operation every host Skill provides */ +#define RXC_HOST_SKILL_ID0 1u /* Skill ids: RXC_HOST_SKILL_ID0 + index */ +#define RXC_HOST_N_WORKERS 2u + +typedef struct { + char name[RXC_HOST_NAME_MAX]; + RxcHostSkillFn fn; + void *ctx; + uint64_t cost; + uint8_t digest[32]; +} HostSkill; + +struct RxcHost { + uint32_t slot; + char dir[200]; + uint64_t session; + AienMachineId self; + AienMachineId slots[8]; + AienMachineIndex ix; + CqCatalog cat; + int cat_init; + AgSkillTable skills; + SrRouter router; + HostSkill sk[RXC_HOST_MAX_SKILLS]; + uint32_t n_sk; + RxcHostVerifyFn verify; + void *verify_ctx; + AienosCapAdmin *admin; + AienosCapView *view; + RxCompose *c; + int opened; + RxcHostInfo info; +}; + +/* ---- slots: ctx-less contract / Skill functions reach their handle ----- */ + +static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; +static RxcHost *volatile g_slot[RXC_HOST_MAX_HANDLES]; + +static int host_contract(uint32_t slot, uint64_t input, uint64_t result) { + RxcHost *h = g_slot[slot]; + if (!h) return 0; + if (h->verify) return h->verify(h->verify_ctx, input, result) == 1; + return result != 0; +} + +static uint64_t host_skill(uint32_t slot, uint32_t k, const uint64_t *in, uint32_t n, int *failed) { + RxcHost *h = g_slot[slot]; + uint64_t result = 0; + *failed = 1; + if (!h || k >= h->n_sk || !h->sk[k].fn || n == 0) return 0; + if (h->sk[k].fn(h->sk[k].ctx, in[0], &result) == 0 && result != 0) *failed = 0; + return *failed ? 0 : result; +} + +#define RXC_HOST_TRAMPOLINES(S) \ + static int host_contract_##S(uint64_t i, uint64_t r) { return host_contract(S, i, r); } \ + static uint64_t host_skill_##S##_0(const uint64_t *in, uint32_t n, uint32_t a, int *f) { \ + (void)a; return host_skill(S, 0, in, n, f); } \ + static uint64_t host_skill_##S##_1(const uint64_t *in, uint32_t n, uint32_t a, int *f) { \ + (void)a; return host_skill(S, 1, in, n, f); } +RXC_HOST_TRAMPOLINES(0) +RXC_HOST_TRAMPOLINES(1) +RXC_HOST_TRAMPOLINES(2) +RXC_HOST_TRAMPOLINES(3) +_Static_assert(RXC_HOST_MAX_HANDLES == 4u, "one RXC_HOST_TRAMPOLINES line per handle slot"); + +static const RxcContract g_contract[RXC_HOST_MAX_HANDLES] = { + host_contract_0, host_contract_1, host_contract_2, host_contract_3 }; +static const AgSkillFn g_skillfn[RXC_HOST_MAX_HANDLES][RXC_HOST_MAX_SKILLS] = { + { host_skill_0_0, host_skill_0_1 }, { host_skill_1_0, host_skill_1_1 }, + { host_skill_2_0, host_skill_2_1 }, { host_skill_3_0, host_skill_3_1 } }; + +/* ---- helpers ------------------------------------------------------------- */ + +static void fill_identity(RxcHostInfo *info, const AienMachineId *m) { + info->machine_root = m->root; + memcpy(info->machine_id, m->id, sizeof info->machine_id); +} + +static void fill_record(RxcHostRecord *r, const CxRecord *x) { + memset(r, 0, sizeof *r); + r->id = x->hdr.id; + r->cls = x->hdr.cls; + r->kind = x->hdr.kind; + r->subject = x->hdr.subject; + r->t = x->hdr.t; + r->generation = x->hdr.generation; + r->tag = x->hdr.tag; + for (uint32_t i = 0; i < 4; i++) r->links[i] = x->hdr.links[i]; + r->n_payload = x->hdr.n; + r->verified = x->verified ? 1u : 0u; + memcpy(r->digest, x->hdr.digest, 32); +} + +static CqEntry host_provide(uint32_t cap, uint32_t real, uint64_t cost) { + CqEntry e; + memset(&e, 0, sizeof e); + e.capability_id = cap; + e.realization_id = real; + e.op = RXC_HOST_OP; + e.effects = CQ_FX_PURE; + e.in_types = 0x1; + e.out_types = 0x2; + e.confidence_ppm = 900000; + e.reliability_ppm = 990000; + e.evidence_level = CQ_EV_MEASURED; + e.evidence_ref = 0xE000u + cap; + e.cost = cost; + e.latency_us = 100; + e.energy_uj = 100; + e.live = CQ_LIVE_AVAILABLE; + e.generation = 1; + return e; +} + +static SrRequirement host_requirement(void) { + SrRequirement q; + memset(&q, 0, sizeof q); + q.need.semantic_operation = RXC_HOST_OP; + q.need.accepted_input_types = ~0ull; + q.need.effect_class = 0x1FF; + q.need.authority_ceiling.resource_hi = UINT64_MAX; + q.need.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; + q.t.n_order = 1; + q.t.order[0] = CQ_DIM_COST; + q.t.k = CQ_MAX_K; + q.local_only = 1; + return q; +} + +static void refresh_info(RxcHost *h) { + h->info.opened = (uint32_t)h->opened; + h->info.n_skills = h->n_sk; + if (h->opened) { + h->info.records = h->c->cx.n; + h->info.recovered_record = h->c->recovered_record; + h->info.rolled_back = h->c->rolled_back; + h->info.recovered_completed = h->c->recovered_completed; + h->info.anchor_unknown = h->c->anchor_unknown ? 1u : 0u; + } +} + +/* Build the Skill graph, start the authority, open the composition. */ +static int host_seal(RxcHost *h) { + if (h->opened) return RXC_HOST_OK; + if (h->info.open_rc != 0) /* a failed open is final */ + return h->info.open_rc == RX_ERR_REPLAY ? RXC_HOST_E_REPLAY : RXC_HOST_E_OPEN; + aien_mid_index_init(&h->ix, h->slots, 8); + if (cq_catalog_init_canonical(&h->cat, &h->ix, &h->self, 8, 8) != CQ_OK) return RXC_HOST_E_NOMEM; + h->cat_init = 1; + uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC); + if (cq_op_define(&h->cat, RXC_HOST_OP, 0, all) != CQ_OK) return RXC_HOST_E_STATE; + h->skills.n = h->n_sk; + for (uint32_t i = 0; i < h->n_sk; i++) { + h->skills.skill[i].id = RXC_HOST_SKILL_ID0 + i; + h->skills.skill[i].fn = g_skillfn[h->slot][i]; + memcpy(h->skills.skill[i].identity, h->sk[i].digest, 32); + SrSkill s; + memset(&s, 0, sizeof s); + s.skill_id = RXC_HOST_SKILL_ID0 + i; + s.version = 1; + memcpy(s.digest, h->sk[i].digest, 32); + s.machine = h->cat.self_machine; + CqEntry e = host_provide(1 + i, 70 + i, h->sk[i].cost); + if (sr_register_skill(&h->cat, &s, &e, 1) != SR_OK) return RXC_HOST_E_STATE; + } + if (cq_catalog_build(&h->cat) != CQ_OK) return RXC_HOST_E_STATE; + h->router = (SrRouter){ &h->cat, &h->skills }; + if (aienos_cap_start(&h->admin, &h->view) != 0) { + h->admin = NULL; + h->info.open_rc = RX_ERR_AUTHORITY; + return RXC_HOST_E_OPEN; + } + int rc = rx_compose_open(h->c, h->dir, &h->self, h->session, &h->router, + g_contract[h->slot], h->admin, h->view, RXC_HOST_N_WORKERS); + h->info.open_rc = rc; + if (rc != RX_OK) { + aienos_cap_stop(h->admin, h->view); + h->admin = NULL; + return rc == RX_ERR_IDENTITY ? RXC_HOST_E_IDENTITY + : rc == RX_ERR_REPLAY ? RXC_HOST_E_REPLAY + : RXC_HOST_E_OPEN; + } + h->opened = 1; + refresh_info(h); + return RXC_HOST_OK; +} + +/* ---- public API ------------------------------------------------------------ */ + +int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, + uint64_t session, uint32_t flags, RxcHost **out, RxcHostInfo *info) { + if (out) *out = NULL; + if (info) memset(info, 0, sizeof *info); + if (!dir || !*dir || !root || !out || strlen(dir) >= 200 - 16 || + flags != 0) + return RXC_HOST_E_ARG; + AienMachineId self; + if (aien_mid_derive((uint8_t)root_kind, root, root_len, &self) != AIEN_MID_OK) + return RXC_HOST_E_IDENTITY; + + RxcHostInfo inf; + memset(&inf, 0, sizeof inf); + inf.abi_version = RXC_HOST_ABI_VERSION; + fill_identity(&inf, &self); + if (mkdir(dir, 0700) && errno != EEXIST) return RXC_HOST_E_ARG; + + /* Identity first (rx_compose_open checks it again). */ + char p[256]; + snprintf(p, sizeof p, "%s/machine.id", dir); + if (access(p, F_OK) == 0) { + AienMachineId stored; + if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) { + if (info) *info = inf; + return RXC_HOST_E_IDENTITY; + } + inf.machine_id_was_stored = 1; + } + /* Torn-tail probe: read-only, no lock, no repair. */ + snprintf(p, sizeof p, "%s/cortex.cx", dir); + if (access(p, F_OK) == 0) { + CxStore probe; + memset(&probe, 0, sizeof probe); + int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); + if (prc == CX_OK) { + inf.records = probe.n; + cx_close(&probe); + } else if (prc == CX_ERR_TORN) { + inf.tail_torn = 1; + } else { + inf.open_rc = prc; + if (info) *info = inf; + return RXC_HOST_E_OPEN; + } + } + /* Strict: a torn tail is refused, never cut here (rxc_host_recover). */ + if (inf.tail_torn) { + if (info) *info = inf; + return RXC_HOST_E_TORN; + } + + RxcHost *h = calloc(1, sizeof *h); + if (!h) return RXC_HOST_E_NOMEM; + h->c = calloc(1, sizeof *h->c); + if (!h->c) { free(h); return RXC_HOST_E_NOMEM; } + pthread_mutex_lock(&g_mu); + uint32_t slot = RXC_HOST_MAX_HANDLES; + for (uint32_t i = 0; i < RXC_HOST_MAX_HANDLES; i++) + if (!g_slot[i]) { slot = i; break; } + if (slot < RXC_HOST_MAX_HANDLES) g_slot[slot] = h; + pthread_mutex_unlock(&g_mu); + if (slot == RXC_HOST_MAX_HANDLES) { + free(h->c); + free(h); + return RXC_HOST_E_FULL; + } + h->slot = slot; + snprintf(h->dir, sizeof h->dir, "%s", dir); + h->session = session; + h->self = self; + h->info = inf; + *out = h; + if (info) *info = h->info; + return RXC_HOST_OK; +} + +int rxc_host_register_skill(RxcHost *h, const char *name, const uint8_t *digest32, uint64_t cost, + RxcHostSkillFn fn, void *ctx) { + if (!h || !name || !fn) return RXC_HOST_E_ARG; + size_t len = strlen(name); + if (len == 0 || len >= RXC_HOST_NAME_MAX) return RXC_HOST_E_ARG; + if (h->opened || h->info.open_rc != 0) return RXC_HOST_E_STATE; + if (h->n_sk >= RXC_HOST_MAX_SKILLS) return RXC_HOST_E_FULL; + HostSkill *s = &h->sk[h->n_sk]; + memset(s, 0, sizeof *s); + memcpy(s->name, name, len); + s->fn = fn; + s->ctx = ctx; + s->cost = cost; + if (digest32) memcpy(s->digest, digest32, 32); + else sha256_hash((const uint8_t *)name, len, s->digest); + return (int)h->n_sk++; +} + +int rxc_host_set_verify(RxcHost *h, RxcHostVerifyFn fn, void *ctx) { + if (!h) return RXC_HOST_E_ARG; + if (h->opened) return RXC_HOST_E_STATE; + h->verify = fn; + h->verify_ctx = ctx; + return RXC_HOST_OK; +} + +int rxc_host_run(RxcHost *h, uint64_t task, uint64_t now_us, RxcHostResult *out) { + if (!h || !out || task == 0) return RXC_HOST_E_ARG; + memset(out, 0, sizeof *out); + out->winner = out->winner_skill = RXC_HOST_NONE; + out->task = task; + if (h->n_sk == 0) return RXC_HOST_E_STATE; + int rc = host_seal(h); + if (rc != RXC_HOST_OK) return rc; + SrRequirement q = host_requirement(); + RxcResult r; + memset(&r, 0, sizeof r); + int rr = rx_compose_run(h->c, task, &q, NULL, now_us, &r); + out->run_rc = rr; + refresh_info(h); + if (rr != RX_OK) return RXC_HOST_E_RUN; + out->outcome = r.outcome; + out->committed = r.outcome == RXC_OUT_COMMITTED; + out->n_branches = r.n_alternatives; + out->branches_reclaimed = r.reclaimed; + out->winner = r.winner; + if (r.winner < r.n_alternatives && r.winner < RXC_K) { + uint32_t id = r.route[r.winner].chosen.skill_id; + if (id >= RXC_HOST_SKILL_ID0 && id - RXC_HOST_SKILL_ID0 < h->n_sk) + out->winner_skill = id - RXC_HOST_SKILL_ID0; + out->result = r.result; + } + for (uint32_t k = 0; k < RXC_K; k++) { + out->cx_candidate[k] = r.cx_candidate[k]; + out->cx_admission[k] = r.cx_admission[k]; + } + out->cx_evidence = r.cx_evidence; + out->cx_promotion = r.cx_promotion; + memcpy(out->winner_digest, r.winner_digest, 32); + /* The AEGIS verdict as recorded: pass mask from the evidence payload, + * goal record from the candidate claim's tag (rx_compose.h layout). */ + if (r.cx_evidence) { + const CxObject *o = cx_get(&h->c->cx, r.cx_evidence); + const uint64_t *p = o ? cx_payload(&h->c->cx, o) : NULL; + if (p && o->n > RXC_EP_PASSMASK) out->aegis_pass_mask = (uint32_t)p[RXC_EP_PASSMASK]; + if (o) out->cx_goal = o->tag; + } else if (r.cx_candidate[0]) { + const CxObject *o = cx_get(&h->c->cx, r.cx_candidate[0]); + if (o) out->cx_goal = o->tag; + } + rx_compose_record_digest(&h->c->cx, out->record_digest); + return RXC_HOST_OK; +} + +int rxc_host_recall(RxcHost *h, uint64_t subject, RxcHostRecord *out, uint32_t max, + uint32_t *n_filled, uint64_t *n_total) { + if (n_filled) *n_filled = 0; + if (n_total) *n_total = 0; + if (!h || (max && !out)) return RXC_HOST_E_ARG; + int rc = host_seal(h); + if (rc != RXC_HOST_OK) return rc; + if (subject >= h->c->cx.n_subjects) return RXC_HOST_E_ARG; + uint64_t total = cx_count_subject(&h->c->cx, subject); + if (n_total) *n_total = total; + if (max == 0 || total == 0) return RXC_HOST_OK; + CxRecord *tmp = calloc(max, sizeof *tmp); + if (!tmp) return RXC_HOST_E_NOMEM; + uint32_t n = cx_recall(&h->c->cx, subject, 0, UINT64_MAX, NULL, tmp, max); + for (uint32_t i = 0; i < n; i++) fill_record(&out[i], &tmp[i]); + free(tmp); + if (n_filled) *n_filled = n; + for (uint32_t i = 0; i < n; i++) + if (!out[i].verified) return RXC_HOST_E_DIGEST; + return RXC_HOST_OK; +} + +int rxc_host_record(RxcHost *h, uint64_t id, RxcHostRecord *out) { + if (!h || !out) return RXC_HOST_E_ARG; + memset(out, 0, sizeof *out); + int rc = host_seal(h); + if (rc != RXC_HOST_OK) return rc; + CxRecord x; + int xr = cx_recall_id(&h->c->cx, id, &x); + if (xr == CX_ERR_ARG) return RXC_HOST_E_NOT_FOUND; + fill_record(out, &x); + return xr == CX_OK && x.verified ? RXC_HOST_OK : RXC_HOST_E_DIGEST; +} + +int rxc_host_payload(RxcHost *h, uint64_t id, uint64_t *out, uint32_t max) { + if (!h || (max && !out)) return RXC_HOST_E_ARG; + int rc = host_seal(h); + if (rc != RXC_HOST_OK) return rc; + const CxObject *o = cx_get(&h->c->cx, id); + if (!o) return RXC_HOST_E_NOT_FOUND; + const uint64_t *p = cx_payload(&h->c->cx, o); + uint32_t n = o->n < max ? o->n : max; + if (n && p) memcpy(out, p, (size_t)n * sizeof *out); + return (int)o->n; +} + +int rxc_host_info(RxcHost *h, RxcHostInfo *info) { + if (!h || !info) return RXC_HOST_E_ARG; + int rc = host_seal(h); + refresh_info(h); + *info = h->info; + return rc; +} + +void rxc_host_close(RxcHost *h) { + if (!h) return; + if (h->opened) rx_compose_close(h->c); + if (h->admin) aienos_cap_stop(h->admin, h->view); + if (h->cat_init) cq_catalog_free(&h->cat); + pthread_mutex_lock(&g_mu); + if (h->slot < RXC_HOST_MAX_HANDLES && g_slot[h->slot] == h) g_slot[h->slot] = NULL; + pthread_mutex_unlock(&g_mu); + free(h->c); + free(h); +} + +uint32_t rxc_host_abi_layout(uint32_t out[4]) { + if (out) { + out[3] = (uint32_t)sizeof(RxcHostRepair); + out[0] = (uint32_t)sizeof(RxcHostInfo); + out[1] = (uint32_t)sizeof(RxcHostResult); + out[2] = (uint32_t)sizeof(RxcHostRecord); + } + return RXC_HOST_ABI_VERSION; +} + +/* ---- host records (cut 2) ---------------------------------------------------- */ + +static void pack_bytes(uint64_t *w, const uint8_t *b, size_t len) { + for (size_t i = 0; i < len; i++) w[i / 8] |= (uint64_t)b[i] << (8 * (i % 8)); +} + +static void words_of_digest(const uint8_t d[32], uint64_t *w) { + memset(w, 0, 4 * sizeof *w); + pack_bytes(w, d, 32); +} + +int rxc_host_note(RxcHost *h, uint32_t note, const uint64_t links[4], const uint8_t *bytes, + size_t len, uint64_t *out_id) { + if (out_id) *out_id = 0; + if (!h || !bytes || len == 0 || len > RXC_HOST_NOTE_MAX || !out_id) return RXC_HOST_E_ARG; + CxHeader hd; + memset(&hd, 0, sizeof hd); + switch (note) { + case RXC_HOST_NOTE_CONSTRAINT: + hd.cls = CX_CLAIM; hd.kind = CX_K_CLAIM; break; + case RXC_HOST_NOTE_AUTHORIZATION: + hd.cls = CX_EVIDENCE; hd.kind = CX_K_ADMISSION; hd.protect = CX_PROT_AUTHORITY; break; + case RXC_HOST_NOTE_EFFECT: + hd.cls = CX_EVIDENCE; hd.kind = CX_K_EVIDENCE_REF; hd.protect = CX_PROT_EFFECT_RECEIPT; break; + default: + return RXC_HOST_E_ARG; + } + int rc = host_seal(h); + if (rc != RXC_HOST_OK) return rc; + hd.subject = RXC_HOST_SUBJECT_HOST; + hd.tag = note; + if (links) { + for (uint32_t i = 0; i < 4; i++) { + if (links[i] > h->c->cx.n) return RXC_HOST_E_NOT_FOUND; /* provenance must exist */ + hd.links[i] = links[i]; + } + } + uint32_t n = (uint32_t)(RXC_HOST_NP_BYTES + (len + 7) / 8); + uint64_t *p = calloc(n, sizeof *p); + if (!p) return RXC_HOST_E_NOMEM; + uint8_t d[32]; + sha256_hash(bytes, len, d); + p[RXC_HOST_NP_LEN] = len; + words_of_digest(d, p + RXC_HOST_NP_SHA); + pack_bytes(p + RXC_HOST_NP_BYTES, bytes, len); + /* The composition's own writer: the attached single-writer link, under + * the World mutex, exactly as rx_compose's cx_put writes its records. */ + hd.t = rx_cortex_next_t_in(h->c->world, &h->c->cx); + uint64_t id = 0; + int ar = rx_cortex_append_in(h->c->world, &h->c->cx, &hd, p, n, &id); + free(p); + refresh_info(h); + if (ar != RX_OK || id == 0) return RXC_HOST_E_RUN; + *out_id = id; + return RXC_HOST_OK; +} + +/* ---- operator repair (cut 2) ------------------------------------------------- */ + +static int recover_trial_skill(void *ctx, uint64_t task, uint64_t *result) { + (void)ctx; (void)task; (void)result; + return 1; /* never runs: the trial only opens */ +} + +int rxc_host_recover(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, + RxcHostRepair *out) { + if (!out) return RXC_HOST_E_ARG; + memset(out, 0, sizeof *out); + if (!dir || !*dir || !root || strlen(dir) >= 200 - 16) return RXC_HOST_E_ARG; + AienMachineId self; + if (aien_mid_derive((uint8_t)root_kind, root, root_len, &self) != AIEN_MID_OK) + return RXC_HOST_E_IDENTITY; + char p[256]; + snprintf(p, sizeof p, "%s/machine.id", dir); + AienMachineId stored; + if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) + return RXC_HOST_E_IDENTITY; /* only the home's own machine repairs it */ + snprintf(p, sizeof p, "%s/cortex.cx", dir); + struct stat st; + if (stat(p, &st) != 0) return RXC_HOST_OK; /* no journal: nothing to repair */ + const uint64_t old_size = (uint64_t)st.st_size; + + /* Keep the tail bytes before anything is cut (the cut goes into the + * repair record, so nothing leaves the journal unrecorded). */ + uint64_t keep_n = old_size < RXC_HOST_REPAIR_KEEP_MAX ? old_size : RXC_HOST_REPAIR_KEEP_MAX; + uint8_t *tail = malloc(keep_n ? keep_n : 1); + if (!tail) return RXC_HOST_E_NOMEM; + int fd = open(p, O_RDONLY | O_CLOEXEC); + if (fd < 0 || pread(fd, tail, keep_n, (off_t)(old_size - keep_n)) != (ssize_t)keep_n) { + if (fd >= 0) close(fd); + free(tail); + return RXC_HOST_E_OPEN; + } + close(fd); + + CxStore probe; + memset(&probe, 0, sizeof probe); + int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); + if (prc == CX_OK) cx_close(&probe); + else if (prc == CX_ERR_TORN) out->tail_torn = 1; + else { free(tail); return RXC_HOST_E_OPEN; } + + /* The writer open: takes the journal lock (a live handle on this home + * makes it fail: RXC_HOST_E_OPEN) and, if torn, truncates the one + * incomplete trailing record. */ + CxStore cx; + memset(&cx, 0, sizeof cx); + if (cx_open(&cx, p, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) { + free(tail); + return RXC_HOST_E_OPEN; + } + if (stat(p, &st) != 0) { cx_close(&cx); free(tail); return RXC_HOST_E_OPEN; } + const uint64_t new_size = (uint64_t)st.st_size; + const uint64_t kept = cx.n; + out->cut_lo = new_size; + out->cut_hi = old_size; + out->partial_records = out->tail_torn ? 1u : 0u; + out->records_kept = kept; + + JsHome home; + memset(&home, 0, sizeof home); + aien_mid_to_slot(&self, home.machine); + home.locality = JS_HOME_LOCAL; + const JsRealizer *rz[1] = { &RXC_REALIZER }; + JsSpace js; + memset(&js, 0, sizeof js); + snprintf(p, sizeof p, "%s/jspace", dir); + if (js_space_open(&js, p, rz, 1, NULL, &home) != JS_OK) { + cx_close(&cx); + free(tail); + return RXC_HOST_E_OPEN; + } + uint8_t an[48]; + uint64_t A = 0; + if (js_space_anchor(&js, an)) { + out->anchor_present = 1; + for (int i = 0; i < 8; i++) A |= (uint64_t)an[i] << (8 * i); + memcpy(out->anchor_head, an + 8, 32); + } + out->anchor_records = A; + int rc = RXC_HOST_OK; + if (A > 0 && A <= kept) { + const CxObject *o = cx_get(&cx, A); + if (!o || memcmp(o->digest, an + 8, 32) != 0) { + rc = RXC_HOST_E_REPLAY; /* a different history, not a cut tail */ + goto done; + } + } + if (!out->tail_torn && A <= kept) goto done; /* whole and anchored: nothing to do */ + + const uint64_t present = kept + out->partial_records; + out->dropped_records = (present > A ? present : A) - kept; + out->cause = out->tail_torn ? CX_ERR_TORN : RX_ERR_REPLAY; + const uint64_t cut = old_size - new_size; + if (cut <= keep_n) { + out->cut_bytes_kept = cut; + sha256_hash(tail + (keep_n - cut), (size_t)cut, out->cut_sha256); + } + { + uint32_t n = (uint32_t)(RXC_HOST_RP_BYTES + (out->cut_bytes_kept + 7) / 8); + uint64_t *w = calloc(n, sizeof *w); + if (!w) { rc = RXC_HOST_E_NOMEM; goto done; } + w[RXC_HOST_RP_CUT_LO] = out->cut_lo; + w[RXC_HOST_RP_CUT_HI] = out->cut_hi; + w[RXC_HOST_RP_PARTIAL] = out->partial_records; + w[RXC_HOST_RP_ANCHOR] = A; + w[RXC_HOST_RP_KEPT] = kept; + w[RXC_HOST_RP_DROPPED] = out->dropped_records; + w[RXC_HOST_RP_CAUSE] = (uint64_t)(int64_t)out->cause; + words_of_digest(out->cut_sha256, w + RXC_HOST_RP_CUT_SHA); + words_of_digest(out->anchor_head, w + RXC_HOST_RP_ANCHOR_HEAD); + w[RXC_HOST_RP_KEPT_BYTES] = out->cut_bytes_kept; + if (out->cut_bytes_kept) + pack_bytes(w + RXC_HOST_RP_BYTES, tail + (keep_n - out->cut_bytes_kept), + (size_t)out->cut_bytes_kept); + CxHeader hd; + memset(&hd, 0, sizeof hd); + hd.cls = CX_EVIDENCE; + hd.kind = CX_K_ADMISSION; + hd.subject = RXC_HOST_SUBJECT_HOST; + hd.tag = RXC_HOST_TAG_REPAIR_TAIL; + hd.protect = CX_PROT_COMMIT_RECEIPT; + hd.links[0] = kept; /* the newest record kept (0 = none) */ + hd.t = cx.n + 1; /* the unattached writer, as rx_compose's cx_put */ + uint64_t id = 0; + int ar = cx_append(&cx, &hd, w, n, &id); + free(w); + if (ar != CX_OK) { rc = RXC_HOST_E_RUN; goto done; } + out->event_id = id; + } + /* Re-anchor the checkpoint on the repaired journal (rx_compose's + * commit_js layout: count LE | head digest | flags = 1). */ + { + uint8_t a[48] = { 0 }; + uint64_t n = cx.n; + for (int i = 0; i < 8; i++) a[i] = (uint8_t)(n >> (8 * i)); + memcpy(a + 8, cx.obj[n - 1].digest, 32); + a[40] = 1; + js_space_set_anchor(&js, a); + if (js_space_commit(&js) != JS_OK) { rc = RXC_HOST_E_OPEN; goto done; } + } + out->repaired = 1; +done: + js_space_destroy(&js); + cx_close(&cx); + free(tail); + if (rc == RXC_HOST_OK && out->repaired) { + /* Trial open: does the composition accept the repaired home? */ + RxcHost *t = NULL; + RxcHostInfo ti; + rc = rxc_host_open(dir, root_kind, root, root_len, 0, 0, &t, &ti); + if (rc == RXC_HOST_OK) { + rxc_host_register_skill(t, "rxc.recover.trial", NULL, 1, recover_trial_skill, NULL); + rc = rxc_host_info(t, &ti); + out->open_rc = ti.open_rc; + out->opens = rc == RXC_HOST_OK; + out->rolled_back = ti.rolled_back; + out->recovered_completed = ti.recovered_completed; + rxc_host_close(t); + } + } + return rc; +} diff --git a/src/runtime/rxc_host_abi.h b/src/runtime/rxc_host_abi.h new file mode 100644 index 000000000..542f467d0 --- /dev/null +++ b/src/runtime/rxc_host_abi.h @@ -0,0 +1,288 @@ +/* + * rxc_host_abi.h -- NEXT-PHASE-1 cut 1a: a thin C ABI over COMPOSITION-2 + * (rx_compose) for a host program in another language (the sovereign-core + * aien-omega-compose crate). + * + * A FACADE, nothing more: open = rx_compose_open on a fresh AIENOS authority, + * run = rx_compose_run, recall = cx_recall / cx_recall_id on the composition's + * own Cortex journal (/cortex.cx), close = rx_compose_close. No new + * scheduler, store or receipt format. The record of a run is the ordinary + * COMPOSITION-2 Cortex record (rx_compose.h): candidate claims, the AEGIS + * verification evidence, one promotion, one admission per loser. + * + * Pointer-light: one opaque handle, plain fixed-size structs, no pointer is + * kept from the caller except the skill/verify ctx it registers. + * + * Lifecycle: + * rxc_host_open identity check + Cortex torn-tail probe; nothing runs yet + * rxc_host_register_skill (0..RXC_HOST_MAX_SKILLS times) + * first rxc_host_run / rxc_host_recall / rxc_host_info: the Skill graph is + * built and the composition opens (OLD-or-NEW recovery). Registration + * closes here (RXC_HOST_E_STATE afterwards). + * rxc_host_close + * + * Skills. A Skill is a host callback; the composition calls it on a World + * worker thread with the run's task handle (the 64-bit `task` given to + * rxc_host_run, which the host maps to its own goal text). It returns 0 and + * a nonzero 64-bit result (a proposal handle the host can resolve), or + * nonzero to report failure. Up to RXC_K (2) Skills compete per goal; two + * may run at once, so the callback must be thread-safe. Every Skill is + * pinned to a 32-byte procedure digest (default: sha256 of its name). + * + * Verification. The AEGIS verifier enforces a contract on (task, result). + * With a verify callback the host decides (1 = meets the contract); without + * one the contract is "result != 0". The callback runs on a World worker. + * + * Bounds: RXC_HOST_MAX_HANDLES open handles per process (the composition + * contract and Skill functions take no user pointer, so each handle owns a + * fixed slot of trampolines). One handle per directory (the Cortex journal + * takes an exclusive writer lock; a second open of the same dir from any + * process fails at the first run with RXC_HOST_E_OPEN). + * + * Torn journal tail (cut 2: strict by default, repair only on request). + * rxc_host_open probes /cortex.cx read-only. If the journal ends + * inside a record it refuses with RXC_HOST_E_TORN (info->tail_torn = 1) and + * leaves every byte on disk as it was. A journal cut at a record boundary + * inside the committed prefix opens, and the first run/recall/info refuses + * with RXC_HOST_E_REPLAY (open_rc = RX_ERR_REPLAY: the journal is behind the + * J-Space checkpoint anchor). Neither refusal is permanent: the operator + * calls rxc_host_recover, which truncates the torn record + * (CX_OPEN_REPAIR_TAIL), keeps the cut bytes inside an explicit repair + * record (RXC_HOST_TAG_REPAIR_TAIL: byte range, dropped record count, sha256 + * of the cut, the old anchor), re-anchors the J-Space checkpoint on the + * repaired journal, and returns the home to a state rxc_host_open accepts. + * rx_compose_open then answers any composition record the cut interrupted + * (rollback / recovered admissions, RxcHostInfo.rolled_back and + * recovered_completed). No open ever cuts the journal by itself. + * + * Host records (cut 2). rxc_host_note appends one operator record (a + * constraint, an authorization, an effect receipt) through the + * composition's own Cortex writer (rx_cortex_append_in on the attached + * link, exactly as rx_compose writes its candidates), on subject + * RXC_HOST_SUBJECT_HOST (0 = no World object), between runs. + * + * Production only: this file is part of build/librx_compose.a, which is + * built without AIEN_TEST_BUILD / RXC_TEST_HOOKS (mk/rx_compose_lib.mk). + */ +#ifndef RXC_HOST_ABI_H +#define RXC_HOST_ABI_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define RXC_HOST_ABI_VERSION 2u /* 2: strict torn refusal, recover, note */ +#define RXC_HOST_MAX_HANDLES 4u +#define RXC_HOST_MAX_SKILLS 2u /* = RXC_K */ +#define RXC_HOST_NAME_MAX 64u +#define RXC_HOST_NONE 0xFFFFFFFFu /* winner: none */ + +enum { + RXC_HOST_OK = 0, + RXC_HOST_E_ARG = -1, + RXC_HOST_E_IDENTITY = -2, /* machine.id in dir names another machine, or bad root */ + RXC_HOST_E_TORN = -3, /* journal ends inside a record (CX_ERR_TORN): rxc_host_recover */ + RXC_HOST_E_OPEN = -4, /* rx_compose_open refused (see RxcHostInfo.open_rc) */ + RXC_HOST_E_FULL = -5, /* RXC_HOST_MAX_HANDLES / RXC_HOST_MAX_SKILLS reached */ + RXC_HOST_E_STATE = -6, /* wrong lifecycle phase (registration closed, no Skill) */ + RXC_HOST_E_RUN = -7, /* rx_compose_run returned an error (RxcHostResult.run_rc) */ + RXC_HOST_E_NOMEM = -8, + RXC_HOST_E_NOT_FOUND = -9, /* recall: no such record */ + RXC_HOST_E_DIGEST = -10, /* recall: stored digest does not match the record */ + RXC_HOST_E_REPLAY = -11 /* journal behind / unlike the J-Space anchor, or a recorded state + is not durable (RX_ERR_REPLAY): rxc_host_recover */ +}; + +/* rxc_host_open flags: none defined in ABI 2 (must be 0). The cut-1 flag + * RXC_HOST_OPEN_REFUSE_TORN is now the only behaviour. */ + +/* Machine root kinds (= AIEN_MID_ROOT_*). */ +enum { RXC_HOST_ROOT_PROVISIONED = 1, RXC_HOST_ROOT_HARDWARE = 2 }; + +/* Cortex subjects of the composition record (= RXC_CX_SUBJECT(slot)). */ +#define RXC_HOST_SUBJECT_GOAL 1ull +#define RXC_HOST_SUBJECT_STATE 5ull /* candidates, evidence, promotion, admissions */ +#define RXC_HOST_SUBJECT_HOST 0ull /* host records (rxc_host_note, repair): no World object */ + +/* Host record tags (subject RXC_HOST_SUBJECT_HOST). */ +enum { + RXC_HOST_TAG_REPAIR_TAIL = 1, /* EVIDENCE / CX_K_ADMISSION: the cut admitted by recover */ + RXC_HOST_NOTE_CONSTRAINT = 2, /* CLAIM / CX_K_CLAIM: an operator constraint */ + RXC_HOST_NOTE_AUTHORIZATION = 3, /* EVIDENCE / CX_K_ADMISSION: an approval for one effect */ + RXC_HOST_NOTE_EFFECT = 4 /* EVIDENCE / CX_K_EVIDENCE_REF: an effect receipt */ +}; +/* Note payload: [0] byte length, [1..5) sha256 of the bytes (LE words), then + * the bytes packed little-endian, 8 per word. */ +#define RXC_HOST_NOTE_MAX 16384u +#define RXC_HOST_NP_LEN 0u +#define RXC_HOST_NP_SHA 1u +#define RXC_HOST_NP_BYTES 5u +/* Repair payload (RXC_HOST_TAG_REPAIR_TAIL). */ +enum { + RXC_HOST_RP_CUT_LO = 0, /* journal byte range cut: [lo, hi) */ + RXC_HOST_RP_CUT_HI, + RXC_HOST_RP_PARTIAL, /* incomplete trailing records truncated (0 or 1) */ + RXC_HOST_RP_ANCHOR, /* records the J-Space anchor named (0 = no anchor) */ + RXC_HOST_RP_KEPT, /* complete records kept */ + RXC_HOST_RP_DROPPED, /* records lost: max(kept + partial, anchor) - kept */ + RXC_HOST_RP_CAUSE, /* CX_ERR_TORN or RX_ERR_REPLAY (as int64) */ + RXC_HOST_RP_CUT_SHA, /* 4 words: sha256 of the cut bytes */ + RXC_HOST_RP_ANCHOR_HEAD = RXC_HOST_RP_CUT_SHA + 4, /* 4 words: old anchor head digest */ + RXC_HOST_RP_KEPT_BYTES = RXC_HOST_RP_ANCHOR_HEAD + 4, /* cut bytes stored below */ + RXC_HOST_RP_BYTES /* the cut bytes, packed LE */ +}; +#define RXC_HOST_REPAIR_KEEP_MAX 65536u + +/* Composition outcomes (= RXC_OUT_*). */ +enum { + RXC_HOST_OUT_COMMITTED = 1, RXC_HOST_OUT_NO_WINNER = 2, RXC_HOST_OUT_NOT_COMMITTED = 3, + RXC_HOST_OUT_NOT_DURABLE = 4, RXC_HOST_OUT_RECORD_FAILED = 5 +}; + +typedef struct RxcHost RxcHost; + +/* 0 + *result (nonzero) = proposal; nonzero return = the Skill failed. */ +typedef int (*RxcHostSkillFn)(void *ctx, uint64_t task, uint64_t *result); +/* 1 = result meets the contract for task, 0 = it does not. */ +typedef int (*RxcHostVerifyFn)(void *ctx, uint64_t task, uint64_t result); + +typedef struct { + uint32_t abi_version; /* RXC_HOST_ABI_VERSION */ + uint8_t machine_root; /* RXC_HOST_ROOT_* */ + uint8_t machine_id[32]; /* AienMachineId.id: identical across restarts */ + uint32_t machine_id_was_stored; /* 1: dir already held machine.id (and it matched) */ + uint32_t tail_torn; /* 1: cortex.cx ended inside a record at open */ + uint32_t opened; /* 1: the composition is open (first run/recall/info) */ + int32_t open_rc; /* rx_compose_open return (0 = RX_OK) */ + uint64_t records; /* Cortex records now */ + uint64_t recovered_record; /* Cortex id naming the recovered state (0 = genesis) */ + uint32_t rolled_back; /* newer records refused at open (rollback admissions) */ + uint32_t recovered_completed; /* composition records completed at open */ + uint32_t anchor_unknown; /* 1: no Cortex anchor in the checkpoint (audit only) */ + uint32_t n_skills; +} RxcHostInfo; + +typedef struct { + int32_t run_rc; /* rx_compose_run return (0 = RX_OK) */ + int32_t outcome; /* RXC_HOST_OUT_* */ + uint32_t committed; /* 1 iff outcome == COMMITTED (NEW committed, durable) */ + uint32_t n_branches; /* staged J-Space branches forked (= routed alternatives) */ + uint32_t branches_reclaimed; /* staged branches reclaimed at settle */ + uint32_t winner; /* index of the winning alternative, RXC_HOST_NONE = none */ + uint32_t winner_skill; /* host Skill index of the winner, RXC_HOST_NONE = none */ + uint32_t aegis_pass_mask; /* bit k: alternative k met the contract (AEGIS verdict) */ + uint64_t task; + uint64_t result; /* the committed result (winner's), 0 if none */ + uint64_t cx_goal; /* Cortex id of the goal record (0 if none) */ + uint64_t cx_candidate[2]; /* Cortex ids, 0 = none */ + uint64_t cx_evidence; /* AEGIS verification evidence */ + uint64_t cx_promotion; + uint64_t cx_admission[2]; + uint8_t winner_digest[32]; /* J-Space content digest of the committed branch */ + uint8_t record_digest[32]; /* rx_compose_record_digest after the run */ +} RxcHostResult; + +typedef struct { + uint64_t id; + uint32_t cls, kind; /* CxClass, CX_K_* */ + uint64_t subject, t, generation, tag; + uint64_t links[4]; + uint32_t n_payload; /* payload words (read with rxc_host_payload) */ + uint32_t verified; /* 1: digest recomputed and matched */ + uint8_t digest[32]; +} RxcHostRecord; + +/* What rxc_host_recover found and did. */ +typedef struct { + uint32_t repaired; /* 1: journal cut and/or anchor reset, repair record written */ + uint32_t tail_torn; /* 1: journal ended inside a record */ + uint32_t anchor_present; /* 1: the checkpoint held a Cortex anchor */ + int32_t cause; /* CX_ERR_TORN (-8), RX_ERR_REPLAY (-21), 0 = nothing to do */ + uint32_t opens; /* 1: a trial open of the composition succeeded after */ + int32_t open_rc; /* that trial open's rx_compose_open return */ + uint32_t rolled_back; /* trial open: newer state records answered with rollback */ + uint32_t recovered_completed; /* trial open: interrupted composition records completed */ + uint64_t cut_lo, cut_hi; /* journal bytes cut: [cut_lo, cut_hi) */ + uint64_t partial_records; /* incomplete trailing records truncated */ + uint64_t anchor_records; /* records the anchor named before the repair */ + uint64_t records_kept; /* complete records kept (an unchanged prefix) */ + uint64_t dropped_records; /* records lost to the cut */ + uint64_t event_id; /* Cortex id of the repair record (0 = none) */ + uint64_t cut_bytes_kept; /* cut bytes stored in the repair record */ + uint8_t cut_sha256[32]; + uint8_t anchor_head[32]; /* the old anchor's head digest */ +} RxcHostRepair; + +/* Open (or create) the composition home `dir` (made 0700 if absent) for + * the machine derived from (root_kind, root[0..root_len)). Identity: a dir + * that already holds machine.id must hold this machine (RXC_HOST_E_IDENTITY + * otherwise). `info` (may be NULL) is filled on success and on + * RXC_HOST_E_TORN / RXC_HOST_E_IDENTITY. `flags` must be 0. */ +int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, + uint64_t session, uint32_t flags, RxcHost **out, RxcHostInfo *info); + +/* Register a Skill (before the first run/recall/info). `name` is 1..63 + * bytes; `digest32` pins the procedure (NULL = sha256(name)); `cost` ranks + * it (lower first). Returns the Skill index (0..) or RXC_HOST_E_*. */ +int rxc_host_register_skill(RxcHost *h, const char *name, const uint8_t *digest32, uint64_t cost, + RxcHostSkillFn fn, void *ctx); + +/* Set the AEGIS contract callback (before the first run; NULL = result != 0). */ +int rxc_host_set_verify(RxcHost *h, RxcHostVerifyFn fn, void *ctx); + +/* One goal through the whole COMPOSITION-2 path. `task` must be nonzero. + * `now_us` is the run's clock (route liveness). RXC_HOST_OK with out filled + * (check out->outcome), or RXC_HOST_E_* (out->run_rc holds the cause). */ +int rxc_host_run(RxcHost *h, uint64_t task, uint64_t now_us, RxcHostResult *out); + +/* Records of `subject`, oldest first, each digest checked: fills up to + * `max`, *n_total = how many exist (may exceed max). */ +int rxc_host_recall(RxcHost *h, uint64_t subject, RxcHostRecord *out, uint32_t max, + uint32_t *n_filled, uint64_t *n_total); + +/* One record by Cortex id (RXC_HOST_E_NOT_FOUND / RXC_HOST_E_DIGEST). */ +int rxc_host_record(RxcHost *h, uint64_t id, RxcHostRecord *out); + +/* Payload words of record `id`: copies up to `max`, returns the count or <0. */ +int rxc_host_payload(RxcHost *h, uint64_t id, uint64_t *out, uint32_t max); + +/* Current state (opens the composition if not yet open). */ +int rxc_host_info(RxcHost *h, RxcHostInfo *info); + +/* Append one host record (RXC_HOST_NOTE_*) through the composition's Cortex + * writer, between runs. `bytes` (1..RXC_HOST_NOTE_MAX) is the record's text + * or receipt; `links` (may be NULL) names up to 4 related Cortex ids (0 = + * none). *out_id = the new record's id. Opens the composition if needed. */ +int rxc_host_note(RxcHost *h, uint32_t note, const uint64_t links[4], const uint8_t *bytes, + size_t len, uint64_t *out_id); + +/* Operator repair of a home that rxc_host_open refused (RXC_HOST_E_TORN) or + * whose composition refused to open (RXC_HOST_E_REPLAY). No handle may be + * open on `dir` (the journal writer lock; RXC_HOST_E_OPEN otherwise). + * Identity is checked as at open. Idempotent: a home whose journal is + * whole and agrees with its anchor is left untouched (out->repaired = 0). + * A journal whose record at the anchored count differs from the anchor is + * not a tail cut: refused (RXC_HOST_E_REPLAY), nothing written. + * After a repair, recover opens the composition once (a trial open, as + * rxc_host_open + info would) and reports it: out->opens = 1, or + * RXC_HOST_E_REPLAY with out->open_rc when the cut removed a committed + * state record (a whole run lost at a record boundary: J-Space holds a + * state no surviving record names, which rx_compose refuses to adopt or to + * overwrite). The repair record is kept either way. */ +int rxc_host_recover(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, + RxcHostRepair *out); + +/* Layout check for bindings: out[0..4) = sizeof RxcHostInfo, RxcHostResult, + * RxcHostRecord, RxcHostRepair; returns RXC_HOST_ABI_VERSION. */ +uint32_t rxc_host_abi_layout(uint32_t out[4]); + +/* Close the composition and its authority; frees the handle. NULL is a no-op. */ +void rxc_host_close(RxcHost *h); + +#ifdef __cplusplus +} +#endif +#endif /* RXC_HOST_ABI_H */ diff --git a/tests/.crumb b/tests/.crumb index 508d059b7..ae0938300 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -253,8 +253,8 @@ "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { - "digest": "810c8881a76b9617e39a887e006c69f51301e96ebcdda35478a7060fc8788b75", - "last_commit": "f816473", + "digest": "27b7f9eb01e2612c581e23eaa9d3d9fccf5faa6b38dbb7399d8edfb47c2af868", + "last_commit": "df6bc1c", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -283,15 +283,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:cc7551dc7594896ade97e5a5f78205cd93aad4878acd6e114b6b188e02ff10e6", - "compiled_at": "2026-10-06T01:57:59Z", + "children_root": "sha256:81a445b3b2b430db307f071883ae531b21fa110d26db79e5106608c4094b8f4b", + "compiled_at": "2026-10-06T02:24:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:03e780fc46936d8b534e4992f9d87e3ce95711a40fdfe0128a96328410e3af5b", + "digest": "sha256:02f1cf99b38d0c0eac5b9e7ab560e1611571922ee3a5ce46fa3440266397ac92", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 812, - "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", - "source_tree": "sha256:c05002c05a6afef20cc155f921d19c6041975d629317e083ea3a91a2f568ec7d" + "files": 813, + "generated_at_commit": "df6bc1cd3e0fbb70807274858bd734eb677be72c", + "source_tree": "sha256:4079d5c49623d1f71203b5e643b338c2e876a52e5f9edf5ea308debb806dd557" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index a90aa9223..b2ff84133 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T00:23:42Z", + "compiled_at": "2026-10-06T01:18:22Z", "compiler": "crumb-compile/1", - "digest": "sha256:810c8881a76b9617e39a887e006c69f51301e96ebcdda35478a7060fc8788b75", + "digest": "sha256:27b7f9eb01e2612c581e23eaa9d3d9fccf5faa6b38dbb7399d8edfb47c2af868", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 60, - "generated_at_commit": "fa18e58a86a7402de785e07044f5f444a1dcca3c", - "source_tree": "sha256:702ddece6dd8b6ca6319e8e6624783ccee2efc24dc7a911b12a9ca47d90449f0" + "files": 61, + "generated_at_commit": "55f11ad10e21f9f2dd49649a95da0a85f93ce4df", + "source_tree": "sha256:ff21d2b5c9620f87a3176d7197f211a68f9cf0078be46069f9b13a860b490093" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rxc_host_abi_test.c b/tests/runtime/rxc_host_abi_test.c new file mode 100644 index 000000000..e615f291a --- /dev/null +++ b/tests/runtime/rxc_host_abi_test.c @@ -0,0 +1,495 @@ +/* + * rxc_host_abi_test.c -- NEXT-PHASE-1 cut 1a: the host ABI facade over + * COMPOSITION-2, through build/librx_compose.a only (no test hooks). + * + * T1 open a fresh home, register one Skill that records its task handle, + * run one goal: committed, AEGIS pass, Cortex record ids present + * T2 close, REOPEN the same dir: same AienMachineId, recall returns the + * same record ids with the same digests (restart + recall); a second + * goal commits after the restart + * T3 another machine is refused on that home (identity) + * T4 containment: only machine.id, cortex.cx and jspace* on disk (staged + * J-Space branches leave no file) + * T5 torn tail (cut 2): the default open refuses a torn journal + * (RXC_HOST_E_TORN) or one cut behind its anchor (RXC_HOST_E_REPLAY), + * stably and without writing; rxc_host_recover repairs, records the cut + * (byte range, dropped count, cause) and re-anchors; the next open + * succeeds with the old records an unchanged prefix and a goal commits + * T6 recover is refused while a handle holds the home + * T7 host records (constraint, authorization, effect) through the + * composition writer, recalled with the same ids after restart + */ +#include "runtime/rxc_host_abi.h" + +#include +#include +#include +#include +#include +#include +#include + +static int fails, checks; +#define CHECK(cond, ...) do { checks++; if (!(cond)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); printf("\n"); } } while (0) + +static _Atomic uint64_t seen_task, calls; +#define PROPOSE(t) ((t) * 3u + 1u) + +static int skill(void *ctx, uint64_t task, uint64_t *result) { + (void)ctx; + atomic_store(&seen_task, task); + atomic_fetch_add(&calls, 1); + *result = PROPOSE(task); + return 0; +} +static int verify(void *ctx, uint64_t task, uint64_t result) { + (void)ctx; + return result == PROPOSE(task); +} + +static const uint8_t ROOT[] = "np1-host-abi-test-machine"; +static const uint8_t OTHER[] = "np1-some-other-machine"; + +static void hex8(const uint8_t *d, char *o) { + static const char *h = "0123456789abcdef"; + for (int i = 0; i < 8; i++) { o[2 * i] = h[d[i] >> 4]; o[2 * i + 1] = h[d[i] & 15]; } + o[16] = 0; +} + +static RxcHost *open_home(const char *dir, uint32_t flags, RxcHostInfo *info, int *rc_out) { + RxcHost *h = NULL; + int rc = rxc_host_open(dir, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, 0x5E55ull, flags, + &h, info); + if (rc_out) *rc_out = rc; + if (rc != RXC_HOST_OK) return NULL; + int k = rxc_host_register_skill(h, "np1.echo-proposal", NULL, 10, skill, NULL); + CHECK(k == 0, "register_skill -> %d", k); + CHECK(rxc_host_set_verify(h, verify, NULL) == RXC_HOST_OK, "set_verify"); + return h; +} + +static uint32_t collect(RxcHost *h, RxcHostRecord *all, uint32_t max) { + RxcHostInfo in; + if (rxc_host_info(h, &in) != RXC_HOST_OK) return 0; + uint32_t n = 0; + for (uint64_t id = 1; id <= in.records && n < max; id++) + if (rxc_host_record(h, id, &all[n]) == RXC_HOST_OK) n++; + CHECK(n == in.records && n > 0, "every record readable (%u of %llu)", n, + (unsigned long long)in.records); + return n; +} + +/* Byte offset where record `id` starts (journal: 32-byte header, then per + * record 13 header words + payload + 4 digest words). */ +static uint64_t record_start(const RxcHostRecord *all, uint32_t nall, uint64_t id) { + uint64_t off = 32; + for (uint32_t i = 0; i < nall && all[i].id < id; i++) off += (17u + all[i].n_payload) * 8u; + return off; +} + +static uint64_t word_digest_nonzero(const uint64_t *w) { return w[0] | w[1] | w[2] | w[3]; } + +/* `all[0..nall)`: every record of the closed home, read just before close. + * Cut the journal to `cut` bytes (torn: inside the last record; boundary: + * at a record start), then: the default open refuses with a named reason + * and leaves the journal untouched, stably; another machine may not + * repair; rxc_host_recover repairs and records the cut; a second recover + * is a no-op; the next open succeeds with records 1..kept an unchanged + * prefix, the repair record readable, and a goal commits. 0 = all held. */ +static int torn_case(const char *home, const char *label, const RxcHostRecord *all, uint32_t nall, + uint64_t cut, int torn, uint64_t next_task, RxcHostRepair *rep_out) { + int f0 = fails, rc; + char cx[400]; + snprintf(cx, sizeof cx, "%s/cortex.cx", home); + struct stat st; + CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size > cut, "%s: journal size", label); + const uint64_t size0 = (uint64_t)st.st_size; + CHECK(truncate(cx, (off_t)cut) == 0, "%s: cut %llu -> %llu bytes", label, + (unsigned long long)size0, (unsigned long long)cut); + + for (int pass = 0; pass < 2; pass++) { /* the refusal is stable */ + RxcHostInfo it, it2; + RxcHost *s = NULL; + int orc = rxc_host_open(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, 0x5E55ull, + 0, &s, &it); + if (torn) { + CHECK(orc == RXC_HOST_E_TORN && s == NULL && it.tail_torn == 1, + "%s: open %d refuses the torn tail -> %d", label, pass, orc); + } else { + CHECK(orc == RXC_HOST_OK && s && !it.tail_torn, "%s: open %d handle -> %d", label, pass, + orc); + CHECK(s && rxc_host_register_skill(s, "np1.echo-proposal", NULL, 10, skill, NULL) == 0, + "%s: register on the refused home", label); + int irc = s ? rxc_host_info(s, &it2) : -99; + CHECK(irc == RXC_HOST_E_REPLAY && it2.open_rc == -21 && !it2.opened, + "%s: open %d refuses a journal behind its anchor (rc %d open_rc %d)", label, pass, + irc, it2.open_rc); + RxcHostResult rq; + int qrc = s ? rxc_host_run(s, next_task, 6000000, &rq) : -99; + CHECK(qrc == RXC_HOST_E_REPLAY, "%s: no run on a refused home -> %d", label, qrc); + rxc_host_close(s); + } + CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size == cut, + "%s: refusal %d left the journal untouched (%lld bytes)", label, pass, + (long long)st.st_size); + } + + RxcHostRepair rep, rep2; + CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, OTHER, sizeof OTHER - 1, &rep) == + RXC_HOST_E_IDENTITY && !rep.repaired, "%s: another machine may not repair", label); + CHECK(stat(cx, &st) == 0 && (uint64_t)st.st_size == cut, "%s: identity refusal wrote nothing", + label); + rc = rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rep); + printf("%s: recover rc %d repaired %u torn %u cause %d cut [%llu,%llu) partial %llu anchor %llu " + "kept %llu dropped %llu event %llu cut_bytes_kept %llu rolled_back %u recovered_completed %u\n", label, rc, rep.repaired, + rep.tail_torn, rep.cause, (unsigned long long)rep.cut_lo, (unsigned long long)rep.cut_hi, + (unsigned long long)rep.partial_records, (unsigned long long)rep.anchor_records, + (unsigned long long)rep.records_kept, (unsigned long long)rep.dropped_records, + (unsigned long long)rep.event_id, (unsigned long long)rep.cut_bytes_kept, rep.rolled_back, + rep.recovered_completed); + CHECK(rc == RXC_HOST_OK && rep.repaired == 1 && rep.event_id == rep.records_kept + 1, + "%s: recover repaired -> %d", label, rc); + CHECK(rep.opens == 1 && rep.open_rc == 0, "%s: recover's trial open succeeded", label); + CHECK(rep.tail_torn == (uint32_t)torn && rep.cause == (torn ? -8 : -21), "%s: cause named", + label); + CHECK(rep.cut_hi == cut && rep.cut_lo <= rep.cut_hi && rep.cut_bytes_kept == rep.cut_hi - rep.cut_lo, + "%s: byte range recorded", label); + CHECK(rep.dropped_records >= 1 && rep.records_kept < nall, "%s: dropped %llu records", label, + (unsigned long long)rep.dropped_records); + CHECK(rep.records_kept + rep.dropped_records == nall || rep.anchor_records < nall, + "%s: kept + dropped = records before the cut (%llu + %llu vs %u)", label, + (unsigned long long)rep.records_kept, (unsigned long long)rep.dropped_records, nall); + if (torn) CHECK(rep.cut_lo == record_start(all, nall, rep.records_kept + 1), + "%s: cut starts at record %llu", label, (unsigned long long)rep.records_kept + 1); + CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rep2) == + RXC_HOST_OK && rep2.repaired == 0, "%s: a second recover is a no-op", label); + + RxcHostInfo ir, ir2; + RxcHost *h = open_home(home, 0, &ir, &rc); + CHECK(h && rc == RXC_HOST_OK && !ir.tail_torn, "%s: open after recover -> %d", label, rc); + if (!h) return -1; + int irc = rxc_host_info(h, &ir2); + printf("%s: after recover info rc %d records %llu rolled_back %u recovered_completed %u\n", + label, irc, (unsigned long long)ir2.records, ir2.rolled_back, ir2.recovered_completed); + CHECK(irc == RXC_HOST_OK && ir2.opened, "%s: the composition opens after recover", label); + int prefix = 1; + for (uint32_t i = 0; i < rep.records_kept && i < nall; i++) { + RxcHostRecord x; + if (rxc_host_record(h, all[i].id, &x) != RXC_HOST_OK || x.id != all[i].id || + memcmp(x.digest, all[i].digest, 32) != 0) { prefix = 0; break; } + } + CHECK(prefix, "%s: records 1..%llu are an unchanged prefix", label, + (unsigned long long)rep.records_kept); + RxcHostRecord ev; + uint64_t w[24]; + CHECK(rxc_host_record(h, rep.event_id, &ev) == RXC_HOST_OK && + ev.subject == RXC_HOST_SUBJECT_HOST && ev.tag == RXC_HOST_TAG_REPAIR_TAIL && + ev.links[0] == rep.records_kept, "%s: repair record %llu in the journal", label, + (unsigned long long)rep.event_id); + int pn = rxc_host_payload(h, rep.event_id, w, 24); + CHECK(pn >= (int)RXC_HOST_RP_BYTES && w[RXC_HOST_RP_CUT_LO] == rep.cut_lo && + w[RXC_HOST_RP_CUT_HI] == rep.cut_hi && w[RXC_HOST_RP_DROPPED] == rep.dropped_records && + (int64_t)w[RXC_HOST_RP_CAUSE] == rep.cause && + (!rep.cut_bytes_kept || word_digest_nonzero(w + RXC_HOST_RP_CUT_SHA)), + "%s: repair payload names range, count, cause", label); + RxcHostResult r3; + rc = rxc_host_run(h, next_task, 5000000 + next_task % 1000, &r3); + CHECK(rc == RXC_HOST_OK && r3.committed, "%s: run after recover -> %d outcome %d", label, rc, + r3.outcome); + rxc_host_close(h); + if (rep_out) *rep_out = rep; + return fails == f0 ? 0 : -1; +} + +#define MAXR 64 +int main(void) { + char dir[256]; + const char *tmp = getenv("TMPDIR"); + snprintf(dir, sizeof dir, "%s/rxc_host_abi.XXXXXX", tmp && *tmp ? tmp : "/tmp"); + if (!mkdtemp(dir)) { perror("mkdtemp"); return 2; } + char home[300]; + snprintf(home, sizeof home, "%s/home", dir); + + uint32_t lay[4]; + CHECK(rxc_host_abi_layout(lay) == RXC_HOST_ABI_VERSION && lay[0] == sizeof(RxcHostInfo) && + lay[1] == sizeof(RxcHostResult) && lay[2] == sizeof(RxcHostRecord) && + lay[3] == sizeof(RxcHostRepair), "abi layout"); + printf("abi layout: info %u result %u record %u repair %u\n", lay[0], lay[1], lay[2], lay[3]); + + /* ---- T1 ---- */ + RxcHostInfo i1; + int rc; + RxcHost *h = open_home(home, 0, &i1, &rc); + CHECK(h && rc == RXC_HOST_OK, "open fresh -> %d", rc); + if (!h) return 1; + CHECK(i1.abi_version == RXC_HOST_ABI_VERSION && !i1.machine_id_was_stored && !i1.tail_torn, + "fresh info"); + uint64_t task1 = 0x4E50310000000001ull; + RxcHostResult r1; + rc = rxc_host_run(h, task1, 1000000, &r1); + CHECK(rc == RXC_HOST_OK, "run 1 -> %d (run_rc %d)", rc, r1.run_rc); + CHECK(r1.outcome == RXC_HOST_OUT_COMMITTED && r1.committed == 1, "run 1 outcome %d", r1.outcome); + CHECK(atomic_load(&seen_task) == task1 && atomic_load(&calls) >= 1, + "Skill saw task %llx", (unsigned long long)atomic_load(&seen_task)); + CHECK(r1.result == PROPOSE(task1), "committed result"); + CHECK(r1.n_branches == 1 && r1.winner == 0 && r1.winner_skill == 0, "branches %u winner %u", + r1.n_branches, r1.winner); + CHECK(r1.aegis_pass_mask & 1u, "AEGIS pass mask %x", r1.aegis_pass_mask); + CHECK(r1.cx_goal && r1.cx_candidate[0] && r1.cx_evidence && r1.cx_promotion, + "cortex ids goal %llu cand %llu ev %llu promo %llu", (unsigned long long)r1.cx_goal, + (unsigned long long)r1.cx_candidate[0], (unsigned long long)r1.cx_evidence, + (unsigned long long)r1.cx_promotion); + uint64_t cited[4] = { r1.cx_goal, r1.cx_candidate[0], r1.cx_evidence, r1.cx_promotion }; + RxcHostRecord cited_rec[4]; + for (int i = 0; i < 4; i++) + CHECK(rxc_host_record(h, cited[i], &cited_rec[i]) == RXC_HOST_OK && cited_rec[i].verified, + "record %llu", (unsigned long long)cited[i]); + static RxcHostRecord before[MAXR], after[MAXR]; + uint32_t nb = 0, na = 0; + uint64_t tb = 0, ta = 0; + CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_STATE, before, MAXR, &nb, &tb) == RXC_HOST_OK && + nb > 0 && nb == tb, "recall before: %u of %llu", nb, (unsigned long long)tb); + RxcHostInfo i1b; + CHECK(rxc_host_info(h, &i1b) == RXC_HOST_OK && i1b.records > 0, "journal has records"); + uint8_t mid[32]; + memcpy(mid, i1b.machine_id, 32); + rxc_host_close(h); + + /* ---- T2 restart + recall ---- */ + RxcHostInfo i2; + h = open_home(home, 0, &i2, &rc); + CHECK(h && rc == RXC_HOST_OK, "reopen -> %d", rc); + if (!h) return 1; + CHECK(i2.machine_id_was_stored && memcmp(i2.machine_id, mid, 32) == 0 && !i2.tail_torn, + "same AienMachineId after restart"); + CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_STATE, after, MAXR, &na, &ta) == RXC_HOST_OK, + "recall after"); + /* Reopen appends (the World's objects come to life again at every open): + * the pre-restart records must be an unchanged PREFIX of the recall. */ + CHECK(na >= nb, "recall count %u vs %u", na, nb); + int same = na >= nb; + for (uint32_t i = 0; same && i < nb; i++) + same = after[i].id == before[i].id && memcmp(after[i].digest, before[i].digest, 32) == 0 && + after[i].verified; + CHECK(same, "recall after restart returns the same ids and digests"); + for (uint32_t i = nb; i < na; i++) + printf("appended at reopen: id %llu kind 0x%x tag %llu\n", (unsigned long long)after[i].id, + after[i].kind, (unsigned long long)after[i].tag); + for (int i = 0; i < 4; i++) { + RxcHostRecord x; + CHECK(rxc_host_record(h, cited[i], &x) == RXC_HOST_OK && + memcmp(x.digest, cited_rec[i].digest, 32) == 0, "cited record %llu after restart", + (unsigned long long)cited[i]); + } + RxcHostInfo i2b; + rxc_host_info(h, &i2b); + CHECK(i2b.recovered_record != 0 && i2b.rolled_back == 0, "recovered state record %llu", + (unsigned long long)i2b.recovered_record); + RxcHostResult r2; + uint64_t task2 = 0x4E50310000000002ull; + rc = rxc_host_run(h, task2, 2000000, &r2); + CHECK(rc == RXC_HOST_OK && r2.committed && r2.result == PROPOSE(task2), + "run 2 after restart -> %d outcome %d", rc, r2.outcome); + + /* ---- T3 identity ---- */ + RxcHost *o = NULL; + RxcHostInfo io; + int orc = rxc_host_open(home, RXC_HOST_ROOT_PROVISIONED, OTHER, sizeof OTHER - 1, 1, 0, &o, &io); + CHECK(orc == RXC_HOST_E_IDENTITY && o == NULL, "other machine refused -> %d", orc); + rxc_host_close(h); + + /* ---- T4 containment ---- */ + DIR *d = opendir(home); + int stray = 0, nfiles = 0; + struct dirent *e; + while (d && (e = readdir(d))) { + if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; + nfiles++; + if (strcmp(e->d_name, "machine.id") && strcmp(e->d_name, "cortex.cx") && + strncmp(e->d_name, "jspace", 6)) { + stray++; + printf("stray file: %s\n", e->d_name); + } + } + if (d) closedir(d); + CHECK(stray == 0 && nfiles >= 3, "home holds only machine.id, cortex.cx, jspace* (%d files)", + nfiles); + + /* ---- T5 torn tail, strict by default, repaired on request ---- + * (a) a goal commits and its own tail is torn (the coordinator's case); + * (b) the torn record lies past the J-Space anchor (written at reopen); + * (c) the journal is cut exactly at a record boundary inside the + * anchored prefix (cx_open sees a shorter valid journal). */ + static RxcHostRecord all[256]; + uint32_t nall; + RxcHostRepair rp; + h = open_home(home, 0, NULL, &rc); + CHECK(h != NULL, "T5a open -> %d", rc); + if (!h) return 1; + RxcHostResult rb; + CHECK(rxc_host_run(h, 0x4E50310000000005ull, 7000000, &rb) == RXC_HOST_OK && rb.committed, + "T5a run"); + nall = collect(h, all, 256); + rxc_host_close(h); + { + struct stat s5; + char cx5[400]; + snprintf(cx5, sizeof cx5, "%s/cortex.cx", home); + CHECK(stat(cx5, &s5) == 0, "T5a stat"); + CHECK(torn_case(home, "T5a torn after a run", all, nall, (uint64_t)s5.st_size - 5, 1, + 0x4E50310000000006ull, &rp) == 0, "T5a held"); + } + + h = open_home(home, 0, NULL, &rc); + CHECK(h != NULL, "T5b open -> %d", rc); + if (!h) return 1; + nall = collect(h, all, 256); + rxc_host_close(h); + h = open_home(home, 0, NULL, &rc); /* this reopen appends past the anchor */ + if (!h) return 1; + nall = collect(h, all, 256); + rxc_host_close(h); + { + struct stat s5; + char cx5[400]; + snprintf(cx5, sizeof cx5, "%s/cortex.cx", home); + CHECK(stat(cx5, &s5) == 0, "T5b stat"); + CHECK(torn_case(home, "T5b torn past anchor", all, nall, (uint64_t)s5.st_size - 5, 1, + 0x4E50310000000007ull, &rp) == 0, "T5b held"); + } + + h = open_home(home, 0, NULL, &rc); + CHECK(h != NULL, "T5c open -> %d", rc); + if (!h) return 1; + CHECK(rxc_host_run(h, 0x4E50310000000008ull, 8000000, &rb) == RXC_HOST_OK && rb.committed, + "T5c run"); + nall = collect(h, all, 256); + rxc_host_close(h); + CHECK(torn_case(home, "T5c cut at a record boundary", all, nall, + record_start(all, nall, all[nall - 1].id), 0, 0x4E50310000000009ull, &rp) == 0, + "T5c held"); + + /* (d) a whole run's records cut at a boundary (fresh home): everything + * the last goal wrote, its state commit included, is gone while + * J-Space holds NEW. MEASURED limit: recover records the cut and + * reports that the composition still refuses (it neither adopts a + * state no record names nor overwrites committed J-Space history); + * the refusal is named and stable and the kept prefix is unchanged. */ + { + char home2[320]; + snprintf(home2, sizeof home2, "%s/home2", dir); + h = open_home(home2, 0, NULL, &rc); + CHECK(h != NULL, "T5d open -> %d", rc); + if (!h) return 1; + CHECK(rxc_host_run(h, 0x4E5031000000000Bull, 8500000, &rb) == RXC_HOST_OK && rb.committed, + "T5d run 1"); + uint32_t npre = collect(h, all, 256); + CHECK(rxc_host_run(h, 0x4E5031000000000Cull, 8600000, &rb) == RXC_HOST_OK && rb.committed, + "T5d run 2"); + nall = collect(h, all, 256); + rxc_host_close(h); + uint64_t cut = record_start(all, nall, npre + 1); + char cx[400]; + snprintf(cx, sizeof cx, "%s/cortex.cx", home2); + static uint8_t keep[1 << 16], now[1 << 16]; + FILE *f = fopen(cx, "rb"); + size_t got = f && cut <= sizeof keep ? fread(keep, 1, cut, f) : 0; + if (f) fclose(f); + CHECK(got == cut && truncate(cx, (off_t)cut) == 0, "T5d cut run 2 (records %u..%u)", npre + 1, + nall); + rc = rxc_host_recover(home2, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rp); + printf("T5d whole run cut: recover rc %d repaired %u cause %d anchor %llu kept %llu dropped %llu " + "event %llu opens %u open_rc %d\n", rc, rp.repaired, rp.cause, + (unsigned long long)rp.anchor_records, (unsigned long long)rp.records_kept, + (unsigned long long)rp.dropped_records, (unsigned long long)rp.event_id, rp.opens, + rp.open_rc); + CHECK(rc == RXC_HOST_E_REPLAY && rp.repaired == 1 && !rp.opens && rp.open_rc == -21 && + rp.event_id == npre + 1 && rp.dropped_records == nall - npre, + "T5d recover records the cut and names the refusal"); + RxcHostInfo i5d; + h = open_home(home2, 0, NULL, &rc); + int qrc = h ? rxc_host_info(h, &i5d) : -99; + CHECK(qrc == RXC_HOST_E_REPLAY, "T5d the refusal is stable and named -> %d", qrc); + rxc_host_close(h); + f = fopen(cx, "rb"); + got = f ? fread(now, 1, cut, f) : 0; + if (f) fclose(f); + CHECK(got == cut && memcmp(keep, now, cut) == 0, "T5d records 1..%u unchanged on disk", npre); + } + + /* ---- T6 recover needs the journal: a live handle blocks it ---- */ + h = open_home(home, 0, NULL, &rc); + CHECK(h != NULL, "T6 open -> %d", rc); + if (!h) return 1; + RxcHostInfo i6; + CHECK(rxc_host_info(h, &i6) == RXC_HOST_OK, "T6 info"); + CHECK(rxc_host_recover(home, RXC_HOST_ROOT_PROVISIONED, ROOT, sizeof ROOT - 1, &rp) == + RXC_HOST_E_OPEN && !rp.repaired, "T6 recover refused while a handle holds the home"); + + /* ---- T7 host records through the composition writer, recalled after restart ---- */ + RxcHostResult r7; + CHECK(rxc_host_run(h, 0x4E5031000000000Aull, 9000000, &r7) == RXC_HOST_OK && r7.committed, + "T7 run"); + static const char C7[] = "constraint: change only files under the workspace"; + static const char A7[] = "authorize: write notes.txt (expected approval 1 of 1)"; + static const char E7[] = "effect: wrote notes.txt sha256=..."; + uint64_t c7 = 0, a7 = 0, e7 = 0; + uint64_t lk[4] = { r7.cx_promotion, r7.cx_evidence, 0, 0 }; + CHECK(rxc_host_note(h, RXC_HOST_NOTE_CONSTRAINT, NULL, (const uint8_t *)C7, sizeof C7 - 1, &c7) == + RXC_HOST_OK && c7, "T7 constraint note %llu", (unsigned long long)c7); + lk[2] = c7; + CHECK(rxc_host_note(h, RXC_HOST_NOTE_AUTHORIZATION, lk, (const uint8_t *)A7, sizeof A7 - 1, &a7) == + RXC_HOST_OK && a7 > c7, "T7 authorization note %llu", (unsigned long long)a7); + lk[3] = a7; + CHECK(rxc_host_note(h, RXC_HOST_NOTE_EFFECT, lk, (const uint8_t *)E7, sizeof E7 - 1, &e7) == + RXC_HOST_OK && e7 > a7, "T7 effect note %llu", (unsigned long long)e7); + uint64_t bad[4] = { 1u << 30, 0, 0, 0 }; + uint64_t x7 = 0; + CHECK(rxc_host_note(h, RXC_HOST_NOTE_EFFECT, bad, (const uint8_t *)E7, 3, &x7) == + RXC_HOST_E_NOT_FOUND && x7 == 0, "T7 a link to a missing record is refused"); + CHECK(rxc_host_note(h, 99, NULL, (const uint8_t *)E7, 3, &x7) == RXC_HOST_E_ARG, + "T7 unknown note kind refused"); + static RxcHostRecord hb[MAXR], ha[MAXR]; + uint32_t nhb = 0, nha = 0; + uint64_t thb = 0, tha = 0; + CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_HOST, hb, MAXR, &nhb, &thb) == RXC_HOST_OK, + "T7 recall host subject"); + RxcHostInfo i7; + rxc_host_info(h, &i7); + rxc_host_close(h); + h = open_home(home, 0, NULL, &rc); + CHECK(h != NULL, "T7 reopen -> %d", rc); + if (!h) return 1; + RxcHostInfo i7b; + CHECK(rxc_host_info(h, &i7b) == RXC_HOST_OK && memcmp(i7b.machine_id, mid, 32) == 0, + "T7 same AienMachineId after restart"); + CHECK(rxc_host_recall(h, RXC_HOST_SUBJECT_HOST, ha, MAXR, &nha, &tha) == RXC_HOST_OK && + nha == nhb, "T7 recall after restart: %u host records (were %u)", nha, nhb); + int same7 = nha == nhb; + for (uint32_t i = 0; same7 && i < nha; i++) + same7 = ha[i].id == hb[i].id && !memcmp(ha[i].digest, hb[i].digest, 32) && ha[i].verified; + CHECK(same7, "T7 host records keep ids and digests across restart"); + uint64_t pw[RXC_HOST_NP_BYTES + 16]; + int pn = rxc_host_payload(h, c7, pw, RXC_HOST_NP_BYTES + 16); + char txt[129] = { 0 }; + if (pn > (int)RXC_HOST_NP_BYTES && pw[RXC_HOST_NP_LEN] < sizeof txt) + for (uint64_t i = 0; i < pw[RXC_HOST_NP_LEN]; i++) + txt[i] = (char)(pw[RXC_HOST_NP_BYTES + i / 8] >> (8 * (i % 8))); + CHECK(strcmp(txt, C7) == 0, "T7 constraint text round-trips: '%s'", txt); + RxcHostRecord ra; + CHECK(rxc_host_record(h, a7, &ra) == RXC_HOST_OK && ra.tag == RXC_HOST_NOTE_AUTHORIZATION && + ra.links[0] == r7.cx_promotion && ra.links[2] == c7, "T7 authorization cites its ids"); + printf("T7 host records: constraint %llu authorization %llu effect %llu; host subject %u\n", + (unsigned long long)c7, (unsigned long long)a7, (unsigned long long)e7, nha); + rxc_host_close(h); + + char m[17]; + hex8(mid, m); + printf("machine %s... records-before-restart %u\n", m, nb); + printf("rxc_host_abi_test: %d checks, %d failures: %s\n", checks, fails, fails ? "FAIL" : "PASS"); + char cmd[400]; + snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir); + if (!fails && system(cmd) != 0) printf("note: could not remove %s\n", dir); + return fails ? 1 : 0; +} ```