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

513 lines · 23920 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 WP-D: the Skill Router end to end and failing closed.
19 *
20 * One real requirement is routed from a CqNeed through cq_compile and the
21 * canonical Capability Graph to a Skill, bound into an action-graph node
22 * (skill id, version, digest, generation proven) and run on the World. Then
23 * each failure mode must fail closed with its specific verdict and leave the
24 * node unbound:
25 * (a) provider withdrawn (b) stale generation (held route, upsert)
26 * (c) provider unavailable (d) machine mismatch / unknown machine
27 * (e) skill digest / version pin (f) authority not held
28 * plus sr_route_alternatives (>= 2 ranked, out[0] == sr_route, dominated
29 * included). The no-mint symbol check on rx_skillroute.o runs in the build.
30 */
31#include "runtime/aien_machine_id.h"
32#include "runtime/aienos_cap.h"
33#include "runtime/rx_capq.h"
34#include "runtime/rx_graph.h"
35#include "runtime/rx_skillroute.h"
36#include "runtime/rx_world.h"
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42static int g_checks, g_fail;
43
44#define CHECK(cond, ...) do { \
45 g_checks++; \
46 if (!(cond)) { \
47 g_fail++; \
48 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
49 fprintf(stderr, __VA_ARGS__); \
50 fputc('\n', stderr); \
51 } \
52 } while (0)
53
54#define NOW_US 1000000ull
55enum { OP_SCALE = 1, OP_SCALE_FAST = 2, OP_SUMMARIZE = 3, ALIAS_SCALE = 501 };
56enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 61 };
57enum { UNBOUND_MACHINE = 1000 }; /* a graph index with no canonical identity */
58#define RES_CELL 0xA000001ull
59#define RES_RUN 0xA000002ull
60#define RES_HELD 0xB000001ull
61
62static AienMachineId mid(uint8_t seed) {
63 AienMachineId m;
64 uint8_t root[8] = { 'n', 'o', 'd', 'e', seed, 0, 0, 0 };
65 aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, root, sizeof root, &m);
66 return m;
67}
68
69static uint64_t sk_scale(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
70 (void)attempt;
71 *failed = 0;
72 return (n ? in[0] : 0) * 3 + 1;
73}
74static uint64_t sk_other(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) {
75 (void)attempt;
76 *failed = 0;
77 return (n ? in[0] : 0) + 1000;
78}
79
80static AgSkillTable g_skills;
81
82static CqEntry provide(uint32_t cap, uint32_t op, uint32_t real, uint64_t cost, uint64_t lat) {
83 CqEntry e;
84 memset(&e, 0, sizeof e);
85 e.capability_id = cap;
86 e.realization_id = real;
87 e.op = op;
88 e.effects = CQ_FX_PURE;
89 e.in_types = 0x1;
90 e.out_types = 0x2;
91 e.confidence_ppm = 900000;
92 e.reliability_ppm = 990000;
93 e.evidence_level = CQ_EV_MEASURED;
94 e.evidence_ref = 0xE000u + cap;
95 e.cost = cost;
96 e.latency_us = lat;
97 e.energy_uj = 100;
98 e.auth_resource = RES_HELD;
99 e.auth_rights = RX_RIGHT_READ;
100 e.live = CQ_LIVE_AVAILABLE;
101 e.generation = 1;
102 return e;
103}
104
105static SrSkill skill(uint32_t id, uint32_t version, uint8_t digest0, uint32_t machine) {
106 SrSkill s;
107 memset(&s, 0, sizeof s);
108 s.skill_id = id;
109 s.version = version;
110 s.digest[0] = digest0;
111 s.machine = machine;
112 return s;
113}
114
115/* The graph: A (self) has skill 7 (scale, cost 10, latency 100, digest 0x07
116 * = the executable one) and skill 9 (scale, cost 20, latency 200, digest
117 * 0x09: dominated by 7 on both named dimensions). Machine B (leased) has
118 * skill 11 (scale.fast, cost 30). Machine C is leased and bound but provides
119 * nothing. Index UNBOUND_MACHINE is leased by index only (no identity) and
120 * provides skill 12 (summarize). */
121typedef struct {
122 AienMachineId slots[8];
123 AienMachineIndex ix;
124 CqCatalog c;
125 SrRouter r;
126 uint32_t b, cm;
127} Fx;
128
129static CqEntry p7(void) { return provide(1, OP_SCALE, 70, 10, 100); }
130
131static void fx_init(Fx *f) {
132 AienMachineId a = mid(1), b = mid(2), cm = mid(3);
133 aien_mid_index_init(&f->ix, f->slots, 8);
134 CHECK(cq_catalog_init_canonical(&f->c, &f->ix, &a, 8, 8) == CQ_OK, "catalog");
135 CHECK(cq_machine_advertise_id(&f->c, &b, NOW_US + 1000000, &f->b) == CQ_OK, "B leased");
136 CHECK(cq_machine_advertise_id(&f->c, &cm, NOW_US + 1000000, &f->cm) == CQ_OK, "C leased");
137 CHECK(cq_machine_advertise(&f->c, UNBOUND_MACHINE, NOW_US + 1000000) == CQ_OK, "index-only lease");
138 uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC);
139 CHECK(cq_op_define(&f->c, OP_SCALE, 0, all) == CQ_OK && cq_op_define(&f->c, OP_SCALE_FAST, OP_SCALE, all) == CQ_OK &&
140 cq_op_define(&f->c, OP_SUMMARIZE, 0, all) == CQ_OK && cq_alias(&f->c, ALIAS_SCALE, OP_SCALE) == CQ_OK,
141 "ontology");
142 SrSkill s7 = skill(7, 1, 0x07, f->c.self_machine), s9 = skill(9, 1, 0x09, f->c.self_machine),
143 s11 = skill(11, 4, 0x11, f->b), s12 = skill(12, 1, 0x12, UNBOUND_MACHINE);
144 CqEntry e7 = p7(), e9 = provide(2, OP_SCALE, 90, 20, 200), e11 = provide(3, OP_SCALE_FAST, 110, 30, 300),
145 e12 = provide(4, OP_SUMMARIZE, 120, 5, 50);
146 CHECK(sr_register_skill(&f->c, &s7, &e7, 1) == SR_OK && sr_register_skill(&f->c, &s9, &e9, 1) == SR_OK &&
147 sr_register_skill(&f->c, &s11, &e11, 1) == SR_OK && sr_register_skill(&f->c, &s12, &e12, 1) == SR_OK,
148 "skills registered");
149 CHECK(cq_catalog_build(&f->c) == CQ_OK, "build");
150 f->r = (SrRouter){ &f->c, &g_skills };
151}
152
153static SrRequirement requirement(uint32_t op) {
154 SrRequirement q;
155 memset(&q, 0, sizeof q);
156 q.need.semantic_operation = op;
157 q.need.accepted_input_types = ~0ull;
158 q.need.required_output_types = 0x2;
159 q.need.effect_class = CQ_FX_PURE;
160 q.need.authority_ceiling.resource_hi = UINT64_MAX;
161 q.need.authority_ceiling.rights = RX_RIGHT_READ;
162 q.t.n_order = 2;
163 q.t.order[0] = CQ_DIM_COST;
164 q.t.order[1] = CQ_DIM_LATENCY;
165 q.t.k = CQ_MAX_K;
166 q.t.require_held = 1;
167 q.local_only = 1;
168 q.need.locality_constraints.allowed = CQ_LOC_LOCAL; /* B's skill never enters the query */
169 return q;
170}
171
172/* An action-graph template with one unbound skill node. */
173static AgGraph g_tmpl;
174static int tmpl(void) {
175 rx_graph_init(&g_tmpl, 0);
176 int k = rx_graph_node(&g_tmpl, AG_CONST, AG_T_U64);
177 g_tmpl.nodes[k].imm = 14;
178 int s = rx_graph_node(&g_tmpl, AG_SKILL, AG_T_U64);
179 int v = rx_graph_node(&g_tmpl, AG_VERIFY, AG_T_VERDICT);
180 g_tmpl.nodes[v].imm = 0;
181 g_tmpl.nodes[v].imm2 = 1000;
182 rx_graph_data(&g_tmpl, (uint32_t)k, (uint32_t)s, 0, AG_EDGE_DATA);
183 rx_graph_data(&g_tmpl, (uint32_t)s, (uint32_t)v, 0, AG_EDGE_DATA);
184 rx_graph_success(&g_tmpl, (uint32_t)v, AG_OK);
185 return s;
186}
187static int unbound(int s) { return g_tmpl.nodes[s].op == 0 && g_tmpl.n_auth == 0; }
188
189static AienosCapAdmin *g_admin;
190static RxWorld g_w;
191static AgCapTable g_caps, g_caps_none;
192
193static RxCapRef mint(uint32_t subject, uint64_t resource, uint32_t rights) {
194 AienosCapRef office;
195 aienos_cap_office(g_admin, &office);
196 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
197 AienosCapRef ref = { UINT32_MAX, 0 };
198 if (aienos_cap_mint(g_admin, &m, &ref) != 0) ref = (AienosCapRef){ UINT32_MAX, 0 };
199 return (RxCapRef){ ref.cap_id, ref.generation };
200}
201
202/* ---- end to end ---- */
203
204static void t_end_to_end(void) {
205 printf("[*] requirement -> cq_compile -> providers -> Skill -> bound node -> runs on the World\n");
206 Fx f;
207 fx_init(&f);
208 CqHeld held = { &g_w, &g_caps };
209 SrRequirement q = requirement(ALIAS_SCALE);
210
211 CqPlan plan;
212 CHECK(cq_compile(&f.c, &q.need, &plan) == CQ_OK && plan.op == OP_SCALE && plan.n_ops == 2,
213 "need compiles to scale + scale.fast (op %u, n_ops %u)", plan.op, plan.n_ops);
214 CqResult res;
215 CqTradeoffs t = q.t;
216 CHECK(cq_query(&f.c, &plan, &q.need, &t, &held, NOW_US, &res, NULL) == CQ_OK && res.n_feasible == 2,
217 "graph has two local providers of the capability (%u)", res.n_feasible);
218
219 int s = tmpl();
220 SrRoute route;
221 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_OK, "bind (%d)",
222 route.verdict);
223 CHECK(g_tmpl.nodes[s].op == 7 && route.chosen.skill_id == 7 && route.skill_version == 1 &&
224 route.skill_digest[0] == 0x07 && memcmp(route.skill_digest, g_skills.skill[0].identity, 32) == 0 &&
225 route.generation == 1 && route.key.skill_id == 7 && route.key.capability_id == 1 &&
226 route.key.machine_id == f.c.self_machine && !route.remote && route.target_known,
227 "node bound to skill 7 v1, digest = executable identity, generation 1");
228 CHECK(g_tmpl.n_auth == 1 && g_tmpl.auth[0].node == (uint16_t)s && g_tmpl.auth[0].resource == RES_HELD &&
229 g_tmpl.auth[0].rights == RX_RIGHT_READ, "bound node declares the provider's authority");
230 CHECK(memcmp(route.plan_digest, plan.digest, 32) == 0, "route carries the compiled plan identity");
231 CHECK(sr_route_check(&f.r, &route, NOW_US) == SR_OK, "held route still valid");
232
233 AgLibrary lib = { 1, { { 1, &g_tmpl } } };
234 AgGoal goal = { 1, 0, { { 0, 0 } } };
235 AgConstraints cons;
236 memset(&cons, 0, sizeof cons);
237 cons.max_effects = UINT32_MAX;
238 cons.budget.slots = RX_MAX_REACTIONS;
239 cons.budget.memory_bytes = UINT64_MAX;
240 cons.budget.energy_budget = UINT64_MAX;
241 cons.budget.offered_locality = UINT32_MAX;
242 cons.budget.offered_accel = UINT32_MAX;
243 cons.budget.compute_mask = UINT32_MAX;
244 static AgGraph g;
245 AgReport rep;
246 int rc = rx_graph_compile(&goal, &g_w, &g_caps, &cons, &lib, 1, &g, &rep);
247 CHECK(rc == AG_OK_READY && rep.n_missing == 0, "compile %d, missing %u", rc, rep.n_missing);
248 RxCapRef cell = mint(SUBJ_PLAN, RES_CELL, RX_RIGHT_READ | RX_RIGHT_WRITE);
249 RxCapRef runc = mint(SUBJ_PLAN, RES_RUN, RX_RIGHT_READ);
250 RxCapRef ext = mint(SUBJ_EXTERNAL, RES_RUN, RX_RIGHT_WRITE);
251 static AgLowered L;
252 CHECK(rx_graph_lower(&L, &g_w, &g, &g_skills, &g_caps, RES_CELL, cell, RES_RUN, runc, NULL) == 0, "lower");
253 CHECK(rx_graph_start(&L, ext, 1) > 0, "start");
254 CHECK(rx_world_wait_quiescent(&g_w, 30000) == RX_OK, "settle");
255 AgResult ar;
256 rx_graph_collect(&L, 1, &ar);
257 int sn = -1;
258 for (uint32_t i = 0; i < g.n_nodes; i++)
259 if (g.nodes[i].alive && g.nodes[i].kind == AG_SKILL) sn = (int)i;
260 CHECK(ar.outcome == AG_RUN_SUCCESS && sn >= 0 && ar.value[sn] == 14 * 3 + 1,
261 "the World ran skill 7 (outcome %d, value %llu)", ar.outcome,
262 sn >= 0 ? (unsigned long long)ar.value[sn] : 0ull);
263 cq_catalog_free(&f.c);
264}
265
266/* ---- failure modes ---- */
267
268static void t_withdrawn(void) {
269 printf("[*] (a) provider withdrawal\n");
270 Fx f;
271 fx_init(&f);
272 CqHeld held = { &g_w, &g_caps };
273 SrRequirement q = requirement(OP_SCALE);
274 SrRoute held_route, route;
275 CHECK(sr_route(&f.r, &q, &held, NOW_US, &held_route) == SR_OK && held_route.chosen.skill_id == 7, "route");
276 CqKey k7 = held_route.key;
277 CHECK(cq_withdraw(&f.c, &k7) == CQ_OK, "withdraw 7");
278 int s = tmpl();
279 CHECK(sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &held_route, NOW_US) == SR_E_WITHDRAWN && unbound(s),
280 "held route to a withdrawn provider: SR_E_WITHDRAWN, node unbound");
281 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_OK &&
282 g_tmpl.nodes[s].op == 9, "fresh route falls back to skill 9");
283 SrRoute r9 = route;
284 CHECK(cq_withdraw(&f.c, &r9.key) == CQ_OK, "withdraw 9");
285 s = tmpl();
286 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
287 route.why == SR_WHY_GONE && route.stats.rejected_dead == 2 && unbound(s),
288 "all local providers withdrawn: SR_E_NO_CANDIDATE why GONE (why 0x%x), node unbound", route.why);
289 cq_catalog_free(&f.c);
290}
291
292static void t_stale(void) {
293 printf("[*] (b) stale generation: a route held from before an upsert\n");
294 Fx f;
295 fx_init(&f);
296 CqHeld held = { &g_w, &g_caps };
297 SrRequirement q = requirement(OP_SCALE);
298 SrRoute held_route, route;
299 CHECK(sr_route(&f.r, &q, &held, NOW_US, &held_route) == SR_OK && held_route.generation == 1, "route gen 1");
300 CqEntry up = p7();
301 up.skill_id = 7;
302 up.skill_version = 1;
303 up.skill_digest[0] = 0x07;
304 up.machine_id = f.c.self_machine;
305 up.source = CQ_SRC_SKILL;
306 up.generation = 2;
307 CHECK(cq_register(&f.c, &up, NULL, 0) == CQ_OK && cq_lookup(&f.c, &held_route.key)->generation == 2,
308 "upsert in place to generation 2");
309 int s = tmpl();
310 CHECK(sr_route_check(&f.r, &held_route, NOW_US) == SR_E_STALE &&
311 sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &held_route, NOW_US) == SR_E_STALE && unbound(s),
312 "held generation-1 route: SR_E_STALE, node unbound");
313 /* An upsert that changes the procedure (new version and digest). */
314 SrRoute held2;
315 CHECK(sr_route(&f.r, &q, &held, NOW_US, &held2) == SR_OK && held2.generation == 2, "route gen 2");
316 up.skill_version = 2;
317 up.skill_digest[0] = 0x77;
318 up.generation = 3;
319 CHECK(cq_register(&f.c, &up, NULL, 0) == CQ_OK, "upsert v2");
320 CHECK(sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &held2, NOW_US) == SR_E_STALE && unbound(s),
321 "held route to v1 after the v2 upsert: SR_E_STALE, node unbound");
322 CHECK(sr_route(&f.r, &q, &held, NOW_US, &route) == SR_OK && route.chosen.skill_id == 9 &&
323 route.rejected.digest_mismatch == 1, "fresh route refuses 7 (graph digest no longer the executable one)");
324 cq_catalog_free(&f.c);
325}
326
327static void t_unavailable(void) {
328 printf("[*] (c) unavailable provider\n");
329 Fx f;
330 fx_init(&f);
331 CqHeld held = { &g_w, &g_caps };
332 SrRequirement q = requirement(OP_SCALE);
333 SrRoute held_route, r9, route;
334 CHECK(sr_route(&f.r, &q, &held, NOW_US, &held_route) == SR_OK, "route");
335 CHECK(cq_set_availability(&f.c, &held_route.key, CQ_LIVE_UNAVAILABLE) == CQ_OK, "7 unavailable");
336 int s = tmpl();
337 CHECK(sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &held_route, NOW_US) == SR_E_UNAVAILABLE && unbound(s),
338 "held route: SR_E_UNAVAILABLE, node unbound");
339 CHECK(sr_route(&f.r, &q, &held, NOW_US, &r9) == SR_OK && r9.chosen.skill_id == 9, "9 still available");
340 CHECK(cq_set_availability(&f.c, &r9.key, CQ_LIVE_UNAVAILABLE) == CQ_OK, "9 unavailable");
341 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
342 route.why == SR_WHY_UNAVAILABLE && route.stats.rejected_unavailable == 2 && unbound(s),
343 "SR_E_NO_CANDIDATE why UNAVAILABLE (why 0x%x), node unbound", route.why);
344 cq_catalog_free(&f.c);
345}
346
347static void t_machine(void) {
348 printf("[*] (d) machine mismatch and unknown machine\n");
349 Fx f;
350 fx_init(&f);
351 CqHeld held = { &g_w, &g_caps };
352 SrRoute route;
353 int s = tmpl();
354 /* Pinned to a machine the graph does not know. */
355 SrRequirement q = requirement(OP_SCALE);
356 q.local_only = 0;
357 q.need.locality_constraints.allowed = 0;
358 q.pin_machine_set = 1;
359 q.pin_machine = mid(9);
360 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_MACHINE && unbound(s),
361 "pinned machine unknown: SR_E_MACHINE, node unbound (%d)", route.verdict);
362 /* Pinned to C: known, leased, provides nothing; A's and B's providers are on other machines. */
363 q.pin_machine = mid(3);
364 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
365 route.why == SR_WHY_MACHINE && route.rejected.machine_mismatch == 3 && unbound(s),
366 "providers on other machines: SR_E_NO_CANDIDATE why MACHINE (why 0x%x, %llu)", route.why,
367 (unsigned long long)route.rejected.machine_mismatch);
368 /* Pinned to B: the winner is B's skill 11, remote, so the node stays unbound. */
369 q.pin_machine = mid(2);
370 AienMachineId b = mid(2);
371 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_REMOTE &&
372 route.chosen.skill_id == 11 && aien_mid_equal(&route.target, &b) && unbound(s),
373 "pinned to B: SR_E_REMOTE to B's skill 11, node unbound");
374 /* A held remote route is checked against the lease: valid before it ends, SR_E_MACHINE after. */
375 CHECK(sr_route_check(&f.r, &route, NOW_US) == SR_E_REMOTE, "held route to B before lease end: SR_E_REMOTE");
376 CHECK(sr_route_check(&f.r, &route, NOW_US + 999999) == SR_E_REMOTE, "one microsecond before lease end");
377 CHECK(sr_route_check(&f.r, &route, NOW_US + 1000000) == SR_E_MACHINE &&
378 sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &route, NOW_US + 2000000) == SR_E_MACHINE && unbound(s),
379 "held route to B after lease end: SR_E_MACHINE, node unbound");
380 CHECK(cq_machine_advertise_id(&f.c, &b, NOW_US + 5000000, NULL) == CQ_OK &&
381 sr_route_check(&f.r, &route, NOW_US + 2000000) == SR_E_REMOTE, "lease renewed: route valid again");
382 /* A provider on a leased index with no canonical identity: never a target. */
383 SrRequirement qs = requirement(OP_SUMMARIZE);
384 qs.local_only = 0;
385 qs.need.locality_constraints.allowed = 0;
386 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &qs, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
387 route.why == SR_WHY_MACHINE_UNKNOWN && unbound(s),
388 "provider machine without identity: SR_E_NO_CANDIDATE why MACHINE_UNKNOWN (verdict %d why 0x%x)",
389 route.verdict, route.why);
390 cq_catalog_free(&f.c);
391}
392
393static void t_digest(void) {
394 printf("[*] (e) skill digest / version pin\n");
395 Fx f;
396 fx_init(&f);
397 CqHeld held = { &g_w, &g_caps };
398 SrRoute route;
399 int s = tmpl();
400 SrRequirement q = requirement(OP_SCALE);
401 q.pin_skill_digest[0] = 0x07;
402 q.pin_skill_digest[1] = 0xAA; /* X: the graph advertises 0x07 00.. (Y) */
403 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
404 route.why == SR_WHY_PIN && route.rejected.pin_mismatch == 2 && unbound(s),
405 "pinned digest X, graph has Y: SR_E_NO_CANDIDATE why PIN (why 0x%x), node unbound", route.why);
406 memset(q.pin_skill_digest, 0, 32);
407 q.pin_skill_version = 2;
408 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_E_NO_CANDIDATE &&
409 route.why == SR_WHY_PIN && unbound(s), "pinned version 2, graph has 1: why PIN");
410 q.pin_skill_version = 1;
411 q.pin_skill_digest[0] = 0x09; /* the costlier, matching provider */
412 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &held, NOW_US, &route) == SR_OK &&
413 g_tmpl.nodes[s].op == 9 && route.skill_digest[0] == 0x09, "matching pin binds skill 9");
414 cq_catalog_free(&f.c);
415}
416
417static void t_authority(void) {
418 printf("[*] (f) authority insufficiency\n");
419 Fx f;
420 fx_init(&f);
421 CqHeld none = { &g_w, &g_caps_none };
422 SrRoute route;
423 int s = tmpl();
424 SrRequirement q = requirement(OP_SCALE);
425 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, &none, NOW_US, &route) == SR_E_NO_CANDIDATE &&
426 route.why == SR_WHY_AUTHORITY && route.stats.rejected_authority == 2 && unbound(s),
427 "authority listed but not held: SR_E_NO_CANDIDATE why AUTHORITY (why 0x%x), node unbound", route.why);
428 CHECK(sr_bind_node(&f.r, &g_tmpl, (uint32_t)s, &q, NULL, NOW_US, &route) == SR_E_NO_CANDIDATE &&
429 route.why == SR_WHY_AUTHORITY && unbound(s), "no authority view at all: why AUTHORITY");
430 cq_catalog_free(&f.c);
431}
432
433/* ---- alternatives ---- */
434
435static void t_alternatives(void) {
436 printf("[*] sr_route_alternatives: ranked, out[0] = sr_route, dominated included\n");
437 Fx f;
438 fx_init(&f);
439 CqHeld held = { &g_w, &g_caps };
440 for (int remote = 0; remote < 2; remote++) {
441 SrRequirement q = requirement(OP_SCALE);
442 q.local_only = (uint32_t)!remote;
443 if (remote) q.need.locality_constraints.allowed = 0;
444 SrRoute one, alt[8];
445 int rc1 = sr_route(&f.r, &q, &held, NOW_US, &one);
446 int n = sr_route_alternatives(&f.r, &q, &held, NOW_US, alt, 8);
447 CHECK(rc1 == SR_OK && n == 2 + remote, "count %d (remote %d)", n, remote);
448 if (n < 2) continue;
449 CHECK(memcmp(&alt[0].chosen, &one.chosen, sizeof one.chosen) == 0 && alt[0].verdict == one.verdict &&
450 alt[0].generation == one.generation && memcmp(&alt[0].key, &one.key, sizeof one.key) == 0,
451 "out[0] is sr_route's choice");
452 CHECK(alt[0].chosen.skill_id == 7 && alt[1].chosen.skill_id == 9 &&
453 alt[1].chosen.expected_cost > alt[0].chosen.expected_cost &&
454 alt[1].chosen.expected_latency > alt[0].chosen.expected_latency,
455 "skill 9, dominated by 7 on cost and latency, is offered second");
456 for (int i = 1; i < n; i++)
457 CHECK(cq_dim_value(&alt[i - 1].chosen, CQ_DIM_COST) <= cq_dim_value(&alt[i].chosen, CQ_DIM_COST),
458 "ranked by cost");
459 if (remote)
460 CHECK(alt[2].chosen.skill_id == 11 && alt[2].verdict == SR_E_REMOTE && alt[2].target_known,
461 "remote alternative carries its canonical target");
462 CHECK(sr_route_check(&f.r, &alt[1], NOW_US) == SR_OK, "an alternative is a valid held route");
463 int s = tmpl();
464 CHECK(sr_bind_route(&f.r, &g_tmpl, (uint32_t)s, &alt[1], NOW_US) == SR_OK && g_tmpl.nodes[s].op == 9,
465 "the second alternative binds");
466 }
467 SrRoute alt[2];
468 SrRequirement q = requirement(OP_SCALE);
469 CHECK(sr_route_alternatives(&f.r, &q, &held, NOW_US, alt, 1) == 1 && alt[0].chosen.skill_id == 7,
470 "max 1 gives the winner");
471 CHECK(sr_route_alternatives(&f.r, &q, &held, NOW_US, alt, 0) == SR_E_ARG, "max 0 refused");
472 cq_catalog_free(&f.c);
473}
474
475int main(void) {
476 memset(&g_skills, 0, sizeof g_skills);
477 g_skills.n = 2;
478 g_skills.skill[0] = (AgSkill){ 7, sk_scale, { 0x07 } };
479 g_skills.skill[1] = (AgSkill){ 9, sk_other, { 0x09 } };
480
481 AienosCapView *view;
482 CHECK(aienos_cap_start(&g_admin, &view) == 0, "authority");
483 CHECK(rx_world_init_native(&g_w, view, 2, 1u << 14) == RX_OK, "world");
484 g_w.external_subject = SUBJ_EXTERNAL;
485 memset(&g_caps, 0, sizeof g_caps);
486 g_caps.subject = SUBJ_PLAN;
487 g_caps.cap[0].ref = mint(SUBJ_PLAN, RES_HELD, RX_RIGHT_READ);
488 g_caps.cap[0].resource = RES_HELD;
489 g_caps.cap[0].rights = RX_RIGHT_READ;
490 g_caps.n = 1;
491 memset(&g_caps_none, 0, sizeof g_caps_none);
492 g_caps_none.subject = SUBJ_PLAN;
493 g_caps_none.cap[0].ref = (RxCapRef){ 4000, 1 }; /* listed, never minted */
494 g_caps_none.cap[0].resource = RES_HELD;
495 g_caps_none.cap[0].rights = RX_RIGHT_READ;
496 g_caps_none.n = 1;
497
498 t_end_to_end();
499 t_withdrawn();
500 t_stale();
501 t_unavailable();
502 t_machine();
503 t_digest();
504 t_authority();
505 t_alternatives();
506
507 rx_world_wait_quiescent(&g_w, 30000);
508 rx_world_destroy(&g_w);
509 aienos_cap_stop(g_admin, view);
510 printf("checks %d failures %d\n", g_checks, g_fail);
511 if (g_fail == 0) printf("COMPOSITION2_SKILLROUTE_PASS\n");
512 return g_fail ? 1 : 0;
513}