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 / omega_resolve.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 | #ifndef OMEGA_RESOLVE_H |
| 18 | #define OMEGA_RESOLVE_H |
| 19 | |
| 20 | /* omega_resolve: the Verified Crumb resolver (VC1 stage 4, ADR 0029 Decisions 3 to 5, |
| 21 | * aien-protocols specs/verified-crumb/SPEC.md sections 5 to 8). |
| 22 | * |
| 23 | * ONE CHAIN, every link must hold, any failure REFUSES and no partial closure is ever returned: |
| 24 | * |
| 25 | * import NAME -> omega.lock line -> semantic_id -> Verified Crumb Store -> receipt check |
| 26 | * -> transitive closure (every dependency repeats the chain, minus the lock) |
| 27 | * |
| 28 | * Per node, in this order (SPEC 6; the first failure wins): |
| 29 | * 1 lock name has a lock line, else DEPENDENCY_NOT_PINNED |
| 30 | * 2 store omega_vcstore_get(semantic_id); absent or integrity failure: UNVERIFIED_DEPENDENCY |
| 31 | * 3 identity key == record id; source or IR recheck (below); lock receipt == record |
| 32 | * receipt_id, else RECEIPT_HASH_MISMATCH |
| 33 | * - kind a BOOTSTRAP record satisfies an import only when its id is a member of the |
| 34 | * pinned genesis set VC-GENESIS-1 (omega_genesis.h), else UNVERIFIED_DEPENDENCY |
| 35 | * (store code GENESIS_NOT_LISTED). Membership is the ONLY input: no flag, no |
| 36 | * environment variable, nothing read from the store. |
| 37 | * 4 taint capability "omega-dev.taint": TAINTED_ARTIFACT (both domains) |
| 38 | * 5 profile not in the table: UNKNOWN_VERIFIER_PROFILE; version below the minimum (or not a |
| 39 | * dotted number): VERIFIER_TOO_OLD |
| 40 | * 6 receipt absent: MISSING_RECEIPT; SPEC 5.1 rules 1,3,4,5,6 fail: RECEIPT_HASH_MISMATCH |
| 41 | * (rule 4 naming the semantic_id but another source digest: STALE_RECEIPT); |
| 42 | * then rule 2 (PASS, assertions pass, tier at least the profile minimum, never |
| 43 | * TEST_ONLY_TRUST): UNVERIFIED_DEPENDENCY |
| 44 | * 7 edges each dependency repeats 2 to 7; its contract_id must equal required_contract; |
| 45 | * a dependency already on the path: DEPENDENCY_CYCLE |
| 46 | * 9 closure the union, sorted ascending by semantic_id, with its digest |
| 47 | * Declaration check (before any store access): every import needs a lock line |
| 48 | * (DEPENDENCY_NOT_PINNED) and every lock line needs an import (UNDECLARED_IMPORT). |
| 49 | * |
| 50 | * DECISIONS (also in the PR body): |
| 51 | * - BOOTSTRAP records (the audited genesis set) carry an audit hash in receipt_id, not a |
| 52 | * receipt. A listed one skips steps 5 and 6 (the lock must still pin the audit hash). A |
| 53 | * BOOTSTRAP record may NOT depend on a VERIFIED record: the audited base must be closed over |
| 54 | * itself, so the resolver refuses it (UNVERIFIED_DEPENDENCY) and omega_resolve_admit_genesis |
| 55 | * refuses to insert it. A VERIFIED record may depend on BOOTSTRAP (the genesis set is where |
| 56 | * trust starts). The set is pinned in the source (omega_genesis.h, docs/osc/VC-GENESIS-1.md); |
| 57 | * changing it is a reviewed source change, never a runtime switch. |
| 58 | * - SOURCE/IR RECHECK (SPEC 6 step 3). In the build domain the source or IR store is |
| 59 | * MANDATORY: with no fetch_blob every node is refused (UNVERIFIED_DEPENDENCY). The blob must |
| 60 | * hash to source_or_ir_digest, and when the digest kind is IR the program id is recomputed |
| 61 | * with omega_program_compute_id (omega_program_ir.h) and must equal the record's semantic id. |
| 62 | * A record whose digest kind is OSC source cannot be recomputed to a program id here, so the |
| 63 | * build domain refuses it. The dev domain may omit the blob store (its output is tainted); |
| 64 | * when it supplies one the same checks run. |
| 65 | * - omega-dev and omega-build run the SAME checks. The domain never loosens a check (ADR 0029 |
| 66 | * Decision 4: flags cannot change what is legal, only the closure can). It labels the output: |
| 67 | * an omega-dev artifact is TAINTED, its identity (build id) commits to that, and the resolver |
| 68 | * refuses to insert it into the store or to treat it as a dependency. |
| 69 | * - There is no skip switch, no environment variable and no fallback in this file or in the |
| 70 | * compiler. A test greps for them. |
| 71 | * |
| 72 | * LIMITS (stated, not hidden): receipts are hashes, not signatures (see omega_receipt.h); the |
| 73 | * receipt binding (rule 4) ties semantic_id and source digest; contract_id is not recomputed |
| 74 | * from the IR (the edge check compares the stored contract ids only). */ |
| 75 | |
| 76 | #include <stddef.h> |
| 77 | #include <stdint.h> |
| 78 | |
| 79 | #include "omega_vcstore.h" |
| 80 | |
| 81 | /* Resolve refusals. 1..9 are ARCH-0029 Decision 9 in the ADR's order (same numbers as the |
| 82 | * reserved OMEGA_VCS_* codes), 10 is the VC1 addition of SPEC 6.1. */ |
| 83 | enum { |
| 84 | OMEGA_RES_OK = 0, |
| 85 | OMEGA_RES_UNVERIFIED_DEPENDENCY = 1, |
| 86 | OMEGA_RES_MISSING_RECEIPT, |
| 87 | OMEGA_RES_RECEIPT_HASH_MISMATCH, |
| 88 | OMEGA_RES_DEPENDENCY_NOT_PINNED, |
| 89 | OMEGA_RES_DEPENDENCY_CYCLE, |
| 90 | OMEGA_RES_STALE_RECEIPT, |
| 91 | OMEGA_RES_UNDECLARED_IMPORT, |
| 92 | OMEGA_RES_TAINTED_ARTIFACT, |
| 93 | OMEGA_RES_UNKNOWN_VERIFIER_PROFILE, |
| 94 | OMEGA_RES_VERIFIER_TOO_OLD, |
| 95 | /* not trust decisions */ |
| 96 | OMEGA_RES_BAD_ARGUMENT = 20, |
| 97 | OMEGA_RES_NOMEM |
| 98 | }; |
| 99 | const char *omega_resolve_code_name(int code); |
| 100 | |
| 101 | typedef struct { |
| 102 | int code; /* OMEGA_RES_* */ |
| 103 | int store_code; /* OMEGA_VCS_* when the store said no, else 0 */ |
| 104 | char subject[140]; /* import name, or hex semantic id */ |
| 105 | char message[240]; /* plain text */ |
| 106 | } OmegaResolveError; |
| 107 | |
| 108 | typedef enum { OMEGA_DOMAIN_BUILD = 1, OMEGA_DOMAIN_DEV = 2 } OmegaDomain; |
| 109 | const char *omega_domain_name(OmegaDomain d); |
| 110 | |
| 111 | /* ---- verifier profiles: profile name -> minimum verifier version and minimum receipt tier ---- */ |
| 112 | typedef struct { const char *name, *min_version, *min_tier; } OmegaVerifierProfile; |
| 113 | /* The static table the compiler uses (names and tiers match aien-closure profile_min_tier): |
| 114 | * host-v1 HOST_TEST, qemu-v1 QEMU, production-v1 PRODUCTION, each minimum version 1.0.0. |
| 115 | * There is no "accept any profile" entry. */ |
| 116 | const OmegaVerifierProfile *omega_resolve_default_profiles(size_t *n); |
| 117 | int omega_resolve_check_profile(const OmegaVerifierProfile *table, size_t n, const char *profile, |
| 118 | const char *version, OmegaResolveError *err); |
| 119 | |
| 120 | /* ---- omega.lock (SPEC 8) ---- */ |
| 121 | #define OMEGA_LOCK_MAX_ENTRIES 4096 |
| 122 | typedef struct { char name[129]; uint8_t semantic[32], receipt[32]; } OmegaLockEntry; |
| 123 | typedef struct { OmegaLockEntry *e; size_t n; } OmegaLock; |
| 124 | /* Fail-closed parser: any malformed lock is DEPENDENCY_NOT_PINNED. */ |
| 125 | int omega_lock_parse(const char *text, size_t len, OmegaLock *out, OmegaResolveError *err); |
| 126 | void omega_lock_free(OmegaLock *l); |
| 127 | const OmegaLockEntry *omega_lock_find(const OmegaLock *l, const char *name); |
| 128 | |
| 129 | /* ---- receipt and blob sources ---- */ |
| 130 | /* 0 = found (*bytes malloc'd, caller frees), 1 = absent, other = error (refused as missing) */ |
| 131 | typedef int (*OmegaReceiptFetch)(void *ctx, const uint8_t id[32], uint8_t **bytes, size_t *len); |
| 132 | typedef int (*OmegaBlobFetch)(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len); |
| 133 | /* ctx = const char *directory; reads <dir>/<64 lowercase hex>.json (receipts) or .blob (blobs) */ |
| 134 | int omega_receipt_dir_fetch(void *dir, const uint8_t id[32], uint8_t **bytes, size_t *len); |
| 135 | int omega_blob_dir_fetch(void *dir, const uint8_t digest[32], uint8_t **bytes, size_t *len); |
| 136 | |
| 137 | /* ---- the resolver ---- */ |
| 138 | typedef struct { |
| 139 | const OmegaVcStore *store; |
| 140 | OmegaDomain domain; |
| 141 | const OmegaVerifierProfile *profiles; /* NULL (with n_profiles 0): the default table */ |
| 142 | size_t n_profiles; |
| 143 | OmegaReceiptFetch fetch_receipt; /* required for any VERIFIED record */ |
| 144 | void *fetch_ctx; |
| 145 | OmegaBlobFetch fetch_blob; /* REQUIRED in the build domain (without it every node |
| 146 | is refused); optional in dev. SHA-256 of the blob named |
| 147 | by source_or_ir_digest must equal that digest, and an |
| 148 | IR blob must recompute to the semantic id */ |
| 149 | void *blob_ctx; |
| 150 | } OmegaResolver; |
| 151 | |
| 152 | typedef struct { uint8_t semantic_id[32], receipt_id[32], admission_kind; } OmegaClosureEntry; |
| 153 | typedef struct { |
| 154 | OmegaClosureEntry *entries; /* sorted ascending by semantic_id */ |
| 155 | size_t n; |
| 156 | uint8_t digest[32]; /* omega_closure_digest of entries */ |
| 157 | } OmegaClosure; |
| 158 | void omega_closure_free(OmegaClosure *c); |
| 159 | |
| 160 | /* SHA-256("OMEGA.CLOSURE.V1" 0x00 || u32be n || n x (semantic_id || receipt_id || kind byte)). */ |
| 161 | void omega_closure_digest(const OmegaClosureEntry *e, size_t n, uint8_t out[32]); |
| 162 | |
| 163 | /* Resolve the imports of one compilation. names may be empty (n_names 0): the declaration check |
| 164 | * still runs, so a lock with entries and no imports is UNDECLARED_IMPORT. 0 or a refusal code |
| 165 | * with *err filled; on refusal *out is empty (never partial). */ |
| 166 | int omega_resolve_imports(const OmegaResolver *r, const OmegaLock *lock, const char *const *names, |
| 167 | size_t n_names, OmegaClosure *out, OmegaResolveError *err); |
| 168 | |
| 169 | /* SPEC 5.1 for one record: the receipt named by vc->receipt_id against profile, semantic id, |
| 170 | * source digest, evidence root and the receipt ids of the record's dependencies (read from the |
| 171 | * store). BUILD also requires a clean-checkout receipt; DEV may inspect dirty |
| 172 | * evidence, with the normal DEV output taint. Steps 5 and 6 of the chain; |
| 173 | * exported for the admit path and tests. */ |
| 174 | int omega_resolve_check_record_receipt(const OmegaResolver *r, const OmegaVcView *vc, OmegaResolveError *err); |
| 175 | |
| 176 | /* ---- the one door into the store ---- */ |
| 177 | /* Build metadata of a compile: the domain, the taint mark and the identities. */ |
| 178 | typedef struct { |
| 179 | OmegaDomain domain; |
| 180 | uint8_t tainted; /* 1 exactly when domain is omega-dev */ |
| 181 | uint8_t ir_digest[32], closure_digest[32], build_id[32]; |
| 182 | } OmegaArtifactMeta; |
| 183 | |
| 184 | /* build_id = SHA-256("OSC1.BUILD.V1" 0x00 || ir_digest || domain byte || closure_digest). |
| 185 | * THE place the closure digest enters a build's identity: two builds of the same IR with |
| 186 | * different closures, or in different domains, have different build ids. */ |
| 187 | void omega_build_id(const uint8_t ir_digest[32], OmegaDomain d, const uint8_t closure_digest[32], uint8_t out[32]); |
| 188 | void omega_artifact_meta_make(OmegaArtifactMeta *m, OmegaDomain d, const uint8_t ir_digest[32], const uint8_t closure_digest[32]); |
| 189 | /* Text header, "OMEGA-ARTIFACT v1\ndomain dev\ntainted 1\nir_sha256 <hex>\nclosure_sha256 <hex>\nbuild_id <hex>\n". */ |
| 190 | int omega_artifact_meta_text(const OmegaArtifactMeta *m, char *buf, size_t cap); |
| 191 | /* Fail-closed parse: unknown line, wrong order, tainted mark inconsistent with the domain, or a |
| 192 | * build_id that does not recompute: refused (TAINTED_ARTIFACT for a mark/identity lie, |
| 193 | * BAD_ARGUMENT for malformed text). */ |
| 194 | int omega_artifact_meta_parse(const char *text, size_t len, OmegaArtifactMeta *m, OmegaResolveError *err); |
| 195 | |
| 196 | /* Insert a Verified Crumb into the store, only together with the receipt that qualifies it |
| 197 | * (THE only way a VERIFIED record enters a store; the raw insert is private to this file, see |
| 198 | * omega_vcstore_priv.h): |
| 199 | * origin (may be NULL when the record did not come from a compile) tainted: TAINTED_ARTIFACT; |
| 200 | * the record lists omega-dev.taint: TAINTED_ARTIFACT; then steps 5 and 6 above; then |
| 201 | * the private raw insert (VERIFIED). The store is unchanged on every refusal. */ |
| 202 | int omega_resolve_admit(const OmegaResolver *r, OmegaVcStore *s, const uint8_t *canonical, size_t len, |
| 203 | const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err); |
| 204 | /* Genesis loading: same taint refusals, no receipt (audit hash), refuses any VERIFIED |
| 205 | * dependency, and refuses any record whose semantic id is not a member of VC-GENESIS-1 |
| 206 | * (UNVERIFIED_DEPENDENCY with store code GENESIS_NOT_LISTED). THE only way a BOOTSTRAP record |
| 207 | * enters a store. */ |
| 208 | int omega_resolve_admit_genesis(OmegaVcStore *s, const uint8_t *canonical, size_t len, |
| 209 | const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err); |
| 210 | |
| 211 | #define OMEGA_TAINT_CAPABILITY "omega-dev.taint" |
| 212 | /* A record minted by omega_vc_bridge (no independent qualification run) lists this capability. The |
| 213 | * build-domain resolver refuses it, so a bridge record can sit in a store but can never satisfy a |
| 214 | * build import (VC1 stage 6 fix). The dev domain accepts it (the artifact is tainted anyway). */ |
| 215 | #define OMEGA_BRIDGE_SELFMINTED_CAPABILITY "omega-bridge.selfminted" |
| 216 | |
| 217 | #endif /* OMEGA_RESOLVE_H */ |