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 / src / runtime / rx_capq.h

433 lines · 21369 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 * rx_capq.h -- Omega capability query IR (OMEGA_CAPABILITY_QUERY).
19 *
20 * AIEN says what it needs done. It does not need to know which Skill, tool,
21 * device or machine can do it. It states a CapabilityNeed: the semantic
22 * operation, the types it can supply and must get back, the effects it
23 * accepts, the most authority it will use, where the work may run, its
24 * latency and energy budgets, and how reliable and how well evidenced the
25 * provider must be. Omega compiles the need into a plan of probes against
26 * the sources that can realize that operation:
27 *
28 * Capability Graph native AIEN capabilities
29 * Skill Network reusable procedures (Skill Net)
30 * MCP registry MCP tools, where the operation allows them
31 * local physical this machine's devices
32 * Fabric capabilities other machines in the house advertise
33 *
34 * and runs it against an index. What comes back into cognition is a short,
35 * fixed-size list of CapabilityCandidate records: ids and numbers, no
36 * description text. The catalog's descriptions stay in the catalog. The
37 * bytes AIEN receives are bounded by CQ_MAX_K, whatever the catalog size.
38 *
39 * Ranking. There is no relevance score. Each candidate carries its values on
40 * explicit dimensions (cost, latency, energy, confidence, reliability,
41 * whether its authority is already held, locality). The caller names the
42 * dimensions that matter, in priority order, with a tolerance band on each.
43 * Omega keeps the candidates no other candidate beats on every named
44 * dimension (the Pareto front), then orders them by the caller's priorities:
45 * on the first dimension it keeps everything within the band of the best,
46 * then the next dimension decides among those, and so on. Ties end on the
47 * ids, so the order is deterministic.
48 *
49 * Authority. A candidate states the authority it needs. The query compares
50 * it with the need's ceiling (pure comparison), and may ask the world's
51 * authority view whether the principal already holds it (a validation, never
52 * a mint). This file has no admin handle; the build fails if rx_capq.o
53 * references an authority admin operation.
54 *
55 * M20: this catalog IS the canonical Capability Graph. One entry says: this
56 * capability (capability_id, a semantic operation), provided by this provider
57 * (realization_id; a Skill when skill_id != 0, with skill_version and
58 * skill_digest = AgSkill.identity of the procedure), on this machine, needs
59 * this authority (a requirement, never a grant), under these constraints
60 * (types, effects, cost, latency, energy), with this availability (`live`)
61 * and this evidence (level + ref). An entry is keyed by CqKey; registering an
62 * existing key updates it in place.
63 *
64 * Machines. CqEntry/CqCandidate/CqNeed carry a uint32 machine INDEX, valid in
65 * this process only. A catalog made with cq_catalog_init_canonical resolves
66 * every index through the caller's AienMachineIndex (aien_machine_id.h);
67 * anything persisted or sent carries the 44-byte AienMachineId record instead
68 * (see the wire form below). A catalog made with cq_catalog_init has no
69 * identity table: it ranks and routes, but cannot export or ingest records.
70 */
71#ifndef RX_CAPQ_H
72#define RX_CAPQ_H
73
74#include "aien_machine_id.h"
75#include "rx_graph.h"
76
77#include <stddef.h>
78#include <stdint.h>
79
80#define CQ_MAX_K 16u /* candidates one query may return into cognition */
81#define CQ_MAX_PLAN_OPS 32u /* operation plus its specializations */
82
83/* Sources a capability can come from. */
84enum { CQ_SRC_GRAPH = 0, /* Capability Graph (native) */
85 CQ_SRC_SKILL, /* Skill Network */
86 CQ_SRC_MCP, /* MCP registry */
87 CQ_SRC_PHYSICAL, /* local physical capability */
88 CQ_SRC_FABRIC, /* advertised by another machine */
89 CQ_SOURCES };
90#define CQ_SRC(s) (1u << (s))
91#define CQ_SRC_ALL ((1u << CQ_SOURCES) - 1u)
92
93/* Effect classes (aien-architecture docs/04). A provider declares every
94 * class it may have; the need lists the classes it accepts. */
95#define CQ_FX_PURE 0x001u
96#define CQ_FX_READ_FILESYSTEM 0x002u
97#define CQ_FX_READ_NETWORK 0x004u
98#define CQ_FX_SPAWN_PROCESS 0x008u
99#define CQ_FX_LOCAL_EPHEMERAL 0x010u
100#define CQ_FX_WORLD_MUTATION 0x020u
101#define CQ_FX_EXTERNAL_WRITE 0x040u
102#define CQ_FX_EXTERNAL_IRREVERSIBLE 0x080u
103#define CQ_FX_SECRET_BEARING 0x100u
104
105/* Locality. */
106#define CQ_LOC_LOCAL 0x1u /* this machine */
107#define CQ_LOC_FABRIC 0x2u /* another machine of the house Fabric */
108
109/* Evidence levels, weakest first. */
110enum { CQ_EV_NONE = 0,
111 CQ_EV_DECLARED, /* the provider says so */
112 CQ_EV_MEASURED, /* we measured it */
113 CQ_EV_RECEIPT }; /* a bound qualification receipt */
114
115#define CQ_PPM 1000000u
116
117typedef struct {
118 uint32_t semantic_operation; /* operation id, or an alias the catalog resolves */
119 uint64_t accepted_input_types; /* types the caller can supply (bit per type) */
120 uint64_t required_output_types; /* types the result must include */
121 uint32_t effect_class; /* accepted effect classes (CQ_FX_*) */
122 struct {
123 uint64_t resource_lo, resource_hi; /* the resource range it may touch */
124 uint32_t rights; /* the most rights it may use */
125 } authority_ceiling;
126 struct {
127 uint32_t allowed; /* CQ_LOC_* */
128 uint32_t machine; /* 0 = any machine, else only this one */
129 } locality_constraints;
130 uint64_t latency_budget; /* microseconds; 0 = no budget */
131 uint64_t energy_budget; /* microjoules; 0 = no budget */
132 uint32_t reliability_requirement; /* minimum, parts per million */
133 uint32_t evidence_requirement; /* minimum CQ_EV_* */
134 uint32_t sources; /* optional restriction (CQ_SRC mask); 0 = all */
135} CqNeed;
136
137typedef struct {
138 uint32_t capability_id;
139 uint32_t skill_id; /* Skill Net procedure that realizes it (0 = none) */
140 uint32_t machine_id;
141 uint32_t realization_id;
142 struct {
143 uint64_t resource;
144 uint32_t rights;
145 uint32_t held; /* 1: the principal holds a valid capability now */
146 } required_authority;
147 uint64_t expected_cost; /* abstract cost units */
148 uint64_t expected_latency; /* microseconds */
149 uint64_t expected_energy; /* microjoules */
150 uint32_t confidence; /* parts per million */
151 uint32_t reliability; /* parts per million */
152 struct {
153 uint64_t ref; /* receipt or measurement id */
154 uint32_t level; /* CQ_EV_* */
155 } evidence;
156 uint8_t source; /* CQ_SRC_* */
157 uint8_t local; /* 1: runs on this machine */
158 uint16_t effects; /* CQ_FX_* the provider may have */
159} CqCandidate;
160
161/* Ranking dimensions. Each is turned into a value to minimize. */
162enum { CQ_DIM_COST = 0, CQ_DIM_LATENCY, CQ_DIM_ENERGY, CQ_DIM_CONFIDENCE,
163 CQ_DIM_RELIABILITY, CQ_DIM_AUTHORITY /* held first */, CQ_DIM_LOCALITY /* local first */,
164 CQ_DIMS };
165
166typedef struct {
167 uint32_t n_order;
168 uint8_t order[CQ_DIMS]; /* dimensions that matter, most important first */
169 uint32_t tolerance_ppm[CQ_DIMS]; /* within best * (1 + tol) counts as tied (by dimension) */
170 uint64_t max_cost; /* 0 = none */
171 uint32_t min_confidence; /* ppm */
172 uint32_t k; /* at most this many (<= CQ_MAX_K) */
173 uint32_t include_dominated; /* 0: Pareto front only */
174 uint32_t require_held; /* 1: only candidates whose authority is held now */
175} CqTradeoffs;
176
177/* ---- catalog (the Capability Graph and its sources) ---- */
178
179typedef struct {
180 uint32_t capability_id, skill_id, machine_id, realization_id;
181 uint32_t op; /* canonical operation */
182 uint8_t source; /* CQ_SRC_* */
183 uint8_t evidence_level;
184 uint16_t effects;
185 uint64_t in_types; /* inputs it requires */
186 uint64_t out_types; /* outputs it produces */
187 uint64_t auth_resource;
188 uint32_t auth_rights;
189 uint32_t confidence_ppm, reliability_ppm;
190 uint64_t cost, latency_us, energy_uj;
191 uint64_t evidence_ref;
192 uint32_t live; /* CQ_LIVE_*: withdrawn, available, unavailable */
193 uint32_t desc_len; /* description text, kept in the catalog */
194 uint64_t desc_off;
195 /* M20 Capability Graph fields (zero = none / unpinned). */
196 uint32_t skill_version; /* version of skill_id's procedure */
197 uint8_t skill_digest[32]; /* AgSkill.identity the provider claims */
198 uint64_t generation; /* advertiser's revision; newer replaces older */
199} CqEntry;
200
201/* Availability (CqEntry.live). Withdrawn is final for that registration: only
202 * a new registration of the key brings it back. */
203#define CQ_LIVE_WITHDRAWN 0u
204#define CQ_LIVE_AVAILABLE 1u
205#define CQ_LIVE_UNAVAILABLE 2u /* registered, not usable now (busy, session down) */
206
207/* An entry's identity: which capability, from which provider, on which
208 * machine, through which Skill. Same tuple the ranking uses to break ties. */
209typedef struct {
210 uint32_t capability_id, realization_id, machine_id, skill_id;
211} CqKey;
212
213typedef struct {
214 uint32_t parent; /* 0 = none */
215 uint32_t domains; /* CQ_SRC mask of sources that can realize it */
216 uint32_t first_child, next_sibling;
217} CqOp;
218
219typedef struct {
220 uint32_t self_machine;
221 uint32_t n_ops, cap_ops;
222 CqOp *ops; /* indexed by op id, 1..n_ops */
223 uint32_t alias_mask;
224 uint32_t *alias_key, *alias_op; /* open addressing; key 0 = empty */
225 uint32_t n_alias;
226 uint32_t mach_mask;
227 uint32_t *mach_key;
228 uint64_t *mach_lease; /* lease end, microseconds */
229 uint32_t n_mach;
230 uint32_t n, cap;
231 CqEntry *e;
232 char *desc;
233 uint64_t desc_bytes, desc_cap;
234 uint32_t *bucket_start; /* (op, source) -> first index in bucket[] */
235 uint32_t *bucket;
236 int built;
237 AienMachineIndex *machines; /* canonical identity behind each index (may be NULL) */
238 uint32_t key_mask; /* CqKey -> entry: open addressing, slot = index + 1 */
239 uint32_t *key_slot;
240} CqCatalog;
241
242#define CQ_OK 0
243#define CQ_E_ARG -1
244#define CQ_E_NOMEM -2
245#define CQ_E_NO_OP -3 /* the need names no known operation */
246#define CQ_E_NO_SOURCE -4 /* no source may realize it under the constraints */
247#define CQ_E_UNBUILT -5
248#define CQ_E_PLAN_FULL -6 /* more specializations than CQ_MAX_PLAN_OPS */
249#define CQ_E_NOT_FOUND -7 /* no entry with that key */
250#define CQ_E_WITHDRAWN -8 /* the entry was withdrawn; register it again */
251#define CQ_E_NO_IDENTITY -9 /* no AienMachineIndex, or the index is unbound */
252#define CQ_E_WIRE -10 /* malformed, foreign or self-originated record */
253#define CQ_E_STALE -11 /* record older than (or as old as) what is held */
254
255/* Index-only catalog: self_machine is a local runtime index. */
256int cq_catalog_init(CqCatalog *c, uint32_t self_machine, uint32_t alias_capacity,
257 uint32_t machine_capacity);
258/* Canonical catalog: binds `self` in `machines` (caller-owned, must outlive
259 * the catalog) and resolves every machine index through it. */
260int cq_catalog_init_canonical(CqCatalog *c, AienMachineIndex *machines, const AienMachineId *self,
261 uint32_t alias_capacity, uint32_t machine_capacity);
262void cq_catalog_free(CqCatalog *c);
263/* Canonical identity <-> local index (CQ_E_NO_IDENTITY without a table). */
264int cq_machine_identity(const CqCatalog *c, uint32_t machine, AienMachineId *out);
265uint32_t cq_machine_index(const CqCatalog *c, const AienMachineId *m); /* 0 if not bound */
266/* Fabric advertisement by canonical identity: binds an index, sets the lease. */
267int cq_machine_advertise_id(CqCatalog *c, const AienMachineId *m, uint64_t lease_until_us,
268 uint32_t *index_out);
269/* Operations are numbered 1..n and defined parent first. */
270int cq_op_define(CqCatalog *c, uint32_t op, uint32_t parent, uint32_t domains);
271int cq_alias(CqCatalog *c, uint32_t alias, uint32_t op);
272/* A machine's Fabric advertisement; its capabilities are live until the lease ends. */
273int cq_machine_advertise(CqCatalog *c, uint32_t machine, uint64_t lease_until_us);
274/* Register, or update in place when the entry's CqKey is already present
275 * (a withdrawn entry is revived). An update that keeps op and source keeps
276 * the index built; anything else needs cq_catalog_build again. An update
277 * with no description keeps the old one. */
278int cq_register(CqCatalog *c, const CqEntry *e, const char *desc, uint32_t desc_len);
279CqKey cq_key_of(const CqEntry *e);
280CqKey cq_key_of_candidate(const CqCandidate *c);
281/* The entry for a key, or NULL. The pointer is valid until the next register. */
282const CqEntry *cq_lookup(const CqCatalog *c, const CqKey *k);
283/* Availability: CQ_LIVE_AVAILABLE or CQ_LIVE_UNAVAILABLE. Never needs a rebuild. */
284int cq_set_availability(CqCatalog *c, const CqKey *k, uint32_t live);
285/* Withdraw: the entry stays in the index but no query returns it again. */
286int cq_withdraw(CqCatalog *c, const CqKey *k);
287/* Build the (operation, source) index. Registration order does not matter. */
288int cq_catalog_build(CqCatalog *c);
289/* Digest of the operation catalog (tree, domains, aliases). Two nodes may
290 * exchange wire records only when their digests match (Fabric handshake). */
291void cq_ontology_digest(const CqCatalog *c, uint8_t out[32]);
292/* Alias or operation id -> canonical operation, 0 if unknown. */
293uint32_t cq_resolve(const CqCatalog *c, uint32_t name);
294/* Bytes the catalog holds (entries, index, descriptions). */
295uint64_t cq_catalog_bytes(const CqCatalog *c);
296
297/* ---- compile ---- */
298
299typedef struct {
300 int verdict; /* CQ_OK or CQ_E_* */
301 uint32_t op; /* canonical operation */
302 uint32_t n_ops;
303 uint32_t ops[CQ_MAX_PLAN_OPS]; /* op and its specializations, sorted */
304 uint32_t sources; /* sources probed */
305 uint32_t n_probes; /* (operation, source) buckets */
306 uint8_t digest[32]; /* independent of how the operation was named */
307} CqPlan;
308
309/* Need -> plan. Resolves aliases, expands specializations, and keeps only
310 * sources that can realize the operation and that the locality allows. */
311int cq_compile(const CqCatalog *c, const CqNeed *need, CqPlan *plan);
312
313/* ---- run ---- */
314
315/* What crosses into cognition. */
316typedef struct {
317 int32_t verdict;
318 uint32_t n; /* candidates returned */
319 uint32_t n_feasible; /* how many satisfied the need */
320 uint32_t n_front; /* how many no other feasible one beat */
321 CqCandidate cand[CQ_MAX_K];
322} CqResult;
323
324/* Bytes of r that reach cognition: the header and the returned candidates. */
325size_t cq_result_bytes(const CqResult *r);
326
327/* Optional: decide `held` through the world's authority view. */
328typedef struct {
329 const RxWorld *w;
330 const AgCapTable *caps; /* the principal's table */
331} CqHeld;
332
333typedef struct {
334 uint64_t probed; /* entries looked at */
335 uint64_t feasible;
336 uint64_t rejected_dead; /* withdrawn, dead session, expired lease */
337 uint64_t desc_bytes_read; /* always 0: the query never reads descriptions */
338 uint64_t rejected_unavailable; /* registered but CQ_LIVE_UNAVAILABLE */
339 uint64_t rejected_authority; /* require_held and not held */
340 uint64_t rejected_admission; /* refused by the caller's admit function */
341} CqStats;
342
343int cq_query(const CqCatalog *c, const CqPlan *plan, const CqNeed *need,
344 const CqTradeoffs *t, const CqHeld *held, uint64_t now_us,
345 CqResult *out, CqStats *stats);
346
347/* Admission before ranking: return nonzero to keep a feasible candidate. The
348 * Pareto front and the order are computed over the admitted set only, so a
349 * dominated but admissible provider is not lost behind an inadmissible one.
350 * The function reads; it must not change the catalog or mint authority. */
351typedef int (*CqAdmitFn)(void *ctx, const CqEntry *e, const CqCandidate *c);
352
353int cq_query_admit(const CqCatalog *c, const CqPlan *plan, const CqNeed *need,
354 const CqTradeoffs *t, const CqHeld *held, uint64_t now_us,
355 CqAdmitFn admit, void *admit_ctx, CqResult *out, CqStats *stats);
356
357/* The same ranking on a caller's set (used by the query; exposed for tests).
358 * Orders `in` into `out` (at most k) and returns how many, or CQ_E_NOMEM;
359 * `front` gets the size of the Pareto front over the named dimensions. */
360int cq_rank(const CqCandidate *in, uint32_t n, const CqTradeoffs *t,
361 CqCandidate *out, uint32_t k, uint32_t *front);
362
363/* Value to minimize on a dimension. */
364uint64_t cq_dim_value(const CqCandidate *c, uint32_t dim);
365
366/* ---- consumption by the action graph ---- */
367
368/* Point an AG_SKILL node at the chosen candidate's procedure and declare the
369 * authority it needs. Compilation then finds it held or reports it missing. */
370int cq_bind_skill_node(AgGraph *g, uint32_t node, const CqCandidate *c);
371
372const char *cq_source_name(uint32_t s);
373
374/* ---- wire form (Fabric advertisement of Capability Graph entries) ----
375 *
376 * One fixed-size record per entry, CQ_WIRE_BYTES long. Every multi-byte
377 * integer is little-endian; the machine is the 44-byte AienMachineId record,
378 * never an index. No description text, no pointer, no local index, no lease.
379 *
380 * off len field
381 * 0 4 magic "ACGR"
382 * 4 1 version 0x01
383 * 5 1 kind CQ_WIRE_ADVERTISE | CQ_WIRE_AVAILABILITY | CQ_WIRE_WITHDRAW
384 * 6 1 source the provider's source on its own machine (never FABRIC)
385 * 7 1 evidence_level CQ_EV_*
386 * 8 44 machine AienMachineId record (aien_machine_id.h)
387 * 52 4 capability_id
388 * 56 4 realization_id (the provider)
389 * 60 4 skill_id 0 = not a Skill
390 * 64 4 skill_version
391 * 68 32 skill_digest AgSkill.identity (zero = unpinned)
392 * 100 4 op semantic operation id of the shared operation catalog
393 * 104 2 effects CQ_FX_*
394 * 106 2 live CQ_LIVE_*
395 * 108 8 in_types
396 * 116 8 out_types
397 * 124 8 auth_resource authority REQUIRED on the provider's machine (not a grant)
398 * 132 4 auth_rights
399 * 136 4 confidence_ppm
400 * 140 4 reliability_ppm
401 * 144 8 cost
402 * 152 8 latency_us
403 * 160 8 energy_uj
404 * 168 8 evidence_ref
405 * 176 8 generation advertiser's revision of this entry; must increase
406 * 184 4 check first 4 bytes of SHA-256(bytes 0..183)
407 *
408 * Rules. Only a machine's own entries are exported (machine = self, source !=
409 * FABRIC): nobody re-advertises another machine's capability. Ingestion
410 * refuses records naming the receiver itself, records whose op the receiver's
411 * operation catalog does not know (the handshake compares cq_ontology_digest),
412 * and records whose generation is not newer than the one held. An ingested
413 * entry becomes source CQ_SRC_FABRIC on its machine's index; the lease is the
414 * receiver's (cq_machine_advertise_id from the Fabric session), not the
415 * record's. Authenticating the sender is Fabric's job; this code checks form.
416 */
417#define CQ_WIRE_BYTES 188u
418#define CQ_WIRE_VERSION 0x01u
419#define CQ_WIRE_ADVERTISE 1u /* register or update */
420#define CQ_WIRE_AVAILABILITY 2u /* `live` changed */
421#define CQ_WIRE_WITHDRAW 3u
422
423/* Export one of this machine's entries. */
424int cq_wire_encode(const CqCatalog *c, const CqEntry *e, uint32_t kind, uint8_t out[CQ_WIRE_BYTES]);
425/* Parse and check a record: no catalog change. out->machine_id is 0; the
426 * machine is returned in *machine. */
427int cq_wire_decode(const uint8_t *in, size_t len, uint32_t *kind, AienMachineId *machine,
428 CqEntry *out);
429/* Ingest a record from another machine into a canonical catalog. On success
430 * *key_out (optional) is the entry's local key. */
431int cq_wire_apply(CqCatalog *c, const uint8_t *in, size_t len, CqKey *key_out);
432
433#endif /* RX_CAPQ_H */