interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / runtime / rx_cortex_record.c

394 lines · 14756 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_record.c -- World execution recorded into canonical Cortex.
19 * See rx_cortex_record.h.
20 *
21 * The World has one recorder slot. It holds a hub of Cortex links: at most
22 * one whole-World link (rx_cortex_attach) and scoped links
23 * (rx_cortex_attach_scoped) that record only the crumbs about their own
24 * objects, under their own subjects. Each link is the single writer of its
25 * own store, so no link ever writes into another's journal.
26 */
27#include "rx_cortex_record.h"
28#include "omega_native.h"
29
30#include <stdlib.h>
31#include <string.h>
32
33#define RXCX_MAX_LINKS 8u
34
35typedef struct {
36 CxStore *s;
37 uint64_t session;
38 uint64_t token; /* writer claim */
39 uint64_t *cx_of_crumb; /* [crumb id] -> Cortex id */
40 uint64_t cap; /* entries in cx_of_crumb (crumb_cap + 1) */
41 uint64_t records, errors;
42 int scoped; /* 0: every crumb, subject = slot + 1 */
43 uint32_t n_obj; /* scoped: objects it records */
44 RxObjRef obj[RXCX_SCOPE_MAX];
45 uint64_t subject[RXCX_SCOPE_MAX];
46} CortexLink;
47
48typedef struct {
49 uint32_t n;
50 CortexLink *link[RXCX_MAX_LINKS];
51} CortexHub;
52
53static uint64_t cx_of(const CortexLink *l, uint64_t crumb) {
54 return crumb && crumb < l->cap ? l->cx_of_crumb[crumb] : 0;
55}
56
57static void kind_of(RxCrumbKind k, uint32_t *cls, uint32_t *kind, uint32_t *protect) {
58 uint64_t dst[4] = { 0 }, res = 0;
59 (void)omega2_rx_cx_kind_of(&res, (uint64_t)k, dst, 4);
60 *cls = (uint32_t)dst[0];
61 *kind = (uint32_t)dst[1];
62 *protect = (uint32_t)dst[2];
63}
64
65/* Subject of `ref` in link `l`; 0 = this link does not record it. */
66static uint64_t subject_in(const CortexLink *l, const RxObjRef *ref) {
67 if (!l->scoped) return ref && ref->id < RX_MAX_OBJECTS ? (uint64_t)ref->id + 1u : 0;
68 for (uint32_t i = 0; ref && i < l->n_obj; i++)
69 if (l->obj[i].id == ref->id && l->obj[i].generation == ref->generation)
70 return l->subject[i];
71 return 0;
72}
73
74/* One crumb into one link. Called with the world mutex held. */
75static void record_link(CortexLink *l, const RxWorld *w, const RxCrumb *k) {
76 /* An operator stop or resume (R16 G6) is about no World object and has no
77 * Cortex kind; its evidence is the crumb log and the durable halt mark. */
78 if (k->kind == RX_CRUMB_OPERATOR_STOP || k->kind == RX_CRUMB_OPERATOR_RESUME) return;
79 const RxObjRef *ref = k->n_outputs ? &k->outputs[0].obj
80 : k->n_inputs ? &k->inputs[0].obj : NULL;
81 uint64_t subject = subject_in(l, ref);
82 if (l->scoped && !subject) return; /* not about this link's objects */
83 CxHeader h;
84 memset(&h, 0, sizeof h);
85 kind_of(k->kind, &h.cls, &h.kind, &h.protect);
86 uint64_t version = k->n_outputs ? k->outputs[0].version
87 : k->n_inputs ? k->inputs[0].version : 0;
88 h.subject = subject;
89 h.t = l->s->n + 1;
90 h.generation = ref ? ref->generation : 0;
91 h.tag = (uint64_t)k->kind;
92 uint64_t cause = cx_of(l, k->wake_cause);
93 uint64_t p_ids[RX_MAX_PARENTS];
94 uint32_t np = k->n_parents < RX_MAX_PARENTS ? k->n_parents : RX_MAX_PARENTS;
95 for (uint32_t i = 0; i < np; i++) p_ids[i] = cx_of(l, k->parents[i]);
96 uint64_t link_out[8] = { 0 }, nl64 = 0;
97 (void)omega2_rx_cx_dedup_links(&nl64, cause, p_ids, (uint64_t)np, link_out, 8, (uint64_t)CX_LINKS);
98 uint32_t nl = (uint32_t)nl64;
99 for (uint32_t i = 0; i < nl && i < CX_LINKS; i++) h.links[i] = link_out[i];
100
101 uint64_t p[CX_WREC_WORDS];
102 memset(p, 0, sizeof p);
103 p[CX_WREC_SESSION] = l->session;
104 p[CX_WREC_CRUMB] = k->id;
105 p[CX_WREC_CRUMB_KIND] = (uint64_t)k->kind;
106 p[CX_WREC_REACTION] = k->reaction;
107 p[CX_WREC_FACULTY] = k->faculty;
108 p[CX_WREC_EPISODE] = k->episode;
109 p[CX_WREC_REASON] = (uint64_t)(int64_t)k->reason;
110 p[CX_WREC_OBJ] = ref ? ref->id : UINT64_MAX;
111 p[CX_WREC_OBJ_GEN] = ref ? ref->generation : 0;
112 p[CX_WREC_OBJ_VERSION] = version;
113 if (ref && ref->id < RX_MAX_OBJECTS)
114 for (uint32_t f = 0; f < RX_MAX_FIELDS && f < 8; f++)
115 p[CX_WREC_FIELD0 + f] = w->objects[ref->id].field[f];
116 {
117 uint64_t digw[4] = { 0 }, res = 0;
118 (void)omega2_rx_cx_pack_digest(&res, k->digest, 32, digw, 4);
119 for (int i = 0; i < 4; i++) p[CX_WREC_DIGEST0 + i] = digw[i];
120 }
121 p[CX_WREC_N_INPUTS] = k->n_inputs;
122 p[CX_WREC_N_OUTPUTS] = k->n_outputs;
123 p[CX_WREC_T_START_NS] = k->t_start_ns;
124 p[CX_WREC_T_END_NS] = k->t_end_ns;
125
126 uint64_t id = 0;
127 if (cx_append_as(l->s, l->token, &h, p, CX_WREC_WORDS, &id) != CX_OK) {
128 l->errors++;
129 return;
130 }
131 if (k->id < l->cap) l->cx_of_crumb[k->id] = id;
132 l->records++;
133}
134
135/* The World's recorder: every crumb to every link. World mutex held. */
136static void record(void *ctx, const RxWorld *w, const RxCrumb *k) {
137 CortexHub *hub = ctx;
138 for (uint32_t i = 0; i < hub->n; i++) record_link(hub->link[i], w, k);
139}
140
141static void link_free(CortexLink *l) {
142 cx_release_writer(l->s, l->token);
143 free(l->cx_of_crumb);
144 free(l);
145}
146
147static void release(void *ctx) {
148 CortexHub *hub = ctx;
149 for (uint32_t i = 0; i < hub->n; i++) link_free(hub->link[i]);
150 free(hub);
151}
152
153static CortexHub *hub_of(RxWorld *w) {
154 return w->recorder == record ? w->recorder_ctx : NULL;
155}
156
157/* The link writing `s`, or (s == NULL) the default link: the whole-World
158 * link, else the only link. World mutex held. */
159static CortexLink *link_of(RxWorld *w, const CxStore *s) {
160 CortexHub *hub = hub_of(w);
161 if (!hub) return NULL;
162 for (uint32_t i = 0; i < hub->n; i++)
163 if (s ? hub->link[i]->s == s : !hub->link[i]->scoped) return hub->link[i];
164 return !s && hub->n == 1 ? hub->link[0] : NULL;
165}
166
167static CortexLink *link_new(RxWorld *w, CxStore *s, uint64_t session, int *rc) {
168 CortexLink *l = calloc(1, sizeof(*l));
169 if (!l) { *rc = RX_ERR_FULL; return NULL; }
170 l->s = s;
171 l->session = session;
172 l->token = (uint64_t)(uintptr_t)l;
173 l->cap = w->crumb_cap + 1;
174 l->cx_of_crumb = calloc(l->cap, sizeof(uint64_t));
175 if (!l->cx_of_crumb) { free(l); *rc = RX_ERR_FULL; return NULL; }
176 if (cx_claim_writer(s, l->token) != CX_OK) {
177 free(l->cx_of_crumb);
178 free(l);
179 *rc = RX_ERR_IDENTITY;
180 return NULL;
181 }
182 *rc = RX_OK;
183 return l;
184}
185
186/* Add `l` to the World's hub (creating it if the World has no recorder) and
187 * hand it every crumb already in the log, all under the world mutex. */
188static int link_add(RxWorld *w, CortexLink *l) {
189 pthread_mutex_lock(&w->mu);
190 CortexHub *hub = hub_of(w);
191 int rc = RX_OK;
192 if (!hub && w->recorder) rc = RX_ERR_EXISTS; /* some other recorder */
193 else if (hub && hub->n >= RXCX_MAX_LINKS) rc = RX_ERR_FULL;
194 else if (hub && !l->scoped && link_of(w, NULL) && !link_of(w, NULL)->scoped) rc = RX_ERR_EXISTS;
195 for (uint32_t i = 0; rc == RX_OK && hub && i < hub->n; i++) {
196 const CortexLink *o = hub->link[i];
197 if (!o->scoped || !l->scoped) continue;
198 for (uint32_t a = 0; a < o->n_obj; a++) /* scopes are disjoint */
199 for (uint32_t b = 0; b < l->n_obj; b++)
200 if (o->obj[a].id == l->obj[b].id && o->obj[a].generation == l->obj[b].generation)
201 rc = RX_ERR_EXISTS;
202 }
203 if (rc == RX_OK && !hub) {
204 hub = calloc(1, sizeof(*hub));
205 if (!hub) rc = RX_ERR_FULL;
206 else {
207 w->recorder = record;
208 w->recorder_release = release;
209 w->recorder_ctx = hub;
210 }
211 }
212 if (rc == RX_OK) {
213 for (uint64_t i = 0; i < w->n_crumbs; i++) record_link(l, w, &w->crumbs[i]);
214 hub->link[hub->n++] = l;
215 }
216 pthread_mutex_unlock(&w->mu);
217 return rc;
218}
219
220int rx_cortex_attach(RxWorld *w, CxStore *s, uint64_t session) {
221 if (!w || !s || s->n_subjects < RX_CORTEX_SUBJECTS) return RX_ERR_ARG;
222 pthread_mutex_lock(&w->mu);
223 CortexLink *d = link_of(w, NULL);
224 int has = (w->recorder && !hub_of(w)) || (d && !d->scoped);
225 pthread_mutex_unlock(&w->mu);
226 if (has) return RX_ERR_EXISTS; /* link_add re-checks under the lock */
227 int rc;
228 CortexLink *l = link_new(w, s, session, &rc);
229 if (!l) return rc;
230 if ((rc = link_add(w, l)) != RX_OK) link_free(l);
231 return rc;
232}
233
234int rx_cortex_attach_scoped(RxWorld *w, CxStore *s, uint64_t session, const RxObjRef *objs,
235 const uint64_t *subjects, uint32_t n) {
236 if (!w || !s || !objs || !subjects || n == 0 || n > RXCX_SCOPE_MAX) return RX_ERR_ARG;
237 for (uint32_t i = 0; i < n; i++)
238 if (subjects[i] == 0 || subjects[i] >= s->n_subjects) return RX_ERR_ARG;
239 int rc;
240 CortexLink *l = link_new(w, s, session, &rc);
241 if (!l) return rc;
242 l->scoped = 1;
243 l->n_obj = n;
244 memcpy(l->obj, objs, n * sizeof *objs);
245 memcpy(l->subject, subjects, n * sizeof *subjects);
246 if ((rc = link_add(w, l)) != RX_OK) link_free(l);
247 return rc;
248}
249
250/* Remove link `l` (found under the lock); the hub goes when it is empty. */
251static int link_remove(RxWorld *w, const CxStore *s) {
252 pthread_mutex_lock(&w->mu);
253 CortexHub *hub = hub_of(w);
254 CortexLink *l = link_of(w, s);
255 int empty = 0;
256 if (l) {
257 uint32_t j = 0;
258 for (uint32_t i = 0; i < hub->n; i++)
259 if (hub->link[i] != l) hub->link[j++] = hub->link[i];
260 hub->n = j;
261 if (j == 0) {
262 w->recorder = NULL;
263 w->recorder_release = NULL;
264 w->recorder_ctx = NULL;
265 empty = 1;
266 }
267 }
268 pthread_mutex_unlock(&w->mu);
269 if (!l) return RX_ERR_NOT_FOUND;
270 link_free(l);
271 if (empty) free(hub);
272 return RX_OK;
273}
274
275int rx_cortex_detach(RxWorld *w) {
276 if (!w) return RX_ERR_ARG;
277 return link_remove(w, NULL);
278}
279
280int rx_cortex_detach_store(RxWorld *w, const CxStore *s) {
281 if (!w || !s) return RX_ERR_ARG;
282 return link_remove(w, s);
283}
284
285int rx_cortex_status(RxWorld *w, uint64_t *records, uint64_t *errors) {
286 if (!w) return RX_ERR_ARG;
287 pthread_mutex_lock(&w->mu);
288 const CortexLink *l = link_of(w, NULL);
289 if (l) {
290 if (records) *records = l->records;
291 if (errors) *errors = l->errors;
292 }
293 pthread_mutex_unlock(&w->mu);
294 return l ? RX_OK : RX_ERR_NOT_FOUND;
295}
296
297uint64_t rx_cortex_record_in(RxWorld *w, const CxStore *s, uint64_t crumb) {
298 if (!w) return 0;
299 pthread_mutex_lock(&w->mu);
300 const CortexLink *l = link_of(w, s);
301 uint64_t id = l ? cx_of(l, crumb) : 0;
302 pthread_mutex_unlock(&w->mu);
303 return id;
304}
305
306uint64_t rx_cortex_record_of(RxWorld *w, uint64_t crumb) {
307 return rx_cortex_record_in(w, NULL, crumb);
308}
309
310uint64_t rx_cortex_recall_result(CxStore *s, uint32_t obj_slot, CxWorldRecord *out) {
311 if (!s || obj_slot >= RX_MAX_OBJECTS) return 0;
312 CxFilter f = { CX_EXECUTION, CX_K_EXEC_COMMIT, 0, 1 };
313 uint64_t id = cx_latest(s, (uint64_t)obj_slot + 1u, UINT64_MAX - 1, &f);
314 if (!id || !out) return id;
315 CxRecord r;
316 if (cx_recall_id(s, id, &r) != CX_OK || cx_world_decode(&r, out) != CX_OK) return 0;
317 return id;
318}
319
320/* COMPOSITION-2: records the composition layer adds about World work
321 * (candidates, verification evidence, admissions, promotions) go through the
322 * writer of their store, under the world mutex so they interleave with crumb
323 * records in one order. */
324int rx_cortex_append_in(RxWorld *w, CxStore *s, const CxHeader *h, const uint64_t *payload,
325 uint32_t n, uint64_t *out_id) {
326 if (!w || !h) return RX_ERR_ARG;
327 pthread_mutex_lock(&w->mu);
328 CortexLink *l = link_of(w, s);
329 int rc = !l ? RX_ERR_NOT_FOUND
330 : cx_append_as(l->s, l->token, h, payload, n, out_id) == CX_OK ? RX_OK : RX_ERR_FULL;
331 if (l && rc != RX_OK) l->errors++;
332 pthread_mutex_unlock(&w->mu);
333 return rc;
334}
335
336int rx_cortex_append(RxWorld *w, const CxHeader *h, const uint64_t *payload, uint32_t n,
337 uint64_t *out_id) {
338 return rx_cortex_append_in(w, NULL, h, payload, n, out_id);
339}
340
341int rx_cortex_promote_in(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence,
342 uint64_t *out_id) {
343 if (!w) return RX_ERR_ARG;
344 pthread_mutex_lock(&w->mu);
345 CortexLink *l = link_of(w, s);
346 int rc = !l ? RX_ERR_NOT_FOUND
347 : cx_promote(l->s, l->token, candidate, evidence, l->s->n + 1, out_id) == CX_OK
348 ? RX_OK : RX_ERR_ARG;
349 pthread_mutex_unlock(&w->mu);
350 return rc;
351}
352
353int rx_cortex_promote(RxWorld *w, uint64_t candidate, uint64_t evidence, uint64_t *out_id) {
354 return rx_cortex_promote_in(w, NULL, candidate, evidence, out_id);
355}
356
357uint64_t rx_cortex_next_t_in(RxWorld *w, const CxStore *s) {
358 if (!w) return 0;
359 pthread_mutex_lock(&w->mu);
360 CortexLink *l = link_of(w, s);
361 uint64_t t = l ? l->s->n + 1 : 0;
362 pthread_mutex_unlock(&w->mu);
363 return t;
364}
365
366uint64_t rx_cortex_next_t(RxWorld *w) { return rx_cortex_next_t_in(w, NULL); }
367
368/* The store of the default link (or the one named), NULL if none. The pointer
369 * is stable while attached; reading through it needs the writer's quiet or
370 * rx_cortex_promote_checked_in. */
371CxStore *rx_cortex_store_in(RxWorld *w, const CxStore *s) {
372 if (!w) return NULL;
373 pthread_mutex_lock(&w->mu);
374 CortexLink *l = link_of(w, s);
375 CxStore *out = l ? l->s : NULL;
376 pthread_mutex_unlock(&w->mu);
377 return out;
378}
379
380/* The gate reads the store and cx_promote appends to it under one hold of the
381 * world mutex, so no crumb record lands between check and promotion. */
382int rx_cortex_promote_checked_in(RxWorld *w, CxStore *s, uint64_t candidate, uint64_t evidence,
383 int (*gate)(const CxStore *, uint64_t, const void *),
384 const void *arg, uint64_t *out_id) {
385 if (!w || !gate) return RX_ERR_ARG;
386 pthread_mutex_lock(&w->mu);
387 CortexLink *l = link_of(w, s);
388 int rc = !l ? RX_ERR_NOT_FOUND : gate(l->s, evidence, arg);
389 if (l && rc == RX_OK)
390 rc = cx_promote(l->s, l->token, candidate, evidence, l->s->n + 1, out_id) == CX_OK
391 ? RX_OK : RX_ERR_ARG;
392 pthread_mutex_unlock(&w->mu);
393 return rc;
394}