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_compose.h

363 lines · 20518 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_compose.h -- COMPOSITION-2: one World, one causal path.
19 *
20 * goal (outside input, with the routed Skill alternatives)
21 * -> compose.candidate.k: fork a staged J-Space branch from the committed
22 * state, run the routed Skill, derive the branch with its result
23 * -> compose.verify (AEGIS faculty): contract check of every candidate,
24 * picks one winner or none
25 * -> compose.commit: names the winner in state.field[0]
26 * -> World commit binder: the new branch must be live, staged, local and
27 * a child of the branch the World names now; then it is sealed (the
28 * point of no return) and the World publishes. Any refusal before the
29 * seal rejects the commit and releases the branch.
30 * -> settle (driver, at quiescence): reclaim every staged loser, make the
31 * sealed winner durable (js_space_commit), record the composition in
32 * Cortex (candidates, verification evidence, one promotion, one
33 * admission per loser), release the superseded branch.
34 *
35 * Routing discovers (rx_skillroute, no mint). Authority is minted here, at
36 * open, by the system owner through the AIENOS admin: each reaction holds
37 * only the rights its step needs, so a candidate cannot write the state and
38 * only the AEGIS verifier can write the verdict.
39 *
40 * Recovery (OLD-or-NEW). On open the state is the newest Cortex record of
41 * the state object (ENTITY_CREATED or EXEC_COMMIT) whose branch is live and
42 * sealed in the reopened durable J-Space checkpoint. A newer record whose
43 * branch did not become durable is answered with a CX_K_ADMISSION rollback
44 * record (tag RXC_ADMIT_ROLLBACK). Staged branches are never persisted, so a
45 * loser can never be recovered.
46 */
47#ifndef RX_COMPOSE_H
48#define RX_COMPOSE_H
49
50/* Lane 32: ONE test-build flag. AIEN_TEST_BUILD (off by default, never set for
51 * the production program) turns on the composition test hooks below; a bare
52 * RXC_TEST_HOOKS without it is refused at build time. */
53#if defined(RXC_TEST_HOOKS) && !defined(AIEN_TEST_BUILD)
54#error "RXC_TEST_HOOKS (composition fault and rogue-candidate hooks) needs -DAIEN_TEST_BUILD=1; it never goes in the production program"
55#endif
56#if defined(AIEN_TEST_BUILD) && !defined(RXC_TEST_HOOKS)
57#define RXC_TEST_HOOKS 1
58#endif
59
60#include "aien_machine_id.h"
61#include "aienos_cap.h"
62#include "rx_cortex.h"
63#include "rx_jspace.h"
64#include "rx_skillroute.h"
65#include "rx_world.h"
66
67#define RXC_K 2u /* alternatives explored per goal */
68#define RXC_UNIT 4096u
69#define RXC_CRASH_EXIT 77 /* exit code of an injected crash */
70#define RXC_NONE 0xFFFFFFFFu /* verdict: no winner */
71
72/* World object slots, created in this order at every open; the Cortex
73 * subject of a World record is slot + 1 (rx_cortex_record.h). Composition
74 * records (candidates, evidence, promotion, admissions) are about the state
75 * object, so they share its subject. */
76enum { RXC_SLOT_GOAL = 0, RXC_SLOT_CAND0, RXC_SLOT_CAND1, RXC_SLOT_VERDICT, RXC_SLOT_STATE };
77#define RXC_CX_SUBJECT(slot) ((uint64_t)(slot) + 1u)
78
79/* Fault points on the path, in causal order. */
80enum {
81 RXC_FP_NONE = 0,
82 RXC_FP_BEFORE_FORK, /* candidate reaction, before js_branch_fork_staged */
83 RXC_FP_CANDIDATE, /* candidate reaction, after fork + derive, before publish */
84 RXC_FP_BEFORE_COMMIT, /* commit reaction, before it proposes the winner */
85 RXC_FP_AFTER_VALIDATION, /* World binder check, after authority and version checks */
86 RXC_FP_SEAL, /* World binder: crash right after the seal / seal refused */
87 RXC_FP_RECLAIM, /* settle: after loser reclaim, before js_space_commit */
88 RXC_FP_CORTEX, /* settle: between composition Cortex records */
89 RXC_FP_END
90};
91
92/* Admission tags (CX_K_ADMISSION.tag). */
93enum { RXC_ADMIT_LOSER = 1, RXC_ADMIT_ROLLBACK = 2, RXC_ADMIT_RECOVERED = 3 };
94
95/* Composition record payloads (subject RXC_CX_SUBJECT(RXC_SLOT_STATE)).
96 * CX_K_CANDIDATE (CLAIM) tag = Cortex id of the goal record;
97 * links {candidate World record, goal record}
98 * CX_K_EVIDENCE_REF (EVIDENCE, VERIFY_EVIDENCE) tag = goal record;
99 * links {verdict World record, claim 0, claim 1,
100 * state World record or 0}
101 * CX_K_PROMOTION cx_promote(winner claim, evidence)
102 * CX_K_ADMISSION tag = RXC_ADMIT_*; LOSER links {loser claim, evidence},
103 * ROLLBACK links {refused state record}, RECOVERED links
104 * {state record, evidence} */
105enum { RXC_CP_K = 0, RXC_CP_REF, RXC_CP_RESULT, RXC_CP_SKILL, RXC_CP_INPUT, RXC_CP_GOALSEQ,
106 RXC_CP_PASS, RXC_CP_DIGEST0, RXC_CP_SKILLDIG0 = RXC_CP_DIGEST0 + 4,
107 RXC_CP_HOME = RXC_CP_SKILLDIG0 + 4, /* RXC_HOME_*: where the Skill ran */
108 RXC_CP_WORDS };
109enum { RXC_EP_WINNER = 0, RXC_EP_WREF, RXC_EP_LREF, RXC_EP_PASSMASK, RXC_EP_RESULT,
110 RXC_EP_GOALSEQ, RXC_EP_VERIFIER, RXC_EP_WDIGEST0, RXC_EP_WORDS = RXC_EP_WDIGEST0 + 4 };
111enum { RXC_AP_REF = 0, RXC_AP_GOAL, RXC_AP_K, RXC_AP_WORDS };
112
113/* Verdict object fields. */
114enum { RXC_V_WINNER = 0, RXC_V_WREF, RXC_V_LREF, RXC_V_PASSMASK, RXC_V_GOAL, RXC_V_RESULT };
115/* Candidate object fields. */
116enum { RXC_C_REF = 0, RXC_C_RESULT, RXC_C_SKILL, RXC_C_GOAL, RXC_C_DONE, RXC_C_HOME };
117/* Where a candidate's Skill ran (candidate RXC_C_HOME, claim RXC_CP_HOME). */
118enum { RXC_HOME_LOCAL = 0, RXC_HOME_FABRIC = 1 };
119/* Goal object fields. */
120enum { RXC_G_INPUT = 0, RXC_G_OP, RXC_G_SKILL0, RXC_G_SKILL1, RXC_G_SEQ };
121/* State object fields. */
122enum { RXC_S_REF = 0, RXC_S_RESULT, RXC_S_GOAL };
123
124typedef enum {
125 RXC_SUBJ_EXTERNAL = 200, RXC_SUBJ_CAND0 = 201, RXC_SUBJ_CAND1 = 202,
126 RXC_SUBJ_AEGIS = 203, RXC_SUBJ_COMMIT = 204
127} RxcSubject;
128
129#define RXC_RES_GOAL 0xC2000001ull
130#define RXC_RES_CAND0 0xC2000002ull
131#define RXC_RES_CAND1 0xC2000003ull
132#define RXC_RES_VERDICT 0xC2000004ull
133#define RXC_RES_STATE 0xC2000005ull
134/* Instances: up to RXC_MAX_ACTIVE compositions share one World; instance i
135 * offsets the role subjects by RXC_SUBJ_STRIDE*i and the resources by
136 * RXC_RES_STRIDE*i (instance 0 = the values above). */
137#define RXC_MAX_ACTIVE 2u
138#define RXC_SUBJ_STRIDE 8u
139#define RXC_RES_STRIDE 0x100ull
140#define RXC_SUBJ_OF(inst, role) ((uint32_t)(role) + (uint32_t)(inst) * RXC_SUBJ_STRIDE)
141#define RXC_RES_OF(inst, res) ((uint64_t)(res) + (uint64_t)(inst) * RXC_RES_STRIDE)
142/* How long rx_compose_run waits for its steps to settle (rx_compose_set_wait_ms;
143 * reset to the default at open/attach). */
144#define RXC_WAIT_MS_DEFAULT 30000u
145#define RXC_WAIT_MS_MAX 600000u
146
147/* The contract the AEGIS verifier enforces on a candidate's result. */
148typedef int (*RxcContract)(uint64_t input, uint64_t result);
149
150/* Fabric: how a candidate runs a route whose provider is on another machine
151 * (route->verdict == SR_E_REMOTE). Called from the candidate reaction, on a
152 * World worker, with the run's clock. It must revalidate the route against
153 * the Capability Graph (sr_route_check == SR_E_REMOTE) and the machine's
154 * Fabric membership at now_us, run exactly the procedure the route names
155 * (skill id + advertised digest) on that machine, and return 0 with its
156 * result, or nonzero to refuse (the candidate then proposes nothing). It
157 * mints and holds no World authority: the result is only a candidate, checked
158 * by the AEGIS verifier like any local one. Two candidates may call it at
159 * once. Without a hook a remote route proposes nothing (as before). */
160typedef int (*RxcRemoteRun)(void *ctx, const SrRouter *router, const SrRoute *route,
161 uint64_t input, uint64_t now_us, uint64_t *result);
162
163typedef struct {
164 int outcome; /* RXC_OUT_* */
165 uint32_t n_alternatives;
166 SrRoute route[RXC_K];
167 uint32_t winner; /* index into route[], UINT32_MAX = none */
168 JsBranchRef old_ref, new_ref, cand_ref[RXC_K];
169 uint64_t result;
170 uint64_t cx_candidate[RXC_K], cx_evidence, cx_promotion, cx_admission[RXC_K];
171 uint64_t goal_crumb; /* EXTERNAL crumb of the goal */
172 uint32_t reclaimed; /* staged branches reclaimed at settle */
173 uint8_t winner_digest[32]; /* J-Space content digest of the committed branch */
174 uint32_t prior_completed; /* records of an earlier failed record completed first */
175} RxcResult;
176
177enum {
178 RXC_OUT_COMMITTED = 1, /* NEW is committed and durable */
179 RXC_OUT_NO_WINNER, /* every candidate failed the contract: OLD kept */
180 RXC_OUT_NOT_COMMITTED, /* the path stopped before the World commit: OLD kept */
181 RXC_OUT_NOT_DURABLE, /* World named NEW, J-Space did not make it durable */
182 RXC_OUT_RECORD_FAILED /* NEW durable; composition record incomplete */
183};
184
185typedef struct RxCompose {
186 char dir[200];
187 AienMachineId self;
188 RxWorld w; /* storage of the World rx_compose_open owns */
189 RxWorld *world; /* the World it runs in: &w (open) or the caller's (attach) */
190 int owns_world; /* 1: open built it and close destroys it */
191 const RxCallerKeyring *keys; /* attach: credentials of the composition subjects */
192 JsSpace js;
193 CxStore cx;
194 const SrRouter *router;
195 RxcContract contract;
196 AienosCapAdmin *admin;
197 uint64_t session;
198 RxObjRef goal, cand[RXC_K], verdict, state;
199 uint32_t rx_cand[RXC_K], rx_verify, rx_commit;
200 uint32_t n_rx; /* of those, registered so far (cand 0, cand 1, verify, commit) */
201 uint64_t seq;
202 /* recovery report of the last open */
203 JsBranchRef recovered;
204 uint64_t recovered_record; /* Cortex id naming it */
205 int anchor_unknown; /* 1: opened from a checkpoint with no Cortex anchor (version 2): cut check is audit only */
206 uint32_t rolled_back; /* newer records refused (rollback admissions) */
207 uint32_t recovered_completed; /* composition records completed at open (0 = none missing) */
208 /* in-process state after RECORD_FAILED / NOT_DURABLE (cleared at open) */
209 int pending; /* 0 none; 1 record to complete first; 2 refuse: reopen */
210 int pend_released; /* the superseded branch was already released */
211 uint64_t pend_S, pend_V;
212 JsBranchRef pend_old;
213 uint8_t pend_cdig[RXC_K][32];
214 /* per-run scratch read by reactions */
215 uint64_t run_input;
216 uint32_t n_routes;
217 SrRoute run_route[RXC_K];
218 uint64_t run_now; /* the run's clock (remote dispatch revalidates at it) */
219 /* Fabric dispatch of remote routes (rx_compose_set_remote; cleared at open/attach). */
220 struct {
221 RxcRemoteRun run;
222 void *ctx;
223 uint64_t ran, refused; /* remote candidate runs: done / refused by the hook */
224 uint64_t seq[RXC_K]; /* goal seq of the last remote run of candidate k */
225 uint8_t digest[RXC_K][32]; /* the advertised digest it ran */
226 } remote;
227 uint32_t wait_ms; /* settle wait of rx_compose_run (rx_compose_set_wait_ms) */
228 /* authority minted at open (one per step, rights of that step only) */
229 RxCapRef cap_ext, cap_cand[RXC_K][3], cap_verify[4], cap_commit[2];
230 struct RxcCandUser { struct RxCompose *c; uint32_t k; } cand_user[RXC_K];
231 int attached; /* its Cortex link is attached to the World */
232 /* what open/attach put into the World (attach close undoes it) */
233 int has_binder;
234 uint32_t n_objs, n_minted;
235 /* instance slot in its World (attach; 0 under open) and what it implies */
236 uint32_t inst;
237 uint32_t subj_cand[RXC_K], subj_aegis, subj_commit;
238 uint64_t res[5];
239 RxObjRef obj[5];
240 /* Test hooks. They fire only in builds compiled with -DAIEN_TEST_BUILD=1
241 * (the composition unit tests and the R13 test-build variant); in every
242 * other build, the production program included, they are ignored and
243 * the _exit path does not exist. */
244 struct RxcTestHooks {
245 int fault_point; /* RXC_FP_* */
246 int fault_crash; /* 1: _exit(RXC_CRASH_EXIT); 0: fail in process */
247 uint32_t fault_k; /* candidate index for candidate-side points */
248 int fault_hit;
249 int rogue_candidate; /* candidate 0 also proposes a state write */
250 /* Hold candidate hold_k1 - 1 (0: none) once, after its Skill ran and
251 * before it proposes: it sets held, waits for release, then sets
252 * hold_done and returns its proposal for the World to publish. */
253 uint32_t hold_k1;
254 int held, release, hold_done;
255 } test;
256} RxCompose;
257
258/* Open (or create) the composition in `dir`: machine identity must match the
259 * stored one (RX_ERR_IDENTITY otherwise), J-Space and the Cortex journal are
260 * reopened (a torn journal tail is repaired, then the chain is verified), the
261 * World is built with `n_workers` (the composition owns it; see
262 * rx_compose_attach to run inside an existing World), state is recovered
263 * OLD-or-NEW. `dir` must pass src/omega_path_check.h (omega_path_copy) and
264 * be under 168 bytes; otherwise RX_ERR_ARG and nothing is created. */
265int rx_compose_open(RxCompose *c, const char *dir, const AienMachineId *self, uint64_t session,
266 const SrRouter *router, RxcContract contract, AienosCapAdmin *admin,
267 AienosCapView *view, uint32_t n_workers);
268/* One goal through the whole path. Returns RX_OK with out->outcome set, or a
269 * negative error (an injected in-process fault shows in outcome instead).
270 * After RXC_OUT_RECORD_FAILED the next run first completes that record (and
271 * releases the superseded branch); if it cannot, it returns RX_ERR_REPLAY.
272 * After RXC_OUT_NOT_DURABLE every run returns RX_ERR_REPLAY until reopened. */
273int rx_compose_run(RxCompose *c, uint64_t input, const SrRequirement *req, const CqHeld *held,
274 uint64_t now_us, RxcResult *out);
275void rx_compose_close(RxCompose *c);
276/* COMPOSITION-2 inside an existing World (the living one).
277 *
278 * enroll_callers: the World's owner enrolls the four composition reaction
279 * subjects of every instance (role + RXC_SUBJ_STRIDE*i for i < RXC_MAX_ACTIVE: 8
280 * subjects) into `w` and gets their
281 * credentials in `keys`; it must run before rx_world_bind_callers (R16: the
282 * enrollment closes one way at bind). RX_ERR_IDENTITY once bound.
283 *
284 * attach: like open, on `dir` and `self`, but the composition runs inside
285 * `w` (which it neither builds nor destroys):
286 * - its five objects get whatever ids `w` has free; c->goal..c->state hold
287 * them. The Cortex journal in `dir` keeps the composition subjects
288 * RXC_CX_SUBJECT(slot) through a scoped Cortex link
289 * (rx_cortex_attach_scoped), so the record is the same as under open and
290 * survives a reopen in a World that hands out other ids; living crumbs
291 * never enter it, and the World's recorder slot stays free for (or
292 * shared with) the World's own Cortex;
293 * - w->external_subject is not changed: the goal capability is minted for
294 * the World's own external subject, on the composition goal resource only;
295 * - reactions are registered with `keys` (rx_world_add_reaction_keyed), so a
296 * World with bound callers admits them; without bound callers keys may be
297 * NULL;
298 * - it takes a free instance slot (below) and installs that instance's
299 * commit binder (one of the World's RX_MAX_BINDERS): RX_ERR_EXISTS if
300 * RXC_MAX_ACTIVE compositions already run in `w`, or one of them uses
301 * `dir` (the journal is single-owner); RX_ERR_FULL if the reaction table
302 * has no room for its four reactions. Either refusal changes nothing.
303 * close (attach mode), in this order: (1) revokes every capability attach
304 * minted, so no composition step can publish from here on (a step already
305 * running finds its rights gone at publish and is REJECTED); (2) removes the
306 * scoped Cortex link, so a refused late write never enters the composition
307 * journal (the World's crumb log still records it); (3) waits until none of
308 * the composition's OWN four reactions is READY, RUNNING, PUBLISHING,
309 * BLOCKED_RESOURCE, re-armed, parked, deferred, or waiting in the World's
310 * fan-out backlog (still DORMANT there): those still use the
311 * RxCompose as their user pointer. There is no wall-clock cutoff and no wait
312 * for the rest of the World: a living World may never be quiet as a whole,
313 * and returning while a step still runs would leave it using freed memory.
314 * A Skill that never returns therefore hangs close (the safe choice);
315 * (4) removes its binder, retires the five objects and reclaims the revoked
316 * capabilities (their AIENOS slots become free; the slot generation
317 * advances, so the old references never validate again); (5) removes its
318 * four reactions from the World (rx_world_remove_reaction): their
319 * subscriptions go, the slots are marked removed and the next attach of the
320 * same instance reuses them. It leaves `w` running.
321 *
322 * Invariant: close reclaims everything attach allocated. After close the
323 * World's footprint (rx_world_footprint: active reactions, subscriptions,
324 * live objects, binders, bound fields, in-flight work, backlog, resource
325 * use) and the AIENOS capability table are what they were before attach; the
326 * reaction table does not grow across attach/close cycles (a removed slot is
327 * reused only by the same subject and faculty, so crumb provenance of old
328 * runs still names the right subject). Only the crumb log grows: it is
329 * history. Tested for 2000 cycles under ASan.
330 *
331 * Invariant: up to RXC_MAX_ACTIVE (2) compositions per World at a time, each
332 * isolated. Instance i (0 or 1) uses subjects RXC_SUBJ_OF(i, role) and
333 * resources RXC_RES_OF(i, res); instance 0 keeps 200..204 and
334 * 0xC2000001..5, so its records are the ones a single composition always
335 * wrote. Capabilities are scoped by subject and resource, so instance B's
336 * steps hold no right on instance A's objects (a write naming them is
337 * refused), each instance has its own binder on its own state object, its
338 * own Cortex scoped link and journal, and its own step set; run waits only
339 * for its own steps. The bound is the caller keyring: 2 instances x 4
340 * subjects = RX_CALLER_KEYRING_MAX. Attaches are serialized process-wide;
341 * runs and closes of different instances may overlap. After close the same
342 * or another RxCompose can attach again and recovers OLD-or-NEW from its
343 * directory. */
344int rx_compose_enroll_callers(RxWorld *w, RxCallerKeyring *keys);
345int rx_compose_attach(RxCompose *c, RxWorld *w, const RxCallerKeyring *keys, const char *dir,
346 const AienMachineId *self, uint64_t session, const SrRouter *router,
347 RxcContract contract, AienosCapAdmin *admin);
348/* Install (run != NULL) or remove the Fabric dispatch of remote routes. Call
349 * after open/attach (both clear it) and never during rx_compose_run. */
350int rx_compose_set_remote(RxCompose *c, RxcRemoteRun run, void *ctx);
351/* Set how long rx_compose_run waits for its steps to settle, 1 to
352 * RXC_WAIT_MS_MAX ms (RX_ERR_ARG otherwise). open/attach reset it to
353 * RXC_WAIT_MS_DEFAULT. Call after open/attach and never during rx_compose_run. */
354int rx_compose_set_wait_ms(RxCompose *c, uint32_t wait_ms);
355/* The branch the World names now. */
356JsBranchRef rx_compose_state(RxCompose *c);
357/* Content digest of the composition record (Cortex objects without timing
358 * words), stable across identical runs. */
359void rx_compose_record_digest(const CxStore *s, uint8_t out[32]);
360/* The deterministic realizer of the state unit. */
361extern const JsRealizer RXC_REALIZER;
362
363#endif /* RX_COMPOSE_H */