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_capability_query.c

1268 lines · 61574 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 * OMEGA_CAPABILITY_QUERY -- AIEN states a CapabilityNeed; Omega compiles it
19 * into probes of the Capability Graph, the Skill Network, the MCP registry,
20 * local physical capabilities and Fabric advertisements, and returns a short
21 * list of CapabilityCandidates ranked on explicit dimensions.
22 *
23 * Two parts.
24 *
25 * Scenarios on small hand-built catalogs: aliases and specializations, each
26 * hard constraint, Fabric leases and locality, MCP sessions, caller-defined
27 * tradeoffs (two orders, two winners), tolerance bands, determinism and
28 * registration-order independence, authority decided through the native
29 * Aienux authority (held, not held, revoked), and the chosen candidate driving
30 * an action graph.
31 *
32 * Scaling on synthetic catalogs of 100, 1,000, 10,000 and 100,000
33 * capabilities. Every query is compared with an oracle written separately in
34 * this file: a full scan of every entry with its own match rule, its own
35 * Pareto front and its own selection. Measured: candidate recall, selection
36 * precision, query latency, bytes that cross into cognition, and how each
37 * grows with the catalog.
38 */
39#include "runtime/aienos_cap.h"
40#include "runtime/rx_capq.h"
41#include "runtime/rx_graph.h"
42#include "runtime/rx_world.h"
43#include "omega_evidence.h"
44#include "sha256.h"
45
46#include <sched.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <sys/utsname.h>
52#include <time.h>
53#include <unistd.h>
54
55static int g_checks;
56static int g_fail;
57
58#define CHECK(cond, ...) do { \
59 g_checks++; \
60 if (!(cond)) { \
61 g_fail++; \
62 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
63 fprintf(stderr, __VA_ARGS__); \
64 fputc('\n', stderr); \
65 } \
66 } while (0)
67
68static uint64_t now_ns(void) {
69 struct timespec ts;
70 clock_gettime(CLOCK_MONOTONIC, &ts);
71 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
72}
73
74#define SELF 1u
75#define NOW_US 1000000ull
76#define RES_BASE 0xB000000ull
77#define ALIAS(op) (0x80000000u | (op))
78
79/* ---- receipt figures ---- */
80
81#define N_SIZES 4
82static const uint32_t SIZES[N_SIZES] = { 100, 1000, 10000, 100000 };
83
84typedef struct {
85 uint32_t catalog;
86 uint32_t n_ops;
87 uint32_t queries, with_candidates, no_op;
88 uint64_t expected, hit; /* recall: oracle's ranked list vs returned */
89 uint64_t returned, returned_ok; /* precision: returned that the oracle finds feasible */
90 uint32_t top1_equal, list_mismatch, feasible_count_mismatch, front_mismatch;
91 uint64_t q_median_ns, q_p99_ns, q_max_ns;
92 uint64_t hot_median_ns;
93 uint64_t compile_median_ns, lookup_median_ns;
94 uint64_t oracle_median_ns;
95 double probed_mean;
96 uint64_t probed_max;
97 uint64_t bytes_max, bytes_sum;
98 uint64_t need_bytes;
99 uint64_t desc_bytes, catalog_bytes;
100 uint64_t desc_bytes_read;
101 uint64_t naive_named_desc_bytes; /* descriptions of just the named operation's providers */
102 double naive_named_precision; /* feasible share of everything named that operation */
103 double build_ms;
104} SizeRow;
105
106static struct {
107 char cpus[128];
108 SizeRow row[N_SIZES];
109 uint32_t scenario_checks;
110 uint32_t tradeoff_distinct_winners;
111 uint32_t det_runs, det_mismatch;
112 uint32_t held_validated, revoked_seen;
113 uint32_t graph_bound, graph_missing_reported;
114 uint32_t seam_skill_ran;
115 uint64_t mints_during_query;
116} R;
117
118/* ---- deterministic generator ---- */
119
120static uint64_t g_rng;
121static uint64_t rnd(void) {
122 g_rng ^= g_rng << 13;
123 g_rng ^= g_rng >> 7;
124 g_rng ^= g_rng << 17;
125 return g_rng;
126}
127static uint64_t rnd_n(uint64_t n) { return n ? rnd() % n : 0; }
128static uint64_t log_uniform(uint64_t lo, uint64_t hi) {
129 /* lo..hi spread over decades */
130 uint32_t decades = 0;
131 for (uint64_t x = lo; x < hi; x *= 10) decades++;
132 uint64_t base = lo;
133 for (uint32_t d = (uint32_t)rnd_n(decades ? decades : 1); d; d--) base *= 10;
134 uint64_t v = base + rnd_n(base * 9);
135 return v > hi ? hi : v;
136}
137
138/* ---- small hand-built catalogs ---- */
139
140static CqEntry E(uint32_t cap, uint32_t op, uint32_t src, uint32_t machine) {
141 CqEntry e;
142 memset(&e, 0, sizeof e);
143 e.capability_id = cap;
144 e.realization_id = cap * 10;
145 e.skill_id = cap;
146 e.op = op;
147 e.source = (uint8_t)src;
148 e.machine_id = machine;
149 e.effects = CQ_FX_PURE;
150 e.in_types = 0x1;
151 e.out_types = 0x2;
152 e.confidence_ppm = 900000;
153 e.reliability_ppm = 990000;
154 e.evidence_level = CQ_EV_MEASURED;
155 e.cost = 10;
156 e.latency_us = 100;
157 e.energy_uj = 100;
158 e.live = 1;
159 return e;
160}
161
162static const char DESC[] =
163 "{\"name\":\"example\",\"description\":\"a provider description the query never reads\"}";
164
165static void reg(CqCatalog *c, const CqEntry *e) {
166 CHECK(cq_register(c, e, DESC, (uint32_t)sizeof DESC - 1) == CQ_OK, "register %u", e->capability_id);
167}
168
169static CqNeed open_need(uint32_t op) {
170 CqNeed n;
171 memset(&n, 0, sizeof n);
172 n.semantic_operation = op;
173 n.accepted_input_types = ~0ull;
174 n.effect_class = 0x1FF;
175 n.authority_ceiling.resource_lo = 0;
176 n.authority_ceiling.resource_hi = UINT64_MAX;
177 n.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT;
178 n.locality_constraints.allowed = CQ_LOC_LOCAL | CQ_LOC_FABRIC;
179 return n;
180}
181
182static CqTradeoffs order1(uint32_t d) {
183 CqTradeoffs t;
184 memset(&t, 0, sizeof t);
185 t.n_order = 1;
186 t.order[0] = (uint8_t)d;
187 t.k = CQ_MAX_K;
188 return t;
189}
190
191static int ask(const CqCatalog *c, const CqNeed *n, const CqTradeoffs *t, const CqHeld *h,
192 CqResult *r) {
193 CqPlan p;
194 cq_compile(c, n, &p);
195 return cq_query(c, &p, n, t, h, NOW_US, r, NULL);
196}
197
198static int has(const CqResult *r, uint32_t cap) {
199 for (uint32_t i = 0; i < r->n; i++)
200 if (r->cand[i].capability_id == cap) return 1;
201 return 0;
202}
203
204/* ops: 1 read (tool), 2 read.local (specializes 1), 3 matvec (physical),
205 * 4 summarize (skill), 5 send (tool, MCP allowed), 6 read.cached (specializes 2) */
206static void small_catalog(CqCatalog *c) {
207 CHECK(cq_catalog_init(c, SELF, 16, 8) == CQ_OK, "init");
208 uint32_t tool = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_MCP) |
209 CQ_SRC(CQ_SRC_FABRIC);
210 CHECK(cq_op_define(c, 1, 0, tool) == CQ_OK, "op 1");
211 CHECK(cq_op_define(c, 2, 1, tool) == CQ_OK, "op 2");
212 CHECK(cq_op_define(c, 3, 0, CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_PHYSICAL) |
213 CQ_SRC(CQ_SRC_FABRIC)) == CQ_OK, "op 3");
214 CHECK(cq_op_define(c, 4, 0, CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC)) == CQ_OK, "op 4");
215 CHECK(cq_op_define(c, 5, 0, tool) == CQ_OK, "op 5");
216 CHECK(cq_op_define(c, 6, 2, tool) == CQ_OK, "op 6");
217 CHECK(cq_op_define(c, 8, 0, tool) == CQ_E_ARG, "operations are numbered densely");
218 CHECK(cq_alias(c, 1001, 1) == CQ_OK && cq_alias(c, 1003, 3) == CQ_OK &&
219 cq_alias(c, 1005, 5) == CQ_OK, "aliases");
220 CHECK(cq_machine_advertise(c, 2, NOW_US + 5000000) == CQ_OK, "machine 2 leased");
221 CHECK(cq_machine_advertise(c, 3, NOW_US - 1) == CQ_OK, "machine 3 lease expired");
222 CHECK(cq_machine_advertise(c, SELF, NOW_US) == CQ_E_ARG, "self is not a Fabric machine");
223}
224
225static void t_resolution(void) {
226 printf("[*] naming: aliases, specializations, unknown operations, sources that apply\n");
227 CqCatalog c;
228 small_catalog(&c);
229 CqEntry e = E(10, 1, CQ_SRC_GRAPH, SELF);
230 reg(&c, &e);
231 e = E(20, 2, CQ_SRC_SKILL, SELF);
232 reg(&c, &e);
233 e = E(30, 6, CQ_SRC_MCP, SELF);
234 reg(&c, &e);
235 e = E(40, 3, CQ_SRC_PHYSICAL, SELF);
236 reg(&c, &e);
237 e = E(50, 3, CQ_SRC_MCP, SELF);
238 CHECK(cq_register(&c, &e, NULL, 0) == CQ_E_ARG, "MCP cannot realize a physical operation");
239 e = E(51, 1, CQ_SRC_FABRIC, SELF);
240 CHECK(cq_register(&c, &e, NULL, 0) == CQ_E_ARG, "the Fabric speaks only for other machines");
241 e = E(52, 1, CQ_SRC_GRAPH, 2);
242 CHECK(cq_register(&c, &e, NULL, 0) == CQ_E_ARG, "another machine arrives only through the Fabric");
243 CqResult r;
244 CqNeed n = open_need(1);
245 CqTradeoffs t = order1(CQ_DIM_COST);
246 t.include_dominated = 1;
247 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_E_UNBUILT, "a catalog is indexed before use");
248 CHECK(cq_catalog_build(&c) == CQ_OK, "build");
249
250 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && r.n == 3 && has(&r, 10) && has(&r, 20) && has(&r, 30),
251 "a need for 'read' reaches its specializations (%u)", r.n);
252 n.semantic_operation = 1001;
253 CqResult r2;
254 CHECK(ask(&c, &n, &t, NULL, &r2) == CQ_OK && r2.n == 3 &&
255 memcmp(&r, &r2, cq_result_bytes(&r)) == 0,
256 "asked by an alias, the provider registered under the canonical name is found");
257 CqPlan p1, p2;
258 CqNeed a = open_need(1), b = open_need(1001);
259 cq_compile(&c, &a, &p1);
260 cq_compile(&c, &b, &p2);
261 CHECK(memcmp(p1.digest, p2.digest, 32) == 0 && p1.op == 1 && p1.n_ops == 3,
262 "the plan's identity does not depend on the name used");
263 n = open_need(2);
264 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && r.n == 2 && !has(&r, 10),
265 "a specialized need does not reach the general provider");
266 n = open_need(0x7FFFFFF0u);
267 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_E_NO_OP && r.n == 0, "an unknown operation");
268 n = open_need(3);
269 cq_compile(&c, &n, &p1);
270 CHECK(p1.verdict == CQ_OK && !(p1.sources & CQ_SRC(CQ_SRC_MCP)) &&
271 (p1.sources & CQ_SRC(CQ_SRC_PHYSICAL)), "MCP is not consulted for a physical operation");
272 n.sources = CQ_SRC(CQ_SRC_MCP);
273 cq_compile(&c, &n, &p1);
274 CHECK(p1.verdict == CQ_E_NO_SOURCE, "no applicable source");
275 n = open_need(1);
276 cq_compile(&c, &n, &p1);
277 CHECK(!(p1.sources & CQ_SRC(CQ_SRC_PHYSICAL)), "local physical is not consulted for a tool operation");
278 cq_catalog_free(&c);
279}
280
281static void t_constraints(void) {
282 printf("[*] each hard constraint excludes exactly what it should\n");
283 CqCatalog c;
284 small_catalog(&c);
285 CqEntry e;
286 e = E(1, 5, CQ_SRC_GRAPH, SELF); reg(&c, &e); /* baseline */
287 e = E(2, 5, CQ_SRC_GRAPH, SELF); e.in_types = 0x5; reg(&c, &e); /* needs type 2 */
288 e = E(3, 5, CQ_SRC_GRAPH, SELF); e.out_types = 0x8; reg(&c, &e); /* wrong output */
289 e = E(4, 5, CQ_SRC_MCP, SELF); e.effects = CQ_FX_EXTERNAL_WRITE; reg(&c, &e);
290 e = E(5, 5, CQ_SRC_MCP, SELF); e.effects = CQ_FX_EXTERNAL_WRITE | CQ_FX_EXTERNAL_IRREVERSIBLE;
291 reg(&c, &e);
292 e = E(6, 5, CQ_SRC_GRAPH, SELF); e.auth_resource = RES_BASE + 1; e.auth_rights = RX_RIGHT_READ;
293 reg(&c, &e);
294 e = E(7, 5, CQ_SRC_GRAPH, SELF); e.auth_resource = RES_BASE + 1;
295 e.auth_rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; reg(&c, &e);
296 e = E(8, 5, CQ_SRC_GRAPH, SELF); e.auth_resource = RES_BASE + 99; e.auth_rights = RX_RIGHT_READ;
297 reg(&c, &e);
298 e = E(9, 5, CQ_SRC_GRAPH, SELF); e.latency_us = 50000; reg(&c, &e);
299 e = E(10, 5, CQ_SRC_GRAPH, SELF); e.energy_uj = 90000; reg(&c, &e);
300 e = E(11, 5, CQ_SRC_GRAPH, SELF); e.reliability_ppm = 800000; reg(&c, &e);
301 e = E(12, 5, CQ_SRC_MCP, SELF); e.evidence_level = CQ_EV_DECLARED; reg(&c, &e);
302 e = E(13, 5, CQ_SRC_GRAPH, SELF); e.live = 0; reg(&c, &e);
303 e = E(14, 5, CQ_SRC_MCP, SELF); e.live = 0; reg(&c, &e); /* session gone */
304 e = E(15, 5, CQ_SRC_FABRIC, 2); reg(&c, &e); /* leased */
305 e = E(16, 5, CQ_SRC_FABRIC, 3); reg(&c, &e); /* lease expired */
306 e = E(17, 5, CQ_SRC_FABRIC, 4); reg(&c, &e); /* never advertised */
307 e = E(18, 5, CQ_SRC_GRAPH, SELF); e.cost = 5000; reg(&c, &e);
308 e = E(19, 5, CQ_SRC_GRAPH, SELF); e.confidence_ppm = 300000; reg(&c, &e);
309 CHECK(cq_catalog_build(&c) == CQ_OK, "build");
310
311 CqTradeoffs t = order1(CQ_DIM_COST);
312 t.include_dominated = 1;
313 CqResult r;
314 CqNeed n = open_need(5);
315 CqStats st;
316 CqPlan p;
317 cq_compile(&c, &n, &p);
318 CHECK(cq_query(&c, &p, &n, &t, NULL, NOW_US, &r, &st) == CQ_OK && r.n_feasible == 15 &&
319 st.rejected_dead == 4 && st.desc_bytes_read == 0,
320 "open need: all but the withdrawn, the dead session and the two unleased machines (%u, dead %llu)",
321 r.n_feasible, (unsigned long long)st.rejected_dead);
322 CHECK(!has(&r, 13) && !has(&r, 14) && !has(&r, 16) && !has(&r, 17) && has(&r, 15),
323 "Fabric capabilities live exactly as long as their machine's lease");
324
325 n = open_need(5); n.accepted_input_types = 0x1;
326 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 2) && has(&r, 1), "inputs: only types the caller can supply");
327 n = open_need(5); n.required_output_types = 0x2;
328 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 3) && has(&r, 1), "outputs: must include what is required");
329 n = open_need(5); n.effect_class = CQ_FX_PURE | CQ_FX_EXTERNAL_WRITE;
330 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && has(&r, 4) && !has(&r, 5),
331 "effects: an irreversible external write is excluded when not accepted");
332 n = open_need(5); n.effect_class = CQ_FX_PURE;
333 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 4) && !has(&r, 5), "effects: pure only");
334 n = open_need(5); n.authority_ceiling.rights = RX_RIGHT_READ;
335 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && has(&r, 6) && !has(&r, 7),
336 "authority: rights above the ceiling are excluded");
337 n = open_need(5); n.authority_ceiling.resource_lo = RES_BASE; n.authority_ceiling.resource_hi = RES_BASE + 10;
338 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && has(&r, 6) && !has(&r, 8) && has(&r, 1),
339 "authority: resources outside the ceiling are excluded; needing none is fine");
340 n = open_need(5); n.latency_budget = 1000;
341 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 9) && has(&r, 1), "latency budget");
342 n = open_need(5); n.energy_budget = 1000;
343 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 10) && has(&r, 1), "energy budget");
344 n = open_need(5); n.reliability_requirement = 950000;
345 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 11) && has(&r, 1), "reliability requirement");
346 n = open_need(5); n.evidence_requirement = CQ_EV_MEASURED;
347 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 12) && has(&r, 1), "evidence requirement");
348 n = open_need(5); n.locality_constraints.allowed = CQ_LOC_LOCAL;
349 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 15) && has(&r, 1), "local only");
350 n = open_need(5); n.locality_constraints.allowed = CQ_LOC_FABRIC;
351 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && r.n == 1 && has(&r, 15), "Fabric only");
352 n = open_need(5); n.locality_constraints.machine = 2;
353 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && r.n == 1 && has(&r, 15), "pinned to machine 2");
354 n = open_need(5); n.locality_constraints.machine = SELF;
355 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && !has(&r, 15) && has(&r, 1), "pinned to this machine");
356 n = open_need(5); n.sources = CQ_SRC(CQ_SRC_MCP);
357 CHECK(ask(&c, &n, &t, NULL, &r) == CQ_OK && r.n == 3 && has(&r, 4) && has(&r, 5) && has(&r, 12),
358 "restricted to the MCP registry");
359 n = open_need(5);
360 CqTradeoffs t2 = t;
361 t2.max_cost = 1000;
362 t2.min_confidence = 500000;
363 CHECK(ask(&c, &n, &t2, NULL, &r) == CQ_OK && !has(&r, 18) && !has(&r, 19) && has(&r, 1),
364 "the caller's hard maxima on cost and minimum confidence");
365 cq_catalog_free(&c);
366}
367
368static void t_tradeoffs(void) {
369 printf("[*] ranking: Pareto front, caller-defined order, tolerance bands, no single score\n");
370 CqCatalog c;
371 small_catalog(&c);
372 CqEntry e;
373 /* fast: low latency, high energy. frugal: the reverse. middling: dominated by nothing
374 * on (latency, energy) but beaten by `better` on both. */
375 e = E(100, 3, CQ_SRC_PHYSICAL, SELF); e.latency_us = 100; e.energy_uj = 9000; e.cost = 50; reg(&c, &e);
376 e = E(200, 3, CQ_SRC_GRAPH, SELF); e.latency_us = 900; e.energy_uj = 1000; e.cost = 40; reg(&c, &e);
377 e = E(300, 3, CQ_SRC_GRAPH, SELF); e.latency_us = 500; e.energy_uj = 5000; e.cost = 30; reg(&c, &e);
378 e = E(400, 3, CQ_SRC_GRAPH, SELF); e.latency_us = 600; e.energy_uj = 6000; e.cost = 30; reg(&c, &e);
379 e = E(500, 3, CQ_SRC_FABRIC, 2); e.latency_us = 105; e.energy_uj = 2000; e.cost = 60; reg(&c, &e);
380 CHECK(cq_catalog_build(&c) == CQ_OK, "build");
381 CqNeed n = open_need(1003);
382 CqResult r;
383
384 CqTradeoffs lat = order1(CQ_DIM_LATENCY);
385 lat.n_order = 2;
386 lat.order[1] = CQ_DIM_ENERGY;
387 CHECK(ask(&c, &n, &lat, NULL, &r) == CQ_OK && r.n >= 1 && r.cand[0].capability_id == 100,
388 "latency first: the fast one");
389 uint32_t w_lat = r.cand[0].capability_id;
390 CHECK(r.n_front == 3 && !has(&r, 300) && !has(&r, 400),
391 "front over (latency, energy): 300 and 400 are beaten by 500 on both");
392 CqTradeoffs en = order1(CQ_DIM_ENERGY);
393 en.n_order = 2;
394 en.order[1] = CQ_DIM_LATENCY;
395 CHECK(ask(&c, &n, &en, NULL, &r) == CQ_OK && r.n >= 1 && r.cand[0].capability_id == 200,
396 "energy first: the frugal one");
397 uint32_t w_en = r.cand[0].capability_id;
398 if (w_lat != w_en) R.tradeoff_distinct_winners++;
399 CHECK(w_lat != w_en, "same candidates, two caller tradeoffs, two winners");
400
401 /* Tolerance: latency within 10% counts as tied, so energy decides 100 vs 500. */
402 lat.tolerance_ppm[CQ_DIM_LATENCY] = 100000;
403 CHECK(ask(&c, &n, &lat, NULL, &r) == CQ_OK && r.cand[0].capability_id == 500 &&
404 r.cand[1].capability_id == 100,
405 "latency within 10%% is a tie; energy then prefers the Fabric provider");
406 CqTradeoffs loc = order1(CQ_DIM_LOCALITY);
407 loc.n_order = 2;
408 loc.order[1] = CQ_DIM_LATENCY;
409 CHECK(ask(&c, &n, &loc, NULL, &r) == CQ_OK && r.cand[0].capability_id == 100 && r.cand[0].local,
410 "locality first keeps the work on this machine");
411 CqTradeoffs cost = order1(CQ_DIM_COST);
412 CHECK(ask(&c, &n, &cost, NULL, &r) == CQ_OK && r.n == 2 && r.n_front == 2 &&
413 r.cand[0].capability_id == 300 && r.cand[1].capability_id == 400,
414 "cost only: the tie at 30 is broken by id, deterministically");
415 CqTradeoffs k1 = lat;
416 k1.k = 1;
417 CHECK(ask(&c, &n, &k1, NULL, &r) == CQ_OK && r.n == 1 && r.n_feasible == 5 &&
418 cq_result_bytes(&r) == offsetof(CqResult, cand) + sizeof(CqCandidate),
419 "k = 1 returns one record; the others stay counts");
420 /* Every candidate carries its dimensions; none carries a combined score. */
421 CHECK(sizeof(CqCandidate) == 88, "candidate record is %zu bytes", sizeof(CqCandidate));
422 R.scenario_checks += 8;
423 cq_catalog_free(&c);
424}
425
426static void t_determinism(void) {
427 printf("[*] determinism: repeated queries and registration order\n");
428 CqCatalog a, b;
429 CHECK(cq_catalog_init(&a, SELF, 8, 8) == CQ_OK && cq_catalog_init(&b, SELF, 8, 8) == CQ_OK, "init");
430 for (CqCatalog *c = &a; c; c = c == &a ? &b : NULL) {
431 CHECK(cq_op_define(c, 1, 0, CQ_SRC_ALL) == CQ_OK, "op");
432 CHECK(cq_machine_advertise(c, 2, NOW_US * 10) == CQ_OK, "machine");
433 }
434 CqEntry es[200];
435 g_rng = 0x5eed;
436 for (uint32_t i = 0; i < 200; i++) {
437 uint32_t src = (uint32_t)rnd_n(CQ_SOURCES);
438 es[i] = E(i + 1, 1, src, src == CQ_SRC_FABRIC ? 2 : SELF);
439 es[i].latency_us = 100 + rnd_n(50); /* many near-ties */
440 es[i].energy_uj = 100 + rnd_n(50);
441 es[i].cost = 1 + rnd_n(5);
442 }
443 for (uint32_t i = 0; i < 200; i++) reg(&a, &es[i]);
444 for (uint32_t i = 200; i-- > 0;) reg(&b, &es[i]);
445 CHECK(cq_catalog_build(&a) == CQ_OK && cq_catalog_build(&b) == CQ_OK, "build");
446 CqNeed n = open_need(1);
447 CqTradeoffs t = order1(CQ_DIM_COST);
448 t.n_order = 3;
449 t.order[1] = CQ_DIM_LATENCY;
450 t.order[2] = CQ_DIM_ENERGY;
451 t.tolerance_ppm[CQ_DIM_LATENCY] = 50000;
452 t.include_dominated = 1;
453 CqResult r0, r;
454 CHECK(ask(&a, &n, &t, NULL, &r0) == CQ_OK && r0.n == CQ_MAX_K, "reference answer");
455 for (uint32_t i = 0; i < 8; i++) {
456 R.det_runs += 2;
457 CHECK(ask(&a, &n, &t, NULL, &r) == CQ_OK, "query");
458 if (memcmp(&r, &r0, sizeof r)) R.det_mismatch++;
459 CHECK(ask(&b, &n, &t, NULL, &r) == CQ_OK, "query");
460 if (memcmp(&r, &r0, sizeof r)) R.det_mismatch++;
461 }
462 CHECK(R.det_mismatch == 0, "identical answers across runs and registration orders (%u)", R.det_mismatch);
463 cq_catalog_free(&a);
464 cq_catalog_free(&b);
465}
466
467/* ---- authority through the native Aienux authority, and the action graph ---- */
468
469enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 61 };
470#define RES_CELL 0xA000001ull
471#define RES_RUN 0xA000002ull
472#define RES_HELD (RES_BASE + 1)
473#define RES_ASK (RES_BASE + 2)
474
475static AienosCapAdmin *g_admin;
476static uint64_t g_mints;
477
478static RxCapRef mint(uint32_t subject, uint64_t resource, uint32_t rights) {
479 AienosCapRef office;
480 aienos_cap_office(g_admin, &office);
481 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
482 AienosCapRef r = { UINT32_MAX, 0 };
483 if (aienos_cap_mint(g_admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 };
484 g_mints++;
485 return (RxCapRef){ r.cap_id, r.generation };
486}
487
488static uint64_t sk_scale(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
489 (void)attempt;
490 *failed = 0;
491 return (n ? in[0] : 0) * 3 + 1;
492}
493static uint64_t sk_other(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
494 (void)attempt;
495 *failed = 0;
496 return (n ? in[0] : 0) + 1000;
497}
498
499static void t_authority_and_graph(void) {
500 printf("[*] authority decided by the native AIENOS authority; the winner drives an action graph\n");
501 AienosCapView *view;
502 CHECK(aienos_cap_start(&g_admin, &view) == 0, "authority");
503 RxWorld w;
504 CHECK(rx_world_init_native(&w, view, 2, 1u << 14) == RX_OK, "world");
505 w.external_subject = SUBJ_EXTERNAL;
506 AgCapTable caps;
507 memset(&caps, 0, sizeof caps);
508 caps.subject = SUBJ_PLAN;
509 RxCapRef held_cap = mint(SUBJ_PLAN, RES_HELD, RX_RIGHT_READ);
510 caps.cap[caps.n].ref = held_cap;
511 caps.cap[caps.n].resource = RES_HELD;
512 caps.cap[caps.n].rights = RX_RIGHT_READ;
513 caps.n++;
514 /* A forged entry: listed in the table, never minted. Validation must refuse it. */
515 caps.cap[caps.n].ref = (RxCapRef){ 4000, 1 };
516 caps.cap[caps.n].resource = RES_ASK;
517 caps.cap[caps.n].rights = RX_RIGHT_READ;
518 caps.n++;
519
520 CqCatalog c;
521 small_catalog(&c);
522 CqEntry e;
523 e = E(7, 4, CQ_SRC_SKILL, SELF); e.skill_id = 7; e.latency_us = 800;
524 e.auth_resource = RES_HELD; e.auth_rights = RX_RIGHT_READ; reg(&c, &e);
525 e = E(8, 4, CQ_SRC_SKILL, SELF); e.skill_id = 8; e.latency_us = 200;
526 e.auth_resource = RES_ASK; e.auth_rights = RX_RIGHT_READ; reg(&c, &e);
527 CHECK(cq_catalog_build(&c) == CQ_OK, "build");
528 CqHeld h = { &w, &caps };
529 CqNeed n = open_need(4);
530 CqResult r;
531 uint64_t mints0 = g_mints;
532
533 CqTradeoffs auth = order1(CQ_DIM_AUTHORITY);
534 auth.n_order = 2;
535 auth.order[1] = CQ_DIM_LATENCY;
536 CHECK(ask(&c, &n, &auth, &h, &r) == CQ_OK && r.n == 2 && r.cand[0].capability_id == 7 &&
537 r.cand[0].required_authority.held == 1,
538 "authority first: the capability the principal already holds (validated by the view)");
539 R.held_validated++;
540 CqCandidate held_winner = r.cand[0];
541 CqTradeoffs fast = order1(CQ_DIM_LATENCY);
542 CHECK(ask(&c, &n, &fast, &h, &r) == CQ_OK && r.n == 1 && r.cand[0].capability_id == 8 &&
543 r.cand[0].required_authority.held == 0,
544 "latency first: the faster one, whose authority is not held (the forged table entry is refused)");
545 CqCandidate ask_winner = r.cand[0];
546 R.mints_during_query = g_mints - mints0;
547 CHECK(R.mints_during_query == 0, "queries minted nothing");
548
549 /* The chosen candidate becomes an action graph's skill node. */
550 AgSkillTable skills;
551 memset(&skills, 0, sizeof skills);
552 skills.n = 2;
553 skills.skill[0] = (AgSkill){ 7, sk_scale, { 0x07 } };
554 skills.skill[1] = (AgSkill){ 8, sk_other, { 0x08 } };
555 for (int pass = 0; pass < 2; pass++) {
556 const CqCandidate *cand = pass == 0 ? &held_winner : &ask_winner;
557 static AgGraph tmpl;
558 rx_graph_init(&tmpl, 0);
559 int k = rx_graph_node(&tmpl, AG_CONST, AG_T_U64);
560 tmpl.nodes[k].imm = 14;
561 int s = rx_graph_node(&tmpl, AG_SKILL, AG_T_U64);
562 int v = rx_graph_node(&tmpl, AG_VERIFY, AG_T_VERDICT);
563 tmpl.nodes[v].imm = 0;
564 tmpl.nodes[v].imm2 = 1000;
565 rx_graph_data(&tmpl, (uint32_t)k, (uint32_t)s, 0, AG_EDGE_DATA);
566 rx_graph_data(&tmpl, (uint32_t)s, (uint32_t)v, 0, AG_EDGE_DATA);
567 rx_graph_success(&tmpl, (uint32_t)v, AG_OK);
568 CHECK(cq_bind_skill_node(&tmpl, (uint32_t)s, cand) == CQ_OK, "bind the candidate");
569 CHECK(tmpl.nodes[s].op == cand->skill_id, "the node runs the candidate's procedure");
570 AgLibrary lib = { 1, { { 1, &tmpl } } };
571 AgGoal goal = { 1, 0, { { 0, 0 } } };
572 AgConstraints cons;
573 memset(&cons, 0, sizeof cons);
574 cons.max_effects = UINT32_MAX;
575 cons.budget.slots = RX_MAX_REACTIONS;
576 cons.budget.memory_bytes = UINT64_MAX;
577 cons.budget.energy_budget = UINT64_MAX;
578 cons.budget.offered_locality = UINT32_MAX;
579 cons.budget.offered_accel = UINT32_MAX;
580 cons.budget.compute_mask = UINT32_MAX;
581 static AgGraph g;
582 AgReport rep;
583 int rc = rx_graph_compile(&goal, &w, &caps, &cons, &lib, 1, &g, &rep);
584 CHECK(rc == AG_OK_READY, "compile %d", rc);
585 if (pass == 0) {
586 CHECK(rep.n_missing == 0, "held authority: nothing missing");
587 R.graph_bound++;
588 RxCapRef cell = mint(SUBJ_PLAN, RES_CELL, RX_RIGHT_READ | RX_RIGHT_WRITE);
589 RxCapRef runc = mint(SUBJ_PLAN, RES_RUN, RX_RIGHT_READ);
590 RxCapRef ext = mint(SUBJ_EXTERNAL, RES_RUN, RX_RIGHT_WRITE);
591 static AgLowered L;
592 CHECK(rx_graph_lower(&L, &w, &g, &skills, &caps, RES_CELL, cell, RES_RUN, runc, NULL) == 0,
593 "lower");
594 CHECK(rx_graph_start(&L, ext, 1) > 0, "start");
595 CHECK(rx_world_wait_quiescent(&w, 30000) == RX_OK, "settle");
596 AgResult res;
597 rx_graph_collect(&L, 1, &res);
598 int sn = -1;
599 for (uint32_t i = 0; i < g.n_nodes; i++)
600 if (g.nodes[i].alive && g.nodes[i].kind == AG_SKILL) sn = (int)i;
601 CHECK(res.outcome == AG_RUN_SUCCESS && sn >= 0 && res.value[sn] == 14 * 3 + 1,
602 "the graph ran the chosen capability's procedure (outcome %d)", res.outcome);
603 if (res.outcome == AG_RUN_SUCCESS) R.seam_skill_ran++;
604 } else {
605 CHECK(rep.n_missing == 1 && rep.missing[0].resource == RES_ASK &&
606 rep.missing[0].rights == RX_RIGHT_READ,
607 "authority not held: compile reports exactly the candidate's requirement");
608 if (rep.n_missing == 1) R.graph_missing_reported++;
609 }
610 }
611
612 /* Revoke the held capability; the next query sees it. */
613 AienosCapRef office;
614 aienos_cap_office(g_admin, &office);
615 CHECK(aienos_cap_revoke(g_admin, office, (AienosCapRef){ held_cap.cap_id, held_cap.generation }) == 0,
616 "revoke");
617 auth.include_dominated = 1;
618 CHECK(ask(&c, &n, &auth, &h, &r) == CQ_OK && r.n == 2 && r.cand[0].capability_id == 8 &&
619 has(&r, 7) && r.cand[0].required_authority.held == 0,
620 "after revocation nothing is held; latency decides");
621 for (uint32_t i = 0; i < r.n; i++)
622 if (r.cand[i].capability_id == 7 && r.cand[i].required_authority.held == 0) R.revoked_seen++;
623 CHECK(R.revoked_seen == 1, "the revoked capability is reported as not held");
624 cq_catalog_free(&c);
625 rx_world_wait_quiescent(&w, 30000);
626 rx_world_destroy(&w);
627 aienos_cap_stop(g_admin, view);
628}
629
630/* ---- synthetic catalogs ---- */
631
632typedef struct {
633 CqCatalog c;
634 uint32_t n_ops, hot;
635 uint32_t *parent; /* the generator's own copy of the tree */
636 uint32_t n_hot;
637} Synth;
638
639static const uint16_t FX_PRESET[] = {
640 CQ_FX_PURE, CQ_FX_PURE, CQ_FX_PURE, CQ_FX_READ_FILESYSTEM, CQ_FX_READ_NETWORK,
641 CQ_FX_SPAWN_PROCESS | CQ_FX_LOCAL_EPHEMERAL, CQ_FX_WORLD_MUTATION, CQ_FX_EXTERNAL_WRITE,
642 CQ_FX_EXTERNAL_WRITE | CQ_FX_EXTERNAL_IRREVERSIBLE, CQ_FX_SECRET_BEARING | CQ_FX_READ_NETWORK,
643};
644#define N_TYPES 12
645#define N_MACH 16
646
647static uint32_t op_domains(uint32_t op) {
648 switch (op % 3) {
649 case 0: return CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_MCP) | CQ_SRC(CQ_SRC_FABRIC);
650 case 1: return CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_PHYSICAL) | CQ_SRC(CQ_SRC_FABRIC);
651 default: return CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC);
652 }
653}
654
655static uint32_t pick_bit(uint32_t mask) {
656 uint32_t n = (uint32_t)__builtin_popcount(mask), k = (uint32_t)rnd_n(n);
657 for (uint32_t b = 0; b < 32; b++)
658 if (mask & (1u << b)) {
659 if (k == 0) return b;
660 k--;
661 }
662 return 0;
663}
664
665static uint32_t make_desc(char *buf, uint32_t cap, uint32_t op, uint32_t len) {
666 static const char *words[] = { "returns", "the", "value", "of", "a", "semantic", "object",
667 "given", "input", "schema", "type", "string", "required",
668 "optional", "provider", "tool", "resource", "timeout" };
669 int n = snprintf(buf, len, "{\"name\":\"op%u.impl%u\",\"description\":\"", op, cap);
670 uint32_t p = (uint32_t)n;
671 while (p + 16 < len) {
672 const char *w = words[rnd_n(sizeof words / sizeof words[0])];
673 size_t l = strlen(w);
674 memcpy(buf + p, w, l);
675 p += (uint32_t)l;
676 buf[p++] = ' ';
677 }
678 memcpy(buf + p, "\",\"inputSchema\":{}}", 19);
679 return p + 19 <= len ? p + 19 : len;
680}
681
682static int synth_build(Synth *s, uint32_t n_entries, double *build_ms) {
683 memset(s, 0, sizeof *s);
684 uint64_t t0 = now_ns();
685 g_rng = 0x9e3779b97f4a7c15ull ^ n_entries;
686 s->n_ops = n_entries / 6 > 4 ? n_entries / 6 : 4;
687 s->hot = 1;
688 s->n_hot = n_entries / 10 < 1000 ? n_entries / 10 : 1000;
689 if (cq_catalog_init(&s->c, SELF, s->n_ops, N_MACH) != CQ_OK) return -1;
690 s->parent = calloc(s->n_ops + 1, sizeof *s->parent);
691 if (!s->parent) return -1;
692 for (uint32_t op = 1; op <= s->n_ops; op++) {
693 uint32_t parent = 0;
694 if (op > 4 && op % 4 == 0) {
695 parent = op - 1 - (uint32_t)rnd_n(3);
696 /* a specialization lives where its general operation can */
697 if (op_domains(op) & ~op_domains(parent)) parent = 0;
698 }
699 s->parent[op] = parent;
700 if (cq_op_define(&s->c, op, parent, op_domains(op)) != CQ_OK) return -1;
701 if (cq_alias(&s->c, ALIAS(op), op) != CQ_OK) return -1;
702 }
703 for (uint32_t m = 2; m < 2 + N_MACH; m++)
704 cq_machine_advertise(&s->c, m, m < 2 + N_MACH - 4 ? NOW_US + 1000000000ull : NOW_US - 1);
705 char desc[2048];
706 for (uint32_t i = 0; i < n_entries; i++) {
707 CqEntry e;
708 memset(&e, 0, sizeof e);
709 e.capability_id = i + 1;
710 e.realization_id = (uint32_t)(0x100000u + i);
711 e.op = i < s->n_hot ? s->hot : 2 + (i - s->n_hot) % (s->n_ops - 1);
712 e.source = (uint8_t)pick_bit(op_domains(e.op));
713 e.machine_id = e.source == CQ_SRC_FABRIC ? 2 + (uint32_t)rnd_n(N_MACH) : SELF;
714 e.skill_id = e.source == CQ_SRC_SKILL ? 1 + (uint32_t)rnd_n(4096) : 0;
715 e.in_types = 1ull << rnd_n(N_TYPES);
716 if (rnd_n(10) < 3) e.in_types |= 1ull << rnd_n(N_TYPES);
717 e.out_types = 1ull << rnd_n(N_TYPES);
718 if (rnd_n(10) < 4) e.out_types |= 1ull << rnd_n(N_TYPES);
719 e.effects = e.source == CQ_SRC_MCP ? FX_PRESET[3 + rnd_n(7)]
720 : FX_PRESET[rnd_n(sizeof FX_PRESET / sizeof FX_PRESET[0])];
721 uint32_t rr = (uint32_t)rnd_n(10);
722 e.auth_rights = rr < 3 ? 0 : rr < 7 ? RX_RIGHT_READ : rr < 9 ? RX_RIGHT_READ | RX_RIGHT_WRITE
723 : RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT;
724 e.auth_resource = e.auth_rights ? RES_BASE + rnd_n(64) : 0;
725 e.cost = log_uniform(1, 1000000);
726 e.latency_us = log_uniform(1, 10000000);
727 e.energy_uj = log_uniform(1, 10000000);
728 e.confidence_ppm = 500000 + (uint32_t)rnd_n(500001);
729 e.reliability_ppm = 900000 + (uint32_t)rnd_n(100001);
730 e.evidence_level = (uint8_t)(e.source == CQ_SRC_MCP ? rnd_n(2) : rnd_n(4));
731 e.evidence_ref = rnd();
732 e.live = rnd_n(100) < (e.source == CQ_SRC_MCP ? 90u : 97u);
733 uint32_t len = make_desc(desc, e.capability_id, e.op, 256 + (uint32_t)rnd_n(1280));
734 if (cq_register(&s->c, &e, desc, len) != CQ_OK) return -1;
735 }
736 int rc = cq_catalog_build(&s->c);
737 *build_ms = (double)(now_ns() - t0) / 1e6;
738 return rc;
739}
740
741static void synth_free(Synth *s) {
742 cq_catalog_free(&s->c);
743 free(s->parent);
744}
745
746/* Loosen a random need toward one real provider, as AIEN usually asks for
747 * something that exists. Each field still has its own chance to be too strict. */
748static void seed_need(CqNeed *n, const CqEntry *e) {
749 n->accepted_input_types |= e->in_types;
750 if (n->required_output_types && rnd_n(10) < 7) n->required_output_types = e->out_types & -e->out_types;
751 if (rnd_n(10) < 8) n->effect_class |= e->effects;
752 if (rnd_n(10) < 8) {
753 n->authority_ceiling.rights |= e->auth_rights;
754 if (e->auth_rights && e->auth_resource > n->authority_ceiling.resource_hi)
755 n->authority_ceiling.resource_hi = e->auth_resource;
756 }
757 if (rnd_n(10) < 8) {
758 n->locality_constraints.allowed |= e->machine_id == SELF ? CQ_LOC_LOCAL : CQ_LOC_FABRIC;
759 n->locality_constraints.machine = 0;
760 }
761 if (n->latency_budget && n->latency_budget < e->latency_us) n->latency_budget = e->latency_us * (1 + rnd_n(10));
762 if (n->energy_budget && n->energy_budget < e->energy_uj) n->energy_budget = e->energy_uj * (1 + rnd_n(10));
763 if (n->reliability_requirement > e->reliability_ppm && rnd_n(2)) n->reliability_requirement = 0;
764 if (n->evidence_requirement > e->evidence_level && rnd_n(10) < 8) n->evidence_requirement = e->evidence_level;
765 if (n->sources) n->sources |= 1u << e->source;
766}
767
768static CqNeed random_need(const Synth *s, int hot) {
769 CqNeed n;
770 memset(&n, 0, sizeof n);
771 const CqEntry *seed = &s->c.e[hot ? rnd_n(s->n_hot) : rnd_n(s->c.n)];
772 int seeded = rnd_n(10) < 8;
773 uint32_t op = seeded ? seed->op : hot ? s->hot : 1 + (uint32_t)rnd_n(s->n_ops);
774 n.semantic_operation = rnd_n(2) ? ALIAS(op) : op;
775 int unknown = !hot && rnd_n(100) < 4;
776 if (unknown) n.semantic_operation = 0x7FFFFF00u + (uint32_t)rnd_n(64);
777 for (uint32_t k = 4 + (uint32_t)rnd_n(6); k; k--) n.accepted_input_types |= 1ull << rnd_n(N_TYPES);
778 if (rnd_n(10) < 7) n.required_output_types = 1ull << rnd_n(N_TYPES);
779 static const uint32_t FX_NEED[] = { CQ_FX_PURE, CQ_FX_PURE | CQ_FX_READ_FILESYSTEM | CQ_FX_READ_NETWORK,
780 0x1FF & ~(CQ_FX_EXTERNAL_IRREVERSIBLE | CQ_FX_SECRET_BEARING), 0x1FF };
781 n.effect_class = FX_NEED[rnd_n(4)];
782 n.authority_ceiling.resource_lo = RES_BASE;
783 n.authority_ceiling.resource_hi = RES_BASE + (rnd_n(2) ? 31 : 63);
784 static const uint32_t RIGHTS[] = { RX_RIGHT_READ, RX_RIGHT_READ | RX_RIGHT_WRITE,
785 RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT };
786 n.authority_ceiling.rights = RIGHTS[rnd_n(3)];
787 uint32_t l = (uint32_t)rnd_n(20);
788 n.locality_constraints.allowed = l < 12 ? CQ_LOC_LOCAL | CQ_LOC_FABRIC : l < 16 ? CQ_LOC_LOCAL
789 : l < 19 ? CQ_LOC_FABRIC : CQ_LOC_LOCAL | CQ_LOC_FABRIC;
790 if (l == 19) n.locality_constraints.machine = 1 + (uint32_t)rnd_n(N_MACH + 1);
791 if (rnd_n(2)) n.latency_budget = log_uniform(100, 10000000);
792 if (rnd_n(2)) n.energy_budget = log_uniform(100, 10000000);
793 static const uint32_t REL[] = { 0, 0, 950000, 990000 };
794 n.reliability_requirement = REL[rnd_n(4)];
795 static const uint32_t EV[] = { CQ_EV_NONE, CQ_EV_NONE, CQ_EV_DECLARED, CQ_EV_MEASURED, CQ_EV_RECEIPT };
796 n.evidence_requirement = EV[rnd_n(5)];
797 if (rnd_n(10) == 0) n.sources = 1u + (uint32_t)rnd_n(CQ_SRC_ALL);
798 if (seeded && !unknown) seed_need(&n, seed);
799 return n;
800}
801
802static CqTradeoffs random_tradeoffs(uint32_t i) {
803 CqTradeoffs t;
804 memset(&t, 0, sizeof t);
805 t.k = i % 2 ? 8 : CQ_MAX_K;
806 switch (i % 5) {
807 case 0: t.n_order = 3; t.order[0] = CQ_DIM_LATENCY; t.order[1] = CQ_DIM_ENERGY; t.order[2] = CQ_DIM_COST;
808 t.tolerance_ppm[CQ_DIM_LATENCY] = 100000; break;
809 case 1: t.n_order = 2; t.order[0] = CQ_DIM_ENERGY; t.order[1] = CQ_DIM_LATENCY; break;
810 case 2: t.n_order = 3; t.order[0] = CQ_DIM_COST; t.order[1] = CQ_DIM_RELIABILITY;
811 t.order[2] = CQ_DIM_LATENCY; t.tolerance_ppm[CQ_DIM_COST] = 250000;
812 t.max_cost = 500000; break;
813 case 3: t.n_order = 3; t.order[0] = CQ_DIM_RELIABILITY; t.order[1] = CQ_DIM_CONFIDENCE;
814 t.order[2] = CQ_DIM_COST; t.min_confidence = 600000; break;
815 default: t.n_order = 0; t.include_dominated = 1; break; /* every dimension, everything feasible */
816 }
817 return t;
818}
819
820/* ---- the oracle: written separately, reads every entry ---- */
821
822static int oracle_is_under(const Synth *s, uint32_t op, uint32_t target) {
823 for (uint32_t x = op; x; x = s->parent[x])
824 if (x == target) return 1;
825 return 0;
826}
827
828static int oracle_match(const Synth *s, const CqNeed *n, const CqTradeoffs *t, const CqEntry *e,
829 uint32_t target) {
830 if (!oracle_is_under(s, e->op, target)) return 0;
831 if (n->sources && !(n->sources & (1u << e->source))) return 0;
832 if (!e->live) return 0;
833 int local = e->machine_id == SELF;
834 if (!local) {
835 /* machines 2..13 hold a lease past NOW; 14..17 expired */
836 if (e->machine_id < 2 || e->machine_id >= 2 + N_MACH - 4) return 0;
837 if (!(n->locality_constraints.allowed & CQ_LOC_FABRIC)) return 0;
838 } else if (!(n->locality_constraints.allowed & CQ_LOC_LOCAL)) {
839 return 0;
840 }
841 if (n->locality_constraints.machine && n->locality_constraints.machine != e->machine_id) return 0;
842 for (uint32_t b = 0; b < 64; b++) {
843 uint64_t bit = 1ull << b;
844 if ((e->in_types & bit) && !(n->accepted_input_types & bit)) return 0; /* can't supply */
845 if ((n->required_output_types & bit) && !(e->out_types & bit)) return 0; /* not produced */
846 }
847 for (uint32_t b = 0; b < 16; b++)
848 if ((e->effects >> b & 1) && !(n->effect_class >> b & 1)) return 0;
849 for (uint32_t b = 0; b < 32; b++)
850 if ((e->auth_rights >> b & 1) && !(n->authority_ceiling.rights >> b & 1)) return 0;
851 if (e->auth_rights && (e->auth_resource < n->authority_ceiling.resource_lo ||
852 e->auth_resource > n->authority_ceiling.resource_hi))
853 return 0;
854 if (n->latency_budget != 0 && e->latency_us > n->latency_budget) return 0;
855 if (n->energy_budget != 0 && e->energy_uj > n->energy_budget) return 0;
856 if (e->reliability_ppm < n->reliability_requirement) return 0;
857 if (e->evidence_level < n->evidence_requirement) return 0;
858 if (t->max_cost != 0 && e->cost > t->max_cost) return 0;
859 if (e->confidence_ppm < t->min_confidence) return 0;
860 return 1;
861}
862
863static void oracle_vec(const CqEntry *e, const CqTradeoffs *t, uint64_t *v, uint32_t *nd) {
864 uint64_t all[CQ_DIMS] = { e->cost, e->latency_us, e->energy_uj, 1000000 - e->confidence_ppm,
865 1000000 - e->reliability_ppm, e->auth_rights ? 1 : 0,
866 e->machine_id == SELF ? 0 : 1 };
867 if (t->n_order == 0) {
868 for (uint32_t d = 0; d < CQ_DIMS; d++) v[d] = all[d];
869 *nd = CQ_DIMS;
870 } else {
871 for (uint32_t d = 0; d < t->n_order; d++) v[d] = all[t->order[d]];
872 *nd = t->n_order;
873 }
874}
875
876typedef struct { const CqEntry *e; uint64_t v[CQ_DIMS]; uint32_t nd; int front, taken; } OItem;
877
878static int oracle_less(const OItem *a, const OItem *b) { /* strict: dimensions then ids */
879 for (uint32_t d = 0; d < a->nd; d++)
880 if (a->v[d] != b->v[d]) return a->v[d] < b->v[d];
881 if (a->e->capability_id != b->e->capability_id) return a->e->capability_id < b->e->capability_id;
882 if (a->e->realization_id != b->e->realization_id) return a->e->realization_id < b->e->realization_id;
883 if (a->e->machine_id != b->e->machine_id) return a->e->machine_id < b->e->machine_id;
884 return a->e->skill_id < b->e->skill_id;
885}
886
887static uint32_t tol_of(const CqTradeoffs *t, uint32_t d) {
888 return t->n_order ? t->tolerance_ppm[t->order[d]] : t->tolerance_ppm[d];
889}
890
891/* Returns the feasible count; fills the ranked list (capability ids) and front size. */
892static uint32_t oracle(const Synth *s, const CqNeed *n, const CqTradeoffs *t, uint32_t *list,
893 uint32_t *n_list, uint32_t *n_front, OItem *items) {
894 *n_list = 0;
895 *n_front = 0;
896 uint32_t target = 0;
897 uint32_t name = n->semantic_operation;
898 if (name & 0x80000000u) target = name & 0x7FFFFFFFu;
899 else if (name >= 1 && name <= s->n_ops) target = name;
900 if (target == 0 || target > s->n_ops) return 0;
901 uint32_t nf = 0;
902 for (uint32_t i = 0; i < s->c.n; i++) {
903 const CqEntry *e = &s->c.e[i];
904 if (!oracle_match(s, n, t, e, target)) continue;
905 items[nf].e = e;
906 oracle_vec(e, t, items[nf].v, &items[nf].nd);
907 items[nf].front = 1;
908 items[nf].taken = 0;
909 nf++;
910 }
911 for (uint32_t i = 0; i < nf; i++)
912 for (uint32_t j = 0; j < nf && items[i].front; j++) {
913 if (i == j) continue;
914 int le = 1, lt = 0;
915 for (uint32_t d = 0; d < items[i].nd; d++) {
916 if (items[j].v[d] > items[i].v[d]) le = 0;
917 if (items[j].v[d] < items[i].v[d]) lt = 1;
918 }
919 if (le && lt) items[i].front = 0;
920 }
921 uint32_t pool = 0;
922 for (uint32_t i = 0; i < nf; i++) {
923 if (items[i].front) (*n_front)++;
924 if (!t->include_dominated && !items[i].front) items[i].taken = 1;
925 else pool++;
926 }
927 uint32_t k = t->k ? t->k : CQ_MAX_K;
928 if (k > CQ_MAX_K) k = CQ_MAX_K;
929 while (*n_list < k && *n_list < pool) {
930 /* band filter per dimension, in priority order */
931 static uint8_t in[200000];
932 for (uint32_t i = 0; i < nf; i++) in[i] = !items[i].taken;
933 uint32_t nd = nf ? items[0].nd : 0;
934 for (uint32_t d = 0; d < nd; d++) {
935 uint64_t best = UINT64_MAX;
936 for (uint32_t i = 0; i < nf; i++)
937 if (in[i] && items[i].v[d] < best) best = items[i].v[d];
938 long double lim = (long double)best * (1.0L + (long double)tol_of(t, d) / 1e6L);
939 for (uint32_t i = 0; i < nf; i++)
940 if (in[i] && (long double)items[i].v[d] > lim) in[i] = 0;
941 }
942 int pick = -1;
943 for (uint32_t i = 0; i < nf; i++)
944 if (in[i] && (pick < 0 || oracle_less(&items[i], &items[pick]))) pick = (int)i;
945 if (pick < 0) break;
946 items[pick].taken = 1;
947 list[(*n_list)++] = items[pick].e->capability_id;
948 }
949 return nf;
950}
951
952static int cmp_u64(const void *a, const void *b) {
953 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;
954 return x < y ? -1 : x > y;
955}
956
957#define QUERIES 2000
958#define HOT_QUERIES 200
959
960static void t_scale(uint32_t si) {
961 uint32_t size = SIZES[si];
962 SizeRow *row = &R.row[si];
963 memset(row, 0, sizeof *row);
964 Synth s;
965 CHECK(synth_build(&s, size, &row->build_ms) == 0, "build synthetic catalog of %u", size);
966 row->catalog = size;
967 row->n_ops = s.n_ops;
968 row->desc_bytes = s.c.desc_bytes;
969 row->catalog_bytes = cq_catalog_bytes(&s.c);
970 row->need_bytes = sizeof(CqNeed) + sizeof(CqTradeoffs);
971 OItem *items = malloc((size_t)size * sizeof *items);
972 uint64_t *lat = malloc(QUERIES * sizeof *lat), *orc = malloc(QUERIES * sizeof *orc);
973 uint64_t *hot = malloc(HOT_QUERIES * sizeof *hot);
974 uint64_t *cmp = malloc(QUERIES * sizeof *cmp), *look = malloc(QUERIES * sizeof *look);
975 CHECK(items && lat && orc && hot && cmp && look, "memory");
976 uint64_t probed_sum = 0, named_total = 0, named_feasible = 0;
977 g_rng = 0xC0FFEEull + size;
978 uint32_t nq = 0;
979 for (uint32_t q = 0; q < QUERIES + HOT_QUERIES; q++) {
980 int is_hot = q >= QUERIES;
981 CqNeed n = random_need(&s, is_hot);
982 CqTradeoffs t = random_tradeoffs(q);
983 CqPlan p;
984 CqResult r;
985 CqStats st;
986 uint64_t t0 = now_ns();
987 cq_compile(&s.c, &n, &p);
988 uint64_t t1 = now_ns();
989 int rc = cq_query(&s.c, &p, &n, &t, NULL, NOW_US, &r, &st);
990 uint64_t t2 = now_ns(), dt = t2 - t0;
991 if (is_hot) hot[q - QUERIES] = dt;
992 else { lat[nq] = dt; cmp[nq] = t1 - t0; look[nq] = t2 - t1; }
993 uint32_t list[CQ_MAX_K], n_list, n_front;
994 uint64_t o0 = now_ns();
995 uint32_t nf = oracle(&s, &n, &t, list, &n_list, &n_front, items);
996 if (!is_hot) orc[nq++] = now_ns() - o0;
997
998 row->queries++;
999 if (rc == CQ_E_NO_OP) { row->no_op++; CHECK(nf == 0, "unknown operation, but the oracle found some"); continue; }
1000 if (rc != CQ_OK && rc != CQ_E_NO_SOURCE) { CHECK(0, "query failed %d", rc); continue; }
1001 if (rc == CQ_E_NO_SOURCE) r.n = r.n_feasible = r.n_front = 0;
1002 probed_sum += st.probed;
1003 if (st.probed > row->probed_max) row->probed_max = st.probed;
1004 row->desc_bytes_read += st.desc_bytes_read;
1005 size_t bytes = cq_result_bytes(&r);
1006 row->bytes_sum += bytes;
1007 if (bytes > row->bytes_max) row->bytes_max = bytes;
1008 if (r.n_feasible != nf) row->feasible_count_mismatch++;
1009 if (!t.include_dominated && r.n_front != n_front) row->front_mismatch++;
1010 row->expected += n_list;
1011 row->returned += r.n;
1012 int same = r.n == n_list;
1013 for (uint32_t i = 0; i < r.n; i++) {
1014 int in_list = 0;
1015 for (uint32_t j = 0; j < n_list; j++) in_list |= list[j] == r.cand[i].capability_id;
1016 row->hit += (uint64_t)in_list;
1017 const CqEntry *e = &s.c.e[r.cand[i].capability_id - 1];
1018 uint32_t target = cq_resolve(&s.c, n.semantic_operation);
1019 row->returned_ok += (uint64_t)oracle_match(&s, &n, &t, e, target);
1020 if (i >= n_list || list[i] != r.cand[i].capability_id) same = 0;
1021 }
1022 if (!same) row->list_mismatch++;
1023 if (n_list > 0) {
1024 row->with_candidates++;
1025 if (r.n > 0 && r.cand[0].capability_id == list[0]) row->top1_equal++;
1026 }
1027 /* What handing AIEN everything named this operation would carry. */
1028 for (uint32_t i = 0; i < p.n_ops; i++)
1029 for (uint32_t src = 0; src < CQ_SOURCES; src++) {
1030 uint32_t b = p.ops[i] * CQ_SOURCES + src;
1031 for (uint32_t j = s.c.bucket_start[b]; j < s.c.bucket_start[b + 1]; j++) {
1032 row->naive_named_desc_bytes += s.c.e[s.c.bucket[j]].desc_len;
1033 named_total++;
1034 }
1035 }
1036 named_feasible += nf;
1037 }
1038 qsort(lat, nq, sizeof *lat, cmp_u64);
1039 qsort(orc, nq, sizeof *orc, cmp_u64);
1040 qsort(hot, HOT_QUERIES, sizeof *hot, cmp_u64);
1041 qsort(cmp, nq, sizeof *cmp, cmp_u64);
1042 qsort(look, nq, sizeof *look, cmp_u64);
1043 row->compile_median_ns = cmp[nq / 2];
1044 row->lookup_median_ns = look[nq / 2];
1045 row->q_median_ns = lat[nq / 2];
1046 row->q_p99_ns = lat[(nq * 99) / 100];
1047 row->q_max_ns = lat[nq - 1];
1048 row->oracle_median_ns = orc[nq / 2];
1049 row->hot_median_ns = hot[HOT_QUERIES / 2];
1050 uint32_t answered = row->queries - row->no_op;
1051 row->probed_mean = answered ? (double)probed_sum / answered : 0;
1052 row->naive_named_desc_bytes = answered ? row->naive_named_desc_bytes / answered : 0;
1053 row->naive_named_precision = named_total ? (double)named_feasible / (double)named_total : 0;
1054
1055 printf(" %6u capabilities (%5u operations): %u queries, %u with candidates; recall %llu/%llu, "
1056 "precision %llu/%llu, top-1 %u/%u\n",
1057 size, s.n_ops, row->queries, row->with_candidates, (unsigned long long)row->hit,
1058 (unsigned long long)row->expected, (unsigned long long)row->returned_ok,
1059 (unsigned long long)row->returned, row->top1_equal, row->with_candidates);
1060 printf(" query median %llu ns (compile %llu + lookup %llu) p99 %llu ns (hot operation, %u providers: %llu ns); "
1061 "full scan median %llu ns; probed %.1f/query (max %llu)\n",
1062 (unsigned long long)row->q_median_ns, (unsigned long long)row->compile_median_ns,
1063 (unsigned long long)row->lookup_median_ns, (unsigned long long)row->q_p99_ns, s.n_hot,
1064 (unsigned long long)row->hot_median_ns, (unsigned long long)row->oracle_median_ns,
1065 row->probed_mean, (unsigned long long)row->probed_max);
1066 printf(" into cognition: max %llu bytes/query; all descriptions %llu bytes; "
1067 "named-operation descriptions %llu bytes/query\n",
1068 (unsigned long long)row->bytes_max, (unsigned long long)row->desc_bytes,
1069 (unsigned long long)row->naive_named_desc_bytes);
1070 CHECK(row->list_mismatch == 0 && row->feasible_count_mismatch == 0 && row->front_mismatch == 0,
1071 "size %u: ranked lists %u, feasible counts %u, fronts %u differ from the full scan", size,
1072 row->list_mismatch, row->feasible_count_mismatch, row->front_mismatch);
1073 CHECK(row->hit == row->expected && row->returned_ok == row->returned &&
1074 row->top1_equal == row->with_candidates, "size %u: recall, precision, selection", size);
1075 CHECK(row->desc_bytes_read == 0, "size %u: the query read no description", size);
1076 CHECK(row->bytes_max <= offsetof(CqResult, cand) + CQ_MAX_K * sizeof(CqCandidate),
1077 "size %u: bytes into cognition bounded by K", size);
1078 CHECK(row->with_candidates >= 500, "size %u: at least 500 queries with candidates to compare (%u)",
1079 size, row->with_candidates);
1080 free(items);
1081 free(lat);
1082 free(orc);
1083 free(hot);
1084 free(cmp);
1085 free(look);
1086 synth_free(&s);
1087}
1088
1089/* ---- placement and receipt ---- */
1090
1091static void place(void) {
1092 cpu_set_t set;
1093 CPU_ZERO(&set);
1094 int count = 0;
1095 char *p = R.cpus;
1096 long n = sysconf(_SC_NPROCESSORS_CONF);
1097 for (long c = 0; c < n; c++) {
1098 char path[128];
1099 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c);
1100 FILE *fp = fopen(path, "r");
1101 if (!fp) continue;
1102 unsigned long long midr = 0;
1103 int ok = fscanf(fp, "%llx", &midr) == 1;
1104 fclose(fp);
1105 if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) {
1106 CPU_SET(c, &set);
1107 count++;
1108 if (p < R.cpus + sizeof R.cpus - 5) p += sprintf(p, "%s%ld", count > 1 ? "," : "", c);
1109 }
1110 }
1111 if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) {
1112 snprintf(R.cpus, sizeof R.cpus, "unpinned");
1113 return;
1114 }
1115 printf("[*] placed on %d Cortex-X925 cores: %s\n", count, R.cpus);
1116}
1117
1118static void binary_digest(char out[65]) {
1119 strcpy(out, "unavailable");
1120 FILE *fp = fopen("/proc/self/exe", "rb");
1121 if (!fp) return;
1122 sha256_ctx c;
1123 sha256_init(&c);
1124 uint8_t buf[65536];
1125 size_t n;
1126 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n);
1127 fclose(fp);
1128 uint8_t d[32];
1129 sha256_final(&c, d);
1130 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]);
1131}
1132
1133static int scaling_ok(void) {
1134 const SizeRow *a = &R.row[0], *z = &R.row[N_SIZES - 1];
1135 /* catalog grows 1000x; the query may grow at most 10x, the bytes not at all */
1136 const uint64_t bound = offsetof(CqResult, cand) + CQ_MAX_K * sizeof(CqCandidate);
1137 for (uint32_t i = 0; i < N_SIZES; i++)
1138 if (R.row[i].bytes_max > bound) return 0;
1139 return z->q_median_ns <= 10 * (a->q_median_ns ? a->q_median_ns : 1) &&
1140 z->oracle_median_ns > 100 * a->oracle_median_ns;
1141}
1142
1143static void write_receipt(void) {
1144 char path[512];
1145 if (omega_evidence_path("CAPABILITY_QUERY/rx_capability_query_receipt.json", path, sizeof path) != 0)
1146 return;
1147 FILE *fp = fopen(path, "w");
1148 if (!fp) return;
1149 char commit[41];
1150 memset(commit, 0, sizeof commit);
1151 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8);
1152 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT");
1153 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && !omega_evidence_tree_dirty();
1154 const char *aienos = getenv("AIENOS_COMMIT");
1155 char digest[65];
1156 binary_digest(digest);
1157 struct utsname u;
1158 memset(&u, 0, sizeof u);
1159 uname(&u);
1160 int scale = scaling_ok();
1161 int pass = g_fail == 0 && scale && R.mints_during_query == 0 && R.det_mismatch == 0;
1162 fprintf(fp,
1163 "{\n"
1164 " \"schema\": \"OMEGA_CAPABILITY_QUERY_V1\",\n"
1165 " \"run_id\": \"%s\",\n"
1166 " \"candidate_commit\": %s%s%s,\n"
1167 " \"candidate_bound\": %s,\n"
1168 " \"run_commit\": \"%s\",\n"
1169 " \"tree_dirty\": %s,\n"
1170 " \"aienos_commit\": %s%s%s,\n"
1171 " \"checks\": %d,\n"
1172 " \"failures\": %d,\n"
1173 " \"test_binary_sha256\": \"%s\",\n"
1174 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"cpus\": \"%s\"},\n"
1175 " \"scope\": \"host processor; in-process synthetic catalogs; native AIENOS authority library\",\n"
1176 " \"record_bytes\": {\"need\": %zu, \"tradeoffs\": %zu, \"candidate\": %zu, \"result_header\": %zu, "
1177 "\"max_candidates\": %u},\n"
1178 " \"oracle\": \"full scan of every entry with an independently written match rule, Pareto front and selection\",\n"
1179 " \"sizes\": [\n",
1180 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null",
1181 candidate ? "\"" : "", bound ? "true" : "false", commit,
1182 omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", aienos ? aienos : "null",
1183 aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname, u.release, u.machine, R.cpus,
1184 sizeof(CqNeed), sizeof(CqTradeoffs), sizeof(CqCandidate), offsetof(CqResult, cand), CQ_MAX_K);
1185 for (uint32_t i = 0; i < N_SIZES; i++) {
1186 const SizeRow *r = &R.row[i];
1187 fprintf(fp,
1188 " {\"capabilities\": %u, \"operations\": %u, \"queries\": %u, \"with_candidates\": %u, "
1189 "\"unknown_operation\": %u,\n"
1190 " \"candidate_recall\": {\"returned_in_oracle_ranking\": %llu, \"oracle_ranking\": %llu, \"value\": %.6f},\n"
1191 " \"selection_precision\": {\"returned_feasible\": %llu, \"returned\": %llu, \"value\": %.6f,\n"
1192 " \"top1_equal\": %u, \"of\": %u, \"top1_value\": %.6f},\n"
1193 " \"ranked_list_mismatches\": %u, \"feasible_count_mismatches\": %u, \"front_mismatches\": %u,\n"
1194 " \"query_latency_ns\": {\"median\": %llu, \"p99\": %llu, \"max\": %llu, \"hot_operation_median\": %llu,\n\"compile_median\": %llu, \"lookup_and_rank_median\": %llu},\n"
1195 " \"full_scan_median_ns\": %llu, \"entries_probed_mean\": %.2f, \"entries_probed_max\": %llu,\n"
1196 " \"bytes_into_cognition\": {\"max_per_query\": %llu, \"mean_per_query\": %.1f, "
1197 "\"need_and_tradeoffs_out\": %llu},\n"
1198 " \"description_bytes_read_by_queries\": %llu,\n"
1199 " \"catalog_description_bytes\": %llu, \"catalog_bytes\": %llu, \"build_ms\": %.1f,\n"
1200 " \"baseline_named_operation\": {\"description_bytes_per_query\": %llu, \"feasible_share\": %.4f}}%s\n",
1201 r->catalog, r->n_ops, r->queries, r->with_candidates, r->no_op,
1202 (unsigned long long)r->hit, (unsigned long long)r->expected,
1203 r->expected ? (double)r->hit / (double)r->expected : 1.0,
1204 (unsigned long long)r->returned_ok, (unsigned long long)r->returned,
1205 r->returned ? (double)r->returned_ok / (double)r->returned : 1.0, r->top1_equal,
1206 r->with_candidates, r->with_candidates ? (double)r->top1_equal / r->with_candidates : 1.0,
1207 r->list_mismatch, r->feasible_count_mismatch, r->front_mismatch,
1208 (unsigned long long)r->q_median_ns, (unsigned long long)r->q_p99_ns,
1209 (unsigned long long)r->q_max_ns, (unsigned long long)r->hot_median_ns,
1210 (unsigned long long)r->compile_median_ns, (unsigned long long)r->lookup_median_ns,
1211 (unsigned long long)r->oracle_median_ns, r->probed_mean, (unsigned long long)r->probed_max,
1212 (unsigned long long)r->bytes_max,
1213 (r->queries - r->no_op) ? (double)r->bytes_sum / (r->queries - r->no_op) : 0.0,
1214 (unsigned long long)r->need_bytes, (unsigned long long)r->desc_bytes_read,
1215 (unsigned long long)r->desc_bytes, (unsigned long long)r->catalog_bytes, r->build_ms,
1216 (unsigned long long)r->naive_named_desc_bytes, r->naive_named_precision,
1217 i + 1 < N_SIZES ? "," : "");
1218 }
1219 const SizeRow *a = &R.row[0], *z = &R.row[N_SIZES - 1];
1220 fprintf(fp,
1221 " ],\n"
1222 " \"scaling\": {\"catalog_growth\": %.0f, \"query_median_growth\": %.3f, "
1223 "\"full_scan_median_growth\": %.1f,\n"
1224 " \"bytes_into_cognition_bound\": %zu, \"bytes_into_cognition_max_by_size\": [%llu, %llu, %llu, %llu],\n"
1225 " \"criterion\": \"catalog x1000: query median at most x10, full scan more than x100, "
1226 "bytes into cognition under the same bound (header + K records) at every size\", \"met\": %s},\n"
1227 " \"tradeoffs\": {\"distinct_winners_from_two_orders\": %u, \"single_relevance_score\": false},\n"
1228 " \"determinism\": {\"runs\": %u, \"mismatches\": %u, \"registration_orders\": 2},\n"
1229 " \"authority\": {\"held_validated_by_native_view\": %u, \"revoked_reported_not_held\": %u,\n"
1230 " \"mints_during_query\": %llu, \"query_has_admin_handle\": false},\n"
1231 " \"action_graph\": {\"winner_bound_and_ran\": %u, \"missing_authority_reported\": %u},\n"
1232 " \"gates\": {\n"
1233 " \"OMEGA_CAPABILITY_QUERY_PASS\": \"%s\",\n"
1234 " \"not_claimed\": [\"live MCP sessions (the MCP registry is an in-process catalog)\", "
1235 "\"Fabric over a network (advertisements and leases are in-process)\", "
1236 "\"lexical, semantic or model-based retrieval (operations are matched by id, alias and specialization)\", "
1237 "\"the query as a resident reaction (it is a host function call)\", "
1238 "\"learned confidence (confidence and reliability are catalog values)\", "
1239 "\"graphics-processor execution\"]\n"
1240 " }\n"
1241 "}\n",
1242 (double)z->catalog / a->catalog, (double)z->q_median_ns / (double)(a->q_median_ns ? a->q_median_ns : 1),
1243 (double)z->oracle_median_ns / (double)(a->oracle_median_ns ? a->oracle_median_ns : 1),
1244 offsetof(CqResult, cand) + CQ_MAX_K * sizeof(CqCandidate),
1245 (unsigned long long)R.row[0].bytes_max, (unsigned long long)R.row[1].bytes_max,
1246 (unsigned long long)R.row[2].bytes_max, (unsigned long long)R.row[3].bytes_max, scale ? "true" : "false",
1247 R.tradeoff_distinct_winners, R.det_runs, R.det_mismatch, R.held_validated, R.revoked_seen,
1248 (unsigned long long)R.mints_during_query, R.seam_skill_ran, R.graph_missing_reported,
1249 pass ? "PASS" : "FAIL");
1250 fclose(fp);
1251 printf("receipt: %s\n", path);
1252}
1253
1254int main(void) {
1255 signal(SIGPIPE, SIG_IGN);
1256 place();
1257 t_resolution();
1258 t_constraints();
1259 t_tradeoffs();
1260 t_determinism();
1261 t_authority_and_graph();
1262 printf("[*] scaling against the full-scan oracle\n");
1263 for (uint32_t i = 0; i < N_SIZES; i++) t_scale(i);
1264 CHECK(scaling_ok(), "scaling criterion");
1265 printf("checks %d failures %d\n", g_checks, g_fail);
1266 write_receipt();
1267 return g_fail ? 1 : 0;
1268}