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_cortex.h # 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_cortex.h` 392 lines · 19239 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_cortex.h -- Cortex, AIEN's durable typed memory. CANONICAL (M20). 19 * 20 * AUTHORITY (M20, 2026-09-30). This file is the one canonical Cortex 21 * contract. Every authoritative Cortex write goes through cx_append or 22 * cx_append_as on a CxStore defined here, and a store has at most one writer: 23 * cx_claim_writer inside a process, an exclusive lock on the journal across 24 * processes. Chosen because it is the C implementation in the runtime tree 25 * that owns World (rx_world.c), the one the runtime already calls 26 * (rx_projection.c), and the only one the no-Rust rule allows to grow. 27 * 28 * Non-authoritative implementations (not edited here, never a second writer): 29 * 30 * aien-sovereign-core crates/cortex-rs Linux LLM-stack memory service 31 * (HTTP :18080; callers aien-cli 32 * src/cortex.rs, spark-mail-rs 33 * src/cortex_sync.rs). 34 * aienos crates/aienos-cortex Rust epistemic store (callers 35 * aienos-aegis, aienos-kernel 36 * src/continuity.rs). 37 * 38 * Their records are not AIEN memory until they enter this store, and they 39 * enter only as CX_K_IMPORTED claims whose tag names the source (CX_SRC_*): 40 * never as facts and never by writing a journal directly. Removal path, per 41 * aien-architecture docs/plans/RUST_TO_C_MIGRATION.md §6 ("port or retire"): 42 * both are retired, not ported. (1) No new caller may be added to either. 43 * (2) Whatever is worth keeping is imported as CX_K_IMPORTED records through 44 * cx_append_as. (3) The crates are deleted. Until step 3 nothing may treat 45 * them as a source of truth. 46 * 47 * Cortex holds what the machine has seen, done, claimed and proven. It is 48 * append-only: an object, once recorded, never changes, and its digest says 49 * so. Nothing in Cortex is text. Every object is a fixed header plus a 50 * payload of 64-bit words. 51 * 52 * Two axes, never merged: 53 * 54 * content class what the object IS: one of the nine memory classes of 55 * doctrine AIEN.md §8.2 (entity, claim, observation, ...). 56 * treatment how one cognitive operation may use it (pinned, live, 57 * compressible, ...). Treatment is not stored here. It is 58 * decided per operation by the state projection 59 * (rx_projection.h). 60 * 61 * Protection bits are the one treatment fact Cortex does own: authority, 62 * commit receipts, verification evidence, generation identity and effect 63 * receipts may never be summarized, whatever an operation asks for. 64 * 65 * Cortex does not hand out its history. It answers typed queries: one 66 * subject, one time range, one class and kind. The indexes are per subject 67 * and time ordered, so a query costs the objects in its range, not the size 68 * of the store. `examined` counts every object a query touched. 69 * 70 * Tiers: one in-memory tier, optionally backed by an append-only journal 71 * file (cx_open) that is replayed and verified on reopen. Doctrine's L1/L2/L3 72 * tiers (accelerator memory, host memory, NVMe) are not implemented here. 73 * Cortex is memory and evidence; runtime state-space material is J-Space 74 * (rx_jspace.h), not this. 75 */ 76 #ifndef RX_CORTEX_H 77 #define RX_CORTEX_H 78 79 #include 80 #include 81 82 /* Content axis: the nine memory classes of doctrine AIEN.md §8.2. */ 83 typedef enum { 84 CX_ENTITY = 1, CX_CLAIM, CX_OBSERVATION, CX_EXECUTION, CX_FAILURE, 85 CX_EVIDENCE, CX_PLAN, CX_REALIZATION, CX_RELATIONSHIP, 86 CX_CLASS_END 87 } CxClass; 88 89 /* State that may never be summarized, derived away or dropped for budget. */ 90 enum { 91 CX_PROT_AUTHORITY = 1u << 0, 92 CX_PROT_COMMIT_RECEIPT = 1u << 1, 93 CX_PROT_VERIFY_EVIDENCE = 1u << 2, 94 CX_PROT_GENERATION_ID = 1u << 3, 95 CX_PROT_EFFECT_RECEIPT = 1u << 4, 96 CX_PROT_ALL = 0x1fu 97 }; 98 99 /* ---- canonical record kinds --------------------------------------------- 100 * `kind` numbering below CX_K_BASE belongs to the writer (rx_projection's 101 * workloads use it). Kinds from CX_K_BASE up are the canonical contract and 102 * mean the same thing to every reader. Each canonical kind lives in exactly 103 * one class, given in the comment. 104 */ 105 #define CX_K_BASE 0x10000u 106 enum { 107 /* World execution (written only by rx_cortex_record.c, payload CX_WREC_*) */ 108 CX_K_WORK_ACCEPTED = CX_K_BASE + 1, /* OBSERVATION: outside input admitted into a World */ 109 CX_K_ENTITY_CREATED, /* ENTITY: a World object came to life */ 110 CX_K_ENTITY_RETIRED, /* ENTITY: a World object was retired */ 111 CX_K_EXEC_COMMIT, /* EXECUTION: work ran and committed a result */ 112 CX_K_EXEC_NOOP, /* EXECUTION: work ran and proposed no change */ 113 CX_K_EXEC_FAILED, /* FAILURE: failed, rejected, blocked, invalidated, 114 contained; tag = World crumb kind */ 115 /* Epistemic records (any claimed writer) */ 116 CX_K_CLAIM = CX_K_BASE + 0x10, /* CLAIM: asserted fact */ 117 CX_K_CANDIDATE, /* CLAIM: summary or memory candidate, not yet fact */ 118 CX_K_EVIDENCE_REF, /* EVIDENCE: reference to a receipt / digest */ 119 CX_K_PROMOTION, /* EVIDENCE: candidate promoted; links[0] = candidate, 120 links[1] = evidence (cx_promote) */ 121 CX_K_ADMISSION, /* EVIDENCE: admission decision; links name its inputs */ 122 CX_K_IMPORTED = CX_K_BASE + 0x20 /* CLAIM: record from a non-authoritative store; 123 tag = CX_SRC_* */ 124 }; 125 126 /* Sources of CX_K_IMPORTED records (see AUTHORITY above). */ 127 enum { CX_SRC_CORTEX_RS = 1, CX_SRC_AIENOS_CORTEX = 2 }; 128 129 #define CX_LINKS 4u 130 131 typedef struct { 132 uint64_t id; /* 1-based and dense: object id == index + 1 */ 133 uint32_t cls; /* CxClass */ 134 uint32_t kind; /* fact type within the class; see CX_K_BASE */ 135 uint64_t subject; /* the entity the object is about */ 136 uint64_t t; /* logical time of the fact */ 137 uint64_t generation; /* generation the fact was recorded under */ 138 uint32_t branch; /* 0 = the main line; else a hypothetical branch */ 139 uint32_t protect; /* CX_PROT_* */ 140 uint64_t tag; /* one small header value (cause code, condition key, ...) */ 141 uint64_t links[CX_LINKS];/* ids of related objects, 0 = none (provenance) */ 142 uint32_t n; /* payload words */ 143 uint64_t off; /* payload offset in the arena */ 144 uint8_t digest[32]; /* SHA-256 of the canonical encoding */ 145 } CxObject; 146 147 typedef struct { 148 uint64_t *ids; 149 uint32_t n, cap; 150 } CxIdList; 151 152 typedef struct CxStore { 153 CxObject *obj; 154 uint64_t n, cap; 155 uint64_t *arena; 156 uint64_t arena_n, arena_cap; 157 CxIdList *by_subject; /* per subject, in append (= time) order */ 158 uint64_t n_subjects; 159 uint8_t chain[32]; /* running hash over every digest in append order */ 160 uint64_t examined; /* objects touched by queries, cumulative */ 161 /* Journal backing (cx_open_with). jops NULL: memory only. */ 162 const struct CxJournalOps *jops; 163 void *jctx; /* backend context, handed back to every op */ 164 void *jh; /* the open journal, owned by the backend */ 165 uint32_t open_flags; 166 /* The one writer, 0 = unclaimed. While claimed, only cx_append_as with 167 * the same token appends. */ 168 uint64_t writer; 169 } CxStore; 170 171 /* What a writer supplies; id, offset and digest are Cortex's. */ 172 typedef struct { 173 uint32_t cls, kind; 174 uint64_t subject, t, generation; 175 uint32_t branch, protect; 176 uint64_t tag; 177 uint64_t links[CX_LINKS]; 178 } CxHeader; 179 180 enum { 181 CX_OK = 0, CX_ERR_ARG = -1, CX_ERR_NOMEM = -2, CX_ERR_ORDER = -3, CX_ERR_DIGEST = -4, 182 CX_ERR_WRITER = -5, /* another writer holds this store or its journal */ 183 CX_ERR_IO = -6, /* journal read/write failed; memory state unchanged */ 184 CX_ERR_FORMAT = -7, /* not a Cortex journal, or another subject count */ 185 CX_ERR_TORN = -8 /* journal ends inside a record (see CX_OPEN_REPAIR_TAIL) */ 186 }; 187 188 int cx_init(CxStore *s, uint64_t n_subjects); 189 void cx_free(CxStore *s); /* also closes and unlocks a journal */ 190 191 /* Append one object. Per subject, time may not go backwards (CX_ERR_ORDER). 192 * CX_ERR_WRITER while a writer has claimed the store. */ 193 int cx_append(CxStore *s, const CxHeader *h, const uint64_t *payload, uint32_t n, uint64_t *out_id); 194 195 /* ---- single writer ------------------------------------------------------ */ 196 197 /* Claim the store for `token` (nonzero). CX_ERR_WRITER if another token 198 * holds it; claiming again with the same token is CX_OK. */ 199 int cx_claim_writer(CxStore *s, uint64_t token); 200 void cx_release_writer(CxStore *s, uint64_t token); 201 /* cx_append for the claimed writer (token 0 = unclaimed store). */ 202 int cx_append_as(CxStore *s, uint64_t token, const CxHeader *h, const uint64_t *payload, 203 uint32_t n, uint64_t *out_id); 204 205 /* ---- journal ------------------------------------------------------------ 206 * File: 32-byte header (magic, version, n_subjects, reserved) then one 207 * record per object: 13 header words, n payload words, 4 digest words, all 208 * little-endian. Reopen replays every record through the append path and 209 * refuses the journal if any recomputed digest differs from the stored one. 210 * Subject numbers are whatever the writer used; for World records they are 211 * local object slots (rx_cortex_record.h), runtime indices, not machine or 212 * object identity. The reserved header word is kept for that identity. 213 */ 214 enum { 215 CX_OPEN_READONLY = 1u << 0, /* no lock, no writes; for inspection */ 216 CX_OPEN_SYNC = 1u << 1, /* fdatasync after every append */ 217 CX_OPEN_REPAIR_TAIL = 1u << 2 /* truncate one incomplete trailing record */ 218 }; 219 220 /* ---- journal backend ------------------------------------------------------ 221 * The store reaches its journal only through this table. cx_journal_posix is 222 * the file backend (one file, flock for the writer lock); it is the default 223 * of cx_open and is compiled out under CX_NO_POSIX, where a freestanding host 224 * (AIENOS: sealed Store objects, P06 adapter) supplies its own table through 225 * cx_open_with. Memory comes from CX_MALLOC / CX_REALLOC / CX_FREE, which a 226 * config header named by CX_CONFIG_H may define; the default is libc. 227 * The record format, the digests, the chain and every check in cx_open are 228 * the same under every backend: a backend stores bytes, it never interprets 229 * them. */ 230 typedef struct CxJournalOps { 231 /* Open the journal `name`. ro: no lock, no writes, must exist. Writable: 232 * create when absent and take the one-writer lock, CX_ERR_WRITER when 233 * another writer holds it. *h receives the backend's handle. */ 234 int (*open)(void *ctx, const char *name, int ro, void **h); 235 /* Current length in bytes. */ 236 int (*size)(void *ctx, void *h, size_t *len); 237 /* Exactly n bytes at offset off, or an error. */ 238 int (*read)(void *ctx, void *h, size_t off, uint8_t *out, size_t n); 239 /* Append n bytes as ONE unit: on return either all of them are in the 240 * journal (durable when sync != 0) or none of them is, and the length is 241 * as before. A partial write is the backend's to undo. */ 242 int (*append)(void *ctx, void *h, const uint8_t *b, size_t n, int sync); 243 /* Cut the journal back to len bytes, durably (CX_OPEN_REPAIR_TAIL). A 244 * backend whose appends are atomic never holds a torn tail and may 245 * answer CX_ERR_IO. */ 246 int (*truncate)(void *ctx, void *h, size_t len); 247 /* Release the handle; a writer's lock drops here. */ 248 void (*close)(void *ctx, void *h); 249 } CxJournalOps; 250 251 #ifndef CX_NO_POSIX 252 extern const CxJournalOps cx_journal_posix; 253 #endif 254 255 /* cx_open through an explicit backend; the contract below is unchanged. */ 256 int cx_open_with(CxStore *s, const CxJournalOps *ops, void *ctx, const char *name, 257 uint64_t n_subjects, uint32_t flags); 258 259 /* Open (creating if absent and writable) a journal-backed store. A writable 260 * open takes an exclusive lock; a second writable open of the same file, 261 * from this or any process, gets CX_ERR_WRITER. n_subjects must match a 262 * nonempty journal's. On error the store is left freed. 263 * 264 * cx_open is the single seam where integrity is established. At open, for 265 * every record: the magic word, the id equal to its position (1, 2, 3, ...), 266 * and the per-record digest recomputed from header plus payload and compared 267 * to the stored one (rx_cortex.c replay). The chain is rebuilt from those 268 * digests, not read from disk. Also checked: the subject count against the 269 * file header, an incomplete trailing record (CX_ERR_TORN, or truncated when 270 * CX_OPEN_REPAIR_TAIL is set), and the writer lock. 271 * After a CX_OK open, callers may assume the objects in memory equal the 272 * journal, and that cx_verify on an object read from this store cannot fail 273 * except through memory corruption or a later in-memory edit. 274 * NOT checked by cx_open: a journal cut exactly at a record boundary looks like a 275 * shorter valid journal (the file holds no record count or head marker). That 276 * cut is detected by rx_compose, which records the Cortex record count and head 277 * record digest in the J-Space checkpoint anchor and cross-checks it at open. 278 * KNOWN LIMIT: if both the Cortex journal and the J-Space checkpoint roll back 279 * together, nothing local detects it (an external anchor problem). */ 280 #ifndef CX_NO_POSIX 281 int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags); 282 #endif 283 void cx_close(CxStore *s); /* = cx_free */ 284 285 const CxObject *cx_get(const CxStore *s, uint64_t id); 286 const uint64_t *cx_payload(const CxStore *s, const CxObject *o); 287 288 /* Recompute an object's digest and compare: CX_OK or CX_ERR_DIGEST. */ 289 int cx_verify(const CxStore *s, uint64_t id); 290 291 /* AUDIT entry point, not needed after cx_open (which already verified every 292 * record). Re-hashes every in-memory object, compares each to its stored 293 * digest, replays the running hash and compares it to `chain`: CX_OK if all 294 * match. For audits and tests. */ 295 int cx_verify_chain(const CxStore *s); 296 297 /* Canonical digest of a header plus payload. */ 298 void cx_digest(const CxObject *o, const uint64_t *payload, uint8_t out[32]); 299 300 /* Record that a candidate (CX_CLAIM / CX_K_CANDIDATE) was promoted on the 301 * strength of an evidence object (class CX_EVIDENCE). Appends a protected 302 * CX_K_PROMOTION about the candidate's subject with links {candidate, 303 * evidence}. CX_ERR_ARG if either id is missing or of the wrong kind. */ 304 int cx_promote(CxStore *s, uint64_t token, uint64_t candidate, uint64_t evidence, 305 uint64_t t, uint64_t *out_id); 306 307 /* ---- typed queries (every touched object is counted in s->examined) ---- */ 308 309 /* Matches cls/kind (0 = any), and branch unless branch_any. */ 310 typedef struct { 311 uint32_t cls, kind; 312 uint32_t branch; 313 int branch_any; 314 } CxFilter; 315 316 typedef int (*CxVisit)(void *user, const CxObject *o); 317 318 /* Objects of `subject` with t in [t0, t1], oldest first. The visitor returns 319 * nonzero to stop. Returns the number visited. */ 320 uint64_t cx_range(CxStore *s, uint64_t subject, uint64_t t0, uint64_t t1, 321 const CxFilter *f, CxVisit visit, void *user); 322 323 /* The newest matching object of `subject` with t <= t_max, or 0. */ 324 uint64_t cx_latest(CxStore *s, uint64_t subject, uint64_t t_max, const CxFilter *f); 325 326 /* Count of `subject`'s objects with t < t0 (index arithmetic, no scan). */ 327 uint64_t cx_count_before(const CxStore *s, uint64_t subject, uint64_t t0); 328 uint64_t cx_count_subject(const CxStore *s, uint64_t subject); 329 330 /* ---- recall: typed, provenanced records ---------------------------------- */ 331 332 typedef struct { 333 CxObject hdr; /* copy of the header, links = provenance */ 334 const uint64_t *payload; /* valid while the store is unchanged */ 335 int verified; /* digest recomputed and matched */ 336 } CxRecord; 337 338 /* Like cx_range, but fills up to `max` records (oldest first), each digest 339 * checked. Returns the number filled. */ 340 uint32_t cx_recall(CxStore *s, uint64_t subject, uint64_t t0, uint64_t t1, 341 const CxFilter *f, CxRecord *out, uint32_t max); 342 343 /* One record by id (CX_OK, CX_ERR_ARG, or CX_ERR_DIGEST with *out still set). */ 344 int cx_recall_id(const CxStore *s, uint64_t id, CxRecord *out); 345 346 /* Every object `id` derives from through links, nearest first, no repeats, 347 * at most `max`. Returns the number written. */ 348 uint32_t cx_provenance(const CxStore *s, uint64_t id, uint64_t *out, uint32_t max); 349 350 /* ---- World execution record payload (CX_K_WORK_ACCEPTED .. EXEC_FAILED) -- 351 * Written by rx_cortex_record.c from one World causal crumb. Defined here so 352 * a reader needs no World header. 353 */ 354 enum { 355 CX_WREC_SESSION = 0, /* World session the writer was attached under */ 356 CX_WREC_CRUMB, /* crumb id in that session's causal log */ 357 CX_WREC_CRUMB_KIND, /* RxCrumbKind */ 358 CX_WREC_REACTION, /* reaction id, UINT32_MAX for outside / create */ 359 CX_WREC_FACULTY, 360 CX_WREC_EPISODE, /* crumb id of the outside input the episode started from */ 361 CX_WREC_REASON, /* crumb reason (sign-extended) */ 362 CX_WREC_OBJ, /* object slot, UINT64_MAX = none */ 363 CX_WREC_OBJ_GEN, 364 CX_WREC_OBJ_VERSION, 365 CX_WREC_FIELD0, /* 8 field values of the object after the crumb */ 366 CX_WREC_DIGEST0 = CX_WREC_FIELD0 + 8, /* crumb digest, 4 little-endian words */ 367 CX_WREC_N_INPUTS = CX_WREC_DIGEST0 + 4, 368 CX_WREC_N_OUTPUTS, 369 CX_WREC_T_START_NS, 370 CX_WREC_T_END_NS, 371 CX_WREC_WORDS 372 }; 373 374 typedef struct { 375 uint64_t cx_id; 376 uint32_t kind; /* CX_K_* */ 377 uint64_t session, crumb, episode, obj, obj_gen, obj_version; 378 uint32_t crumb_kind, reaction, faculty; 379 int32_t reason; 380 uint64_t field[8]; 381 uint8_t crumb_digest[32]; 382 uint32_t n_inputs, n_outputs; 383 uint64_t cause; /* Cortex id of the record of the crumb that caused it, 0 = none */ 384 } CxWorldRecord; 385 386 /* Decode a World execution record: CX_OK, or CX_ERR_ARG if `r` is not one. */ 387 int cx_world_decode(const CxRecord *r, CxWorldRecord *out); 388 389 /* Test hook: overwrite one payload word without updating the digest. */ 390 void cx_tamper(CxStore *s, uint64_t id, uint32_t word, uint64_t value); 391 392 #endif /* RX_CORTEX_H */