omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / rx_compose_fixture.h
| 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 | * rx_compose_fixture.h -- shared setup for the COMPOSITION-2 tests and gate: |
| 19 | * a canonical machine, a Capability Graph with two digest-pinned local |
| 20 | * Skills for one operation, the contract the AEGIS verifier enforces, and |
| 21 | * open/close of a composition on a fresh Aienux authority. |
| 22 | */ |
| 23 | #ifndef RX_COMPOSE_FIXTURE_H |
| 24 | #define RX_COMPOSE_FIXTURE_H |
| 25 | |
| 26 | #include "runtime/aien_machine_id.h" |
| 27 | #include "runtime/aienos_cap.h" |
| 28 | #include "runtime/rx_capq.h" |
| 29 | #include "runtime/rx_compose.h" |
| 30 | #include "runtime/rx_cortex_record.h" |
| 31 | #include "runtime/rx_graph.h" |
| 32 | #include "runtime/rx_skillroute.h" |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #define FX_NOW_US 1000000ull |
| 39 | enum { FX_OP_SCALE = 1 }; |
| 40 | enum { FX_SKILL_A = 7, FX_SKILL_B = 8 }; /* A is cheaper: ranked first */ |
| 41 | #define FX_SESSION 0x5E55ull |
| 42 | |
| 43 | /* Contract: result = 3 * input + 1. */ |
| 44 | static __attribute__((unused)) int fx_contract(uint64_t input, uint64_t result) { return result == input * 3 + 1; } |
| 45 | |
| 46 | static int fx_a_bad, fx_b_bad; /* make a Skill break the contract */ |
| 47 | |
| 48 | static __attribute__((unused)) uint64_t fx_sk_a(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { |
| 49 | (void)attempt; |
| 50 | *failed = 0; |
| 51 | return (n ? in[0] : 0) * 3 + 1 + (fx_a_bad ? 1 : 0); |
| 52 | } |
| 53 | static __attribute__((unused)) uint64_t fx_sk_b(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { |
| 54 | (void)attempt; |
| 55 | *failed = 0; |
| 56 | return (n ? in[0] : 0) * 3 + 1 + (fx_b_bad ? 5 : 0); |
| 57 | } |
| 58 | |
| 59 | static __attribute__((unused)) AienMachineId fx_mid(uint8_t seed) { |
| 60 | AienMachineId m; |
| 61 | uint8_t root[8] = { 'c', 'o', 'm', 'p', seed, 0, 0, 0 }; |
| 62 | aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, root, sizeof root, &m); |
| 63 | return m; |
| 64 | } |
| 65 | |
| 66 | static __attribute__((unused)) CqEntry fx_provide(uint32_t cap, uint32_t op, uint32_t real, uint64_t cost) { |
| 67 | CqEntry e; |
| 68 | memset(&e, 0, sizeof e); |
| 69 | e.capability_id = cap; |
| 70 | e.realization_id = real; |
| 71 | e.op = op; |
| 72 | e.effects = CQ_FX_PURE; |
| 73 | e.in_types = 0x1; |
| 74 | e.out_types = 0x2; |
| 75 | e.confidence_ppm = 900000; |
| 76 | e.reliability_ppm = 990000; |
| 77 | e.evidence_level = CQ_EV_MEASURED; |
| 78 | e.evidence_ref = 0xE000u + cap; |
| 79 | e.cost = cost; |
| 80 | e.latency_us = 100; |
| 81 | e.energy_uj = 100; |
| 82 | e.live = CQ_LIVE_AVAILABLE; |
| 83 | e.generation = 1; |
| 84 | return e; |
| 85 | } |
| 86 | |
| 87 | static __attribute__((unused)) SrRequirement fx_requirement(void) { |
| 88 | SrRequirement q; |
| 89 | memset(&q, 0, sizeof q); |
| 90 | q.need.semantic_operation = FX_OP_SCALE; |
| 91 | q.need.accepted_input_types = ~0ull; |
| 92 | q.need.effect_class = 0x1FF; |
| 93 | q.need.authority_ceiling.resource_hi = UINT64_MAX; |
| 94 | q.need.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; |
| 95 | q.t.n_order = 1; |
| 96 | q.t.order[0] = CQ_DIM_COST; |
| 97 | q.t.k = CQ_MAX_K; |
| 98 | q.local_only = 1; |
| 99 | return q; |
| 100 | } |
| 101 | |
| 102 | typedef struct { |
| 103 | AienMachineId self; |
| 104 | AienMachineId slots[8]; |
| 105 | AienMachineIndex ix; |
| 106 | CqCatalog cat; |
| 107 | AgSkillTable skills; |
| 108 | SrRouter router; |
| 109 | AienosCapAdmin *admin; |
| 110 | AienosCapView *view; |
| 111 | int owns_authority; /* 1: fx_open started it, fx_close stops it */ |
| 112 | } Fx; |
| 113 | |
| 114 | /* Graph: Skill A (cost 10) and Skill B (cost 40), both local, both pinned |
| 115 | * to the digest of the procedure this machine runs, registered for the |
| 116 | * machine `self` (the living system passes its own provisioned identity; |
| 117 | * the stand-alone tests use fx_mid(1)). Returns 0 on success. */ |
| 118 | static __attribute__((unused)) int fx_init_for(Fx *f, const AienMachineId *self) { |
| 119 | memset(f, 0, sizeof *f); |
| 120 | f->self = *self; |
| 121 | aien_mid_index_init(&f->ix, f->slots, 8); |
| 122 | if (cq_catalog_init_canonical(&f->cat, &f->ix, &f->self, 8, 8) != CQ_OK) return -1; |
| 123 | uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC); |
| 124 | if (cq_op_define(&f->cat, FX_OP_SCALE, 0, all) != CQ_OK) return -2; |
| 125 | f->skills.n = 2; |
| 126 | f->skills.skill[0] = (AgSkill){ FX_SKILL_A, fx_sk_a, { 0xA7, 0x01 } }; |
| 127 | f->skills.skill[1] = (AgSkill){ FX_SKILL_B, fx_sk_b, { 0xB8, 0x02 } }; |
| 128 | for (uint32_t i = 0; i < 2; i++) { |
| 129 | SrSkill s; |
| 130 | memset(&s, 0, sizeof s); |
| 131 | s.skill_id = f->skills.skill[i].id; |
| 132 | s.version = 1; |
| 133 | memcpy(s.digest, f->skills.skill[i].identity, 32); |
| 134 | s.machine = f->cat.self_machine; |
| 135 | CqEntry e = fx_provide(1 + i, FX_OP_SCALE, 70 + i, i ? 40 : 10); |
| 136 | if (sr_register_skill(&f->cat, &s, &e, 1) != SR_OK) return -3; |
| 137 | } |
| 138 | if (cq_catalog_build(&f->cat) != CQ_OK) return -4; |
| 139 | f->router = (SrRouter){ &f->cat, &f->skills }; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static __attribute__((unused)) int fx_init(Fx *f) { |
| 144 | AienMachineId m = fx_mid(1); |
| 145 | return fx_init_for(f, &m); |
| 146 | } |
| 147 | |
| 148 | static __attribute__((unused)) void fx_free(Fx *f) { cq_catalog_free(&f->cat); } |
| 149 | |
| 150 | /* Open on an authority the caller already runs (the living system's Aienux |
| 151 | * admin and view). fx_close then leaves that authority running. */ |
| 152 | static __attribute__((unused)) int fx_open_on(Fx *f, RxCompose *c, const char *dir, |
| 153 | uint32_t n_workers, AienosCapAdmin *admin, |
| 154 | AienosCapView *view) { |
| 155 | f->admin = admin; |
| 156 | f->view = view; |
| 157 | f->owns_authority = 0; |
| 158 | return rx_compose_open(c, dir, &f->self, FX_SESSION, &f->router, fx_contract, admin, view, |
| 159 | n_workers); |
| 160 | } |
| 161 | |
| 162 | static __attribute__((unused)) int fx_open(Fx *f, RxCompose *c, const char *dir, uint32_t n_workers) { |
| 163 | if (aienos_cap_start(&f->admin, &f->view) != 0) return -100; |
| 164 | f->owns_authority = 1; |
| 165 | int rc = rx_compose_open(c, dir, &f->self, FX_SESSION, &f->router, fx_contract, f->admin, |
| 166 | f->view, n_workers); |
| 167 | if (rc != RX_OK) { |
| 168 | aienos_cap_stop(f->admin, f->view); |
| 169 | f->admin = NULL; |
| 170 | } |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | static __attribute__((unused)) void fx_close(Fx *f, RxCompose *c) { |
| 175 | rx_compose_close(c); |
| 176 | if (f->admin && f->owns_authority) aienos_cap_stop(f->admin, f->view); |
| 177 | f->admin = NULL; |
| 178 | } |
| 179 | |
| 180 | static __attribute__((unused)) int fx_run(Fx *f, RxCompose *c, uint64_t input, RxcResult *out) { |
| 181 | (void)f; |
| 182 | SrRequirement q = fx_requirement(); |
| 183 | return rx_compose_run(c, input, &q, NULL, FX_NOW_US, out); |
| 184 | } |
| 185 | |
| 186 | static __attribute__((unused)) uint32_t fx_live_branches(JsSpace *s) { |
| 187 | uint32_t n = 0; |
| 188 | for (uint32_t i = 0; i < s->n_branches; i++) n += s->branches[i] != NULL; |
| 189 | return n; |
| 190 | } |
| 191 | |
| 192 | /* Count objects of `kind` (and tag when tag != UINT64_MAX). */ |
| 193 | static __attribute__((unused)) uint32_t fx_count(const CxStore *s, uint32_t kind, uint64_t tag) { |
| 194 | uint32_t n = 0; |
| 195 | for (uint64_t id = 1; id <= s->n; id++) { |
| 196 | const CxObject *o = cx_get(s, id); |
| 197 | if (o && o->kind == kind && (tag == UINT64_MAX || o->tag == tag)) n++; |
| 198 | } |
| 199 | return n; |
| 200 | } |
| 201 | |
| 202 | static __attribute__((unused)) uint64_t fx_pack(JsBranchRef r) { return js_branch_ref_pack(r); } |
| 203 | |
| 204 | static __attribute__((unused)) void fx_hex(const uint8_t *d, size_t n, char *out) { |
| 205 | static const char *h = "0123456789abcdef"; |
| 206 | for (size_t i = 0; i < n; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } |
| 207 | out[2 * n] = 0; |
| 208 | } |
| 209 | |
| 210 | #endif /* RX_COMPOSE_FIXTURE_H */ |