Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/runtime/rxc_host_abi.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / runtime / rxc_host_abi.c` 710 lines ยท 27372 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * rxc_host_abi.c -- the host ABI facade over COMPOSITION-2 (rxc_host_abi.h). 19 * 20 * Everything here forwards to existing calls: aien_mid_* (identity), 21 * cx_open read-only (torn-tail probe), cq_* / sr_register_skill (the Skill 22 * graph, built exactly as the COMPOSITION-2 tests build it), aienos_cap_* 23 * (authority), rx_compose_open/run/close, cx_recall/cx_recall_id. The only 24 * state it adds is the per-handle slot that lets a ctx-less RxcContract and 25 * AgSkillFn reach the host callback. 26 */ 27 #include "runtime/rxc_host_abi.h" 28 29 #include "runtime/aien_machine_id.h" 30 #include "runtime/aienos_cap.h" 31 #include "runtime/rx_capq.h" 32 #include "runtime/rx_compose.h" 33 #include "runtime/rx_cortex.h" 34 #include "runtime/rx_cortex_record.h" 35 #include "runtime/rx_graph.h" 36 #include "runtime/rx_skillroute.h" 37 #include "sha256.h" 38 #include "omega_path_check.h" 39 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 49 _Static_assert(RXC_HOST_MAX_SKILLS == RXC_K, "one host Skill per composition alternative"); 50 _Static_assert(RXC_HOST_SUBJECT_GOAL == RXC_CX_SUBJECT(RXC_SLOT_GOAL), "goal subject"); 51 _Static_assert(RXC_HOST_SUBJECT_STATE == RXC_CX_SUBJECT(RXC_SLOT_STATE), "state subject"); 52 _Static_assert((int)RXC_HOST_OUT_COMMITTED == (int)RXC_OUT_COMMITTED && (int)RXC_HOST_OUT_RECORD_FAILED == 53 (int)RXC_OUT_RECORD_FAILED, "outcome codes"); 54 _Static_assert((int)RXC_HOST_ROOT_PROVISIONED == (int)AIEN_MID_ROOT_PROVISIONED && 55 (int)RXC_HOST_ROOT_HARDWARE == (int)AIEN_MID_ROOT_HARDWARE, "root kinds"); 56 _Static_assert(RXC_HOST_NONE == RXC_NONE, "no winner"); 57 _Static_assert(RXC_HOST_SUBJECT_HOST < RXC_CX_SUBJECT(0), "host records stay off composition subjects"); 58 59 #define RXC_HOST_OP 1u /* the one semantic operation every host Skill provides */ 60 #define RXC_HOST_SKILL_ID0 1u /* Skill ids: RXC_HOST_SKILL_ID0 + index */ 61 #define RXC_HOST_N_WORKERS 2u 62 63 typedef struct { 64 char name[RXC_HOST_NAME_MAX]; 65 RxcHostSkillFn fn; 66 void *ctx; 67 uint64_t cost; 68 uint8_t digest[32]; 69 } HostSkill; 70 71 struct RxcHost { 72 uint32_t slot; 73 char dir[200]; 74 uint64_t session; 75 AienMachineId self; 76 AienMachineId slots[8]; 77 AienMachineIndex ix; 78 CqCatalog cat; 79 int cat_init; 80 AgSkillTable skills; 81 SrRouter router; 82 HostSkill sk[RXC_HOST_MAX_SKILLS]; 83 uint32_t n_sk; 84 RxcHostVerifyFn verify; 85 void *verify_ctx; 86 AienosCapAdmin *admin; 87 AienosCapView *view; 88 RxCompose *c; 89 uint32_t wait_ms; /* 0 = default; applied when the composition opens */ 90 int opened; 91 RxcHostInfo info; 92 }; 93 94 /* ---- slots: ctx-less contract / Skill functions reach their handle ----- */ 95 96 static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; 97 static RxcHost *volatile g_slot[RXC_HOST_MAX_HANDLES]; 98 99 static int host_contract(uint32_t slot, uint64_t input, uint64_t result) { 100 RxcHost *h = g_slot[slot]; 101 if (!h) return 0; 102 if (h->verify) return h->verify(h->verify_ctx, input, result) == 1; 103 return result != 0; 104 } 105 106 static uint64_t host_skill(uint32_t slot, uint32_t k, const uint64_t *in, uint32_t n, int *failed) { 107 RxcHost *h = g_slot[slot]; 108 uint64_t result = 0; 109 *failed = 1; 110 if (!h || k >= h->n_sk || !h->sk[k].fn || n == 0) return 0; 111 if (h->sk[k].fn(h->sk[k].ctx, in[0], &result) == 0 && result != 0) *failed = 0; 112 return *failed ? 0 : result; 113 } 114 115 #define RXC_HOST_TRAMPOLINES(S) \ 116 static int host_contract_##S(uint64_t i, uint64_t r) { return host_contract(S, i, r); } \ 117 static uint64_t host_skill_##S##_0(const uint64_t *in, uint32_t n, uint32_t a, int *f) { \ 118 (void)a; return host_skill(S, 0, in, n, f); } \ 119 static uint64_t host_skill_##S##_1(const uint64_t *in, uint32_t n, uint32_t a, int *f) { \ 120 (void)a; return host_skill(S, 1, in, n, f); } 121 RXC_HOST_TRAMPOLINES(0) 122 RXC_HOST_TRAMPOLINES(1) 123 RXC_HOST_TRAMPOLINES(2) 124 RXC_HOST_TRAMPOLINES(3) 125 _Static_assert(RXC_HOST_MAX_HANDLES == 4u, "one RXC_HOST_TRAMPOLINES line per handle slot"); 126 127 static const RxcContract g_contract[RXC_HOST_MAX_HANDLES] = { 128 host_contract_0, host_contract_1, host_contract_2, host_contract_3 }; 129 static const AgSkillFn g_skillfn[RXC_HOST_MAX_HANDLES][RXC_HOST_MAX_SKILLS] = { 130 { host_skill_0_0, host_skill_0_1 }, { host_skill_1_0, host_skill_1_1 }, 131 { host_skill_2_0, host_skill_2_1 }, { host_skill_3_0, host_skill_3_1 } }; 132 133 /* ---- helpers ------------------------------------------------------------- */ 134 135 static void fill_identity(RxcHostInfo *info, const AienMachineId *m) { 136 info->machine_root = m->root; 137 memcpy(info->machine_id, m->id, sizeof info->machine_id); 138 } 139 140 static void fill_record(RxcHostRecord *r, const CxRecord *x) { 141 memset(r, 0, sizeof *r); 142 r->id = x->hdr.id; 143 r->cls = x->hdr.cls; 144 r->kind = x->hdr.kind; 145 r->subject = x->hdr.subject; 146 r->t = x->hdr.t; 147 r->generation = x->hdr.generation; 148 r->tag = x->hdr.tag; 149 for (uint32_t i = 0; i < 4; i++) r->links[i] = x->hdr.links[i]; 150 r->n_payload = x->hdr.n; 151 r->verified = x->verified ? 1u : 0u; 152 memcpy(r->digest, x->hdr.digest, 32); 153 } 154 155 static CqEntry host_provide(uint32_t cap, uint32_t real, uint64_t cost) { 156 CqEntry e; 157 memset(&e, 0, sizeof e); 158 e.capability_id = cap; 159 e.realization_id = real; 160 e.op = RXC_HOST_OP; 161 e.effects = CQ_FX_PURE; 162 e.in_types = 0x1; 163 e.out_types = 0x2; 164 e.confidence_ppm = 900000; 165 e.reliability_ppm = 990000; 166 e.evidence_level = CQ_EV_MEASURED; 167 e.evidence_ref = 0xE000u + cap; 168 e.cost = cost; 169 e.latency_us = 100; 170 e.energy_uj = 100; 171 e.live = CQ_LIVE_AVAILABLE; 172 e.generation = 1; 173 return e; 174 } 175 176 static SrRequirement host_requirement(void) { 177 SrRequirement q; 178 memset(&q, 0, sizeof q); 179 q.need.semantic_operation = RXC_HOST_OP; 180 q.need.accepted_input_types = ~0ull; 181 q.need.effect_class = 0x1FF; 182 q.need.authority_ceiling.resource_hi = UINT64_MAX; 183 q.need.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; 184 q.t.n_order = 1; 185 q.t.order[0] = CQ_DIM_COST; 186 q.t.k = CQ_MAX_K; 187 q.local_only = 1; 188 return q; 189 } 190 191 static void refresh_info(RxcHost *h) { 192 h->info.opened = (uint32_t)h->opened; 193 h->info.n_skills = h->n_sk; 194 if (h->opened) { 195 h->info.records = h->c->cx.n; 196 h->info.recovered_record = h->c->recovered_record; 197 h->info.rolled_back = h->c->rolled_back; 198 h->info.recovered_completed = h->c->recovered_completed; 199 h->info.anchor_unknown = h->c->anchor_unknown ? 1u : 0u; 200 } 201 } 202 203 /* Build the Skill graph, start the authority, open the composition. */ 204 static int host_seal(RxcHost *h) { 205 if (h->opened) return RXC_HOST_OK; 206 if (h->info.open_rc != 0) /* a failed open is final */ 207 return h->info.open_rc == RX_ERR_REPLAY ? RXC_HOST_E_REPLAY : RXC_HOST_E_OPEN; 208 aien_mid_index_init(&h->ix, h->slots, 8); 209 if (cq_catalog_init_canonical(&h->cat, &h->ix, &h->self, 8, 8) != CQ_OK) return RXC_HOST_E_NOMEM; 210 h->cat_init = 1; 211 uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC); 212 if (cq_op_define(&h->cat, RXC_HOST_OP, 0, all) != CQ_OK) return RXC_HOST_E_STATE; 213 h->skills.n = h->n_sk; 214 for (uint32_t i = 0; i < h->n_sk; i++) { 215 h->skills.skill[i].id = RXC_HOST_SKILL_ID0 + i; 216 h->skills.skill[i].fn = g_skillfn[h->slot][i]; 217 memcpy(h->skills.skill[i].identity, h->sk[i].digest, 32); 218 SrSkill s; 219 memset(&s, 0, sizeof s); 220 s.skill_id = RXC_HOST_SKILL_ID0 + i; 221 s.version = 1; 222 memcpy(s.digest, h->sk[i].digest, 32); 223 s.machine = h->cat.self_machine; 224 CqEntry e = host_provide(1 + i, 70 + i, h->sk[i].cost); 225 if (sr_register_skill(&h->cat, &s, &e, 1) != SR_OK) return RXC_HOST_E_STATE; 226 } 227 if (cq_catalog_build(&h->cat) != CQ_OK) return RXC_HOST_E_STATE; 228 h->router = (SrRouter){ &h->cat, &h->skills }; 229 if (aienos_cap_start(&h->admin, &h->view) != 0) { 230 h->admin = NULL; 231 h->info.open_rc = RX_ERR_AUTHORITY; 232 return RXC_HOST_E_OPEN; 233 } 234 int rc = rx_compose_open(h->c, h->dir, &h->self, h->session, &h->router, 235 g_contract[h->slot], h->admin, h->view, RXC_HOST_N_WORKERS); 236 h->info.open_rc = rc; 237 if (rc != RX_OK) { 238 aienos_cap_stop(h->admin, h->view); 239 h->admin = NULL; 240 return rc == RX_ERR_IDENTITY ? RXC_HOST_E_IDENTITY 241 : rc == RX_ERR_REPLAY ? RXC_HOST_E_REPLAY 242 : RXC_HOST_E_OPEN; 243 } 244 if (h->wait_ms) rx_compose_set_wait_ms(h->c, h->wait_ms); 245 h->opened = 1; 246 refresh_info(h); 247 return RXC_HOST_OK; 248 } 249 250 /* ---- public API ------------------------------------------------------------ */ 251 252 int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, 253 uint64_t session, uint32_t flags, RxcHost **out, RxcHostInfo *info) { 254 if (out) *out = NULL; 255 if (info) memset(info, 0, sizeof *info); 256 if (!dir || !*dir || !root || !out || strlen(dir) >= 200 - 16 || 257 flags != 0) 258 return RXC_HOST_E_ARG; 259 AienMachineId self; 260 if (aien_mid_derive((uint8_t)root_kind, root, root_len, &self) != AIEN_MID_OK) 261 return RXC_HOST_E_IDENTITY; 262 263 RxcHostInfo inf; 264 memset(&inf, 0, sizeof inf); 265 inf.abi_version = RXC_HOST_ABI_VERSION; 266 fill_identity(&inf, &self); 267 /* Check before mkdir (src/omega_path_check.h): a refused dir is an argument error and creates nothing. */ 268 char checked[200]; 269 if (omega_path_copy(checked, sizeof checked, dir) != 0) return RXC_HOST_E_ARG; 270 if (mkdir(checked, 0700) && errno != EEXIST) return RXC_HOST_E_ARG; 271 272 /* Identity first (rx_compose_open checks it again). */ 273 char p[256]; 274 snprintf(p, sizeof p, "%s/machine.id", dir); 275 if (access(p, F_OK) == 0) { 276 AienMachineId stored; 277 if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) { 278 if (info) *info = inf; 279 return RXC_HOST_E_IDENTITY; 280 } 281 inf.machine_id_was_stored = 1; 282 } 283 /* Torn-tail probe: read-only, no lock, no repair. */ 284 snprintf(p, sizeof p, "%s/cortex.cx", dir); 285 if (access(p, F_OK) == 0) { 286 CxStore probe; 287 memset(&probe, 0, sizeof probe); 288 int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); 289 if (prc == CX_OK) { 290 inf.records = probe.n; 291 cx_close(&probe); 292 } else if (prc == CX_ERR_TORN) { 293 inf.tail_torn = 1; 294 } else { 295 inf.open_rc = prc; 296 if (info) *info = inf; 297 return RXC_HOST_E_OPEN; 298 } 299 } 300 /* Strict: a torn tail is refused, never cut here (rxc_host_recover). */ 301 if (inf.tail_torn) { 302 if (info) *info = inf; 303 return RXC_HOST_E_TORN; 304 } 305 306 RxcHost *h = calloc(1, sizeof *h); 307 if (!h) return RXC_HOST_E_NOMEM; 308 h->c = calloc(1, sizeof *h->c); 309 if (!h->c) { free(h); return RXC_HOST_E_NOMEM; } 310 pthread_mutex_lock(&g_mu); 311 uint32_t slot = RXC_HOST_MAX_HANDLES; 312 for (uint32_t i = 0; i < RXC_HOST_MAX_HANDLES; i++) 313 if (!g_slot[i]) { slot = i; break; } 314 if (slot < RXC_HOST_MAX_HANDLES) g_slot[slot] = h; 315 pthread_mutex_unlock(&g_mu); 316 if (slot == RXC_HOST_MAX_HANDLES) { 317 free(h->c); 318 free(h); 319 return RXC_HOST_E_FULL; 320 } 321 h->slot = slot; 322 snprintf(h->dir, sizeof h->dir, "%s", dir); 323 h->session = session; 324 h->self = self; 325 h->info = inf; 326 *out = h; 327 if (info) *info = h->info; 328 return RXC_HOST_OK; 329 } 330 331 int rxc_host_register_skill(RxcHost *h, const char *name, const uint8_t *digest32, uint64_t cost, 332 RxcHostSkillFn fn, void *ctx) { 333 if (!h || !name || !fn) return RXC_HOST_E_ARG; 334 size_t len = strlen(name); 335 if (len == 0 || len >= RXC_HOST_NAME_MAX) return RXC_HOST_E_ARG; 336 if (h->opened || h->info.open_rc != 0) return RXC_HOST_E_STATE; 337 if (h->n_sk >= RXC_HOST_MAX_SKILLS) return RXC_HOST_E_FULL; 338 HostSkill *s = &h->sk[h->n_sk]; 339 memset(s, 0, sizeof *s); 340 memcpy(s->name, name, len); 341 s->fn = fn; 342 s->ctx = ctx; 343 s->cost = cost; 344 if (digest32) memcpy(s->digest, digest32, 32); 345 else sha256_hash((const uint8_t *)name, len, s->digest); 346 return (int)h->n_sk++; 347 } 348 349 int rxc_host_set_verify(RxcHost *h, RxcHostVerifyFn fn, void *ctx) { 350 if (!h) return RXC_HOST_E_ARG; 351 if (h->opened) return RXC_HOST_E_STATE; 352 h->verify = fn; 353 h->verify_ctx = ctx; 354 return RXC_HOST_OK; 355 } 356 357 int rxc_host_set_wait_ms(RxcHost *h, uint32_t wait_ms) { 358 if (!h) return RX_ERR_ARG; 359 if (wait_ms == 0 || wait_ms > RXC_WAIT_MS_MAX) return RX_ERR_ARG; 360 h->wait_ms = wait_ms; 361 return h->opened ? rx_compose_set_wait_ms(h->c, wait_ms) : RX_OK; 362 } 363 364 int rxc_host_run(RxcHost *h, uint64_t task, uint64_t now_us, RxcHostResult *out) { 365 if (!h || !out || task == 0) return RXC_HOST_E_ARG; 366 memset(out, 0, sizeof *out); 367 out->winner = out->winner_skill = RXC_HOST_NONE; 368 out->task = task; 369 if (h->n_sk == 0) return RXC_HOST_E_STATE; 370 int rc = host_seal(h); 371 if (rc != RXC_HOST_OK) return rc; 372 SrRequirement q = host_requirement(); 373 RxcResult r; 374 memset(&r, 0, sizeof r); 375 int rr = rx_compose_run(h->c, task, &q, NULL, now_us, &r); 376 out->run_rc = rr; 377 refresh_info(h); 378 if (rr != RX_OK) return RXC_HOST_E_RUN; 379 out->outcome = r.outcome; 380 out->committed = r.outcome == RXC_OUT_COMMITTED; 381 out->n_branches = r.n_alternatives; 382 out->branches_reclaimed = r.reclaimed; 383 out->winner = r.winner; 384 if (r.winner < r.n_alternatives && r.winner < RXC_K) { 385 uint32_t id = r.route[r.winner].chosen.skill_id; 386 if (id >= RXC_HOST_SKILL_ID0 && id - RXC_HOST_SKILL_ID0 < h->n_sk) 387 out->winner_skill = id - RXC_HOST_SKILL_ID0; 388 out->result = r.result; 389 } 390 for (uint32_t k = 0; k < RXC_K; k++) { 391 out->cx_candidate[k] = r.cx_candidate[k]; 392 out->cx_admission[k] = r.cx_admission[k]; 393 } 394 out->cx_evidence = r.cx_evidence; 395 out->cx_promotion = r.cx_promotion; 396 memcpy(out->winner_digest, r.winner_digest, 32); 397 /* The AEGIS verdict as recorded: pass mask from the evidence payload, 398 * goal record from the candidate claim's tag (rx_compose.h layout). */ 399 if (r.cx_evidence) { 400 const CxObject *o = cx_get(&h->c->cx, r.cx_evidence); 401 const uint64_t *p = o ? cx_payload(&h->c->cx, o) : NULL; 402 if (p && o->n > RXC_EP_PASSMASK) out->aegis_pass_mask = (uint32_t)p[RXC_EP_PASSMASK]; 403 if (o) out->cx_goal = o->tag; 404 } else if (r.cx_candidate[0]) { 405 const CxObject *o = cx_get(&h->c->cx, r.cx_candidate[0]); 406 if (o) out->cx_goal = o->tag; 407 } 408 rx_compose_record_digest(&h->c->cx, out->record_digest); 409 return RXC_HOST_OK; 410 } 411 412 int rxc_host_recall(RxcHost *h, uint64_t subject, RxcHostRecord *out, uint32_t max, 413 uint32_t *n_filled, uint64_t *n_total) { 414 if (n_filled) *n_filled = 0; 415 if (n_total) *n_total = 0; 416 if (!h || (max && !out)) return RXC_HOST_E_ARG; 417 int rc = host_seal(h); 418 if (rc != RXC_HOST_OK) return rc; 419 if (subject >= h->c->cx.n_subjects) return RXC_HOST_E_ARG; 420 uint64_t total = cx_count_subject(&h->c->cx, subject); 421 if (n_total) *n_total = total; 422 if (max == 0 || total == 0) return RXC_HOST_OK; 423 CxRecord *tmp = calloc(max, sizeof *tmp); 424 if (!tmp) return RXC_HOST_E_NOMEM; 425 uint32_t n = cx_recall(&h->c->cx, subject, 0, UINT64_MAX, NULL, tmp, max); 426 for (uint32_t i = 0; i < n; i++) fill_record(&out[i], &tmp[i]); 427 free(tmp); 428 if (n_filled) *n_filled = n; 429 for (uint32_t i = 0; i < n; i++) 430 if (!out[i].verified) return RXC_HOST_E_DIGEST; 431 return RXC_HOST_OK; 432 } 433 434 int rxc_host_record(RxcHost *h, uint64_t id, RxcHostRecord *out) { 435 if (!h || !out) return RXC_HOST_E_ARG; 436 memset(out, 0, sizeof *out); 437 int rc = host_seal(h); 438 if (rc != RXC_HOST_OK) return rc; 439 CxRecord x; 440 int xr = cx_recall_id(&h->c->cx, id, &x); 441 if (xr == CX_ERR_ARG) return RXC_HOST_E_NOT_FOUND; 442 fill_record(out, &x); 443 return xr == CX_OK && x.verified ? RXC_HOST_OK : RXC_HOST_E_DIGEST; 444 } 445 446 int rxc_host_payload(RxcHost *h, uint64_t id, uint64_t *out, uint32_t max) { 447 if (!h || (max && !out)) return RXC_HOST_E_ARG; 448 int rc = host_seal(h); 449 if (rc != RXC_HOST_OK) return rc; 450 const CxObject *o = cx_get(&h->c->cx, id); 451 if (!o) return RXC_HOST_E_NOT_FOUND; 452 const uint64_t *p = cx_payload(&h->c->cx, o); 453 uint32_t n = o->n < max ? o->n : max; 454 if (n && p) memcpy(out, p, (size_t)n * sizeof *out); 455 return (int)o->n; 456 } 457 458 int rxc_host_info(RxcHost *h, RxcHostInfo *info) { 459 if (!h || !info) return RXC_HOST_E_ARG; 460 int rc = host_seal(h); 461 refresh_info(h); 462 *info = h->info; 463 return rc; 464 } 465 466 void rxc_host_close(RxcHost *h) { 467 if (!h) return; 468 if (h->opened) rx_compose_close(h->c); 469 if (h->admin) aienos_cap_stop(h->admin, h->view); 470 if (h->cat_init) cq_catalog_free(&h->cat); 471 pthread_mutex_lock(&g_mu); 472 if (h->slot < RXC_HOST_MAX_HANDLES && g_slot[h->slot] == h) g_slot[h->slot] = NULL; 473 pthread_mutex_unlock(&g_mu); 474 free(h->c); 475 free(h); 476 } 477 478 uint32_t rxc_host_abi_layout(uint32_t out[4]) { 479 if (out) { 480 out[3] = (uint32_t)sizeof(RxcHostRepair); 481 out[0] = (uint32_t)sizeof(RxcHostInfo); 482 out[1] = (uint32_t)sizeof(RxcHostResult); 483 out[2] = (uint32_t)sizeof(RxcHostRecord); 484 } 485 return RXC_HOST_ABI_VERSION; 486 } 487 488 /* ---- host records (cut 2) ---------------------------------------------------- */ 489 490 static void pack_bytes(uint64_t *w, const uint8_t *b, size_t len) { 491 for (size_t i = 0; i < len; i++) w[i / 8] |= (uint64_t)b[i] << (8 * (i % 8)); 492 } 493 494 static void words_of_digest(const uint8_t d[32], uint64_t *w) { 495 memset(w, 0, 4 * sizeof *w); 496 pack_bytes(w, d, 32); 497 } 498 499 int rxc_host_note(RxcHost *h, uint32_t note, const uint64_t links[4], const uint8_t *bytes, 500 size_t len, uint64_t *out_id) { 501 if (out_id) *out_id = 0; 502 if (!h || !bytes || len == 0 || len > RXC_HOST_NOTE_MAX || !out_id) return RXC_HOST_E_ARG; 503 CxHeader hd; 504 memset(&hd, 0, sizeof hd); 505 switch (note) { 506 case RXC_HOST_NOTE_CONSTRAINT: 507 hd.cls = CX_CLAIM; hd.kind = CX_K_CLAIM; break; 508 case RXC_HOST_NOTE_AUTHORIZATION: 509 hd.cls = CX_EVIDENCE; hd.kind = CX_K_ADMISSION; hd.protect = CX_PROT_AUTHORITY; break; 510 case RXC_HOST_NOTE_EFFECT: 511 hd.cls = CX_EVIDENCE; hd.kind = CX_K_EVIDENCE_REF; hd.protect = CX_PROT_EFFECT_RECEIPT; break; 512 default: 513 return RXC_HOST_E_ARG; 514 } 515 int rc = host_seal(h); 516 if (rc != RXC_HOST_OK) return rc; 517 hd.subject = RXC_HOST_SUBJECT_HOST; 518 hd.tag = note; 519 if (links) { 520 for (uint32_t i = 0; i < 4; i++) { 521 if (links[i] > h->c->cx.n) return RXC_HOST_E_NOT_FOUND; /* provenance must exist */ 522 hd.links[i] = links[i]; 523 } 524 } 525 uint32_t n = (uint32_t)(RXC_HOST_NP_BYTES + (len + 7) / 8); 526 uint64_t *p = calloc(n, sizeof *p); 527 if (!p) return RXC_HOST_E_NOMEM; 528 uint8_t d[32]; 529 sha256_hash(bytes, len, d); 530 p[RXC_HOST_NP_LEN] = len; 531 words_of_digest(d, p + RXC_HOST_NP_SHA); 532 pack_bytes(p + RXC_HOST_NP_BYTES, bytes, len); 533 /* The composition's own writer: the attached single-writer link, under 534 * the World mutex, exactly as rx_compose's cx_put writes its records. */ 535 hd.t = rx_cortex_next_t_in(h->c->world, &h->c->cx); 536 uint64_t id = 0; 537 int ar = rx_cortex_append_in(h->c->world, &h->c->cx, &hd, p, n, &id); 538 free(p); 539 refresh_info(h); 540 if (ar != RX_OK || id == 0) return RXC_HOST_E_RUN; 541 *out_id = id; 542 return RXC_HOST_OK; 543 } 544 545 /* ---- operator repair (cut 2) ------------------------------------------------- */ 546 547 static int recover_trial_skill(void *ctx, uint64_t task, uint64_t *result) { 548 (void)ctx; (void)task; (void)result; 549 return 1; /* never runs: the trial only opens */ 550 } 551 552 int rxc_host_recover(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, 553 RxcHostRepair *out) { 554 if (!out) return RXC_HOST_E_ARG; 555 memset(out, 0, sizeof *out); 556 if (!dir || !*dir || !root || strlen(dir) >= 200 - 16) return RXC_HOST_E_ARG; 557 AienMachineId self; 558 if (aien_mid_derive((uint8_t)root_kind, root, root_len, &self) != AIEN_MID_OK) 559 return RXC_HOST_E_IDENTITY; 560 char p[256]; 561 snprintf(p, sizeof p, "%s/machine.id", dir); 562 AienMachineId stored; 563 if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) 564 return RXC_HOST_E_IDENTITY; /* only the home's own machine repairs it */ 565 snprintf(p, sizeof p, "%s/cortex.cx", dir); 566 struct stat st; 567 if (stat(p, &st) != 0) return RXC_HOST_OK; /* no journal: nothing to repair */ 568 const uint64_t old_size = (uint64_t)st.st_size; 569 570 /* Keep the tail bytes before anything is cut (the cut goes into the 571 * repair record, so nothing leaves the journal unrecorded). */ 572 uint64_t keep_n = old_size < RXC_HOST_REPAIR_KEEP_MAX ? old_size : RXC_HOST_REPAIR_KEEP_MAX; 573 uint8_t *tail = malloc(keep_n ? keep_n : 1); 574 if (!tail) return RXC_HOST_E_NOMEM; 575 int fd = open(p, O_RDONLY | O_CLOEXEC); 576 if (fd < 0 || pread(fd, tail, keep_n, (off_t)(old_size - keep_n)) != (ssize_t)keep_n) { 577 if (fd >= 0) close(fd); 578 free(tail); 579 return RXC_HOST_E_OPEN; 580 } 581 close(fd); 582 583 CxStore probe; 584 memset(&probe, 0, sizeof probe); 585 int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); 586 if (prc == CX_OK) cx_close(&probe); 587 else if (prc == CX_ERR_TORN) out->tail_torn = 1; 588 else { free(tail); return RXC_HOST_E_OPEN; } 589 590 /* The writer open: takes the journal lock (a live handle on this home 591 * makes it fail: RXC_HOST_E_OPEN) and, if torn, truncates the one 592 * incomplete trailing record. */ 593 CxStore cx; 594 memset(&cx, 0, sizeof cx); 595 if (cx_open(&cx, p, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) { 596 free(tail); 597 return RXC_HOST_E_OPEN; 598 } 599 if (stat(p, &st) != 0) { cx_close(&cx); free(tail); return RXC_HOST_E_OPEN; } 600 const uint64_t new_size = (uint64_t)st.st_size; 601 const uint64_t kept = cx.n; 602 out->cut_lo = new_size; 603 out->cut_hi = old_size; 604 out->partial_records = out->tail_torn ? 1u : 0u; 605 out->records_kept = kept; 606 607 JsHome home; 608 memset(&home, 0, sizeof home); 609 aien_mid_to_slot(&self, home.machine); 610 home.locality = JS_HOME_LOCAL; 611 const JsRealizer *rz[1] = { &RXC_REALIZER }; 612 JsSpace js; 613 memset(&js, 0, sizeof js); 614 snprintf(p, sizeof p, "%s/jspace", dir); 615 if (js_space_open(&js, p, rz, 1, NULL, &home) != JS_OK) { 616 cx_close(&cx); 617 free(tail); 618 return RXC_HOST_E_OPEN; 619 } 620 uint8_t an[48]; 621 uint64_t A = 0; 622 if (js_space_anchor(&js, an)) { 623 out->anchor_present = 1; 624 for (int i = 0; i < 8; i++) A |= (uint64_t)an[i] << (8 * i); 625 memcpy(out->anchor_head, an + 8, 32); 626 } 627 out->anchor_records = A; 628 int rc = RXC_HOST_OK; 629 if (A > 0 && A <= kept) { 630 const CxObject *o = cx_get(&cx, A); 631 if (!o || memcmp(o->digest, an + 8, 32) != 0) { 632 rc = RXC_HOST_E_REPLAY; /* a different history, not a cut tail */ 633 goto done; 634 } 635 } 636 if (!out->tail_torn && A <= kept) goto done; /* whole and anchored: nothing to do */ 637 638 const uint64_t present = kept + out->partial_records; 639 out->dropped_records = (present > A ? present : A) - kept; 640 out->cause = out->tail_torn ? CX_ERR_TORN : RX_ERR_REPLAY; 641 const uint64_t cut = old_size - new_size; 642 if (cut <= keep_n) { 643 out->cut_bytes_kept = cut; 644 sha256_hash(tail + (keep_n - cut), (size_t)cut, out->cut_sha256); 645 } 646 { 647 uint32_t n = (uint32_t)(RXC_HOST_RP_BYTES + (out->cut_bytes_kept + 7) / 8); 648 uint64_t *w = calloc(n, sizeof *w); 649 if (!w) { rc = RXC_HOST_E_NOMEM; goto done; } 650 w[RXC_HOST_RP_CUT_LO] = out->cut_lo; 651 w[RXC_HOST_RP_CUT_HI] = out->cut_hi; 652 w[RXC_HOST_RP_PARTIAL] = out->partial_records; 653 w[RXC_HOST_RP_ANCHOR] = A; 654 w[RXC_HOST_RP_KEPT] = kept; 655 w[RXC_HOST_RP_DROPPED] = out->dropped_records; 656 w[RXC_HOST_RP_CAUSE] = (uint64_t)(int64_t)out->cause; 657 words_of_digest(out->cut_sha256, w + RXC_HOST_RP_CUT_SHA); 658 words_of_digest(out->anchor_head, w + RXC_HOST_RP_ANCHOR_HEAD); 659 w[RXC_HOST_RP_KEPT_BYTES] = out->cut_bytes_kept; 660 if (out->cut_bytes_kept) 661 pack_bytes(w + RXC_HOST_RP_BYTES, tail + (keep_n - out->cut_bytes_kept), 662 (size_t)out->cut_bytes_kept); 663 CxHeader hd; 664 memset(&hd, 0, sizeof hd); 665 hd.cls = CX_EVIDENCE; 666 hd.kind = CX_K_ADMISSION; 667 hd.subject = RXC_HOST_SUBJECT_HOST; 668 hd.tag = RXC_HOST_TAG_REPAIR_TAIL; 669 hd.protect = CX_PROT_COMMIT_RECEIPT; 670 hd.links[0] = kept; /* the newest record kept (0 = none) */ 671 hd.t = cx.n + 1; /* the unattached writer, as rx_compose's cx_put */ 672 uint64_t id = 0; 673 int ar = cx_append(&cx, &hd, w, n, &id); 674 free(w); 675 if (ar != CX_OK) { rc = RXC_HOST_E_RUN; goto done; } 676 out->event_id = id; 677 } 678 /* Re-anchor the checkpoint on the repaired journal (rx_compose's 679 * commit_js layout: count LE | head digest | flags = 1). */ 680 { 681 uint8_t a[48] = { 0 }; 682 uint64_t n = cx.n; 683 for (int i = 0; i < 8; i++) a[i] = (uint8_t)(n >> (8 * i)); 684 memcpy(a + 8, cx.obj[n - 1].digest, 32); 685 a[40] = 1; 686 js_space_set_anchor(&js, a); 687 if (js_space_commit(&js) != JS_OK) { rc = RXC_HOST_E_OPEN; goto done; } 688 } 689 out->repaired = 1; 690 done: 691 js_space_destroy(&js); 692 cx_close(&cx); 693 free(tail); 694 if (rc == RXC_HOST_OK && out->repaired) { 695 /* Trial open: does the composition accept the repaired home? */ 696 RxcHost *t = NULL; 697 RxcHostInfo ti; 698 rc = rxc_host_open(dir, root_kind, root, root_len, 0, 0, &t, &ti); 699 if (rc == RXC_HOST_OK) { 700 rxc_host_register_skill(t, "rxc.recover.trial", NULL, 1, recover_trial_skill, NULL); 701 rc = rxc_host_info(t, &ti); 702 out->open_rc = ti.open_rc; 703 out->opens = rc == RXC_HOST_OK; 704 out->rolled_back = ti.rolled_back; 705 out->recovered_completed = ti.recovered_completed; 706 rxc_host_close(t); 707 } 708 } 709 return rc; 710 }