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
omega-mv / src / runtime / rx_skillroute.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_skillroute.h -- Skill Router over the canonical Capability Graph (M20). |
| 19 | * |
| 20 | * A Skill is a provider of capabilities: one procedure (skill id, version, |
| 21 | * content digest = AgSkill.identity) that realizes one or more semantic |
| 22 | * operations. The router turns a work requirement into a target: |
| 23 | * |
| 24 | * requirement (CqNeed: the capability needed, not an implementation) |
| 25 | * -> cq_compile / cq_query_admit over the Capability Graph (rx_capq) |
| 26 | * -> Skill providers of that capability (skill_id != 0) |
| 27 | * -> admissible targets: a local Skill must be in this machine's |
| 28 | * executable table with the digest the graph advertises; a remote one |
| 29 | * must be on a leased Fabric machine; authority is held when the |
| 30 | * requirement says so (otherwise compile/AEGIS decides, never here) |
| 31 | * -> ranked with the rx_capq order (Pareto front, caller's priorities, |
| 32 | * ids last), so the same graph and requirement give the same target. |
| 33 | * |
| 34 | * Ranking happens over admissible providers only (the admit function runs |
| 35 | * before ranking), so a cheaper provider this machine cannot run never hides |
| 36 | * one it can. |
| 37 | * |
| 38 | * The runtime path: an action-graph template leaves its AG_SKILL nodes |
| 39 | * unbound (op 0: the engine fails such a node, there is no skill 0). The |
| 40 | * caller states, per node, the capability it needs; sr_bind_node routes it |
| 41 | * and binds the node to the chosen Skill (cq_bind_skill_node), then the usual |
| 42 | * rx_graph_compile -> rx_graph_lower -> rx_graph_start runs it on the World. |
| 43 | * A remote winner is returned with its canonical AienMachineId and is not |
| 44 | * bound: sending work to it is the Fabric's job (SR_E_REMOTE). |
| 45 | * |
| 46 | * This router discovers; it does not authorize. It has no authority admin |
| 47 | * handle and mints nothing; the build checks rx_skillroute.o for admin |
| 48 | * symbols as it does for rx_capq.o. |
| 49 | */ |
| 50 | #ifndef RX_SKILLROUTE_H |
| 51 | #define RX_SKILLROUTE_H |
| 52 | |
| 53 | #include "rx_capq.h" |
| 54 | |
| 55 | #include <stdint.h> |
| 56 | |
| 57 | #define SR_OK 0 |
| 58 | #define SR_E_ARG -40 |
| 59 | #define SR_E_NO_CANDIDATE -41 /* nothing admissible provides the capability */ |
| 60 | #define SR_E_REMOTE -42 /* the winner is on another machine: hand it to the Fabric */ |
| 61 | #define SR_E_QUERY -43 /* the graph query failed (see route.query_verdict) */ |
| 62 | #define SR_E_BIND -44 |
| 63 | /* COMPOSITION-2: revalidating a held route (sr_route_check / sr_bind_route). */ |
| 64 | #define SR_E_STALE -45 /* the entry changed since the route (generation, version, digest) */ |
| 65 | #define SR_E_WITHDRAWN -46 /* the chosen provider was withdrawn (or is gone) */ |
| 66 | #define SR_E_UNAVAILABLE -47 /* the chosen provider is registered but not usable now */ |
| 67 | #define SR_E_MACHINE -48 /* pinned or target machine unknown to the graph, or its lease ended */ |
| 68 | |
| 69 | /* SrRoute.why on SR_E_NO_CANDIDATE: every rejection class seen (bit set). */ |
| 70 | #define SR_WHY_GONE 0x001u /* withdrawn, or its machine's lease ended */ |
| 71 | #define SR_WHY_UNAVAILABLE 0x002u |
| 72 | #define SR_WHY_AUTHORITY 0x004u /* require_held and the authority is not held */ |
| 73 | #define SR_WHY_NOT_SKILL 0x008u |
| 74 | #define SR_WHY_NOT_EXECUTABLE 0x010u |
| 75 | #define SR_WHY_DIGEST 0x020u /* local procedure differs from the graph */ |
| 76 | #define SR_WHY_REMOTE_REFUSED 0x040u |
| 77 | #define SR_WHY_PIN 0x080u /* requirement pins another skill version or digest */ |
| 78 | #define SR_WHY_MACHINE 0x100u /* provider not on the pinned machine */ |
| 79 | #define SR_WHY_MACHINE_UNKNOWN 0x200u /* provider machine has no canonical identity */ |
| 80 | |
| 81 | typedef struct { |
| 82 | CqCatalog *graph; /* the canonical Capability Graph */ |
| 83 | const AgSkillTable *skills; /* procedures this machine can execute */ |
| 84 | } SrRouter; |
| 85 | |
| 86 | typedef struct { |
| 87 | CqNeed need; /* need.semantic_operation: the required capability */ |
| 88 | CqTradeoffs t; /* ranking; t.require_held filters on held authority */ |
| 89 | uint32_t local_only; /* 1: refuse remote targets */ |
| 90 | /* COMPOSITION-2 pins; zero = unpinned. */ |
| 91 | uint32_t pin_skill_version; /* only this version of the provider's Skill */ |
| 92 | uint8_t pin_skill_digest[32]; /* only a provider advertising this AgSkill.identity */ |
| 93 | uint32_t pin_machine_set; /* 1: only providers on pin_machine */ |
| 94 | AienMachineId pin_machine; /* canonical; unknown to the graph = SR_E_MACHINE */ |
| 95 | } SrRequirement; |
| 96 | |
| 97 | typedef struct { |
| 98 | int verdict; /* SR_OK, SR_E_REMOTE or an error */ |
| 99 | int query_verdict; /* CQ_* from the graph */ |
| 100 | CqCandidate chosen; |
| 101 | uint8_t remote; |
| 102 | uint8_t target_known; /* 1: target holds the canonical identity */ |
| 103 | AienMachineId target; /* canonical machine of the chosen provider */ |
| 104 | uint32_t skill_version; |
| 105 | uint8_t skill_digest[32]; |
| 106 | uint8_t plan_digest[32]; /* cq_compile identity of the requirement */ |
| 107 | uint32_t n_admissible; |
| 108 | CqStats stats; |
| 109 | struct { |
| 110 | uint64_t not_a_skill; /* provider is not a Skill */ |
| 111 | uint64_t not_executable; /* local Skill missing from the executable table */ |
| 112 | uint64_t digest_mismatch; /* local procedure differs from what the graph says */ |
| 113 | uint64_t remote_refused; /* local_only */ |
| 114 | uint64_t pin_mismatch; /* requirement pins another version or digest */ |
| 115 | uint64_t machine_mismatch; /* not on the pinned machine */ |
| 116 | uint64_t machine_unknown; /* machine has no canonical identity (canonical graph) */ |
| 117 | } rejected; |
| 118 | /* COMPOSITION-2: what the route was decided on, for sr_route_check. */ |
| 119 | CqKey key; /* graph key of the chosen provider */ |
| 120 | uint64_t generation; /* its CqEntry.generation at routing time */ |
| 121 | uint32_t why; /* SR_WHY_* on SR_E_NO_CANDIDATE */ |
| 122 | } SrRoute; |
| 123 | |
| 124 | /* A Skill and the capabilities it provides. */ |
| 125 | typedef struct { |
| 126 | uint32_t skill_id, version; |
| 127 | uint8_t digest[32]; /* AgSkill.identity */ |
| 128 | uint32_t machine; /* index in the graph (self for a local Skill) */ |
| 129 | } SrSkill; |
| 130 | |
| 131 | /* Register every capability a Skill provides: each `provides[i]` is a full |
| 132 | * CqEntry minus the Skill fields, which this fills (skill_id, skill_version, |
| 133 | * skill_digest, machine_id; source SKILL on this machine, FABRIC elsewhere). */ |
| 134 | int sr_register_skill(CqCatalog *graph, const SrSkill *skill, const CqEntry *provides, uint32_t n); |
| 135 | |
| 136 | /* Skill -> capabilities: keys of the live entries this Skill provides, in |
| 137 | * catalog order; returns the total count (may exceed max). */ |
| 138 | uint32_t sr_skill_capabilities(const CqCatalog *graph, uint32_t skill_id, uint32_t version, |
| 139 | CqKey *out, uint32_t max); |
| 140 | |
| 141 | /* Requirement -> chosen target. */ |
| 142 | int sr_route(const SrRouter *r, const SrRequirement *req, const CqHeld *held, uint64_t now_us, |
| 143 | SrRoute *out); |
| 144 | |
| 145 | /* COMPOSITION-2: ranked admissible alternatives (dominated included), at |
| 146 | * most `max`; out[0] is sr_route's choice. Returns the count or an SR_E_*. */ |
| 147 | int sr_route_alternatives(const SrRouter *r, const SrRequirement *req, const CqHeld *held, |
| 148 | uint64_t now_us, SrRoute *out, uint32_t max); |
| 149 | |
| 150 | /* Route, then bind action-graph node `node` (AG_SKILL/AG_RETRY) to the local |
| 151 | * winner. SR_E_REMOTE leaves the node unbound and the route filled. Any |
| 152 | * failure leaves the node as it was (unbound). */ |
| 153 | int sr_bind_node(const SrRouter *r, AgGraph *g, uint32_t node, const SrRequirement *req, |
| 154 | const CqHeld *held, uint64_t now_us, SrRoute *out); |
| 155 | |
| 156 | /* COMPOSITION-2: is a route the caller holds (from sr_route or |
| 157 | * sr_route_alternatives) still what the graph says? Fails closed with |
| 158 | * SR_E_WITHDRAWN, SR_E_UNAVAILABLE, SR_E_STALE (generation, version or digest |
| 159 | * moved, or the local procedure no longer matches) or SR_E_MACHINE (lease |
| 160 | * ended at now_us, identity gone). Authority is not decided here: binding declares it |
| 161 | * and compile finds it held or missing. */ |
| 162 | int sr_route_check(const SrRouter *r, const SrRoute *route, uint64_t now_us); |
| 163 | |
| 164 | /* Check a held route, then bind node `node` to it. SR_E_REMOTE for a remote |
| 165 | * route; any failure leaves the node unbound. */ |
| 166 | int sr_bind_route(const SrRouter *r, AgGraph *g, uint32_t node, const SrRoute *route, |
| 167 | uint64_t now_us); |
| 168 | |
| 169 | #endif /* RX_SKILLROUTE_H */ |