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

563 lines · 25570 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 * M20 canonical Capability Graph and Skill Router.
19 *
20 * Behaviour added in M20 only: register / update / availability / withdraw
21 * on keyed entries, canonical machine identity behind the graph's indexes,
22 * query by required capability, authority filtering through the native
23 * authority view, deterministic routing, Skill -> capabilities, no-candidate,
24 * the wire form between two catalogs, and one real action-graph run whose
25 * skill node was chosen by capability, not by implementation.
26 */
27#include "runtime/aien_machine_id.h"
28#include "runtime/aienos_cap.h"
29#include "runtime/rx_capq.h"
30#include "runtime/rx_graph.h"
31#include "runtime/rx_skillroute.h"
32#include "runtime/rx_world.h"
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38static int g_checks, g_fail;
39
40#define CHECK(cond, ...) do { \
41 g_checks++; \
42 if (!(cond)) { \
43 g_fail++; \
44 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
45 fprintf(stderr, __VA_ARGS__); \
46 fputc('\n', stderr); \
47 } \
48 } while (0)
49
50#define NOW_US 1000000ull
51enum { OP_SCALE = 1, OP_SCALE_FAST = 2, OP_SUMMARIZE = 3, OP_LOCAL_ONLY = 4, ALIAS_SCALE = 501 };
52enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 61 };
53#define RES_CELL 0xA000001ull
54#define RES_RUN 0xA000002ull
55#define RES_HELD 0xB000001ull
56#define RES_ASK 0xB000002ull
57
58static AienMachineId mid(uint8_t seed) {
59 AienMachineId m;
60 uint8_t root[8] = { 'n', 'o', 'd', 'e', seed, 0, 0, 0 };
61 aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, root, sizeof root, &m);
62 return m;
63}
64
65/* The shared operation catalog both nodes load. */
66static void ontology(CqCatalog *c) {
67 uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC);
68 CHECK(cq_op_define(c, OP_SCALE, 0, all) == CQ_OK, "op scale");
69 CHECK(cq_op_define(c, OP_SCALE_FAST, OP_SCALE, all) == CQ_OK, "op scale.fast");
70 CHECK(cq_op_define(c, OP_SUMMARIZE, 0, all) == CQ_OK, "op summarize");
71 CHECK(cq_op_define(c, OP_LOCAL_ONLY, 0, CQ_SRC(CQ_SRC_SKILL)) == CQ_OK, "op local");
72 CHECK(cq_alias(c, ALIAS_SCALE, OP_SCALE) == CQ_OK, "alias");
73}
74
75static CqEntry provide(uint32_t cap, uint32_t op, uint32_t real, uint64_t cost) {
76 CqEntry e;
77 memset(&e, 0, sizeof e);
78 e.capability_id = cap;
79 e.realization_id = real;
80 e.op = op;
81 e.effects = CQ_FX_PURE;
82 e.in_types = 0x1;
83 e.out_types = 0x2;
84 e.confidence_ppm = 900000;
85 e.reliability_ppm = 990000;
86 e.evidence_level = CQ_EV_MEASURED;
87 e.evidence_ref = 0xE000u + cap;
88 e.cost = cost;
89 e.latency_us = 100;
90 e.energy_uj = 100;
91 e.live = CQ_LIVE_AVAILABLE;
92 e.generation = 1;
93 return e;
94}
95
96static SrRequirement requirement(uint32_t op) {
97 SrRequirement q;
98 memset(&q, 0, sizeof q);
99 q.need.semantic_operation = op;
100 q.need.accepted_input_types = ~0ull;
101 q.need.effect_class = 0x1FF;
102 q.need.authority_ceiling.resource_hi = UINT64_MAX;
103 q.need.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT;
104 q.t.n_order = 2;
105 q.t.order[0] = CQ_DIM_COST;
106 q.t.order[1] = CQ_DIM_LOCALITY;
107 q.t.k = CQ_MAX_K;
108 return q;
109}
110
111static uint64_t sk_scale(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
112 (void)attempt;
113 *failed = 0;
114 return (n ? in[0] : 0) * 3 + 1;
115}
116static uint64_t sk_other(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
117 (void)attempt;
118 *failed = 0;
119 return (n ? in[0] : 0) + 1000;
120}
121
122static AgSkillTable g_skills;
123
124static void skills_init(void) {
125 memset(&g_skills, 0, sizeof g_skills);
126 g_skills.n = 2;
127 g_skills.skill[0] = (AgSkill){ 7, sk_scale, { 0x07 } };
128 g_skills.skill[1] = (AgSkill){ 9, sk_other, { 0x09 } };
129}
130
131static SrSkill skill(uint32_t id, uint32_t version, uint8_t digest0, uint32_t machine) {
132 SrSkill s;
133 memset(&s, 0, sizeof s);
134 s.skill_id = id;
135 s.version = version;
136 s.digest[0] = digest0;
137 s.machine = machine;
138 return s;
139}
140
141/* ---- register / update / availability / withdraw, machine identity ---- */
142
143static void t_lifecycle(void) {
144 printf("[*] register, update in place, availability, withdraw, canonical machines\n");
145 AienMachineId slots[8], a = mid(1), b = mid(2);
146 AienMachineIndex ix;
147 aien_mid_index_init(&ix, slots, 8);
148 CqCatalog c;
149 CHECK(cq_catalog_init_canonical(&c, &ix, &a, 8, 8) == CQ_OK, "canonical catalog");
150 AienMachineId got;
151 CHECK(cq_machine_identity(&c, c.self_machine, &got) == CQ_OK && aien_mid_equal(&got, &a),
152 "self index resolves to the canonical identity");
153 uint32_t bi = 0;
154 CHECK(cq_machine_advertise_id(&c, &b, NOW_US + 1000, &bi) == CQ_OK && bi != c.self_machine &&
155 cq_machine_index(&c, &b) == bi, "remote machine bound by identity");
156 ontology(&c);
157
158 CqEntry e = provide(10, OP_SCALE, 100, 50);
159 e.source = CQ_SRC_GRAPH;
160 e.machine_id = c.self_machine;
161 CHECK(cq_register(&c, &e, "x", 1) == CQ_OK && c.n == 1, "register");
162 CHECK(cq_catalog_build(&c) == CQ_OK, "build");
163 SrRouter r = { &c, &g_skills };
164 SrRequirement q = requirement(ALIAS_SCALE);
165 CqPlan p;
166 CqResult res;
167 cq_compile(&c, &q.need, &p);
168 CHECK(cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, NULL) == CQ_OK && res.n == 1 &&
169 res.cand[0].expected_cost == 50, "query by alias finds the capability");
170
171 e.cost = 20;
172 e.generation = 2;
173 CHECK(cq_register(&c, &e, NULL, 0) == CQ_OK && c.n == 1 && c.built, "update in place keeps the index");
174 CqKey k = cq_key_of(&e);
175 CHECK(cq_lookup(&c, &k)->desc_len == 1, "update without text keeps the description");
176 cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, NULL);
177 CHECK(res.n == 1 && res.cand[0].expected_cost == 20, "the update is what the query sees");
178
179 CqStats st;
180 CHECK(cq_set_availability(&c, &k, CQ_LIVE_UNAVAILABLE) == CQ_OK && c.built, "unavailable");
181 cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, &st);
182 CHECK(res.n == 0 && st.rejected_unavailable == 1, "an unavailable provider is not returned");
183 CHECK(cq_set_availability(&c, &k, CQ_LIVE_AVAILABLE) == CQ_OK, "available again");
184 cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, NULL);
185 CHECK(res.n == 1, "back");
186 CHECK(cq_withdraw(&c, &k) == CQ_OK, "withdraw");
187 cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, &st);
188 CHECK(res.n == 0 && st.rejected_dead == 1, "a withdrawn provider is not returned");
189 CHECK(cq_set_availability(&c, &k, CQ_LIVE_AVAILABLE) == CQ_E_WITHDRAWN,
190 "availability cannot revive a withdrawal");
191 CqKey nk = { 99, 1, c.self_machine, 0 };
192 CHECK(cq_withdraw(&c, &nk) == CQ_E_NOT_FOUND, "unknown key");
193 e.generation = 3;
194 CHECK(cq_register(&c, &e, NULL, 0) == CQ_OK && c.n == 1, "registering again revives it");
195 cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, NULL);
196 CHECK(res.n == 1, "revived");
197 e.op = OP_SUMMARIZE;
198 CHECK(cq_register(&c, &e, NULL, 0) == CQ_OK && !c.built, "moving operations needs a rebuild");
199 CHECK(cq_query(&c, &p, &q.need, &q.t, NULL, NOW_US, &res, NULL) == CQ_E_UNBUILT, "unbuilt refused");
200
201 SrRoute out;
202 CHECK(sr_route(&r, &q, NULL, NOW_US, &out) == SR_E_QUERY, "router reports the unbuilt graph");
203 cq_catalog_free(&c);
204
205 CqCatalog plain;
206 CHECK(cq_catalog_init(&plain, 1, 4, 4) == CQ_OK, "index-only catalog");
207 CHECK(cq_machine_identity(&plain, 1, &got) == CQ_E_NO_IDENTITY &&
208 cq_machine_advertise_id(&plain, &b, 1, NULL) == CQ_E_NO_IDENTITY,
209 "no identity table: no canonical identity");
210 cq_catalog_free(&plain);
211}
212
213/* ---- Skill -> capabilities, routing, authority, no-candidate, determinism ---- */
214
215static AienosCapAdmin *g_admin;
216
217static RxCapRef mint(uint32_t subject, uint64_t resource, uint32_t rights) {
218 AienosCapRef office;
219 aienos_cap_office(g_admin, &office);
220 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
221 AienosCapRef ref = { UINT32_MAX, 0 };
222 if (aienos_cap_mint(g_admin, &m, &ref) != 0) ref = (AienosCapRef){ UINT32_MAX, 0 };
223 return (RxCapRef){ ref.cap_id, ref.generation };
224}
225
226/* Node A's graph: skill 7 (scale, digest 0x07, matches the table), skill 8
227 * (scale, cheaper, but this machine has no procedure 8), skill 9 (scale,
228 * cheapest, advertised digest differs from the executable one), a native
229 * (non-Skill) provider, and machine B's skill 11 (scale, remote). */
230static void graph_a(CqCatalog *c, AienMachineIndex *ix, AienMachineId *slots, int reverse) {
231 AienMachineId a = mid(1), b = mid(2);
232 aien_mid_index_init(ix, slots, 8);
233 CHECK(cq_catalog_init_canonical(c, ix, &a, 8, 8) == CQ_OK, "catalog A");
234 uint32_t bi = 0;
235 CHECK(cq_machine_advertise_id(c, &b, NOW_US + 1000000, &bi) == CQ_OK, "B leased");
236 ontology(c);
237 SrSkill s7 = skill(7, 1, 0x07, c->self_machine), s8 = skill(8, 1, 0x08, c->self_machine),
238 s9 = skill(9, 1, 0x99, c->self_machine), s11 = skill(11, 4, 0x11, bi);
239 CqEntry p7[2] = { provide(1, OP_SCALE, 70, 40), provide(2, OP_SUMMARIZE, 71, 40) };
240 CqEntry p7h = provide(3, OP_LOCAL_ONLY, 72, 40);
241 p7h.auth_resource = RES_HELD;
242 p7h.auth_rights = RX_RIGHT_READ;
243 CqEntry p8 = provide(4, OP_SCALE, 80, 30);
244 CqEntry p9 = provide(5, OP_SCALE, 90, 10);
245 CqEntry p9a = provide(6, OP_LOCAL_ONLY, 91, 10);
246 p9a.auth_resource = RES_ASK;
247 p9a.auth_rights = RX_RIGHT_READ;
248 CqEntry p11 = provide(7, OP_SCALE_FAST, 110, 5);
249 CqEntry nat = provide(8, OP_SCALE, 120, 1);
250 nat.source = CQ_SRC_GRAPH;
251 nat.machine_id = c->self_machine;
252 for (int pass = 0; pass < 2; pass++) {
253 if ((pass == 0) != (reverse == 0)) {
254 CHECK(sr_register_skill(c, &s7, p7, 2) == SR_OK && sr_register_skill(c, &s7, &p7h, 1) == SR_OK,
255 "skill 7");
256 CHECK(sr_register_skill(c, &s8, &p8, 1) == SR_OK, "skill 8");
257 } else {
258 CHECK(cq_register(c, &nat, NULL, 0) == CQ_OK, "native provider");
259 CHECK(sr_register_skill(c, &s11, &p11, 1) == SR_OK, "remote skill 11");
260 CHECK(sr_register_skill(c, &s9, &p9, 1) == SR_OK && sr_register_skill(c, &s9, &p9a, 1) == SR_OK,
261 "skill 9");
262 }
263 }
264 CHECK(cq_catalog_build(c) == CQ_OK, "build A");
265}
266
267static void t_routing(void) {
268 printf("[*] Skill -> capabilities; requirement -> admissible Skill target; authority; determinism\n");
269 AienosCapView *view;
270 CHECK(aienos_cap_start(&g_admin, &view) == 0, "authority");
271 RxWorld w;
272 CHECK(rx_world_init_native(&w, view, 2, 1u << 14) == RX_OK, "world");
273 w.external_subject = SUBJ_EXTERNAL;
274 AgCapTable caps;
275 memset(&caps, 0, sizeof caps);
276 caps.subject = SUBJ_PLAN;
277 caps.cap[0].ref = mint(SUBJ_PLAN, RES_HELD, RX_RIGHT_READ);
278 caps.cap[0].resource = RES_HELD;
279 caps.cap[0].rights = RX_RIGHT_READ;
280 caps.cap[1].ref = (RxCapRef){ 4000, 1 }; /* listed, never minted */
281 caps.cap[1].resource = RES_ASK;
282 caps.cap[1].rights = RX_RIGHT_READ;
283 caps.n = 2;
284 CqHeld held = { &w, &caps };
285
286 AienMachineId slots[8], slots2[8], b = mid(2);
287 AienMachineIndex ix, ix2;
288 CqCatalog c, c2;
289 graph_a(&c, &ix, slots, 0);
290 graph_a(&c2, &ix2, slots2, 1);
291 SrRouter r = { &c, &g_skills }, r2 = { &c2, &g_skills };
292
293 CqKey keys[8];
294 CHECK(sr_skill_capabilities(&c, 7, 1, keys, 8) == 3, "skill 7 provides three capabilities");
295 CHECK(sr_skill_capabilities(&c, 7, 2, keys, 8) == 0, "no such version");
296 CHECK(sr_skill_capabilities(&c, 11, 4, keys, 8) == 1 && keys[0].machine_id == cq_machine_index(&c, &b),
297 "the remote Skill's capability is on machine B");
298
299 /* Fabric allowed: the cheapest admissible provider is B's skill 11
300 * (scale.fast specializes scale). Skill 9 is cheaper locally but its
301 * advertised digest is not the procedure this machine runs; skill 8 has
302 * no procedure here; the native provider is not a Skill. */
303 SrRequirement q = requirement(OP_SCALE);
304 SrRoute out, out2;
305 CHECK(sr_route(&r, &q, &held, NOW_US, &out) == SR_E_REMOTE && out.chosen.capability_id == 7 &&
306 out.target_known && aien_mid_equal(&out.target, &b) && out.skill_version == 4 &&
307 out.skill_digest[0] == 0x11, "remote winner returned with B's canonical identity");
308 CHECK(out.rejected.not_a_skill == 1 && out.rejected.not_executable == 1 &&
309 out.rejected.digest_mismatch == 1 && out.n_admissible == 2,
310 "rejections counted: native %llu, missing %llu, digest %llu, admissible %u",
311 (unsigned long long)out.rejected.not_a_skill, (unsigned long long)out.rejected.not_executable,
312 (unsigned long long)out.rejected.digest_mismatch, out.n_admissible);
313
314 /* Local only: skill 7. Same answer from the other registration order. */
315 q.local_only = 1;
316 CHECK(sr_route(&r, &q, &held, NOW_US, &out) == SR_OK && out.chosen.skill_id == 7 &&
317 out.chosen.capability_id == 1 && !out.remote, "local only: skill 7");
318 CHECK(sr_route(&r2, &q, &held, NOW_US, &out2) == SR_OK && out2.chosen.skill_id == 7 &&
319 !memcmp(&out.chosen, &out2.chosen, sizeof out.chosen) &&
320 !memcmp(out.plan_digest, out2.plan_digest, 32) && !memcmp(&out.target, &out2.target, sizeof out.target),
321 "registration order does not change the route");
322 for (int i = 0; i < 50; i++) {
323 sr_route(&r, &q, &held, NOW_US, &out2);
324 if (memcmp(&out.chosen, &out2.chosen, sizeof out.chosen)) { CHECK(0, "route changed on run %d", i); break; }
325 }
326
327 /* Authority. OP_LOCAL_ONLY: skill 9's provider (cheaper) needs RES_ASK,
328 * whose table entry is forged; skill 7's needs RES_HELD, which is held. */
329 SrRoute oa;
330 g_skills.skill[1].identity[0] = 0x99; /* make skill 9 executable as advertised */
331 SrRequirement qa = requirement(OP_LOCAL_ONLY);
332 CHECK(sr_route(&r, &qa, &held, NOW_US, &oa) == SR_OK && oa.chosen.skill_id == 9 &&
333 oa.chosen.required_authority.held == 0, "without the filter: the cheaper, unheld one");
334 qa.t.require_held = 1;
335 CHECK(sr_route(&r, &qa, &held, NOW_US, &oa) == SR_OK && oa.chosen.skill_id == 7 &&
336 oa.chosen.required_authority.held == 1 && oa.stats.rejected_authority == 1,
337 "require_held: the forged authority is refused by the view, skill 7 wins");
338 qa.need.authority_ceiling.rights = 0;
339 CHECK(sr_route(&r, &qa, &held, NOW_US, &oa) == SR_E_NO_CANDIDATE,
340 "ceiling without rights: no candidate");
341 g_skills.skill[1].identity[0] = 0x09;
342
343 /* No candidate. */
344 SrRequirement qn = requirement(OP_SUMMARIZE);
345 qn.need.required_output_types = 0x4;
346 CHECK(sr_route(&r, &qn, &held, NOW_US, &out) == SR_E_NO_CANDIDATE && out.n_admissible == 0,
347 "nothing produces that type");
348 SrRequirement qu = requirement(777);
349 CHECK(sr_route(&r, &qu, &held, NOW_US, &out) == SR_E_QUERY && out.query_verdict == CQ_E_NO_OP,
350 "unknown capability");
351 SrRequirement ql = requirement(OP_SCALE);
352 CqKey k7 = { 1, 70, c.self_machine, 7 };
353 CHECK(cq_withdraw(&c, &k7) == CQ_OK, "withdraw skill 7's scale");
354 ql.local_only = 1;
355 CHECK(sr_route(&r, &ql, &held, NOW_US, &out) == SR_E_NO_CANDIDATE &&
356 out.rejected.digest_mismatch == 1 && out.rejected.not_executable == 1,
357 "only inadmissible local providers left: no candidate");
358 CHECK(sr_skill_capabilities(&c, 7, 1, keys, 8) == 2, "skill 7 now provides two");
359 ql.local_only = 0;
360 CHECK(sr_route(&r, &ql, &held, NOW_US, &out) == SR_E_REMOTE && out.chosen.skill_id == 11,
361 "B still serves it while its lease holds");
362 CHECK(sr_route(&r, &ql, &held, NOW_US + 2000000, &out) == SR_E_NO_CANDIDATE &&
363 out.stats.rejected_dead >= 1, "after B's lease ends nothing remote either");
364
365 cq_catalog_free(&c);
366 cq_catalog_free(&c2);
367 rx_world_wait_quiescent(&w, 30000);
368 rx_world_destroy(&w);
369 aienos_cap_stop(g_admin, view);
370}
371
372/* ---- wire form between two catalogs ---- */
373
374static void t_wire(void) {
375 printf("[*] wire form: B advertises to A by canonical identity\n");
376 AienMachineId sa[8], sb[8], a = mid(1), b = mid(2);
377 AienMachineIndex ia, ib;
378 aien_mid_index_init(&ia, sa, 8);
379 aien_mid_index_init(&ib, sb, 8);
380 /* Bind something first on A so B's index differs between the two catalogs. */
381 AienMachineId other = mid(9);
382 aien_mid_index_bind(&ia, &other);
383 CqCatalog ca, cb;
384 CHECK(cq_catalog_init_canonical(&ca, &ia, &a, 8, 8) == CQ_OK, "A");
385 CHECK(cq_catalog_init_canonical(&cb, &ib, &b, 8, 8) == CQ_OK, "B");
386 ontology(&ca);
387 ontology(&cb);
388 uint8_t da[32], db[32];
389 cq_ontology_digest(&ca, da);
390 cq_ontology_digest(&cb, db);
391 CHECK(!memcmp(da, db, 32), "same operation catalog, same digest");
392
393 SrSkill s = skill(11, 4, 0x11, cb.self_machine);
394 CqEntry p = provide(7, OP_SCALE_FAST, 110, 5);
395 p.auth_resource = 0x77;
396 p.auth_rights = RX_RIGHT_READ;
397 p.generation = 5;
398 CHECK(sr_register_skill(&cb, &s, &p, 1) == SR_OK, "B registers its skill");
399 CqKey kb = { 7, 110, cb.self_machine, 11 };
400 const CqEntry *eb = cq_lookup(&cb, &kb);
401 uint8_t rec[CQ_WIRE_BYTES];
402 CHECK(eb && cq_wire_encode(&cb, eb, CQ_WIRE_ADVERTISE, rec) == CQ_OK, "B encodes");
403
404 CqKey ka;
405 CHECK(cq_wire_apply(&ca, rec, sizeof rec, &ka) == CQ_OK, "A ingests");
406 const CqEntry *ea = cq_lookup(&ca, &ka);
407 AienMachineId got;
408 CHECK(ea && ea->source == CQ_SRC_FABRIC && ka.machine_id != cb.self_machine &&
409 cq_machine_identity(&ca, ka.machine_id, &got) == CQ_OK && aien_mid_equal(&got, &b),
410 "on A the entry is Fabric, on A's own index for B's identity");
411 CHECK(ea && ea->skill_id == 11 && ea->skill_version == 4 && ea->skill_digest[0] == 0x11 &&
412 ea->cost == 5 && ea->auth_resource == 0x77 && ea->auth_rights == RX_RIGHT_READ &&
413 ea->evidence_ref == eb->evidence_ref && ea->generation == 5 && ea->op == OP_SCALE_FAST,
414 "fields survive the trip");
415 uint8_t rec2[CQ_WIRE_BYTES];
416 CHECK(ea && cq_wire_encode(&ca, ea, CQ_WIRE_ADVERTISE, rec2) == CQ_E_WIRE,
417 "A will not re-advertise B's capability");
418 CHECK(cq_wire_apply(&cb, rec, sizeof rec, NULL) == CQ_E_WIRE, "B refuses a record naming itself");
419 CHECK(cq_wire_apply(&ca, rec, sizeof rec, NULL) == CQ_E_STALE, "replayed generation is stale");
420
421 uint8_t bad[CQ_WIRE_BYTES + 1];
422 memcpy(bad, rec, sizeof rec);
423 CHECK(cq_wire_apply(&ca, bad, CQ_WIRE_BYTES - 1, NULL) == CQ_E_WIRE, "short record");
424 CHECK(cq_wire_apply(&ca, bad, CQ_WIRE_BYTES + 1, NULL) == CQ_E_WIRE, "long record");
425 bad[130] ^= 1;
426 CHECK(cq_wire_apply(&ca, bad, CQ_WIRE_BYTES, NULL) == CQ_E_WIRE, "flipped bit fails the check");
427 memcpy(bad, rec, sizeof rec);
428 bad[0] = 'X';
429 CHECK(cq_wire_apply(&ca, bad, CQ_WIRE_BYTES, NULL) == CQ_E_WIRE, "bad magic");
430
431 /* Unknown operation on the receiver. */
432 CqCatalog cc;
433 AienMachineId sc[4], cself = mid(3);
434 AienMachineIndex ic;
435 aien_mid_index_init(&ic, sc, 4);
436 CHECK(cq_catalog_init_canonical(&cc, &ic, &cself, 4, 4) == CQ_OK, "C");
437 CHECK(cq_op_define(&cc, 1, 0, CQ_SRC(CQ_SRC_FABRIC)) == CQ_OK, "C knows one op");
438 uint8_t dc[32];
439 cq_ontology_digest(&cc, dc);
440 CHECK(memcmp(dc, db, 32) != 0, "different catalog, different digest");
441 CHECK(cq_wire_apply(&cc, rec, sizeof rec, NULL) == CQ_E_NO_OP, "C does not know scale.fast");
442 cq_catalog_free(&cc);
443
444 /* Availability and withdrawal travel the same way. */
445 CHECK(cq_catalog_build(&ca) == CQ_OK, "A build");
446 CqEntry x = *eb;
447 x.generation = 6;
448 x.live = CQ_LIVE_UNAVAILABLE;
449 CHECK(cq_wire_encode(&cb, &x, CQ_WIRE_AVAILABILITY, rec) == CQ_OK &&
450 cq_wire_apply(&ca, rec, sizeof rec, NULL) == CQ_OK && cq_lookup(&ca, &ka)->live == CQ_LIVE_UNAVAILABLE &&
451 ca.built, "availability update, index kept");
452 x.generation = 7;
453 CHECK(cq_wire_encode(&cb, &x, CQ_WIRE_WITHDRAW, rec) == CQ_OK &&
454 cq_wire_apply(&ca, rec, sizeof rec, NULL) == CQ_OK && cq_lookup(&ca, &ka)->live == CQ_LIVE_WITHDRAWN,
455 "withdrawal");
456 x.generation = 8;
457 x.live = CQ_LIVE_AVAILABLE;
458 CHECK(cq_wire_encode(&cb, &x, CQ_WIRE_AVAILABILITY, rec) == CQ_OK &&
459 cq_wire_apply(&ca, rec, sizeof rec, NULL) == CQ_E_WITHDRAWN, "availability cannot revive");
460 CHECK(cq_wire_encode(&cb, &x, CQ_WIRE_ADVERTISE, rec) == CQ_OK &&
461 cq_wire_apply(&ca, rec, sizeof rec, NULL) == CQ_OK && cq_lookup(&ca, &ka)->live == CQ_LIVE_AVAILABLE,
462 "a new advertisement does");
463
464 CqCatalog plain;
465 CHECK(cq_catalog_init(&plain, 1, 4, 4) == CQ_OK, "index-only");
466 CHECK(cq_wire_apply(&plain, rec, sizeof rec, NULL) == CQ_E_NO_IDENTITY, "index-only catalog cannot ingest");
467 cq_catalog_free(&plain);
468 cq_catalog_free(&ca);
469 cq_catalog_free(&cb);
470}
471
472/* ---- one real runtime route: an action graph that names a capability ---- */
473
474static void t_runtime_route(void) {
475 printf("[*] runtime: an action graph's skill node is routed by capability and runs on the World\n");
476 AienosCapView *view;
477 CHECK(aienos_cap_start(&g_admin, &view) == 0, "authority");
478 RxWorld w;
479 CHECK(rx_world_init_native(&w, view, 2, 1u << 14) == RX_OK, "world");
480 w.external_subject = SUBJ_EXTERNAL;
481 AgCapTable caps;
482 memset(&caps, 0, sizeof caps);
483 caps.subject = SUBJ_PLAN;
484 caps.cap[0].ref = mint(SUBJ_PLAN, RES_HELD, RX_RIGHT_READ);
485 caps.cap[0].resource = RES_HELD;
486 caps.cap[0].rights = RX_RIGHT_READ;
487 caps.n = 1;
488 CqHeld held = { &w, &caps };
489
490 AienMachineId slots[8];
491 AienMachineIndex ix;
492 CqCatalog c;
493 graph_a(&c, &ix, slots, 0);
494 SrRouter r = { &c, &g_skills };
495
496 /* The template states no implementation: node s is AG_SKILL op 0. */
497 static AgGraph tmpl;
498 rx_graph_init(&tmpl, 0);
499 int k = rx_graph_node(&tmpl, AG_CONST, AG_T_U64);
500 tmpl.nodes[k].imm = 14;
501 int s = rx_graph_node(&tmpl, AG_SKILL, AG_T_U64);
502 int v = rx_graph_node(&tmpl, AG_VERIFY, AG_T_VERDICT);
503 tmpl.nodes[v].imm = 0;
504 tmpl.nodes[v].imm2 = 1000;
505 rx_graph_data(&tmpl, (uint32_t)k, (uint32_t)s, 0, AG_EDGE_DATA);
506 rx_graph_data(&tmpl, (uint32_t)s, (uint32_t)v, 0, AG_EDGE_DATA);
507 rx_graph_success(&tmpl, (uint32_t)v, AG_OK);
508 CHECK(tmpl.nodes[s].op == 0, "unbound skill node");
509
510 SrRequirement q = requirement(ALIAS_SCALE); /* "I need scale", by alias */
511 q.local_only = 1;
512 q.t.require_held = 1;
513 SrRoute route;
514 CHECK(sr_bind_node(&r, &tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_OK &&
515 tmpl.nodes[s].op == 7, "the router bound the node to skill 7 (route %d)", route.verdict);
516
517 AgLibrary lib = { 1, { { 1, &tmpl } } };
518 AgGoal goal = { 1, 0, { { 0, 0 } } };
519 AgConstraints cons;
520 memset(&cons, 0, sizeof cons);
521 cons.max_effects = UINT32_MAX;
522 cons.budget.slots = RX_MAX_REACTIONS;
523 cons.budget.memory_bytes = UINT64_MAX;
524 cons.budget.energy_budget = UINT64_MAX;
525 cons.budget.offered_locality = UINT32_MAX;
526 cons.budget.offered_accel = UINT32_MAX;
527 cons.budget.compute_mask = UINT32_MAX;
528 static AgGraph g;
529 AgReport rep;
530 int rc = rx_graph_compile(&goal, &w, &caps, &cons, &lib, 1, &g, &rep);
531 CHECK(rc == AG_OK_READY && rep.n_missing == 0, "compile %d, missing %u", rc, rep.n_missing);
532 RxCapRef cell = mint(SUBJ_PLAN, RES_CELL, RX_RIGHT_READ | RX_RIGHT_WRITE);
533 RxCapRef runc = mint(SUBJ_PLAN, RES_RUN, RX_RIGHT_READ);
534 RxCapRef ext = mint(SUBJ_EXTERNAL, RES_RUN, RX_RIGHT_WRITE);
535 static AgLowered L;
536 CHECK(rx_graph_lower(&L, &w, &g, &g_skills, &caps, RES_CELL, cell, RES_RUN, runc, NULL) == 0, "lower");
537 CHECK(rx_graph_start(&L, ext, 1) > 0, "start");
538 CHECK(rx_world_wait_quiescent(&w, 30000) == RX_OK, "settle");
539 AgResult res;
540 rx_graph_collect(&L, 1, &res);
541 int sn = -1;
542 for (uint32_t i = 0; i < g.n_nodes; i++)
543 if (g.nodes[i].alive && g.nodes[i].kind == AG_SKILL) sn = (int)i;
544 CHECK(res.outcome == AG_RUN_SUCCESS && sn >= 0 && res.value[sn] == 14 * 3 + 1,
545 "the World ran the routed Skill (outcome %d, value %llu)", res.outcome,
546 sn >= 0 ? (unsigned long long)res.value[sn] : 0ull);
547
548 cq_catalog_free(&c);
549 rx_world_wait_quiescent(&w, 30000);
550 rx_world_destroy(&w);
551 aienos_cap_stop(g_admin, view);
552}
553
554int main(void) {
555 skills_init();
556 t_lifecycle();
557 t_routing();
558 t_wire();
559 t_runtime_route();
560 printf("checks %d failures %d\n", g_checks, g_fail);
561 if (g_fail == 0) printf("M20_CAPABILITY_GRAPH_PASS\n");
562 return g_fail ? 1 : 0;
563}