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_contract.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_contract.h -- Omega typed result constraints (OMEGA_TYPED_RESULT_CONSTRAINTS). |
| 19 | * |
| 20 | * When cognition is asked for a structured semantic object (a plan, a |
| 21 | * hypothesis, a capability need, an action graph, a skill invocation, a World |
| 22 | * mutation, an effect proposal, a generation candidate, an evidence record), |
| 23 | * the answer has an output contract. The contract is typed Omega state: typed |
| 24 | * fields and a table of rules, with a SHA-256 identity over a canonical |
| 25 | * binary encoding. JSON is one serialization of an object (rc_to_json / |
| 26 | * rc_from_json). It is not the contract, and a JSON text is only accepted |
| 27 | * after it has been parsed back into typed state and checked. |
| 28 | * |
| 29 | * Rule kinds: ranges, enums, structural invariants, cross-field relations, |
| 30 | * authority, generation, referential integrity, required evidence and effect |
| 31 | * restrictions. Field types are checked before any rule. |
| 32 | * |
| 33 | * Two paths to a published object: |
| 34 | * |
| 35 | * constrained the contract is compiled (rc_compile) into one allowed-value |
| 36 | * domain per field, in the contract's generation order, given |
| 37 | * the fields already chosen and the context. A backend that can |
| 38 | * decode under a mask (RcBackend.constrained) only ever chooses |
| 39 | * inside it. Rules that cannot become a per-field domain are |
| 40 | * checked once the candidate is complete. A domain that is |
| 41 | * empty ends the request before more is generated. |
| 42 | * |
| 43 | * free generate a candidate, check it, repair what the contract |
| 44 | * marks repairable, otherwise reject and generate again, up |
| 45 | * to a bound. |
| 46 | * |
| 47 | * Either way nothing reaches the World unchecked. The publish gate |
| 48 | * (rc_gate_register) is a resident reaction: it is the only holder of WRITE |
| 49 | * on the published object, it checks every draft again against the contract |
| 50 | * and the live World, repairs only derived fields and narrowing, and publishes |
| 51 | * the object or records the rejection. A producer that writes the published |
| 52 | * object itself is blocked by the authority. |
| 53 | * |
| 54 | * Repair never invents meaning. Only two repairs exist: recompute a field the |
| 55 | * contract marks as derived (a sequence number, a digest or count taken from |
| 56 | * an attached graph, an object's resource), and clear bits a rule says may |
| 57 | * not be set (asking for fewer rights). Stale generations, missing evidence, |
| 58 | * wrong principals, broken references and effect restrictions are rejected. |
| 59 | */ |
| 60 | #ifndef RX_CONTRACT_H |
| 61 | #define RX_CONTRACT_H |
| 62 | |
| 63 | #include "rx_graph.h" |
| 64 | #include "rx_world.h" |
| 65 | |
| 66 | #include <pthread.h> |
| 67 | #include <stdint.h> |
| 68 | |
| 69 | #define RC_MAX_FIELDS 8u |
| 70 | #define RC_MAX_RULES 24u |
| 71 | #define RC_MAX_SET 256u |
| 72 | #define RC_MAX_EVIDENCE 256u |
| 73 | #define RC_MAX_REFS 4u |
| 74 | #define RC_MAX_EFFECT_RES 8u |
| 75 | #define RC_NONE 0xffu /* no field */ |
| 76 | |
| 77 | typedef enum { |
| 78 | RC_PLAN = 1, |
| 79 | RC_HYPOTHESIS, |
| 80 | RC_CAPABILITY_NEED, |
| 81 | RC_ACTION_GRAPH, |
| 82 | RC_SKILL_INVOCATION, |
| 83 | RC_WORLD_MUTATION, |
| 84 | RC_EFFECT_PROPOSAL, |
| 85 | RC_GENERATION_CANDIDATE, |
| 86 | RC_EVIDENCE, |
| 87 | RC_KIND_COUNT |
| 88 | } RcKind; |
| 89 | |
| 90 | /* Field types. */ |
| 91 | typedef enum { |
| 92 | RC_T_U64 = 1, |
| 93 | RC_T_U32, /* <= UINT32_MAX */ |
| 94 | RC_T_BOOL, /* 0 or 1 */ |
| 95 | RC_T_ENUM, /* < 64 */ |
| 96 | RC_T_BITS, /* a rights word, <= UINT32_MAX */ |
| 97 | RC_T_OBJ, /* World object id, < RX_MAX_OBJECTS */ |
| 98 | RC_T_SUBJECT, /* principal, 1..UINT32_MAX */ |
| 99 | RC_T_RESOURCE, /* capability resource, nonzero */ |
| 100 | RC_T_DIGEST /* one 64-bit word of a SHA-256 */ |
| 101 | } RcType; |
| 102 | |
| 103 | /* Constraint types (RcRule.kind). */ |
| 104 | typedef enum { |
| 105 | RC_RULE_RANGE = 1, /* f[a] in [k, k2] */ |
| 106 | RC_RULE_ENUM, /* f[a] in the value set k (bit v = value v, v < 64) */ |
| 107 | RC_RULE_STRUCT, /* structural invariant, op RC_S_* */ |
| 108 | RC_RULE_CROSS, /* when guard holds: f[a] cmp (b == RC_NONE ? k : f[b] + k) */ |
| 109 | RC_RULE_AUTHORITY, /* op RC_A_* */ |
| 110 | RC_RULE_GENERATION, /* op RC_G_* */ |
| 111 | RC_RULE_REFERENCE, /* op RC_R_* */ |
| 112 | RC_RULE_EVIDENCE, /* a PASS evidence record of subject kind k names f[a] */ |
| 113 | RC_RULE_EFFECT, /* op RC_E_* */ |
| 114 | RC_RULE_KIND_COUNT |
| 115 | } RcRuleKind; |
| 116 | |
| 117 | /* Rule ops. */ |
| 118 | enum { RC_S_NONZERO = 1, /* f[a] != 0 */ |
| 119 | RC_S_BITS_WITHIN, /* f[a] & ~k == 0 (repair: narrow) */ |
| 120 | RC_S_ACTION_GRAPH, /* attached graph validates, belongs to f[2], evidence per effect */ |
| 121 | RC_S_GRAPH_FIELDS }; /* fields 0,1,3,4,6 are the graph's digest and counts (derived) */ |
| 122 | enum { RC_A_PRINCIPAL = 1, /* f[a] == the producing principal */ |
| 123 | RC_A_HOLDS, /* principal holds rights (b field, or k when b = NONE) on resource f[a] */ |
| 124 | RC_A_NO_PRIVILEGED }; /* f[a] & k == 0 (repair: narrow) */ |
| 125 | enum { RC_G_LIVE = 1, /* object f[a] is live at generation f[b] */ |
| 126 | RC_G_ACTIVE, /* f[a] == the active generation */ |
| 127 | RC_G_NEXT, /* f[a] > the active generation */ |
| 128 | RC_G_EPOCH }; /* f[a] == the authority epoch */ |
| 129 | enum { RC_R_CTX_FIELD = 1, /* f[a] == field k2 of context object k (b = 1: 0 also allowed) */ |
| 130 | RC_R_NEXT_SEQ, /* f[a] == the context's next sequence number (derived) */ |
| 131 | RC_R_SKILL, /* f[a] is a skill of the table */ |
| 132 | RC_R_ARITY, /* f[a] == arity of skill f[b] */ |
| 133 | RC_R_OBJECT, /* f[a] names a live World object */ |
| 134 | RC_R_RESOURCE_OF, /* f[a] == the resource of live object f[b] (derived) */ |
| 135 | RC_R_RESOURCE_LIVE, /* some live World object is guarded by resource f[a] */ |
| 136 | RC_R_CTX_VALUE }; /* f[a] == context value k (index into RcContext.values) */ |
| 137 | enum { RC_E_FORBIDDEN = 1, /* resource f[a] outside the forbidden range */ |
| 138 | RC_E_ALLOWED, /* resource f[a] is an effect resource of the context, not forbidden */ |
| 139 | RC_E_BUDGET, /* the context still has an effect left */ |
| 140 | RC_E_MAX, /* f[a] <= effects the context has left */ |
| 141 | RC_E_APPROVAL, /* when the guard holds, f[a] (an approval) != 0 */ |
| 142 | RC_E_GRAPH_CLEAR }; /* no requirement of the attached graph is forbidden */ |
| 143 | |
| 144 | /* Comparisons. */ |
| 145 | enum { RC_EQ = 1, RC_NE, RC_LT, RC_LE, RC_GT, RC_GE }; |
| 146 | |
| 147 | /* Guard modes of a CROSS rule (and an EFFECT approval rule). */ |
| 148 | enum { RC_ALWAYS = 0, RC_GUARD_IN = 1 /* f[g] < 64 and bit f[g] of kg */, |
| 149 | RC_GUARD_BITS = 2 /* f[g] & kg != 0 */ }; |
| 150 | |
| 151 | /* Repair policy of a rule. */ |
| 152 | enum { RC_FIX_NONE = 0, /* violation rejects */ |
| 153 | RC_FIX_DERIVE, /* recompute field a from the context / attached graph */ |
| 154 | RC_FIX_NARROW }; /* clear the offending bits of field a */ |
| 155 | |
| 156 | typedef struct { |
| 157 | uint8_t kind, op; |
| 158 | uint8_t a, b; /* fields (RC_NONE = unused / constant) */ |
| 159 | uint8_t g, gmode; /* guard field and mode */ |
| 160 | uint8_t cmp; /* RC_EQ.. for CROSS */ |
| 161 | uint8_t repair; |
| 162 | uint64_t k, k2, kg; |
| 163 | } RcRule; |
| 164 | |
| 165 | typedef struct { |
| 166 | const char *name; /* serialization key; not part of the identity */ |
| 167 | RcType type; |
| 168 | uint8_t derived; /* recomputed, never meaning-bearing */ |
| 169 | } RcFieldSpec; |
| 170 | |
| 171 | typedef struct { |
| 172 | RcKind kind; |
| 173 | const char *name; |
| 174 | uint32_t n_fields; |
| 175 | RcFieldSpec field[RC_MAX_FIELDS]; |
| 176 | uint8_t order[RC_MAX_FIELDS]; /* generation order */ |
| 177 | uint32_t n_rules; |
| 178 | RcRule rule[RC_MAX_RULES]; |
| 179 | uint32_t world_type; /* object type of the published object */ |
| 180 | uint8_t identity[32]; /* SHA-256 over the canonical contract */ |
| 181 | } RcContract; |
| 182 | |
| 183 | /* A semantic object: typed state. The graph is attached for ACTION_GRAPH. */ |
| 184 | typedef struct { |
| 185 | RcKind kind; |
| 186 | uint64_t f[RC_MAX_FIELDS]; |
| 187 | const AgGraph *graph; |
| 188 | } RcObject; |
| 189 | |
| 190 | /* CAPABILITY_NEED: effect classes a need may accept, and where it may run. */ |
| 191 | #define RC_EFFECT_CLASSES 0x3fu /* pure, read, write, external, irreversible, physical */ |
| 192 | #define RC_LOCALITIES 0x7u /* this core class, this machine, the Fabric */ |
| 193 | |
| 194 | /* Evidence subject kinds (EVIDENCE field 0). */ |
| 195 | enum { RC_EV_VERIFY = 1, RC_EV_GENERATION = 2, RC_EV_MEASURE = 3 }; |
| 196 | enum { RC_VERDICT_PASS = 1, RC_VERDICT_FAIL = 2 }; |
| 197 | |
| 198 | typedef struct { uint32_t subject_kind; uint64_t subject; uint32_t verdict; } RcEvidenceRecord; |
| 199 | |
| 200 | /* What rules are checked against. The evidence registry is append-only and |
| 201 | * is filled by the evidence gate when it publishes. */ |
| 202 | typedef struct { |
| 203 | RxWorld *w; |
| 204 | uint32_t principal; |
| 205 | const AgCapTable *caps; /* what the principal holds (read only) */ |
| 206 | uint64_t next_seq; |
| 207 | uint64_t active_generation, authority_epoch; |
| 208 | RxObjRef refs[RC_MAX_REFS]; /* context objects rules may refer to */ |
| 209 | uint64_t values[4]; /* context values rules may refer to */ |
| 210 | const AgSkillTable *skills; |
| 211 | uint32_t skill_arity[AG_MAX_SKILLS]; |
| 212 | uint32_t n_effect_res; |
| 213 | uint64_t effect_res[RC_MAX_EFFECT_RES]; |
| 214 | uint64_t forbid_lo, forbid_hi; /* hi 0 = nothing forbidden */ |
| 215 | uint32_t effects_used, max_effects; |
| 216 | uint32_t max_graph_nodes; |
| 217 | pthread_mutex_t mu; /* guards the evidence registry and effects_used */ |
| 218 | uint32_t n_evidence; |
| 219 | RcEvidenceRecord evidence[RC_MAX_EVIDENCE]; |
| 220 | } RcContext; |
| 221 | |
| 222 | void rc_context_init(RcContext *ctx, RxWorld *w, uint32_t principal, const AgCapTable *caps); |
| 223 | void rc_context_destroy(RcContext *ctx); |
| 224 | int rc_context_add_evidence(RcContext *ctx, uint32_t subject_kind, uint64_t subject, |
| 225 | uint32_t verdict); |
| 226 | |
| 227 | /* ---- contracts ---- */ |
| 228 | |
| 229 | /* The standard contract of `kind`; fills its identity. */ |
| 230 | int rc_contract_std(RcKind kind, RcContract *out); |
| 231 | void rc_contract_identify(RcContract *c); |
| 232 | const char *rc_kind_name(RcKind k); |
| 233 | const char *rc_rule_kind_name(RcRuleKind k); |
| 234 | |
| 235 | /* ---- checking ---- */ |
| 236 | |
| 237 | #define RC_TYPE_RULE 0xffffu /* violation of a field type, not of a rule */ |
| 238 | |
| 239 | typedef struct { |
| 240 | int ok; |
| 241 | uint32_t n; /* violations */ |
| 242 | struct { uint16_t rule; uint8_t field; uint8_t kind; } v[RC_MAX_RULES + RC_MAX_FIELDS]; |
| 243 | uint32_t kinds_violated; /* bit per RcRuleKind; bit 0 = a type */ |
| 244 | } RcVerdict; |
| 245 | |
| 246 | /* Every type and every rule, against the context. Pure: changes nothing. */ |
| 247 | int rc_check(const RcContract *c, const RcContext *ctx, const RcObject *o, RcVerdict *v); |
| 248 | |
| 249 | /* Apply the repairs the violated rules allow. Returns the number of fields |
| 250 | * changed, or -1 when a violation has no repair (the object must be |
| 251 | * rejected). Only derived fields and narrowing are ever touched. */ |
| 252 | int rc_repair(const RcContract *c, const RcContext *ctx, RcObject *o, const RcVerdict *v); |
| 253 | |
| 254 | /* ---- compiling for a constrained backend ---- */ |
| 255 | |
| 256 | typedef struct { |
| 257 | uint64_t lo, hi; |
| 258 | uint64_t bits; /* allowed bits (is_bits), else allowed values < 64 (has_enum) */ |
| 259 | uint8_t is_bits, has_enum, has_set, empty; |
| 260 | uint32_t n_set; |
| 261 | uint64_t set[RC_MAX_SET]; /* explicit allowed values (has_set) */ |
| 262 | uint64_t ne[RC_MAX_RULES]; /* values excluded */ |
| 263 | uint32_t n_ne; |
| 264 | uint8_t has_x; |
| 265 | uint64_t xlo, xhi; /* an excluded interval (has_x) */ |
| 266 | } RcDomain; |
| 267 | |
| 268 | typedef struct { |
| 269 | const RcContract *c; |
| 270 | uint8_t pos[RC_MAX_FIELDS]; /* position of each field in the order */ |
| 271 | uint8_t enforced[RC_MAX_RULES]; /* 1: folded into a domain; 0: checked after */ |
| 272 | uint32_t n_enforced, n_post; |
| 273 | } RcCompiled; |
| 274 | |
| 275 | /* Decide which rules become per-field domains. Context-free. */ |
| 276 | int rc_compile(const RcContract *c, RcCompiled *out); |
| 277 | |
| 278 | /* Allowed values of `field`, given the fields before it in the order |
| 279 | * (partial->f) and the context. */ |
| 280 | int rc_domain(const RcCompiled *p, const RcContext *ctx, const RcObject *partial, |
| 281 | uint32_t field, RcDomain *d); |
| 282 | int rc_domain_contains(const RcDomain *d, uint64_t v); |
| 283 | /* The allowed value nearest to v (a constrained decoder's fallback). */ |
| 284 | int rc_domain_pick(const RcDomain *d, uint64_t v, uint64_t *out); |
| 285 | |
| 286 | /* ---- producing ---- */ |
| 287 | |
| 288 | /* A cognitive backend. draw proposes one field value; when the backend is |
| 289 | * constrained, `dom` is the compiled domain and the value must lie in it. |
| 290 | * graph returns the graph an ACTION_GRAPH candidate attaches. */ |
| 291 | typedef struct { |
| 292 | const char *name; |
| 293 | int constrained; |
| 294 | uint64_t (*draw)(void *self, const RcContract *c, uint32_t attempt, uint32_t field, |
| 295 | const RcObject *partial, const RcDomain *dom); |
| 296 | const AgGraph *(*graph)(void *self, uint32_t attempt); |
| 297 | void (*begin)(void *self, uint32_t attempt); |
| 298 | void *self; |
| 299 | } RcBackend; |
| 300 | |
| 301 | enum { RC_OUT_VALID = 1, /* first candidate was valid */ |
| 302 | RC_OUT_REPAIRED, /* valid after repair */ |
| 303 | RC_OUT_RETRIED, /* valid after at least one rejected candidate */ |
| 304 | RC_OUT_REJECTED, /* retries exhausted: nothing to publish */ |
| 305 | RC_OUT_UNSATISFIABLE }; /* an empty domain: nothing could be valid; nothing generated past it */ |
| 306 | |
| 307 | typedef struct { |
| 308 | uint32_t candidates; /* complete candidates generated */ |
| 309 | uint32_t invalid; /* of which failed the contract as generated */ |
| 310 | uint32_t repairs; /* candidates repaired into validity */ |
| 311 | uint32_t retries; |
| 312 | uint32_t dead_ends; /* constrained: a later domain was empty (no lookahead) */ |
| 313 | uint32_t draws; /* field values drawn from the backend (compute) */ |
| 314 | uint32_t checks; /* rc_check calls */ |
| 315 | uint32_t kinds_violated; /* union over invalid candidates */ |
| 316 | uint64_t gen_ns, check_ns; |
| 317 | } RcStats; |
| 318 | |
| 319 | /* Generate -> check -> repair/reject -> retry, up to max_retries extra |
| 320 | * candidates. On RC_OUT_VALID/REPAIRED/RETRIED *out holds a candidate |
| 321 | * that passed rc_check; otherwise *out must not be published. */ |
| 322 | int rc_produce(const RcCompiled *p, const RcContext *ctx, const RcBackend *b, |
| 323 | uint32_t max_retries, RcObject *out, RcStats *st); |
| 324 | |
| 325 | /* ---- serialization ---- */ |
| 326 | |
| 327 | /* Canonical binary: kind (u32 LE), contract identity (32), fields (u64 LE). */ |
| 328 | size_t rc_encode(const RcContract *c, const RcObject *o, uint8_t *buf, size_t cap); |
| 329 | int rc_decode(const RcContract *c, const uint8_t *buf, size_t n, RcObject *o); |
| 330 | /* Object identity: SHA-256 of the canonical binary (graph digest folded in). */ |
| 331 | void rc_identity(const RcContract *c, const RcObject *o, uint8_t out[32]); |
| 332 | /* One JSON object: {"kind":"<name>","<field>":<uint>,...}. */ |
| 333 | int rc_to_json(const RcContract *c, const RcObject *o, char *buf, size_t cap); |
| 334 | /* Strict: exact keys, unsigned decimal integers, no duplicates, nothing else. |
| 335 | * Returns 0, or negative for a text that is not this contract's object. It |
| 336 | * does not check rules; rc_check does. */ |
| 337 | int rc_from_json(const RcContract *c, const char *text, RcObject *o); |
| 338 | |
| 339 | /* ---- the publish gate (a resident reaction) ---- */ |
| 340 | |
| 341 | #define RC_OT_DRAFT 0x5C00u /* + kind */ |
| 342 | #define RC_OT_STATUS 0x5C40u /* + kind */ |
| 343 | #define RC_OT_PUBLISH 0x5C80u /* + kind (default world_type) */ |
| 344 | |
| 345 | /* status: 0 drafts seen, 1 last verdict (RC_GATE_*), 2 first violated rule |
| 346 | * (RC_TYPE_RULE for a type), 3 violated kinds, 4 published, |
| 347 | * 5 rejected, 6 repaired, 7 last draft digest word */ |
| 348 | enum { RC_GATE_PUBLISHED = 1, RC_GATE_REPAIRED, RC_GATE_REJECTED }; |
| 349 | |
| 350 | typedef struct { |
| 351 | const RcContract *c; |
| 352 | RcContext *ctx; |
| 353 | RxObjRef draft, published, status; |
| 354 | uint32_t reaction; |
| 355 | uint32_t subject; |
| 356 | const AgGraph *volatile graph; /* graph attached to the current ACTION_GRAPH draft */ |
| 357 | } RcGate; |
| 358 | |
| 359 | /* Register the gate. subject: the gate's principal; caps: R on draft, RW on |
| 360 | * published and status (published must be writable by the gate only). */ |
| 361 | int rc_gate_register(RcGate *g, RxWorld *w, const RcContract *c, RcContext *ctx, |
| 362 | uint32_t subject, RxObjRef draft, RxCapRef draft_read, |
| 363 | RxObjRef published, RxCapRef published_rw, |
| 364 | RxObjRef status, RxCapRef status_rw); |
| 365 | |
| 366 | #endif /* RX_CONTRACT_H */ |