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

372 lines · 16091 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_semantic.h -- Omega semantic variables and incremental recomputation.
19 *
20 * Objective: do not recompute cognition or derived state when nothing it
21 * depends on changed.
22 *
23 * A semantic value names one piece of meaning at field granularity: one field
24 * of one world object (a source), or the result of one pure derivation over
25 * named inputs (a derived value). A derived value is reused when
26 *
27 * same derivation + same semantic inputs + same relevant generations
28 *
29 * and recomputed otherwise. "Same semantic inputs" is content, not position:
30 * two inputs are the same when their semantic id, their generation and the
31 * digest of their value bytes are all equal. Nothing else participates.
32 *
33 * Three layers, from cheapest to most general:
34 *
35 * 1. Branch memo. Each branch keeps the last value of every derived node it
36 * has asked for. A node is CLEAN until one of its declared inputs
37 * changes in that branch; then it and everything downstream of it is
38 * marked DIRTY (field-granular, transitive). A CLEAN node is served with
39 * no work at all. An unrelated field or object never marks it.
40 * 2. Early cutoff. A DIRTY node first brings its inputs up to date and
41 * recomputes its content key. If the key equals the one its memo was
42 * built from, the memo is still exact and nothing runs.
43 * 3. Content-addressed cache. The key (derivation id, and for each input
44 * its semantic id, generation and value digest) addresses one immutable
45 * entry shared by every branch, agent, task and J-Space candidate in the
46 * engine. A hit is exact reuse of a result computed elsewhere from the
47 * same inputs. A miss runs the function once and files the result.
48 *
49 * Generations. A source value carries its world object's generation;
50 * retiring an object and creating another in its slot changes it, so no
51 * result derived from the old object is served for the new one even when
52 * the bytes match. A derivation carries its implementation generation (the
53 * R9 generation that put the function in force); promoting a new
54 * implementation gives a new derivation id, so results of the old one are
55 * not served for it.
56 *
57 * Branches. A branch is a forked view of the semantic world: its own source
58 * values and its own memo. A J-Space candidate is a branch with a candidate
59 * id. Writes in one branch never change another branch's values, memo or
60 * dirty state. The cache is shared, and is safe to share because its keys
61 * are the complete content of what a result was derived from.
62 *
63 * Provenance. Every cache entry records who produced it (branch, agent,
64 * task, candidate), from exactly which inputs (with each source input's
65 * world causal crumb), and a digest over all of that. Every reuse appends a
66 * hash-chained REUSE event naming the entry it served and who consumed it.
67 * rx_sem_verify recomputes every digest and link.
68 *
69 * Functions must be pure over their declared inputs. The engine cannot see
70 * a function read something it did not declare; the audit mode (recompute
71 * on every hit and compare) exists to catch that, and the qualification
72 * runs with it on.
73 *
74 * This is the simple human reference: one engine mutex, bounded fixed-size
75 * tables, a fork copies the branch's tables. It is not optimized.
76 */
77#ifndef RX_SEMANTIC_H
78#define RX_SEMANTIC_H
79
80#include "rx_world.h"
81
82#include <pthread.h>
83#include <stdbool.h>
84#include <stdint.h>
85
86#define RX_SEM_MAX_SOURCES 256u
87#define RX_SEM_MAX_NODES 256u
88#define RX_SEM_MAX_FNS 32u
89#define RX_SEM_MAX_INPUTS 8u
90#define RX_SEM_MAX_BRANCHES 64u
91#define RX_SEM_MAX_EVIDENCE 4u
92#define RX_SEM_VALUE_WORDS 8u
93#define RX_SEM_CACHE_SLOTS 4096u /* power of two */
94#define RX_SEM_CONFIDENCE_ONE 1000000u /* confidence is parts per million */
95
96#define RX_SEM_OK 0
97#define RX_SEM_ERR_ARG -60
98#define RX_SEM_ERR_FULL -61
99#define RX_SEM_ERR_NOT_FOUND -62
100#define RX_SEM_ERR_STALE -63 /* a source's world object was retired */
101#define RX_SEM_ERR_FN -64 /* the derivation function failed */
102#define RX_SEM_ERR_CYCLE -65
103#define RX_SEM_ERR_EXISTS -66
104#define RX_SEM_ERR_VERIFY -67
105
106/* Semantic ids. The top byte is the kind; the rest identifies. */
107#define RX_SEM_KIND_SOURCE 0x01u
108#define RX_SEM_KIND_DERIVED 0x02u
109#define RX_SEM_KIND(id) ((uint32_t)((id) >> 56))
110
111/* Source id of one field of one world object slot. Stable across that
112 * slot's generations: the generation travels with the value instead. */
113#define RX_SEM_SOURCE_ID(obj_id, field) \
114 (((uint64_t)RX_SEM_KIND_SOURCE << 56) | ((uint64_t)(obj_id) << 8) | (uint64_t)(field))
115
116typedef struct { uint8_t b[32]; } RxSemDigest;
117
118/* SemanticValue. */
119typedef struct {
120 uint64_t semantic_id;
121 uint32_t type;
122 uint64_t value_version; /* per branch; moves only when the content changes */
123 uint32_t n_deps;
124 uint64_t dependencies[RX_SEM_MAX_INPUTS]; /* semantic ids it was derived from */
125 uint32_t generation; /* source: world object generation; derived: fn generation */
126 RxSemDigest derivation_id; /* derived: function identity; source: zero */
127 uint32_t confidence; /* ppm */
128 uint32_t n_evidence;
129 RxSemDigest evidence_refs[RX_SEM_MAX_EVIDENCE];
130 uint32_t n_words;
131 uint64_t value[RX_SEM_VALUE_WORDS];
132 RxSemDigest value_digest; /* content address of (type, value words) */
133} RxSemValue;
134
135/* One input as a derivation saw it. The first three fields are the key. */
136typedef struct {
137 uint64_t semantic_id;
138 uint32_t generation;
139 RxSemDigest value_digest;
140 uint64_t value_version; /* provenance only: versions are branch-local */
141 RxSemDigest evidence; /* source: the world crumb that wrote it */
142} RxSemInputVersion;
143
144/* DerivedSemanticValue: one immutable cache entry. */
145typedef struct {
146 bool used;
147 uint32_t function_id;
148 RxSemDigest derivation_id;
149 RxSemDigest key;
150 uint32_t n_inputs;
151 RxSemInputVersion input_versions[RX_SEM_MAX_INPUTS];
152 RxSemValue result;
153 /* Provenance of the one computation that produced it. */
154 uint32_t producer_branch;
155 uint32_t producer_agent;
156 uint64_t producer_task;
157 uint64_t producer_candidate;
158 uint64_t compute_ns; /* measured wall time of that computation */
159 uint64_t seq; /* order of production in this engine */
160 RxSemDigest entry_digest; /* over everything above */
161 uint64_t reuses;
162} RxSemDerived;
163
164/* A derivation function: read `in[0..n_in)`, fill out->value / n_words /
165 * confidence. Return 0 on success. It must depend on nothing else. */
166typedef int (*RxSemFn)(const RxSemValue *const *in, uint32_t n_in, RxSemValue *out, void *user);
167
168typedef struct {
169 const char *name;
170 uint32_t out_type;
171 uint32_t generation; /* implementation generation (R9) */
172 RxSemDigest impl_digest; /* identity of the implementation bytes */
173 RxSemFn fn;
174 void *user;
175} RxSemFnDesc;
176
177/* Per-branch, per-node memo state. */
178enum { RX_SEM_ABSENT = 0, RX_SEM_CLEAN, RX_SEM_DIRTY };
179
180/* How a get was answered. */
181typedef enum {
182 RX_SEM_SERVED_MEMO = 1, /* clean memo: no work */
183 RX_SEM_SERVED_CUTOFF, /* dirty, but the content key was unchanged */
184 RX_SEM_SERVED_CACHE, /* key found in the shared cache */
185 RX_SEM_SERVED_COMPUTED /* the function ran */
186} RxSemServed;
187
188typedef struct {
189 uint64_t gets;
190 uint64_t memo_hits;
191 uint64_t cutoffs; /* dirty nodes whose inputs came back unchanged */
192 uint64_t cache_hits;
193 uint64_t cache_hits_cross_branch;
194 uint64_t cache_hits_cross_agent;
195 uint64_t cache_hits_cross_task;
196 uint64_t cache_hits_cross_candidate;
197 uint64_t computes;
198 uint64_t compute_ns; /* time spent in functions that ran */
199 uint64_t saved_ns; /* producers' measured time for every avoided run */
200 uint64_t invalidations; /* memo entries marked DIRTY */
201 uint64_t source_writes;
202 uint64_t source_writes_unchanged; /* same content: nothing invalidated */
203 uint64_t audits; /* hits recomputed in audit mode */
204 uint64_t false_hits; /* audit recompute differed from the served value */
205 uint64_t cache_full; /* results not filed because the cache was full */
206} RxSemStats;
207
208typedef struct {
209 uint64_t id; /* 1-based */
210 uint32_t kind; /* 1 = PRODUCE, 2 = REUSE */
211 uint32_t entry; /* cache slot */
212 RxSemDigest entry_digest;
213 uint32_t branch;
214 uint32_t agent;
215 uint64_t task;
216 uint64_t candidate;
217 /* The consumer's own input evidence (its world crumbs). A reuse keeps
218 * the producer's provenance in the entry and adds the consumer's here. */
219 RxSemDigest inputs_evidence;
220 RxSemDigest prev;
221 RxSemDigest digest; /* H(prev, this event) */
222} RxSemEvent;
223
224enum { RX_SEM_EVENT_PRODUCE = 1, RX_SEM_EVENT_REUSE = 2 };
225
226typedef struct {
227 bool used;
228 uint64_t semantic_id;
229 uint32_t function_id;
230 uint32_t n_inputs;
231 uint64_t inputs[RX_SEM_MAX_INPUTS];
232 /* Reverse index: nodes that read this node. */
233 uint32_t n_readers;
234 uint32_t readers[RX_SEM_MAX_NODES];
235} RxSemNode;
236
237typedef struct {
238 bool used;
239 uint64_t semantic_id;
240 uint32_t type;
241 bool world_bound;
242 RxObjRef obj; /* bound world slot and generation */
243 uint32_t field;
244 uint32_t n_readers;
245 uint32_t readers[RX_SEM_MAX_NODES];
246 /* Object-granular mode only: every field of this object, for the baseline. */
247} RxSemSource;
248
249typedef struct {
250 bool live;
251 uint32_t id;
252 uint32_t parent; /* UINT32_MAX for a root */
253 uint32_t agent;
254 uint64_t task;
255 uint64_t candidate; /* J-Space candidate id; 0 = none */
256 uint64_t version_clock;
257 bool have_source[RX_SEM_MAX_SOURCES];
258 bool source_stale[RX_SEM_MAX_SOURCES];
259 RxSemValue source[RX_SEM_MAX_SOURCES];
260 uint64_t world_version[RX_SEM_MAX_SOURCES]; /* field version last pulled */
261 uint8_t state[RX_SEM_MAX_NODES];
262 RxSemValue memo[RX_SEM_MAX_NODES];
263 RxSemDigest memo_key[RX_SEM_MAX_NODES];
264 uint32_t memo_entry[RX_SEM_MAX_NODES]; /* cache slot it came from; UINT32_MAX if none */
265 uint64_t invalidations;
266} RxSemBranch;
267
268/* Invalidation granularity. FIELD is the engine. OBJECT is the measured
269 * baseline: a write to any field of an object dirties every reader of any
270 * field of that object. */
271enum { RX_SEM_GRAIN_FIELD = 0, RX_SEM_GRAIN_OBJECT = 1 };
272
273typedef struct RxSemEngine {
274 pthread_mutex_t mu;
275 uint32_t grain;
276 bool cache_enabled; /* false: memo + cutoff only (baseline) */
277 bool audit; /* recompute every hit and compare */
278
279 RxSemFnDesc fns[RX_SEM_MAX_FNS];
280 RxSemDigest fn_derivation[RX_SEM_MAX_FNS];
281 uint32_t n_fns;
282
283 RxSemSource sources[RX_SEM_MAX_SOURCES];
284 uint32_t n_sources;
285 RxSemNode nodes[RX_SEM_MAX_NODES];
286 uint32_t n_nodes;
287
288 RxSemBranch *branches; /* RX_SEM_MAX_BRANCHES */
289 RxSemDerived *cache; /* RX_SEM_CACHE_SLOTS */
290 uint64_t cache_seq;
291 uint32_t cache_used;
292
293 RxSemEvent *events;
294 uint64_t n_events;
295 uint64_t event_cap;
296 uint64_t event_overflow;
297
298 RxSemStats stats;
299} RxSemEngine;
300
301int rx_sem_init(RxSemEngine *e, uint64_t event_cap);
302void rx_sem_destroy(RxSemEngine *e);
303void rx_sem_set_mode(RxSemEngine *e, uint32_t grain, bool cache_enabled, bool audit);
304
305/* Digest helpers. */
306void rx_sem_digest_value(uint32_t type, const uint64_t *words, uint32_t n, RxSemDigest *out);
307void rx_sem_digest_impl(const char *text, RxSemDigest *out);
308bool rx_sem_digest_eq(const RxSemDigest *a, const RxSemDigest *b);
309
310/* Register a derivation function. Its derivation id is
311 * H(name, out_type, generation, impl_digest). */
312int rx_sem_add_fn(RxSemEngine *e, const RxSemFnDesc *d, uint32_t *out_fn);
313
314/* Declare a source: one field of a world object slot, or (obj.id, field)
315 * with world_bound false for a value that only branches set. */
316int rx_sem_add_source(RxSemEngine *e, RxObjRef obj, uint32_t field, uint32_t type,
317 bool world_bound, uint64_t *out_sid);
318/* Declare a derived node: `fn` over the listed semantic ids (sources or
319 * earlier nodes). Its semantic id is structural (function and inputs), so
320 * the same question has the same id in every branch. */
321int rx_sem_add_node(RxSemEngine *e, uint32_t fn, const uint64_t *inputs, uint32_t n,
322 uint64_t *out_sid);
323
324/* Branches. A root has no parent. A fork copies the parent's sources and
325 * memo as they are now; afterwards they are independent. */
326int rx_sem_branch_root(RxSemEngine *e, uint32_t agent, uint64_t task, uint32_t *out);
327int rx_sem_branch_fork(RxSemEngine *e, uint32_t parent, uint32_t agent, uint64_t task,
328 uint64_t candidate, uint32_t *out);
329int rx_sem_branch_drop(RxSemEngine *e, uint32_t branch);
330/* The branch's context (who is asking). Does not change any value. */
331int rx_sem_branch_context(RxSemEngine *e, uint32_t branch, uint32_t agent, uint64_t task,
332 uint64_t candidate);
333
334/* Write a source value in one branch. Same content (and generation): no
335 * version change, nothing invalidated. Otherwise the value version moves and
336 * the readers are marked DIRTY, transitively. `evidence` may be null. */
337int rx_sem_set_source(RxSemEngine *e, uint32_t branch, uint64_t sid, uint32_t generation,
338 const uint64_t *words, uint32_t n, uint32_t confidence,
339 const RxSemDigest *evidence);
340
341/* Pull every world-bound source from the world into one branch. A field whose
342 * world version did not move is not touched. A source whose object was
343 * retired becomes stale: nodes reading it fail with RX_SEM_ERR_STALE until
344 * rx_sem_rebind names the new object. Returns the number of sources that
345 * changed, or a negative error. */
346int rx_sem_sync_world(RxSemEngine *e, uint32_t branch, RxWorld *w);
347int rx_sem_rebind(RxSemEngine *e, uint64_t sid, RxObjRef obj);
348
349/* Demand a derived value in a branch. `served` (may be null) says how. */
350int rx_sem_get(RxSemEngine *e, uint32_t branch, uint64_t sid, RxSemValue *out,
351 RxSemServed *served);
352/* Read a source value as the branch holds it. */
353int rx_sem_read_source(RxSemEngine *e, uint32_t branch, uint64_t sid, RxSemValue *out);
354
355/* Memo state of a node in a branch (RX_SEM_ABSENT/CLEAN/DIRTY). */
356int rx_sem_state(RxSemEngine *e, uint32_t branch, uint64_t sid);
357
358/* The cache entry a clean derived value in this branch was taken from. */
359int rx_sem_explain(RxSemEngine *e, uint32_t branch, uint64_t sid, RxSemDerived *out);
360
361/* Recompute every entry digest, every event link, and check that each clean
362 * memo in each live branch is exactly its entry's result and that the
363 * entry's inputs are what the branch holds now. 0 on success. */
364int rx_sem_verify(RxSemEngine *e, uint64_t *entries_checked, uint64_t *events_checked,
365 uint64_t *memos_checked);
366
367/* Declared structure, for dependency checks: the node's inputs and the
368 * transitive set of nodes a write to `sid` would dirty (field grain). */
369int rx_sem_node_inputs(RxSemEngine *e, uint64_t sid, uint64_t *inputs, uint32_t *n);
370int rx_sem_downstream(RxSemEngine *e, uint64_t sid, uint64_t *out, uint32_t cap, uint32_t *n);
371
372#endif