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 / src / runtime / rx_projection.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_projection.h -- Omega cognitive state projection IR (host reference). |
| 19 | * |
| 20 | * Cognition does not get "everything we know". It states what one operation |
| 21 | * needs (CognitiveNeed). Omega turns that into the smallest state that is |
| 22 | * enough for the operation (StateProjection), by typed queries to Cortex. |
| 23 | * Nothing here is text or tokens. A projection is object references, typed |
| 24 | * summaries and derived numbers. |
| 25 | * |
| 26 | * "Compile" here means selecting and shaping state from a declared need. It |
| 27 | * generates no code. |
| 28 | * |
| 29 | * Every projected entry has a treatment (PjStateClass). Treatment is |
| 30 | * semantic. It says what the operation may do with the state, not how it is |
| 31 | * laid out: |
| 32 | * |
| 33 | * PINNED always present, in full; never dropped for budget |
| 34 | * LIVE the current value, in full |
| 35 | * DERIVED replaced by one computed feature; sources recoverable |
| 36 | * RECALLABLE a reference; the body can be recalled on demand |
| 37 | * COMPRESSIBLE lossy summary allowed; source recoverable |
| 38 | * REFERENCE_ONLY identity, header and digest only |
| 39 | * EPHEMERAL valid for this operation only; not part of the |
| 40 | * recoverability contract; never protected state |
| 41 | * BRANCH_LOCAL exists only on the need's hypothetical branch |
| 42 | * PROTECTED_EVIDENCE never summarized, never derived, never budget-dropped |
| 43 | * |
| 44 | * Protected state (rx_cortex.h CX_PROT_*) is authority, commit receipts, |
| 45 | * verification evidence, generation identity and effect receipts. A need |
| 46 | * that asks to summarize or derive it does not compile: the compiler refuses |
| 47 | * the whole projection (PJ_ERR_PROTECTED). Protected state may appear in |
| 48 | * full, or as a reference that keeps its exact digest, never as a summary. |
| 49 | * |
| 50 | * Excluded state is not copied anywhere. The projection records the Cortex |
| 51 | * chain head it was compiled against and counts of what it left out; the |
| 52 | * audit (pj_audit) re-derives, object by object, why each excluded object |
| 53 | * was left out and checks it is still intact in Cortex. |
| 54 | */ |
| 55 | #ifndef RX_PROJECTION_H |
| 56 | #define RX_PROJECTION_H |
| 57 | |
| 58 | #include "rx_cortex.h" |
| 59 | |
| 60 | #include <stdint.h> |
| 61 | |
| 62 | typedef enum { |
| 63 | PJ_PINNED = 1, PJ_LIVE, PJ_DERIVED, PJ_RECALLABLE, PJ_COMPRESSIBLE, |
| 64 | PJ_REFERENCE_ONLY, PJ_EPHEMERAL, PJ_BRANCH_LOCAL, PJ_PROTECTED_EVIDENCE, |
| 65 | PJ_STATE_CLASS_END |
| 66 | } PjStateClass; |
| 67 | |
| 68 | typedef enum { PJ_FORM_FULL = 1, PJ_FORM_SUMMARY, PJ_FORM_REF } PjForm; |
| 69 | |
| 70 | typedef enum { PJ_OP_EXPLAIN = 1, PJ_OP_VERIFY, PJ_OP_PLAN, PJ_OP_ASSESS, PJ_OP_PREDICT } PjOperation; |
| 71 | |
| 72 | /* Which part of a subject's history one fact type draws from. */ |
| 73 | typedef enum { |
| 74 | PJ_T_WINDOW = 1, /* t in the need's window */ |
| 75 | PJ_T_LATEST, /* the newest at or before now */ |
| 76 | PJ_T_CARRY_IN, /* the newest before the window, plus the window */ |
| 77 | PJ_T_ALL, /* the whole history up to now */ |
| 78 | PJ_T_MEMORY /* WINDOW or ALL, from the need's memory scope */ |
| 79 | } PjTemporal; |
| 80 | |
| 81 | /* Where the need's window starts. */ |
| 82 | typedef enum { |
| 83 | PJ_A_FIXED = 1, /* temporal.t0 */ |
| 84 | PJ_A_SINCE_VERIFIED, /* the subject's newest verified realization */ |
| 85 | PJ_A_GENERATION /* the start of the current generation */ |
| 86 | } PjAnchor; |
| 87 | |
| 88 | typedef enum { |
| 89 | PJ_W_SELF = 1, /* the subject */ |
| 90 | PJ_W_DEPENDENCIES, /* the subject and what it depends on (one hop) */ |
| 91 | PJ_W_MACHINE /* subject 0: machine-wide facts (generations) */ |
| 92 | } PjWorld; |
| 93 | |
| 94 | typedef enum { PJ_U_NONE = 1, PJ_U_POINT, PJ_U_INTERVAL } PjUncertainty; |
| 95 | typedef enum { PJ_E_NONE = 1, PJ_E_REFS, PJ_E_FULL } PjEvidence; |
| 96 | typedef enum { PJ_M_WORKING = 1, PJ_M_EPISODIC } PjMemory; |
| 97 | |
| 98 | /* Sections of a projection. */ |
| 99 | typedef enum { |
| 100 | PJ_SEC_WORLD = 1, /* canonical_world_refs: current (LIVE, LATEST) state */ |
| 101 | PJ_SEC_CORTEX, /* cortex_refs */ |
| 102 | PJ_SEC_HYPOTHESIS, /* hypotheses: claims */ |
| 103 | PJ_SEC_EVIDENCE /* evidence_refs: evidence and protected state */ |
| 104 | } PjSection; |
| 105 | |
| 106 | /* Verification evidence carries this tag when the check passed. */ |
| 107 | #define PJ_VERIFIED 1u |
| 108 | |
| 109 | #define PJ_MAX_FACTS 16u |
| 110 | |
| 111 | typedef struct { |
| 112 | uint32_t cls, kind; /* Cortex class and kind */ |
| 113 | uint32_t treatment; /* PjStateClass */ |
| 114 | uint32_t world; /* PjWorld */ |
| 115 | uint32_t temporal; /* PjTemporal */ |
| 116 | } PjFactReq; |
| 117 | |
| 118 | typedef struct { |
| 119 | uint32_t goal; /* the caller's goal code (opaque to Omega) */ |
| 120 | uint32_t operation_class; /* PjOperation */ |
| 121 | uint64_t subject; |
| 122 | uint64_t focus; /* one object the operation is about (0 = none); pinned */ |
| 123 | uint32_t branch; /* 0 = the main line */ |
| 124 | PjFactReq required_fact_types[PJ_MAX_FACTS]; |
| 125 | uint32_t n_fact_types; |
| 126 | struct { |
| 127 | uint32_t anchor; /* PjAnchor */ |
| 128 | uint64_t t0; /* PJ_A_FIXED */ |
| 129 | uint64_t t_now; |
| 130 | uint32_t realization_kind; /* PJ_A_SINCE_VERIFIED: which realizations */ |
| 131 | uint32_t generation_kind; /* PJ_A_GENERATION: machine entity kind */ |
| 132 | } required_temporal_scope; |
| 133 | struct { |
| 134 | uint32_t dependency_kind; /* relationship kind naming a dependency */ |
| 135 | } required_world_scope; |
| 136 | uint32_t uncertainty_requirement; /* PjUncertainty */ |
| 137 | uint32_t evidence_requirement; /* PjEvidence */ |
| 138 | uint32_t memory_scope; /* PjMemory */ |
| 139 | struct { |
| 140 | uint64_t max_bytes, max_objects; /* 0 = unbounded */ |
| 141 | } resource_budget; |
| 142 | } CognitiveNeed; |
| 143 | |
| 144 | typedef struct { |
| 145 | uint64_t id; |
| 146 | uint32_t section, treatment, form; |
| 147 | uint32_t n; /* summary words (PJ_FORM_SUMMARY) */ |
| 148 | uint64_t off; /* in the projection arena */ |
| 149 | uint8_t digest[32]; /* the source object's digest */ |
| 150 | } PjEntry; |
| 151 | |
| 152 | /* A number computed from many Cortex objects (DERIVED). */ |
| 153 | typedef struct { |
| 154 | uint32_t section; /* PJ_FEAT_DERIVED or PJ_FEAT_UNCERTAINTY */ |
| 155 | uint32_t cls, kind, branch; |
| 156 | uint64_t subject, t0, t1; |
| 157 | uint64_t n_src, count, sum, sumsq, min, max; |
| 158 | uint8_t src_digest[32]; /* over the sources' digests, in order */ |
| 159 | } PjFeature; |
| 160 | |
| 161 | enum { PJ_FEAT_DERIVED = 1, PJ_FEAT_UNCERTAINTY }; |
| 162 | |
| 163 | typedef struct { |
| 164 | uint64_t subject, t0, t1; |
| 165 | uint32_t anchor; |
| 166 | uint64_t anchor_id; /* the verified realization or generation object */ |
| 167 | uint32_t treatment; /* always PJ_EPHEMERAL: compile-time scaffolding */ |
| 168 | } PjWindow; |
| 169 | |
| 170 | enum { PJ_X_OTHER_SUBJECTS = 1, PJ_X_IN_SCOPE_SUBJECT }; |
| 171 | |
| 172 | typedef struct { |
| 173 | uint32_t reason; /* PJ_X_* */ |
| 174 | uint64_t subject; /* PJ_X_IN_SCOPE_SUBJECT */ |
| 175 | uint64_t count; |
| 176 | } PjExclusion; |
| 177 | |
| 178 | /* Per-object reasons, from the audit. */ |
| 179 | typedef enum { |
| 180 | PJ_WHY_INCLUDED = 0, PJ_WHY_SUBJECT = 1, PJ_WHY_TYPE, PJ_WHY_TIME, PJ_WHY_BRANCH, |
| 181 | PJ_WHY_SUPERSEDED, PJ_WHY_DERIVED_SOURCE, PJ_WHY_END |
| 182 | } PjWhy; |
| 183 | |
| 184 | typedef struct { |
| 185 | uint64_t id; |
| 186 | uint32_t form; /* PJ_FORM_SUMMARY or PJ_FORM_REF */ |
| 187 | uint8_t digest[32]; |
| 188 | } PjRecover; |
| 189 | |
| 190 | typedef struct { |
| 191 | PjEntry *e; |
| 192 | uint32_t n, cap; |
| 193 | PjFeature f[PJ_MAX_FACTS * 4]; |
| 194 | uint32_t nf; |
| 195 | PjWindow w[8]; |
| 196 | uint32_t nw; |
| 197 | PjExclusion x[72]; |
| 198 | uint32_t nx; |
| 199 | PjRecover *r; /* recoverability manifest */ |
| 200 | uint32_t nr, rcap; |
| 201 | uint64_t *arena; |
| 202 | uint64_t arena_n, arena_cap; |
| 203 | uint64_t store_count; /* Cortex objects at compile time */ |
| 204 | uint8_t store_chain[32]; /* Cortex chain head at compile time */ |
| 205 | /* measurements */ |
| 206 | uint64_t bytes; /* what cognition receives */ |
| 207 | uint64_t objects; /* entries plus features */ |
| 208 | uint64_t examined; /* Cortex objects the queries touched */ |
| 209 | uint64_t compile_ns; |
| 210 | } StateProjection; |
| 211 | |
| 212 | enum { |
| 213 | PJ_OK = 0, PJ_ERR_ARG = -1, PJ_ERR_NOMEM = -2, |
| 214 | PJ_ERR_PROTECTED = -3, /* a need asked to summarize or derive protected state */ |
| 215 | PJ_ERR_BUDGET = -4, /* the pinned and protected state alone exceeds the budget */ |
| 216 | PJ_ERR_NO_ANCHOR = -5, /* no verified realization / generation to anchor the window */ |
| 217 | PJ_ERR_BRANCH = -6, /* BRANCH_LOCAL without a branch */ |
| 218 | PJ_ERR_RECOVER = -7 /* recovery found a source that no longer matches */ |
| 219 | }; |
| 220 | |
| 221 | /* Bytes cognition is charged per entry header, per payload word, per digest. */ |
| 222 | #define PJ_HEADER_BYTES 96u |
| 223 | #define PJ_WORD_BYTES 8u |
| 224 | #define PJ_DIGEST_BYTES 32u |
| 225 | #define PJ_FEATURE_BYTES 96u |
| 226 | /* The sample-series summary: count, sum, sum of squares, min, max. */ |
| 227 | #define PJ_SUMMARY_WORDS 5u |
| 228 | |
| 229 | void pj_need_init(CognitiveNeed *n, uint32_t operation, uint64_t subject, uint64_t t_now); |
| 230 | int pj_need_add(CognitiveNeed *n, uint32_t cls, uint32_t kind, uint32_t treatment, |
| 231 | uint32_t world, uint32_t temporal); |
| 232 | |
| 233 | int pj_compile(CxStore *s, const CognitiveNeed *need, StateProjection *out); |
| 234 | void pj_free(StateProjection *p); |
| 235 | |
| 236 | /* ---- what cognition consumes ---- |
| 237 | * |
| 238 | * One flat view for both a projection and the whole store, so the same |
| 239 | * consumer runs on either. Items are sorted by id. */ |
| 240 | typedef struct { |
| 241 | uint64_t id; |
| 242 | uint32_t cls, kind; |
| 243 | uint64_t subject, t, generation, tag; |
| 244 | uint64_t links[CX_LINKS]; |
| 245 | uint32_t branch, protect; |
| 246 | uint32_t section, treatment, form; |
| 247 | uint32_t n; |
| 248 | const uint64_t *w; /* payload (FULL) or summary words (SUMMARY) */ |
| 249 | } PjItem; |
| 250 | |
| 251 | typedef struct { |
| 252 | PjItem *items; |
| 253 | uint32_t n; |
| 254 | const PjFeature *feat; |
| 255 | uint32_t nf; |
| 256 | int dense; /* items[i].id == i + 1 (the whole store) */ |
| 257 | uint64_t bytes; |
| 258 | uint64_t build_ns; |
| 259 | } PjView; |
| 260 | |
| 261 | int pj_view_projection(const CxStore *s, const StateProjection *p, PjView *v); |
| 262 | int pj_view_full(const CxStore *s, PjView *v); |
| 263 | /* Arbitrary objects in full form (for baselines such as "the newest K"). */ |
| 264 | int pj_view_ids(const CxStore *s, const uint64_t *ids, uint32_t n, PjView *v); |
| 265 | void pj_view_free(PjView *v); |
| 266 | const PjItem *pj_view_find(const PjView *v, uint64_t id); |
| 267 | |
| 268 | /* Bytes of the whole store under the same accounting as a projection. */ |
| 269 | uint64_t pj_full_bytes(const CxStore *s); |
| 270 | |
| 271 | /* Recall one RECALLABLE or REFERENCE_ONLY body; the digest is checked first. */ |
| 272 | const uint64_t *pj_recall(const CxStore *s, const StateProjection *p, uint64_t id, uint32_t *n_out); |
| 273 | |
| 274 | /* The feature from its sources: recompute count, sums, extremes and digest. */ |
| 275 | int pj_feature_compute(CxStore *s, uint64_t subject, uint32_t cls, uint32_t kind, uint32_t branch, |
| 276 | uint64_t t0, uint64_t t1, PjFeature *out); |
| 277 | |
| 278 | /* Summary of one sample series (count, sum, sumsq, min, max). */ |
| 279 | void pj_summarize(const uint64_t *w, uint32_t n, uint64_t out[PJ_SUMMARY_WORDS]); |
| 280 | |
| 281 | /* ---- recoverability ---- |
| 282 | * |
| 283 | * pj_recover_all checks every manifest entry and every derived feature |
| 284 | * against Cortex: sources present, digests unchanged, features recompute to |
| 285 | * the same numbers. Returns PJ_OK or PJ_ERR_RECOVER; counts in *checked. */ |
| 286 | int pj_recover_all(CxStore *s, const StateProjection *p, uint64_t *checked); |
| 287 | |
| 288 | typedef struct { |
| 289 | uint64_t included, excluded; |
| 290 | uint64_t by_why[PJ_WHY_END]; |
| 291 | uint64_t excluded_intact; /* excluded objects whose digest still verifies */ |
| 292 | uint64_t overlap; /* objects both included and excluded (must be 0) */ |
| 293 | uint64_t other_branch_included; /* must be 0 */ |
| 294 | int counts_match; /* excluded_state counts agree with the audit */ |
| 295 | } PjAudit; |
| 296 | |
| 297 | /* Classify every Cortex object as included or excluded, with one reason. |
| 298 | * O(store); it is an audit, not part of the cognition path. */ |
| 299 | int pj_audit(CxStore *s, const CognitiveNeed *need, const StateProjection *p, PjAudit *a); |
| 300 | |
| 301 | const char *pj_state_class_name(uint32_t c); |
| 302 | |
| 303 | #endif /* RX_PROJECTION_H */ |