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_console.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 | * visor_console.h -- Omega Visor console / REPL (lane 2). |
| 19 | * |
| 20 | * The console parses a line, classifies it, dispatches it to a hook in |
| 21 | * VisorConsoleOps, and prints the result in a fixed shape. It never executes |
| 22 | * anything itself and carries no authority. Wiring of the hooks to the other |
| 23 | * lanes lives in tools/omega.c (visor_console_default_ops), so this module |
| 24 | * stays dependency-free. A NULL hook prints "not wired in this build". |
| 25 | * |
| 26 | * Output classes (every command has exactly one): |
| 27 | * inspection -- reads session / graph / files; changes nothing semantic |
| 28 | * pure-execution -- evaluates or runs pure code (no effects, no authority) |
| 29 | * simulation -- estimates / compares without executing |
| 30 | * effect-request -- would need authority; routed as an EffectIntent, NEVER executed here |
| 31 | * |
| 32 | * Human mode: results go to `out`; errors go to `err` as "error: <command>: <msg>". |
| 33 | * A successful pure-execution / simulation command result (not a source line, |
| 34 | * which prints only its value) is preceded by one tag line |
| 35 | * "[<class>]" on `out` (inspection results are untagged). An effect request |
| 36 | * prints on `err`, without the "error:" prefix: |
| 37 | * "EFFECT REQUEST: requires authority; routed via EffectIntent -> AEGIS/PHYSICS (not executed)[; reason: <r>]". |
| 38 | * Script mode echoes each non-empty line as "Ω> <line>" on `out` first. |
| 39 | * JSON mode: exactly one object per command on `out`, one line, no spaces, key order fixed: |
| 40 | * {"command":"<name>","status":"ok"|"error","class":"<class>","result":<json>,"error":<string>|null} |
| 41 | * `result` is null on error. JSON is a serialization of a view, never canonical bytes. |
| 42 | * Blank lines and '#' comments produce no output in either mode. |
| 43 | */ |
| 44 | #ifndef OMEGA_VISOR_CONSOLE_H |
| 45 | #define OMEGA_VISOR_CONSOLE_H |
| 46 | |
| 47 | #include <stdbool.h> |
| 48 | #include <stddef.h> |
| 49 | #include <stdio.h> |
| 50 | |
| 51 | #include "visor.h" |
| 52 | #include "visor_parse_command.h" |
| 53 | |
| 54 | typedef enum { |
| 55 | VISOR_CLASS_INSPECTION = 0, |
| 56 | VISOR_CLASS_PURE_EXECUTION = 1, |
| 57 | VISOR_CLASS_SIMULATION = 2, |
| 58 | VISOR_CLASS_EFFECT_REQUEST = 3 |
| 59 | } VisorClass; |
| 60 | |
| 61 | const char *visor_class_name(VisorClass c); /* "inspection", "pure-execution", ... */ |
| 62 | |
| 63 | /* Everything a view hook gets. `subject` is args[0] resolved via visor_resolve |
| 64 | * (NULL if the command takes no subject or none was given); `subject2` is |
| 65 | * args[1] resolved for `compare`. Raw tokens are in cmd->args. */ |
| 66 | typedef struct { |
| 67 | VisorSession *session; |
| 68 | const VisorCommand *cmd; |
| 69 | const VisorBinding *subject; |
| 70 | const VisorBinding *subject2; |
| 71 | bool json; /* true: write ONE JSON value; false: human text lines */ |
| 72 | const char *evidence_root; /* from --evidence-root, may be NULL */ |
| 73 | void *ctx; /* VisorConsoleOps.ctx */ |
| 74 | } VisorViewCall; |
| 75 | |
| 76 | /* View hook: write the result to `out` (JSON value in json mode, text otherwise). |
| 77 | * Return 0 on success; VISOR_VIEW_ERROR_WITH_OUTPUT (2) = error but still show |
| 78 | * what was written (e.g. a failed verify report); any other nonzero = error |
| 79 | * with a message in err (out is discarded). */ |
| 80 | #define VISOR_VIEW_ERROR_WITH_OUTPUT 2 |
| 81 | typedef int (*VisorViewFn)(const VisorViewCall *call, FILE *out, char *err, size_t errn); |
| 82 | |
| 83 | /* Source-line hook (`let ...`, `fn ...`, bare expressions). On success fill |
| 84 | * `out` (the result binding; kind NONE = nothing to remember) and `value_text` |
| 85 | * (what to print, e.g. "18"; may be empty) and return 0. The console then sets |
| 86 | * `_` via visor_set_last when out->kind != VISOR_BIND_NONE. Named bindings |
| 87 | * (`let x = ...`) are created by the hook itself, not by the console. */ |
| 88 | typedef int (*VisorEvalLineFn)(VisorSession *s, const char *line, VisorBinding *out, |
| 89 | char *value_text, size_t n, char *err, size_t errn); |
| 90 | |
| 91 | /* Effect classifier used by `run` before anything executes. Set *cls to |
| 92 | * VISOR_CLASS_PURE_EXECUTION or VISOR_CLASS_EFFECT_REQUEST and a one-line reason. */ |
| 93 | typedef int (*VisorClassifyFn)(const VisorViewCall *call, VisorClass *cls, |
| 94 | char *reason, size_t reasonn, char *err, size_t errn); |
| 95 | |
| 96 | typedef struct { |
| 97 | VisorViewFn inspect; /* inspect <x> inspection */ |
| 98 | VisorViewFn type_of; /* type <x> inspection */ |
| 99 | VisorViewFn id_of; /* id <x>; NULL = built-in id printer inspection */ |
| 100 | VisorViewFn graph_text; /* graph [x] inspection */ |
| 101 | VisorEvalLineFn eval_line; /* source lines pure-execution */ |
| 102 | VisorViewFn verify; /* verify <x> pure-execution */ |
| 103 | VisorViewFn machine; /* machine inspection */ |
| 104 | VisorViewFn realize; /* realize <x> [for current.machine] pure-execution */ |
| 105 | VisorViewFn cost; /* cost <x> simulation */ |
| 106 | VisorViewFn run_pure; /* run <x> [args..] after classify says pure pure-execution */ |
| 107 | VisorViewFn evidence; /* evidence [name] inspection */ |
| 108 | VisorViewFn world; /* world [cap] inspection */ |
| 109 | VisorClassifyFn effects_classify; /* gate for `run` (not a command itself) */ |
| 110 | VisorViewFn effects; /* effects [x] effect-request (listing, never executed) */ |
| 111 | VisorViewFn alternatives; /* alternatives <x> simulation */ |
| 112 | VisorViewFn compare; /* compare <a> <b> simulation */ |
| 113 | VisorViewFn why; /* why <x> inspection */ |
| 114 | void *ctx; /* passed through in VisorViewCall.ctx */ |
| 115 | } VisorConsoleOps; |
| 116 | |
| 117 | typedef struct { |
| 118 | VisorSession *session; |
| 119 | bool json; |
| 120 | FILE *out; |
| 121 | FILE *err; |
| 122 | bool quit; |
| 123 | int last_status; /* 0 ok, 1 error, for the last executed command */ |
| 124 | size_t error_count; /* errors since init */ |
| 125 | VisorConsoleOps ops; /* zeroed by init; install with visor_console_set_ops */ |
| 126 | const char *evidence_root; /* may be NULL */ |
| 127 | } VisorConsole; |
| 128 | |
| 129 | int visor_console_init(VisorConsole *c, VisorSession *s, FILE *out, FILE *err, bool json); |
| 130 | void visor_console_set_ops(VisorConsole *c, const VisorConsoleOps *ops); |
| 131 | |
| 132 | /* One line -> parse -> dispatch -> print. 0 ok, 1 command error. Never exits. */ |
| 133 | int visor_console_exec_line(VisorConsole *c, const char *line); |
| 134 | |
| 135 | /* Batch mode: each non-empty line echoed as "Ω> <line>" on out (human mode |
| 136 | * only), then its output. Stops after `quit`. Returns 0 if every command was |
| 137 | * ok, 1 otherwise. */ |
| 138 | int visor_console_run_script(VisorConsole *c, FILE *script); |
| 139 | |
| 140 | /* Interactive loop. Prints "Ω> " before each read when `in` is a tty and not |
| 141 | * in JSON mode. Stops at EOF or `quit`. Same return convention as run_script. */ |
| 142 | int visor_console_repl(VisorConsole *c, FILE *in); |
| 143 | |
| 144 | /* Implemented in tools/omega.c (the main), not in the console: fills the |
| 145 | * hooks with the other lanes' functions. Phase A: all NULL. */ |
| 146 | void visor_console_default_ops(VisorConsoleOps *ops); |
| 147 | |
| 148 | /* Write `s` as a JSON string literal (quotes + escapes). Exposed for hooks. */ |
| 149 | void visor_json_string(FILE *out, const char *s); |
| 150 | |
| 151 | #endif /* OMEGA_VISOR_CONSOLE_H */ |