interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / third_party / omega / rx_cortex.h

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