interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / visor / visor_semantic.h

202 lines · 8511 bytes Raw Blame History
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 * visor_semantic.h -- Omega Visor V1, lane 1: read-only semantic inspection API.
19 *
20 * A stable, structured, READ-ONLY view over canonical objects stored in an
21 * OmegaGraph. The console, the --json output and any future GUI consume
22 * these views and never touch OmegaObject internals themselves.
23 *
24 * Contract:
25 * - const everywhere: nothing here mutates the graph or any object, never
26 * recomputes/stores an id, never mints or uses authority.
27 * - Fixed storage only (no heap). A VisorObjectView is roughly 30 KB; the
28 * caller owns it (stack, static or session storage).
29 * - Everything is derived from the canonical object (kind, payload,
30 * attributes, relations, constraints). No duplicated metadata.
31 * - Deterministic: fields and lists keep stored order; the same object always
32 * yields byte-identical text and JSON.
33 * - Return codes: 0 ok; -1 invalid argument / id not in graph / unsupported;
34 * -2 malformed object (reported in the view, its payload never trusted);
35 * -3 (eval only) the existing evaluator refused (overflow FAIL_CLOSED, div by 0).
36 *
37 * "Apply" objects: omega_build_apply stores an ApplyPayload under
38 * KIND_OPERATION. As in omega_validate.c, an OPERATION whose payload_len is
39 * >= sizeof(OperationPayload) is an operation; otherwise >= sizeof(ApplyPayload)
40 * is an apply; shorter is malformed. The view reports kind "OPERATION" and
41 * shape "APPLY" for those.
42 */
43#ifndef OMEGA_VISOR_SEMANTIC_H
44#define OMEGA_VISOR_SEMANTIC_H
45
46#include <stdbool.h>
47#include <stddef.h>
48#include <stdint.h>
49
50#include "omega_types.h"
51
52#define VISOR_SEM_TYPE_TEXT 96
53#define VISOR_SEM_REASON_LEN 128
54#define VISOR_SEM_NAME_LEN 24
55#define VISOR_SEM_MAX_DEPS (OMEGA_MAX_RELATIONS + 8)
56#define VISOR_SEM_EVAL_DEPTH 64
57#define VISOR_SEM_GRAPH_DEPTH 64
58
59typedef enum {
60 VISOR_SHAPE_NONE = 0,
61 VISOR_SHAPE_TYPE = 1,
62 VISOR_SHAPE_VALUE = 2,
63 VISOR_SHAPE_OPERATION = 3,
64 VISOR_SHAPE_APPLY = 4,
65 VISOR_SHAPE_OPAQUE = 5 /* any other kind: payload shown as length + hash only */
66} VisorShape;
67
68typedef struct {
69 char key[OMEGA_MAX_KEY_LEN];
70 uint16_t len;
71 uint8_t value[OMEGA_MAX_VAL_LEN];
72 bool is_utf8; /* every byte printable ASCII: formatters show it as text too */
73} VisorAttrView;
74
75typedef struct {
76 uint16_t kind;
77 char kind_name[VISOR_SEM_NAME_LEN];
78 SemanticId target;
79 bool resolved; /* target present in the graph */
80} VisorRelView;
81
82typedef struct {
83 uint16_t kind;
84 char kind_name[VISOR_SEM_NAME_LEN];
85 uint16_t len;
86 uint8_t payload[128];
87} VisorConstraintView;
88
89typedef struct {
90 SemanticId id;
91 char role[VISOR_SEM_NAME_LEN]; /* "rel:DEPENDS_ON", "type", "input0", "output", "op", "operand1", "elem" */
92 bool resolved;
93} VisorDepView;
94
95typedef struct VisorObjectView {
96 int status; /* 0 ok, -2 malformed */
97 char malformed_reason[VISOR_SEM_REASON_LEN];
98
99 SemanticId id;
100 char id_text[72]; /* "sha256:<64 hex>" */
101 uint8_t kind;
102 char kind_name[VISOR_SEM_NAME_LEN]; /* "VALUE", "OPERATION", ... */
103 VisorShape shape;
104 char shape_name[VISOR_SEM_NAME_LEN]; /* "TYPE", "VALUE", "OPERATION", "APPLY", "OPAQUE" */
105 char type_text[VISOR_SEM_TYPE_TEXT]; /* "u64", "bool", "bitvector<32>", "seq<u8,16>", "fn(u64,u64)->u64" */
106 bool has_type_id; /* VALUE type / OPERATION+APPLY result type */
107 SemanticId type_id;
108
109 /* TYPE */
110 uint8_t type_tag;
111 uint16_t type_width;
112 uint32_t type_length;
113 bool has_elem_type;
114 SemanticId elem_type;
115
116 /* VALUE */
117 uint16_t value_byte_len;
118 uint8_t value_bytes[64];
119 bool has_u64; /* decoded when integer/bool type and width <= 64 */
120 uint64_t value_u64;
121 bool value_is_bool;
122
123 /* OPERATION */
124 uint8_t opcode;
125 char opcode_name[VISOR_SEM_NAME_LEN];
126 uint8_t overflow;
127 char overflow_name[VISOR_SEM_NAME_LEN];
128 uint8_t arity;
129 SemanticId op_type_id;
130 SemanticId input_types[4];
131 SemanticId output_type;
132
133 /* APPLY */
134 SemanticId apply_op;
135 uint8_t operand_count;
136 SemanticId operands[4];
137
138 /* Attributes / relations / constraints, in stored order. */
139 uint16_t attr_count;
140 VisorAttrView attrs[OMEGA_MAX_ATTRIBUTES];
141 uint16_t rel_count;
142 VisorRelView rels[OMEGA_MAX_RELATIONS];
143 uint16_t const_count;
144 VisorConstraintView consts[OMEGA_MAX_CONSTRAINTS];
145
146 /* Canonical encoding (omega_canonical_encode) of the stored object. */
147 size_t canonical_len;
148 char canonical_sha256[65];
149 bool canonical_id_matches; /* sha256(encoding) == stored id */
150
151 /* Dependencies: relation targets (stored order), then payload ids; deduped, zero ids skipped. */
152 uint16_t dep_count;
153 VisorDepView deps[VISOR_SEM_MAX_DEPS];
154 uint16_t dangling_count; /* deps not present in the graph (reported, not an error) */
155} VisorObjectView;
156
157/* Inspect one object. 0 ok; -1 bad args or id not in graph; -2 malformed
158 * (out->status == -2, out->malformed_reason set, id/kind still filled). */
159int visor_semantic_inspect(const OmegaGraph *g, const SemanticId *id, VisorObjectView *out);
160
161/* Render a view. Returns bytes written (excluding NUL) or -1 if it does not fit
162 * (never a silently truncated result). Text is human; JSON is a view of the
163 * view (valid JSON, fixed key order), NOT canonical bytes. */
164int visor_semantic_format_text(const VisorObjectView *v, char *out, size_t n);
165int visor_semantic_format_json(const VisorObjectView *v, char *out, size_t n);
166
167/* Canonical type of a VALUE, result type of an OPERATION or APPLY.
168 * Either output may be NULL. 0 ok; -1 none (TYPE/opaque kinds, zero or
169 * unresolved type, missing id); -2 malformed. */
170int visor_semantic_type_of(const OmegaGraph *g, const SemanticId *id, SemanticId *out_type_id,
171 char *type_text, size_t n);
172
173/* Reachable subgraph from root, DFS pre-order following dependencies in view
174 * order, deduped, two-space indent per depth. Line: "<KIND|APPLY> sha256:<hex> <summary>".
175 * Unresolved targets print as "MISSING sha256:<hex>"; malformed objects as
176 * "MALFORMED sha256:<hex> <reason>" (not followed). Returns bytes written,
177 * -1 bad args / root missing / does not fit / depth > VISOR_SEM_GRAPH_DEPTH. */
178int visor_semantic_graph_text(const OmegaGraph *g, const SemanticId *root, char *out, size_t n);
179
180/* Reference evaluation of a pure graph: VALUE -> its u64; APPLY of a binary
181 * OPERATION -> omega_eval_pure_binary_uint(opcode, overflow, width, a, b)
182 * with width from the OPERATION's type (uint/bitvector/byte, 1..64), operands
183 * evaluated recursively and type-checked against the OPERATION's input types.
184 * Depth > VISOR_SEM_EVAL_DEPTH fails closed.
185 * 0 ok; -1 unsupported/invalid/missing; -2 malformed; -3 evaluator refused. */
186int visor_semantic_eval_u64(const OmegaGraph *g, const SemanticId *id, uint64_t *out);
187
188/* Parts of an APPLY. out_operands must hold 4 entries. 0 ok; -1 not an apply/missing; -2 malformed. */
189int visor_semantic_apply_parts(const OmegaGraph *g, const SemanticId *apply_id, SemanticId *out_op,
190 SemanticId *out_operands, size_t *out_count);
191
192/* u64 of a VALUE whose type is uint/bitvector/byte/bool with width <= 64. 0 ok; -1 otherwise; -2 malformed. */
193int visor_semantic_value_u64(const OmegaGraph *g, const SemanticId *value_id, uint64_t *out);
194
195/* Name helpers (static strings; "UNKNOWN" for out-of-range). */
196const char *visor_semantic_opcode_name(unsigned op);
197const char *visor_semantic_overflow_name(unsigned ov);
198const char *visor_semantic_relation_name(unsigned kind);
199const char *visor_semantic_constraint_name(unsigned kind);
200const char *visor_semantic_shape_name(VisorShape s);
201
202#endif /* OMEGA_VISOR_SEMANTIC_H */