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

312 lines · 14354 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_generation.h -- one active generation, and a candidate that is not
19 * active until a promotion is committed.
20 *
21 * The organism keeps running while a candidate is prepared. The barrier is
22 * strict only for that candidate, and only once promotion has started.
23 * After any stop, recovery selects the previous generation or the next one
24 * as a whole. It never mixes the two.
25 *
26 * The candidate may describe its successor. It cannot authorize the
27 * transition. Authorization is the promotion right on the native authority.
28 */
29#ifndef RX_GENERATION_H
30#define RX_GENERATION_H
31
32#include <stddef.h>
33#include <stdint.h>
34
35#include "rx_caller.h"
36
37#define RX_GEN_OK 0
38#define RX_GEN_ERR_ARG -40
39#define RX_GEN_ERR_STALE -41
40#define RX_GEN_ERR_AUTHORITY -42
41#define RX_GEN_ERR_CLOSING -43
42#define RX_GEN_ERR_TORN -44
43#define RX_GEN_ERR_MISSING -45
44#define RX_GEN_ERR_REPLAY -46
45#define RX_GEN_ERR_BUSY -47
46#define RX_GEN_ERR_VERIFY -48
47#define RX_GEN_ERR_IO -49
48#define RX_GEN_ERR_IDENTITY -50 /* caller credential absent, forged, stale or revoked */
49#define RX_GEN_ERR_HALTED -51 /* an operator emergency stop is in force (world or durable mark) */
50
51/* Durable mark of an operator emergency stop (R16 G6), written by the world into
52 * its halt directory (rx_world_set_halt_dir), the store directory. While it exists
53 * the store promotes nothing, bound or not. */
54#define RX_GEN_HALT_MARK "OPERATOR_HALT"
55
56#define RX_GEN_MAX_OBJECTS 32u
57#define RX_GEN_MAX_WORK 16u
58#define RX_GEN_MAX_EXTERNAL 8u
59#define RX_GEN_MAX_EXCLUDED 8u
60
61/* Resource and right a promotion must present. The right is also defined
62 * on the authority root; this is the same number. */
63#define RX_GEN_RES_PROMOTION 0x905ull
64#define RX_GEN_RIGHT_PROMOTE 0x200u
65/* Work completion uses an ordinary WRITE capability on a resource derived
66 * from the store, parent generation, candidate, work id and finish operation. */
67#define RX_GEN_RIGHT_FINISH 0x2u
68
69enum {
70 RX_WORK_EPHEMERAL = 1,
71 RX_WORK_EVIDENCE = 2,
72 RX_WORK_EXTERNAL = 3
73};
74
75enum {
76 RX_WORK_PENDING = 0,
77 RX_WORK_ISSUED = 1,
78 RX_WORK_DONE = 2,
79 RX_WORK_CANCELLED = 3
80};
81
82/* Where a crash is injected. The process stops at that point. */
83enum {
84 RX_CRASH_NONE = 0,
85 RX_CRASH_BEFORE_CANDIDATE_WRITE = 1,
86 RX_CRASH_DURING_CANDIDATE_WRITE = 2,
87 RX_CRASH_AFTER_CANDIDATE_WRITE = 3,
88 RX_CRASH_BEFORE_ROOT_FLIP = 4,
89 RX_CRASH_DURING_ROOT_FLIP = 5,
90 RX_CRASH_AFTER_ROOT_FLIP = 6,
91 RX_CRASH_BEFORE_RECEIPT = 7,
92 RX_CRASH_AFTER_RECEIPT = 8
93};
94
95typedef struct RxGenStore RxGenStore;
96
97typedef struct {
98 uint32_t id;
99 uint32_t generation;
100 uint8_t digest[32];
101} RxGenObject;
102
103typedef struct {
104 uint64_t authority_epoch;
105 uint64_t authority_generation;
106 int proofs_ok;
107 const RxGenObject *objects;
108 uint32_t n_objects;
109 const uint8_t *evidence;
110 size_t evidence_len;
111 const uint8_t *model;
112 size_t model_len;
113 const uint8_t *realization;
114 size_t realization_len;
115 const uint8_t *config;
116 size_t config_len;
117 const uint8_t *provenance;
118 size_t provenance_len;
119} RxGenDraft;
120
121typedef struct {
122 uint64_t id;
123 uint32_t class;
124 uint32_t state;
125 int required;
126 /* On a bound store, only the candidate proposer may add work. `worker`
127 * names its owner/delegate when worker_set is nonzero (otherwise the
128 * proposer owns it), including when the explicit worker is subject zero.
129 * finish_cap_* names that worker's capability for rx_gen_work_resource.
130 * The remaining fields are runtime-owned completion provenance; input
131 * values are ignored. Unbound stores retain legacy accounting behavior. */
132 uint32_t worker;
133 int worker_set;
134 uint32_t finish_cap_id;
135 uint64_t finish_cap_generation;
136 uint32_t completer;
137 uint64_t finish_resource;
138 uint8_t evidence_digest[32];
139} RxGenWork;
140
141typedef struct {
142 uint64_t candidate_id;
143 uint32_t subject;
144 uint32_t cap_id;
145 uint64_t cap_generation;
146 uint64_t resource;
147 uint32_t rights;
148 /* R16 C5: the credential the runtime issued for `subject`. Required by a
149 * store bound with rx_gen_bind_authority; ignored by an unbound one. */
150 RxCallerCred caller;
151} RxPromotionRequest;
152
153typedef struct {
154 int coherent;
155 uint64_t active_id;
156 uint64_t lineage;
157 uint64_t parent_id;
158 int receipt_present;
159 int event_present;
160 uint32_t n_external;
161 uint64_t external_ids[RX_GEN_MAX_EXTERNAL];
162 uint32_t n_excluded;
163 uint64_t excluded_ids[RX_GEN_MAX_EXCLUDED];
164} RxRecoveryRecord;
165
166typedef int (*RxGenAuthFn)(void *ctx, uint32_t cap_id, uint64_t cap_generation,
167 uint32_t subject, uint64_t resource, uint32_t rights);
168typedef int (*RxGenDrainFn)(uint64_t work_id, void *ctx);
169typedef void (*RxGenLiveFn)(void *ctx);
170typedef void (*RxGenDiskHook)(const char *generation_dir, void *ctx);
171
172/* Monotonic timestamps for the most recent promotion attempt. A zero phase
173 * means the attempt stopped before reaching it. Read after promotion returns. */
174typedef struct {
175 uint64_t candidate_id;
176 uint64_t enter_ns;
177 uint64_t barrier_ns;
178 uint64_t flip_ns;
179 uint64_t receipt_ns;
180 int result;
181 /* Finer stamps inside the barrier (R15 trace, spec §6.7). */
182 uint64_t verified_ns; /* work classified, observations and proofs checked */
183 uint64_t blobs_ns; /* generation and work-provenance blobs written and synced */
184 uint64_t candidate_ns; /* root and CANDIDATE journal written and synced */
185 uint64_t reachable_ns; /* root re-read from disk and checked */
186 uint64_t flipped_ns; /* FLIPPED journal written and synced */
187 uint64_t receipt_file_ns; /* receipt file written and synced */
188 uint64_t event_ns; /* event log appended and synced */
189} RxGenPhases;
190
191/* rx_gen_open, rx_gen_recover and rx_gen_reject_replay answer RX_GEN_ERR_ARG,
192 * before touching any file, for a dir that src/omega_path_check.h refuses
193 * (a ".." component, a byte outside its set, or too long for RxGenStore). */
194int rx_gen_open(const char *dir, RxGenStore **out);
195void rx_gen_close(RxGenStore *store);
196/* R15: process-wide bytes written and fsync calls made by every store.
197 * Cumulative; a cross-check only, never a benchmark's own figure. */
198void rx_gen_io_counters(uint64_t *bytes, uint64_t *syncs);
199/* R15: bytes passed to write() and fsync calls issued by this store since
200 * rx_gen_open (spec §6.7, L1-G). Directory creation, rename and unlink are
201 * metadata operations and are not counted as bytes. */
202int rx_gen_store_io(const RxGenStore *store, uint64_t *bytes, uint64_t *syncs);
203int rx_gen_last_phases(const RxGenStore *store, RxGenPhases *out);
204int rx_gen_active(const RxGenStore *store, uint64_t *id, uint64_t *lineage);
205
206/* One blob of committed generation `id` ("evidence", "model", "realization",
207 * "config", "provenance", "objects" or "work"), checked against that
208 * generation's root: length and SHA-256. The work blob records each item's
209 * owner, scoped capability, completer and evidence digest. A missing root is
210 * RX_GEN_ERR_MISSING; a blob that does not match is RX_GEN_ERR_TORN. */
211int rx_gen_read_blob(const RxGenStore *store, uint64_t id, const char *name, uint8_t *buf,
212 size_t cap, size_t *out_len);
213
214int rx_gen_propose(RxGenStore *store, uint32_t proposer, const RxGenDraft *draft,
215 uint64_t *out_id);
216/* R16 C5: the proposer presents its credential. On a bound store plain
217 * rx_gen_propose (no credential) is refused with RX_GEN_ERR_IDENTITY. */
218int rx_gen_propose_as(RxGenStore *store, uint32_t proposer, const RxCallerCred *cred,
219 const RxGenDraft *draft, uint64_t *out_id);
220int rx_gen_mutate_object(RxGenStore *store, uint64_t candidate, uint32_t index,
221 uint32_t generation, const uint8_t digest[32]);
222int rx_gen_set_evidence(RxGenStore *store, uint64_t candidate, const uint8_t *bytes,
223 size_t n);
224int rx_gen_observe_object(RxGenStore *store, uint64_t candidate, uint32_t index,
225 uint32_t generation);
226int rx_gen_add_work(RxGenStore *store, uint64_t candidate, const RxGenWork *work);
227int rx_gen_finish_work(RxGenStore *store, uint64_t candidate, uint64_t work_id);
228/* Return the exact authority resource for finishing `work_id`. It is scoped
229 * to this store, the candidate's parent generation, candidate and operation.
230 * Call this after proposal and mint/delegate RX_GEN_RIGHT_FINISH to the work's
231 * worker before rx_gen_add_work_as. */
232int rx_gen_work_resource(const RxGenStore *store, uint64_t candidate, uint64_t work_id,
233 uint64_t *out_resource);
234/* R16 C7: the work-accounting calls present the caller credential. On a bound
235 * store the bare rx_gen_add_work and rx_gen_finish_work are refused with
236 * RX_GEN_ERR_IDENTITY. Only the candidate proposer may add pending/issued
237 * work and name its worker; the worker's scoped, live capability is checked
238 * both when added and when finished. Only rx_gen_finish_work_as marks it done.
239 * A drain callback on a bound store must use the delegated worker credential. */
240int rx_gen_add_work_as(RxGenStore *store, uint32_t subject, const RxCallerCred *cred,
241 uint64_t candidate, const RxGenWork *work);
242int rx_gen_finish_work_as(RxGenStore *store, uint32_t subject, const RxCallerCred *cred,
243 uint64_t candidate, uint64_t work_id);
244
245void rx_gen_set_crash(RxGenStore *store, int step);
246void rx_gen_set_disk_hook(RxGenStore *store, RxGenDiskHook fn, void *ctx);
247int rx_gen_hold_barrier(RxGenStore *store);
248int rx_gen_release_barrier(RxGenStore *store);
249
250int rx_gen_promote(RxGenStore *store, const RxPromotionRequest *request,
251 RxGenAuthFn auth, void *auth_ctx, RxGenDrainFn drain, void *drain_ctx,
252 RxGenLiveFn live, void *live_ctx);
253
254/* R16 C5: bind the store to the runtime's caller check and to the native
255 * promotion authority, once (RX_GEN_ERR_BUSY if already bound). From then
256 * on: rx_gen_propose_as and rx_gen_promote check the proposer's / the
257 * request subject's credential through `caller` before anything else
258 * (RX_GEN_ERR_IDENTITY). Work assignment/completion validates its scoped
259 * finish capability and rx_gen_promote validates the promotion right with the
260 * bound `auth` / `auth_ctx` only: an authority callback passed by the caller
261 * is not consulted. There is no unbind. */
262/* `op` is RX_CALLER_OP_CHECK, or RX_CALLER_OP_HOLD then RX_CALLER_OP_RELEASE
263 * (rx_caller.h): C7, rx_gen_promote checks the request subject again with
264 * HOLD right before the active pointer moves, and refuses the flip
265 * (RX_GEN_ERR_IDENTITY, active unchanged) when the promoter was revoked in
266 * the meantime; no revocation lands until the flip is in memory. On a bound
267 * store rx_gen_mutate_object and rx_gen_set_evidence, which carry no
268 * credential, return RX_GEN_ERR_IDENTITY. */
269typedef int (*RxGenCallerFn)(void *ctx, uint32_t subject, const RxCallerCred *cred, int op);
270int rx_gen_bind_authority(RxGenStore *store, RxGenCallerFn caller, void *caller_ctx,
271 RxGenAuthFn auth, void *auth_ctx);
272
273/* Durable executor (R15 G7). One thread per store that performs a store's
274 * physical work (the fsyncs of a proposal and of a promotion) so that the
275 * reaction deciding it does not hold a semantic worker while the disk works.
276 * It decides nothing: it runs rx_gen_propose or rx_gen_promote exactly as a
277 * caller would, with the caller's request and authority callback, one job at
278 * a time in the order posted, and then calls `done` (outside every lock).
279 * There is one job slot per kind; a slot holds its result until
280 * rx_gen_job_take. The world never waits on it except to learn it finished.
281 * Without rx_gen_exec_start a store is used synchronously, as before. */
282enum { RX_GEN_JOB_PROPOSE = 0, RX_GEN_JOB_PROMOTE = 1, RX_GEN_JOB_KINDS = 2 };
283enum { RX_GEN_JOB_IDLE = 0, RX_GEN_JOB_PENDING = 1, RX_GEN_JOB_DONE = 2 };
284typedef void (*RxGenDoneFn)(void *ctx);
285typedef struct {
286 uint64_t key; /* the caller's key for this job */
287 int rc; /* what rx_gen_propose / rx_gen_promote returned */
288 uint64_t id; /* proposal: the candidate id */
289 uint64_t active, lineage; /* the store's active generation after the job */
290 uint64_t ns; /* wall time the store call took */
291} RxGenJobResult;
292
293int rx_gen_exec_start(RxGenStore *store);
294int rx_gen_exec_running(const RxGenStore *store);
295/* RX_GEN_OK: posted. RX_GEN_ERR_BUSY: that slot is pending or holds an
296 * untaken result. The draft is copied; the caller's buffers may go away. */
297int rx_gen_post_propose(RxGenStore *store, uint64_t key, uint32_t proposer,
298 const RxGenDraft *draft, RxGenDoneFn done, void *done_ctx);
299int rx_gen_post_propose_as(RxGenStore *store, uint64_t key, uint32_t proposer,
300 const RxCallerCred *cred, const RxGenDraft *draft,
301 RxGenDoneFn done, void *done_ctx);
302int rx_gen_post_promote(RxGenStore *store, uint64_t key, const RxPromotionRequest *request,
303 RxGenAuthFn auth, void *auth_ctx, RxGenDoneFn done, void *done_ctx);
304/* State of one slot; on RX_GEN_JOB_DONE `out` (if given) holds the result. */
305int rx_gen_job_state(RxGenStore *store, int kind, RxGenJobResult *out);
306/* Empty a DONE slot. */
307void rx_gen_job_take(RxGenStore *store, int kind);
308
309int rx_gen_recover(const char *dir, RxRecoveryRecord *out);
310int rx_gen_reject_replay(const char *dir, uint64_t effect_id);
311
312#endif