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 / tests / runtime / rx_composition_gate.c

349 lines · 17058 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 * COMPOSITION-2 gate (WP-E): the whole causal path in one process, in fresh
19 * directories, against the real World, J-Space, Cortex, Capability Graph and
20 * Aienux authority (nothing mocked). Writes a JSON receipt with one verdict
21 * per step; the wrapper (tools/composition_gate.sh) passes the commit and
22 * stores the receipt under its content hash. A step is PASS only when its
23 * check held; the gate verdict is PASS only when every step of both runs did.
24 *
25 * usage: rx_composition_gate <git-commit> <receipt-path>
26 *
27 * Built with -DRXC_GATE_GPU (rx_composition_gate_gpu) the two Skills run on
28 * the GB10 (rx_compose_gpu_skill.h); every other step is unchanged. That
29 * tier is PASS only when all 14 steps pass AND every Skill call executed on
30 * the chip with a checked result (receipt "chip" block).
31 */
32#include "rx_compose_fixture.h"
33#ifdef RXC_GATE_GPU
34#include "rx_compose_gpu_skill.h"
35#define GATE_TIER "GPU"
36#else
37#define GATE_TIER "HOST"
38#endif
39
40#include <ftw.h>
41#include <sys/stat.h>
42#include <unistd.h>
43
44#define N_STEPS 14
45
46typedef struct {
47 int pass;
48 char detail[200];
49} Step;
50
51typedef struct {
52 Step st[N_STEPS];
53 uint8_t record_digest[32], winner_digest[32];
54 JsBranchRef old_ref, new_ref, cand[RXC_K];
55 uint64_t claim[RXC_K], evidence, promotion, goal_crumb;
56} Run;
57
58static const char *step_name[N_STEPS] = {
59 "canonical_machine_id", "register_capability_and_pinned_skill", "create_world",
60 "goal_needs_capability", "fork_alternatives", "one_fails_contract", "select_survivor",
61 "commit_world", "loser_not_externalized", "seal_winner_reclaim_losers",
62 "recorded_in_cortex", "close_reopen_replay", "recall_and_provenance", "deterministic_rerun"
63};
64
65static Fx g_fx;
66static RxCompose g_c;
67
68static int rm_one(const char *p, const struct stat *sb, int flag, struct FTW *ftw) {
69 (void)sb; (void)flag; (void)ftw;
70 return remove(p);
71}
72static void rmtree(const char *p) { nftw(p, rm_one, 16, FTW_DEPTH | FTW_PHYS); }
73
74#define STEP(r, i, cond, ...) do { \
75 (r)->st[(i) - 1].pass = !!(cond); \
76 snprintf((r)->st[(i) - 1].detail, sizeof (r)->st[0].detail, __VA_ARGS__); \
77 } while (0)
78
79static int ref_eq(JsBranchRef a, JsBranchRef b) { return a.id == b.id && a.gen == b.gen; }
80
81static int all_pass(const Run *r, int upto) {
82 for (int i = 0; i < upto; i++)
83 if (!r->st[i].pass) return 0;
84 return 1;
85}
86
87static void run_once(const char *dir, Run *r) {
88 memset(r, 0, sizeof *r);
89 for (int i = 0; i < N_STEPS; i++) snprintf(r->st[i].detail, sizeof r->st[i].detail, "not reached");
90
91 /* 1. canonical machine identity: stored with the composition, a foreign
92 * identity is refused. */
93 memset(&g_c, 0, sizeof g_c);
94 int rc = fx_open(&g_fx, &g_c, dir, 1);
95 if (rc != RX_OK) { STEP(r, 1, 0, "open failed %d", rc); return; }
96 fx_close(&g_fx, &g_c);
97 char p[256];
98 snprintf(p, sizeof p, "%s/machine.id", dir);
99 AienMachineId stored, mine = g_fx.self;
100 int loaded = aien_mid_load(p, &stored) == AIEN_MID_OK && aien_mid_equal(&stored, &mine);
101 g_fx.self = fx_mid(2);
102 memset(&g_c, 0, sizeof g_c);
103 int foreign = fx_open(&g_fx, &g_c, dir, 1);
104 g_fx.self = mine;
105 if (foreign == RX_OK) fx_close(&g_fx, &g_c);
106 STEP(r, 1, loaded && foreign == RX_ERR_IDENTITY, "stored=%d foreign_open=%d", loaded, foreign);
107
108 /* 2. local capability + digest-pinned Skill in the Capability Graph. */
109 CqKey keys[4];
110 uint32_t na = sr_skill_capabilities(&g_fx.cat, FX_SKILL_A, 1, keys, 4);
111 uint32_t nb = sr_skill_capabilities(&g_fx.cat, FX_SKILL_B, 1, keys, 4);
112 STEP(r, 2, na == 1 && nb == 1, "skill_a_caps=%u skill_b_caps=%u", na, nb);
113
114 /* 3. the World. */
115 memset(&g_c, 0, sizeof g_c);
116 rc = fx_open(&g_fx, &g_c, dir, 1);
117 if (rc != RX_OK) { STEP(r, 3, 0, "open failed %d", rc); return; }
118 r->old_ref = rx_compose_state(&g_c);
119 JsBranchInfo bi;
120 int root_ok = js_branch_info(&g_c.js, r->old_ref, &bi) == JS_OK && !bi.staged;
121 STEP(r, 3, root_ok && g_c.attached && g_c.state.id == RXC_SLOT_STATE,
122 "state_ref=%u/%u attached=%d", r->old_ref.id, r->old_ref.gen, g_c.attached);
123
124 /* 4..8: one goal; Skill A breaks the contract, Skill B passes. */
125 fx_a_bad = 1;
126 RxcResult o;
127 rc = fx_run(&g_fx, &g_c, 5, &o);
128 fx_a_bad = 0;
129 if (rc != RX_OK) { STEP(r, 4, 0, "run failed %d", rc); fx_close(&g_fx, &g_c); return; }
130 r->goal_crumb = o.goal_crumb;
131 int routed = o.n_alternatives == 2 && o.route[0].verdict == SR_OK &&
132 o.route[1].verdict == SR_OK &&
133 o.route[0].chosen.local && o.route[1].chosen.local &&
134 o.route[0].chosen.capability_id == 1 && o.route[1].chosen.capability_id == 2;
135 STEP(r, 4, routed && o.goal_crumb, "alternatives=%u skills=%u,%u goal_crumb=%llu",
136 o.n_alternatives, o.route[0].chosen.skill_id, o.route[1].chosen.skill_id,
137 (unsigned long long)o.goal_crumb);
138 r->cand[0] = o.cand_ref[0];
139 r->cand[1] = o.cand_ref[1];
140 int forked = (o.cand_ref[0].id || o.cand_ref[0].gen) && (o.cand_ref[1].id || o.cand_ref[1].gen) &&
141 !ref_eq(o.cand_ref[0], o.cand_ref[1]) && !ref_eq(o.cand_ref[0], r->old_ref) &&
142 !ref_eq(o.cand_ref[1], r->old_ref);
143 STEP(r, 5, forked, "cand0=%u/%u cand1=%u/%u", o.cand_ref[0].id, o.cand_ref[0].gen,
144 o.cand_ref[1].id, o.cand_ref[1].gen);
145 const CxObject *ev = cx_get(&g_c.cx, o.cx_evidence);
146 uint64_t passmask = ev && ev->n > RXC_EP_PASSMASK ? cx_payload(&g_c.cx, ev)[RXC_EP_PASSMASK] : 99;
147 STEP(r, 6, passmask == 2, "passmask=%llu (bit k = candidate k met the contract)",
148 (unsigned long long)passmask);
149 STEP(r, 7, o.winner == 1 && o.result == 16, "winner=%u result=%llu", o.winner,
150 (unsigned long long)o.result);
151 r->new_ref = o.new_ref;
152 STEP(r, 8, o.outcome == RXC_OUT_COMMITTED && ref_eq(rx_compose_state(&g_c), o.new_ref) &&
153 ref_eq(o.new_ref, o.cand_ref[1]),
154 "outcome=%d new_ref=%u/%u", o.outcome, o.new_ref.id, o.new_ref.gen);
155
156 /* 9. the loser is not externalized: its branch is gone and its claim is
157 * admitted as a loser, never promoted. */
158 uint32_t lp = 0;
159 for (uint64_t id = 1; id <= g_c.cx.n; id++) {
160 const CxObject *x = cx_get(&g_c.cx, id);
161 if (x && x->kind == CX_K_PROMOTION && x->links[0] == o.cx_candidate[0]) lp++;
162 }
163 const CxObject *la = cx_get(&g_c.cx, o.cx_admission[0]);
164 int loser_ok = js_branch_check(&g_c.js, o.cand_ref[0]) == JS_ERR_STALE && lp == 0 && la &&
165 la->tag == RXC_ADMIT_LOSER && la->links[0] == o.cx_candidate[0];
166 STEP(r, 9, loser_ok, "loser_stale=%d loser_promotions=%u loser_admission=%llu",
167 js_branch_check(&g_c.js, o.cand_ref[0]) == JS_ERR_STALE, lp,
168 (unsigned long long)o.cx_admission[0]);
169
170 /* 10. winner sealed (not staged, no owner), losers reclaimed, the
171 * superseded branch released. */
172 int winfo = js_branch_info(&g_c.js, o.new_ref, &bi) == JS_OK;
173 int sealed = winfo && !bi.staged && bi.owner == 0 && bi.locality == JS_HOME_LOCAL;
174 int old_stale = js_branch_check(&g_c.js, r->old_ref) == JS_ERR_STALE;
175 STEP(r, 10, sealed && old_stale && o.reclaimed >= 1 && fx_live_branches(&g_c.js) == 1,
176 "winner_sealed=%d old_stale=%d reclaimed=%u live=%u", sealed, old_stale, o.reclaimed,
177 fx_live_branches(&g_c.js));
178
179 /* 11. recorded in Cortex. */
180 const CxObject *pr = cx_get(&g_c.cx, o.cx_promotion);
181 const CxObject *wc = cx_get(&g_c.cx, o.cx_candidate[1]);
182 int rec = pr && wc && ev && pr->kind == CX_K_PROMOTION && pr->links[0] == o.cx_candidate[1] &&
183 pr->links[1] == o.cx_evidence && ev->protect == CX_PROT_VERIFY_EVIDENCE &&
184 cx_payload(&g_c.cx, wc)[RXC_CP_REF] == fx_pack(o.new_ref);
185 r->claim[0] = o.cx_candidate[0];
186 r->claim[1] = o.cx_candidate[1];
187 r->evidence = o.cx_evidence;
188 r->promotion = o.cx_promotion;
189 STEP(r, 11, rec, "claims=%llu,%llu evidence=%llu promotion=%llu",
190 (unsigned long long)o.cx_candidate[0], (unsigned long long)o.cx_candidate[1],
191 (unsigned long long)o.cx_evidence, (unsigned long long)o.cx_promotion);
192 fx_close(&g_fx, &g_c);
193
194 /* 12. close + reopen: the journal replays and verifies, the World names
195 * the durable winner, the loser is not recoverable. */
196 memset(&g_c, 0, sizeof g_c);
197 rc = fx_open(&g_fx, &g_c, dir, 1);
198 if (rc != RX_OK) { STEP(r, 12, 0, "reopen failed %d", rc); return; }
199 uint8_t dg[32];
200 int same = js_branch_content_digest(&g_c.js, r->new_ref.id, dg) == JS_OK &&
201 memcmp(dg, o.winner_digest, 32) == 0;
202 int replay = cx_verify_chain(&g_c.cx) == CX_OK && ref_eq(rx_compose_state(&g_c), r->new_ref) &&
203 same && js_branch_check(&g_c.js, r->cand[0]) == JS_ERR_STALE &&
204 g_c.rolled_back == 0 && g_c.recovered_completed == 0;
205 STEP(r, 12, replay, "records=%llu state=%u/%u content_match=%d rolled_back=%u",
206 (unsigned long long)g_c.cx.n, rx_compose_state(&g_c).id, rx_compose_state(&g_c).gen,
207 same, g_c.rolled_back);
208 memcpy(r->winner_digest, dg, 32);
209
210 /* 13. recall + provenance chain: promotion -> claim -> candidate World
211 * record (branch ref) and goal World record (outside crumb). */
212 CxFilter f = { CX_CLAIM, CX_K_CANDIDATE, 0, 1 };
213 CxRecord recs[16];
214 uint32_t nr = cx_recall(&g_c.cx, RXC_CX_SUBJECT(RXC_SLOT_STATE), 0, UINT64_MAX, &f, recs, 16);
215 int recalled = 0;
216 for (uint32_t i = 0; i < nr; i++)
217 if (recs[i].hdr.id == r->claim[1] && recs[i].verified &&
218 recs[i].payload[RXC_CP_REF] == fx_pack(r->new_ref) &&
219 (uint32_t)recs[i].payload[RXC_CP_SKILL] == FX_SKILL_B)
220 recalled = 1;
221 uint64_t prov[64];
222 uint32_t np = cx_provenance(&g_c.cx, r->promotion, prov, 64);
223 int goal_ok = 0, cand_ok = 0;
224 for (uint32_t i = 0; i < np; i++) {
225 const CxObject *x = cx_get(&g_c.cx, prov[i]);
226 if (!x) continue;
227 const uint64_t *xp = cx_payload(&g_c.cx, x);
228 if (x->kind == CX_K_WORK_ACCEPTED && xp[CX_WREC_CRUMB] == r->goal_crumb) goal_ok = 1;
229 if (x->kind == CX_K_EXEC_COMMIT && x->subject == RXC_CX_SUBJECT(RXC_SLOT_CAND1) &&
230 xp[CX_WREC_FIELD0 + RXC_C_REF] == fx_pack(r->new_ref))
231 cand_ok = 1;
232 }
233 STEP(r, 13, recalled && goal_ok && cand_ok, "recalled=%d provenance=%u goal=%d candidate=%d",
234 recalled, np, goal_ok, cand_ok);
235 rx_compose_record_digest(&g_c.cx, r->record_digest);
236 fx_close(&g_fx, &g_c);
237}
238
239static void hex(const uint8_t *d, char *out) { fx_hex(d, 32, out); }
240
241int main(int argc, char **argv) {
242 if (argc != 3) {
243 fprintf(stderr, "usage: %s <git-commit> <receipt-path>\n", argv[0]);
244 return 2;
245 }
246 const char *commit = argv[1], *outp = argv[2];
247 char base[128];
248 snprintf(base, sizeof base, "/tmp/rx_composition_gate.XXXXXX");
249 if (!mkdtemp(base)) { perror("mkdtemp"); return 2; }
250 if (fx_init(&g_fx) != 0) { fprintf(stderr, "fixture init failed\n"); return 2; }
251#ifdef RXC_GATE_GPU
252 if (gpu_sk_init() != 0) { fprintf(stderr, "GB10 kernel synthesis failed\n"); return 2; }
253 g_fx.skills.skill[0].fn = gpu_sk_a;
254 g_fx.skills.skill[1].fn = gpu_sk_b;
255#endif
256
257 static Run run[2];
258 for (int i = 0; i < 2; i++) {
259 char d[200];
260 snprintf(d, sizeof d, "%s/run%d", base, i);
261 run_once(d, &run[i]);
262 }
263 int same_rec = memcmp(run[0].record_digest, run[1].record_digest, 32) == 0;
264 int same_win = memcmp(run[0].winner_digest, run[1].winner_digest, 32) == 0;
265 int both = all_pass(&run[0], N_STEPS - 1) && all_pass(&run[1], N_STEPS - 1);
266 char h0[65], h1[65], w0[65], w1[65];
267 hex(run[0].record_digest, h0);
268 hex(run[1].record_digest, h1);
269 hex(run[0].winner_digest, w0);
270 hex(run[1].winner_digest, w1);
271 for (int i = 0; i < 2; i++)
272 STEP(&run[i], 14, both && same_rec && same_win && (run[0].winner_digest[0] | run[0].winner_digest[1]) != 0,
273 "record_digest_equal=%d winner_digest_equal=%d both_runs_pass=%d", same_rec, same_win,
274 both);
275 int pass = all_pass(&run[0], N_STEPS) && all_pass(&run[1], N_STEPS);
276#ifdef RXC_GATE_GPU
277 /* Two runs, each runs Skill A and Skill B once on the chip. */
278 int chip_ok = g_gpu.failures == 0 && g_gpu.out_of_range == 0 && g_gpu.runs[0] >= 2 &&
279 g_gpu.runs[1] >= 2 && g_gpu.zero_libcuda_linkage && g_gpu.zero_cuda_symbols &&
280 g_gpu.zero_libcuda_runtime;
281 pass = pass && chip_ok;
282#endif
283
284 char mid_hex[2 * AIEN_MID_ID_BYTES + 1];
285 fx_hex(g_fx.self.id, AIEN_MID_ID_BYTES, mid_hex);
286 char sk_a[65], sk_b[65];
287 fx_hex(g_fx.skills.skill[0].identity, 32, sk_a);
288 fx_hex(g_fx.skills.skill[1].identity, 32, sk_b);
289
290 FILE *f = fopen(outp, "w");
291 if (!f) { perror(outp); return 2; }
292 fprintf(f, "{\n \"gate\": \"COMPOSITION-2\",\n \"commit\": \"%s\",\n", commit);
293 fprintf(f, " \"verdict\": \"%s\",\n \"machine_id\": \"%s\",\n", pass ? "PASS" : "FAIL", mid_hex);
294 fprintf(f, " \"tier\": \"%s\",\n", GATE_TIER);
295#ifdef RXC_GATE_GPU
296 char kd[65];
297 fx_hex(g_gpu.kernel_digest, 32, kd);
298 fprintf(f, " \"chip\": {\"device\": \"/dev/nvidia0\", \"target\": \"GB10 sm_121\", "
299 "\"path\": \"m16_native + nvrm (no CUDA)\", \"op\": \"INT32 matmul %ux%ux%u, C[0][0] = w0*x + w1\",\n"
300 " \"kernel_code_digest\": \"%s\", \"kernel_insns\": %zu, "
301 "\"executions_skill_a\": %u, \"executions_skill_b\": %u, \"failures\": %u, "
302 "\"out_of_range\": %u, \"elapsed_ns_total\": %llu,\n"
303 " \"completion_marker\": %u, \"intermediate_semaphore\": %u, "
304 "\"zero_libcuda_linkage\": %d, \"zero_cuda_symbols\": %d, \"zero_libcuda_runtime\": %d, "
305 "\"chip_ok\": %d},\n",
306 GPU_SK_DIM, GPU_SK_K, GPU_SK_DIM, kd, g_gpu.kernel_insns, g_gpu.runs[0], g_gpu.runs[1],
307 g_gpu.failures, g_gpu.out_of_range, (unsigned long long)g_gpu.elapsed_ns, g_gpu.last_marker,
308 g_gpu.last_semaphore, g_gpu.zero_libcuda_linkage, g_gpu.zero_cuda_symbols,
309 g_gpu.zero_libcuda_runtime, chip_ok);
310#endif
311 fprintf(f, " \"skills\": [{\"id\": %u, \"version\": 1, \"digest\": \"%s\"}, "
312 "{\"id\": %u, \"version\": 1, \"digest\": \"%s\"}],\n",
313 FX_SKILL_A, sk_a, FX_SKILL_B, sk_b);
314 fprintf(f, " \"record_digest\": [\"%s\", \"%s\"],\n", h0, h1);
315 fprintf(f, " \"winner_digest\": [\"%s\", \"%s\"],\n", w0, w1);
316 fprintf(f, " \"runs\": [\n");
317 for (int i = 0; i < 2; i++) {
318 Run *r = &run[i];
319 fprintf(f, " {\"run\": %d, \"old_ref\": \"%u/%u\", \"new_ref\": \"%u/%u\", "
320 "\"candidate_refs\": [\"%u/%u\", \"%u/%u\"], \"goal_crumb\": %llu, "
321 "\"claims\": [%llu, %llu], \"evidence\": %llu, \"promotion\": %llu,\n"
322 " \"steps\": [\n",
323 i, r->old_ref.id, r->old_ref.gen, r->new_ref.id, r->new_ref.gen, r->cand[0].id,
324 r->cand[0].gen, r->cand[1].id, r->cand[1].gen, (unsigned long long)r->goal_crumb,
325 (unsigned long long)r->claim[0], (unsigned long long)r->claim[1],
326 (unsigned long long)r->evidence, (unsigned long long)r->promotion);
327 for (int s = 0; s < N_STEPS; s++)
328 fprintf(f, " {\"step\": %d, \"name\": \"%s\", \"result\": \"%s\", \"detail\": \"%s\"}%s\n",
329 s + 1, step_name[s], r->st[s].pass ? "PASS" : "FAIL", r->st[s].detail,
330 s + 1 < N_STEPS ? "," : "");
331 fprintf(f, " ]}%s\n", i == 0 ? "," : "");
332 }
333 fprintf(f, " ]\n}\n");
334 fclose(f);
335
336 for (int s = 0; s < N_STEPS; s++)
337 printf(" step %2d %-38s %s / %s %s\n", s + 1, step_name[s], run[0].st[s].pass ? "PASS" : "FAIL",
338 run[1].st[s].pass ? "PASS" : "FAIL", run[0].st[s].detail);
339#ifdef RXC_GATE_GPU
340 printf(" chip: skill_a=%u skill_b=%u failures=%u out_of_range=%u elapsed_ns=%llu chip_ok=%d\n",
341 g_gpu.runs[0], g_gpu.runs[1], g_gpu.failures, g_gpu.out_of_range,
342 (unsigned long long)g_gpu.elapsed_ns, chip_ok);
343 gpu_sk_free();
344#endif
345 printf("COMPOSITION-2 gate (%s tier): %s\n", GATE_TIER, pass ? "PASS" : "FAIL");
346 fx_free(&g_fx);
347 rmtree(base);
348 return pass ? 0 : 1;
349}