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

335 lines · 14623 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_fusion.h -- Omega verified workflow fusion (OMEGA_WORKFLOW_FUSION).
19 *
20 * Omega watches the action graphs it has already run and verified. When the
21 * same fragment A -> B -> C -> D keeps coming back with the same semantic
22 * dependencies, the same authority pattern and the same result contract, a
23 * low failure rate and stable evidence, Omega may propose it as a MetaSkill:
24 * one node that does what the fragment did.
25 *
26 * observe verified runs only (lowered result == sequential reference)
27 * propose MetaSkill { ancestry, contracts, realization, reference graph }
28 * verify against the original fragment: semantics, authority, effects,
29 * evidence, failure behaviour
30 * measure reactions and crumbs per run, latency, CPU time, energy,
31 * resource use, success and failure rates
32 * canary the fused graph runs in shadow beside production; one
33 * divergence quarantines the MetaSkill for good
34 * promote through the generation barrier (rx_generation.h), with the
35 * promotion right on the native authority
36 * publish only then does the Skill Net table carry it, and only then
37 * does compile use it
38 *
39 * A MetaSkill never replaces behaviour silently. rx_fusion_compile applies
40 * only published MetaSkills and reports every fusion it made. Everything
41 * before publication (a candidate, a verified or measured MetaSkill, one in
42 * canary) is refused and counted.
43 *
44 * A realization is not assumed to be text or code: it is a typed step
45 * program (compiled), a table learned from verified observations (learned),
46 * both (hybrid), or a hardware realization (a contract slot; none is built
47 * on this host).
48 *
49 * What a fragment may hold (so the fused node means exactly what it did):
50 * CONST, PURE, WORLD_READ, RECALL, PHYSICAL, SKILL, RETRY, EFFECT_PROPOSE,
51 * and VERIFY as the exit. Never an effect boundary: the effect chain is left
52 * as it was. No BRANCH or JOIN, no control dependency touching a step, no
53 * ON_FAIL edge between steps, one exit (the only step whose result leaves
54 * the fragment), every step an ancestor of the exit, and it must fit the
55 * engine's per-reaction dependency budget.
56 *
57 * This file takes no authority admin handle. The build checks that
58 * rx_fusion.o references none.
59 */
60#ifndef RX_FUSION_H
61#define RX_FUSION_H
62
63#include "rx_generation.h"
64#include "rx_graph.h"
65
66#include <stdint.h>
67
68#define AG_FUSION_MAX_PATTERNS 64u
69#define AG_FUSION_MAX_ANCESTRY 16u
70#define AG_FUSION_MAX_SAMPLES 64u
71#define AG_FUSION_MAX_GRAPHS 8u
72#define AG_FUSION_MAX_LIBRARY 8u
73#define AG_FUSION_MAX_APPLIED 8u
74
75/* Errors (beyond AG_E_*). */
76#define AG_FUSION_E_STATE -20 /* the MetaSkill is not in the state this step needs */
77#define AG_FUSION_E_GENERATION -21 /* not the active generation */
78#define AG_FUSION_E_IDENTITY -22 /* bytes or identity differ from what was promoted */
79#define AG_FUSION_E_AUTHORITY -23 /* fused authority differs from the steps' */
80
81/* One verified run a pattern was seen in. */
82typedef struct {
83 uint8_t graph[32]; /* digest of the graph it ran in */
84 uint64_t run;
85 uint32_t exit_origin; /* the exit node's origin in that graph */
86} AgAncestry;
87
88/* One observed input of a fragment and what the reference produced. */
89typedef struct {
90 uint8_t s[AG_MAX_IN];
91 uint64_t v[AG_MAX_IN];
92 uint64_t world[AG_META_MAX_OBJ][RX_MAX_FIELDS];
93 uint8_t status;
94 uint64_t value;
95 uint64_t steps;
96 uint32_t attempts;
97} AgFusionSample;
98
99typedef struct {
100 AgMetaProgram prog; /* canonical fragment; prog.signature is the pattern key */
101 uint32_t occurrences;
102 uint32_t n_graphs;
103 uint8_t graphs[AG_FUSION_MAX_GRAPHS][32];
104 uint32_t ok, failed, skipped;
105 uint32_t evidence_stable; /* a repeated input gave the same result and step evidence */
106 uint32_t evidence_unstable; /* ... or did not */
107 uint32_t n_ancestry;
108 AgAncestry ancestry[AG_FUSION_MAX_ANCESTRY];
109 uint32_t n_samples;
110 AgFusionSample sample[AG_FUSION_MAX_SAMPLES];
111} AgFusionPattern;
112
113typedef struct {
114 uint32_t n;
115 AgFusionPattern p[AG_FUSION_MAX_PATTERNS];
116 uint32_t runs_observed; /* verified runs taken in */
117 uint32_t runs_refused; /* runs not taken: lowered != reference, incomplete, evidence missing */
118 uint32_t fragments_seen;
119 uint32_t fragments_over_budget; /* would not fit one reaction */
120 uint32_t patterns_dropped; /* table full */
121} AgFusionObserver;
122
123/* Where a fragment sits in one graph. */
124typedef struct {
125 uint64_t members;
126 uint32_t exit;
127 uint32_t n_steps;
128 uint16_t node[AG_META_MAX_STEPS]; /* graph node of each step */
129 uint32_t n_ext;
130 uint16_t ext[AG_MAX_IN]; /* graph node feeding each port */
131 uint32_t n_obj;
132 RxObjRef obj[AG_META_MAX_OBJ];
133} AgFusionSite;
134
135typedef struct {
136 uint32_t min_occurrences;
137 uint32_t min_graphs;
138 uint32_t max_fail_permille;
139 uint32_t min_steps;
140} AgFusionPolicy;
141
142/* Why a pattern is or is not a candidate. */
143enum {
144 AG_FUSE_ELIGIBLE = 0,
145 AG_FUSE_TOO_SMALL,
146 AG_FUSE_TOO_FEW,
147 AG_FUSE_TOO_FEW_GRAPHS,
148 AG_FUSE_FAILURE_RATE,
149 AG_FUSE_UNSTABLE
150};
151
152/* MetaSkill life. Promotion is one way up; REJECTED, SLOWER and QUARANTINED
153 * are final. */
154enum {
155 AG_MS_CANDIDATE = 1,
156 AG_MS_VERIFIED,
157 AG_MS_REJECTED,
158 AG_MS_MEASURED,
159 AG_MS_SLOWER,
160 AG_MS_CANARY_PASSED,
161 AG_MS_QUARANTINED,
162 AG_MS_PROMOTED,
163 AG_MS_PUBLISHED
164};
165
166/* Verification verdict reasons, first failure wins. */
167enum {
168 AG_VR_OK = 0,
169 AG_VR_IDENTITY, /* recomputed identities differ from what it carries */
170 AG_VR_CONTRACT, /* input/output contract differs from the fragment */
171 AG_VR_AUTHORITY, /* authority contract differs from the fragment's */
172 AG_VR_EFFECT, /* effect contract differs, or it performs an effect */
173 AG_VR_RESOURCE,
174 AG_VR_NOT_RUNNABLE, /* cannot run on this host (hardware; learned outside its table) */
175 AG_VR_SEMANTIC,
176 AG_VR_FAILURE, /* differs when an input failed or was skipped */
177 AG_VR_EVIDENCE
178};
179
180typedef struct {
181 uint32_t vectors, observed, generated, status_combos;
182 uint32_t semantic_mismatch, failure_mismatch, evidence_mismatch, not_runnable;
183 uint32_t table_hits;
184 int identity_ok, contract_ok, authority_equal, effect_equal, resource_equal;
185 int pass;
186 uint32_t reason;
187} AgFusionVerdict;
188
189typedef struct {
190 uint32_t runs; /* per leg */
191 double crumbs_before, crumbs_after; /* per run */
192 double commits_before, commits_after; /* reactions that computed, per run */
193 uint64_t median_ns_before, median_ns_after;
194 uint32_t spread_permille_before; /* (p75 - p25) / median of the original leg */
195 uint32_t tolerance_permille; /* what acceptance used (set by accept) */
196 uint64_t cpu_ns_before, cpu_ns_after; /* process CPU time per run */
197 int energy_available;
198 double energy_uj_before, energy_uj_after; /* per run, idle subtracted */
199 double energy_idle_uj; /* per run-length of idle */
200 double energy_noise_uj; /* spread of repeated idle windows, per run */
201 uint32_t reactions_before, reactions_after; /* registered */
202 uint32_t objects_before, objects_after; /* World objects the lowering created */
203 uint32_t ok_before, failed_before, ok_after, failed_after;
204} AgFusionMeasure;
205
206typedef struct {
207 uint32_t id; /* Skill Net id */
208 uint32_t state;
209 uint8_t identity[32]; /* rx_fusion_identity */
210 uint32_t n_ancestry;
211 AgAncestry ancestry[AG_FUSION_MAX_ANCESTRY];
212 struct { uint32_t n; AgType type[AG_MAX_IN]; uint8_t mode[AG_MAX_IN]; } input_contract;
213 struct { AgType type; uint8_t may_fail; } output_contract;
214 /* READ on each object slot and nothing else: no effect rights, no
215 * write, no admin. The binding decides which resource a slot is. */
216 struct { uint32_t n_obj; uint32_t rights[AG_META_MAX_OBJ]; } authority_contract;
217 RxResourceNeed resource_contract;
218 struct { uint32_t performs, publishes, proposals; } effect_contract;
219 AgRealization realization;
220 AgMetaProgram reference; /* the reference graph: the fragment as observed */
221 uint32_t n_samples; /* observed inputs and what the reference produced */
222 AgFusionSample samples[AG_FUSION_MAX_SAMPLES];
223 AgFusionVerdict verdict;
224 AgFusionMeasure measure;
225 uint32_t canary_runs, canary_divergences;
226 uint64_t generation; /* the generation that promoted it */
227} AgMetaSkill;
228
229typedef struct {
230 uint32_t n;
231 AgMetaSkill *m[AG_FUSION_MAX_LIBRARY];
232} AgFusionLibrary;
233
234typedef struct {
235 int verdict;
236 uint32_t applied;
237 struct { uint32_t skill; uint32_t steps; uint32_t node; } fused[AG_FUSION_MAX_APPLIED];
238 uint32_t refused_unpublished; /* MetaSkills in the library that were not published */
239} AgFusionReport;
240
241/* ---- observation ---- */
242
243void rx_fusion_observer_init(AgFusionObserver *obs);
244
245/* Take in one run of `g` (lowered result `got`, sequential reference `ref`).
246 * Only a verified run counts: every node's status, value and evidence equal
247 * the reference's, the outcome is decided, and every evidence node of a
248 * successful run has its word. Returns 1 when taken in, 0 when refused. */
249int rx_fusion_observe(AgFusionObserver *obs, const RxWorld *w, const AgGraph *g,
250 const AgResult *got, const AgReference *ref);
251
252int rx_fusion_judge(const AgFusionObserver *obs, uint32_t pattern, const AgFusionPolicy *pol);
253
254/* Eligible patterns, most steps first, then most occurrences. */
255uint32_t rx_fusion_candidates(const AgFusionObserver *obs, const AgFusionPolicy *pol,
256 uint32_t *idx, uint32_t max);
257
258/* ---- candidate ---- */
259
260int rx_fusion_build(const AgFusionPattern *p, AgRealKind kind, uint32_t skill_id, AgMetaSkill *out);
261
262/* SHA-256 over the id, contracts, realization identity, reference
263 * signature and ancestry. */
264void rx_fusion_identity(const AgMetaSkill *m, uint8_t out[32]);
265
266/* ---- verification ---- */
267
268/* Test the candidate against its reference fragment. Inputs: every observed
269 * sample, then every combination of OK / FAILED / SKIPPED over the ports,
270 * each with generated values and World contents (seeded). Both sides run in
271 * the sequential reference: the original fragment step by step as graph
272 * nodes, the candidate as one AG_META node. `skills` holds the procedures
273 * the steps call. Sets state VERIFIED or REJECTED. */
274int rx_fusion_verify(AgMetaSkill *m, const AgSkillTable *skills, uint64_t seed);
275
276/* ---- measurement, canary ---- */
277
278/* Rewrite `g` with a MetaSkill that is verified but not published, for
279 * measurement and canary only. `trial` receives a copy of `skills` plus the
280 * realization. compile never calls this. */
281int rx_fusion_trial(const AgGraph *g, RxWorld *w, const AgMetaSkill *m, const AgSkillTable *skills,
282 AgSkillTable *trial, AgGraph *out, uint32_t *applied);
283
284/* Accept a measurement: fewer reactions computing per run, latency within
285 * the original's own spread (never less than `tolerance_permille`) of the
286 * original, and the same success and failure counts. Sets MEASURED or
287 * SLOWER. Returns 0 when accepted. */
288int rx_fusion_accept_measure(AgMetaSkill *m, const AgFusionMeasure *ms, uint32_t tolerance_permille);
289
290/* One canary run: `diverged` when the shadow result differed from
291 * production. After `required` clean runs: CANARY_PASSED. Any divergence:
292 * QUARANTINED. */
293int rx_fusion_canary(AgMetaSkill *m, int diverged, uint32_t required);
294
295/* ---- promotion and publication ---- */
296
297/* Propose a generation carrying the MetaSkill (realization bytes, identities,
298 * contracts, verification and measurement) and promote it with `req` (its
299 * candidate id is filled in). The promotion right is checked by `auth` on
300 * the native authority. Sets PROMOTED. Returns RX_GEN_* or AG_FUSION_E_*. */
301int rx_fusion_promote(AgMetaSkill *m, RxGenStore *store, uint32_t proposer,
302 const RxPromotionRequest *req, RxGenAuthFn auth, void *auth_ctx);
303
304/* Publish into the Skill Net table: the promoted generation must be the
305 * active one and its bytes must match the MetaSkill as it is now. */
306int rx_fusion_publish(AgMetaSkill *m, const RxGenStore *store, AgSkillTable *skills);
307
308/* ---- use ---- */
309
310/* Every occurrence of a fragment with this signature, up to `max`. */
311uint32_t rx_fusion_find(const AgGraph *g, const RxWorld *w, const uint8_t signature[32],
312 AgFusionSite *sites, uint32_t max);
313
314/* The step evidence an original run produced at one site (the chain an
315 * AG_META node would write). */
316uint64_t rx_fusion_site_steps(const AgGraph *g, const AgFusionSite *site, const AgMetaProgram *p,
317 const AgResult *r);
318
319/* Apply every published MetaSkill in `lib` whose realization `skills`
320 * carries. Validates the result and checks the fused node's authority is
321 * exactly the union of the steps'. */
322int rx_fusion_apply(AgGraph *g, RxWorld *w, const AgFusionLibrary *lib, const AgSkillTable *skills,
323 AgFusionReport *rep);
324
325/* rx_graph_compile (optimized), then rx_fusion_apply, then the report of
326 * missing authority and resources for the fused graph. */
327int rx_fusion_compile(const AgGoal *goal, RxWorld *w, const AgCapTable *caps,
328 const AgConstraints *cons, const AgLibrary *procedures,
329 const AgFusionLibrary *lib, const AgSkillTable *skills, AgGraph *out,
330 AgReport *rep, AgFusionReport *frep);
331
332const char *rx_fusion_state_name(uint32_t state);
333const char *rx_fusion_reason_name(uint32_t reason);
334
335#endif /* RX_FUSION_H */