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_state_projection.c
| 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 | * OMEGA_STATE_PROJECTION: cognition gets a compiled state projection, not |
| 19 | * everything Cortex knows. |
| 20 | * |
| 21 | * Part A. Five representative AIEN operations (explain a regression, verify |
| 22 | * a cost claim, plan the next experiment, a what-if on a hypothetical |
| 23 | * branch, assess a goal with uncertainty), 24 planted-answer histories each. |
| 24 | * Every task runs twice with the same consumer: on the whole store and on |
| 25 | * the projection Omega compiles from the task's CognitiveNeed. Both are |
| 26 | * scored against the planted answer. Measured: bytes, objects, retrieval, |
| 27 | * consumer work, latency, recall precision and the unnecessary-state ratio. |
| 28 | * |
| 29 | * Controls that must fail: remove any one projected item (leave-one-out), |
| 30 | * a naive "newest objects" projection of the same size, a "whole subject" |
| 31 | * projection, and under-specified needs. Protected state asked to be |
| 32 | * summarized, derived or made ephemeral must be refused, for each of the |
| 33 | * five protections. A budget below the pinned state must be refused. |
| 34 | * |
| 35 | * Recoverability: every summary, reference and derived feature is checked |
| 36 | * back against Cortex; every excluded object is classified with one reason |
| 37 | * and checked intact; a tampered source must be caught. |
| 38 | * |
| 39 | * Part B. The real R11 AIEN faculty (rx_aien.o, native Aienux authority) is |
| 40 | * fed one regime's whole lifetime history, and then only the projection for |
| 41 | * "why did this prediction fail". Both are scored against the planted cause. |
| 42 | * |
| 43 | * Part C. Scale: the same explain operation over 16, 64 and 256 subsystems. |
| 44 | */ |
| 45 | #include "runtime/aienos_cap.h" |
| 46 | #include "runtime/rx_aien.h" |
| 47 | #include "runtime/rx_cortex.h" |
| 48 | #include "runtime/rx_omega.h" /* demand and selection object types only */ |
| 49 | #include "runtime/rx_projection.h" |
| 50 | #include "runtime/rx_world.h" |
| 51 | #include "rx_sp_workloads.h" |
| 52 | #include "omega_evidence.h" |
| 53 | #include "sha256.h" |
| 54 | |
| 55 | #include <signal.h> |
| 56 | #include <stdio.h> |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
| 59 | #include <sys/utsname.h> |
| 60 | #include <time.h> |
| 61 | |
| 62 | static int g_checks; |
| 63 | static int g_fail; |
| 64 | |
| 65 | #define CHECK(cond, ...) do { \ |
| 66 | g_checks++; \ |
| 67 | if (!(cond)) { \ |
| 68 | g_fail++; \ |
| 69 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 70 | fprintf(stderr, __VA_ARGS__); \ |
| 71 | fputc('\n', stderr); \ |
| 72 | } \ |
| 73 | } while (0) |
| 74 | |
| 75 | static uint64_t now_ns(void) { |
| 76 | struct timespec ts; |
| 77 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 78 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 79 | } |
| 80 | |
| 81 | #define SEEDS 24u |
| 82 | #define SUBJECTS 64u |
| 83 | #define T_END 5990u |
| 84 | |
| 85 | static int ans_eq(const SpAnswer *a, const SpAnswer *b) { return memcmp(a, b, sizeof *a) == 0; } |
| 86 | |
| 87 | static int cmp_u64(const void *a, const void *b) { |
| 88 | uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; |
| 89 | return x < y ? -1 : x > y; |
| 90 | } |
| 91 | |
| 92 | static uint64_t median(const uint64_t *v, uint32_t n) { |
| 93 | uint64_t tmp[256]; |
| 94 | if (!n || n > 256) return 0; |
| 95 | memcpy(tmp, v, n * sizeof(uint64_t)); |
| 96 | qsort(tmp, n, sizeof(uint64_t), cmp_u64); |
| 97 | return tmp[n / 2]; |
| 98 | } |
| 99 | |
| 100 | /* Reason three times; keep the fastest wall time and the first trace. */ |
| 101 | static uint64_t reason_timed(const SpTask *t, const PjView *v, SpAnswer *out, SpTrace *tr) { |
| 102 | uint64_t best = UINT64_MAX; |
| 103 | for (int k = 0; k < 3; k++) { |
| 104 | SpTrace dummy = { 0, 0, NULL }; |
| 105 | uint64_t t0 = now_ns(); |
| 106 | sp_reason(t, v, out, k == 0 ? tr : &dummy); |
| 107 | uint64_t d = now_ns() - t0; |
| 108 | if (d < best) best = d; |
| 109 | } |
| 110 | return best; |
| 111 | } |
| 112 | |
| 113 | /* ---- Part A ---- */ |
| 114 | |
| 115 | typedef struct { |
| 116 | uint32_t tasks, full_ok, proj_ok, agree; |
| 117 | uint64_t full_bytes[SEEDS], proj_bytes[SEEDS], full_objects[SEEDS], proj_objects[SEEDS]; |
| 118 | uint64_t full_touched[SEEDS], proj_touched[SEEDS], full_words[SEEDS], proj_words[SEEDS]; |
| 119 | uint64_t examined[SEEDS], compile_ns[SEEDS], proj_build_ns[SEEDS], full_build_ns[SEEDS]; |
| 120 | uint64_t full_reason_ns[SEEDS], proj_reason_ns[SEEDS], full_total_ns[SEEDS], proj_total_ns[SEEDS]; |
| 121 | uint64_t full_used, full_provided, proj_used, proj_provided; |
| 122 | uint64_t needed, needed_present; |
| 123 | uint64_t loo_items, loo_necessary, tasks_with_necessary; |
| 124 | uint32_t recentk_ok, subject_ok, underspec_ok, underspec_tasks; |
| 125 | uint64_t recover_checked, recover_fail, tamper_caught, tamper_tried; |
| 126 | uint64_t audit_ok, excluded, excluded_intact, other_branch; |
| 127 | uint64_t by_why[PJ_WHY_END]; |
| 128 | uint64_t by_treatment[PJ_STATE_CLASS_END]; |
| 129 | uint32_t e2e_faster; |
| 130 | } WStats; |
| 131 | |
| 132 | static WStats g_w[W_COUNT]; |
| 133 | static uint32_t g_protect_refusals, g_protect_attempts; |
| 134 | static int g_budget_refused, g_branch_refused, g_chain_ok = 1, g_recall_ok, g_ephemeral_ok; |
| 135 | |
| 136 | static int needed_present(const SpTask *t, const CxStore *s, const StateProjection *p, uint64_t id) { |
| 137 | for (uint32_t i = 0; i < p->n; i++) |
| 138 | if (p->e[i].id == id) return 1; |
| 139 | const CxObject *o = cx_get(s, id); |
| 140 | for (uint32_t i = 0; o && i < p->nf; i++) { |
| 141 | const PjFeature *f = &p->f[i]; |
| 142 | if (f->subject == o->subject && f->cls == o->cls && f->kind == o->kind && f->branch == o->branch && |
| 143 | o->t >= f->t0 && o->t <= f->t1) |
| 144 | return 1; |
| 145 | } |
| 146 | (void)t; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* Remove one item (or one feature) and see whether the answer survives. */ |
| 151 | static void leave_one_out(const SpTask *t, const PjView *v, WStats *w) { |
| 152 | PjView m = *v; |
| 153 | PjItem *items = malloc((v->n ? v->n : 1) * sizeof(PjItem)); |
| 154 | PjFeature *feat = malloc((v->nf ? v->nf : 1) * sizeof(PjFeature)); |
| 155 | uint64_t necessary = 0; |
| 156 | for (uint32_t i = 0; i < v->n + v->nf; i++) { |
| 157 | if (i < v->n) { |
| 158 | memcpy(items, v->items, i * sizeof(PjItem)); |
| 159 | memcpy(items + i, v->items + i + 1, (v->n - i - 1) * sizeof(PjItem)); |
| 160 | m.items = items; |
| 161 | m.n = v->n - 1; |
| 162 | m.feat = v->feat; |
| 163 | m.nf = v->nf; |
| 164 | } else { |
| 165 | uint32_t j = i - v->n; |
| 166 | memcpy(feat, v->feat, j * sizeof(PjFeature)); |
| 167 | memcpy(feat + j, v->feat + j + 1, (v->nf - j - 1) * sizeof(PjFeature)); |
| 168 | m.items = v->items; |
| 169 | m.n = v->n; |
| 170 | m.feat = feat; |
| 171 | m.nf = v->nf - 1; |
| 172 | } |
| 173 | SpAnswer a; |
| 174 | SpTrace tr = { 0, 0, NULL }; |
| 175 | sp_reason(t, &m, &a, &tr); |
| 176 | if (!ans_eq(&a, &t->truth)) necessary++; |
| 177 | } |
| 178 | w->loo_items += v->n + v->nf; |
| 179 | w->loo_necessary += necessary; |
| 180 | if (necessary) w->tasks_with_necessary++; |
| 181 | free(items); |
| 182 | free(feat); |
| 183 | } |
| 184 | |
| 185 | static const CxStore *g_sort_store; |
| 186 | |
| 187 | static int cmp_newest(const void *a, const void *b) { |
| 188 | const CxObject *x = cx_get(g_sort_store, *(const uint64_t *)a), *y = cx_get(g_sort_store, *(const uint64_t *)b); |
| 189 | if (x->t != y->t) return x->t > y->t ? -1 : 1; |
| 190 | return x->id > y->id ? -1 : 1; |
| 191 | } |
| 192 | |
| 193 | /* Naive baseline 1: the newest objects in Cortex, as many bytes as the projection. */ |
| 194 | static int naive_recent(const SpTask *t, CxStore *s, uint64_t budget) { |
| 195 | uint64_t *ids = malloc(s->n * sizeof(uint64_t)); |
| 196 | for (uint64_t i = 0; i < s->n; i++) ids[i] = i + 1; |
| 197 | g_sort_store = s; |
| 198 | qsort(ids, s->n, sizeof(uint64_t), cmp_newest); |
| 199 | uint64_t bytes = 0; |
| 200 | uint32_t k = 0; |
| 201 | while (k < s->n && bytes < budget) { |
| 202 | const CxObject *o = cx_get(s, ids[k++]); |
| 203 | bytes += PJ_HEADER_BYTES + (uint64_t)o->n * PJ_WORD_BYTES; |
| 204 | } |
| 205 | PjView v; |
| 206 | pj_view_ids(s, ids, k, &v); |
| 207 | SpAnswer a; |
| 208 | SpTrace tr = { 0, 0, NULL }; |
| 209 | sp_reason(t, &v, &a, &tr); |
| 210 | pj_view_free(&v); |
| 211 | free(ids); |
| 212 | return ans_eq(&a, &t->truth); |
| 213 | } |
| 214 | |
| 215 | /* Naive baseline 2: everything Cortex has about the subject, all time, in full. */ |
| 216 | static int naive_subject(const SpTask *t, CxStore *s) { |
| 217 | const CxIdList *l = &s->by_subject[t->subject]; |
| 218 | PjView v; |
| 219 | pj_view_ids(s, l->ids, l->n, &v); |
| 220 | SpAnswer a; |
| 221 | SpTrace tr = { 0, 0, NULL }; |
| 222 | sp_reason(t, &v, &a, &tr); |
| 223 | pj_view_free(&v); |
| 224 | return ans_eq(&a, &t->truth); |
| 225 | } |
| 226 | |
| 227 | /* An under-specified need: the operation's need with one requirement weakened. */ |
| 228 | static int underspecified(const SpTask *t, CxStore *s, int *applicable) { |
| 229 | CognitiveNeed n; |
| 230 | sp_need(t, &n); |
| 231 | *applicable = 1; |
| 232 | if (t->workload == W_EXPLAIN) { |
| 233 | for (uint32_t i = 0; i < n.n_fact_types; i++) |
| 234 | if (n.required_fact_types[i].world == PJ_W_DEPENDENCIES) n.required_fact_types[i].world = PJ_W_SELF; |
| 235 | } else if (t->workload == W_VERIFY) { |
| 236 | n.evidence_requirement = PJ_E_REFS; /* the receipt's body is not there to check */ |
| 237 | } else if (t->workload == W_PLAN) { |
| 238 | n.memory_scope = PJ_M_WORKING; /* forgets experiments before the window */ |
| 239 | n.required_temporal_scope.t0 = t->t_now - 1000; |
| 240 | } else if (t->workload == W_BRANCH) { |
| 241 | /* asks on the main line: the branch's hypothetical change is not there */ |
| 242 | n.branch = 0; |
| 243 | uint32_t k = 0; |
| 244 | for (uint32_t i = 0; i < n.n_fact_types; i++) |
| 245 | if (n.required_fact_types[i].treatment != PJ_BRANCH_LOCAL) n.required_fact_types[k++] = n.required_fact_types[i]; |
| 246 | n.n_fact_types = k; |
| 247 | } else { |
| 248 | /* W_ASSESS: the whole lifetime instead of the current generation */ |
| 249 | n.required_temporal_scope.anchor = PJ_A_FIXED; |
| 250 | n.required_temporal_scope.t0 = 0; |
| 251 | } |
| 252 | StateProjection p; |
| 253 | if (pj_compile(s, &n, &p) != PJ_OK) return 0; |
| 254 | PjView v; |
| 255 | pj_view_projection(s, &p, &v); |
| 256 | SpAnswer a; |
| 257 | SpTrace tr = { 0, 0, NULL }; |
| 258 | sp_reason(t, &v, &a, &tr); |
| 259 | pj_view_free(&v); |
| 260 | pj_free(&p); |
| 261 | return ans_eq(&a, &t->truth); |
| 262 | } |
| 263 | |
| 264 | /* Ask for protected state to be summarized, derived or made ephemeral. */ |
| 265 | static void protected_refusals(CxStore *s, const SpTask *t) { |
| 266 | static const struct { uint32_t cls, kind, world; } prot[5] = { |
| 267 | { CX_EVIDENCE, K_AUTHORITY, PJ_W_SELF }, /* authority */ |
| 268 | { CX_EXECUTION, K_COMMIT, PJ_W_SELF }, /* commit receipt */ |
| 269 | { CX_EVIDENCE, K_VERIFY, PJ_W_SELF }, /* verification evidence */ |
| 270 | { CX_ENTITY, K_GENERATION, PJ_W_MACHINE }, /* generation identity */ |
| 271 | { CX_EXECUTION, K_EFFECT, PJ_W_SELF }, /* effect receipt */ |
| 272 | }; |
| 273 | static const uint32_t bad[3] = { PJ_COMPRESSIBLE, PJ_DERIVED, PJ_EPHEMERAL }; |
| 274 | for (uint32_t i = 0; i < 5; i++) |
| 275 | for (uint32_t k = 0; k < 3; k++) { |
| 276 | CognitiveNeed n; |
| 277 | pj_need_init(&n, PJ_OP_EXPLAIN, t->subject, t->t_now); |
| 278 | n.memory_scope = PJ_M_EPISODIC; |
| 279 | pj_need_add(&n, prot[i].cls, prot[i].kind, bad[k], prot[i].world, PJ_T_ALL); |
| 280 | StateProjection p; |
| 281 | int rc = pj_compile(s, &n, &p); |
| 282 | g_protect_attempts++; |
| 283 | if (rc == PJ_ERR_PROTECTED && p.n == 0 && p.e == NULL) g_protect_refusals++; |
| 284 | else pj_free(&p); |
| 285 | CHECK(rc == PJ_ERR_PROTECTED, "protected class %u kind %u as %s was not refused (rc %d)", |
| 286 | prot[i].cls, prot[i].kind, pj_state_class_name(bad[k]), rc); |
| 287 | } |
| 288 | /* A reference keeps the exact digest: allowed. */ |
| 289 | CognitiveNeed n; |
| 290 | pj_need_init(&n, PJ_OP_EXPLAIN, t->subject, t->t_now); |
| 291 | pj_need_add(&n, CX_EVIDENCE, K_VERIFY, PJ_REFERENCE_ONLY, PJ_W_SELF, PJ_T_ALL); |
| 292 | StateProjection p; |
| 293 | CHECK(pj_compile(s, &n, &p) == PJ_OK && p.n > 0 && p.nr == p.n, "protected reference refused"); |
| 294 | pj_free(&p); |
| 295 | } |
| 296 | |
| 297 | static void run_task(uint32_t w, uint32_t seed) { |
| 298 | WStats *st = &g_w[w]; |
| 299 | CxStore s; |
| 300 | SpTask t; |
| 301 | SpScale sc = { SUBJECTS, T_END }; |
| 302 | CHECK(sp_build(&s, w, seed, &sc, &t) == 0, "%s seed %u: build", sp_workload_names[w], seed); |
| 303 | if (seed == 0) { |
| 304 | g_chain_ok &= cx_verify_chain(&s) == CX_OK; |
| 305 | protected_refusals(&s, &t); |
| 306 | } |
| 307 | uint32_t k = st->tasks++; |
| 308 | |
| 309 | /* whole store */ |
| 310 | PjView full; |
| 311 | pj_view_full(&s, &full); |
| 312 | full.bytes = pj_full_bytes(&s); |
| 313 | uint8_t *used_full = calloc(full.n, 1); |
| 314 | SpTrace trf = { 0, 0, used_full }; |
| 315 | SpAnswer af; |
| 316 | uint64_t rf = reason_timed(&t, &full, &af, &trf); |
| 317 | |
| 318 | /* projection */ |
| 319 | CognitiveNeed need; |
| 320 | sp_need(&t, &need); |
| 321 | StateProjection p; |
| 322 | int rc = pj_compile(&s, &need, &p); |
| 323 | CHECK(rc == PJ_OK, "%s seed %u: compile rc %d", sp_workload_names[w], seed, rc); |
| 324 | if (rc != PJ_OK) { pj_view_free(&full); free(used_full); cx_free(&s); return; } |
| 325 | PjView pv; |
| 326 | pj_view_projection(&s, &p, &pv); |
| 327 | uint8_t *used_proj = calloc(pv.n ? pv.n : 1, 1); |
| 328 | SpTrace trp = { 0, 0, used_proj }; |
| 329 | SpAnswer ap; |
| 330 | uint64_t rp = reason_timed(&t, &pv, &ap, &trp); |
| 331 | |
| 332 | int fok = ans_eq(&af, &t.truth), pok = ans_eq(&ap, &t.truth); |
| 333 | st->full_ok += fok; |
| 334 | st->proj_ok += pok; |
| 335 | st->agree += ans_eq(&af, &ap); |
| 336 | CHECK(fok, "%s seed %u: whole-store answer %llu/%llu/%llu/%llu/%llu, planted %llu/%llu/%llu/%llu/%llu", |
| 337 | sp_workload_names[w], seed, (unsigned long long)af.a[0], (unsigned long long)af.a[1], |
| 338 | (unsigned long long)af.a[2], (unsigned long long)af.a[3], (unsigned long long)af.a[4], |
| 339 | (unsigned long long)t.truth.a[0], (unsigned long long)t.truth.a[1], (unsigned long long)t.truth.a[2], |
| 340 | (unsigned long long)t.truth.a[3], (unsigned long long)t.truth.a[4]); |
| 341 | CHECK(pok, "%s seed %u: projection answer %llu/%llu/%llu/%llu/%llu, planted %llu/%llu/%llu/%llu/%llu", |
| 342 | sp_workload_names[w], seed, (unsigned long long)ap.a[0], (unsigned long long)ap.a[1], |
| 343 | (unsigned long long)ap.a[2], (unsigned long long)ap.a[3], (unsigned long long)ap.a[4], |
| 344 | (unsigned long long)t.truth.a[0], (unsigned long long)t.truth.a[1], (unsigned long long)t.truth.a[2], |
| 345 | (unsigned long long)t.truth.a[3], (unsigned long long)t.truth.a[4]); |
| 346 | |
| 347 | st->full_bytes[k] = full.bytes; |
| 348 | st->proj_bytes[k] = p.bytes; |
| 349 | st->full_objects[k] = full.n; |
| 350 | st->proj_objects[k] = p.objects; |
| 351 | st->full_touched[k] = trf.touched; |
| 352 | st->proj_touched[k] = trp.touched; |
| 353 | st->full_words[k] = trf.words; |
| 354 | st->proj_words[k] = trp.words; |
| 355 | st->examined[k] = p.examined; |
| 356 | st->compile_ns[k] = p.compile_ns; |
| 357 | st->proj_build_ns[k] = pv.build_ns; |
| 358 | st->full_build_ns[k] = full.build_ns; |
| 359 | st->full_reason_ns[k] = rf; |
| 360 | st->proj_reason_ns[k] = rp; |
| 361 | st->full_total_ns[k] = full.build_ns + rf; |
| 362 | st->proj_total_ns[k] = p.compile_ns + pv.build_ns + rp; |
| 363 | if (st->proj_total_ns[k] < st->full_total_ns[k]) st->e2e_faster++; |
| 364 | CHECK(p.bytes < full.bytes && p.objects < full.n && trp.touched < trf.touched, |
| 365 | "%s seed %u: projection not smaller", sp_workload_names[w], seed); |
| 366 | |
| 367 | for (uint32_t i = 0; i < full.n; i++) st->full_used += used_full[i]; |
| 368 | st->full_provided += full.n; |
| 369 | for (uint32_t i = 0; i < pv.n; i++) st->proj_used += used_proj[i]; |
| 370 | st->proj_used += pv.nf; /* a feature present is a feature read (see reason_assess) */ |
| 371 | st->proj_provided += pv.n + pv.nf; |
| 372 | for (uint32_t i = 0; i < t.n_needed; i++) { |
| 373 | st->needed++; |
| 374 | st->needed_present += needed_present(&t, &s, &p, t.needed[i]); |
| 375 | } |
| 376 | for (uint32_t i = 0; i < p.n; i++) st->by_treatment[p.e[i].treatment]++; |
| 377 | st->by_treatment[PJ_DERIVED] += p.nf; |
| 378 | |
| 379 | /* controls */ |
| 380 | leave_one_out(&t, &pv, st); |
| 381 | st->recentk_ok += naive_recent(&t, &s, p.bytes); |
| 382 | st->subject_ok += naive_subject(&t, &s); |
| 383 | int applicable = 0; |
| 384 | int u = underspecified(&t, &s, &applicable); |
| 385 | if (applicable) { st->underspec_tasks++; st->underspec_ok += u; } |
| 386 | |
| 387 | /* recoverability and the audit */ |
| 388 | uint64_t checked = 0; |
| 389 | int rr = pj_recover_all(&s, &p, &checked); |
| 390 | st->recover_checked += checked; |
| 391 | st->recover_fail += rr != PJ_OK; |
| 392 | CHECK(rr == PJ_OK, "%s seed %u: recovery failed", sp_workload_names[w], seed); |
| 393 | PjAudit au; |
| 394 | pj_audit(&s, &need, &p, &au); |
| 395 | int aok = au.included == p.n && au.included + au.excluded == s.n && au.counts_match && |
| 396 | au.excluded_intact == au.excluded && au.other_branch_included == 0; |
| 397 | st->audit_ok += aok; |
| 398 | st->excluded += au.excluded; |
| 399 | st->excluded_intact += au.excluded_intact; |
| 400 | st->other_branch += au.other_branch_included; |
| 401 | for (uint32_t i = 0; i < PJ_WHY_END; i++) st->by_why[i] += au.by_why[i]; |
| 402 | CHECK(aok, "%s seed %u: audit included %llu/%u excluded %llu intact %llu counts %d other-branch %llu", |
| 403 | sp_workload_names[w], seed, (unsigned long long)au.included, p.n, (unsigned long long)au.excluded, |
| 404 | (unsigned long long)au.excluded_intact, au.counts_match, (unsigned long long)au.other_branch_included); |
| 405 | |
| 406 | /* tamper with one source the projection depends on without carrying it */ |
| 407 | uint64_t victim = 0; |
| 408 | if (p.nr) victim = p.r[0].id; |
| 409 | else if (p.nf) { |
| 410 | const CxIdList *l = &s.by_subject[p.f[0].subject]; |
| 411 | for (uint32_t i = 0; i < l->n && !victim; i++) { |
| 412 | const CxObject *o = cx_get(&s, l->ids[i]); |
| 413 | if (o->kind == p.f[0].kind && o->t >= p.f[0].t0 && o->t <= p.f[0].t1 && o->n) victim = o->id; |
| 414 | } |
| 415 | } |
| 416 | if (victim && cx_get(&s, victim)->n) { |
| 417 | st->tamper_tried++; |
| 418 | cx_tamper(&s, victim, 0, cx_payload(&s, cx_get(&s, victim))[0] ^ 1); |
| 419 | uint32_t n_out = 0; |
| 420 | int caught = pj_recover_all(&s, &p, NULL) == PJ_ERR_RECOVER && |
| 421 | (p.nr == 0 || pj_recall(&s, &p, victim, &n_out) == NULL); |
| 422 | st->tamper_caught += caught; |
| 423 | CHECK(caught, "%s seed %u: tampered source %llu not caught", sp_workload_names[w], seed, |
| 424 | (unsigned long long)victim); |
| 425 | } |
| 426 | |
| 427 | /* budget below the pinned state: refused, never trimmed */ |
| 428 | if (seed == 0 && w == W_EXPLAIN) { |
| 429 | CognitiveNeed small = need; |
| 430 | small.resource_budget.max_bytes = 256; |
| 431 | StateProjection q; |
| 432 | g_budget_refused = pj_compile(&s, &small, &q) == PJ_ERR_BUDGET && q.n == 0; |
| 433 | CHECK(g_budget_refused, "a budget below the pinned state was not refused"); |
| 434 | } |
| 435 | if (seed == 0 && w == W_EXPLAIN) { |
| 436 | /* RECALLABLE: the body comes back on demand, digest-checked (before any tamper) */ |
| 437 | g_recall_ok = 0; |
| 438 | for (uint32_t i = 0; i < p.n; i++) |
| 439 | if (p.e[i].treatment == PJ_RECALLABLE) { |
| 440 | uint32_t n_out = 0; |
| 441 | const uint64_t *b = pj_recall(&s, &p, p.e[i].id, &n_out); |
| 442 | g_recall_ok = b && n_out == cx_get(&s, p.e[i].id)->n; |
| 443 | break; |
| 444 | } |
| 445 | CHECK(g_recall_ok, "RECALLABLE body could not be recalled"); |
| 446 | /* EPHEMERAL: in full for this operation, outside the recoverability contract */ |
| 447 | CognitiveNeed e; |
| 448 | pj_need_init(&e, PJ_OP_PREDICT, t.subject, t.t_now); |
| 449 | e.required_temporal_scope.t0 = t.t_now - 100; |
| 450 | pj_need_add(&e, CX_OBSERVATION, K_TELEMETRY, PJ_EPHEMERAL, PJ_W_SELF, PJ_T_WINDOW); |
| 451 | StateProjection q; |
| 452 | int erc = pj_compile(&s, &e, &q); |
| 453 | g_ephemeral_ok = erc == PJ_OK && q.n > 0 && q.nr == 0 && q.e[0].form == PJ_FORM_FULL; |
| 454 | CHECK(g_ephemeral_ok, "EPHEMERAL projection rc %d", erc); |
| 455 | if (erc == PJ_OK) pj_free(&q); |
| 456 | } |
| 457 | if (seed == 0 && w == W_BRANCH) { |
| 458 | CognitiveNeed mainline = need; |
| 459 | mainline.branch = 0; |
| 460 | StateProjection q; |
| 461 | g_branch_refused = pj_compile(&s, &mainline, &q) == PJ_ERR_BRANCH; |
| 462 | CHECK(g_branch_refused, "BRANCH_LOCAL without a branch compiled"); |
| 463 | } |
| 464 | |
| 465 | pj_view_free(&pv); |
| 466 | pj_view_free(&full); |
| 467 | free(used_full); |
| 468 | free(used_proj); |
| 469 | pj_free(&p); |
| 470 | cx_free(&s); |
| 471 | } |
| 472 | |
| 473 | /* ---- Part B: the real R11 faculty ---- */ |
| 474 | |
| 475 | enum { SUBJ_EXTERNAL = 100, ISSUER = 3 }; |
| 476 | #define RES_DEMAND 0x7712000ull |
| 477 | #define RES_SELECTION 0x7712001ull |
| 478 | enum { K_R11_SELECTION = 30, K_R11_RECEIPT, K_R11_DEMAND, K_R11_PLACEMENT }; |
| 479 | #define CLASS_A 0x111u |
| 480 | #define CLASS_B 0x222u |
| 481 | #define REGIMES 4u |
| 482 | |
| 483 | typedef struct { |
| 484 | AienosCapAdmin *admin; |
| 485 | AienosCapView *view; |
| 486 | RxWorld w; |
| 487 | RxAienFaculty a; |
| 488 | RxAienCaps acaps; |
| 489 | RxObjRef demand, selection; |
| 490 | RxCapRef ext_demand, ext_selection, ext_placement, ext_goal; |
| 491 | uint64_t publishes; |
| 492 | } Env; |
| 493 | |
| 494 | static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 495 | AienosCapRef office; |
| 496 | aienos_cap_office(e->admin, &office); |
| 497 | AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; |
| 498 | AienosCapRef r = { UINT32_MAX, 0 }; |
| 499 | if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; |
| 500 | return (RxCapRef){ r.cap_id, r.generation }; |
| 501 | } |
| 502 | |
| 503 | static uint64_t fld(Env *e, RxObjRef r, uint32_t i) { |
| 504 | RxObject o; |
| 505 | if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; |
| 506 | return o.field[i]; |
| 507 | } |
| 508 | |
| 509 | static int publish(Env *e, RxCapRef cap, RxObjRef o, const uint32_t *fields, const uint64_t *vals, uint32_t n) { |
| 510 | RxMutation m[RX_MAX_FIELDS]; |
| 511 | for (uint32_t i = 0; i < n; i++) m[i] = (RxMutation){ o, fields[i], vals[i] }; |
| 512 | int64_t id = rx_world_publish_external(&e->w, cap, m, n); |
| 513 | e->publishes++; |
| 514 | return id > 0 && rx_world_wait_quiescent(&e->w, 20000) == RX_OK ? 0 : -1; |
| 515 | } |
| 516 | |
| 517 | static int env_start(Env *e) { |
| 518 | memset(e, 0, sizeof(*e)); |
| 519 | if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; |
| 520 | if (rx_world_init_native(&e->w, e->view, 4, 1u << 18) != RX_OK) return -1; |
| 521 | e->w.external_subject = SUBJ_EXTERNAL; |
| 522 | uint64_t z[RX_MAX_FIELDS] = { 0 }; |
| 523 | if (rx_world_create(&e->w, RX_OT_DEMAND, RX_PERSIST_RESIDENT, RES_DEMAND, z, &e->demand) != RX_OK || |
| 524 | rx_world_create(&e->w, RX_OT_SELECTION, RX_PERSIST_RESIDENT, RES_SELECTION, z, &e->selection) != RX_OK) |
| 525 | return -1; |
| 526 | e->ext_demand = mint(e, SUBJ_EXTERNAL, RES_DEMAND, RX_RIGHT_WRITE); |
| 527 | e->ext_selection = mint(e, SUBJ_EXTERNAL, RES_SELECTION, RX_RIGHT_WRITE); |
| 528 | RxAienConfig cfg; |
| 529 | rx_aien_default_config(&cfg); |
| 530 | RxAienInputs in = { e->demand, e->selection }; |
| 531 | if (rx_aien_create_objects(&e->a, &e->w, &cfg, &in) != RX_OK) return -1; |
| 532 | const uint32_t RW = RX_RIGHT_READ | RX_RIGHT_WRITE; |
| 533 | for (uint32_t i = 0; i < RX_AIEN_RES_COUNT; i++) |
| 534 | e->acaps.own[i] = mint(e, RX_AIEN_SUBJ, RX_AIEN_RES_BASE + i, |
| 535 | i == RX_AIEN_RES_PLACEMENT || i == RX_AIEN_RES_GOAL ? RX_RIGHT_READ : RW); |
| 536 | e->acaps.demand = mint(e, RX_AIEN_SUBJ, RES_DEMAND, RX_RIGHT_READ); |
| 537 | e->acaps.selection = mint(e, RX_AIEN_SUBJ, RES_SELECTION, RX_RIGHT_READ); |
| 538 | e->ext_placement = mint(e, SUBJ_EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_PLACEMENT, RX_RIGHT_WRITE); |
| 539 | e->ext_goal = mint(e, SUBJ_EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, RX_RIGHT_WRITE); |
| 540 | return rx_aien_register(&e->a, &e->acaps) == RX_OK ? 0 : -1; |
| 541 | } |
| 542 | |
| 543 | static void env_stop(Env *e) { |
| 544 | rx_world_wait_quiescent(&e->w, 20000); |
| 545 | rx_world_destroy(&e->w); |
| 546 | aienos_cap_stop(e->admin, e->view); |
| 547 | } |
| 548 | |
| 549 | static uint64_t aien_activations(Env *e) { |
| 550 | const uint32_t r[5] = { e->a.r_observe, e->a.r_predict, e->a.r_explain, e->a.r_assess, e->a.r_plan }; |
| 551 | uint64_t n = 0; |
| 552 | for (int i = 0; i < 5; i++) n += e->w.reactions[r[i]].activations; |
| 553 | return n; |
| 554 | } |
| 555 | |
| 556 | typedef struct { |
| 557 | uint32_t kind; /* RX_AIEN_HYP_CORE_CLASS or RX_AIEN_HYP_DRIFT */ |
| 558 | uint64_t then, now, expected, observed; |
| 559 | } R11Truth; |
| 560 | |
| 561 | /* One regime's lifetime plus three other regimes, on one clock. Cortex |
| 562 | * subject = regime index (1..4); X = 1. */ |
| 563 | static void r11_history(CxStore *s, uint32_t seed, R11Truth *truth) { |
| 564 | uint64_t rng = 0xA5A5A5A5ull ^ ((uint64_t)seed * 0x9E3779B97F4A7C15ull); |
| 565 | #define RND(n) ((rng ^= rng << 13, rng ^= rng >> 7, rng ^= rng << 17), rng % (n)) |
| 566 | cx_init(s, REGIMES + 1); |
| 567 | uint64_t clock = 1, calls[REGIMES + 1] = { 0 }, spent[REGIMES + 1] = { 0 }, wins[REGIMES + 1] = { 0 }; |
| 568 | uint64_t placement_seq = 0, cls = CLASS_A, epoch = 0; |
| 569 | const uint32_t epochs = 24; |
| 570 | int core_case = seed % 2 == 0; |
| 571 | RxAienConfig cfg; |
| 572 | rx_aien_default_config(&cfg); |
| 573 | const uint32_t steady = 1 + cfg.baseline_intervals + cfg.confirm_intervals + 3; |
| 574 | CxHeader h; |
| 575 | uint64_t pl[8]; |
| 576 | |
| 577 | #define DEMAND(reg, ns, real) do { \ |
| 578 | calls[reg] += 16; spent[reg] += 16 * (ns); wins[reg]++; \ |
| 579 | memset(&h, 0, sizeof h); h.cls = CX_OBSERVATION; h.kind = K_R11_DEMAND; \ |
| 580 | h.subject = (reg); h.t = clock++; \ |
| 581 | pl[0] = calls[reg]; pl[1] = spent[reg]; pl[2] = wins[reg]; \ |
| 582 | pl[3] = 64 * (reg); pl[4] = 256; pl[5] = (real); \ |
| 583 | cx_append(s, &h, pl, 6, NULL); \ |
| 584 | } while (0) |
| 585 | |
| 586 | for (uint32_t ep = 1; ep <= epochs; ep++) { |
| 587 | /* between records the work may be moved; a move alone is not evidence */ |
| 588 | if (ep > 1 && RND(3) == 0) { |
| 589 | cls = cls == CLASS_A ? CLASS_B : CLASS_A; |
| 590 | memset(&h, 0, sizeof h); |
| 591 | h.cls = CX_OBSERVATION; h.kind = K_R11_PLACEMENT; h.subject = 1; h.t = clock++; |
| 592 | pl[0] = ++placement_seq; pl[1] = cls; |
| 593 | cx_append(s, &h, pl, 2, NULL); |
| 594 | } |
| 595 | if (ep == 1) { |
| 596 | memset(&h, 0, sizeof h); |
| 597 | h.cls = CX_OBSERVATION; h.kind = K_R11_PLACEMENT; h.subject = 1; h.t = clock++; |
| 598 | pl[0] = ++placement_seq; pl[1] = cls; |
| 599 | cx_append(s, &h, pl, 2, NULL); |
| 600 | } |
| 601 | epoch = ep; |
| 602 | uint64_t real = 0xA000 + ep, cost = 3000 + RND(4000); |
| 603 | /* the record, with the verification receipt it was made under */ |
| 604 | memset(&h, 0, sizeof h); |
| 605 | h.cls = CX_EVIDENCE; h.kind = K_R11_RECEIPT; h.subject = 1; h.t = clock++; |
| 606 | h.protect = CX_PROT_VERIFY_EVIDENCE; h.tag = PJ_VERIFIED; |
| 607 | pl[0] = real; pl[1] = cost; |
| 608 | uint64_t rid = 0; |
| 609 | cx_append(s, &h, pl, 2, &rid); |
| 610 | memset(&h, 0, sizeof h); |
| 611 | h.cls = CX_REALIZATION; h.kind = K_R11_SELECTION; h.subject = 1; h.t = clock++; |
| 612 | h.tag = epoch; h.links[0] = rid; |
| 613 | pl[0] = epoch; pl[1] = real; pl[2] = rx_aien_regime(64, 256); |
| 614 | cx_append(s, &h, pl, 3, NULL); |
| 615 | for (uint32_t k = 0; k < steady; k++) { |
| 616 | DEMAND(1, cost, real); |
| 617 | for (uint64_t o = 2; o <= REGIMES; o++) DEMAND(o, 2000 + 100 * o + RND(50), 0xB000 + o); |
| 618 | } |
| 619 | if (ep == epochs) { |
| 620 | truth->then = cls; |
| 621 | truth->expected = cost; |
| 622 | uint64_t fail_cost = core_case ? cost / 2 : cost * 2; |
| 623 | if (core_case) { |
| 624 | cls = cls == CLASS_A ? CLASS_B : CLASS_A; |
| 625 | memset(&h, 0, sizeof h); |
| 626 | h.cls = CX_OBSERVATION; h.kind = K_R11_PLACEMENT; h.subject = 1; h.t = clock++; |
| 627 | pl[0] = ++placement_seq; pl[1] = cls; |
| 628 | cx_append(s, &h, pl, 2, NULL); |
| 629 | } |
| 630 | for (uint32_t k = 0; k < cfg.misses_to_fail; k++) { |
| 631 | DEMAND(1, fail_cost, real); |
| 632 | for (uint64_t o = 2; o <= REGIMES; o++) DEMAND(o, 2000 + 100 * o, 0xB000 + o); |
| 633 | } |
| 634 | truth->kind = core_case ? RX_AIEN_HYP_CORE_CLASS : RX_AIEN_HYP_DRIFT; |
| 635 | truth->now = cls; |
| 636 | truth->observed = fail_cost; |
| 637 | } |
| 638 | } |
| 639 | #undef DEMAND |
| 640 | #undef RND |
| 641 | } |
| 642 | |
| 643 | static const CxStore *g_t_store; |
| 644 | static int cmp_time(const void *a, const void *b) { |
| 645 | const CxObject *x = cx_get(g_t_store, *(const uint64_t *)a), *y = cx_get(g_t_store, *(const uint64_t *)b); |
| 646 | return x->t < y->t ? -1 : x->t > y->t; |
| 647 | } |
| 648 | |
| 649 | /* Publish Cortex objects into the world, in time order, as the world saw them. */ |
| 650 | static int replay(Env *e, const CxStore *s, uint64_t *ids, uint32_t n) { |
| 651 | g_t_store = s; |
| 652 | qsort(ids, n, sizeof(uint64_t), cmp_time); |
| 653 | for (uint32_t i = 0; i < n; i++) { |
| 654 | const CxObject *o = cx_get(s, ids[i]); |
| 655 | const uint64_t *w = cx_payload(s, o); |
| 656 | if (o->kind == K_R11_DEMAND) { |
| 657 | uint32_t f[6] = { 0, 1, 2, 4, 5, 6 }; |
| 658 | uint64_t v[6] = { w[0], w[1], w[2], w[3], w[4], w[5] }; |
| 659 | if (publish(e, e->ext_demand, e->demand, f, v, 6)) return -1; |
| 660 | } else if (o->kind == K_R11_SELECTION) { |
| 661 | uint32_t f[3] = { 0, 1, 6 }; |
| 662 | uint64_t v[3] = { w[0], w[1], w[2] }; |
| 663 | if (publish(e, e->ext_selection, e->selection, f, v, 3)) return -1; |
| 664 | } else if (o->kind == K_R11_PLACEMENT) { |
| 665 | uint32_t f[2] = { 0, 1 }; |
| 666 | uint64_t v[2] = { w[0], w[1] }; |
| 667 | if (publish(e, e->ext_placement, e->a.o.placement, f, v, 2)) return -1; |
| 668 | } |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | typedef struct { |
| 674 | uint32_t runs, full_ok, proj_ok, agree; |
| 675 | uint64_t full_objects[16], proj_objects[16], full_bytes[16], proj_bytes[16]; |
| 676 | uint64_t full_act[16], proj_act[16], full_ns[16], proj_ns[16], compile_ns[16]; |
| 677 | } R11Stats; |
| 678 | static R11Stats g_r11; |
| 679 | |
| 680 | static int hyp_matches(Env *e, const R11Truth *t) { |
| 681 | RxObjRef h = e->a.o.hypothesis; |
| 682 | return fld(e, h, 0) >= 1 && fld(e, h, 1) == t->kind && fld(e, h, 3) == t->then && fld(e, h, 4) == t->now && |
| 683 | fld(e, h, 5) == t->expected && fld(e, h, 6) == t->observed; |
| 684 | } |
| 685 | |
| 686 | static void part_b(void) { |
| 687 | for (uint32_t seed = 0; seed < 8; seed++) { |
| 688 | CxStore s; |
| 689 | R11Truth truth; |
| 690 | memset(&truth, 0, sizeof truth); |
| 691 | r11_history(&s, seed, &truth); |
| 692 | uint32_t k = g_r11.runs++; |
| 693 | |
| 694 | /* whole history */ |
| 695 | uint64_t *all = malloc(s.n * sizeof(uint64_t)); |
| 696 | for (uint64_t i = 0; i < s.n; i++) all[i] = i + 1; |
| 697 | Env e; |
| 698 | CHECK(env_start(&e) == 0, "R11 env"); |
| 699 | uint64_t t0 = now_ns(); |
| 700 | CHECK(replay(&e, &s, all, (uint32_t)s.n) == 0, "R11 full replay"); |
| 701 | g_r11.full_ns[k] = now_ns() - t0; |
| 702 | g_r11.full_act[k] = aien_activations(&e); |
| 703 | int fok = hyp_matches(&e, &truth); |
| 704 | uint64_t f_plan = fld(&e, e.a.o.plan, 3); |
| 705 | env_stop(&e); |
| 706 | g_r11.full_objects[k] = s.n; |
| 707 | g_r11.full_bytes[k] = pj_full_bytes(&s); |
| 708 | |
| 709 | /* the projection for "why did X's prediction fail" */ |
| 710 | CognitiveNeed n; |
| 711 | pj_need_init(&n, PJ_OP_EXPLAIN, 1, s.obj[s.n - 1].t); |
| 712 | n.required_temporal_scope.anchor = PJ_A_SINCE_VERIFIED; |
| 713 | n.required_temporal_scope.realization_kind = K_R11_SELECTION; |
| 714 | n.evidence_requirement = PJ_E_REFS; |
| 715 | pj_need_add(&n, CX_OBSERVATION, K_R11_DEMAND, PJ_LIVE, PJ_W_SELF, PJ_T_WINDOW); |
| 716 | pj_need_add(&n, CX_OBSERVATION, K_R11_PLACEMENT, PJ_PINNED, PJ_W_SELF, PJ_T_CARRY_IN); |
| 717 | StateProjection p; |
| 718 | int rc = pj_compile(&s, &n, &p); |
| 719 | CHECK(rc == PJ_OK, "R11 compile rc %d", rc); |
| 720 | uint64_t *ids = malloc((p.n ? p.n : 1) * sizeof(uint64_t)); |
| 721 | for (uint32_t i = 0; i < p.n; i++) ids[i] = p.e[i].id; |
| 722 | CHECK(env_start(&e) == 0, "R11 env"); |
| 723 | t0 = now_ns(); |
| 724 | CHECK(replay(&e, &s, ids, p.n) == 0, "R11 projection replay"); |
| 725 | g_r11.proj_ns[k] = now_ns() - t0 + p.compile_ns; |
| 726 | g_r11.compile_ns[k] = p.compile_ns; |
| 727 | g_r11.proj_act[k] = aien_activations(&e); |
| 728 | int pok = hyp_matches(&e, &truth); |
| 729 | uint64_t p_plan = fld(&e, e.a.o.plan, 3); |
| 730 | env_stop(&e); |
| 731 | g_r11.proj_objects[k] = p.n; |
| 732 | g_r11.proj_bytes[k] = p.bytes; |
| 733 | |
| 734 | g_r11.full_ok += fok; |
| 735 | g_r11.proj_ok += pok; |
| 736 | g_r11.agree += fok == pok && f_plan == p_plan; |
| 737 | CHECK(fok, "R11 seed %u: whole history did not produce the planted hypothesis", seed); |
| 738 | CHECK(pok, "R11 seed %u: projection did not produce the planted hypothesis", seed); |
| 739 | CHECK(f_plan == p_plan, "R11 seed %u: plans differ (%llu vs %llu)", seed, (unsigned long long)f_plan, |
| 740 | (unsigned long long)p_plan); |
| 741 | CHECK(g_r11.proj_act[k] < g_r11.full_act[k] && p.bytes < g_r11.full_bytes[k], |
| 742 | "R11 seed %u: projection not cheaper", seed); |
| 743 | uint64_t checked; |
| 744 | CHECK(pj_recover_all(&s, &p, &checked) == PJ_OK, "R11 recovery"); |
| 745 | free(ids); |
| 746 | free(all); |
| 747 | pj_free(&p); |
| 748 | cx_free(&s); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* ---- Part C: scale ---- */ |
| 753 | |
| 754 | typedef struct { |
| 755 | uint32_t subjects; |
| 756 | uint64_t full_bytes, proj_bytes, full_ns, proj_ns, examined; |
| 757 | uint32_t ok; |
| 758 | } ScaleRow; |
| 759 | static ScaleRow g_scale[3]; |
| 760 | |
| 761 | static void part_c(void) { |
| 762 | const uint32_t sizes[3] = { 16, 64, 256 }; |
| 763 | for (uint32_t i = 0; i < 3; i++) { |
| 764 | uint64_t fb[8], pb[8], fn[8], pn[8], ex[8]; |
| 765 | uint32_t ok = 0; |
| 766 | for (uint32_t seed = 0; seed < 8; seed++) { |
| 767 | CxStore s; |
| 768 | SpTask t; |
| 769 | SpScale sc = { sizes[i], T_END }; |
| 770 | sp_build(&s, W_EXPLAIN, 100 + seed, &sc, &t); |
| 771 | PjView full; |
| 772 | pj_view_full(&s, &full); |
| 773 | SpAnswer af, ap; |
| 774 | SpTrace tr = { 0, 0, NULL }; |
| 775 | uint64_t rf = reason_timed(&t, &full, &af, &tr); |
| 776 | CognitiveNeed n; |
| 777 | sp_need(&t, &n); |
| 778 | StateProjection p; |
| 779 | pj_compile(&s, &n, &p); |
| 780 | PjView pv; |
| 781 | pj_view_projection(&s, &p, &pv); |
| 782 | uint64_t rp = reason_timed(&t, &pv, &ap, &tr); |
| 783 | ok += ans_eq(&af, &t.truth) && ans_eq(&ap, &t.truth); |
| 784 | fb[seed] = pj_full_bytes(&s); |
| 785 | pb[seed] = p.bytes; |
| 786 | fn[seed] = full.build_ns + rf; |
| 787 | pn[seed] = p.compile_ns + pv.build_ns + rp; |
| 788 | ex[seed] = p.examined; |
| 789 | pj_view_free(&pv); |
| 790 | pj_view_free(&full); |
| 791 | pj_free(&p); |
| 792 | cx_free(&s); |
| 793 | } |
| 794 | g_scale[i] = (ScaleRow){ sizes[i], median(fb, 8), median(pb, 8), median(fn, 8), median(pn, 8), |
| 795 | median(ex, 8), ok }; |
| 796 | CHECK(ok == 8, "scale %u: %u/8 correct", sizes[i], ok); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* ---- receipt ---- */ |
| 801 | |
| 802 | static void binary_digest(char out[65]) { |
| 803 | strcpy(out, "unavailable"); |
| 804 | FILE *fp = fopen("/proc/self/exe", "rb"); |
| 805 | if (!fp) return; |
| 806 | sha256_ctx c; |
| 807 | sha256_init(&c); |
| 808 | uint8_t buf[65536]; |
| 809 | size_t n; |
| 810 | while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); |
| 811 | fclose(fp); |
| 812 | uint8_t d[32]; |
| 813 | sha256_final(&c, d); |
| 814 | for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]); |
| 815 | } |
| 816 | |
| 817 | static const char *pass(int b) { return b ? "true" : "false"; } |
| 818 | |
| 819 | static void write_receipt(int gate_pass, int preserve, int reduce, int controls, int recover, int grounded) { |
| 820 | char path[512]; |
| 821 | if (omega_evidence_path("STATE_PROJECTION/omega_state_projection_receipt.json", path, sizeof path) != 0) return; |
| 822 | FILE *fp = fopen(path, "w"); |
| 823 | if (!fp) return; |
| 824 | char commit[41]; |
| 825 | memset(commit, 0, sizeof commit); |
| 826 | if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); |
| 827 | const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); |
| 828 | int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && !omega_evidence_tree_dirty(); |
| 829 | const char *aienos = getenv("AIENOS_COMMIT"); |
| 830 | char digest[65]; |
| 831 | binary_digest(digest); |
| 832 | struct utsname u; |
| 833 | memset(&u, 0, sizeof u); |
| 834 | uname(&u); |
| 835 | fprintf(fp, "{\n \"schema\": \"AIEN_OMEGA_STATE_PROJECTION_V1\",\n"); |
| 836 | fprintf(fp, " \"run_id\": \"%s\",\n", omega_evidence_run_id()); |
| 837 | fprintf(fp, " \"candidate_commit\": %s%s%s,\n", candidate ? "\"" : "", candidate ? candidate : "null", |
| 838 | candidate ? "\"" : ""); |
| 839 | fprintf(fp, " \"candidate_bound\": %s,\n \"run_commit\": \"%s\",\n \"tree_dirty\": %s,\n", pass(bound), |
| 840 | commit, pass(omega_evidence_tree_dirty())); |
| 841 | fprintf(fp, " \"aienos_commit\": %s%s%s,\n", aienos ? "\"" : "", aienos ? aienos : "null", aienos ? "\"" : ""); |
| 842 | fprintf(fp, " \"checks\": %d,\n \"failures\": %d,\n \"test_binary_sha256\": \"%s\",\n", g_checks, g_fail, digest); |
| 843 | fprintf(fp, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n", u.sysname, |
| 844 | u.release, u.machine); |
| 845 | fprintf(fp, " \"consumer\": \"explicit rule-based reasoner (part A) and the R11 statistical AIEN faculty " |
| 846 | "(part B); not a neural model\",\n"); |
| 847 | fprintf(fp, " \"cortex\": \"in-memory host reference; L1/L2/L3 tiers not implemented\",\n"); |
| 848 | fprintf(fp, " \"histories\": \"generated with planted answers; %u subsystems, t_end %u\",\n", SUBJECTS, T_END); |
| 849 | fprintf(fp, " \"workloads\": [\n"); |
| 850 | for (uint32_t w = 0; w < W_COUNT; w++) { |
| 851 | const WStats *s = &g_w[w]; |
| 852 | uint32_t n = s->tasks; |
| 853 | fprintf(fp, " {\"name\": \"%s\", \"tasks\": %u, \"whole_store_correct\": %u, " |
| 854 | "\"projection_correct\": %u, \"answers_agree\": %u,\n", sp_workload_names[w], n, |
| 855 | s->full_ok, s->proj_ok, s->agree); |
| 856 | fprintf(fp, " \"median\": {\"whole_bytes\": %llu, \"projection_bytes\": %llu, \"whole_objects\": %llu, " |
| 857 | "\"projection_objects\": %llu, \"cortex_objects_examined\": %llu,\n", |
| 858 | (unsigned long long)median(s->full_bytes, n), (unsigned long long)median(s->proj_bytes, n), |
| 859 | (unsigned long long)median(s->full_objects, n), (unsigned long long)median(s->proj_objects, n), |
| 860 | (unsigned long long)median(s->examined, n)); |
| 861 | fprintf(fp, " \"whole_items_touched\": %llu, \"projection_items_touched\": %llu, " |
| 862 | "\"whole_words_read\": %llu, \"projection_words_read\": %llu,\n", |
| 863 | (unsigned long long)median(s->full_touched, n), (unsigned long long)median(s->proj_touched, n), |
| 864 | (unsigned long long)median(s->full_words, n), (unsigned long long)median(s->proj_words, n)); |
| 865 | fprintf(fp, " \"compile_ns\": %llu, \"projection_view_ns\": %llu, \"whole_view_ns\": %llu, " |
| 866 | "\"whole_reason_ns\": %llu, \"projection_reason_ns\": %llu, " |
| 867 | "\"whole_end_to_end_ns\": %llu, \"projection_end_to_end_ns\": %llu},\n", |
| 868 | (unsigned long long)median(s->compile_ns, n), (unsigned long long)median(s->proj_build_ns, n), |
| 869 | (unsigned long long)median(s->full_build_ns, n), (unsigned long long)median(s->full_reason_ns, n), |
| 870 | (unsigned long long)median(s->proj_reason_ns, n), (unsigned long long)median(s->full_total_ns, n), |
| 871 | (unsigned long long)median(s->proj_total_ns, n)); |
| 872 | fprintf(fp, " \"end_to_end_faster_tasks\": %u,\n", s->e2e_faster); |
| 873 | fprintf(fp, " \"recall_precision_x1000\": {\"whole\": %llu, \"projection\": %llu},\n", |
| 874 | (unsigned long long)(s->full_provided ? s->full_used * 1000 / s->full_provided : 0), |
| 875 | (unsigned long long)(s->proj_provided ? s->proj_used * 1000 / s->proj_provided : 0)); |
| 876 | fprintf(fp, " \"recall_x1000\": %llu,\n", |
| 877 | (unsigned long long)(s->needed ? s->needed_present * 1000 / s->needed : 0)); |
| 878 | fprintf(fp, " \"unnecessary_state_ratio_x1000\": {\"whole_by_trace\": %llu, \"projection_by_trace\": %llu, " |
| 879 | "\"projection_by_leave_one_out\": %llu},\n", |
| 880 | (unsigned long long)(s->full_provided ? 1000 - s->full_used * 1000 / s->full_provided : 0), |
| 881 | (unsigned long long)(s->proj_provided ? 1000 - s->proj_used * 1000 / s->proj_provided : 0), |
| 882 | (unsigned long long)(s->loo_items ? 1000 - s->loo_necessary * 1000 / s->loo_items : 0)); |
| 883 | fprintf(fp, " \"controls\": {\"leave_one_out_items\": %llu, \"leave_one_out_necessary\": %llu, " |
| 884 | "\"tasks_with_a_necessary_item\": %llu, \"newest_objects_same_bytes_correct\": %u, " |
| 885 | "\"whole_subject_correct\": %u, \"underspecified_need_correct\": %u, " |
| 886 | "\"underspecified_need_tasks\": %u},\n", |
| 887 | (unsigned long long)s->loo_items, (unsigned long long)s->loo_necessary, |
| 888 | (unsigned long long)s->tasks_with_necessary, s->recentk_ok, s->subject_ok, s->underspec_ok, |
| 889 | s->underspec_tasks); |
| 890 | fprintf(fp, " \"recoverability\": {\"checked\": %llu, \"failed\": %llu, \"tamper_tried\": %llu, " |
| 891 | "\"tamper_caught\": %llu, \"audits_ok\": %llu, \"excluded\": %llu, \"excluded_intact\": %llu, " |
| 892 | "\"other_branch_included\": %llu},\n", |
| 893 | (unsigned long long)s->recover_checked, (unsigned long long)s->recover_fail, |
| 894 | (unsigned long long)s->tamper_tried, (unsigned long long)s->tamper_caught, |
| 895 | (unsigned long long)s->audit_ok, (unsigned long long)s->excluded, |
| 896 | (unsigned long long)s->excluded_intact, (unsigned long long)s->other_branch); |
| 897 | fprintf(fp, " \"excluded_by_reason\": {\"subject\": %llu, \"type\": %llu, \"time\": %llu, " |
| 898 | "\"branch\": %llu, \"superseded\": %llu, \"derived_source\": %llu},\n", |
| 899 | (unsigned long long)s->by_why[PJ_WHY_SUBJECT], (unsigned long long)s->by_why[PJ_WHY_TYPE], |
| 900 | (unsigned long long)s->by_why[PJ_WHY_TIME], (unsigned long long)s->by_why[PJ_WHY_BRANCH], |
| 901 | (unsigned long long)s->by_why[PJ_WHY_SUPERSEDED], (unsigned long long)s->by_why[PJ_WHY_DERIVED_SOURCE]); |
| 902 | fprintf(fp, " \"projected_by_treatment\": {"); |
| 903 | for (uint32_t c = 1; c < PJ_STATE_CLASS_END; c++) |
| 904 | fprintf(fp, "%s\"%s\": %llu", c > 1 ? ", " : "", pj_state_class_name(c), |
| 905 | (unsigned long long)s->by_treatment[c]); |
| 906 | fprintf(fp, "}}%s\n", w + 1 < W_COUNT ? "," : ""); |
| 907 | } |
| 908 | fprintf(fp, " ],\n"); |
| 909 | fprintf(fp, " \"protected_state\": {\"attempts\": %u, \"refused\": %u, " |
| 910 | "\"kinds\": [\"authority\", \"commit receipt\", \"verification evidence\", \"generation identity\", " |
| 911 | "\"effect receipt\"], \"treatments_refused\": [\"COMPRESSIBLE\", \"DERIVED\", \"EPHEMERAL\"]},\n", |
| 912 | g_protect_attempts, g_protect_refusals); |
| 913 | fprintf(fp, " \"budget_below_pinned_refused\": %s,\n \"branch_local_without_branch_refused\": %s,\n" |
| 914 | " \"cortex_chain_verified\": %s,\n \"recallable_body_recalled\": %s,\n" |
| 915 | " \"ephemeral_outside_recoverability\": %s,\n", pass(g_budget_refused), pass(g_branch_refused), |
| 916 | pass(g_chain_ok), pass(g_recall_ok), pass(g_ephemeral_ok)); |
| 917 | uint32_t n = g_r11.runs; |
| 918 | fprintf(fp, " \"r11_faculty\": {\"runs\": %u, \"whole_history_correct\": %u, \"projection_correct\": %u, " |
| 919 | "\"plans_agree\": %u,\n \"median\": {\"whole_objects\": %llu, \"projection_objects\": %llu, " |
| 920 | "\"whole_bytes\": %llu, \"projection_bytes\": %llu, \"whole_aien_activations\": %llu, " |
| 921 | "\"projection_aien_activations\": %llu, \"whole_ns\": %llu, \"projection_ns_including_compile\": %llu, " |
| 922 | "\"compile_ns\": %llu}},\n", |
| 923 | n, g_r11.full_ok, g_r11.proj_ok, g_r11.agree, (unsigned long long)median(g_r11.full_objects, n), |
| 924 | (unsigned long long)median(g_r11.proj_objects, n), (unsigned long long)median(g_r11.full_bytes, n), |
| 925 | (unsigned long long)median(g_r11.proj_bytes, n), (unsigned long long)median(g_r11.full_act, n), |
| 926 | (unsigned long long)median(g_r11.proj_act, n), (unsigned long long)median(g_r11.full_ns, n), |
| 927 | (unsigned long long)median(g_r11.proj_ns, n), (unsigned long long)median(g_r11.compile_ns, n)); |
| 928 | fprintf(fp, " \"scale_explain_regression\": [\n"); |
| 929 | for (uint32_t i = 0; i < 3; i++) |
| 930 | fprintf(fp, " {\"subsystems\": %u, \"correct\": %u, \"median_whole_bytes\": %llu, " |
| 931 | "\"median_projection_bytes\": %llu, \"median_cortex_examined\": %llu, " |
| 932 | "\"median_whole_end_to_end_ns\": %llu, \"median_projection_end_to_end_ns\": %llu}%s\n", |
| 933 | g_scale[i].subjects, g_scale[i].ok, (unsigned long long)g_scale[i].full_bytes, |
| 934 | (unsigned long long)g_scale[i].proj_bytes, (unsigned long long)g_scale[i].examined, |
| 935 | (unsigned long long)g_scale[i].full_ns, (unsigned long long)g_scale[i].proj_ns, i < 2 ? "," : ""); |
| 936 | fprintf(fp, " ],\n"); |
| 937 | fprintf(fp, " \"gates\": {\n \"semantics_preserved\": %s,\n \"cost_reduced\": %s,\n" |
| 938 | " \"controls_fail_as_expected\": %s,\n \"protected_and_recoverable\": %s,\n" |
| 939 | " \"r11_faculty_grounded\": %s,\n \"OMEGA_STATE_PROJECTION_PASS\": \"%s\",\n" |
| 940 | " \"not_claimed\": [\"neural cognition\", \"Cortex storage tiers\", \"energy measurement\", " |
| 941 | "\"live production histories\", \"code generation\"]\n }\n}\n", |
| 942 | pass(preserve), pass(reduce), pass(controls), pass(recover), pass(grounded), |
| 943 | gate_pass ? "PASS" : "FAIL"); |
| 944 | fclose(fp); |
| 945 | printf("receipt: %s\n", path); |
| 946 | } |
| 947 | |
| 948 | static void summary_boundaries(void) { |
| 949 | const uint32_t n = 1000001; |
| 950 | uint64_t *words = calloc(n, sizeof(*words)); |
| 951 | CHECK(words != NULL, "allocate projection boundary fixture"); |
| 952 | if (!words) return; |
| 953 | words[n - 1] = 3; |
| 954 | pj_summarize(words, n, words); |
| 955 | const uint64_t expected[] = {n, 3, 9, 0, 3}; |
| 956 | CHECK(memcmp(words, expected, sizeof expected) == 0, "large aliased summary"); |
| 957 | pj_summarize(NULL, 0, words); |
| 958 | const uint64_t empty[PJ_SUMMARY_WORDS] = {0}; |
| 959 | CHECK(memcmp(words, empty, sizeof empty) == 0, "empty summary"); |
| 960 | free(words); |
| 961 | } |
| 962 | |
| 963 | int main(void) { |
| 964 | signal(SIGPIPE, SIG_IGN); |
| 965 | summary_boundaries(); |
| 966 | printf("[*] part A: five operations x %u planted histories, whole store vs projection\n", SEEDS); |
| 967 | for (uint32_t w = 0; w < W_COUNT; w++) { |
| 968 | for (uint32_t seed = 0; seed < SEEDS; seed++) run_task(w, seed); |
| 969 | const WStats *s = &g_w[w]; |
| 970 | printf(" %-30s correct whole %2u/%u projection %2u/%u bytes %7llu -> %5llu " |
| 971 | "touched %6llu -> %4llu end-to-end %7llu -> %6llu ns\n", |
| 972 | sp_workload_names[w], s->full_ok, s->tasks, s->proj_ok, s->tasks, |
| 973 | (unsigned long long)median(s->full_bytes, s->tasks), (unsigned long long)median(s->proj_bytes, s->tasks), |
| 974 | (unsigned long long)median(s->full_touched, s->tasks), |
| 975 | (unsigned long long)median(s->proj_touched, s->tasks), |
| 976 | (unsigned long long)median(s->full_total_ns, s->tasks), |
| 977 | (unsigned long long)median(s->proj_total_ns, s->tasks)); |
| 978 | printf(" %-30s controls: newest-same-bytes %u/%u, whole-subject %u/%u, under-specified %u/%u, " |
| 979 | "leave-one-out necessary %llu/%llu\n", "", s->recentk_ok, s->tasks, s->subject_ok, s->tasks, |
| 980 | s->underspec_ok, s->underspec_tasks, (unsigned long long)s->loo_necessary, |
| 981 | (unsigned long long)s->loo_items); |
| 982 | } |
| 983 | printf("[*] part B: the R11 AIEN faculty, whole regime history vs projection\n"); |
| 984 | part_b(); |
| 985 | printf(" correct whole %u/%u projection %u/%u AIEN activations %llu -> %llu objects %llu -> %llu\n", |
| 986 | g_r11.full_ok, g_r11.runs, g_r11.proj_ok, g_r11.runs, |
| 987 | (unsigned long long)median(g_r11.full_act, g_r11.runs), (unsigned long long)median(g_r11.proj_act, g_r11.runs), |
| 988 | (unsigned long long)median(g_r11.full_objects, g_r11.runs), |
| 989 | (unsigned long long)median(g_r11.proj_objects, g_r11.runs)); |
| 990 | printf("[*] part C: scale\n"); |
| 991 | part_c(); |
| 992 | for (uint32_t i = 0; i < 3; i++) |
| 993 | printf(" %3u subsystems: bytes %9llu -> %6llu end-to-end %9llu -> %7llu ns correct %u/8\n", |
| 994 | g_scale[i].subjects, (unsigned long long)g_scale[i].full_bytes, |
| 995 | (unsigned long long)g_scale[i].proj_bytes, (unsigned long long)g_scale[i].full_ns, |
| 996 | (unsigned long long)g_scale[i].proj_ns, g_scale[i].ok); |
| 997 | |
| 998 | /* ---- the gate, from what ran ---- */ |
| 999 | int preserve = 1, reduce = 1, controls = 1, recover = 1; |
| 1000 | for (uint32_t w = 0; w < W_COUNT; w++) { |
| 1001 | const WStats *s = &g_w[w]; |
| 1002 | preserve &= s->tasks == SEEDS && s->full_ok == s->tasks && s->proj_ok == s->tasks && s->agree == s->tasks; |
| 1003 | reduce &= median(s->proj_bytes, s->tasks) < median(s->full_bytes, s->tasks) && |
| 1004 | median(s->proj_touched, s->tasks) < median(s->full_touched, s->tasks) && |
| 1005 | median(s->proj_total_ns, s->tasks) < median(s->full_total_ns, s->tasks) && |
| 1006 | /* even when the whole store costs nothing to hand over */ |
| 1007 | median(s->proj_total_ns, s->tasks) < median(s->full_reason_ns, s->tasks); |
| 1008 | controls &= s->tasks_with_necessary == s->tasks; |
| 1009 | recover &= s->recover_fail == 0 && s->tamper_caught == s->tamper_tried && s->tamper_tried > 0 && |
| 1010 | s->audit_ok == s->tasks && s->other_branch == 0; |
| 1011 | } |
| 1012 | /* a same-size naive projection must lose somewhere, and so must an under-specified need */ |
| 1013 | uint32_t recent_all = 0, under_all = 0, under_tasks = 0; |
| 1014 | for (uint32_t w = 0; w < W_COUNT; w++) { |
| 1015 | recent_all += g_w[w].recentk_ok; |
| 1016 | under_all += g_w[w].underspec_ok; |
| 1017 | under_tasks += g_w[w].underspec_tasks; |
| 1018 | } |
| 1019 | controls &= recent_all < W_COUNT * SEEDS && under_all < under_tasks && |
| 1020 | g_w[W_EXPLAIN].subject_ok < g_w[W_EXPLAIN].tasks; |
| 1021 | recover &= g_protect_refusals == g_protect_attempts && g_protect_attempts == 15 * W_COUNT && g_budget_refused && |
| 1022 | g_branch_refused && g_chain_ok && g_recall_ok && g_ephemeral_ok; |
| 1023 | int grounded = g_r11.runs == 8 && g_r11.full_ok == 8 && g_r11.proj_ok == 8 && g_r11.agree == 8 && |
| 1024 | median(g_r11.proj_act, 8) < median(g_r11.full_act, 8); |
| 1025 | CHECK(preserve, "semantics not preserved"); |
| 1026 | CHECK(reduce, "cost not reduced"); |
| 1027 | CHECK(controls, "a control did not fail as expected"); |
| 1028 | CHECK(recover, "protection or recoverability"); |
| 1029 | CHECK(grounded, "R11 grounding"); |
| 1030 | int gate = preserve && reduce && controls && recover && grounded && g_fail == 0; |
| 1031 | printf("checks %d failures %d\nOMEGA_STATE_PROJECTION_PASS %s\n", g_checks, g_fail, gate ? "PASS" : "FAIL"); |
| 1032 | write_receipt(gate, preserve, reduce, controls, recover, grounded); |
| 1033 | return gate ? 0 : 1; |
| 1034 | } |