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 / tools / estimation / est3c_eval.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 | /* EST-3c protocol v3, sections 6 to 8: the sealed evaluator. |
| 18 | * |
| 19 | * est3c_eval --precheck --dir D |
| 20 | * Section 7 pre-check. Reads only t, loadavg, cpu, marks and schedule.txt |
| 21 | * (never thermal_mc). Prints VALID or INCONCLUSIVE with the numbers. |
| 22 | * est3c_eval --recorded --dir D2 --params P --outdir O [--synthetic-test] |
| 23 | * The one scored run (step 4). Refuses: dirty tree, absent compiled D2 |
| 24 | * identity (ndjson, marks and schedule.txt from d2.sha256), protocol doc |
| 25 | * changed, absent compiled params SHA or a params file whose SHA-256 |
| 26 | * differs from it (receipts/est3c-v3/params.txt at build time), O other |
| 27 | * than <repo>/docs/estimation/receipts/est3c-v3 (by realpath), params not |
| 28 | * a binding D1 fit, params PHASE_A_FAIL, a `selected` that the section 5 |
| 29 | * rule (c3_select on the params' screen results and log scores) does not |
| 30 | * reproduce, an existing receipt in O, a pre-check that is not VALID, D2 |
| 31 | * data whose SHA-256 differs from the compiled one. Scores S, F1 and E0 |
| 32 | * on the same steps (section 6), adds the informational block (S on A, B, |
| 33 | * C1), writes O/receipt-<sha256 of the receipt>.json, prints the verdict. |
| 34 | * --synthetic-test lifts only the dirty-tree, D1, compiled-D2, |
| 35 | * compiled-params and receipt-folder guards (never the selection check), |
| 36 | * refuses a held-out (D2) or D1 path, skips the informational block and |
| 37 | * marks the receipt synthetic_test true. |
| 38 | * est3c_eval --make-synthetic DIR [--seed N] [--lines N] [--foreign X] |
| 39 | * Writes a synthetic run folder (tests and smoke runs only). |
| 40 | */ |
| 41 | #include <dirent.h> |
| 42 | #include <errno.h> |
| 43 | #include <limits.h> |
| 44 | #include <math.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | #include <sys/stat.h> |
| 49 | |
| 50 | #include "est3c_common.h" |
| 51 | #include "sha256.h" |
| 52 | |
| 53 | #ifndef TOOL_COMMIT |
| 54 | #define TOOL_COMMIT "unknown" |
| 55 | #endif |
| 56 | #ifndef TOOL_DIRTY |
| 57 | #define TOOL_DIRTY 1 |
| 58 | #endif |
| 59 | #ifndef C3_PROTOCOL_SHA |
| 60 | #define C3_PROTOCOL_SHA "unknown" |
| 61 | #endif |
| 62 | /* From docs/estimation/receipts/est3c-v3/d2.sha256 at build time ("absent" when |
| 63 | * that file does not exist yet: --recorded then refuses). */ |
| 64 | #ifndef C3_D2_RAW_SHA |
| 65 | #define C3_D2_RAW_SHA "absent" |
| 66 | #endif |
| 67 | #ifndef C3_D2_MARKS_SHA |
| 68 | #define C3_D2_MARKS_SHA "absent" |
| 69 | #endif |
| 70 | #ifndef C3_D2_SCHED_SHA |
| 71 | #define C3_D2_SCHED_SHA "absent" |
| 72 | #endif |
| 73 | /* SHA-256 of docs/estimation/receipts/est3c-v3/params.txt at build time |
| 74 | * ("absent" before it is committed: --recorded then refuses). */ |
| 75 | #ifndef C3_PARAMS_SHA |
| 76 | #define C3_PARAMS_SHA "absent" |
| 77 | #endif |
| 78 | /* repository root at build time; the one receipt folder lives under it */ |
| 79 | #ifndef C3_REPO_ROOT |
| 80 | #define C3_REPO_ROOT "unknown" |
| 81 | #endif |
| 82 | #define C3_RECEIPT_SUBDIR "docs/estimation/receipts/est3c-v3" |
| 83 | |
| 84 | static int tool_dirty(void) |
| 85 | { |
| 86 | #ifdef C3_TEST_BUILD |
| 87 | const char *e = getenv("C3_TEST_DIRTY"); if (e && *e) return atoi(e); |
| 88 | return TOOL_DIRTY; |
| 89 | #else |
| 90 | /* measured once, at the first call (before any output file exists) */ |
| 91 | static int cached = -1; |
| 92 | if (cached < 0) cached = TOOL_DIRTY ? TOOL_DIRTY : c3_tree_dirty_now(); |
| 93 | return cached; |
| 94 | #endif |
| 95 | } |
| 96 | static const char *d2_raw_sha(void) |
| 97 | { |
| 98 | #ifdef C3_TEST_BUILD |
| 99 | const char *e = getenv("C3_TEST_D2_RAW_SHA"); if (e) return e; |
| 100 | #endif |
| 101 | return C3_D2_RAW_SHA; |
| 102 | } |
| 103 | static const char *d2_marks_sha(void) |
| 104 | { |
| 105 | #ifdef C3_TEST_BUILD |
| 106 | const char *e = getenv("C3_TEST_D2_MARKS_SHA"); if (e) return e; |
| 107 | #endif |
| 108 | return C3_D2_MARKS_SHA; |
| 109 | } |
| 110 | static const char *d2_sched_sha(void) |
| 111 | { |
| 112 | #ifdef C3_TEST_BUILD |
| 113 | const char *e = getenv("C3_TEST_D2_SCHED_SHA"); if (e) return e; |
| 114 | #endif |
| 115 | return C3_D2_SCHED_SHA; |
| 116 | } |
| 117 | static const char *params_sha(void) |
| 118 | { |
| 119 | #ifdef C3_TEST_BUILD |
| 120 | const char *e = getenv("C3_TEST_PARAMS_SHA"); if (e) return e; |
| 121 | #endif |
| 122 | return C3_PARAMS_SHA; |
| 123 | } |
| 124 | /* the only folder a recorded (non-synthetic) receipt may be written to */ |
| 125 | static int receipt_dir(char *out, size_t cap) |
| 126 | { |
| 127 | #ifdef C3_TEST_BUILD |
| 128 | const char *e = getenv("C3_TEST_RECEIPT_DIR"); |
| 129 | if (e) { snprintf(out, cap, "%s", e); return 0; } |
| 130 | #endif |
| 131 | if (!strcmp(C3_REPO_ROOT, "unknown")) return 1; |
| 132 | snprintf(out, cap, "%s/%s", C3_REPO_ROOT, C3_RECEIPT_SUBDIR); |
| 133 | return 0; |
| 134 | } |
| 135 | /* D1 folder whose SHA256SUMS (text only) proves the params' fit identity. */ |
| 136 | static const char *d1_dir(void) |
| 137 | { |
| 138 | #ifdef C3_TEST_BUILD |
| 139 | return getenv("C3_TEST_D1_DIR"); /* the test build never falls back to real D1 */ |
| 140 | #else |
| 141 | return C3_D1_DIR_DEFAULT; |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | static void print_pre(FILE *o, const c3_pre *p) |
| 146 | { |
| 147 | fprintf(o, "precheck lines %zu (min %u) %s\n", p->lines, C3_MIN_LINES, p->ok_lines ? "ok" : "FAIL"); |
| 148 | fprintf(o, "precheck gaps_above_1.5s %zu of %zu = %.4f (max 0.02) %s\n", p->gaps_big, p->gaps, p->gap_big_frac, p->ok_gaps ? "ok" : "FAIL"); |
| 149 | fprintf(o, "precheck schedule %s trials %zu segments %zu; marks %s (%s)\n", p->have_schedule ? "present" : "ABSENT", |
| 150 | p->sched_trials, p->sched_segments, p->ok_marks ? "ok" : "FAIL", p->marks_why); |
| 151 | fprintf(o, "precheck foreign_busy_cores mean %.4f (max 1.5) %s; over_3.0 %zu of %zu = %.4f (max 0.10) %s; unmeasured %zu %s\n", |
| 152 | p->foreign_mean, p->ok_foreign_mean ? "ok" : "FAIL", p->foreign_over3, p->foreign_n, p->foreign_over3_frac, |
| 153 | p->ok_foreign_tail ? "ok" : "FAIL", p->foreign_unmeasured, p->ok_foreign_measured ? "ok" : "FAIL"); |
| 154 | fprintf(o, "precheck loadavg1_mean %.3f bad_t %zu\n", p->loadavg1_mean, p->bad_t); |
| 155 | fprintf(o, "precheck raw_sha256 %s marks_sha256 %s schedule_sha256 %s\n", p->raw_sha, p->marks_sha, p->sched_sha); |
| 156 | fprintf(o, "%s: %s\n", p->valid ? "VALID" : "INCONCLUSIVE", p->why); |
| 157 | } |
| 158 | |
| 159 | static int has_receipt(const char *dir) |
| 160 | { |
| 161 | DIR *d = opendir(dir); |
| 162 | if (!d) return -1; |
| 163 | struct dirent *e; int found = 0; |
| 164 | while ((e = readdir(d))) if (!strncmp(e->d_name, "receipt-", 8)) found = 1; |
| 165 | closedir(d); |
| 166 | return found; |
| 167 | } |
| 168 | |
| 169 | static const char *class_hint(int s) |
| 170 | { |
| 171 | if (s < 0) return "none"; |
| 172 | if (s <= C3_ST_COV95 || (s >= C3_ST_PIT0 && s < C3_ST_BIAS)) return "wrong noise shape"; |
| 173 | if (s == C3_ST_BIAS) return "misspecification"; |
| 174 | if (s == C3_ST_LAG1 || s == C3_ST_TEN) return "dependence"; |
| 175 | if (s >= C3_ST_Q0 && s < C3_ST_REG_TRIAL) return "nonstationarity"; |
| 176 | if (s == C3_ST_REG_IDLE || s == C3_ST_REG_TRIAL) return "regime switching"; |
| 177 | return "sensor fault"; |
| 178 | } |
| 179 | |
| 180 | static int load_run(const char *dir, c3_ticks *tk, c3_marks *mk, char *raw_sha, char *err, size_t cap) |
| 181 | { |
| 182 | char path[1200]; |
| 183 | est_file f; |
| 184 | snprintf(path, sizeof path, "%s/machine-state.ndjson", dir); |
| 185 | if (est_file_load(path, &f, err, cap)) return 1; |
| 186 | memcpy(raw_sha, f.sha_hex, 65); |
| 187 | int r = c3_ticks_build(&f, tk, err, cap); |
| 188 | est_file_free(&f); |
| 189 | if (r) return 1; |
| 190 | snprintf(path, sizeof path, "%s/machine-state-marks.txt", dir); |
| 191 | if (c3_marks_load(path, 1, mk, err, cap)) { c3_ticks_free(tk); return 1; } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int recorded(const char *dir, const char *ppath, const char *outdir, int synth) |
| 196 | { |
| 197 | char err[512]; |
| 198 | if (!synth) { |
| 199 | if (tool_dirty()) { fprintf(stderr, "refuse: --recorded needs a clean working tree at build time (TOOL_DIRTY=%d)\n", tool_dirty()); return 2; } |
| 200 | if (!strcmp(d2_raw_sha(), "absent") || !strcmp(d2_marks_sha(), "absent") || !strcmp(d2_sched_sha(), "absent")) { |
| 201 | fprintf(stderr, "refuse: no D2 identity compiled in (docs/estimation/receipts/est3c-v3/d2.sha256 absent or incomplete at build time)\n"); return 2; } |
| 202 | char doc[65] = ""; |
| 203 | if (est_sha_file_hex(C3_PROTOCOL_DOC, doc) || strcmp(doc, C3_PROTOCOL_SHA)) { |
| 204 | fprintf(stderr, "refuse: %s SHA-256 on disk differs from the compiled protocol SHA %s\n", C3_PROTOCOL_DOC, C3_PROTOCOL_SHA); return 2; } |
| 205 | if (!strcmp(params_sha(), "absent")) { |
| 206 | fprintf(stderr, "refuse: no params SHA-256 compiled in (%s/params.txt absent at build time)\n", C3_RECEIPT_SUBDIR); return 2; } |
| 207 | char ph0[65] = ""; |
| 208 | if (est_sha_file_hex(ppath, ph0) || strcmp(ph0, params_sha())) { |
| 209 | fprintf(stderr, "refuse: params file SHA-256 %s differs from the compiled params SHA %s\n", ph0[0] ? ph0 : "unreadable", params_sha()); return 2; } |
| 210 | char want[PATH_MAX + 64], rw[PATH_MAX], ro[PATH_MAX]; |
| 211 | if (receipt_dir(want, sizeof want) || !realpath(want, rw) || !realpath(outdir, ro) || strcmp(rw, ro)) { |
| 212 | fprintf(stderr, "refuse: a recorded receipt goes only to <repo>/%s (outdir %s)\n", C3_RECEIPT_SUBDIR, outdir); return 2; } |
| 213 | } else if (c3_path_is_heldout(dir) || strstr(dir, C3_D1_ID)) { |
| 214 | fprintf(stderr, "refuse: --synthetic-test never runs on D1 or a held-out run\n"); return 2; |
| 215 | } |
| 216 | static c3_params P; |
| 217 | if (c3_params_read(ppath, &P, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 2; } |
| 218 | if (!P.phase_a_pass) { fprintf(stderr, "refuse: params say PHASE_A_FAIL; D2 is not scored\n"); return 2; } |
| 219 | /* section 5 selection recomputed from the params' own screen results and log scores */ |
| 220 | int resel = c3_select(P.have, P.screen_pass, P.logscore); |
| 221 | if (resel != P.selected || P.selected < 1 || P.screen_pass[P.selected] != 1) { |
| 222 | fprintf(stderr, "refuse: params say selected %s but the selection rule on their screen results gives %s\n", |
| 223 | c3_name(P.selected), resel ? c3_name(resel) : "none"); return 2; } |
| 224 | if (!synth) { |
| 225 | const char *d1 = d1_dir(); |
| 226 | char h1[65], h2[65]; |
| 227 | if (P.synthetic != 0 || P.tool_dirty != 0) { fprintf(stderr, "refuse: params are not a binding fit (synthetic_test %d tool_dirty %d)\n", P.synthetic, P.tool_dirty); return 2; } |
| 228 | if (strcmp(P.protocol_sha, C3_PROTOCOL_SHA)) { fprintf(stderr, "refuse: params were fitted under another protocol SHA\n"); return 2; } |
| 229 | if (!strstr(P.fit_raw_path, C3_D1_ID) || !strstr(P.fit_marks_path, C3_D1_ID)) { fprintf(stderr, "refuse: params are not a D1 fit (path)\n"); return 2; } |
| 230 | if (!d1 || est_sums_lookup(d1, "machine-state.ndjson", h1) || est_sums_lookup(d1, "machine-state-marks.txt", h2) |
| 231 | || strcmp(h1, P.fit_raw_sha) || strcmp(h2, P.fit_marks_sha)) { |
| 232 | fprintf(stderr, "refuse: params are not a D1 fit (SHA-256 differs from D1 SHA256SUMS)\n"); return 2; } |
| 233 | } |
| 234 | int hr = has_receipt(outdir); |
| 235 | if (hr < 0) { fprintf(stderr, "refuse: outdir %s is not a readable directory\n", outdir); return 2; } |
| 236 | if (hr > 0) { fprintf(stderr, "refuse: %s already holds a receipt; a second scored run is a new protocol version\n", outdir); return 2; } |
| 237 | c3_pre pre; |
| 238 | if (c3_precheck(dir, &pre, err, sizeof err)) { fprintf(stderr, "refuse: precheck: %s\n", err); return 2; } |
| 239 | print_pre(stdout, &pre); |
| 240 | if (!pre.valid) { fprintf(stderr, "refuse: the pre-check is INCONCLUSIVE; D2 is not scored\n"); return 3; } |
| 241 | if (!synth && (strcmp(pre.raw_sha, d2_raw_sha()) || strcmp(pre.marks_sha, d2_marks_sha()) || strcmp(pre.sched_sha, d2_sched_sha()))) { |
| 242 | fprintf(stderr, "refuse: D2 data SHA-256 differs from the compiled D2 identity\n"); return 2; } |
| 243 | |
| 244 | /* scored run */ |
| 245 | c3_ticks tk; static c3_marks mk; char raw_sha[65]; |
| 246 | if (load_run(dir, &tk, &mk, raw_sha, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 2; } |
| 247 | static c3_model mS, m1, m0; |
| 248 | if (c3_model_from_params(&P, P.selected, &mS) || c3_model_from_params(&P, 1, &m1) || c3_model_from_params(&P, 0, &m0)) { |
| 249 | fprintf(stderr, "refuse: parameter file models do not validate\n"); c3_ticks_free(&tk); return 2; } |
| 250 | static c3_stats sS, s1, s0; |
| 251 | if (c3_score(&tk, &mk, &mS, 1, &sS) || c3_score(&tk, &mk, &m1, 1, &s1) || c3_score(&tk, &mk, &m0, 1, &s0)) { |
| 252 | fprintf(stderr, "scoring error: %s%s%s\n", sS.errmsg, s1.errmsg, s0.errmsg); c3_ticks_free(&tk); return 1; } |
| 253 | int same = sS.n == s1.n && sS.n == s0.n && sS.unscorable == s1.unscorable && sS.unscorable == s0.unscorable; |
| 254 | int beat1 = sS.logscore >= s1.logscore - C3_TIE_NATS, beat0 = sS.logscore >= s0.logscore - C3_TIE_NATS; |
| 255 | /* est-json-1: a non-finite measured value is recorded as invalid and can never pass */ |
| 256 | size_t inv = c3_stats_invalid(&sS, C3_TEN_ABSENT, 0) + c3_stats_invalid(&s1, C3_TEN_ABSENT, 0) + c3_stats_invalid(&s0, C3_TEN_ABSENT, 0); |
| 257 | int pass = inv == 0 && same && sS.calibrated && beat1 && beat0; |
| 258 | const char *verdict = pass ? "PASS" : "FAIL"; |
| 259 | const char *reason = inv ? "non-finite measured value (marked invalid in the receipt)" : !same ? "S, F1 and E0 were not scored on the same steps" |
| 260 | : !sS.calibrated ? "S not calibrated" |
| 261 | : (!beat1 || !beat0) ? "calibrated but not better than a baseline" : "calibrated and no worse than F1 and E0"; |
| 262 | |
| 263 | /* informational block (not gating) */ |
| 264 | static c3_stats si[3]; int si_ok[3] = { 0, 0, 0 }; char si_err[3][256]; char si_sha[3][65]; |
| 265 | const char *dev[3] = { C3_DEV_A, C3_DEV_B, C3_DEV_C1 }; |
| 266 | const char *devn[3] = { "A", "B", "C1" }; |
| 267 | if (!synth) { |
| 268 | for (int i = 0; i < 3; i++) { |
| 269 | c3_ticks t2; static c3_marks m2; |
| 270 | si_err[i][0] = 0; snprintf(si_sha[i], 65, "unavailable"); |
| 271 | est_allow_run_b = (i == 1); /* B is spent (v1 held-out); read here only as information */ |
| 272 | if (load_run(dev[i], &t2, &m2, si_sha[i], si_err[i], sizeof si_err[i])) { est_allow_run_b = 0; continue; } |
| 273 | est_allow_run_b = 0; |
| 274 | if (c3_score(&t2, &m2, &mS, 1, &si[i]) == 0) si_ok[i] = 1; else snprintf(si_err[i], sizeof si_err[i], "%s", si[i].errmsg); |
| 275 | c3_ticks_free(&t2); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | char ph[65] = "unavailable"; |
| 280 | est_sha_file_hex(ppath, ph); |
| 281 | char *buf = NULL; size_t blen = 0; |
| 282 | FILE *o = open_memstream(&buf, &blen); |
| 283 | if (!o) { fprintf(stderr, "out of memory\n"); return 1; } |
| 284 | fprintf(o, "{\n \"receipt\": \"est3c-v3\",\n \"protocol\": \"%s\",\n \"protocol_sha256\": \"%s\",\n", C3_PROTOCOL_DOC, C3_PROTOCOL_SHA); |
| 285 | fprintf(o, " \"tool_commit\": \"%s\",\n \"tool_dirty\": %d,\n \"synthetic_test\": %s,\n", TOOL_COMMIT, tool_dirty(), synth ? "true" : "false"); |
| 286 | fprintf(o, " \"params_path\": "); c3_json_str(o, ppath); |
| 287 | fprintf(o, ",\n \"params_sha256\": \"%s\",\n \"fit_raw_sha256\": \"%s\",\n \"fit_marks_sha256\": \"%s\",\n", ph, P.fit_raw_sha, P.fit_marks_sha); |
| 288 | fprintf(o, " \"d2_dir\": "); c3_json_str(o, dir); |
| 289 | fprintf(o, ",\n \"d2_raw_sha256\": \"%s\",\n \"d2_marks_sha256\": \"%s\",\n \"d2_schedule_sha256\": \"%s\",\n", pre.raw_sha, pre.marks_sha, pre.sched_sha); |
| 290 | fprintf(o, " \"d2_compiled_raw_sha256\": \"%s\",\n \"d2_compiled_marks_sha256\": \"%s\",\n \"d2_compiled_schedule_sha256\": \"%s\",\n \"params_compiled_sha256\": \"%s\",\n", d2_raw_sha(), d2_marks_sha(), d2_sched_sha(), params_sha()); |
| 291 | fprintf(o, " \"precheck\": {\"verdict\": \"VALID\", \"lines\": %zu, \"gap_big_frac\": ", pre.lines); c3_json_num(o, pre.gap_big_frac); |
| 292 | fprintf(o, ", \"foreign_mean\": "); c3_json_num(o, pre.foreign_mean); |
| 293 | fprintf(o, ", \"foreign_over3_frac\": "); c3_json_num(o, pre.foreign_over3_frac); |
| 294 | fprintf(o, ", \"foreign_unmeasured\": %zu, \"sched_trials\": %zu, \"loadavg1_mean\": ", pre.foreign_unmeasured, pre.sched_trials); c3_json_num(o, pre.loadavg1_mean); |
| 295 | fprintf(o, "},\n"); |
| 296 | fprintf(o, " \"ticks\": {\"total\": %zu, \"inserted\": %zu, \"bad_t\": %zu, \"absent_value\": %zu, \"unparsable_value\": %zu},\n", |
| 297 | tk.n, tk.inserted, tk.bad_t, tk.absent_value, tk.unparsable_value); |
| 298 | fprintf(o, " \"selected\": \"%s\",\n \"same_steps\": %s,\n \"scores\": {\n", c3_name(P.selected), same ? "true" : "false"); |
| 299 | c3_json_stats(o, "S", &sS, C3_TEN_ABSENT, 0); fprintf(o, ",\n"); c3_json_stats(o, "F1", &s1, C3_TEN_ABSENT, 0); fprintf(o, ",\n"); c3_json_stats(o, "E0", &s0, C3_TEN_ABSENT, 0); |
| 300 | fprintf(o, "\n },\n \"json_rules\": \"est-json-1\",\n \"invalid_measurements\": %zu,\n \"comparison\": {\"S_minus_F1\": ", inv); |
| 301 | c3_json_num(o, sS.logscore - s1.logscore); fprintf(o, ", \"S_minus_E0\": "); c3_json_num(o, sS.logscore - s0.logscore); |
| 302 | fprintf(o, ", \"tolerance_nats\": %.2f, \"no_worse_than_F1\": %s, \"no_worse_than_E0\": %s},\n", C3_TIE_NATS, beat1 ? "true" : "false", beat0 ? "true" : "false"); |
| 303 | fprintf(o, " \"informational\": {"); |
| 304 | if (synth) fprintf(o, "\"skipped\": \"synthetic test\""); |
| 305 | else for (int i = 0; i < 3; i++) { |
| 306 | fprintf(o, "%s\n \"%s\": {\"dir\": \"%s\", \"raw_sha256\": \"%s\", ", i ? "," : "", devn[i], dev[i], si_sha[i]); |
| 307 | if (si_ok[i]) { |
| 308 | fprintf(o, "\"n\": %zu, \"mean_log_score\": ", si[i].n); c3_json_num(o, si[i].logscore); |
| 309 | fprintf(o, ", \"calibrated\": %s, \"first_fail\": \"%s\", \"cov95\": ", si[i].calibrated ? "true" : "false", |
| 310 | si[i].calibrated ? "none" : c3_stat_name(si[i].first_fail)); |
| 311 | c3_json_num(o, si[i].value[C3_ST_COV95]); fprintf(o, "}"); |
| 312 | } |
| 313 | else { fprintf(o, "\"error\": "); c3_json_str(o, si_err[i]); fprintf(o, "}"); } |
| 314 | } |
| 315 | fprintf(o, "},\n \"verdict\": \"%s\",\n \"verdict_reason\": \"%s\",\n", verdict, reason); |
| 316 | fprintf(o, " \"first_failing_statistic\": "); |
| 317 | if (sS.calibrated) fprintf(o, "null"); else c3_json_str(o, c3_stat_name(sS.first_fail)); |
| 318 | fprintf(o, ",\n \"failure_class_hint\": \"%s\",\n \"failure_class\": null\n}\n", sS.calibrated ? "none" : class_hint(sS.first_fail)); |
| 319 | if (fclose(o)) { free(buf); return 1; } |
| 320 | uint8_t dg[32]; char hx[65]; |
| 321 | sha256_hash((const uint8_t *)buf, blen, dg); |
| 322 | est_hex(dg, 32, hx); |
| 323 | char rp[1400]; |
| 324 | snprintf(rp, sizeof rp, "%s/receipt-%s.json", outdir, hx); |
| 325 | FILE *w = fopen(rp, "wbx"); |
| 326 | if (!w || fwrite(buf, 1, blen, w) != blen || fclose(w)) { fprintf(stderr, "cannot write %s: %s\n", rp, strerror(errno)); free(buf); return 1; } |
| 327 | free(buf); |
| 328 | printf("S=%s n=%zu logscore S %.6f F1 %.6f E0 %.6f; S calibrated %d first_fail %s; width80 mean/median S %.1f/%.1f F1 %.1f/%.1f E0 %.1f/%.1f mC\n", |
| 329 | c3_name(P.selected), sS.n, sS.logscore, s1.logscore, s0.logscore, sS.calibrated, |
| 330 | sS.calibrated ? "none" : c3_stat_name(sS.first_fail), sS.width_mean, sS.width_median, s1.width_mean, s1.width_median, s0.width_mean, s0.width_median); |
| 331 | printf("receipt %s\n", rp); |
| 332 | printf("ESTIMATION_CALIBRATION (v3) = %s%s\n", verdict, synth ? " (synthetic test, not a result)" : ""); |
| 333 | c3_stats_free(&sS); c3_stats_free(&s1); c3_stats_free(&s0); |
| 334 | for (int i = 0; i < 3; i++) if (si_ok[i]) c3_stats_free(&si[i]); |
| 335 | c3_ticks_free(&tk); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | #ifdef C3_AS_LIB |
| 340 | #define C3_MAIN c3_eval_main |
| 341 | int c3_eval_main(int argc, char **argv); |
| 342 | #else |
| 343 | #define C3_MAIN main |
| 344 | #endif |
| 345 | |
| 346 | int C3_MAIN(int argc, char **argv) |
| 347 | { |
| 348 | const char *dir = NULL, *params = NULL, *outdir = NULL, *mkdir_s = NULL; |
| 349 | int pre = 0, rec = 0, synth = 0; |
| 350 | c3_synth sy = { 1, 2400, 0.0, 0, 0, 0, 0 }; |
| 351 | for (int i = 1; i < argc; i++) { |
| 352 | if (!strcmp(argv[i], "--precheck")) pre = 1; |
| 353 | else if (!strcmp(argv[i], "--recorded")) rec = 1; |
| 354 | else if (!strcmp(argv[i], "--synthetic-test")) synth = 1; |
| 355 | else if (!strcmp(argv[i], "--dir") && i + 1 < argc) dir = argv[++i]; |
| 356 | else if (!strcmp(argv[i], "--params") && i + 1 < argc) params = argv[++i]; |
| 357 | else if (!strcmp(argv[i], "--outdir") && i + 1 < argc) outdir = argv[++i]; |
| 358 | else if (!strcmp(argv[i], "--make-synthetic") && i + 1 < argc) mkdir_s = argv[++i]; |
| 359 | else if (!strcmp(argv[i], "--seed") && i + 1 < argc) sy.seed = strtoull(argv[++i], NULL, 0); |
| 360 | else if (!strcmp(argv[i], "--lines") && i + 1 < argc) sy.lines = (size_t)strtoull(argv[++i], NULL, 10); |
| 361 | else if (!strcmp(argv[i], "--foreign") && i + 1 < argc) sy.foreign = atof(argv[++i]); |
| 362 | else { fprintf(stderr, "usage: est3c_eval --precheck --dir D | --recorded --dir D2 --params P --outdir O [--synthetic-test] | --make-synthetic DIR [--seed N] [--lines N] [--foreign X]\n"); return 2; } |
| 363 | } |
| 364 | if (mkdir_s) { |
| 365 | if (c3_path_is_heldout(mkdir_s) || strstr(mkdir_s, "evidence/")) { fprintf(stderr, "refuse: synthetic data never goes under evidence/\n"); return 2; } |
| 366 | if (mkdir(mkdir_s, 0755) && errno != EEXIST) { fprintf(stderr, "cannot create %s\n", mkdir_s); return 1; } |
| 367 | if (c3_synth_write(mkdir_s, &sy)) { fprintf(stderr, "synthetic write failed\n"); return 1; } |
| 368 | printf("synthetic run written to %s (%zu lines, seed %llu)\n", mkdir_s, sy.lines, (unsigned long long)sy.seed); |
| 369 | return 0; |
| 370 | } |
| 371 | if (pre + rec != 1 || !dir) { fprintf(stderr, "est3c_eval: exactly one of --precheck / --recorded, and --dir, are required\n"); return 2; } |
| 372 | if (pre) { |
| 373 | char err[512]; c3_pre p; |
| 374 | if (c3_precheck(dir, &p, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 2; } |
| 375 | print_pre(stdout, &p); |
| 376 | return p.valid ? 0 : 3; |
| 377 | } |
| 378 | if (!params || !outdir) { fprintf(stderr, "est3c_eval --recorded needs --params and --outdir\n"); return 2; } |
| 379 | return recorded(dir, params, outdir, synth); |
| 380 | } |