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 / research / atemporal / at1 / model / at1_main.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 | /* at1-model: command-line tool of the AT-1 candidate engine. |
| 18 | * |
| 19 | * at1-model result <case> <oracle-record|none> <provenance> [--reversed] |
| 20 | * writes the complete AT1_RESULT_V1 file to standard output. The reference_* lines are |
| 21 | * spliced verbatim from the oracle record (charter reading c); "none", or an oracle record |
| 22 | * that cannot be read, writes an ERROR |
| 23 | * result with error_code ORACLE_UNAVAILABLE. The provenance file holds the provenance |
| 24 | * lines (source_repo .. run_finished_utc, then optional artifact lines) so that the |
| 25 | * engine itself never reads a clock. |
| 26 | * at1-model values <case> [--reversed] |
| 27 | * writes the engine's own values lines only (component output, not a contract file). |
| 28 | * |
| 29 | * Exit status: 0 written; 2 case refused (one line "AT1_CASE_REFUSED <code>" on stderr); |
| 30 | * 1 engine error (one line "AT1_ENGINE_ERROR <reason>" on stderr), nothing on stdout. */ |
| 31 | #include "at1_model.h" |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | static int usage(void) |
| 38 | { |
| 39 | fputs("usage: at1-model result <case> <oracle-record|none> <provenance> [--reversed]\n" |
| 40 | " at1-model values <case> [--reversed]\n", stderr); |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | static int engine_error(const char *what) |
| 45 | { |
| 46 | fprintf(stderr, "AT1_ENGINE_ERROR %s\n", what); |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | static int emit(char *out, size_t len) |
| 51 | { |
| 52 | size_t w = fwrite(out, 1, len, stdout); |
| 53 | free(out); |
| 54 | if (w != len || fflush(stdout) != 0) return engine_error("write failed"); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int main(int argc, char **argv) |
| 59 | { |
| 60 | if (argc < 3) return usage(); |
| 61 | int result_mode = strcmp(argv[1], "result") == 0; |
| 62 | if (!result_mode && strcmp(argv[1], "values") != 0) return usage(); |
| 63 | int fixed = result_mode ? 5 : 3, reversed = 0; |
| 64 | if (argc == fixed + 1 && strcmp(argv[fixed], "--reversed") == 0) reversed = 1; |
| 65 | else if (argc != fixed) return usage(); |
| 66 | |
| 67 | uint8_t *case_bytes = NULL, *oracle = NULL, *prov = NULL; |
| 68 | size_t case_len = 0, oracle_len = 0, prov_len = 0; |
| 69 | at1_status st = at1_read_file(argv[2], &case_bytes, &case_len); |
| 70 | if (st == AT1_ERR_RESOURCE) return engine_error("RESOURCE_LIMIT case file larger than 1 MiB"); |
| 71 | if (st != AT1_OK) return engine_error("cannot read case file"); |
| 72 | /* the case is validated before any other input is read, so a refusal is never masked */ |
| 73 | at1_case c; |
| 74 | st = at1_case_parse(case_bytes, case_len, &c); |
| 75 | free(case_bytes); |
| 76 | if (st >= AT1_CASE_PARSE_ERROR && st <= AT1_CASE_ID_MISMATCH) { |
| 77 | fprintf(stderr, "AT1_CASE_REFUSED %s\n", at1_status_name(st)); |
| 78 | return 2; |
| 79 | } |
| 80 | if (st != AT1_OK) return engine_error(st == AT1_ERR_RESOURCE ? "RESOURCE_LIMIT engine limit in case file" : "INTERNAL_ERROR case parse"); |
| 81 | |
| 82 | /* an oracle record that cannot be read is a run error (ORACLE_UNAVAILABLE), not a tool failure; |
| 83 | * the provenance is required to write any result at all */ |
| 84 | if (result_mode) { |
| 85 | st = at1_read_file(argv[4], &prov, &prov_len); |
| 86 | if (st != AT1_OK) { at1_case_free(&c); return engine_error("cannot read provenance file"); } |
| 87 | if (strcmp(argv[3], "none") != 0 && at1_read_file(argv[3], &oracle, &oracle_len) != AT1_OK) { |
| 88 | free(oracle); oracle = NULL; oracle_len = 0; /* written as ERROR ORACLE_UNAVAILABLE */ |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | at1_kernel k; |
| 93 | at1_values *v = at1_xcalloc(1, sizeof *v); |
| 94 | const char *run_error = NULL; |
| 95 | st = at1_kernel_build(&c, &k); |
| 96 | if (st == AT1_OK) { |
| 97 | st = at1_compute(&c, &k, reversed, v); |
| 98 | at1_kernel_free(&k); |
| 99 | } |
| 100 | if (st == AT1_ERR_RESOURCE) run_error = "RESOURCE_LIMIT"; |
| 101 | else if (st != AT1_OK) run_error = "INTERNAL_ERROR"; |
| 102 | |
| 103 | char *out = NULL; size_t out_len = 0; char err[256]; |
| 104 | int rc; |
| 105 | if (!result_mode) { |
| 106 | if (run_error) rc = engine_error(run_error); |
| 107 | else { at1_values_write(&c, v, &out, &out_len); rc = emit(out, out_len); } |
| 108 | } else { |
| 109 | if (run_error) |
| 110 | st = at1_result_write_error(&c, run_error, (const char *)prov, prov_len, &out, &out_len, err, sizeof err); |
| 111 | else |
| 112 | st = at1_result_write(&c, v, (const char *)oracle, oracle_len, (const char *)prov, prov_len, &out, &out_len, err, sizeof err); |
| 113 | if (st == AT1_OK) rc = emit(out, out_len); |
| 114 | else if (st == AT1_ERR_ARGUMENT) rc = engine_error(err); |
| 115 | else rc = engine_error("INTERNAL_ERROR result writer"); |
| 116 | } |
| 117 | free(v); free(oracle); free(prov); |
| 118 | at1_case_free(&c); |
| 119 | return rc; |
| 120 | } |