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_semcomm.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_semcomm.h -- semantic communication pruning for J-space branches and |
| 19 | * swarms (OMEGA_SEMANTIC_COMMUNICATION). |
| 20 | * |
| 21 | * A branch or cognitive worker does not receive another worker's history or |
| 22 | * the whole World. It declares an InformationNeed: what operation it will |
| 23 | * perform, on which types and objects, over which part of time, with what |
| 24 | * evidence and what uncertainty. Omega answers with a SemanticProjection: |
| 25 | * the object references, fields, derived values and evidence references that |
| 26 | * operation needs, and nothing else. |
| 27 | * |
| 28 | * InformationNeed + World + receiver's capabilities |
| 29 | * -> rx_sem_project select, check authority, derive |
| 30 | * -> rx_sem_encode canonical bytes (one encoder for every mode) |
| 31 | * -> rx_sem_delta after the first delivery: only what changed |
| 32 | * |
| 33 | * Authority. Every object in a projection was validated for READ through the |
| 34 | * world's authority view at the time of projection, for the receiver's own |
| 35 | * principal. Fields a type marks private need READ on the object's private |
| 36 | * resource as well. A projection carries values and references. It carries |
| 37 | * no capability, so receiving one gives no write authority: a write still |
| 38 | * needs the writer's own capability, checked by the engine at start and at |
| 39 | * publication. This file takes no admin handle and never mints; the build |
| 40 | * checks its object file for that. |
| 41 | * |
| 42 | * Uncertainty. Omega does not estimate uncertainty. A type may declare one |
| 43 | * field as its producer's uncertainty (parts per million). A need may exclude |
| 44 | * objects above a bound and may ask for that field to be delivered. |
| 45 | * |
| 46 | * Time. The time of a field is the causal id of the crumb that last wrote it |
| 47 | * (rx_world_explain). A time range selects fields by that id. |
| 48 | * |
| 49 | * Evidence. An evidence reference is that writer crumb id, and on request |
| 50 | * the crumb's content digest, so a receiver can check it against the World. |
| 51 | */ |
| 52 | #ifndef RX_SEMCOMM_H |
| 53 | #define RX_SEMCOMM_H |
| 54 | |
| 55 | #include "rx_world.h" |
| 56 | |
| 57 | #include <stdbool.h> |
| 58 | #include <stddef.h> |
| 59 | #include <stdint.h> |
| 60 | |
| 61 | #define SC_MAX_TYPES 8u |
| 62 | #define SC_MAX_RELEVANT 64u |
| 63 | #define SC_MAX_CAPS 32u |
| 64 | #define SC_MAX_SCHEMA 16u |
| 65 | #define SC_MAX_DERIVED 4u |
| 66 | #define SC_MAX_FIELDS (RX_MAX_OBJECTS * RX_MAX_FIELDS) |
| 67 | |
| 68 | /* Private-field resource of an object guarded by `resource`. */ |
| 69 | #define SC_PRIVATE_BIT (1ull << 62) |
| 70 | static inline uint64_t rx_sem_private_resource(uint64_t resource) { return resource | SC_PRIVATE_BIT; } |
| 71 | |
| 72 | /* What the receiver will do with the state. It decides which fields travel |
| 73 | * and whether Omega sends a derived value instead of the inputs. */ |
| 74 | enum { |
| 75 | SC_OP_INSPECT = 1, /* needs the listed fields of every selected object */ |
| 76 | SC_OP_SUM, /* needs the sum of one field over the selected objects */ |
| 77 | SC_OP_MAX /* needs the largest value of one field and which object holds it */ |
| 78 | }; |
| 79 | |
| 80 | enum { SC_EV_NONE = 0, SC_EV_WRITER = 1, SC_EV_DIGEST = 2 }; |
| 81 | |
| 82 | /* Why an object or a field stopped being part of a receiver's projection. */ |
| 83 | enum { |
| 84 | SC_INV_RETIRED = 1, /* the object's generation moved */ |
| 85 | SC_INV_AUTHORITY, /* the receiver no longer holds READ */ |
| 86 | SC_INV_UNCERTAIN, /* its declared uncertainty now exceeds the bound */ |
| 87 | SC_INV_SCOPE /* out of the need's time range or type set */ |
| 88 | }; |
| 89 | |
| 90 | /* Message kinds. */ |
| 91 | enum { SC_MSG_FULL_STATE = 1, SC_MSG_PROJECTION = 2, SC_MSG_DELTA = 3 }; |
| 92 | |
| 93 | #define SC_OK 0 |
| 94 | #define SC_E_ARG -1 |
| 95 | #define SC_E_FULL -2 |
| 96 | #define SC_E_DECODE -3 |
| 97 | |
| 98 | /* Per-type facts Omega uses. Not a claim about meaning, only about layout. */ |
| 99 | typedef struct { |
| 100 | uint32_t type; |
| 101 | uint64_t private_mask; /* fields that need READ on the private resource */ |
| 102 | int32_t uncertainty_field; /* -1 = this type declares no uncertainty */ |
| 103 | bool conclusion; /* new objects of this type travel as conclusions */ |
| 104 | } RxSemType; |
| 105 | |
| 106 | typedef struct { |
| 107 | uint32_t n; |
| 108 | RxSemType t[SC_MAX_SCHEMA]; |
| 109 | } RxSemSchema; |
| 110 | |
| 111 | /* The receiver's own capabilities. Omega validates them; it does not add any. */ |
| 112 | typedef struct { |
| 113 | uint32_t subject; |
| 114 | uint32_t n; |
| 115 | struct { RxCapRef ref; uint64_t resource; uint32_t rights; } cap[SC_MAX_CAPS]; |
| 116 | } RxSemCaps; |
| 117 | |
| 118 | typedef struct { |
| 119 | uint32_t kind; /* SC_OP_* */ |
| 120 | uint64_t fields; /* INSPECT: field mask */ |
| 121 | uint32_t field; /* SUM, MAX: the field */ |
| 122 | } RxSemOperation; |
| 123 | |
| 124 | typedef struct { |
| 125 | uint32_t receiver; /* principal; must equal caps->subject */ |
| 126 | RxSemOperation operation; |
| 127 | uint32_t n_types; /* 0 = any type */ |
| 128 | uint32_t required_types[SC_MAX_TYPES]; |
| 129 | uint32_t n_objects; /* 0 = every object of the required types */ |
| 130 | RxObjRef relevant_objects[SC_MAX_RELEVANT]; |
| 131 | struct { uint64_t from_crumb, to_crumb; } relevant_time_range; /* to 0 = open */ |
| 132 | uint32_t evidence_requirement; /* SC_EV_* */ |
| 133 | struct { uint32_t max_ppm; bool deliver; } uncertainty_requirement; /* max 0 = any */ |
| 134 | } RxInformationNeed; |
| 135 | |
| 136 | typedef struct { |
| 137 | RxObjRef ref; |
| 138 | uint32_t type; |
| 139 | uint64_t resource; |
| 140 | uint64_t mask; /* fields delivered for this object */ |
| 141 | bool conclusion; |
| 142 | } RxSemObject; |
| 143 | |
| 144 | typedef struct { |
| 145 | uint16_t obj; /* index into object_refs */ |
| 146 | uint8_t field; |
| 147 | uint64_t value; |
| 148 | uint64_t version; |
| 149 | } RxSemField; |
| 150 | |
| 151 | typedef struct { |
| 152 | uint32_t op; /* SC_OP_SUM or SC_OP_MAX */ |
| 153 | uint32_t field; |
| 154 | uint64_t value; |
| 155 | RxObjRef arg; /* MAX: the object holding it; {0,0} for SUM */ |
| 156 | uint32_t n_inputs; |
| 157 | } RxSemDerived; |
| 158 | |
| 159 | typedef struct { |
| 160 | uint16_t obj; |
| 161 | uint8_t field; |
| 162 | uint64_t crumb; |
| 163 | uint8_t digest[32]; /* only filled for SC_EV_DIGEST */ |
| 164 | } RxSemEvidence; |
| 165 | |
| 166 | typedef struct { |
| 167 | uint32_t n_objects; |
| 168 | RxSemObject object_refs[RX_MAX_OBJECTS]; |
| 169 | uint32_t n_fields; |
| 170 | RxSemField fields[SC_MAX_FIELDS]; |
| 171 | uint32_t n_derived; |
| 172 | RxSemDerived derived_values[SC_MAX_DERIVED]; |
| 173 | uint32_t n_evidence; |
| 174 | RxSemEvidence evidence_refs[SC_MAX_FIELDS]; |
| 175 | uint32_t evidence_mode; |
| 176 | /* Observability, not delivered. */ |
| 177 | uint32_t withheld_authority; /* objects in scope the receiver may not read */ |
| 178 | uint32_t withheld_private; /* private fields withheld */ |
| 179 | uint32_t excluded_uncertain; |
| 180 | uint32_t validations; /* authority checks performed */ |
| 181 | } RxSemanticProjection; |
| 182 | |
| 183 | /* Compute the projection of the current World for `need`. `caps` must belong |
| 184 | * to need->receiver. Takes the world lock once; reads only. */ |
| 185 | int rx_sem_project(RxWorld *w, const RxSemSchema *schema, const RxInformationNeed *need, |
| 186 | const RxSemCaps *caps, RxSemanticProjection *out); |
| 187 | |
| 188 | /* ---- canonical encoding ---- |
| 189 | * Little-endian, fixed record layouts, objects in id order. The same records |
| 190 | * serve full-state broadcast, projections and deltas, so byte counts compare. */ |
| 191 | typedef struct { |
| 192 | uint8_t *buf; |
| 193 | size_t len; |
| 194 | size_t cap; |
| 195 | } RxSemBuf; |
| 196 | |
| 197 | void rx_sem_buf_reset(RxSemBuf *b); |
| 198 | void rx_sem_buf_free(RxSemBuf *b); |
| 199 | |
| 200 | /* Everything: every live object, every field with its version and writer. |
| 201 | * This is the broadcast baseline. */ |
| 202 | int rx_sem_encode_full_state(RxWorld *w, uint32_t receiver, uint64_t seq, RxSemBuf *out); |
| 203 | int rx_sem_encode_projection(const RxSemanticProjection *p, uint32_t receiver, uint64_t seq, |
| 204 | RxSemBuf *out); |
| 205 | |
| 206 | /* ---- delta delivery ---- |
| 207 | * The sender keeps, per receiver, what it last delivered. A delta carries |
| 208 | * changed relevant fields, new evidence, invalidations, new objects (tagged |
| 209 | * as conclusions where the type says so) and changed derived values. The |
| 210 | * cursor then equals the new projection. */ |
| 211 | typedef struct { |
| 212 | bool have; /* at least one delivery happened */ |
| 213 | bool present[RX_MAX_OBJECTS]; |
| 214 | uint32_t gen[RX_MAX_OBJECTS]; |
| 215 | uint64_t mask[RX_MAX_OBJECTS]; |
| 216 | uint64_t version[RX_MAX_OBJECTS][RX_MAX_FIELDS]; |
| 217 | uint64_t crumb[RX_MAX_OBJECTS][RX_MAX_FIELDS]; |
| 218 | uint32_t n_derived; |
| 219 | RxSemDerived derived[SC_MAX_DERIVED]; |
| 220 | } RxSemCursor; |
| 221 | |
| 222 | typedef struct { |
| 223 | uint32_t changed_fields, new_evidence, invalidated_objects, invalidated_fields; |
| 224 | uint32_t new_objects, new_conclusions, derived_changed; |
| 225 | } RxSemDeltaStats; |
| 226 | |
| 227 | /* Encode what the receiver lacks relative to `cur`, then advance `cur` to |
| 228 | * `p`. With no prior delivery this is the whole projection. `reason_of` |
| 229 | * explains an object that left (may be null: SC_INV_SCOPE). */ |
| 230 | typedef uint32_t (*RxSemReasonFn)(void *ctx, RxObjRef gone); |
| 231 | int rx_sem_delta(RxSemCursor *cur, const RxSemanticProjection *p, uint32_t receiver, |
| 232 | uint64_t seq, RxSemReasonFn reason_of, void *reason_ctx, RxSemBuf *out, |
| 233 | RxSemDeltaStats *st); |
| 234 | |
| 235 | /* Bytes of sender-side state one cursor represents (for memory accounting). */ |
| 236 | size_t rx_sem_cursor_bytes(const RxSemCursor *cur); |
| 237 | |
| 238 | /* ---- receiver ---- |
| 239 | * What a receiver holds after decoding. It applies full states, projections |
| 240 | * and deltas alike. */ |
| 241 | typedef struct { |
| 242 | bool present[RX_MAX_OBJECTS]; |
| 243 | uint32_t gen[RX_MAX_OBJECTS]; |
| 244 | uint32_t type[RX_MAX_OBJECTS]; |
| 245 | uint64_t resource[RX_MAX_OBJECTS]; |
| 246 | bool conclusion[RX_MAX_OBJECTS]; |
| 247 | uint64_t mask[RX_MAX_OBJECTS]; |
| 248 | uint64_t value[RX_MAX_OBJECTS][RX_MAX_FIELDS]; |
| 249 | uint64_t version[RX_MAX_OBJECTS][RX_MAX_FIELDS]; |
| 250 | uint64_t crumb[RX_MAX_OBJECTS][RX_MAX_FIELDS]; |
| 251 | uint64_t ev_mask[RX_MAX_OBJECTS]; /* fields with evidence held */ |
| 252 | uint8_t (*digest)[RX_MAX_FIELDS][32]; /* allocated on first digest record */ |
| 253 | uint32_t n_derived; |
| 254 | RxSemDerived derived[SC_MAX_DERIVED]; |
| 255 | uint64_t last_seq; |
| 256 | uint32_t invalidations_seen; |
| 257 | uint32_t conclusions_seen; |
| 258 | } RxSemView; |
| 259 | |
| 260 | void rx_sem_view_init(RxSemView *v); |
| 261 | void rx_sem_view_free(RxSemView *v); |
| 262 | /* Decode and apply one message. A full state or projection replaces the view; |
| 263 | * a delta edits it. */ |
| 264 | int rx_sem_view_apply(RxSemView *v, const uint8_t *msg, size_t len); |
| 265 | /* Bytes of state the view actually holds (objects, fields, evidence, derived). */ |
| 266 | size_t rx_sem_view_bytes(const RxSemView *v); |
| 267 | /* SHA-256 over the held content in canonical order. A view rebuilt from |
| 268 | * deltas and a view built from the fresh projection hash the same. */ |
| 269 | void rx_sem_view_digest(const RxSemView *v, uint8_t out[32]); |
| 270 | |
| 271 | #endif /* RX_SEMCOMM_H */ |