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/rx_projection.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 / rx_projection.c` 746 lines ยท 26157 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 * rx_projection.c -- Omega cognitive state projection (host reference). 19 * See rx_projection.h. 20 */ 21 #include "rx_projection.h" 22 23 #include "omega_api_decl.h" 24 #include "sha256.h" 25 26 #include 27 #include 28 #include 29 30 static uint64_t now_ns(void) { 31 struct timespec ts; 32 clock_gettime(CLOCK_MONOTONIC, &ts); 33 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 34 } 35 36 static const char *const g_class_names[] = { 37 "?", "PINNED", "LIVE", "DERIVED", "RECALLABLE", "COMPRESSIBLE", 38 "REFERENCE_ONLY", "EPHEMERAL", "BRANCH_LOCAL", "PROTECTED_EVIDENCE" 39 }; 40 41 const char *pj_state_class_name(uint32_t c) { 42 return c < PJ_STATE_CLASS_END ? g_class_names[c] : "?"; 43 } 44 45 void pj_need_init(CognitiveNeed *n, uint32_t operation, uint64_t subject, uint64_t t_now) { 46 memset(n, 0, sizeof(*n)); 47 n->operation_class = operation; 48 n->subject = subject; 49 n->required_temporal_scope.anchor = PJ_A_FIXED; 50 n->required_temporal_scope.t_now = t_now; 51 n->uncertainty_requirement = PJ_U_NONE; 52 n->evidence_requirement = PJ_E_NONE; 53 n->memory_scope = PJ_M_WORKING; 54 } 55 56 int pj_need_add(CognitiveNeed *n, uint32_t cls, uint32_t kind, uint32_t treatment, 57 uint32_t world, uint32_t temporal) { 58 if (n->n_fact_types >= PJ_MAX_FACTS) return PJ_ERR_ARG; 59 n->required_fact_types[n->n_fact_types++] = (PjFactReq){ cls, kind, treatment, world, temporal }; 60 return PJ_OK; 61 } 62 63 void pj_summarize(const uint64_t *w, uint32_t n, uint64_t out[PJ_SUMMARY_WORDS]) { 64 uint64_t summary[PJ_SUMMARY_WORDS]; 65 rx_pj_summarize(w, n, summary); 66 memcpy(out, summary, sizeof summary); 67 } 68 69 /* ---- a small id set ---- */ 70 71 typedef struct { 72 uint64_t *slot; 73 uint32_t cap, n; 74 } IdSet; 75 76 static int set_has(const IdSet *s, uint64_t id) { 77 if (!s->cap) return 0; 78 for (uint32_t i = (uint32_t)(id * 0x9E3779B97F4A7C15ull >> 40) & (s->cap - 1);; i = (i + 1) & (s->cap - 1)) { 79 if (s->slot[i] == id) return 1; 80 if (s->slot[i] == 0) return 0; 81 } 82 } 83 84 static int set_add(IdSet *s, uint64_t id) { 85 if ((s->n + 1) * 2 > s->cap) { 86 uint32_t c = s->cap ? s->cap * 2 : 256; 87 uint64_t *q = calloc(c, sizeof(uint64_t)); 88 if (!q) return -1; 89 for (uint32_t i = 0; i < s->cap; i++) { 90 uint64_t v = s->slot[i]; 91 if (!v) continue; 92 uint32_t j = (uint32_t)(v * 0x9E3779B97F4A7C15ull >> 40) & (c - 1); 93 while (q[j]) j = (j + 1) & (c - 1); 94 q[j] = v; 95 } 96 free(s->slot); 97 s->slot = q; 98 s->cap = c; 99 } 100 uint32_t i = (uint32_t)(id * 0x9E3779B97F4A7C15ull >> 40) & (s->cap - 1); 101 while (s->slot[i]) { 102 if (s->slot[i] == id) return 0; 103 i = (i + 1) & (s->cap - 1); 104 } 105 s->slot[i] = id; 106 s->n++; 107 return 1; 108 } 109 110 static void set_free(IdSet *s) { free(s->slot); memset(s, 0, sizeof(*s)); } 111 112 /* ---- compile ---- */ 113 114 typedef struct { 115 CxStore *s; 116 const CognitiveNeed *need; 117 StateProjection *p; 118 IdSet in; 119 uint64_t w0, now; 120 uint64_t subjects[64]; 121 uint32_t n_subjects; 122 int err; 123 } Ctx; 124 125 static int add_subject(uint64_t *list, uint32_t *n, uint32_t cap, uint64_t s) { 126 for (uint32_t i = 0; i < *n; i++) 127 if (list[i] == s) return 0; 128 if (*n >= cap) return -1; 129 list[(*n)++] = s; 130 return 1; 131 } 132 133 static int is_protected(const CxObject *o) { return o->protect != 0 || o->cls == CX_EVIDENCE; } 134 135 static uint32_t section_of(const CxObject *o, uint32_t treatment, uint32_t temporal) { 136 return (uint32_t)rx_pj_section_of(o->protect, o->cls, treatment, temporal); 137 } 138 139 static uint32_t form_of(uint32_t treatment) { 140 return (uint32_t)rx_pj_form_of(treatment); 141 } 142 143 static int grow(void **p, uint64_t *cap, uint64_t need, size_t elem) { 144 if (need <= *cap) return 0; 145 uint64_t c = *cap ? *cap : 64; 146 while (c < need) c *= 2; 147 void *q = realloc(*p, c * elem); 148 if (!q) return -1; 149 *p = q; 150 *cap = c; 151 return 0; 152 } 153 154 static int add_entry(Ctx *c, const CxObject *o, uint32_t section, uint32_t treatment, uint32_t form) { 155 StateProjection *p = c->p; 156 if (set_has(&c->in, o->id)) return PJ_OK; 157 if (is_protected(o) && (form == PJ_FORM_SUMMARY || treatment == PJ_EPHEMERAL)) 158 return PJ_ERR_PROTECTED; 159 uint64_t cap = p->cap; 160 if (grow((void **)&p->e, &cap, (uint64_t)p->n + 1, sizeof(PjEntry)) || set_add(&c->in, o->id) < 0) 161 return PJ_ERR_NOMEM; 162 p->cap = (uint32_t)cap; 163 PjEntry *e = &p->e[p->n++]; 164 memset(e, 0, sizeof(*e)); 165 e->id = o->id; 166 e->section = section; 167 e->treatment = treatment; 168 e->form = form; 169 memcpy(e->digest, o->digest, 32); 170 p->bytes += PJ_HEADER_BYTES; 171 if (form == PJ_FORM_FULL) { 172 p->bytes += (uint64_t)o->n * PJ_WORD_BYTES; 173 } else if (form == PJ_FORM_SUMMARY) { 174 if (grow((void **)&p->arena, &p->arena_cap, p->arena_n + PJ_SUMMARY_WORDS, sizeof(uint64_t))) 175 return PJ_ERR_NOMEM; 176 e->off = p->arena_n; 177 e->n = PJ_SUMMARY_WORDS; 178 pj_summarize(cx_payload(c->s, o), o->n, p->arena + p->arena_n); 179 p->arena_n += PJ_SUMMARY_WORDS; 180 p->bytes += PJ_SUMMARY_WORDS * PJ_WORD_BYTES; 181 } else { 182 p->bytes += PJ_DIGEST_BYTES; 183 } 184 if (form != PJ_FORM_FULL && treatment != PJ_EPHEMERAL) { 185 uint64_t rcap = p->rcap; 186 if (grow((void **)&p->r, &rcap, (uint64_t)p->nr + 1, sizeof(PjRecover))) return PJ_ERR_NOMEM; 187 p->rcap = (uint32_t)rcap; 188 PjRecover *r = &p->r[p->nr++]; 189 r->id = o->id; 190 r->form = form; 191 memcpy(r->digest, o->digest, 32); 192 } 193 add_subject(c->subjects, &c->n_subjects, 64, o->subject); 194 return PJ_OK; 195 } 196 197 /* Linked evidence and protected state, as the need's evidence requirement says. */ 198 static int follow_evidence(Ctx *c, const CxObject *o) { 199 uint32_t req = c->need->evidence_requirement; 200 if (req != PJ_E_REFS && req != PJ_E_FULL) return PJ_OK; 201 for (uint32_t i = 0; i < CX_LINKS; i++) { 202 const CxObject *l = cx_get(c->s, o->links[i]); 203 if (!l || !is_protected(l)) continue; 204 if (l->branch != 0 && l->branch != c->need->branch) continue; 205 c->s->examined++; 206 int rc = add_entry(c, l, PJ_SEC_EVIDENCE, PJ_PROTECTED_EVIDENCE, 207 req == PJ_E_FULL ? PJ_FORM_FULL : PJ_FORM_REF); 208 if (rc) return rc; 209 } 210 return PJ_OK; 211 } 212 213 static int include(Ctx *c, const CxObject *o, const PjFactReq *f, uint32_t temporal) { 214 if (is_protected(o) && (f->treatment == PJ_COMPRESSIBLE || f->treatment == PJ_DERIVED || 215 f->treatment == PJ_EPHEMERAL)) 216 return PJ_ERR_PROTECTED; 217 int rc = add_entry(c, o, section_of(o, f->treatment, temporal), f->treatment, form_of(f->treatment)); 218 if (rc) return rc; 219 return follow_evidence(c, o); 220 } 221 222 typedef struct { 223 Ctx *c; 224 const PjFactReq *f; 225 uint32_t temporal; 226 } Visit; 227 228 static int visit_include(void *user, const CxObject *o) { 229 Visit *v = user; 230 int rc = include(v->c, o, v->f, v->temporal); 231 if (rc) { 232 v->c->err = rc; 233 return 1; 234 } 235 return 0; 236 } 237 238 int pj_feature_compute(CxStore *s, uint64_t subject, uint32_t cls, uint32_t kind, uint32_t branch, 239 uint64_t t0, uint64_t t1, PjFeature *out) { 240 memset(out, 0, sizeof(*out)); 241 out->cls = cls; 242 out->kind = kind; 243 out->branch = branch; 244 out->subject = subject; 245 out->t0 = t0; 246 out->t1 = t1; 247 out->min = UINT64_MAX; 248 if (subject >= s->n_subjects) return PJ_ERR_ARG; 249 const CxIdList *l = &s->by_subject[subject]; 250 sha256_ctx h; 251 sha256_init(&h); 252 for (uint32_t i = 0; i < l->n; i++) { 253 const CxObject *o = &s->obj[l->ids[i] - 1]; 254 if (o->t < t0) continue; 255 if (o->t > t1) break; 256 s->examined++; 257 if (o->cls != cls || o->kind != kind || o->branch != branch) continue; 258 if (is_protected(o)) return PJ_ERR_PROTECTED; 259 const uint64_t *w = cx_payload(s, o); 260 for (uint32_t k = 0; k < o->n; k++) { 261 out->sum += w[k]; 262 out->sumsq += w[k] * w[k]; 263 if (w[k] < out->min) out->min = w[k]; 264 if (w[k] > out->max) out->max = w[k]; 265 } 266 out->count += o->n; 267 out->n_src++; 268 sha256_update(&h, o->digest, 32); 269 } 270 if (!out->count) out->min = 0; 271 sha256_final(&h, out->src_digest); 272 return PJ_OK; 273 } 274 275 /* Per-read cx_verify stays here: a store may be memory-only (never through cx_open), 276 * and a projection binds to exact object digests, so a source edited after the build 277 * must be caught (tests/runtime/rx_state_projection.c tamper case, lines 390-408). */ 278 /* The subject's newest realization whose linked evidence verifies. */ 279 static const CxObject *verified_anchor(Ctx *c) { 280 const CognitiveNeed *n = c->need; 281 const CxIdList *l = &c->s->by_subject[n->subject]; 282 for (uint32_t i = l->n; i > 0; i--) { 283 const CxObject *o = &c->s->obj[l->ids[i - 1] - 1]; 284 c->s->examined++; 285 if (o->t > c->now || o->branch != 0 || o->cls != CX_REALIZATION || 286 o->kind != n->required_temporal_scope.realization_kind) 287 continue; 288 const CxObject *ev = cx_get(c->s, o->links[0]); 289 if (ev && (ev->protect & CX_PROT_VERIFY_EVIDENCE) && ev->tag == PJ_VERIFIED && 290 cx_verify(c->s, ev->id) == CX_OK) 291 return o; 292 } 293 return NULL; 294 } 295 296 static int resolve_window(Ctx *c) { 297 const CognitiveNeed *n = c->need; 298 StateProjection *p = c->p; 299 PjWindow *w = &p->w[p->nw++]; 300 memset(w, 0, sizeof(*w)); 301 w->subject = n->subject; 302 w->anchor = n->required_temporal_scope.anchor; 303 w->treatment = PJ_EPHEMERAL; 304 w->t1 = c->now; 305 if (w->anchor == PJ_A_FIXED) { 306 c->w0 = n->required_temporal_scope.t0; 307 } else if (w->anchor == PJ_A_SINCE_VERIFIED) { 308 const CxObject *a = verified_anchor(c); 309 if (!a) return PJ_ERR_NO_ANCHOR; 310 c->w0 = a->t; 311 w->anchor_id = a->id; 312 int rc = add_entry(c, a, PJ_SEC_CORTEX, PJ_PINNED, PJ_FORM_FULL); 313 if (rc) return rc; 314 const CxObject *ev = cx_get(c->s, a->links[0]); 315 rc = add_entry(c, ev, PJ_SEC_EVIDENCE, PJ_PROTECTED_EVIDENCE, 316 n->evidence_requirement == PJ_E_FULL ? PJ_FORM_FULL : PJ_FORM_REF); 317 if (rc) return rc; 318 } else if (w->anchor == PJ_A_GENERATION) { 319 CxFilter f = { CX_ENTITY, n->required_temporal_scope.generation_kind, 0, 0 }; 320 uint64_t id = cx_latest(c->s, 0, c->now, &f); 321 const CxObject *g = cx_get(c->s, id); 322 if (!g) return PJ_ERR_NO_ANCHOR; 323 c->w0 = g->t; 324 w->anchor_id = g->id; 325 int rc = add_entry(c, g, PJ_SEC_WORLD, PJ_PINNED, PJ_FORM_FULL); 326 if (rc) return rc; 327 } else { 328 return PJ_ERR_ARG; 329 } 330 w->t0 = c->w0; 331 return PJ_OK; 332 } 333 334 /* Subjects a fact type draws from. */ 335 static uint32_t fact_subjects(Ctx *c, const PjFactReq *f, uint64_t *out, uint64_t *deps, uint32_t nd) { 336 if (f->world == PJ_W_MACHINE) { 337 out[0] = 0; 338 return 1; 339 } 340 out[0] = c->need->subject; 341 uint32_t n = 1; 342 if (f->world == PJ_W_DEPENDENCIES) 343 for (uint32_t i = 0; i < nd && n < 64; i++) out[n++] = deps[i]; 344 return n; 345 } 346 347 static int run_fact(Ctx *c, const PjFactReq *f, uint64_t *deps, uint32_t nd) { 348 const CognitiveNeed *n = c->need; 349 uint32_t temporal = f->temporal; 350 if (temporal == PJ_T_MEMORY) temporal = n->memory_scope == PJ_M_EPISODIC ? PJ_T_ALL : PJ_T_WINDOW; 351 uint32_t branch = 0; 352 if (f->treatment == PJ_BRANCH_LOCAL) { 353 if (n->branch == 0) return PJ_ERR_BRANCH; 354 branch = n->branch; 355 } 356 CxFilter filt = { f->cls, f->kind, branch, 0 }; 357 uint64_t subj[64]; 358 uint32_t ns = fact_subjects(c, f, subj, deps, nd); 359 for (uint32_t i = 0; i < ns; i++) { 360 uint64_t t0 = temporal == PJ_T_ALL ? 0 : c->w0; 361 if (f->treatment == PJ_DERIVED) { 362 if (temporal != PJ_T_WINDOW && temporal != PJ_T_ALL) return PJ_ERR_ARG; 363 StateProjection *p = c->p; 364 if (p->nf >= sizeof p->f / sizeof p->f[0]) return PJ_ERR_ARG; 365 PjFeature *ft = &p->f[p->nf]; 366 int rc = pj_feature_compute(c->s, subj[i], f->cls, f->kind, branch, t0, c->now, ft); 367 if (rc) return rc; 368 ft->section = n->uncertainty_requirement == PJ_U_INTERVAL ? PJ_FEAT_UNCERTAINTY : PJ_FEAT_DERIVED; 369 p->nf++; 370 p->bytes += PJ_FEATURE_BYTES; 371 add_subject(c->subjects, &c->n_subjects, 64, subj[i]); 372 continue; 373 } 374 Visit v = { c, f, temporal }; 375 if (temporal == PJ_T_LATEST) { 376 const CxObject *o = cx_get(c->s, cx_latest(c->s, subj[i], c->now, &filt)); 377 if (o) { 378 int rc = include(c, o, f, temporal); 379 if (rc) return rc; 380 } 381 continue; 382 } 383 if (temporal == PJ_T_CARRY_IN && c->w0 > 0) { 384 const CxObject *o = cx_get(c->s, cx_latest(c->s, subj[i], c->w0 - 1, &filt)); 385 if (o) { 386 int rc = include(c, o, f, temporal); 387 if (rc) return rc; 388 } 389 } 390 cx_range(c->s, subj[i], t0, c->now, &filt, visit_include, &v); 391 if (c->err) return c->err; 392 } 393 return PJ_OK; 394 } 395 396 static int needs_deps(const CognitiveNeed *n) { 397 for (uint32_t i = 0; i < n->n_fact_types; i++) 398 if (n->required_fact_types[i].world == PJ_W_DEPENDENCIES) return 1; 399 return 0; 400 } 401 402 typedef struct { 403 Ctx *c; 404 uint64_t *deps; 405 uint32_t nd; 406 } DepVisit; 407 408 static int visit_dep(void *user, const CxObject *o) { 409 DepVisit *v = user; 410 int rc = add_entry(v->c, o, PJ_SEC_CORTEX, PJ_REFERENCE_ONLY, PJ_FORM_REF); 411 if (rc) { 412 v->c->err = rc; 413 return 1; 414 } 415 if (o->tag < v->c->s->n_subjects && o->tag != v->c->need->subject) 416 add_subject(v->deps, &v->nd, 63, o->tag); 417 return 0; 418 } 419 420 static int cmp_entry(const void *a, const void *b) { 421 uint64_t x = ((const PjEntry *)a)->id, y = ((const PjEntry *)b)->id; 422 return x < y ? -1 : x > y; 423 } 424 425 int pj_compile(CxStore *s, const CognitiveNeed *need, StateProjection *out) { 426 memset(out, 0, sizeof(*out)); 427 if (!s || !need || need->subject >= s->n_subjects || need->n_fact_types > PJ_MAX_FACTS) 428 return PJ_ERR_ARG; 429 uint64_t t_start = now_ns(), ex0 = s->examined; 430 Ctx c; 431 memset(&c, 0, sizeof c); 432 c.s = s; 433 c.need = need; 434 c.p = out; 435 c.now = need->required_temporal_scope.t_now; 436 out->store_count = s->n; 437 memcpy(out->store_chain, s->chain, 32); 438 439 int rc = resolve_window(&c); 440 uint64_t deps[64]; 441 uint32_t nd = 0; 442 if (!rc && needs_deps(need)) { 443 CxFilter f = { CX_RELATIONSHIP, need->required_world_scope.dependency_kind, 0, 0 }; 444 DepVisit dv = { &c, deps, 0 }; 445 cx_range(s, need->subject, 0, c.now, &f, visit_dep, &dv); 446 rc = c.err; 447 nd = dv.nd; 448 } 449 if (!rc && need->focus) { 450 const CxObject *fo = cx_get(s, need->focus); 451 if (!fo) rc = PJ_ERR_ARG; 452 else { 453 rc = add_entry(&c, fo, section_of(fo, PJ_PINNED, 0), PJ_PINNED, PJ_FORM_FULL); 454 if (!rc) rc = follow_evidence(&c, fo); 455 } 456 } 457 for (uint32_t i = 0; !rc && i < need->n_fact_types; i++) 458 rc = run_fact(&c, &need->required_fact_types[i], deps, nd); 459 460 if (!rc) { 461 out->objects = out->n + out->nf; 462 const uint64_t mb = need->resource_budget.max_bytes, mo = need->resource_budget.max_objects; 463 if ((mb && out->bytes > mb) || (mo && out->objects > mo)) rc = PJ_ERR_BUDGET; 464 } 465 if (!rc) { 466 qsort(out->e, out->n, sizeof(PjEntry), cmp_entry); 467 uint64_t in_scope = 0; 468 for (uint32_t i = 0; i < c.n_subjects && out->nx < 71; i++) { 469 uint64_t sj = c.subjects[i], mine = 0; 470 for (uint32_t k = 0; k < out->n; k++) 471 if (s->obj[out->e[k].id - 1].subject == sj) mine++; 472 in_scope += cx_count_subject(s, sj); 473 out->x[out->nx++] = (PjExclusion){ PJ_X_IN_SCOPE_SUBJECT, sj, cx_count_subject(s, sj) - mine }; 474 } 475 out->x[out->nx++] = (PjExclusion){ PJ_X_OTHER_SUBJECTS, UINT64_MAX, s->n - in_scope }; 476 } 477 set_free(&c.in); 478 out->examined = s->examined - ex0; 479 out->compile_ns = now_ns() - t_start; 480 if (rc) { 481 pj_free(out); 482 return rc; 483 } 484 return PJ_OK; 485 } 486 487 void pj_free(StateProjection *p) { 488 free(p->e); 489 free(p->r); 490 free(p->arena); 491 p->e = NULL; 492 p->r = NULL; 493 p->arena = NULL; 494 p->n = p->cap = p->nr = p->rcap = 0; 495 p->arena_n = p->arena_cap = 0; 496 } 497 498 /* ---- views ---- */ 499 500 static void item_from(PjItem *it, const CxStore *s, const CxObject *o) { 501 it->id = o->id; 502 it->cls = o->cls; 503 it->kind = o->kind; 504 it->subject = o->subject; 505 it->t = o->t; 506 it->generation = o->generation; 507 it->tag = o->tag; 508 memcpy(it->links, o->links, sizeof it->links); 509 it->branch = o->branch; 510 it->protect = o->protect; 511 it->n = o->n; 512 it->w = cx_payload(s, o); 513 } 514 515 int pj_view_projection(const CxStore *s, const StateProjection *p, PjView *v) { 516 uint64_t t0 = now_ns(); 517 memset(v, 0, sizeof(*v)); 518 v->items = calloc(p->n ? p->n : 1, sizeof(PjItem)); 519 if (!v->items) return PJ_ERR_NOMEM; 520 for (uint32_t i = 0; i < p->n; i++) { 521 const PjEntry *e = &p->e[i]; 522 PjItem *it = &v->items[i]; 523 item_from(it, s, cx_get(s, e->id)); 524 it->section = e->section; 525 it->treatment = e->treatment; 526 it->form = e->form; 527 if (e->form == PJ_FORM_SUMMARY) { 528 it->n = e->n; 529 it->w = p->arena + e->off; 530 } else if (e->form == PJ_FORM_REF) { 531 it->n = 0; 532 it->w = NULL; 533 } 534 } 535 v->n = p->n; 536 v->feat = p->f; 537 v->nf = p->nf; 538 v->bytes = p->bytes; 539 v->build_ns = now_ns() - t0; 540 return PJ_OK; 541 } 542 543 uint64_t pj_full_bytes(const CxStore *s) { 544 return s->n * (uint64_t)PJ_HEADER_BYTES + s->arena_n * (uint64_t)PJ_WORD_BYTES; 545 } 546 547 int pj_view_full(const CxStore *s, PjView *v) { 548 uint64_t t0 = now_ns(); 549 memset(v, 0, sizeof(*v)); 550 v->items = calloc(s->n ? s->n : 1, sizeof(PjItem)); 551 if (!v->items) return PJ_ERR_NOMEM; 552 for (uint64_t i = 0; i < s->n; i++) { 553 item_from(&v->items[i], s, &s->obj[i]); 554 v->items[i].section = PJ_SEC_CORTEX; 555 v->items[i].treatment = PJ_PINNED; 556 v->items[i].form = PJ_FORM_FULL; 557 } 558 v->n = (uint32_t)s->n; 559 v->dense = 1; 560 v->bytes = pj_full_bytes(s); 561 v->build_ns = now_ns() - t0; 562 return PJ_OK; 563 } 564 565 static int cmp_item(const void *a, const void *b) { 566 uint64_t x = ((const PjItem *)a)->id, y = ((const PjItem *)b)->id; 567 return x < y ? -1 : x > y; 568 } 569 570 int pj_view_ids(const CxStore *s, const uint64_t *ids, uint32_t n, PjView *v) { 571 uint64_t t0 = now_ns(); 572 memset(v, 0, sizeof(*v)); 573 v->items = calloc(n ? n : 1, sizeof(PjItem)); 574 if (!v->items) return PJ_ERR_NOMEM; 575 for (uint32_t i = 0; i < n; i++) { 576 const CxObject *o = cx_get(s, ids[i]); 577 if (!o) return PJ_ERR_ARG; 578 item_from(&v->items[i], s, o); 579 v->items[i].section = PJ_SEC_CORTEX; 580 v->items[i].treatment = PJ_PINNED; 581 v->items[i].form = PJ_FORM_FULL; 582 v->bytes += PJ_HEADER_BYTES + (uint64_t)o->n * PJ_WORD_BYTES; 583 } 584 qsort(v->items, n, sizeof(PjItem), cmp_item); 585 v->n = n; 586 v->build_ns = now_ns() - t0; 587 return PJ_OK; 588 } 589 590 void pj_view_free(PjView *v) { 591 free(v->items); 592 memset(v, 0, sizeof(*v)); 593 } 594 595 const PjItem *pj_view_find(const PjView *v, uint64_t id) { 596 if (v->dense) return id >= 1 && id <= v->n ? &v->items[id - 1] : NULL; 597 uint32_t lo = 0, hi = v->n; 598 while (lo < hi) { 599 uint32_t mid = lo + (hi - lo) / 2; 600 if (v->items[mid].id < id) lo = mid + 1; 601 else hi = mid; 602 } 603 return lo < v->n && v->items[lo].id == id ? &v->items[lo] : NULL; 604 } 605 606 const uint64_t *pj_recall(const CxStore *s, const StateProjection *p, uint64_t id, uint32_t *n_out) { 607 for (uint32_t i = 0; i < p->nr; i++) { 608 if (p->r[i].id != id) continue; 609 const CxObject *o = cx_get(s, id); 610 if (!o || memcmp(o->digest, p->r[i].digest, 32) != 0 || cx_verify(s, id) != CX_OK) return NULL; 611 if (n_out) *n_out = o->n; 612 return cx_payload(s, o); 613 } 614 return NULL; 615 } 616 617 /* ---- recoverability ---- */ 618 619 int pj_recover_all(CxStore *s, const StateProjection *p, uint64_t *checked) { 620 uint64_t k = 0; 621 int bad = 0; 622 for (uint32_t i = 0; i < p->n; i++) { 623 const CxObject *o = cx_get(s, p->e[i].id); 624 k++; 625 if (!o || memcmp(o->digest, p->e[i].digest, 32) != 0 || cx_verify(s, o->id) != CX_OK) bad++; 626 } 627 for (uint32_t i = 0; i < p->nr; i++) { 628 const CxObject *o = cx_get(s, p->r[i].id); 629 k++; 630 if (!o || memcmp(o->digest, p->r[i].digest, 32) != 0 || cx_verify(s, o->id) != CX_OK) { 631 bad++; 632 continue; 633 } 634 if (p->r[i].form == PJ_FORM_SUMMARY) { 635 /* The summary must be what the verified source summarizes to. */ 636 const PjEntry *e = NULL; 637 for (uint32_t j = 0; j < p->n && !e; j++) 638 if (p->e[j].id == o->id) e = &p->e[j]; 639 uint64_t sm[PJ_SUMMARY_WORDS]; 640 pj_summarize(cx_payload(s, o), o->n, sm); 641 if (!e || memcmp(sm, p->arena + e->off, sizeof sm) != 0) bad++; 642 } 643 } 644 for (uint32_t i = 0; i < p->nf; i++) { 645 const PjFeature *f = &p->f[i]; 646 PjFeature g; 647 k++; 648 if (pj_feature_compute(s, f->subject, f->cls, f->kind, f->branch, f->t0, f->t1, &g) != PJ_OK || 649 g.n_src != f->n_src || g.count != f->count || g.sum != f->sum || g.sumsq != f->sumsq || 650 g.min != f->min || g.max != f->max || memcmp(g.src_digest, f->src_digest, 32) != 0) 651 bad++; 652 /* each source object must verify too */ 653 const CxIdList *l = &s->by_subject[f->subject]; 654 for (uint32_t j = 0; j < l->n; j++) { 655 const CxObject *o = &s->obj[l->ids[j] - 1]; 656 if (o->t < f->t0 || o->t > f->t1 || o->cls != f->cls || o->kind != f->kind || o->branch != f->branch) 657 continue; 658 k++; 659 if (cx_verify(s, o->id) != CX_OK) bad++; 660 } 661 } 662 if (checked) *checked = k; 663 return bad ? PJ_ERR_RECOVER : PJ_OK; 664 } 665 666 /* ---- audit ---- */ 667 668 static int in_list(const uint64_t *l, uint32_t n, uint64_t v) { 669 for (uint32_t i = 0; i < n; i++) 670 if (l[i] == v) return 1; 671 return 0; 672 } 673 674 int pj_audit(CxStore *s, const CognitiveNeed *need, const StateProjection *p, PjAudit *a) { 675 memset(a, 0, sizeof(*a)); 676 IdSet in = { 0 }; 677 uint64_t scope[64]; 678 uint32_t nscope = 0; 679 for (uint32_t i = 0; i < p->n; i++) { 680 if (set_add(&in, p->e[i].id) < 0) return PJ_ERR_NOMEM; 681 const CxObject *o = cx_get(s, p->e[i].id); 682 add_subject(scope, &nscope, 64, o->subject); 683 if (o->branch != 0 && o->branch != need->branch) a->other_branch_included++; 684 } 685 for (uint32_t i = 0; i < p->nf; i++) add_subject(scope, &nscope, 64, p->f[i].subject); 686 const uint64_t w0 = p->nw ? p->w[0].t0 : 0, now = need->required_temporal_scope.t_now; 687 uint64_t per_scope[64] = { 0 }, other = 0; 688 689 for (uint64_t id = 1; id <= s->n; id++) { 690 const CxObject *o = &s->obj[id - 1]; 691 if (set_has(&in, id)) { 692 a->included++; 693 a->by_why[PJ_WHY_INCLUDED]++; 694 continue; 695 } 696 a->excluded++; 697 if (cx_verify(s, id) == CX_OK) a->excluded_intact++; 698 uint32_t why; 699 if (!in_list(scope, nscope, o->subject)) { 700 why = PJ_WHY_SUBJECT; 701 other++; 702 } else { 703 for (uint32_t k = 0; k < nscope; k++) 704 if (scope[k] == o->subject) per_scope[k]++; 705 why = PJ_WHY_TYPE; 706 for (uint32_t k = 0; k < need->n_fact_types; k++) { 707 const PjFactReq *f = &need->required_fact_types[k]; 708 if (f->cls != o->cls || (f->kind && f->kind != o->kind)) continue; 709 uint32_t br = f->treatment == PJ_BRANCH_LOCAL ? need->branch : 0; 710 if (o->branch != br) { 711 if (why == PJ_WHY_TYPE) why = PJ_WHY_BRANCH; 712 continue; 713 } 714 uint32_t tp = f->temporal; 715 if (tp == PJ_T_MEMORY) tp = need->memory_scope == PJ_M_EPISODIC ? PJ_T_ALL : PJ_T_WINDOW; 716 uint64_t t0 = tp == PJ_T_ALL ? 0 : w0; 717 if (f->treatment == PJ_DERIVED && o->t >= t0 && o->t <= now) { 718 why = PJ_WHY_DERIVED_SOURCE; 719 break; 720 } 721 if ((tp == PJ_T_LATEST || tp == PJ_T_CARRY_IN) && o->t <= now) { 722 why = PJ_WHY_SUPERSEDED; 723 continue; 724 } 725 if (why == PJ_WHY_TYPE || why == PJ_WHY_BRANCH) why = PJ_WHY_TIME; 726 } 727 } 728 a->by_why[why]++; 729 } 730 /* The projection's own counts must agree with the object-by-object audit. */ 731 int match = 1; 732 for (uint32_t i = 0; i < p->nx; i++) { 733 const PjExclusion *x = &p->x[i]; 734 if (x->reason == PJ_X_OTHER_SUBJECTS) { 735 if (x->count != other) match = 0; 736 } else { 737 uint64_t got = 0; 738 for (uint32_t k = 0; k < nscope; k++) 739 if (scope[k] == x->subject) got = per_scope[k]; 740 if (x->count != got) match = 0; 741 } 742 } 743 a->counts_match = match; 744 set_free(&in); 745 return PJ_OK; 746 }