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 / visor / visor.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 | /* visor.c -- Omega Visor V1 shared session (lead-owned). No authority here. */ |
| 18 | #include "visor.h" |
| 19 | #include "omega_core.h" |
| 20 | #include "omega_canonical.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <ctype.h> |
| 26 | |
| 27 | static bool visor_name_ok(const char *name) { |
| 28 | if (!name || !*name) return false; |
| 29 | size_t n = strlen(name); |
| 30 | if (n >= VISOR_NAME_LEN) return false; |
| 31 | if (!(isalpha((unsigned char)name[0]) || name[0] == '_')) return false; |
| 32 | for (size_t i = 1; i < n; i++) { |
| 33 | if (!(isalnum((unsigned char)name[i]) || name[i] == '_' || name[i] == '.')) return false; |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | int visor_session_init(VisorSession *s) { |
| 39 | if (!s) return -1; |
| 40 | memset(s, 0, sizeof(*s)); |
| 41 | s->graph = omega_graph_create(); |
| 42 | if (!s->graph) return -1; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void visor_session_destroy(VisorSession *s) { |
| 47 | if (!s) return; |
| 48 | for (size_t i = 0; i < s->program_count; i++) omega_program_destroy(&s->programs[i]); |
| 49 | if (s->graph) omega_graph_destroy(s->graph); |
| 50 | memset(s, 0, sizeof(*s)); |
| 51 | } |
| 52 | |
| 53 | void visor_session_clear(VisorSession *s) { |
| 54 | if (!s) return; |
| 55 | OmegaMachineGraph keep = s->machine; |
| 56 | bool has_machine = s->has_machine, observed = s->machine_is_observed; |
| 57 | visor_session_destroy(s); |
| 58 | (void)visor_session_init(s); |
| 59 | s->machine = keep; |
| 60 | s->has_machine = has_machine; |
| 61 | s->machine_is_observed = observed; |
| 62 | } |
| 63 | |
| 64 | int visor_binding_set(VisorSession *s, const char *name, VisorBindKind kind, |
| 65 | const SemanticId *id, int index) { |
| 66 | if (!s || !visor_name_ok(name) || !id || kind == VISOR_BIND_NONE) return -1; |
| 67 | if (strcmp(name, "_") == 0) return -1; /* `_` is reserved for the last result */ |
| 68 | VisorBinding *slot = NULL; |
| 69 | for (size_t i = 0; i < s->bindings.count; i++) { |
| 70 | if (strcmp(s->bindings.items[i].name, name) == 0) { slot = &s->bindings.items[i]; break; } |
| 71 | } |
| 72 | if (!slot) { |
| 73 | if (s->bindings.count >= VISOR_MAX_BINDINGS) return -1; |
| 74 | slot = &s->bindings.items[s->bindings.count++]; |
| 75 | } |
| 76 | memset(slot, 0, sizeof(*slot)); |
| 77 | snprintf(slot->name, sizeof(slot->name), "%s", name); |
| 78 | slot->kind = kind; |
| 79 | slot->id = *id; |
| 80 | slot->index = (kind == VISOR_BIND_OBJECT) ? -1 : index; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | const VisorBinding *visor_binding_get(const VisorSession *s, const char *name) { |
| 85 | if (!s || !name) return NULL; |
| 86 | if (strcmp(name, "_") == 0) return s->has_last ? &s->last : NULL; |
| 87 | for (size_t i = 0; i < s->bindings.count; i++) { |
| 88 | if (strcmp(s->bindings.items[i].name, name) == 0) return &s->bindings.items[i]; |
| 89 | } |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | void visor_set_last(VisorSession *s, const VisorBinding *b) { |
| 94 | if (!s || !b) return; |
| 95 | s->last = *b; |
| 96 | snprintf(s->last.name, sizeof(s->last.name), "_"); |
| 97 | s->has_last = true; |
| 98 | } |
| 99 | |
| 100 | int visor_resolve(const VisorSession *s, const char *token, VisorBinding *out) { |
| 101 | if (!s || !token || !out || !*token) return -1; |
| 102 | const VisorBinding *b = visor_binding_get(s, token); |
| 103 | if (b) { *out = *b; return 0; } |
| 104 | |
| 105 | const char *hex = token; |
| 106 | if (strncmp(hex, "sha256:", 7) == 0) hex += 7; |
| 107 | if (strlen(hex) != 64) return -1; |
| 108 | SemanticId id; |
| 109 | if (omega_parse_hex_semantic_id(hex, &id) != 0) return -1; |
| 110 | |
| 111 | if (omega_graph_find_object_const(s->graph, &id)) { |
| 112 | memset(out, 0, sizeof(*out)); |
| 113 | snprintf(out->name, sizeof(out->name), "%.63s", token); |
| 114 | out->kind = VISOR_BIND_OBJECT; out->id = id; out->index = -1; |
| 115 | return 0; |
| 116 | } |
| 117 | for (size_t i = 0; i < s->program_count; i++) { |
| 118 | if (omega_compare_semantic_id(&s->programs[i].program_id, &id) == 0) { |
| 119 | memset(out, 0, sizeof(*out)); |
| 120 | snprintf(out->name, sizeof(out->name), "%.63s", token); |
| 121 | out->kind = VISOR_BIND_PROGRAM; out->id = id; out->index = (int)i; |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | for (size_t i = 0; i < s->realization_count; i++) { |
| 126 | if (omega_compare_semantic_id(&s->realizations[i].real.realization_id, &id) == 0) { |
| 127 | memset(out, 0, sizeof(*out)); |
| 128 | snprintf(out->name, sizeof(out->name), "%.63s", token); |
| 129 | out->kind = VISOR_BIND_REALIZATION; out->id = id; out->index = (int)i; |
| 130 | return 0; |
| 131 | } |
| 132 | } |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | int visor_program_add(VisorSession *s, const OmegaProgram *p) { |
| 137 | if (!s || !p || s->program_count >= VISOR_MAX_PROGRAMS) return -1; |
| 138 | s->programs[s->program_count] = *p; |
| 139 | return (int)s->program_count++; |
| 140 | } |
| 141 | |
| 142 | int visor_realization_add(VisorSession *s, const VisorRealizationEntry *e) { |
| 143 | if (!s || !e || s->realization_count >= VISOR_MAX_REALIZATIONS) return -1; |
| 144 | s->realizations[s->realization_count] = *e; |
| 145 | return (int)s->realization_count++; |
| 146 | } |
| 147 | |
| 148 | void visor_format_id(const SemanticId *id, char out[72]) { |
| 149 | char hex[65]; |
| 150 | if (!id) { snprintf(out, 72, "sha256:<null>"); return; } |
| 151 | omega_hex_semantic_id(id, hex); |
| 152 | snprintf(out, 72, "sha256:%s", hex); |
| 153 | } |
| 154 | |
| 155 | const char *visor_kind_name(SemanticKind k) { |
| 156 | switch (k) { |
| 157 | case KIND_VALUE: return "VALUE"; |
| 158 | case KIND_TYPE: return "TYPE"; |
| 159 | case KIND_OPERATION: return "OPERATION"; |
| 160 | case KIND_RELATION: return "RELATION"; |
| 161 | case KIND_CONSTRAINT: return "CONSTRAINT"; |
| 162 | case KIND_MEMORY: return "MEMORY"; |
| 163 | case KIND_MACHINE: return "MACHINE"; |
| 164 | case KIND_EFFECT: return "EFFECT"; |
| 165 | case KIND_REALIZATION: return "REALIZATION"; |
| 166 | case KIND_EVIDENCE: return "EVIDENCE"; |
| 167 | case KIND_PROOF: return "PROOF"; |
| 168 | default: return "INVALID"; |
| 169 | } |
| 170 | } |