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 / allen / allen_bind.c
| 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 | /* allen_bind.c -- see allen_bind.h. Pure functions over the subject object, |
| 18 | * a Cortex store and World references. No I/O except reading the object file. */ |
| 19 | #include "allen/allen_bind.h" |
| 20 | #include "omega_api_decl.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | int allen_load(const char *path, struct cs_subject *s, uint8_t id[32], const char **why) |
| 26 | { |
| 27 | static uint8_t buf[CC_MAX_OBJECT_BYTES + 1]; |
| 28 | FILE *f; |
| 29 | size_t n; |
| 30 | int rc; |
| 31 | if (!path || !s || !id) { |
| 32 | if (why) *why = "null argument"; |
| 33 | return -1; |
| 34 | } |
| 35 | f = fopen(path, "rb"); |
| 36 | if (!f) { |
| 37 | if (why) *why = "cannot open subject file"; |
| 38 | return -1; |
| 39 | } |
| 40 | n = fread(buf, 1, sizeof buf, f); |
| 41 | fclose(f); |
| 42 | if ((rc = cs_subject_decode(buf, n, s, why)) != CC_OK) return -1; |
| 43 | if (cs_subject_id(buf, n, id) != CC_OK) { |
| 44 | if (why) *why = "object id"; |
| 45 | return -1; |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int allen_lineage(const CxStore *cx, uint8_t out[32]) |
| 51 | { |
| 52 | const CxObject *o = cx ? cx_get(cx, 1) : NULL; |
| 53 | if (!o) { |
| 54 | memset(out, 0, 32); |
| 55 | return -1; |
| 56 | } |
| 57 | memcpy(out, o->digest, 32); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int allen_check_memory(const struct cs_subject *s, const CxStore *cx, const char **why) |
| 62 | { |
| 63 | uint8_t ref[32]; |
| 64 | int has_gen = (allen_lineage(cx, ref) == 0); |
| 65 | int64_t r = allen_check_lineage(s->cortex, ref, has_gen ? 1 : 0); |
| 66 | if (r == -1) { |
| 67 | if (why) *why = "subject has no Cortex lineage binding"; |
| 68 | return -1; |
| 69 | } |
| 70 | if (r == -2) { |
| 71 | if (why) *why = "Cortex journal holds no genesis record"; |
| 72 | return -1; |
| 73 | } |
| 74 | if (r == -3) { |
| 75 | if (why) *why = "Cortex lineage mismatch: this journal is not the subject's memory"; |
| 76 | return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void allen_bind_identity(const RxWorld *w, const struct cs_subject *s, const uint8_t id[32], |
| 82 | AllenBinding *out) |
| 83 | { |
| 84 | memset(out, 0, sizeof *out); |
| 85 | out->external_subject = w->external_subject; |
| 86 | memcpy(out->agent, s->agent, 32); |
| 87 | memcpy(out->subject_id, id, 32); |
| 88 | out->sequence = s->sequence; |
| 89 | } |
| 90 | |
| 91 | uint32_t allen_goal_mutations(const struct cs_intent *a, RxObjRef goal, uint64_t seq, |
| 92 | RxMutation out[4]) |
| 93 | { |
| 94 | if (!a || a->state != CS_ACTIVE || a->kind != CS_INTENT_GOAL_LATENCY) return 0; |
| 95 | uint64_t id_lo = allen_le64(a->id); |
| 96 | out[0] = (RxMutation){goal, 0, seq}; |
| 97 | out[1] = (RxMutation){goal, 1, a->payload[0]}; |
| 98 | out[2] = (RxMutation){goal, 2, a->payload[1]}; |
| 99 | out[3] = (RxMutation){goal, 3, id_lo}; |
| 100 | return 4; |
| 101 | } |
| 102 | |
| 103 | void allen_hex(const uint8_t *b, size_t n, char *out) |
| 104 | { |
| 105 | if (n) allen_encode_hex(b, n, (uint8_t *)out, 2 * n); |
| 106 | out[2 * n] = 0; |
| 107 | } |
| 108 | |
| 109 | int allen_unhex(const char *s, uint8_t *out, size_t n) |
| 110 | { |
| 111 | if (!s || strlen(s) != 2 * n) return -1; |
| 112 | if (!n) return 0; |
| 113 | return allen_decode_hex((const uint8_t *)s, 2 * n, out, n) ? 0 : -1; |
| 114 | } |